|
@@ -0,0 +1,124 @@
|
|
|
+/**
|
|
|
+ * Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
|
|
|
+ * <p>
|
|
|
+ * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
|
|
|
+ * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
|
|
|
+ * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
|
|
|
+ * This notice may not be removed from this file.
|
|
|
+ */
|
|
|
+
|
|
|
+package com.compdfkit.flutter.compdfkit_flutter.ui.reader.platformview;
|
|
|
+
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.content.MutableContextWrapper;
|
|
|
+import android.view.View;
|
|
|
+
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+import androidx.fragment.app.FragmentActivity;
|
|
|
+import androidx.fragment.app.FragmentContainerView;
|
|
|
+
|
|
|
+import com.compdfkit.tools.common.pdf.CPDFConfigurationUtils;
|
|
|
+import com.compdfkit.tools.common.pdf.CPDFDocumentFragment;
|
|
|
+import com.compdfkit.tools.common.pdf.config.CPDFConfiguration;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import io.flutter.plugin.common.BinaryMessenger;
|
|
|
+import io.flutter.plugin.platform.PlatformView;
|
|
|
+
|
|
|
+public class CPDFViewCtrlFlutter implements PlatformView {
|
|
|
+
|
|
|
+ private FragmentContainerView fragmentContainerView;
|
|
|
+
|
|
|
+ private CPDFDocumentFragment documentFragment;
|
|
|
+
|
|
|
+ private BinaryMessenger binaryMessenger;
|
|
|
+
|
|
|
+ private int viewId;
|
|
|
+
|
|
|
+ public CPDFViewCtrlFlutter(Context context, BinaryMessenger binaryMessenger, int viewId, Map<String, Object> creationParams) {
|
|
|
+ this.binaryMessenger = binaryMessenger;
|
|
|
+ this.viewId = viewId;
|
|
|
+ // Initialize CPDFViewCtrl and initialize related configuration information
|
|
|
+ initCPDFViewCtrl(context, creationParams);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initCPDFViewCtrl(Context context, Map<String, Object> creationParams) {
|
|
|
+ fragmentContainerView = new FragmentContainerView(context);
|
|
|
+ fragmentContainerView.setId(View.generateViewId());
|
|
|
+ String filePath = (String) creationParams.get("document");
|
|
|
+ String password = (String) creationParams.get("password");
|
|
|
+ CPDFConfiguration configuration = CPDFConfigurationUtils.normalConfig(context, "tools_default_configuration.json");
|
|
|
+
|
|
|
+ documentFragment = CPDFDocumentFragment.newInstance(filePath, password, configuration);
|
|
|
+
|
|
|
+ fragmentContainerView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
|
|
|
+ @Override
|
|
|
+ public void onViewAttachedToWindow(@NonNull View v) {
|
|
|
+ FragmentActivity fragmentActivity = getFragmentActivity(context);
|
|
|
+ if (fragmentActivity != null) {
|
|
|
+ fragmentActivity.getSupportFragmentManager()
|
|
|
+ .beginTransaction()
|
|
|
+ .add(fragmentContainerView.getId(), documentFragment)
|
|
|
+ .setReorderingAllowed(true)
|
|
|
+ .commitNow();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onViewDetachedFromWindow(@NonNull View v) {
|
|
|
+ FragmentActivity fragmentActivity = getFragmentActivity(context);
|
|
|
+ if (fragmentActivity != null) {
|
|
|
+ fragmentActivity.getSupportFragmentManager()
|
|
|
+ .beginTransaction()
|
|
|
+ .remove(documentFragment)
|
|
|
+ .setReorderingAllowed(true)
|
|
|
+ .commit();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Nullable
|
|
|
+ @Override
|
|
|
+ public View getView() {
|
|
|
+ return fragmentContainerView;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFlutterViewAttached(@NonNull View flutterView) {
|
|
|
+ PlatformView.super.onFlutterViewAttached(flutterView);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFlutterViewDetached() {
|
|
|
+ PlatformView.super.onFlutterViewDetached();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void dispose() {
|
|
|
+ fragmentContainerView = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onInputConnectionLocked() {
|
|
|
+ PlatformView.super.onInputConnectionLocked();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onInputConnectionUnlocked() {
|
|
|
+ PlatformView.super.onInputConnectionUnlocked();
|
|
|
+ }
|
|
|
+
|
|
|
+ private FragmentActivity getFragmentActivity(Context context) {
|
|
|
+ if (context instanceof FragmentActivity){
|
|
|
+ return (FragmentActivity) context;
|
|
|
+ } else if (context instanceof MutableContextWrapper) {
|
|
|
+ return getFragmentActivity(((MutableContextWrapper) context).getBaseContext());
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|