Skip to content

Commit

Permalink
Finally fixed annoying permission request conflict bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinbjornt committed Nov 15, 2022
1 parent c494fac commit dece91b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 54 deletions.
40 changes: 18 additions & 22 deletions lib/loc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -38,33 +39,28 @@ class LocationTracking {
StreamSubscription<Position> 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() {
Expand Down
35 changes: 13 additions & 22 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Permission, PermissionStatus> 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
Expand Down
19 changes: 9 additions & 10 deletions lib/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,12 @@ class SessionRouteState extends State<SessionRoute> with TickerProviderStateMixi
}

Future<void> 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");
}
}

Expand Down Expand Up @@ -360,13 +358,14 @@ class SessionRouteState extends State<SessionRoute> 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) {
Expand Down

0 comments on commit dece91b

Please sign in to comment.