configure method

Future<void> configure(
  1. LocationService configuration
)

Configures the LocationManager, incl. sending a notification to the Android notification system.

Configuration is done based on the configuration. If not provided, as set of default configurations are used.

Implementation

Future<void> configure(LocationService configuration) async {
  // fast out if already configured
  if (configured) return;
  _configuration = configuration;

  // ensured that this location manager is enable first
  await enable();

  info('Configuring $runtimeType - configuration: $configuration');
  _configured = false;

  // Only on Android, configure the notification shown when running in background.
  if (Platform.isAndroid) {
    // Need to check if location permission has been granted before trying to
    // change settings using the "changeSettings()" methods.
    // The location plugin will throw a native Android exception trying to change
    // setting without permissions to access location. And this exception is not
    // propagated to Flutter and is hence not caught by the try-catch block below.
    //
    // See https://github.com/Lyokone/flutterlocation/blob/c14f8173caf33f8c38d01b28c94e0804c63e0db9/packages/location/android/src/main/java/com/lyokone/location/FlutterLocation.java#L201
    var permission = await Permission.location.status;
    if (permission != PermissionStatus.granted) {
      warning(
        "$runtimeType - Permission to collect location data has not been granted. "
        "Cannot configure $runtimeType. "
        "Make sure to grant this BEFORE sensing is resumed. "
        "The context sampling package does not handle location permissions. This should be handled on the application level.",
      );

      // If not granted, try to request 'when in use' permission.
      await SmartPhoneClientManager().requestPermissions([
        Permission.locationWhenInUse,
      ]);
    }

    // Change notification options - only on Android.
    try {
      await _provider.changeNotificationOptions(
        title: configuration.notificationTitle ?? 'CARP Location Service',
        subtitle: configuration.notificationMessage ?? 'The location service is running in the background',
        description:
            configuration.notificationDescription ??
            'Background location is on to keep the CARP Mobile Sensing app up-to-date with your location. '
                'This is required for main features to work properly when the app is not in use.',
        onTapBringToFront: configuration.notificationOnTapBringToFront,
        iconName: configuration.notificationIconName,
      );
    } catch (error) {
      warning(
        '$runtimeType - Configuration of Android notification failed - $error\n'
        'Ignoring this.',
      );
    }
  }

  // Change location settings - both Android and iOS.
  try {
    await _provider.changeSettings(
      accuracy: location.LocationAccuracy.values[configuration.accuracy.index],
      distanceFilter: configuration.distance,
      interval: configuration.interval.inMilliseconds,
    );

    info('$runtimeType - configured successfully.');
    _configured = true;
  } catch (error) {
    warning('$runtimeType - Configuration failed - $error');
  }
}