askForAllPermissions method

Future<void> askForAllPermissions()

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

Should be called before sensing is started.

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;
  }

  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');
    }
  }
}