From dea7e933ee99f8df5f44aa9886a3db4feb9b4a81 Mon Sep 17 00:00:00 2001 From: Joshua Hoogstraat <34964599+jhoogstraat@users.noreply.github.com> Date: Sat, 23 Oct 2021 11:48:12 +0200 Subject: [PATCH] Fix no error reported on Android --- fast_barcode_scanner/CHANGELOG.md | 1 + .../jhoogstraat/fast_barcode_scanner/BarcodeReader.kt | 10 ++++++++-- fast_barcode_scanner/lib/src/barcode_camera.dart | 7 +++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/fast_barcode_scanner/CHANGELOG.md b/fast_barcode_scanner/CHANGELOG.md index 4c3eab80..a91b0d9c 100644 --- a/fast_barcode_scanner/CHANGELOG.md +++ b/fast_barcode_scanner/CHANGELOG.md @@ -1,6 +1,7 @@ ## 1.1.4 - Fixes `pauseDetector` on iOS +- Fixes no error reported when permissions are denied on Android - Updates CameraX (compileSdk is now 31) ## 1.1.3 diff --git a/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt b/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt index 3b3de10d..b8b00366 100644 --- a/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt +++ b/fast_barcode_scanner/android/src/main/kotlin/com/jhoogstraat/fast_barcode_scanner/BarcodeReader.kt @@ -43,6 +43,7 @@ class BarcodeReader(private val flutterTextureEntry: TextureRegistry.SurfaceText /* State */ private var isInitialized = false private var pauseDetection = false + private var pendingResult: Result? = null fun attachToActivity(activity: Activity) { this.activity = activity @@ -75,15 +76,15 @@ class BarcodeReader(private val flutterTextureEntry: TextureRegistry.SurfaceText if (allPermissionsGranted()) { initCamera() + result.success(hashMapOf("textureId" to flutterTextureEntry.id(), "surfaceOrientation" to 0, "surfaceHeight" to 1280, "surfaceWidth" to 720)) } else { + pendingResult = result ActivityCompat.requestPermissions( activity!!, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS ) } - - result.success(hashMapOf("textureId" to flutterTextureEntry.id(), "surfaceOrientation" to 0, "surfaceHeight" to 1280, "surfaceWidth" to 720)) } fun stop(result: Result? = null) { @@ -113,6 +114,11 @@ class BarcodeReader(private val flutterTextureEntry: TextureRegistry.SurfaceText if (requestCode == REQUEST_CODE_PERMISSIONS) { if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) { initCamera() + } else { + pendingResult?.let { + it.error("UNAUTHORIZED", "The application is not authorized to use the camera device", null) + pendingResult = null + } } } diff --git a/fast_barcode_scanner/lib/src/barcode_camera.dart b/fast_barcode_scanner/lib/src/barcode_camera.dart index dec2fadf..fb2c24d8 100644 --- a/fast_barcode_scanner/lib/src/barcode_camera.dart +++ b/fast_barcode_scanner/lib/src/barcode_camera.dart @@ -9,8 +9,11 @@ import 'package:flutter/rendering.dart'; typedef ErrorCallback = Widget Function(BuildContext context, Object? error); Widget _defaultOnError(BuildContext context, Object? error) { - debugPrint("Error reading from camera: $error"); - return const Center(child: Text("Error reading from camera...")); + return const Center( + child: Text( + "Error reading from camera...", + style: TextStyle(color: Colors.white), + )); } /// The main class connecting the platform code to the UI.