π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile][photos] Enable background ML on iOS
laurenspriem41247aabe47c
diff --git a/mobile/apps/photos/ios/Runner/AppDelegate.swift b/mobile/apps/photos/ios/Runner/AppDelegate.swiftindex 3a26fe44e7..bab6736270 100644--- a/mobile/apps/photos/ios/Runner/AppDelegate.swift+++ b/mobile/apps/photos/ios/Runner/AppDelegate.swift@@ -39,6 +39,8 @@ import workmanager_appleWorkmanagerPlugin.registerPeriodicTask(withIdentifier: "io.ente.frame.iOSBackgroundAppRefresh",frequency: NSNumber(value: freqInMinutes))+ WorkmanagerPlugin.registerBGProcessingTask(+ withIdentifier: "io.ente.frame.iOSBackgroundProcessing")if let url = AppLinks.shared.getLink(launchOptions: launchOptions) {// only accept non-homewidget urls for AppLinksdiff --git a/mobile/apps/photos/ios/Runner/Info.plist b/mobile/apps/photos/ios/Runner/Info.plistindex 499ef0d81a..b09b78f1d5 100644--- a/mobile/apps/photos/ios/Runner/Info.plist+++ b/mobile/apps/photos/ios/Runner/Info.plist@@ -5,6 +5,7 @@<key>BGTaskSchedulerPermittedIdentifiers</key><array><string>io.ente.frame.iOSBackgroundAppRefresh</string>+ <string>io.ente.frame.iOSBackgroundProcessing</string></array><key>CFBundleDevelopmentRegion</key><string>$(DEVELOPMENT_LANGUAGE)</string>diff --git a/mobile/apps/photos/lib/app.dart b/mobile/apps/photos/lib/app.dartindex bccd3fb0a2..16789f80b6 100644--- a/mobile/apps/photos/lib/app.dart+++ b/mobile/apps/photos/lib/app.dart@@ -19,6 +19,8 @@ import "package:photos/service_locator.dart";import 'package:photos/services/app_lifecycle_service.dart';import "package:photos/services/app_navigation_service.dart";import "package:photos/services/home_widget_service.dart";+import "package:photos/services/machine_learning/ml_run_control.dart";+import "package:photos/services/machine_learning/ml_service.dart";import "package:photos/services/memory_home_widget_service.dart";import "package:photos/services/people_home_widget_service.dart";import 'package:photos/services/sync/sync_service.dart';@@ -262,8 +264,16 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {}unawaited(_reloadCachesUpdatedInBackground(lastAppOpenTime));SyncService.instance.sync();+ if (Platform.isIOS) {+ MLService.instance.triggerML();+ }} else {AppLifecycleService.instance.onAppInBackground(stateChangeReason);+ if (Platform.isIOS && state == AppLifecycleState.paused) {+ // Stop before suspension freezes the run mid-flight, so the ML lock+ // is free for background-task engines while the app stays suspended.+ MLService.instance.stopActiveRun(MlStopReason.appPaused);+ }}}diff --git a/mobile/apps/photos/lib/main.dart b/mobile/apps/photos/lib/main.dartindex 5d019047f8..b6e8e76347 100644--- a/mobile/apps/photos/lib/main.dart+++ b/mobile/apps/photos/lib/main.dart@@ -76,11 +76,13 @@ const kHeartBeatFrequency = Duration(seconds: 1);const kFGSyncFrequency = Duration(minutes: 5);const kFGHomeWidgetSyncFrequency = Duration(minutes: 15);const kBGTaskTimeout = Duration(seconds: 28);+const kBGProcessingTaskTimeout = Duration(minutes: 4);const kBGPushTimeout = Duration(seconds: 28);-// ML self-stops before the platform hard-kills the BG engine (kBGTaskTimeout-// on iOS, the ~10-minute WorkManager system stop on Android), leaving margin-// to drain in-flight work and release the process lock cleanly.+// ML self-stops before the platform hard-kills the BG engine (the task+// timeouts above on iOS, the ~10-minute WorkManager system stop on Android),+// leaving margin to drain in-flight work and release the process lock cleanly.const kBGTaskMLSelfStopIOS = Duration(seconds: 26);+const kBGProcessingTaskMLSelfStopIOS = Duration(minutes: 3, seconds: 45);const kBGTaskMLSelfStopAndroid = Duration(minutes: 9);bool isProcessBg = true;bool _stopHearBeat = false;@@ -245,12 +247,14 @@ Future<void> runBackgroundTask(String taskId,TimeLogger tlog, {String mode = 'normal',+ Duration? mlSelfStop,}) async {// Created at task start so a stop that fires before ML begins stays// latched for the whole task.final mlRunControl = MlRunControl();final mlSelfStopTimer = Timer(- Platform.isIOS ? kBGTaskMLSelfStopIOS : kBGTaskMLSelfStopAndroid,+ mlSelfStop ??+ (Platform.isIOS ? kBGTaskMLSelfStopIOS : kBGTaskMLSelfStopAndroid),() => mlRunControl.requestStop(MlStopReason.backgroundDeadline),);@@ -669,10 +673,18 @@ Future<void> _scheduleHeartBeat(SharedPreferences prefs,bool isBackground,) async {- await prefs.setInt(- isBackground ? kLastBGTaskHeartBeatTime : kLastFGTaskHeartBeatTime,- DateTime.now().microsecondsSinceEpoch,- );+ // iOS launches the full app (running main) for background task wake-ups, so+ // the main engine being alive doesn't imply the app is actually in use.+ final bool skipFGWrite =+ !isBackground &&+ Platform.isIOS &&+ WidgetsBinding.instance.lifecycleState != AppLifecycleState.resumed;+ if (!skipFGWrite) {+ await prefs.setInt(+ isBackground ? kLastBGTaskHeartBeatTime : kLastFGTaskHeartBeatTime,+ DateTime.now().microsecondsSinceEpoch,+ );+ }Future.delayed(kHeartBeatFrequency, () async {// ignore: unawaited_futures_scheduleHeartBeat(prefs, isBackground);diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_run_control.dart b/mobile/apps/photos/lib/services/machine_learning/ml_run_control.dartindex 6ae526ee8e..d6fe322829 100644--- a/mobile/apps/photos/lib/services/machine_learning/ml_run_control.dart+++ b/mobile/apps/photos/lib/services/machine_learning/ml_run_control.dart@@ -2,6 +2,7 @@ import "package:logging/logging.dart";enum MlStopReason {foregroundActive,+ appPaused,backgroundDeadline,controller,logout,diff --git a/mobile/apps/photos/lib/utils/bg_task_utils.dart b/mobile/apps/photos/lib/utils/bg_task_utils.dartindex 8842734f70..6f6cb66fcb 100644--- a/mobile/apps/photos/lib/utils/bg_task_utils.dart+++ b/mobile/apps/photos/lib/utils/bg_task_utils.dart@@ -23,8 +23,19 @@ void callbackDispatcher() {await runWithLogs(() async {try {BgTaskUtils.$.info('Task started $tlog');- await runBackgroundTask(taskName, tlog).timeout(- Platform.isIOS ? kBGTaskTimeout : const Duration(hours: 1),+ if (Platform.isIOS &&+ taskName == BgTaskUtils.iOSBackgroundProcessingTask) {+ // Re-arm at task start, mirroring the plugin's native resubmit for+ // periodic tasks: a window that closes early never runs end-of-task+ // code, so rescheduling there would break the chain.+ await BgTaskUtils.scheduleIOSBackgroundProcessingTask();+ }+ await runBackgroundTask(+ taskName,+ tlog,+ mlSelfStop: BgTaskUtils.mlSelfStopFor(taskName),+ ).timeout(+ BgTaskUtils.taskTimeoutFor(taskName),onTimeout: () async {BgTaskUtils.$.warning("TLE, committing seppuku for taskID: $taskName",@@ -55,6 +66,26 @@ void callbackDispatcher() {class BgTaskUtils {static final $ = Logger("BgTaskUtils");+ static const iOSBackgroundAppRefreshTask =+ "io.ente.frame.iOSBackgroundAppRefresh";+ static const iOSBackgroundProcessingTask =+ "io.ente.frame.iOSBackgroundProcessing";+ static const androidPeriodicTask = "io.ente.photos.androidPeriodicTask";++ static Duration taskTimeoutFor(String taskName) {+ if (!Platform.isIOS) return const Duration(hours: 1);+ return taskName == iOSBackgroundProcessingTask+ ? kBGProcessingTaskTimeout+ : kBGTaskTimeout;+ }++ static Duration mlSelfStopFor(String taskName) {+ if (!Platform.isIOS) return kBGTaskMLSelfStopAndroid;+ return taskName == iOSBackgroundProcessingTask+ ? kBGProcessingTaskMLSelfStopIOS+ : kBGTaskMLSelfStopIOS;+ }+static Future<void> releaseResourcesForKill(String taskId,SharedPreferences prefs,@@ -67,23 +98,25 @@ class BgTaskUtils {}static Future configureWorkmanager() async {- if (Platform.isIOS) {- final status = await Permission.backgroundRefresh.status;- if (status != PermissionStatus.granted) {- $.warning(- "Background refresh permission is not granted. Please grant it to start the background service.",- );- return;- }- }- $.warning("Configuring Work Manager for background tasks");- const iOSBackgroundAppRefresh = "io.ente.frame.iOSBackgroundAppRefresh";- const androidPeriodicTask = "io.ente.photos.androidPeriodicTask";- final backgroundTaskIdentifier = Platform.isIOS- ? iOSBackgroundAppRefresh- : androidPeriodicTask;try {await workmanager.Workmanager().initialize(callbackDispatcher);+ if (Platform.isIOS) {+ // The processing task is scheduled regardless of the background+ // refresh setting: that setting reliably gates app refresh tasks,+ // while processing tasks may still be granted overnight windows.+ await scheduleIOSBackgroundProcessingTask();+ final status = await Permission.backgroundRefresh.status;+ if (status != PermissionStatus.granted) {+ $.warning(+ "Background refresh permission is not granted. Please grant it to start the background app refresh task.",+ );+ return;+ }+ }+ $.warning("Configuring Work Manager for background tasks");+ final backgroundTaskIdentifier = Platform.isIOS+ ? iOSBackgroundAppRefreshTask+ : androidPeriodicTask;await workmanager.Workmanager().registerPeriodicTask(backgroundTaskIdentifier,backgroundTaskIdentifier,@@ -116,4 +149,24 @@ class BgTaskUtils {$.warning("Failed to configure WorkManager: $e");}}++ // BGProcessingTask requests are one-shot: the plugin resubmits app refresh+ // tasks natively but not processing tasks, so each run schedules the next+ // one and every foreground launch re-arms the chain after a force-quit.+ static Future<void> scheduleIOSBackgroundProcessingTask() async {+ try {+ await workmanager.Workmanager().registerProcessingTask(+ iOSBackgroundProcessingTask,+ iOSBackgroundProcessingTask,+ initialDelay: kDebugMode ? Duration.zero : const Duration(minutes: 30),+ constraints: workmanager.Constraints(+ networkType: workmanager.NetworkType.connected,+ requiresCharging: true,+ ),+ );+ $.info("Scheduled iOS background processing task");+ } catch (e) {+ $.warning("Failed to schedule iOS background processing task: $e");+ }+ }}