Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid use Registrar.activity() beforr Flutter engine attached to any activity #1

Merged
merged 1 commit into from
Dec 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,56 @@ package com.apptreesoftware.barcodescan

import android.app.Activity
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.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
companion object {
@JvmStatic
fun registerWith(registrar: Registrar): Unit {
val channel = MethodChannel(registrar.messenger(), "com.apptreesoftware.barcode_scan")
if (registrar.activity() != null) {
val plugin = BarcodeScanPlugin(registrar.activity())
class BarcodeScanPlugin(private val registrar: Registrar) : MethodCallHandler, PluginRegistry.ActivityResultListener {
var result: Result? = null

companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "com.apptreesoftware.barcode_scan")
val plugin = BarcodeScanPlugin(registrar)
channel.setMethodCallHandler(plugin)
registrar.addActivityResultListener(plugin)
}
}
}
}

override fun onMethodCall(call: MethodCall, result: Result): Unit {
if (call.method.equals("scan")) {
this.result = result
showBarcodeView()
} else {
result.notImplemented()
override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "scan") {
this.result = result
showBarcodeView()
} else {
result.notImplemented()
}
}
}

private fun showBarcodeView() {
val intent = Intent(activity, BarcodeScannerActivity::class.java)
activity.startActivityForResult(intent, 100)
}
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 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
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
}
return false
}
}