Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
mobile_scanner
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Navaron Bracke
2024-05-17 12:53:41 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
5b80d2c7475c4351ac0765303b27ea49d0d330b1
5b80d2c7
1 parent
47534aec
unify the error codes on Android
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
30 deletions
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScannerHandler.kt
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScannerPermissions.kt
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScannerHandler.kt
View file @
5b80d2c
...
...
@@ -10,6 +10,7 @@ import androidx.camera.core.ExperimentalGetImage
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
import dev.steenbakker.mobile_scanner.objects.BarcodeFormats
import dev.steenbakker.mobile_scanner.objects.DetectionSpeed
import dev.steenbakker.mobile_scanner.objects.MobileScannerErrorCodes
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
...
...
@@ -28,7 +29,7 @@ class MobileScannerHandler(
private val analyzeImageErrorCallback: AnalyzerErrorCallback = {
Handler(Looper.getMainLooper()).post {
analyzerResult?.error(
"MobileScanner"
, it, null)
analyzerResult?.error(
MobileScannerErrorCodes.GENERIC_ERROR
, it, null)
analyzerResult = null
}
}
...
...
@@ -106,21 +107,21 @@ class MobileScannerHandler(
@ExperimentalGetImage
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
if (mobileScanner == null) {
result.error("MobileScanner", "Called ${call.method} before initializing.", null)
return
}
when (call.method) {
"state" -> result.success(permissions.hasCameraPermission(activity))
"request" -> permissions.requestPermission(
activity,
addPermissionListener,
object: MobileScannerPermissions.ResultCallback {
override fun onResult(errorCode: String?
, errorDescription: String?
) {
override fun onResult(errorCode: String?) {
when(errorCode) {
null -> result.success(true)
MobileScannerPermissions.CAMERA_ACCESS_DENIED -> result.success(false)
else -> result.error(errorCode, errorDescription, null)
MobileScannerErrorCodes.CAMERA_ACCESS_DENIED -> result.success(false)
MobileScannerErrorCodes.CAMERA_PERMISSIONS_REQUEST_ONGOING -> result.error(
MobileScannerErrorCodes.CAMERA_PERMISSIONS_REQUEST_ONGOING,
MobileScannerErrorCodes.CAMERA_PERMISSIONS_REQUEST_ONGOING_MESSAGE, null)
else -> result.error(
MobileScannerErrorCodes.GENERIC_ERROR, MobileScannerErrorCodes.GENERIC_ERROR_MESSAGE, null)
}
}
})
...
...
@@ -185,29 +186,29 @@ class MobileScannerHandler(
when (it) {
is AlreadyStarted -> {
result.error(
"MobileScanner",
"Called start() while already started",
MobileScannerErrorCodes.ALREADY_STARTED_ERROR,
MobileScannerErrorCodes.ALREADY_STARTED_ERROR_MESSAGE,
null
)
}
is CameraError -> {
result.error(
"MobileScanner",
"Error occurred when setting up camera!",
MobileScannerErrorCodes.CAMERA_ERROR,
MobileScannerErrorCodes.CAMERA_ERROR_MESSAGE,
null
)
}
is NoCamera -> {
result.error(
"MobileScanner",
"No camera found or failed to open camera!",
MobileScannerErrorCodes.NO_CAMERA_ERROR,
MobileScannerErrorCodes.NO_CAMERA_ERROR_MESSAGE,
null
)
}
else -> {
result.error(
"MobileScanner",
"Unknown error occurred.",
MobileScannerErrorCodes.GENERIC_ERROR,
MobileScannerErrorCodes.GENERIC_ERROR_MESSAGE,
null
)
}
...
...
@@ -252,9 +253,11 @@ class MobileScannerHandler(
mobileScanner!!.setScale(call.arguments as Double)
result.success(null)
} catch (e: ZoomWhenStopped) {
result.error("MobileScanner", "Called setScale() while stopped!", null)
result.error(
MobileScannerErrorCodes.SET_SCALE_WHEN_STOPPED_ERROR, MobileScannerErrorCodes.SET_SCALE_WHEN_STOPPED_ERROR_MESSAGE, null)
} catch (e: ZoomNotInRange) {
result.error("MobileScanner", "Scale should be within 0 and 1", null)
result.error(
MobileScannerErrorCodes.GENERIC_ERROR, MobileScannerErrorCodes.INVALID_ZOOM_SCALE_ERROR_MESSAGE, null)
}
}
...
...
@@ -263,7 +266,8 @@ class MobileScannerHandler(
mobileScanner!!.resetScale()
result.success(null)
} catch (e: ZoomWhenStopped) {
result.error("MobileScanner", "Called resetScale() while stopped!", null)
result.error(
MobileScannerErrorCodes.SET_SCALE_WHEN_STOPPED_ERROR, MobileScannerErrorCodes.SET_SCALE_WHEN_STOPPED_ERROR_MESSAGE, null)
}
}
...
...
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScannerPermissions.kt
View file @
5b80d2c
...
...
@@ -5,6 +5,7 @@ import android.app.Activity
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import dev.steenbakker.mobile_scanner.objects.MobileScannerErrorCodes
import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener
/**
...
...
@@ -12,11 +13,6 @@ import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener
*/
class MobileScannerPermissions {
companion object {
const val CAMERA_ACCESS_DENIED = "CameraAccessDenied"
const val CAMERA_ACCESS_DENIED_MESSAGE = "Camera access permission was denied."
const val CAMERA_PERMISSIONS_REQUEST_ONGOING = "CameraPermissionsRequestOngoing"
const val CAMERA_PERMISSIONS_REQUEST_ONGOING_MESSAGE = "Another request is ongoing and multiple requests cannot be handled at once."
/**
* When the application's activity is [androidx.fragment.app.FragmentActivity], requestCode can only use the lower 16 bits.
* @see androidx.fragment.app.FragmentActivity.validateRequestPermissionsRequestCode
...
...
@@ -25,7 +21,7 @@ class MobileScannerPermissions {
}
interface ResultCallback {
fun onResult(errorCode: String?
, errorDescription: String?
)
fun onResult(errorCode: String?)
}
private var listener: RequestPermissionsResultListener? = null
...
...
@@ -53,14 +49,13 @@ class MobileScannerPermissions {
addPermissionListener: (RequestPermissionsResultListener) -> Unit,
callback: ResultCallback) {
if (ongoing) {
callback.onResult(
CAMERA_PERMISSIONS_REQUEST_ONGOING, CAMERA_PERMISSIONS_REQUEST_ONGOING_MESSAGE)
callback.onResult(MobileScannerErrorCodes.CAMERA_PERMISSIONS_REQUEST_ONGOING)
return
}
if(hasCameraPermission(activity) == 1) {
// Permissions already exist. Call the callback with success.
callback.onResult(null
, null
)
callback.onResult(null)
return
}
...
...
@@ -68,10 +63,10 @@ class MobileScannerPermissions {
// Keep track of the listener, so that it can be unregistered later.
listener = MobileScannerPermissionsListener(
object: ResultCallback {
override fun onResult(errorCode: String?
, errorDescription: String?
) {
override fun onResult(errorCode: String?) {
ongoing = false
listener = null
callback.onResult(errorCode
, errorDescription
)
callback.onResult(errorCode)
}
}
)
...
...
Please
register
or
login
to post a comment