flutter_barcode_reader/android/src/main/kotlin/com/apptreesoftware/barcodescan/BarcodeScanPlugin.kt

93 lines
2.9 KiB
Kotlin
Raw Normal View History

2017-10-30 01:05:11 +08:00
package com.apptreesoftware.barcodescan
import android.app.Activity
import android.content.Intent
2019-12-26 16:30:33 +08:00
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
2017-10-30 01:05:11 +08:00
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
2019-12-26 16:30:33 +08:00
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
}
2017-10-30 01:05:11 +08:00
companion object {
@JvmStatic
2019-12-26 16:30:33 +08:00
fun registerWith(registrar: Registrar) {
2017-10-30 01:05:11 +08:00
val channel = MethodChannel(registrar.messenger(), "com.apptreesoftware.barcode_scan")
2019-12-26 16:30:33 +08:00
if (registrar.activity() != null) {
val plugin = BarcodeScanPlugin(registrar.activity())
channel.setMethodCallHandler(plugin)
registrar.addActivityResultListener(plugin)
}
2017-10-30 01:05:11 +08:00
}
}
2019-12-26 16:30:33 +08:00
override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "scan") {
2017-10-30 01:05:11 +08:00
this.result = result
showBarcodeView()
} else {
result.notImplemented()
}
}
private fun showBarcodeView() {
2019-12-26 16:30:33 +08:00
activity?.let {
val intent = Intent(it, BarcodeScannerActivity::class.java)
it.startActivityForResult(intent, 100)
}
2017-10-30 01:05:11 +08:00
}
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)
2017-10-30 01:05:11 +08:00
}
return true
2017-10-30 01:05:11 +08:00
}
return false
}
2019-12-26 16:30:33 +08:00
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()
}
2017-10-30 01:05:11 +08:00
}