Merge pull request #168 from AreaSeys-Team/master

Supporting add-to-app feature
This commit is contained in:
Julian Finkler 2020-02-19 21:35:21 +01:00 committed by GitHub
commit 0a25923ae2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,53 +2,56 @@ package com.apptreesoftware.barcodescan
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.util.Log
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result 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
import io.flutter.plugin.common.PluginRegistry.Registrar import io.flutter.plugin.common.PluginRegistry.Registrar
class BarcodeScanPlugin(val activity: Activity): MethodCallHandler, class BarcodeScanPlugin(private val registrar: Registrar) : MethodCallHandler, PluginRegistry.ActivityResultListener {
PluginRegistry.ActivityResultListener { var result: Result? = null
var result : Result? = null
companion object { companion object {
@JvmStatic @JvmStatic
fun registerWith(registrar: Registrar): Unit { fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "com.apptreesoftware.barcode_scan") val channel = MethodChannel(registrar.messenger(), "com.apptreesoftware.barcode_scan")
if (registrar.activity() != null) { val plugin = BarcodeScanPlugin(registrar)
val plugin = BarcodeScanPlugin(registrar.activity())
channel.setMethodCallHandler(plugin) channel.setMethodCallHandler(plugin)
registrar.addActivityResultListener(plugin) registrar.addActivityResultListener(plugin)
} }
} }
}
override fun onMethodCall(call: MethodCall, result: Result): Unit { override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method.equals("scan")) { if (call.method == "scan") {
this.result = result this.result = result
showBarcodeView() showBarcodeView()
} else { } else {
result.notImplemented() result.notImplemented()
}
} }
}
private fun showBarcodeView() { private fun showBarcodeView() {
val intent = Intent(activity, BarcodeScannerActivity::class.java) if (registrar.activity() == null) {
activity.startActivityForResult(intent, 100) Log.e("BarcodeScanPlugin", "plugin can't launch scan activity, because plugin is not attached to any activity.")
} return
}
override fun onActivityResult(code: Int, resultCode: Int, data: Intent?): Boolean { val intent = Intent(registrar.activity(), BarcodeScannerActivity::class.java)
if (code == 100) { registrar.activity().startActivityForResult(intent, 100)
if (resultCode == Activity.RESULT_OK) { }
val barcode = data?.getStringExtra("SCAN_RESULT")
barcode?.let { this.result?.success(barcode) } override fun onActivityResult(code: Int, resultCode: Int, data: Intent?): Boolean {
} else { if (code == 100) {
val errorCode = data?.getStringExtra("ERROR_CODE") if (resultCode == Activity.RESULT_OK) {
this.result?.error(errorCode, null, null) val barcode = data?.getStringExtra("SCAN_RESULT")
} barcode?.let { this.result?.success(barcode) }
return true } else {
val errorCode = data?.getStringExtra("ERROR_CODE")
this.result?.error(errorCode, null, null)
}
return true
}
return false
} }
return false
}
} }