configure method
- SmartphoneRegistration? registration,
- DeploymentService? deploymentService,
- DeviceDataCollectorFactory? dataCollectorFactory,
- bool enableNotifications = true,
- bool enableBackgroundMode = true,
- String? backgroundNotificationTitle,
- String? backgroundNotificationText,
- bool askForPermissions = true,
Configure this SmartPhoneClientManager.
If the deploymentService is not specified, the local
SmartphoneDeploymentService will be used.
If the dataCollectorFactory is not specified, the default DeviceController
is used.
The registration is a unique device registration for this client device.
If not specified, a registration is created from the Smartphone.createRegistration
factory method.
If enableNotifications is true (default), notifications is created when
an AppTask is triggered.
If enableBackgroundMode is true (default), data sampling will be enabled
to run in the background. This means that data sampling will continue
even when the app is not in the foreground, as long as the phone is not
restarted. If background mode is enabled, the backgroundNotificationTitle
and backgroundNotificationText can be specified to customize the notification
shown when data sampling is running in the background. If not specified,
default English titles and text will be used. If you want to use localized
titles and text, you can provide them here.
Note that background mode is only supported on Android, and will be ignored on iOS.
If askForPermissions is true (default), this client manager will
automatically ask for permissions for all sampling packages at once.
If you want the app to handle permissions itself, set this to false.
When this method is called, the client manager will restore the state of all previously added studies and resume data sampling in those studies if they were previously resumed.
Note that the client manager needs to be configured before adding any studies.
If the client manager is already configured, this method will do nothing.
Implementation
@override
Future<void> configure({
SmartphoneRegistration? registration,
DeploymentService? deploymentService,
DeviceDataCollectorFactory? dataCollectorFactory,
bool enableNotifications = true,
bool enableBackgroundMode = true,
String? backgroundNotificationTitle,
String? backgroundNotificationText,
bool askForPermissions = true,
}) async {
// Fast out if already configured
if (state.index >= ClientManagerState.configured.index) return;
_askForPermissions = askForPermissions;
// Initialize infrastructure services and the repository.
await DeviceInfoService().init();
await Settings().init();
await PersistenceService().init();
await SmartphoneClientRepository().init();
// Create and register the built-in data managers.
DataManagerRegistry().register(ConsoleDataManagerFactory());
DataManagerRegistry().register(FileDataManagerFactory());
DataManagerRegistry().register(SQLiteDataManagerFactory());
// Initialize default registration and services and configure this client manager.
registration ??= Smartphone().createRegistration();
deploymentService ??= SmartphoneDeploymentService();
dataCollectorFactory ??= DeviceController();
super.configure(
registration: registration,
deploymentService: deploymentService,
dataCollectorFactory: dataCollectorFactory,
);
// Register all primary and connected device and service managers available.
deviceController.registerAllAvailableDevices();
// Enable background mode if specified. This will make sure that data sampling
// will continue even when the app is not in the foreground, as long as the
// phone is not restarted.
if (enableBackgroundMode) {
await BackgroundService().initialize(
notificationTitle: backgroundNotificationTitle,
notificationText: backgroundNotificationText,
);
await BackgroundService().enable();
}
// Configure the notification manager.
// This will ask for permissions if needed.
await notificationManager.configure();
// Initialize the app task controller.
// This will restore previous queue from persistent storage.
await AppTaskController().initialize(
enableNotifications: enableNotifications,
);
var statusMsg =
'===========================================================\n'
' CARP Mobile Sensing (CAMS) - $runtimeType\n'
'===========================================================\n'
' Device : ${registration.deviceDisplayName}\n'
' Repository : $repository\n'
' Deployment Service : $deploymentService\n'
' Device Controller : $deviceController\n'
' Available Devices : ${deviceController.devicesToString()}\n'
' Persistence : ${PersistenceService().databaseName.split('/').last}\n'
' Background Mode : ${BackgroundService().isEnabled ? "enabled" : "disabled"}\n'
'===========================================================\n';
debugPrint(statusMsg);
// Now add previously stored studies to this client.
debug(
'$runtimeType - Loaded ${studies.length} studies. Now starting them...',
);
for (var study in studies) {
await addStudy(study);
// Mark that an updated deployment status has been received for this study,
// and if a deployment is available, mark that as well.
// This will trigger the study controller to update the deployment and start
// sampling if the deployment is valid.
study.deploymentStatusReceived();
if (study.deployment != null) {
study.deviceDeploymentReceived();
}
}
state = ClientManagerState.configured;
notifyListeners();
}