Просмотр исходного кода

Updated permission callback and added requests when permission is not granted. (#251)

flutter-beta
Julian Steenbakker 5 лет назад
Родитель
Сommit
a17ae80aed
Не найден GPG ключ соответствующий данной подписи Идентификатор GPG ключа: 16E437C7A3D94250
2 измененных файлов: 47 добавлений и 40 удалений
  1. +32
    -26
      android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt
  2. +15
    -14
      lib/src/qr_code_scanner.dart

+ 32
- 26
android/src/main/kotlin/net/touchcapture/qr/flutterqr/QRView.kt Просмотреть файл

@@ -41,13 +41,13 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
channel.setMethodCallHandler(this) channel.setMethodCallHandler(this)
Shared.activity?.application?.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks { Shared.activity?.application?.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
override fun onActivityPaused(p0: Activity) { override fun onActivityPaused(p0: Activity) {
if (p0 == Shared.activity && !isPaused) {
if (p0 == Shared.activity && !isPaused && hasCameraPermission()) {
barcodeView?.pause() barcodeView?.pause()
} }
} }


override fun onActivityResumed(p0: Activity) { override fun onActivityResumed(p0: Activity) {
if (p0 == Shared.activity && !isPaused) {
if (p0 == Shared.activity && !isPaused && hasCameraPermission()) {
barcodeView?.resume() barcodeView?.resume()
} }
} }
@@ -102,19 +102,23 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
private fun flipCamera(result: MethodChannel.Result) { private fun flipCamera(result: MethodChannel.Result) {
if (barcodeView == null) { if (barcodeView == null) {
return barCodeViewNotSet(result) return barCodeViewNotSet(result)
}
} else if (!hasCameraPermission()) {
checkAndRequestPermission(result)
} else {
barcodeView!!.pause()
val settings = barcodeView!!.cameraSettings


barcodeView!!.pause()
val settings = barcodeView!!.cameraSettings
if(settings.requestedCameraId == CameraInfo.CAMERA_FACING_FRONT)
settings.requestedCameraId = CameraInfo.CAMERA_FACING_BACK
else
settings.requestedCameraId = CameraInfo.CAMERA_FACING_FRONT

barcodeView!!.cameraSettings = settings
barcodeView!!.resume()
result.success(settings.requestedCameraId)
}


if(settings.requestedCameraId == CameraInfo.CAMERA_FACING_FRONT)
settings.requestedCameraId = CameraInfo.CAMERA_FACING_BACK
else
settings.requestedCameraId = CameraInfo.CAMERA_FACING_FRONT


barcodeView!!.cameraSettings = settings
barcodeView!!.resume()
result.success(settings.requestedCameraId)
} }


private fun getFlashInfo(result: MethodChannel.Result) { private fun getFlashInfo(result: MethodChannel.Result) {
@@ -142,13 +146,15 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
private fun pauseCamera(result: MethodChannel.Result) { private fun pauseCamera(result: MethodChannel.Result) {
if (barcodeView == null) { if (barcodeView == null) {
return barCodeViewNotSet(result) return barCodeViewNotSet(result)
} else if (!hasCameraPermission()) {
checkAndRequestPermission(result)
} else {
if (barcodeView!!.isPreviewActive) {
isPaused = true
barcodeView!!.pause()
}
result.success(true)
} }
if (barcodeView!!.isPreviewActive) {
isPaused = true
barcodeView!!.pause()
}
result.success(true)
} }


private fun resumeCamera(result: MethodChannel.Result) { private fun resumeCamera(result: MethodChannel.Result) {
@@ -156,13 +162,13 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
return barCodeViewNotSet(result) return barCodeViewNotSet(result)
} else if (!hasCameraPermission()) { } else if (!hasCameraPermission()) {
checkAndRequestPermission(result) checkAndRequestPermission(result)
} else {
if (!barcodeView!!.isPreviewActive) {
isPaused = false
barcodeView!!.resume()
}
result.success(true)
} }

if (!barcodeView!!.isPreviewActive) {
isPaused = false
barcodeView!!.resume()
}
result.success(true)
} }


private fun hasFlash(): Boolean { private fun hasFlash(): Boolean {
@@ -198,7 +204,7 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
} }
} else { } else {
if (hasCameraPermission()) { if (hasCameraPermission()) {
barcodeView!!.resume()
if (!isPaused) barcodeView!!.resume()
} else { } else {
checkAndRequestPermission(null) checkAndRequestPermission(null)
} }
@@ -270,7 +276,7 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
permissions: Array<out String>?, permissions: Array<out String>?,
grantResults: IntArray): Boolean { grantResults: IntArray): Boolean {


if (requestCode == Shared.CAMERA_REQUEST_ID && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (requestCode == Shared.CAMERA_REQUEST_ID && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
permissionGranted = true permissionGranted = true
channel.invokeMethod("onPermissionSet", true) channel.invokeMethod("onPermissionSet", true)
return true return true


+ 15
- 14
lib/src/qr_code_scanner.dart Просмотреть файл

@@ -64,12 +64,12 @@ class _QRViewState extends State<QRView> {
super.initState(); super.initState();
WidgetsBinding.instance.addObserver(LifecycleEventHandler( WidgetsBinding.instance.addObserver(LifecycleEventHandler(
resumeCallBack: () async => { resumeCallBack: () async => {
if (_channel != null) {
QRViewController.updateDimensions(
widget.key, _channel,
overlay: widget.overlay)
}
}));
if (_channel != null)
{
QRViewController.updateDimensions(widget.key, _channel,
overlay: widget.overlay)
}
}));
} }


@override @override
@@ -86,9 +86,9 @@ class _QRViewState extends State<QRView> {


bool onNotification(notification) { bool onNotification(notification) {
Future.microtask(() => { Future.microtask(() => {
QRViewController.updateDimensions(widget.key, _channel,
overlay: widget.overlay)
});
QRViewController.updateDimensions(widget.key, _channel,
overlay: widget.overlay)
});


return false; return false;
} }
@@ -139,9 +139,9 @@ class _QRViewState extends State<QRView> {
_channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id'); _channel = MethodChannel('net.touchcapture.qr.flutterqr/qrview_$id');


// Start scan after creation of the view // Start scan after creation of the view
final controller =
QRViewController._(_channel, widget.key, widget.onPermissionSet, widget.cameraFacing)
.._startScan(widget.key, widget.overlay, widget.formatsAllowed);
final controller = QRViewController._(
_channel, widget.key, widget.onPermissionSet, widget.cameraFacing)
.._startScan(widget.key, widget.overlay, widget.formatsAllowed);


// Initialize the controller for controlling the QRView // Initialize the controller for controlling the QRView
if (widget.onQRViewCreated != null) { if (widget.onQRViewCreated != null) {
@@ -167,7 +167,8 @@ class _QrCameraSettings {
class QRViewController { class QRViewController {
QRViewController._(MethodChannel channel, GlobalKey qrKey, QRViewController._(MethodChannel channel, GlobalKey qrKey,
PermissionSetCallback onPermissionSet, CameraFacing cameraFacing) PermissionSetCallback onPermissionSet, CameraFacing cameraFacing)
: _channel = channel, _cameraFacing = cameraFacing{
: _channel = channel,
_cameraFacing = cameraFacing {
_channel.setMethodCallHandler((call) async { _channel.setMethodCallHandler((call) async {
switch (call.method) { switch (call.method) {
case 'onRecognizeQR': case 'onRecognizeQR':
@@ -323,7 +324,7 @@ class QRViewController {
await Future.delayed(Duration(milliseconds: 300)); await Future.delayed(Duration(milliseconds: 300));
} }
final RenderBox renderBox = key.currentContext.findRenderObject(); final RenderBox renderBox = key.currentContext.findRenderObject();
try {
try {
await channel.invokeMethod('setDimensions', { await channel.invokeMethod('setDimensions', {
'width': renderBox.size.width, 'width': renderBox.size.width,
'height': renderBox.size.height, 'height': renderBox.size.height,


Загрузка…
Отмена
Сохранить