fix: permission screen location request - request WhenInUse first then Always
This commit is contained in:
parent
93d5534755
commit
4f16db59b2
1 changed files with 34 additions and 14 deletions
|
|
@ -56,32 +56,54 @@ class _PermissionScreenState extends State<PermissionScreen> {
|
||||||
_locationGranted && _notificationGranted && _batteryOptOut;
|
_locationGranted && _notificationGranted && _batteryOptOut;
|
||||||
|
|
||||||
Future<void> _requestLocation() async {
|
Future<void> _requestLocation() async {
|
||||||
final status = await Permission.locationAlways.request();
|
// Check current status first
|
||||||
|
var status = await Permission.locationAlways.status;
|
||||||
|
if (status.isGranted) {
|
||||||
|
if (mounted) setState(() => _locationGranted = true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// On Android, request locationWhenInUse first before locationAlways
|
||||||
|
var whenInUse = await Permission.locationWhenInUse.status;
|
||||||
|
if (!whenInUse.isGranted) {
|
||||||
|
whenInUse = await Permission.locationWhenInUse.request();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now request locationAlways (background permission)
|
||||||
|
status = await Permission.locationAlways.request();
|
||||||
|
|
||||||
if (!status.isGranted && status.isPermanentlyDenied) {
|
if (!status.isGranted && status.isPermanentlyDenied) {
|
||||||
// Permission permanently denied - open app settings
|
|
||||||
await openAppSettings();
|
await openAppSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final finalStatus = await Permission.locationAlways.status;
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
final newStatus = await Permission.locationAlways.status;
|
setState(() => _locationGranted = finalStatus.isGranted);
|
||||||
setState(() => _locationGranted = newStatus.isGranted);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _requestNotification() async {
|
Future<void> _requestNotification() async {
|
||||||
final status = await Permission.notification.request();
|
var status = await Permission.notification.status;
|
||||||
|
if (status.isGranted) {
|
||||||
|
if (mounted) setState(() => _notificationGranted = true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = await Permission.notification.request();
|
||||||
|
|
||||||
if (!status.isGranted && status.isPermanentlyDenied) {
|
if (!status.isGranted && status.isPermanentlyDenied) {
|
||||||
await openAppSettings();
|
await openAppSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final finalStatus = await Permission.notification.status;
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
final newStatus = await Permission.notification.status;
|
setState(() => _notificationGranted = finalStatus.isGranted);
|
||||||
setState(() => _notificationGranted = newStatus.isGranted);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _requestBatteryOptOut() async {
|
Future<void> _requestBatteryOptOut() async {
|
||||||
final opened = await LocationBridge.openBatteryOptimizationSettings();
|
final opened = await LocationBridge.openBatteryOptimizationSettings();
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
// Show dialog after the settings screen opens
|
|
||||||
_showBatteryOptDialog(opened);
|
_showBatteryOptDialog(opened);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -109,8 +131,8 @@ class _PermissionScreenState extends State<PermissionScreen> {
|
||||||
content: Text(
|
content: Text(
|
||||||
settingsOpened
|
settingsOpened
|
||||||
? 'A settings screen has opened.\n\n'
|
? 'A settings screen has opened.\n\n'
|
||||||
'Find "Traccar Client" and set it to "Don\'t optimize" or "Unrestricted", then come back and tap DONE below.'
|
'Find "Traccar Client" and set it to "Don\'t optimize" or "Unrestricted", then come back and tap CHECK & DONE.'
|
||||||
: 'Open Settings → Apps → Traccar Client → Battery and select "Don\'t optimize" or "Unrestricted", then come back and tap DONE below.',
|
: 'Open Settings → Apps → Traccar Client → Battery and select "Don\'t optimize" or "Unrestricted", then come back and tap CHECK & DONE.',
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
fontFamily: 'monospace',
|
fontFamily: 'monospace',
|
||||||
fontSize: 12,
|
fontSize: 12,
|
||||||
|
|
@ -121,7 +143,6 @@ class _PermissionScreenState extends State<PermissionScreen> {
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
Navigator.pop(ctx);
|
Navigator.pop(ctx);
|
||||||
// Open app details settings as fallback
|
|
||||||
openAppSettings();
|
openAppSettings();
|
||||||
},
|
},
|
||||||
child: const Text(
|
child: const Text(
|
||||||
|
|
@ -138,7 +159,6 @@ class _PermissionScreenState extends State<PermissionScreen> {
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
Navigator.pop(ctx);
|
Navigator.pop(ctx);
|
||||||
// Re-check battery optimization state via native
|
|
||||||
final disabled = await _isBatteryOptimizationDisabled();
|
final disabled = await _isBatteryOptimizationDisabled();
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
setState(() => _batteryOptOut = disabled);
|
setState(() => _batteryOptOut = disabled);
|
||||||
|
|
@ -273,8 +293,8 @@ class _PermissionScreenState extends State<PermissionScreen> {
|
||||||
size: 20,
|
size: 20,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
SizedBox(width: 14),
|
const SizedBox(width: 14),
|
||||||
Expanded(
|
const Expanded(
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue