Navaron Bracke

unify the error codes on Android

@@ -10,6 +10,7 @@ import androidx.camera.core.ExperimentalGetImage @@ -10,6 +10,7 @@ import androidx.camera.core.ExperimentalGetImage
10 import com.google.mlkit.vision.barcode.BarcodeScannerOptions 10 import com.google.mlkit.vision.barcode.BarcodeScannerOptions
11 import dev.steenbakker.mobile_scanner.objects.BarcodeFormats 11 import dev.steenbakker.mobile_scanner.objects.BarcodeFormats
12 import dev.steenbakker.mobile_scanner.objects.DetectionSpeed 12 import dev.steenbakker.mobile_scanner.objects.DetectionSpeed
  13 +import dev.steenbakker.mobile_scanner.objects.MobileScannerErrorCodes
13 import io.flutter.plugin.common.BinaryMessenger 14 import io.flutter.plugin.common.BinaryMessenger
14 import io.flutter.plugin.common.MethodCall 15 import io.flutter.plugin.common.MethodCall
15 import io.flutter.plugin.common.MethodChannel 16 import io.flutter.plugin.common.MethodChannel
@@ -28,7 +29,7 @@ class MobileScannerHandler( @@ -28,7 +29,7 @@ class MobileScannerHandler(
28 29
29 private val analyzeImageErrorCallback: AnalyzerErrorCallback = { 30 private val analyzeImageErrorCallback: AnalyzerErrorCallback = {
30 Handler(Looper.getMainLooper()).post { 31 Handler(Looper.getMainLooper()).post {
31 - analyzerResult?.error("MobileScanner", it, null) 32 + analyzerResult?.error(MobileScannerErrorCodes.GENERIC_ERROR, it, null)
32 analyzerResult = null 33 analyzerResult = null
33 } 34 }
34 } 35 }
@@ -106,21 +107,21 @@ class MobileScannerHandler( @@ -106,21 +107,21 @@ class MobileScannerHandler(
106 107
107 @ExperimentalGetImage 108 @ExperimentalGetImage
108 override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { 109 override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
109 - if (mobileScanner == null) {  
110 - result.error("MobileScanner", "Called ${call.method} before initializing.", null)  
111 - return  
112 - }  
113 when (call.method) { 110 when (call.method) {
114 "state" -> result.success(permissions.hasCameraPermission(activity)) 111 "state" -> result.success(permissions.hasCameraPermission(activity))
115 "request" -> permissions.requestPermission( 112 "request" -> permissions.requestPermission(
116 activity, 113 activity,
117 addPermissionListener, 114 addPermissionListener,
118 object: MobileScannerPermissions.ResultCallback { 115 object: MobileScannerPermissions.ResultCallback {
119 - override fun onResult(errorCode: String?, errorDescription: String?) { 116 + override fun onResult(errorCode: String?) {
120 when(errorCode) { 117 when(errorCode) {
121 null -> result.success(true) 118 null -> result.success(true)
122 - MobileScannerPermissions.CAMERA_ACCESS_DENIED -> result.success(false)  
123 - else -> result.error(errorCode, errorDescription, null) 119 + MobileScannerErrorCodes.CAMERA_ACCESS_DENIED -> result.success(false)
  120 + MobileScannerErrorCodes.CAMERA_PERMISSIONS_REQUEST_ONGOING -> result.error(
  121 + MobileScannerErrorCodes.CAMERA_PERMISSIONS_REQUEST_ONGOING,
  122 + MobileScannerErrorCodes.CAMERA_PERMISSIONS_REQUEST_ONGOING_MESSAGE, null)
  123 + else -> result.error(
  124 + MobileScannerErrorCodes.GENERIC_ERROR, MobileScannerErrorCodes.GENERIC_ERROR_MESSAGE, null)
124 } 125 }
125 } 126 }
126 }) 127 })
@@ -185,29 +186,29 @@ class MobileScannerHandler( @@ -185,29 +186,29 @@ class MobileScannerHandler(
185 when (it) { 186 when (it) {
186 is AlreadyStarted -> { 187 is AlreadyStarted -> {
187 result.error( 188 result.error(
188 - "MobileScanner",  
189 - "Called start() while already started", 189 + MobileScannerErrorCodes.ALREADY_STARTED_ERROR,
  190 + MobileScannerErrorCodes.ALREADY_STARTED_ERROR_MESSAGE,
190 null 191 null
191 ) 192 )
192 } 193 }
193 is CameraError -> { 194 is CameraError -> {
194 result.error( 195 result.error(
195 - "MobileScanner",  
196 - "Error occurred when setting up camera!", 196 + MobileScannerErrorCodes.CAMERA_ERROR,
  197 + MobileScannerErrorCodes.CAMERA_ERROR_MESSAGE,
197 null 198 null
198 ) 199 )
199 } 200 }
200 is NoCamera -> { 201 is NoCamera -> {
201 result.error( 202 result.error(
202 - "MobileScanner",  
203 - "No camera found or failed to open camera!", 203 + MobileScannerErrorCodes.NO_CAMERA_ERROR,
  204 + MobileScannerErrorCodes.NO_CAMERA_ERROR_MESSAGE,
204 null 205 null
205 ) 206 )
206 } 207 }
207 else -> { 208 else -> {
208 result.error( 209 result.error(
209 - "MobileScanner",  
210 - "Unknown error occurred.", 210 + MobileScannerErrorCodes.GENERIC_ERROR,
  211 + MobileScannerErrorCodes.GENERIC_ERROR_MESSAGE,
211 null 212 null
212 ) 213 )
213 } 214 }
@@ -252,9 +253,11 @@ class MobileScannerHandler( @@ -252,9 +253,11 @@ class MobileScannerHandler(
252 mobileScanner!!.setScale(call.arguments as Double) 253 mobileScanner!!.setScale(call.arguments as Double)
253 result.success(null) 254 result.success(null)
254 } catch (e: ZoomWhenStopped) { 255 } catch (e: ZoomWhenStopped) {
255 - result.error("MobileScanner", "Called setScale() while stopped!", null) 256 + result.error(
  257 + MobileScannerErrorCodes.SET_SCALE_WHEN_STOPPED_ERROR, MobileScannerErrorCodes.SET_SCALE_WHEN_STOPPED_ERROR_MESSAGE, null)
256 } catch (e: ZoomNotInRange) { 258 } catch (e: ZoomNotInRange) {
257 - result.error("MobileScanner", "Scale should be within 0 and 1", null) 259 + result.error(
  260 + MobileScannerErrorCodes.GENERIC_ERROR, MobileScannerErrorCodes.INVALID_ZOOM_SCALE_ERROR_MESSAGE, null)
258 } 261 }
259 } 262 }
260 263
@@ -263,7 +266,8 @@ class MobileScannerHandler( @@ -263,7 +266,8 @@ class MobileScannerHandler(
263 mobileScanner!!.resetScale() 266 mobileScanner!!.resetScale()
264 result.success(null) 267 result.success(null)
265 } catch (e: ZoomWhenStopped) { 268 } catch (e: ZoomWhenStopped) {
266 - result.error("MobileScanner", "Called resetScale() while stopped!", null) 269 + result.error(
  270 + MobileScannerErrorCodes.SET_SCALE_WHEN_STOPPED_ERROR, MobileScannerErrorCodes.SET_SCALE_WHEN_STOPPED_ERROR_MESSAGE, null)
267 } 271 }
268 } 272 }
269 273
@@ -5,6 +5,7 @@ import android.app.Activity @@ -5,6 +5,7 @@ import android.app.Activity
5 import android.content.pm.PackageManager 5 import android.content.pm.PackageManager
6 import androidx.core.app.ActivityCompat 6 import androidx.core.app.ActivityCompat
7 import androidx.core.content.ContextCompat 7 import androidx.core.content.ContextCompat
  8 +import dev.steenbakker.mobile_scanner.objects.MobileScannerErrorCodes
8 import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener 9 import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener
9 10
10 /** 11 /**
@@ -12,11 +13,6 @@ import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener @@ -12,11 +13,6 @@ import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener
12 */ 13 */
13 class MobileScannerPermissions { 14 class MobileScannerPermissions {
14 companion object { 15 companion object {
15 - const val CAMERA_ACCESS_DENIED = "CameraAccessDenied"  
16 - const val CAMERA_ACCESS_DENIED_MESSAGE = "Camera access permission was denied."  
17 - const val CAMERA_PERMISSIONS_REQUEST_ONGOING = "CameraPermissionsRequestOngoing"  
18 - const val CAMERA_PERMISSIONS_REQUEST_ONGOING_MESSAGE = "Another request is ongoing and multiple requests cannot be handled at once."  
19 -  
20 /** 16 /**
21 * When the application's activity is [androidx.fragment.app.FragmentActivity], requestCode can only use the lower 16 bits. 17 * When the application's activity is [androidx.fragment.app.FragmentActivity], requestCode can only use the lower 16 bits.
22 * @see androidx.fragment.app.FragmentActivity.validateRequestPermissionsRequestCode 18 * @see androidx.fragment.app.FragmentActivity.validateRequestPermissionsRequestCode
@@ -25,7 +21,7 @@ class MobileScannerPermissions { @@ -25,7 +21,7 @@ class MobileScannerPermissions {
25 } 21 }
26 22
27 interface ResultCallback { 23 interface ResultCallback {
28 - fun onResult(errorCode: String?, errorDescription: String?) 24 + fun onResult(errorCode: String?)
29 } 25 }
30 26
31 private var listener: RequestPermissionsResultListener? = null 27 private var listener: RequestPermissionsResultListener? = null
@@ -53,14 +49,13 @@ class MobileScannerPermissions { @@ -53,14 +49,13 @@ class MobileScannerPermissions {
53 addPermissionListener: (RequestPermissionsResultListener) -> Unit, 49 addPermissionListener: (RequestPermissionsResultListener) -> Unit,
54 callback: ResultCallback) { 50 callback: ResultCallback) {
55 if (ongoing) { 51 if (ongoing) {
56 - callback.onResult(  
57 - CAMERA_PERMISSIONS_REQUEST_ONGOING, CAMERA_PERMISSIONS_REQUEST_ONGOING_MESSAGE) 52 + callback.onResult(MobileScannerErrorCodes.CAMERA_PERMISSIONS_REQUEST_ONGOING)
58 return 53 return
59 } 54 }
60 55
61 if(hasCameraPermission(activity) == 1) { 56 if(hasCameraPermission(activity) == 1) {
62 // Permissions already exist. Call the callback with success. 57 // Permissions already exist. Call the callback with success.
63 - callback.onResult(null, null) 58 + callback.onResult(null)
64 return 59 return
65 } 60 }
66 61
@@ -68,10 +63,10 @@ class MobileScannerPermissions { @@ -68,10 +63,10 @@ class MobileScannerPermissions {
68 // Keep track of the listener, so that it can be unregistered later. 63 // Keep track of the listener, so that it can be unregistered later.
69 listener = MobileScannerPermissionsListener( 64 listener = MobileScannerPermissionsListener(
70 object: ResultCallback { 65 object: ResultCallback {
71 - override fun onResult(errorCode: String?, errorDescription: String?) { 66 + override fun onResult(errorCode: String?) {
72 ongoing = false 67 ongoing = false
73 listener = null 68 listener = null
74 - callback.onResult(errorCode, errorDescription) 69 + callback.onResult(errorCode)
75 } 70 }
76 } 71 }
77 ) 72 )