From 234183a26ab316efd7caca4bed77603afc2a7bf4 Mon Sep 17 00:00:00 2001 From: weilu Date: Fri, 10 Apr 2020 15:11:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../barcodescan/BarcodeScanPlugin.kt | 92 --------------- .../barcode_scan/BarcodeScanPlugin.kt | 111 ++++++++++++------ .../android/app/src/main/AndroidManifest.xml | 6 +- .../barcodescanexample/MainActivity.kt | 4 +- pubspec.yaml | 6 +- 5 files changed, 81 insertions(+), 138 deletions(-) delete mode 100644 android/src/main/kotlin/com/apptreesoftware/barcodescan/BarcodeScanPlugin.kt diff --git a/android/src/main/kotlin/com/apptreesoftware/barcodescan/BarcodeScanPlugin.kt b/android/src/main/kotlin/com/apptreesoftware/barcodescan/BarcodeScanPlugin.kt deleted file mode 100644 index 53a9bfe..0000000 --- a/android/src/main/kotlin/com/apptreesoftware/barcodescan/BarcodeScanPlugin.kt +++ /dev/null @@ -1,92 +0,0 @@ -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.PluginRegistry -import io.flutter.plugin.common.PluginRegistry.Registrar - -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) { - val channel = MethodChannel(registrar.messenger(), "com.apptreesoftware.barcode_scan") - if (registrar.activity() != null) { - val plugin = BarcodeScanPlugin(registrar.activity()) - channel.setMethodCallHandler(plugin) - registrar.addActivityResultListener(plugin) - } - } - } - - override fun onMethodCall(call: MethodCall, result: Result) { - if (call.method == "scan") { - this.result = result - showBarcodeView() - } else { - result.notImplemented() - } - } - - private fun showBarcodeView() { - activity?.let { - val intent = Intent(it, BarcodeScannerActivity::class.java) - it.startActivityForResult(intent, 100) - } - } - - override fun onActivityResult(code: Int, resultCode: Int, data: Intent?): Boolean { - if (code == 100) { - if (resultCode == Activity.RESULT_OK) { - val barcode = data?.getStringExtra("SCAN_RESULT") - barcode?.let { this.result?.success(barcode) } - } else { - val errorCode = data?.getStringExtra("ERROR_CODE") - this.result?.error(errorCode, null, null) - } - return true - } - 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/android/src/main/kotlin/de/mintware/barcode_scan/BarcodeScanPlugin.kt b/android/src/main/kotlin/de/mintware/barcode_scan/BarcodeScanPlugin.kt index 59539da..20abafe 100644 --- a/android/src/main/kotlin/de/mintware/barcode_scan/BarcodeScanPlugin.kt +++ b/android/src/main/kotlin/de/mintware/barcode_scan/BarcodeScanPlugin.kt @@ -2,7 +2,9 @@ package de.mintware.barcode_scan import android.app.Activity import android.content.Intent -import android.util.Log +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 @@ -10,48 +12,81 @@ import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.PluginRegistry import io.flutter.plugin.common.PluginRegistry.Registrar -class BarcodeScanPlugin(private val registrar: Registrar) : 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 - companion object { - @JvmStatic - fun registerWith(registrar: Registrar) { - val channel = MethodChannel(registrar.messenger(), "de.mintware.barcode_scan") - val plugin = BarcodeScanPlugin(registrar) - channel.setMethodCallHandler(plugin) - registrar.addActivityResultListener(plugin) - } - } + constructor(activity: Activity?) : this() { + this.activity = activity + } - override fun onMethodCall(call: MethodCall, result: Result) { - if (call.method == "scan") { - this.result = result - showBarcodeView() - } else { - result.notImplemented() - } + companion object { + @JvmStatic + fun registerWith(registrar: Registrar) { + val channel = MethodChannel(registrar.messenger(), "de.mintware.barcode_scan") + if (registrar.activity() != null) { + val plugin = BarcodeScanPlugin(registrar.activity()) + channel.setMethodCallHandler(plugin) + registrar.addActivityResultListener(plugin) + } } + } - private fun showBarcodeView() { - if (registrar.activity() == null) { - Log.e("BarcodeScanPlugin", "plugin can't launch scan activity, because plugin is not attached to any activity.") - return - } - val intent = Intent(registrar.activity(), BarcodeScannerActivity::class.java) - registrar.activity().startActivityForResult(intent, 100) + override fun onMethodCall(call: MethodCall, result: Result) { + if (call.method == "scan") { + this.result = result + showBarcodeView() + } else { + result.notImplemented() } + } - override fun onActivityResult(code: Int, resultCode: Int, data: Intent?): Boolean { - if (code == 100) { - if (resultCode == Activity.RESULT_OK) { - val barcode = data?.getStringExtra("SCAN_RESULT") - barcode?.let { this.result?.success(barcode) } - } else { - val errorCode = data?.getStringExtra("ERROR_CODE") - this.result?.error(errorCode, null, null) - } - return true - } - return false + private fun showBarcodeView() { + activity?.let { + val intent = Intent(it, BarcodeScannerActivity::class.java) + it.startActivityForResult(intent, 100) } + } + + override fun onActivityResult(code: Int, resultCode: Int, data: Intent?): Boolean { + if (code == 100) { + if (resultCode == Activity.RESULT_OK) { + val barcode = data?.getStringExtra("SCAN_RESULT") + barcode?.let { this.result?.success(barcode) } + } else { + val errorCode = data?.getStringExtra("ERROR_CODE") + this.result?.error(errorCode, null, null) + } + return true + } + return false + } + + override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { + channel = MethodChannel(binding.binaryMessenger, "de.mintware.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 9462fca..f26a271 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -1,5 +1,5 @@ + package="com.apptreesoftware.barcodescanexample"> 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 e90c15b..991996f 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,6 +1,6 @@ -package de.mintware.barcode_scan_example +package com.apptreesoftware.barcodescanexample -import com.apptreesoftware.barcodescan.BarcodeScanPlugin +import de.mintware.barcode_scan.BarcodeScanPlugin import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.engine.FlutterEngine diff --git a/pubspec.yaml b/pubspec.yaml index c3cc534..66b791e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: barcode_scan -description: A flutter plugin for scanning 2D barcodes and QRCodes. -version: 1.0.0 +description: A flutter plugin for scanning 2D barcodes and QRCodes via camera. +version: 2.0.0 author: Matthew Smith homepage: https://github.com/apptreesoftware/flutter_barcode_reader @@ -20,7 +20,7 @@ flutter: plugin: platforms: android: - package: com.apptreesoftware.barcodescan + package: de.mintware.barcode_scan pluginClass: BarcodeScanPlugin ios: pluginClass: BarcodeScanPlugin