Prechádzať zdrojové kódy

ComPDFKit(flutter) - 调整部分初始化代码

ComPDFKit-Youna 10 mesiacov pred
rodič
commit
c50d2d86f8

+ 0 - 1
android/src/main/java/com/compdfkit/flutter/compdfkit_flutter/CompdfkitFlutterPlugin.java

@@ -34,7 +34,6 @@ public class CompdfkitFlutterPlugin implements FlutterPlugin, ActivityAware {
         mMessenger = flutterPluginBinding.getBinaryMessenger();
         mRegistry = flutterPluginBinding.getPlatformViewRegistry();
         new ComPDFKitSDKPlugin(flutterPluginBinding.getApplicationContext(), mMessenger, mRegistry);
-
     }
 
     @Override

+ 5 - 7
android/src/main/java/com/compdfkit/flutter/compdfkit_flutter/platformview/CPDFViewCtrlFlutter.java

@@ -36,6 +36,7 @@ import io.flutter.plugin.platform.PlatformView;
 
 public class CPDFViewCtrlFlutter implements PlatformView, MethodChannel.MethodCallHandler {
 
+    public static final String LOG_TAG = "ComPDFKit-Plugin";
     private FragmentContainerView fragmentContainerView;
 
     private CPDFDocumentFragment documentFragment;
@@ -50,17 +51,14 @@ public class CPDFViewCtrlFlutter implements PlatformView, MethodChannel.MethodCa
         this.binaryMessenger = binaryMessenger;
         this.viewId = viewId;
 
-
         // Initialize CPDFViewCtrl and initialize related configuration information
-        Log.e("ComPDFKit_Flutter", "Android创建:CPDFViewCtrl");
+        Log.e(LOG_TAG, "CPDFViewCtrlFlutter:Create CPDFDocumentFragment");
         initCPDFViewCtrl(context, creationParams);
 
-
         String channelName = "com.compdfkit.flutter.ui.pdfviewer."+ viewId;
-        Log.e("ComPDFKit_Flutter", "Android创建:methodChannel: "+ channelName);
+        Log.e(LOG_TAG, "CPDFViewCtrlFlutter: create MethodChannel:"+ channelName);
         methodChannel = new MethodChannel(binaryMessenger, channelName);
         methodChannel.setMethodCallHandler(this);
-
     }
 
 
@@ -144,13 +142,13 @@ public class CPDFViewCtrlFlutter implements PlatformView, MethodChannel.MethodCa
 
     @Override
     public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
-        Log.e("ComPDFKit_Flutter", "Android MethodCall:" + call.method);
+        Log.e(LOG_TAG, "CPDFViewCtrlFlutter:onMethodCall:" + call.method);
         switch (call.method) {
             case "pdfviewer":
                 result.success("rick");
                 break;
             default:
-                Log.e("ComPDFKit_Flutter", "onMethodCall:default, notImplemented");
+                Log.e(LOG_TAG, "CPDFViewCtrlFlutter:onMethodCall:notImplemented");
                 result.notImplemented();
                 break;
         }

+ 19 - 39
android/src/main/java/com/compdfkit/flutter/compdfkit_flutter/plugin/ComPDFKitSDKPlugin.java

@@ -11,6 +11,7 @@ package com.compdfkit.flutter.compdfkit_flutter.plugin;
 
 import android.content.Context;
 import android.content.Intent;
+import android.util.Log;
 
 import androidx.annotation.NonNull;
 
@@ -18,18 +19,17 @@ import com.compdfkit.core.document.CPDFSdk;
 import com.compdfkit.tools.common.pdf.CPDFConfigurationUtils;
 import com.compdfkit.tools.common.pdf.CPDFDocumentActivity;
 import com.compdfkit.tools.common.pdf.config.CPDFConfiguration;
+import com.compdfkit.tools.common.utils.CLog;
 
 import java.util.Map;
 
