From fbd6ece45f079f7ec5f48532e668be2f70135904 Mon Sep 17 00:00:00 2001 From: weilu Date: Thu, 26 Dec 2019 16:30:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=811.12=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=96=B0=E7=9A=84android=E6=8F=92=E4=BB=B6api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../barcodescan/BarcodeScanPlugin.kt | 66 +++++++++++++++---- .../android/app/src/main/AndroidManifest.xml | 36 +++++++--- .../barcodescanexample/EmbeddingV1Activity.kt | 12 ++++ .../barcodescanexample/MainActivity.kt | 13 ++-- 4 files changed, 97 insertions(+), 30 deletions(-) create mode 100644 example/android/app/src/main/kotlin/com/apptreesoftware/barcodescanexample/EmbeddingV1Activity.kt diff --git a/android/src/main/kotlin/com/apptreesoftware/barcodescan/BarcodeScanPlugin.kt b/android/src/main/kotlin/com/apptreesoftware/barcodescan/BarcodeScanPlugin.kt index e3a58f6..53a9bfe 100644 --- a/android/src/main/kotlin/com/apptreesoftware/barcodescan/BarcodeScanPlugin.kt +++ b/android/src/main/kotlin/com/apptreesoftware/barcodescan/BarcodeScanPlugin.kt @@ -2,30 +2,40 @@ package com.apptreesoftware.barcodescan import android.app.Activity import android.content.Intent +import io.flutter.embedding.engine.plugins.FlutterPlugin +import io.flutter.embedding.engine.plugins.activity.ActivityAware +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding +import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.Result -import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.PluginRegistry import io.flutter.plugin.common.PluginRegistry.Registrar -class BarcodeScanPlugin(val activity: Activity): MethodCallHandler, - PluginRegistry.ActivityResultListener { - var result : Result? = null +class BarcodeScanPlugin(): MethodCallHandler, PluginRegistry.ActivityResultListener, FlutterPlugin, ActivityAware { + + private var result : Result? = null + private var channel: MethodChannel? = null + private var activity: Activity? = null + + constructor(activity: Activity?) : this() { + this.activity = activity + } + companion object { @JvmStatic - fun registerWith(registrar: Registrar): Unit { + fun registerWith(registrar: Registrar) { val channel = MethodChannel(registrar.messenger(), "com.apptreesoftware.barcode_scan") - if (registrar.activity() != null) { - val plugin = BarcodeScanPlugin(registrar.activity()) - channel.setMethodCallHandler(plugin) - registrar.addActivityResultListener(plugin) - } + if (registrar.activity() != null) { + val plugin = BarcodeScanPlugin(registrar.activity()) + channel.setMethodCallHandler(plugin) + registrar.addActivityResultListener(plugin) + } } } - override fun onMethodCall(call: MethodCall, result: Result): Unit { - if (call.method.equals("scan")) { + override fun onMethodCall(call: MethodCall, result: Result) { + if (call.method == "scan") { this.result = result showBarcodeView() } else { @@ -34,8 +44,10 @@ class BarcodeScanPlugin(val activity: Activity): MethodCallHandler, } private fun showBarcodeView() { - val intent = Intent(activity, BarcodeScannerActivity::class.java) - activity.startActivityForResult(intent, 100) + activity?.let { + val intent = Intent(it, BarcodeScannerActivity::class.java) + it.startActivityForResult(intent, 100) + } } override fun onActivityResult(code: Int, resultCode: Int, data: Intent?): Boolean { @@ -51,4 +63,30 @@ class BarcodeScanPlugin(val activity: Activity): MethodCallHandler, } return false } + + override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { + channel = MethodChannel(binding.binaryMessenger, "com.apptreesoftware.barcode_scan") + } + + override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { + channel?.setMethodCallHandler(null) + } + + override fun onAttachedToActivity(binding: ActivityPluginBinding) { + val plugin = BarcodeScanPlugin(binding.activity) + channel?.setMethodCallHandler(plugin) + binding.addActivityResultListener(plugin) + } + + override fun onDetachedFromActivity() { + + } + + override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { + onAttachedToActivity(binding) + } + + override fun onDetachedFromActivityForConfigChanges() { + onDetachedFromActivity() + } } diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index b18be67..9462fca 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -16,19 +16,37 @@ android:label="barcode_scan_example" android:icon="@mipmap/ic_launcher"> - + + android:value="true"/> + + + + + + + + + diff --git a/example/android/app/src/main/kotlin/com/apptreesoftware/barcodescanexample/EmbeddingV1Activity.kt b/example/android/app/src/main/kotlin/com/apptreesoftware/barcodescanexample/EmbeddingV1Activity.kt new file mode 100644 index 0000000..8521421 --- /dev/null +++ b/example/android/app/src/main/kotlin/com/apptreesoftware/barcodescanexample/EmbeddingV1Activity.kt @@ -0,0 +1,12 @@ +package com.apptreesoftware.barcodescanexample + +import android.os.Bundle +import io.flutter.app.FlutterActivity +import io.flutter.plugins.GeneratedPluginRegistrant + +class EmbeddingV1Activity : FlutterActivity() { + override fun onCreate(savedInstanceState: Bundle) { + super.onCreate(savedInstanceState) + GeneratedPluginRegistrant.registerWith(this) + } +} \ No newline at end of file diff --git a/example/android/app/src/main/kotlin/com/apptreesoftware/barcodescanexample/MainActivity.kt b/example/android/app/src/main/kotlin/com/apptreesoftware/barcodescanexample/MainActivity.kt index 0752e1a..7d70e96 100644 --- a/example/android/app/src/main/kotlin/com/apptreesoftware/barcodescanexample/MainActivity.kt +++ b/example/android/app/src/main/kotlin/com/apptreesoftware/barcodescanexample/MainActivity.kt @@ -1,13 +1,12 @@ package com.apptreesoftware.barcodescanexample -import android.os.Bundle +import com.apptreesoftware.barcodescan.BarcodeScanPlugin +import io.flutter.embedding.android.FlutterActivity +import io.flutter.embedding.engine.FlutterEngine -import io.flutter.app.FlutterActivity -import io.flutter.plugins.GeneratedPluginRegistrant -class MainActivity(): FlutterActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - GeneratedPluginRegistrant.registerWith(this) +class MainActivity: FlutterActivity() { + override fun configureFlutterEngine(flutterEngine: FlutterEngine) { + flutterEngine.plugins.add(BarcodeScanPlugin()) } }