mirror of
https://github.com/simplezhli/flutter_barcode_reader.git
synced 2024-11-23 03:19:24 +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) {
|
||||
val barcode = data?.getStringExtra("SCAN_RESULT")
|
||||
barcode?.let { this.result?.success(barcode) }
|
||||
} else {
|
||||
val errorCode = data?.getStringExtra("ERROR_CODE")
|
||||
this.result?.error(errorCode, null, null)
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
@ -26,7 +26,6 @@ import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import android.support.design.widget.Snackbar
|
||||
import android.support.v4.app.ActivityCompat
|
||||
import android.support.v4.content.ContextCompat
|
||||
import android.view.Menu
|
||||
@ -38,22 +37,19 @@ import me.dm7.barcodescanner.zxing.ZXingScannerView
|
||||
class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
||||
|
||||
lateinit var scannerView: me.dm7.barcodescanner.zxing.ZXingScannerView
|
||||
//lateinit var pressLightSnackBar: Snackbar
|
||||
|
||||
companion object {
|
||||
val REQUEST_TAKE_PHOTO_CAMERA_PERMISSION = 100
|
||||
val TOGGLE_FLASH = 200
|
||||
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
title = ""
|
||||
scannerView = ZXingScannerView(this)
|
||||
scannerView.setAutoFocus(true)
|
||||
setContentView(scannerView)
|
||||
// pressLightSnackBar = Snackbar.make(scannerView, "Press Screen To Turn Light On",
|
||||
// Int.MAX_VALUE)
|
||||
// pressLightSnackBar.show()
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
@ -81,6 +77,7 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
scannerView.setResultHandler(this)
|
||||
// start camera immediately if permission is already given
|
||||
if (!requestCameraAccessIfNecessary()) {
|
||||
scannerView.startCamera()
|
||||
}
|
||||
@ -98,40 +95,40 @@ class BarcodeScannerActivity : Activity(), ZXingScannerView.ResultHandler {
|
||||
finish()
|
||||
}
|
||||
|
||||
fun finishWithError(errorCode: String) {
|
||||
val intent = Intent()
|
||||
intent.putExtra("ERROR_CODE", errorCode)
|
||||
setResult(Activity.RESULT_CANCELED, intent)
|
||||
finish()
|
||||
}
|
||||
|
||||
private fun requestCameraAccessIfNecessary(): Boolean {
|
||||
val array = arrayOf(Manifest.permission.CAMERA)
|
||||
if (ContextCompat
|
||||
.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)
|
||||
snackbar.setAction("OK") {
|
||||
ActivityCompat.requestPermissions(this,
|
||||
array,
|
||||
|
||||
ActivityCompat.requestPermissions(this, array,
|
||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
|
||||
}
|
||||
snackbar.show()
|
||||
} else {
|
||||
ActivityCompat.requestPermissions(this,
|
||||
array,
|
||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION)
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
|
||||
grantResults: IntArray) {
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,grantResults: IntArray) {
|
||||
when (requestCode) {
|
||||
REQUEST_TAKE_PHOTO_CAMERA_PERMISSION -> {
|
||||
if (PermissionUtil.verifyPermissions(grantResults)) {
|
||||
scannerView.startCamera()
|
||||
} else {
|
||||
finishWithError("PERMISSION_NOT_GRANTED")
|
||||
}
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object PermissionUtil {
|
||||
|
||||
|
@ -2,6 +2,7 @@ import 'dart:async';
|
||||
|
||||
import 'package:barcode_scan/barcode_scan.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
void main() {
|
||||
runApp(new MyApp());
|
||||
@ -43,7 +44,21 @@ class _MyAppState extends State<MyApp> {
|
||||
}
|
||||
|
||||
Future scan() async {
|
||||
try {
|
||||
String barcode = await BarcodeScanner.scan();
|
||||
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