From dece91bf586102fe46dcb665e51654eef2262676 Mon Sep 17 00:00:00 2001 From: Sveinbjorn Thordarson Date: Tue, 15 Nov 2022 16:49:41 +0000 Subject: [PATCH] Finally fixed annoying permission request conflict bug --- lib/loc.dart | 40 ++++++++++++++++++---------------------- lib/main.dart | 35 +++++++++++++---------------------- lib/session.dart | 19 +++++++++---------- 3 files changed, 40 insertions(+), 54 deletions(-) diff --git a/lib/loc.dart b/lib/loc.dart index 934ad16..2c7ceeb 100644 --- a/lib/loc.dart +++ b/lib/loc.dart @@ -21,6 +21,7 @@ import 'dart:async'; import 'package:geolocator/geolocator.dart'; +import 'package:permission_handler/permission_handler.dart'; import './common.dart'; import './prefs.dart'; @@ -38,33 +39,28 @@ class LocationTracking { StreamSubscription positionStream; void start() async { - try { - LocationPermission permission = await Geolocator.requestPermission(); - if (permission == LocationPermission.denied || - permission == LocationPermission.deniedForever) { - Prefs().setBoolForKey('share_location', false); - return; - } - } catch (err) { - dlog("Error requesting permission: $err"); + // We never start location tracking if it's disabled in prefs or if we don't have permission + if (Prefs().boolForKey('share_location') == false || + await Permission.location.isGranted == false) { + dlog("Could not start location tracking, permission not granted"); + return; } - if (positionStream != null) { - // Already ongoing + // Location tracking already ongoing return; } + dlog('Starting location tracking'); - // positionStream = Geolocator.getPositionStream(desiredAccuracy: LocationAccuracy.best) - // .listen((Position position) { - // if (position == null) { - // known = false; - // return; - // } - // lat = position.latitude; - // lon = position.longitude; - // known = true; - // //dlog("Location: ${lat.toString()}, ${lon.toString()}"); - // }); + positionStream = Geolocator.getPositionStream().listen((Position position) { + if (position == null) { + known = false; + return; + } + lat = position.latitude; + lon = position.longitude; + known = true; + //dlog("Location: ${lat.toString()}, ${lon.toString()}"); + }); } void stop() { diff --git a/lib/main.dart b/lib/main.dart index d97a30f..08cd1bc 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -69,30 +69,21 @@ void main() async { // This wakelock is disabled when leaving session route Wakelock.enable(); - // Request microphone permission - // TODO: We should ask for all permissions in one request - PermissionStatus status = await Permission.microphone.request(); - if (status != PermissionStatus.granted) { - dlog("Microphone permission refused"); + // Request permissions + Map statuses = await [ + Permission.microphone, + Permission.location, + ].request(); + + if (statuses[Permission.microphone].isDenied) { + dlog("Microphone permission is denied."); } - // Request and activate location tracking - if (Prefs().boolForKey('share_location') == true) { - // Wrap in try/catch in case another location permission request is ongoing. - // This is a hack. For some reason, some versions of Android can activate a - // location permission request without being triggered by the Flutter - // permissions package, and simultaneous requests trigger an exception. - try { - LocationPermission permission = await Geolocator.requestPermission(); - if (permission != LocationPermission.denied && - permission != LocationPermission.deniedForever) { - LocationTracking().start(); - } else { - Prefs().setBoolForKey('share_location', false); - } - } catch (err) { - LocationTracking().start(); - } + if (statuses[Permission.location].isDenied) { + print("Location permission is denied."); + Prefs().setBoolForKey('share_location', false); + } else { + LocationTracking().start(); } // Launch app with session route diff --git a/lib/session.dart b/lib/session.dart index e0e49aa..4470cfa 100644 --- a/lib/session.dart +++ b/lib/session.dart @@ -154,14 +154,12 @@ class SessionRouteState extends State with TickerProviderStateMixi } Future requestMicPermissionAndStartHotwordDetection() async { - // Request microphone permission - PermissionStatus status = await Permission.microphone.request(); - if (status != PermissionStatus.granted) { - dlog("Microphone permission refused"); - } else { + if (await Permission.microphone.isGranted) { if (Prefs().boolForKey('hotword_activation') == true) { HotwordDetector().start(hotwordHandler); } + } else { + dlog("Cannot start hotword detection, microphone permission refused"); } } @@ -360,13 +358,14 @@ class SessionRouteState extends State with TickerProviderStateMixi dlog('Session start called during pre-existing session!'); return; } + dlog('Starting session'); - // if (await SpeechRecognizer().canRecognizeSpeech() == false) { - // AudioPlayer().playSound('rec_cancel'); - // showRecognitionErrorAlert(context); - // return; - // } + if (await Permission.microphone.isGranted == false) { + AudioPlayer().playSound('rec_cancel'); + showRecognitionErrorAlert(context); + return; + } // Check for internet connectivity if (await isConnectedToInternet() == false) {