-import io.flutter.embedding.engine.plugins.activity.ActivityAware;
-import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
 import io.flutter.plugin.common.BinaryMessenger;
 import io.flutter.plugin.common.MethodCall;
 import io.flutter.plugin.common.MethodChannel;
 import io.flutter.plugin.platform.PlatformViewRegistry;
 
 
-public class ComPDFKitSDKPlugin extends BaseMethodChannelPlugin implements ActivityAware {
+public class ComPDFKitSDKPlugin extends BaseMethodChannelPlugin {
 
     public static final String INIT_SDK = "init_sdk";
 
@@ -39,26 +39,29 @@ public class ComPDFKitSDKPlugin extends BaseMethodChannelPlugin implements Activ
 
     public static final String SDK_BUILD_TAG = "sdk_build_tag";
 
-    private PlatformViewRegistry mRegistry;
-
-
     public ComPDFKitSDKPlugin(Context context, BinaryMessenger binaryMessenger, PlatformViewRegistry registry) {
         super(context, binaryMessenger);
-        this.mRegistry = registry;
+    }
+
+    @Override
+    public String methodName() {
+        return "com.compdfkit.flutter.plugin";
     }
 
     @Override
     public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
         switch (call.method) {
             case INIT_SDK:
-                Map<String, Object> map = (Map<String, Object>) call.arguments;
-                String key = (String) map.get("key");
-                CPDFSdk.init(context, key, true);
+                String key = call.argument("key");
+                CPDFSdk.init(context, key, true, (code, msg) -> {
+                    Log.e("ComPDFKit-Plugin", "INIT_SDK: code:" + code + ", msg:" + msg);
+                });
                 break;
             case INIT_SDK_KEYS:
-                Map<String, Object> map1 = (Map<String, Object>) call.arguments;
-                String androidLicenseKey = (String) map1.get("androidOnlineLicense");
-                CPDFSdk.init(context, androidLicenseKey, false);
+                String androidLicenseKey = call.argument("androidOnlineLicense");
+                CPDFSdk.init(context, androidLicenseKey, false, (code, msg) -> {
+                    Log.e("ComPDFKit-Plugin", "INIT_SDK_KEYS: code:" + code + ", msg:" + msg);
+                });
                 break;
             case SDK_VERSION_CODE:
                 result.success("ComPDFKit " + CPDFSdk.getSDKVersion() + " for Android");
@@ -67,10 +70,9 @@ public class ComPDFKitSDKPlugin extends BaseMethodChannelPlugin implements Activ
                 result.success(CPDFSdk.getSDKBuildTag());
                 break;
             case "openDocument":
-                Map<String, Object> arguments = (Map<String, Object>) call.arguments;
-                String filePath = (String) arguments.get("document");
-                String password = (String) arguments.get("password");
-                String configurationJson = (String) arguments.get("configuration");
+                String filePath = call.argument("document");
+                String password = call.argument("password");
+                String configurationJson = call.argument("configuration");
                 CPDFConfiguration configuration = CPDFConfigurationUtils.fromJson(configurationJson);
                 Intent intent = new Intent(context, CPDFDocumentActivity.class);
                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
@@ -87,27 +89,5 @@ public class ComPDFKitSDKPlugin extends BaseMethodChannelPlugin implements Activ
         }
     }
 
-    @Override
-    public String methodName() {
-        return "com.compdfkit.flutter.plugin";
-    }
 
-    @Override
-    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
-    }
-
-    @Override
-    public void onDetachedFromActivityForConfigChanges() {
-
-    }
-
-    @Override
-    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
-
-    }
-
-    @Override
-    public void onDetachedFromActivity() {
-
-    }
 }

+ 1 - 8
example/android/app/src/main/AndroidManifest.xml

@@ -1,5 +1,4 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:tools="http://schemas.android.com/tools"
     package="com.compdfkit.flutter.example">
 
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
@@ -20,7 +19,7 @@
             android:hardwareAccelerated="true"
             android:launchMode="singleTop"
             android:theme="@style/LaunchTheme"
