askForAllPermissions method

Future<void> askForAllPermissions()

Asking for permissions for all the measures included in this study deployment.

Should be called after deployment has taken place (using the tryDeployment method) but before this controller is started (via the start method).

This method is only relevant on Android, and does nothing on iOS. iOS automatically asks for permissions when a resource is accessed.

Implementation

Future<void> askForAllPermissions() async {
  if (Platform.isIOS) {
    warning(
        '$runtimeType - Requesting all permissions at once is not feasible on iOS. Skipping this.');
    return;
  }

  if (deployment == null) {
    warning(
        '$runtimeType - No deployment available. Skipping requesting permissions.');
    return;
  }

  Set<Permission> permissions = {};

  for (var measure in deployment!.measures) {
    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 - status:');
      _permissions = await permissions.toList().request();

      _permissions?.forEach((permission, status) => info(
          ' - ${permission.toString().split('.').last} : ${status.name}'));
    } catch (error) {
      warning('$runtimeType - Error requesting permissions - error: $error');
    }
  }
}