From 486b740ec1ce7cb562a286bf43015bb7fda8375b Mon Sep 17 00:00:00 2001 From: Leon Date: Wed, 3 Apr 2019 17:26:34 +0800 Subject: [PATCH] Improve and fix bugs --- .../net/touchcapture/qr/flutterqr/QRView.kt | 81 ++++++++++++------- 1 file changed, 54 insertions(+), 27 deletions(-) diff --git a/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt index 72fa5c6..a783084 100644 --- a/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt +++ b/android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt @@ -5,6 +5,7 @@ import android.app.Activity import android.app.Application import android.content.Context import android.content.pm.PackageManager +import android.content.pm.PackageManager.PERMISSION_GRANTED import android.os.Build import android.os.Bundle import android.view.View @@ -12,7 +13,6 @@ import com.google.zxing.ResultPoint import com.journeyapps.barcodescanner.BarcodeCallback import com.journeyapps.barcodescanner.BarcodeResult import com.journeyapps.barcodescanner.BarcodeView -import com.journeyapps.barcodescanner.DefaultDecoderFactory import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.PluginRegistry @@ -24,57 +24,84 @@ class QRView(context: Context, private val registrar: PluginRegistry.Registrar, companion object { const val CAMERA_REQUEST_ID = 513469796 } - var barcodeView = BarcodeView(context) + + var barcodeView: BarcodeView? = null private val activity = registrar.activity() var cameraPermissionContinuation: Runnable? = null var requestingPermission = false + val channel: MethodChannel + init { registrar.addRequestPermissionsResultListener(CameraRequestPermissionsListener()) - - val channel = MethodChannel(registrar.messenger(), "net.touchcapture.qr.flutterqr/qrview_$id") + channel = MethodChannel(registrar.messenger(), "net.touchcapture.qr.flutterqr/qrview_$id") channel.setMethodCallHandler(this) + checkAndRequestPermission(null) + registrar.activity().application.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks { + override fun onActivityPaused(p0: Activity?) { + if (p0 == registrar.activity()) { + barcodeView?.pause() + } + } - barcodeView.decodeContinuous( - object : BarcodeCallback { - override fun barcodeResult(result: BarcodeResult) { - channel.invokeMethod("onRecognizeQR",result.text) - } - override fun possibleResultPoints(resultPoints: List) {} + override fun onActivityResumed(p0: Activity?) { + if (p0 == registrar.activity()) { + barcodeView?.resume() } - ) - barcodeView.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener { - override fun onViewDetachedFromWindow(p0: View?) { - barcodeView.pause() } - override fun onViewAttachedToWindow(p0: View?) { - barcodeView.resume() + override fun onActivityStarted(p0: Activity?) { + } + + override fun onActivityDestroyed(p0: Activity?) { + } + + override fun onActivitySaveInstanceState(p0: Activity?, p1: Bundle?) { + } + + override fun onActivityStopped(p0: Activity?) { + } + + override fun onActivityCreated(p0: Activity?, p1: Bundle?) { } }) } - private fun init(){ - if(!hasCameraPermission()) { - checkAndRequestPermission(null) - } else { - barcodeView.resume() - } + override fun getView(): View { + return initBarCodeView()?.apply { + resume() + }!! } - override fun getView(): View { - return barcodeView.apply { - init() + private fun initBarCodeView(): BarcodeView? { + if (barcodeView == null) { + barcodeView = createBarCodeView() } + return barcodeView + } + + private fun createBarCodeView(): BarcodeView? { + val barcode = BarcodeView(registrar.activity()) + barcode.decodeContinuous( + object : BarcodeCallback { + override fun barcodeResult(result: BarcodeResult) { + channel.invokeMethod("onRecognizeQR", result.text) + } + + override fun possibleResultPoints(resultPoints: List) {} + } + ) + return barcode } override fun dispose() { - barcodeView.pause() + barcodeView?.pause() + barcodeView = null } private inner class CameraRequestPermissionsListener : PluginRegistry.RequestPermissionsResultListener { override fun onRequestPermissionsResult(id: Int, permissions: Array, grantResults: IntArray): Boolean { - if (id == CAMERA_REQUEST_ID) { + if (id == CAMERA_REQUEST_ID && grantResults[0] == PERMISSION_GRANTED) { cameraPermissionContinuation?.run() return true }