-            android:windowSoftInputMode="adjustPan|adjustResize">
+            android:windowSoftInputMode="adjustPan">
             <meta-data
                 android:name="io.flutter.embedding.android.NormalTheme"
                 android:resource="@style/NormalTheme" />
@@ -29,17 +28,11 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
-        <!-- Don't delete the meta-data below.
-             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
         <meta-data
             android:name="flutterEmbedding"
             android:value="2" />
     </application>
-    <!-- Required to query activities that can process text, see:
-         https://developer.android.com/training/package-visibility?hl=en and
-         https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
 
-         In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
     <queries>
         <intent>
             <action android:name="android.intent.action.PROCESS_TEXT" />

+ 0 - 1
example/android/app/src/main/java/com/compdfkit/flutter/example/MainActivity.java

@@ -21,6 +21,5 @@ public class MainActivity extends FlutterFragmentActivity {
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
     }
 }

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 6 - 12
example/lib/main.dart


+ 14 - 42
example/lib/page/pdf/pdf_page.dart

@@ -6,18 +6,17 @@
 ///  This notice may not be removed from this file.
 ///
 
-import 'dart:io';
 
 import 'package:compdfkit_flutter/cpdf_configuration.dart';
 import 'package:compdfkit_flutter/cpdf_options.dart';
 import 'package:compdfkit_flutter/widgets/cpdf_reader_widget.dart';
 import 'package:flutter/material.dart';
 
-import '../../utils/file_util.dart';
-
 
 class PDFDocumentPage extends StatefulWidget {
-  const PDFDocumentPage({super.key});
+  final String documentPath;
+
+  const PDFDocumentPage({super.key, required this.documentPath});
 
   @override
   State<PDFDocumentPage> createState() => _PDFDocumentPageState();
@@ -27,43 +26,16 @@ class _PDFDocumentPageState extends State<PDFDocumentPage> {
   @override
   Widget build(BuildContext context) {
     return Scaffold(
-      resizeToAvoidBottomInset: false,
-        appBar: AppBar(title: const Text('CPDFReaderWidget'),),
-        body: SafeArea(
-            child: Column(
-          children: [
-            Expanded(
-                child: FutureBuilder(
-                    future: getPDFPath(),
-                    builder: (context, snapShot) {
-                      if (snapShot.connectionState == ConnectionState.done &&
-                          snapShot.hasData) {
-                        String document = snapShot.data!;
-                        return CPDFReaderWidget(
-                            document: document,
-                            configuration: CPDFConfiguration(
-                                globalConfig: const CPDFGlobalConfig(themeMode: CPDFThemeMode.dark)
-                            ),
-                            onCreated: (controller) {
-                            },
-                        );
-                      } else {
-                        return const Center(
-                          child: Column(
-                            children: [
-                              CircularProgressIndicator(),
-                              Text('Loading...')
-                            ],
-                          ),
-                        );
-                      }
-                    }))
-          ],
-        )));
-  }
-
-  Future<String> getPDFPath() async {
-    File document = await extractAsset(context, 'pdfs/PDF_Document.pdf');
-    return document.path;
+        resizeToAvoidBottomInset: false,
+        appBar: AppBar(
+          title: const Text('CPDFReaderWidget'),
+        ),
+        body: CPDFReaderWidget(
+          document: widget.documentPath,
+          configuration: CPDFConfiguration(
+              globalConfig:
+                  const CPDFGlobalConfig(themeMode: CPDFThemeMode.dark)),
+          onCreated: (controller) {},
+        ));
   }
 }

+ 1 - 0
lib/widgets/cpdf_reader_widget.dart

@@ -92,6 +92,7 @@ class _CPDFReaderWidgetState extends State<CPDFReaderWidget> {
   }
 
   Future<void> _onPlatformViewCreated(int id) async {
+    debugPrint('ComPDFKit-Flutter: CPDFReaderWidget created');
     widget.onCreated(CPDFReaderWidgetController(id));
   }
 }