Ver código fonte

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

flutter-beta
Julian Steenbakker 5 anos atrás
pai
commit
a17ae80aed
Nenhuma chave conhecida encontrada para esta assinatura no banco de dados ID da chave GPG: 16E437C7A3D94250
2 arquivos alterados com 47 adições e 40 exclusões
  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 Ver arquivo

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

override fun onActivityResumed(p0: Activity) {
if (p0 == Shared.activity && !isPaused) {
if (p0 == Shared.activity && !isPaused && hasCameraPermission()) {
barcodeView?.resume()
}
}
@@ -102,19 +102,23 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
private fun flipCamera(result: MethodChannel.Result) {
if (barcodeView == null) {
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) {
@@ -142,13 +146,15 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
private fun pauseCamera(result: MethodChannel.Result) {
if (barcodeView == null) {
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) {
@@ -156,13 +162,13 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
return barCodeViewNotSet(result)
} else if (!hasCameraPermission()) {
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 {
@@ -198,7 +204,7 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
}
} else {
if (hasCameraPermission()) {
barcodeView!!.resume()
if (!isPaused) barcodeView!!.resume()
} else {
checkAndRequestPermission(null)
}
@@ -270,7 +276,7 @@ class QRView(messenger: BinaryMessenger, id: Int, private val params: HashMap<St
permissions: Array<out String>?,
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
channel.invokeMethod("onPermissionSet", true)
return true


+ 15
- 14
lib/src/qr_code_scanner.dart Ver arquivo

@@ -64,12 +64,12 @@ class _QRViewState extends State<QRView> {
super.initState();
WidgetsBinding.instance.addObserver(LifecycleEventHandler(
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
@@ -86,9 +86,9 @@ class _QRViewState extends State<QRView> {

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

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

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


Carregando…
Cancelar
Salvar