askForAllPermissions method
Asking for permissions for all the measures included in this study.
Since we only ask for permission relevant to the deployment, this method should be called after deployment has taken place but before this controller is resumed.
This method is only relevant on Android, and does nothing on iOS. iOS automatically asks for permissions when a resource is accessed.
Note that location permissions are never asked for in this method, since they can cause issues when asking for multiple permissions at once. Location permissions should be handled separately in the app.
Implementation
Future<void> askForAllPermissions() async {
if (deployment == null) {
warning(
'$runtimeType - No deployment available. Skipping requesting permissions.',
);
return;
}
if (Platform.isIOS) {
warning(
'$runtimeType - Requesting all permissions at once is not feasible on iOS. Skipping this.',
);
return;
}
Set<Permission> permissions = {};
for (var measure in deployment?.measures ?? <Measure>[]) {
var schema = SamplingPackageRegistry().samplingSchemes[measure.type];
if (schema != null && schema.dataType is CamsDataTypeMetaData) {
permissions.addAll(
(schema.dataType as CamsDataTypeMetaData).permissions,
);
}
}
debug(
'$runtimeType - Required permissions for this deployment: $permissions',
);
if (permissions.isNotEmpty) {
// Never ask for location permissions.
// Will mess it up when requesting multiple permissions at once.
permissions
..remove(Permission.location)
..remove(Permission.locationWhenInUse)
..remove(Permission.locationAlways);
try {
info(
'$runtimeType - Asking for permissions for all measures in this deployment...',
);
_permissions = await permissions.toList().request();
debug('$runtimeType - Permissions granted: $_permissions');
_permissions?.forEach(
(permission, status) => info(
'$runtimeType - Permission status for ${permission.toString().split('.').last} : ${status.name}',
),
);
} catch (error) {
warning('$runtimeType - Error requesting permissions - error: $error');
}
}
}