Skip to content

Commit dece91b

Browse files
committed
Finally fixed annoying permission request conflict bug
1 parent c494fac commit dece91b

File tree

3 files changed

+40
-54
lines changed

3 files changed

+40
-54
lines changed

lib/loc.dart

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import 'dart:async';
2222

2323
import 'package:geolocator/geolocator.dart';
24+
import 'package:permission_handler/permission_handler.dart';
2425

2526
import './common.dart';
2627
import './prefs.dart';
@@ -38,33 +39,28 @@ class LocationTracking {
3839
StreamSubscription<Position> positionStream;
3940

4041
void start() async {
41-
try {
42-
LocationPermission permission = await Geolocator.requestPermission();
43-
if (permission == LocationPermission.denied ||
44-
permission == LocationPermission.deniedForever) {
45-
Prefs().setBoolForKey('share_location', false);
46-
return;
47-
}
48-
} catch (err) {
49-
dlog("Error requesting permission: $err");
42+
// We never start location tracking if it's disabled in prefs or if we don't have permission
43+
if (Prefs().boolForKey('share_location') == false ||
44+
await Permission.location.isGranted == false) {
45+
dlog("Could not start location tracking, permission not granted");
46+
return;
5047
}
51-
5248
if (positionStream != null) {
53-
// Already ongoing
49+
// Location tracking already ongoing
5450
return;
5551
}
52+
5653
dlog('Starting location tracking');
57-
// positionStream = Geolocator.getPositionStream(desiredAccuracy: LocationAccuracy.best)
58-
// .listen((Position position) {
59-
// if (position == null) {
60-
// known = false;
61-
// return;
62-
// }
63-
// lat = position.latitude;
64-
// lon = position.longitude;
65-
// known = true;
66-
// //dlog("Location: ${lat.toString()}, ${lon.toString()}");
67-
// });
54+
positionStream = Geolocator.getPositionStream().listen((Position position) {
55+
if (position == null) {
56+
known = false;
57+
return;
58+
}
59+
lat = position.latitude;
60+
lon = position.longitude;
61+
known = true;
62+
//dlog("Location: ${lat.toString()}, ${lon.toString()}");
63+
});
6864
}
6965

7066
void stop() {

lib/main.dart

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,21 @@ void main() async {
6969
// This wakelock is disabled when leaving session route
7070
Wakelock.enable();
7171

72-
// Request microphone permission
73-
// TODO: We should ask for all permissions in one request
74-
PermissionStatus status = await Permission.microphone.request();
75-
if (status != PermissionStatus.granted) {
76-
dlog("Microphone permission refused");
72+
// Request permissions
73+
Map<Permission, PermissionStatus> statuses = await [
74+
Permission.microphone,
75+
Permission.location,
76+
].request();
77+
78+
if (statuses[Permission.microphone].isDenied) {
79+
dlog("Microphone permission is denied.");
7780
}
7881

79-
// Request and activate location tracking
80-
if (Prefs().boolForKey('share_location') == true) {
81-
// Wrap in try/catch in case another location permission request is ongoing.
82-
// This is a hack. For some reason, some versions of Android can activate a
83-
// location permission request without being triggered by the Flutter
84-
// permissions package, and simultaneous requests trigger an exception.
85-
try {
86-
LocationPermission permission = await Geolocator.requestPermission();
87-
if (permission != LocationPermission.denied &&
88-
permission != LocationPermission.deniedForever) {
89-
LocationTracking().start();
90-
} else {
91-
Prefs().setBoolForKey('share_location', false);
92-
}
93-
} catch (err) {
94-
LocationTracking().start();
95-
}
82+
if (statuses[Permission.location].isDenied) {
83+
print("Location permission is denied.");
84+
Prefs().setBoolForKey('share_location', false);
85+
} else {
86+
LocationTracking().start();
9687
}
9788

9889
// Launch app with session route

lib/session.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,12 @@ class SessionRouteState extends State<SessionRoute> with TickerProviderStateMixi
154154
}
155155

156156
Future<void> requestMicPermissionAndStartHotwordDetection() async {
157-
// Request microphone permission
158-
PermissionStatus status = await Permission.microphone.request();
159-
if (status != PermissionStatus.granted) {
160-
dlog("Microphone permission refused");
161-
} else {
157+
if (await Permission.microphone.isGranted) {
162158
if (Prefs().boolForKey('hotword_activation') == true) {
163159
HotwordDetector().start(hotwordHandler);
164160
}
161+
} else {
162+
dlog("Cannot start hotword detection, microphone permission refused");
165163
}
166164
}
167165

@@ -360,13 +358,14 @@ class SessionRouteState extends State<SessionRoute> with TickerProviderStateMixi
360358
dlog('Session start called during pre-existing session!');
361359
return;
362360
}
361+
363362
dlog('Starting session');
364363

365-
// if (await SpeechRecognizer().canRecognizeSpeech() == false) {
366-
// AudioPlayer().playSound('rec_cancel');
367-
// showRecognitionErrorAlert(context);
368-
// return;
369-
// }
364+
if (await Permission.microphone.isGranted == false) {
365+
AudioPlayer().playSound('rec_cancel');
366+
showRecognitionErrorAlert(context);
367+
return;
368+
}
370369

371370
// Check for internet connectivity
372371
if (await isConnectedToInternet() == false) {

0 commit comments

Comments
 (0)