Julian Steenbakker

bug: fix merge conflicts and refactor

1 package dev.steenbakker.mobile_scanner 1 package dev.steenbakker.mobile_scanner
2 2
3 import android.app.Activity 3 import android.app.Activity
4 -import android.content.pm.PackageManager  
5 import android.graphics.Rect 4 import android.graphics.Rect
6 import android.net.Uri 5 import android.net.Uri
7 import android.os.Handler 6 import android.os.Handler
8 import android.os.Looper 7 import android.os.Looper
9 -import android.util.Log  
10 import android.view.Surface 8 import android.view.Surface
11 import androidx.camera.core.* 9 import androidx.camera.core.*
12 import androidx.camera.lifecycle.ProcessCameraProvider 10 import androidx.camera.lifecycle.ProcessCameraProvider
@@ -217,10 +215,6 @@ class MobileScanner( @@ -217,10 +215,6 @@ class MobileScanner(
217 torchStateCallback(state) 215 torchStateCallback(state)
218 } 216 }
219 217
220 -// val analysisSize = analysis.resolutionInfo?.resolution ?: Size(0, 0)  
221 -// val previewSize = preview!!.resolutionInfo?.resolution ?: Size(0, 0)  
222 -// Log.i("LOG", "Analyzer: $analysisSize")  
223 -// Log.i("LOG", "Preview: $previewSize")  
224 218
225 // Enable torch if provided 219 // Enable torch if provided
226 camera!!.cameraControl.enableTorch(torch) 220 camera!!.cameraControl.enableTorch(torch)
@@ -15,7 +15,7 @@ import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding @@ -15,7 +15,7 @@ import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
15 import io.flutter.view.TextureRegistry 15 import io.flutter.view.TextureRegistry
16 import java.io.File 16 import java.io.File
17 17
18 -class MethodCallHandlerImpl( 18 +class MobileScannerHandler(
19 private val activity: Activity, 19 private val activity: Activity,
20 private val barcodeHandler: BarcodeHandler, 20 private val barcodeHandler: BarcodeHandler,
21 binaryMessenger: BinaryMessenger, 21 binaryMessenger: BinaryMessenger,
@@ -38,12 +38,14 @@ class MethodCallHandlerImpl( @@ -38,12 +38,14 @@ class MethodCallHandlerImpl(
38 38
39 private var analyzerResult: MethodChannel.Result? = null 39 private var analyzerResult: MethodChannel.Result? = null
40 40
41 - private val callback: MobileScannerCallback = { barcodes: List<Map<String, Any?>>, image: ByteArray? -> 41 + private val callback: MobileScannerCallback = { barcodes: List<Map<String, Any?>>, image: ByteArray?, width: Int?, height: Int? ->
42 if (image != null) { 42 if (image != null) {
43 barcodeHandler.publishEvent(mapOf( 43 barcodeHandler.publishEvent(mapOf(
44 "name" to "barcode", 44 "name" to "barcode",
45 "data" to barcodes, 45 "data" to barcodes,
46 - "image" to image 46 + "image" to image,
  47 + "width" to width!!.toDouble(),
  48 + "height" to height!!.toDouble()
47 )) 49 ))
48 } else { 50 } else {
49 barcodeHandler.publishEvent(mapOf( 51 barcodeHandler.publishEvent(mapOf(
@@ -112,6 +114,8 @@ class MethodCallHandlerImpl( @@ -112,6 +114,8 @@ class MethodCallHandlerImpl(
112 "torch" -> toggleTorch(call, result) 114 "torch" -> toggleTorch(call, result)
113 "stop" -> stop(result) 115 "stop" -> stop(result)
114 "analyzeImage" -> analyzeImage(call, result) 116 "analyzeImage" -> analyzeImage(call, result)
  117 + "setScale" -> setScale(call, result)
  118 + "updateScanWindow" -> updateScanWindow(call)
115 else -> result.notImplemented() 119 else -> result.notImplemented()
116 } 120 }
117 } 121 }
@@ -213,4 +217,19 @@ class MethodCallHandlerImpl( @@ -213,4 +217,19 @@ class MethodCallHandlerImpl(
213 result.error("MobileScanner", "Called toggleTorch() while stopped!", null) 217 result.error("MobileScanner", "Called toggleTorch() while stopped!", null)
214 } 218 }
215 } 219 }
  220 +
  221 + private fun setScale(call: MethodCall, result: MethodChannel.Result) {
  222 + try {
  223 + mobileScanner!!.setScale(call.arguments as Double)
  224 + result.success(null)
  225 + } catch (e: ZoomWhenStopped) {
  226 + result.error("MobileScanner", "Called setScale() while stopped!", null)
  227 + } catch (e: ZoomNotInRange) {
  228 + result.error("MobileScanner", "Scale should be within 0 and 1", null)
  229 + }
  230 + }
  231 +
  232 + private fun updateScanWindow(call: MethodCall) {
  233 + mobileScanner!!.scanWindow = call.argument<List<Float>>("rect")
  234 + }
216 } 235 }
@@ -86,40 +86,3 @@ class MobileScannerPermissions { @@ -86,40 +86,3 @@ class MobileScannerPermissions {
86 } 86 }
87 } 87 }
88 88
89 -/**  
90 - * This class handles incoming camera permission results.  
91 - */  
92 -@SuppressWarnings("deprecation")  
93 -private class MobileScannerPermissionsListener(  
94 - private val resultCallback: MobileScannerPermissions.ResultCallback,  
95 -): RequestPermissionsResultListener {  
96 - // There's no way to unregister permission listeners in the v1 embedding, so we'll be called  
97 - // duplicate times in cases where the user denies and then grants a permission. Keep track of if  
98 - // we've responded before and bail out of handling the callback manually if this is a repeat  
99 - // call.  
100 - private var alreadyCalled: Boolean = false  
101 -  
102 - override fun onRequestPermissionsResult(  
103 - requestCode: Int,  
104 - permissions: Array<out String>,  
105 - grantResults: IntArray  
106 - ): Boolean {  
107 - if (alreadyCalled || requestCode != MobileScannerPermissions.REQUEST_CODE) {  
108 - return false  
109 - }  
110 -  
111 - alreadyCalled = true  
112 -  
113 - // grantResults could be empty if the permissions request with the user is interrupted  
114 - // https://developer.android.com/reference/android/app/Activity#onRequestPermissionsResult(int,%20java.lang.String[],%20int[])  
115 - if (grantResults.isEmpty() || grantResults[0] != PackageManager.PERMISSION_GRANTED) {  
116 - resultCallback.onResult(  
117 - MobileScannerPermissions.CAMERA_ACCESS_DENIED,  
118 - MobileScannerPermissions.CAMERA_ACCESS_DENIED_MESSAGE)  
119 - } else {  
120 - resultCallback.onResult(null, null)  
121 - }  
122 -  
123 - return true  
124 - }  
125 -}  
  1 +package dev.steenbakker.mobile_scanner
  2 +
  3 +import android.content.pm.PackageManager
  4 +import io.flutter.plugin.common.PluginRegistry
  5 +
  6 +/**
  7 + * This class handles incoming camera permission results.
  8 + */
  9 +internal class MobileScannerPermissionsListener(
  10 + private val resultCallback: MobileScannerPermissions.ResultCallback,
  11 +): PluginRegistry.RequestPermissionsResultListener {
  12 + // There's no way to unregister permission listeners in the v1 embedding, so we'll be called
  13 + // duplicate times in cases where the user denies and then grants a permission. Keep track of if
  14 + // we've responded before and bail out of handling the callback manually if this is a repeat
  15 + // call.
  16 + private var alreadyCalled: Boolean = false
  17 +
  18 + override fun onRequestPermissionsResult(
  19 + requestCode: Int,
  20 + permissions: Array<out String>,
  21 + grantResults: IntArray
  22 + ): Boolean {
  23 + if (alreadyCalled || requestCode != MobileScannerPermissions.REQUEST_CODE) {
  24 + return false
  25 + }
  26 +
  27 + alreadyCalled = true
  28 +
  29 + // grantResults could be empty if the permissions request with the user is interrupted
  30 + // https://developer.android.com/reference/android/app/Activity#onRequestPermissionsResult(int,%20java.lang.String[],%20int[])
  31 + if (grantResults.isEmpty() || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
  32 + resultCallback.onResult(
  33 + MobileScannerPermissions.CAMERA_ACCESS_DENIED,
  34 + MobileScannerPermissions.CAMERA_ACCESS_DENIED_MESSAGE)
  35 + } else {
  36 + resultCallback.onResult(null, null)
  37 + }
  38 +
  39 + return true
  40 + }
  41 +}
@@ -8,73 +8,7 @@ import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding @@ -8,73 +8,7 @@ import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
8 class MobileScannerPlugin : FlutterPlugin, ActivityAware { 8 class MobileScannerPlugin : FlutterPlugin, ActivityAware {
9 private var activityPluginBinding: ActivityPluginBinding? = null 9 private var activityPluginBinding: ActivityPluginBinding? = null
10 private var flutterPluginBinding: FlutterPlugin.FlutterPluginBinding? = null 10 private var flutterPluginBinding: FlutterPlugin.FlutterPluginBinding? = null
11 - private var methodCallHandler: MethodCallHandlerImpl? = null  
12 - private var handler: MobileScanner? = null  
13 - private var method: MethodChannel? = null  
14 -  
15 - private lateinit var barcodeHandler: BarcodeHandler  
16 -  
17 - private var analyzerResult: MethodChannel.Result? = null  
18 -  
19 - private val callback: MobileScannerCallback = { barcodes: List<Map<String, Any?>>, image: ByteArray?, width: Int?, height: Int? ->  
20 - if (image != null) {  
21 - barcodeHandler.publishEvent(mapOf(  
22 - "name" to "barcode",  
23 - "data" to barcodes,  
24 - "image" to image,  
25 - "width" to width!!.toDouble(),  
26 - "height" to height!!.toDouble()  
27 - ))  
28 - } else {  
29 - barcodeHandler.publishEvent(mapOf(  
30 - "name" to "barcode",  
31 - "data" to barcodes  
32 - ))  
33 - }  
34 - }  
35 -  
36 - private val analyzerCallback: AnalyzerCallback = { barcodes: List<Map<String, Any?>>?->  
37 - if (barcodes != null) {  
38 - barcodeHandler.publishEvent(mapOf(  
39 - "name" to "barcode",  
40 - "data" to barcodes  
41 - ))  
42 - analyzerResult?.success(true)  
43 - } else {  
44 - analyzerResult?.success(false)  
45 - }  
46 - analyzerResult = null  
47 - }  
48 -  
49 - private val errorCallback: MobileScannerErrorCallback = {error: String ->  
50 - barcodeHandler.publishEvent(mapOf(  
51 - "name" to "error",  
52 - "data" to error,  
53 - ))  
54 - }  
55 -  
56 - private val torchStateCallback: TorchStateCallback = {state: Int ->  
57 - barcodeHandler.publishEvent(mapOf("name" to "torchState", "data" to state))  
58 - }  
59 -  
60 - @ExperimentalGetImage  
61 - override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {  
62 - if (handler == null) {  
63 - result.error("MobileScanner", "Called ${call.method} before initializing.", null)  
64 - return  
65 - }  
66 - when (call.method) {  
67 - "state" -> result.success(handler!!.hasCameraPermission())  
68 - "request" -> handler!!.requestPermission(result)  
69 - "start" -> start(call, result)  
70 - "torch" -> toggleTorch(call, result)  
71 - "stop" -> stop(result)  
72 - "analyzeImage" -> analyzeImage(call, result)  
73 - "setScale" -> setScale(call, result)  
74 - "updateScanWindow" -> updateScanWindow(call)  
75 - else -> result.notImplemented()  
76 - }  
77 - } 11 + private var methodCallHandler: MobileScannerHandler? = null
78 12
79 override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { 13 override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
80 this.flutterPluginBinding = binding 14 this.flutterPluginBinding = binding
@@ -87,7 +21,7 @@ class MobileScannerPlugin : FlutterPlugin, ActivityAware { @@ -87,7 +21,7 @@ class MobileScannerPlugin : FlutterPlugin, ActivityAware {
87 override fun onAttachedToActivity(activityPluginBinding: ActivityPluginBinding) { 21 override fun onAttachedToActivity(activityPluginBinding: ActivityPluginBinding) {
88 val binaryMessenger = this.flutterPluginBinding!!.binaryMessenger 22 val binaryMessenger = this.flutterPluginBinding!!.binaryMessenger
89 23
90 - methodCallHandler = MethodCallHandlerImpl( 24 + methodCallHandler = MobileScannerHandler(
91 activityPluginBinding.activity, 25 activityPluginBinding.activity,
92 BarcodeHandler(binaryMessenger), 26 BarcodeHandler(binaryMessenger),
93 binaryMessenger, 27 binaryMessenger,
@@ -112,19 +46,4 @@ class MobileScannerPlugin : FlutterPlugin, ActivityAware { @@ -112,19 +46,4 @@ class MobileScannerPlugin : FlutterPlugin, ActivityAware {
112 override fun onDetachedFromActivityForConfigChanges() { 46 override fun onDetachedFromActivityForConfigChanges() {
113 onDetachedFromActivity() 47 onDetachedFromActivity()
114 } 48 }
115 -  
116 - private fun setScale(call: MethodCall, result: MethodChannel.Result) {  
117 - try {  
118 - handler!!.setScale(call.arguments as Double)  
119 - result.success(null)  
120 - } catch (e: ZoomWhenStopped) {  
121 - result.error("MobileScanner", "Called setScale() while stopped!", null)  
122 - } catch (e: ZoomNotInRange) {  
123 - result.error("MobileScanner", "Scale should be within 0 and 1", null)  
124 - }  
125 - }  
126 -  
127 - private fun updateScanWindow(call: MethodCall) {  
128 - handler!!.scanWindow = call.argument<List<Float>>("rect")  
129 - }  
130 } 49 }