checkPermissionStatus method
Check status of a specific permission
Implementation
@override
Future<String> checkPermissionStatus(String permission) async {
try {
debugPrint('Checking permission status for: $permission');
// Special handling for storage permissions across all Android versions
if (permission == 'android.permission.READ_EXTERNAL_STORAGE' ||
permission == 'android.permission.WRITE_EXTERNAL_STORAGE' ||
permission == 'android.permission.READ_MEDIA_IMAGES' ||
permission == 'android.permission.READ_MEDIA_VIDEO' ||
permission == 'android.permission.READ_MEDIA_AUDIO') {
if (Platform.isAndroid) {
try {
final androidVersion = await methodChannel.invokeMethod<int>(
'getAndroidVersion',
);
debugPrint(
'Checking storage permission on Android $androidVersion',
);
// Use hasModernStorageAccess which handles all Android versions appropriately
final hasAccess = await methodChannel.invokeMethod<bool>(
'hasModernStorageAccess',
);
debugPrint('Storage access check result: $hasAccess');
return hasAccess == true ? 'GRANTED' : 'DENIED';
} catch (e) {
debugPrint('Error checking storage access: $e');
}
}
}
// Special handling for alarm permission
if (permission == 'android.permission.SET_ALARM' ||
permission == 'android.permission.SCHEDULE_EXACT_ALARM') {
if (Platform.isAndroid) {
try {
// First check if we have a saved permission status
final savedStatus = await methodChannel.invokeMethod<String>(
'storage_read',
{
'key':
'permission_android.permission.SCHEDULE_EXACT_ALARM_status',
'defaultValue': '',
},
);
debugPrint('Saved alarm permission status: $savedStatus');
if (savedStatus == 'GRANTED') {
debugPrint('Using saved GRANTED status for alarm permission');
return 'GRANTED';
}
final androidVersion = await methodChannel.invokeMethod<int>(
'getAndroidVersion',
);
if (androidVersion != null && androidVersion >= 31) {
// For Android 12+, check if we can schedule exact alarms
final canSchedule = await methodChannel.invokeMethod<bool>(
'canScheduleExactAlarms',
);
// Save the status
final status = canSchedule == true ? 'GRANTED' : 'DENIED';
await methodChannel.invokeMethod<bool>('storage_write', {
'key':
'permission_android.permission.SCHEDULE_EXACT_ALARM_status',
'value': status,
});
return status;
} else {
// For Android 11 and below, no special permission is needed
await methodChannel.invokeMethod<bool>('storage_write', {
'key':
'permission_android.permission.SCHEDULE_EXACT_ALARM_status',
'value': 'GRANTED',
});
return 'GRANTED';
}
} catch (e) {
debugPrint('Error checking alarm permission: $e');
}
} else if (Platform.isIOS) {
// On iOS, check notification permission instead
return await checkPermissionStatus('ios.permission.NOTIFICATIONS');
}
}
// Handle platform-specific permission names
String platformPermission = permission;
if (Platform.isIOS && permission.startsWith('android.')) {
// Map Android permission to iOS equivalent if needed
switch (permission) {
case 'android.permission.READ_PHONE_STATE':
return 'NOT_SUPPORTED';
// Add other mappings as needed
}
}
final result = await methodChannel.invokeMethod<String>(
'checkPermissionStatus',
{'permission': platformPermission},
);
debugPrint('Permission status result: $result');
return result ?? 'DENIED';
} on PlatformException catch (e) {
debugPrint('Error checking permission status: ${e.message}');
if (e.code == 'PERMISSION_NOT_FOUND') {
return 'NOT_SUPPORTED';
}
return 'ERROR';
}
}