mirror of
https://github.com/simplezhli/flutter_barcode_reader.git
synced 2024-11-23 08:19:21 +08:00
Added error handling for declined permission requests.
This commit is contained in:
parent
9b88c3ac57
commit
51f43844d7
@ -41,7 +41,11 @@ class BarcodeScanPlugin(val activity: Activity): MethodCallHandler,
|
|||||||
if (resultCode == Activity.RESULT_OK) {
|
if (resultCode == Activity.RESULT_OK) {
|
||||||
val barcode = data?.getStringExtra("SCAN_RESULT")
|
val barcode = data?.getStringExtra("SCAN_RESULT")
|
||||||
barcode?.let { this.result?.success(barcode) }
|
barcode?.let { this.result?.success(barcode) }
|
||||||
|
} else {
|
||||||
|
val errorCode = data?.getStringExtra("ERROR_CODE")
|
||||||
|
this.result?.error(errorCode, null, null)
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,6 @@ import android.app.Activity
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.support.design.widget.Snackbar
|
|
||||||
import android.support.v4.app.ActivityCompat
|
import android.support.v4.app.ActivityCompat
|
||||||
import android.support.v4.content.ContextCompat
|
import android.support.v4.content.ContextCompat
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
@ -38,22 +37,19 @@ import me.dm7.barcodescanner.zxing.ZXingScannerView
|
|||||||
class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
||||||
|
|
||||||
lateinit var scannerView: me.dm7.barcodescanner.zxing.ZXingScannerView
|
lateinit var scannerView: me.dm7.barcodescanner.zxing.ZXingScannerView
|
||||||
//lateinit var pressLightSnackBar: Snackbar
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val REQUEST_TAKE_PHOTO_CAMERA_PERMISSION = 100
|
val REQUEST_TAKE_PHOTO_CAMERA_PERMISSION = 100
|
||||||
val TOGGLE_FLASH = 200
|
val TOGGLE_FLASH = 200
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
title = ""
|
title = ""
|
||||||
scannerView = ZXingScannerView(this)
|
scannerView = ZXingScannerView(this)
|
||||||
scannerView.setAutoFocus(true)
|
scannerView.setAutoFocus(true)
|
||||||
setContentView(scannerView)
|
setContentView(scannerView)
|
||||||
// pressLightSnackBar = Snackbar.make(scannerView, "Press Screen To Turn Light On",
|
|
||||||
// Int.MAX_VALUE)
|
|
||||||
// pressLightSnackBar.show()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
@ -81,6 +77,7 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
|||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
scannerView.setResultHandler(this)
|
scannerView.setResultHandler(this)
|
||||||
|
// start camera immediately if permission is already given
|
||||||
if (!requestCameraAccessIfNecessary()) {
|
if (!requestCameraAccessIfNecessary()) {
|
||||||
scannerView.startCamera()
|
scannerView.startCamera()
|
||||||
}
|
}
|
||||||
@ -98,39 +95,39 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
|||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun finishWithError(errorCode: String) {
|
||||||
|
val intent = Intent()
|
||||||
|
intent.putExtra("ERROR_CODE", errorCode)
|
||||||
|
setResult(Activity.RESULT_CANCELED, intent)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
|
||||||
private fun requestCameraAccessIfNecessary(): Boolean {
|
private fun requestCameraAccessIfNecessary(): Boolean {
|
||||||
val array = arrayOf(Manifest.permission.CAMERA)
|
val array = arrayOf(Manifest.permission.CAMERA)
|
||||||
if (ContextCompat
|
if (ContextCompat
|
||||||
.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||||
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
|
|
||||||
val snackbar = Snackbar.make(scannerView, "Grant access to the camera to enable barcode scanning.", Snackbar.LENGTH_INDEFINITE)
|
ActivityCompat.requestPermissions(this, array,
|
||||||
snackbar.setAction("OK") {
|
|
||||||
ActivityCompat.requestPermissions(this,
|
|
||||||
array,
|
|
||||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
|
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
|
||||||
}
|
|
||||||
snackbar.show()
|
|
||||||
} else {
|
|
||||||
ActivityCompat.requestPermissions(this,
|
|
||||||
array,
|
|
||||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
|
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
|
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,grantResults: IntArray) {
|
||||||
grantResults: IntArray) {
|
|
||||||
when (requestCode) {
|
when (requestCode) {
|
||||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION -> {
|
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION -> {
|
||||||
if (PermissionUtil.verifyPermissions(grantResults)) {
|
if (PermissionUtil.verifyPermissions(grantResults)) {
|
||||||
scannerView.startCamera()
|
scannerView.startCamera()
|
||||||
|
} else {
|
||||||
|
finishWithError("PERMISSION_NOT_GRANTED")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
else -> {
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object PermissionUtil {
|
object PermissionUtil {
|
||||||
|
@ -2,6 +2,7 @@ import 'dart:async';
|
|||||||
|
|
||||||
import 'package:barcode_scan/barcode_scan.dart';
|
import 'package:barcode_scan/barcode_scan.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(new MyApp());
|
runApp(new MyApp());
|
||||||
@ -43,7 +44,21 @@ class _MyAppState extends State<MyApp> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future scan() async {
|
Future scan() async {
|
||||||
|
try {
|
||||||
String barcode = await BarcodeScanner.scan();
|
String barcode = await BarcodeScanner.scan();
|
||||||
setState(() => this.barcode = barcode);
|
setState(() => this.barcode = barcode);
|
||||||
|
} on PlatformException catch (e) {
|
||||||
|
if (e.code == 'PERMISSION_NOT_GRANTED') {
|
||||||
|
setState(() {
|
||||||
|
this.barcode = 'The user did not grant the camera permission!';
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setState(() => this.barcode = 'Unknown error: $e');
|
||||||
|
}
|
||||||
|
} on FormatException{
|
||||||
|
setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
|
||||||
|
} catch (e) {
|
||||||
|
setState(() => this.barcode = 'Unknown error: $e');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user