remove permission callback; move method call result to delegate
Showing
2 changed files
with
12 additions
and
28 deletions
| @@ -17,10 +17,9 @@ import com.google.mlkit.vision.barcode.BarcodeScanning | @@ -17,10 +17,9 @@ import com.google.mlkit.vision.barcode.BarcodeScanning | ||
| 17 | import com.google.mlkit.vision.common.InputImage | 17 | import com.google.mlkit.vision.common.InputImage |
| 18 | import dev.steenbakker.mobile_scanner.objects.DetectionSpeed | 18 | import dev.steenbakker.mobile_scanner.objects.DetectionSpeed |
| 19 | import dev.steenbakker.mobile_scanner.objects.MobileScannerStartParameters | 19 | import dev.steenbakker.mobile_scanner.objects.MobileScannerStartParameters |
| 20 | +import io.flutter.plugin.common.MethodChannel | ||
| 20 | import io.flutter.plugin.common.PluginRegistry | 21 | import io.flutter.plugin.common.PluginRegistry |
| 21 | import io.flutter.view.TextureRegistry | 22 | import io.flutter.view.TextureRegistry |
| 22 | - | ||
| 23 | -typealias PermissionCallback = (permissionGranted: Boolean) -> Unit | ||
| 24 | typealias MobileScannerCallback = (barcodes: List<Map<String, Any?>>, image: ByteArray?) -> Unit | 23 | typealias MobileScannerCallback = (barcodes: List<Map<String, Any?>>, image: ByteArray?) -> Unit |
| 25 | typealias AnalyzerCallback = (barcodes: List<Map<String, Any?>>?) -> Unit | 24 | typealias AnalyzerCallback = (barcodes: List<Map<String, Any?>>?) -> Unit |
| 26 | typealias MobileScannerErrorCallback = (error: String) -> Unit | 25 | typealias MobileScannerErrorCallback = (error: String) -> Unit |
| @@ -49,10 +48,9 @@ class MobileScanner( | @@ -49,10 +48,9 @@ class MobileScanner( | ||
| 49 | private const val REQUEST_CODE = 0x0786 | 48 | private const val REQUEST_CODE = 0x0786 |
| 50 | } | 49 | } |
| 51 | 50 | ||
| 52 | - private var listener: PluginRegistry.RequestPermissionsResultListener? = null | ||
| 53 | - | ||
| 54 | private var cameraProvider: ProcessCameraProvider? = null | 51 | private var cameraProvider: ProcessCameraProvider? = null |
| 55 | private var camera: Camera? = null | 52 | private var camera: Camera? = null |
| 53 | + private var pendingPermissionResult: MethodChannel.Result? = null | ||
| 56 | private var preview: Preview? = null | 54 | private var preview: Preview? = null |
| 57 | private var textureEntry: TextureRegistry.SurfaceTextureEntry? = null | 55 | private var textureEntry: TextureRegistry.SurfaceTextureEntry? = null |
| 58 | 56 | ||
| @@ -86,17 +84,7 @@ class MobileScanner( | @@ -86,17 +84,7 @@ class MobileScanner( | ||
| 86 | /** | 84 | /** |
| 87 | * Request camera permissions. | 85 | * Request camera permissions. |
| 88 | */ | 86 | */ |
| 89 | - fun requestPermission(permissionCallback: PermissionCallback) { | ||
| 90 | - listener | ||
| 91 | - ?: PluginRegistry.RequestPermissionsResultListener { requestCode, _, grantResults -> | ||
| 92 | - if (requestCode != REQUEST_CODE) { | ||
| 93 | - false | ||
| 94 | - } else { | ||
| 95 | - val authorized = grantResults[0] == PackageManager.PERMISSION_GRANTED | ||
| 96 | - permissionCallback(authorized) | ||
| 97 | - true | ||
| 98 | - } | ||
| 99 | - } | 87 | + fun requestPermission(result: MethodChannel.Result) { |
| 100 | val permissions = arrayOf(Manifest.permission.CAMERA) | 88 | val permissions = arrayOf(Manifest.permission.CAMERA) |
| 101 | ActivityCompat.requestPermissions(activity, permissions, REQUEST_CODE) | 89 | ActivityCompat.requestPermissions(activity, permissions, REQUEST_CODE) |
| 102 | } | 90 | } |
| @@ -109,7 +97,14 @@ class MobileScanner( | @@ -109,7 +97,14 @@ class MobileScanner( | ||
| 109 | permissions: Array<out String>, | 97 | permissions: Array<out String>, |
| 110 | grantResults: IntArray | 98 | grantResults: IntArray |
| 111 | ): Boolean { | 99 | ): Boolean { |
| 112 | - return listener?.onRequestPermissionsResult(requestCode, permissions, grantResults) ?: false | 100 | + if (requestCode != REQUEST_CODE) { |
| 101 | + return false | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + pendingPermissionResult?.success(grantResults[0] == PackageManager.PERMISSION_GRANTED) | ||
| 105 | + pendingPermissionResult = null | ||
| 106 | + | ||
| 107 | + return true | ||
| 113 | } | 108 | } |
| 114 | 109 | ||
| 115 | /** | 110 | /** |
| @@ -23,14 +23,8 @@ class MobileScannerPlugin : FlutterPlugin, ActivityAware, MethodChannel.MethodCa | @@ -23,14 +23,8 @@ class MobileScannerPlugin : FlutterPlugin, ActivityAware, MethodChannel.MethodCa | ||
| 23 | 23 | ||
| 24 | private lateinit var barcodeHandler: BarcodeHandler | 24 | private lateinit var barcodeHandler: BarcodeHandler |
| 25 | 25 | ||
| 26 | - private var permissionResult: MethodChannel.Result? = null | ||
| 27 | private var analyzerResult: MethodChannel.Result? = null | 26 | private var analyzerResult: MethodChannel.Result? = null |
| 28 | 27 | ||
| 29 | - private val permissionCallback: PermissionCallback = {hasPermission: Boolean -> | ||
| 30 | - permissionResult?.success(hasPermission) | ||
| 31 | - permissionResult = null | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | private val callback: MobileScannerCallback = { barcodes: List<Map<String, Any?>>, image: ByteArray? -> | 28 | private val callback: MobileScannerCallback = { barcodes: List<Map<String, Any?>>, image: ByteArray? -> |
| 35 | if (image != null) { | 29 | if (image != null) { |
| 36 | barcodeHandler.publishEvent(mapOf( | 30 | barcodeHandler.publishEvent(mapOf( |
| @@ -78,7 +72,7 @@ class MobileScannerPlugin : FlutterPlugin, ActivityAware, MethodChannel.MethodCa | @@ -78,7 +72,7 @@ class MobileScannerPlugin : FlutterPlugin, ActivityAware, MethodChannel.MethodCa | ||
| 78 | } | 72 | } |
| 79 | when (call.method) { | 73 | when (call.method) { |
| 80 | "state" -> result.success(handler!!.hasCameraPermission()) | 74 | "state" -> result.success(handler!!.hasCameraPermission()) |
| 81 | - "request" -> requestPermission(result) | 75 | + "request" -> handler!!.requestPermission(result) |
| 82 | "start" -> start(call, result) | 76 | "start" -> start(call, result) |
| 83 | "torch" -> toggleTorch(call, result) | 77 | "torch" -> toggleTorch(call, result) |
| 84 | "stop" -> stop(result) | 78 | "stop" -> stop(result) |
| @@ -124,11 +118,6 @@ class MobileScannerPlugin : FlutterPlugin, ActivityAware, MethodChannel.MethodCa | @@ -124,11 +118,6 @@ class MobileScannerPlugin : FlutterPlugin, ActivityAware, MethodChannel.MethodCa | ||
| 124 | onDetachedFromActivity() | 118 | onDetachedFromActivity() |
| 125 | } | 119 | } |
| 126 | 120 | ||
| 127 | - private fun requestPermission(result: MethodChannel.Result) { | ||
| 128 | - permissionResult = result | ||
| 129 | - handler!!.requestPermission(permissionCallback) | ||
| 130 | - } | ||
| 131 | - | ||
| 132 | @ExperimentalGetImage | 121 | @ExperimentalGetImage |
| 133 | private fun start(call: MethodCall, result: MethodChannel.Result) { | 122 | private fun start(call: MethodCall, result: MethodChannel.Result) { |
| 134 | val torch: Boolean = call.argument<Boolean>("torch") ?: false | 123 | val torch: Boolean = call.argument<Boolean>("torch") ?: false |
-
Please register or login to post a comment