2020-02-20 06:19:14 +08:00
|
|
|
package de.mintware.barcode_scan
|
2017-10-30 01:05:11 +08:00
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.content.Intent
|
2019-12-19 00:43:01 +08:00
|
|
|
import android.util.Log
|
|
|
|
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-19 00:43:01 +08:00
|
|
|
class BarcodeScanPlugin(private val registrar: Registrar) : MethodCallHandler, PluginRegistry.ActivityResultListener {
|
|
|
|
var result: Result? = null
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
@JvmStatic
|
|
|
|
fun registerWith(registrar: Registrar) {
|
2020-02-20 06:19:14 +08:00
|
|
|
val channel = MethodChannel(registrar.messenger(), "de.mintware.barcode_scan")
|
2019-12-19 00:43:01 +08:00
|
|
|
val plugin = BarcodeScanPlugin(registrar)
|
2019-06-08 20:12:23 +08:00
|
|
|
channel.setMethodCallHandler(plugin)
|
|
|
|
registrar.addActivityResultListener(plugin)
|
2019-12-19 00:43:01 +08:00
|
|
|
}
|
2017-10-30 01:05:11 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 00:43:01 +08:00
|
|
|
override fun onMethodCall(call: MethodCall, result: Result) {
|
|
|
|
if (call.method == "scan") {
|
|
|
|
this.result = result
|
|
|
|
showBarcodeView()
|
|
|
|
} else {
|
|
|
|
result.notImplemented()
|
|
|
|
}
|
2017-10-30 01:05:11 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 00:43:01 +08:00
|
|
|
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)
|
|
|
|
}
|
2017-10-30 01:05:11 +08:00
|
|
|
|
2019-12-19 00:43:01 +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)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2017-10-30 01:05:11 +08:00
|
|
|
}
|
|
|
|
}
|