π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile] Keep native background integration at Workmanager parity
laurenspriemd79a5d5071ea
diff --git a/mobile/apps/photos/changes/native-background-processing.md b/mobile/apps/photos/changes/native-background-processing.mdindex 84220929a1..4bad560ba2 100644--- a/mobile/apps/photos/changes/native-background-processing.md+++ b/mobile/apps/photos/changes/native-background-processing.md@@ -1 +1 @@-- Internal users now use Ente's native background manager for sync and ML processing, with cooperative stopping when the app opens. Other users continue using Workmanager.+- Internal users now use Ente's native background manager to schedule the existing sync and ML tasks. Other users continue using Workmanager.diff --git a/mobile/apps/photos/lib/core/configuration.dart b/mobile/apps/photos/lib/core/configuration.dartindex bc7819e7b9..7f35117f89 100644--- a/mobile/apps/photos/lib/core/configuration.dart+++ b/mobile/apps/photos/lib/core/configuration.dart@@ -5,7 +5,6 @@ import "dart:io";import 'package:backup_exclusion/backup_exclusion.dart';import 'package:bip39/bip39.dart' as bip39;import 'package:ente_account_deletion/account_deletion.dart';-import 'package:ente_background_manager/ente_background_manager.dart';import 'package:ente_contacts/contacts.dart';import "package:ente_crypto/ente_crypto.dart";import 'package:ente_lock_screen/lock_screen_host.dart';@@ -37,7 +36,6 @@ import 'package:photos/events/user_logged_out_event.dart';import 'package:photos/gateways/users/models/key_attributes.dart';import 'package:photos/gateways/users/models/key_gen_result.dart';import 'package:photos/gateways/users/models/private_key_attributes.dart';-import 'package:photos/main.dart' show isProcessBg;import 'package:photos/module/upload/upload_artifact.dart';import 'package:photos/service_locator.dart';import 'package:photos/services/authenticated_session.dart';@@ -198,11 +196,6 @@ class Configuration implements LockScreenHost, AccountDeletionHost {Future<void> logout({bool autoLogout = false}) async {_logger.info("Logging out, autoLogout: $autoLogout");MLService.instance.stopActiveRun(MlStopReason.logout);- if (isProcessBg) {- unawaited(BackgroundManager.stopActiveRun());- } else {- await BackgroundManager.stopActiveRun();- }if (!autoLogout) {if (flagService.stopStreamProcess) {VideoPreviewService.instance.stop('logout');diff --git a/mobile/apps/photos/lib/main.dart b/mobile/apps/photos/lib/main.dartindex da8e34f734..c891675b12 100644--- a/mobile/apps/photos/lib/main.dart+++ b/mobile/apps/photos/lib/main.dart@@ -4,7 +4,6 @@ import 'dart:io';import "package:adaptive_theme/adaptive_theme.dart";import "package:computer/computer.dart";import "package:ente_account_deletion/account_deletion.dart";-import "package:ente_background_manager/ente_background_manager.dart";import "package:ente_components/ente_components.dart" as components;import 'package:ente_crypto/ente_crypto.dart';import "package:ente_crypto_api/ente_crypto_api.dart" show registerCryptoApi;@@ -47,7 +46,6 @@ import 'package:photos/services/collections_service.dart';import 'package:photos/services/favorites_service.dart';import 'package:photos/services/home_widget_service.dart';import "package:photos/services/machine_learning/face_ml/person/person_service.dart";-import "package:photos/services/machine_learning/ml_process_lock.dart";import "package:photos/services/machine_learning/ml_run_control.dart";import 'package:photos/services/machine_learning/ml_service.dart';import 'package:photos/services/machine_learning/semantic_search/semantic_search_service.dart';@@ -67,7 +65,6 @@ import "package:photos/services/video_preview_service.dart";import "package:photos/src/rust/api/log.dart" as photos_rust_log;import "package:photos/src/rust/frb_generated.dart";import 'package:photos/ui/wallpaper/wallpaper_page.dart';-import "package:photos/utils/background_tasks.dart";import "package:photos/utils/bg_task_utils.dart";import "package:photos/utils/device_info.dart";import "package:photos/utils/email_util.dart";@@ -248,33 +245,11 @@ Future<void> runBackgroundTask(String mode = 'normal',Duration? mlSelfStop,Duration? mlLockWait,- BackgroundTask? nativeTask,+ MlRunControl? control,}) async {// Created at task start so a stop that fires before ML begins stays// latched for the whole task.- final mlRunControl = MlRunControl();- var finished = false;- var switchingBackend = false;- bool shouldStop() =>- finished || switchingBackend || nativeTask?.isStopping == true;- void checkStop() {- if (shouldStop()) throw const BackgroundTaskStopped();- }-- SyncService.instance.setBackgroundStopCheck(shouldStop);- if (nativeTask != null) {- unawaited(- nativeTask.stopped.then((reason) {- if (finished) return;- mlRunControl.requestStop(- reason == BackgroundStopReason.foreground- ? MlStopReason.foregroundActive- : MlStopReason.backgroundDeadline,- );- SyncService.instance.stopSync();- }),- );- }+ final mlRunControl = control ?? MlRunControl();final mlBudget =mlSelfStop ??(Platform.isIOS ? kBGTaskMLSelfStopIOS : kBGTaskMLSelfStopAndroid);@@ -287,26 +262,18 @@ Future<void> runBackgroundTask(() => mlRunControl.requestStop(MlStopReason.backgroundDeadline),): null;- final mlForegroundWatchTimer = nativeTask != null- ? null- : Timer.periodic(const Duration(milliseconds: 500), (_) async {- if (finished) return;- final prefs = await SharedPreferences.getInstance();- if (await BackgroundTasks.nativeEnabled(prefs)) {- switchingBackend = true;- mlRunControl.requestStop(MlStopReason.manual);- SyncService.instance.stopSync();- }- if (mlRunControl.stopRequested) return;- if (await isForegroundEngineActive()) {- mlRunControl.requestStop(MlStopReason.foregroundActive);- }- });+ final mlForegroundWatchTimer = Timer.periodic(+ const Duration(milliseconds: 500),+ (_) async {+ if (mlRunControl.stopRequested) return;+ if (await isForegroundEngineActive()) {+ mlRunControl.requestStop(MlStopReason.foregroundActive);+ }+ },+ );try {- checkStop();- final isRunningInFG =- nativeTask == null && await isForegroundEngineActive();+ final isRunningInFG = await isForegroundEngineActive();if (isRunningInFG) {_logger.info("[BG TASK] Foreground recently active, skipping background work",@@ -318,18 +285,10 @@ Future<void> runBackgroundTask("[BG TASK] No recent foreground activity, proceeding with background work",);- await _runMinimally(- taskId,- tlog,- mlRunControl,- mlLockWait,- checkStop: checkStop,- propagateFailure: nativeTask != null,- );+ await _runMinimally(taskId, tlog, mlRunControl, mlLockWait);} finally {- finished = true;mlSelfStopTimer?.cancel();- mlForegroundWatchTimer?.cancel();+ mlForegroundWatchTimer.cancel();}}@@ -337,18 +296,13 @@ Future<void> _runMinimally(String taskId,TimeLogger tlog,MlRunControl mlRunControl,- Duration? mlLockWait, {- required void Function() checkStop,- required bool propagateFailure,-}) async {+ Duration? mlLockWait,+) async {try {- var successful = true;- checkStop();final PackageInfo packageInfo = await PackageInfo.fromPlatform();final SharedPreferences prefs = await SharedPreferences.getInstance();await _scheduleHeartBeat(prefs, true);await _ensureRustInitialized(via: 'workmanager:$taskId');- checkStop();_logger.info("[BG TASK] NetworkClient init $tlog");await NetworkClient.instance.init(packageInfo, prefs);@@ -368,7 +322,6 @@ Future<void> _runMinimally(_logger.info("(for debugging) Configuration init $tlog");await Configuration.instance.init(prefs);- checkStop();_logger.info("(for debugging) Configuration done $tlog");AppLifecycleService.instance.init(prefs);@@ -377,22 +330,18 @@ Future<void> _runMinimally();await Computer.shared().turnOn(workersCount: 4);- checkStop();CryptoUtil.init();// Initialize early so thermal/battery listeners can warm up while the// rest of background services are being initialized.final controller = computeController;await MemoryShareService.instance.init();- checkStop();_logger.info("(for debugging) CollectionsService init $tlog");await CollectionsService.instance.init(prefs);- checkStop();_logger.info("(for debugging) CollectionsService init done $tlog");await FileUploader.instance.init(prefs, true);- checkStop();LocalFileUpdateService.instance.init(prefs);await LocalSyncService.instance.init(prefs);RemoteSyncService.instance.init(prefs);@@ -400,25 +349,18 @@ Future<void> _runMinimally(_isSyncInitialized = true;await UserService.instance.init();- checkStop();SocialNotificationCoordinator.instance.init(prefs);await NotificationService.instance.initializeForBackground();_logger.info("[BG TASK] update notification");updateService.showUpdateNotification().ignore();_logger.info("[BG TASK] sync starting");- if (propagateFailure) {- successful = await SyncService.instance.sync();- } else {- await _sync('bgTaskActiveProcess');- }- checkStop();+ await _sync('bgTaskActiveProcess');_logger.info("[BG TASK] sync completed");_logger.info("[BG TASK] locale fetch");final locale = await getLocale();await initializeDateFormatting(locale?.languageCode ?? "en");- checkStop();_logger.info("[BG TASK] home widget sync");if (!isLocalGalleryMode &&hasGrantedMLConsent &&@@ -436,12 +378,10 @@ Future<void> _runMinimally(}}await _homeWidgetSync(true);- checkStop();if ((isLocalGalleryMode || flagService.enableMLInBackground) &&hasGrantedMLConsent) {await controller.init();- checkStop();final canRunML = controller.requestCompute(ml: true);if (!canRunML) {_logger.info(@@ -458,26 +398,17 @@ Future<void> _runMinimally(lockWait: mlLockWait,);_logger.info("[BG TASK] ML run disposition: ${disposition.name}");- if (disposition == MlRunDisposition.failed) successful = false;} finally {controller.releaseCompute(ml: true);}}}_logger.info("[BG TASK] smart albums sync");- checkStop();await smartAlbumsService.syncSmartAlbums();- checkStop();- if (propagateFailure && !successful) {- throw StateError("Background work reported a failure");- }_logger.info("[BG TASK] $taskId completed");- } on BackgroundTaskStopped {- if (propagateFailure) rethrow;} catch (e, s) {_logger.severe("[BG TASK] $taskId error", e, s);- if (propagateFailure) rethrow;}}diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart b/mobile/apps/photos/lib/services/machine_learning/ml_service.dartindex 67e1ea3ade..05feb84134 100644--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart@@ -607,11 +607,7 @@ class MLService {);}final Stream<List<FileMLInstruction>> instructionStream =- fetchEmbeddingsAndInstructions(- fileDownloadMlLimit,- mode: mode,- control: control,- );+ fetchEmbeddingsAndInstructions(fileDownloadMlLimit, mode: mode);int fileAnalyzedCount = 0;final Stopwatch stopwatch = Stopwatch()..start();diff --git a/mobile/apps/photos/lib/services/sync/local_sync_service.dart b/mobile/apps/photos/lib/services/sync/local_sync_service.dartindex cd565102ec..a7d5302302 100644--- a/mobile/apps/photos/lib/services/sync/local_sync_service.dart+++ b/mobile/apps/photos/lib/services/sync/local_sync_service.dart@@ -26,7 +26,6 @@ import "package:photos/services/sync/import/diff.dart";import "package:photos/services/sync/import/local_assets.dart";import "package:photos/services/sync/import/model.dart";import "package:photos/services/sync/origin_fetch_tracker.dart";-import "package:photos/services/sync/sync_service.dart";import "package:shared_preferences/shared_preferences.dart";import "package:synchronized/synchronized.dart";import "package:tuple/tuple.dart";@@ -77,7 +76,6 @@ class LocalSyncService {}Future<void> sync() async {- if (SyncService.instance.backgroundWorkStopped) return;if (!permissionService.hasGrantedPermissions()) {_logger.info("Skipping local sync since permission has not been granted");return;@@ -103,7 +101,7 @@ class LocalSyncService {// Local sync must not race downloads; that can create incorrect FilesDB// rows.- final localSync = _lock.synchronized(() async {+ await _lock.synchronized(() async {final existingLocalFileIDs = await _db.getExistingLocalFileIDs(ownerID);_logger.info("${existingLocalFileIDs.length} localIDs were discovered");@@ -124,7 +122,6 @@ class LocalSyncService {var toYear = 2010;var toTime = DateTime(toYear).microsecondsSinceEpoch;while (toTime < syncStartTime) {- SyncService.instance.checkBackgroundWork();await _loadAndStoreDiff(existingLocalFileIDs,fromTime: startTime,@@ -163,12 +160,9 @@ class LocalSyncService {final duration = Duration(microseconds: endTime - startTime);_logger.info("Load took " + duration.inMilliseconds.toString() + "ms");});- try {- await localSync;- } finally {- _existingSync?.complete();- _existingSync = null;- }++ _existingSync?.complete();+ _existingSync = null;}Future<bool> _refreshDeviceFolderCountAndCover({@@ -188,7 +182,6 @@ class LocalSyncService {}Future<bool> syncAll() async {- if (SyncService.instance.backgroundWorkStopped) return false;if (!Configuration.instance.isLoggedIn()) {if (!isLocalGalleryMode) {_logger.warning("syncAll called when user is not logged in");@@ -200,7 +193,6 @@ class LocalSyncService {final localAssets = await getAllLocalAssets(needsTitle: isLocalGalleryMode ? true : null,);- SyncService.instance.checkBackgroundWork();_logger.info("Loading allLocalAssets ${localAssets.length} took ${stopwatch.elapsedMilliseconds}ms ",);@@ -218,7 +210,6 @@ class LocalSyncService {existingLocalFileIDs,pathToLocalIDs,);- SyncService.instance.checkBackgroundWork();bool hasAnyMappingChanged = false;if (localDiffResult.newPathToLocalIDs?.isNotEmpty ?? false) {await _db.insertPathIDToLocalIDMapping(diff --git a/mobile/apps/photos/lib/services/sync/remote_sync_service.dart b/mobile/apps/photos/lib/services/sync/remote_sync_service.dartindex b80952aa6a..00457b3ae9 100644--- a/mobile/apps/photos/lib/services/sync/remote_sync_service.dart+++ b/mobile/apps/photos/lib/services/sync/remote_sync_service.dart@@ -99,7 +99,6 @@ class RemoteSyncService {}Future<void> sync({bool silently = false}) async {- if (SyncService.instance.backgroundWorkStopped) return;if (!_config.hasConfiguredAccount()) {_logger.info("Skipping remote sync since account is not configured");return;@@ -130,9 +129,7 @@ class RemoteSyncService {await syncDeviceCollectionFilesForUpload();}await _pullDiff();- SyncService.instance.checkBackgroundWork();await trashSyncService.syncTrash();- SyncService.instance.checkBackgroundWork();await _collectionsService.movePendingRemovalActionsToUncategorized();if (AppLifecycleService.instance.isForeground) {@@ -157,9 +154,7 @@ class RemoteSyncService {}final filesToBeUploaded = await _getFilesToBeUploaded();- SyncService.instance.checkBackgroundWork();final hasUploadedFiles = await _uploadFiles(filesToBeUploaded);- SyncService.instance.checkBackgroundWork();if (filesToBeUploaded.isNotEmpty) {_logger.info("Files ${filesToBeUploaded.length} queued for upload, completed: "@@ -195,10 +190,6 @@ class RemoteSyncService {_existingSync?.complete();_existingSync = null;}- } on SyncStopRequestedError {- _existingSync?.complete();- _existingSync = null;- rethrow;} catch (e, s) {_existingSync?.complete();_existingSync = null;@@ -265,7 +256,6 @@ class RemoteSyncService {final Map<int, int> idsToRemoteUpdationTimeMap,) async {for (final cid in idsToRemoteUpdationTimeMap.keys) {- SyncService.instance.checkBackgroundWork();await _syncCollectionDiff(cid,_collectionsService.getCollectionSyncTime(cid),@@ -283,7 +273,6 @@ class RemoteSyncService {_logger.info('re-setting all collections syncTime to: $resetSyncTime');final collections = _collectionsService.getActiveCollections();for (final c in collections) {- SyncService.instance.checkBackgroundWork();final int newSyncTime = min(_collectionsService.getCollectionSyncTime(c.id),resetSyncTime,@@ -293,7 +282,6 @@ class RemoteSyncService {}Future<void> _syncCollectionDiff(int collectionID, int sinceTime) async {- SyncService.instance.checkBackgroundWork();_logger.info("[Collection-$collectionID] fetch diff silently: $_isExistingSyncSilent ""since: $sinceTime",@@ -404,7 +392,6 @@ class RemoteSyncService {bool moreFilesMarkedForBackup = false;for (final deviceCollection in deviceCollections) {- SyncService.instance.checkBackgroundWork();final Set<String> localIDsToSync =pathIdToLocalIDs[deviceCollection.id]?.toSet() ?? {};if (deviceCollection.uploadStrategy == UploadStrategy.ifMissing) {@@ -539,7 +526,6 @@ class RemoteSyncService {Future<void> removeFilesQueuedForUpload(List<int> collectionIDs) async {_logger.info("Removing files for collections $collectionIDs");for (int collectionID in collectionIDs) {- SyncService.instance.checkBackgroundWork();final List<EnteFile> pendingUploads = await _db.getPendingUploadForCollection(collectionID);if (pendingUploads.isEmpty) {@@ -680,7 +666,6 @@ class RemoteSyncService {}for (final file in uploadQueue) {- if (SyncService.instance.shouldStopSync()) break;if (shouldThrottleUpload &&futures.length >= kMaximumPermissibleUploadsInThrottledMode) {break;@@ -697,7 +682,6 @@ class RemoteSyncService {}for (final uploadedFileID in updatedFileIDs) {- if (SyncService.instance.shouldStopSync()) break;if (shouldThrottleUpload &&futures.length >= kMaximumPermissibleUploadsInThrottledMode) {break;diff --git a/mobile/apps/photos/lib/services/sync/sync_service.dart b/mobile/apps/photos/lib/services/sync/sync_service.dartindex 6580ecb0bd..f3751c6c18 100644--- a/mobile/apps/photos/lib/services/sync/sync_service.dart+++ b/mobile/apps/photos/lib/services/sync/sync_service.dart@@ -33,7 +33,6 @@ class SyncService {final _uploader = FileUploader.instance;bool _syncStopRequested = false;Completer<bool>? _existingSync;- bool Function()? _backgroundStopCheck;late SharedPreferences _prefs;SyncStatusUpdate? _lastSyncStatusEvent;bool _isInitialized = false;@@ -113,7 +112,6 @@ class SyncService {}Future<bool> sync() async {- if (_backgroundStopCheck?.call() == true) return false;if (!_isInitialized) {_logger.warning("Sync requested before init, skipping");return false;@@ -198,19 +196,9 @@ class SyncService {}bool shouldStopSync() {- return _syncStopRequested || _backgroundStopCheck?.call() == true;+ return _syncStopRequested;}- void setBackgroundStopCheck(bool Function() check) {- _backgroundStopCheck = check;- }-- void checkBackgroundWork() {- if (backgroundWorkStopped) throw SyncStopRequestedError();- }-- bool get backgroundWorkStopped => _backgroundStopCheck?.call() == true;-bool isSyncInProgress() {return _existingSync != null;}@@ -242,13 +230,10 @@ class SyncService {}Future<void> _doSync() async {- checkBackgroundWork();_logger.info("[SYNC] Starting local sync");await _localSyncService.sync();- checkBackgroundWork();if (isLocalGalleryMode) {await _localSyncService.syncAll();- checkBackgroundWork();if (Platform.isAndroid) {final processing = OfflineImportMetadataService.instance.processPendingFiles(@@ -275,10 +260,8 @@ class SyncService {if (allowRemoteSync) {_logger.info("[SYNC] Starting remote sync");await _remoteSyncService.sync();- checkBackgroundWork();final shouldSync = await _localSyncService.syncAll();- checkBackgroundWork();if (shouldSync) {_logger.info("[SYNC] Starting second remote sync");await _remoteSyncService.sync();diff --git a/mobile/apps/photos/lib/utils/background_tasks.dart b/mobile/apps/photos/lib/utils/background_tasks.dartindex 6d4c00f051..ec547e54f1 100644--- a/mobile/apps/photos/lib/utils/background_tasks.dart+++ b/mobile/apps/photos/lib/utils/background_tasks.dart@@ -13,6 +13,7 @@ import "package:permission_handler/permission_handler.dart";import "package:photos/db/upload_locks_db.dart";import "package:photos/main.dart";import "package:photos/module/upload/service/file_uploader.dart";+import "package:photos/services/machine_learning/ml_run_control.dart";import "package:photos/utils/bg_task_utils.dart";import "package:shared_preferences/shared_preferences.dart";import "package:workmanager/workmanager.dart" as legacy;@@ -202,13 +203,31 @@ class BackgroundTasks {: (Platform.isIOS? BgTaskUtils.iOSBackgroundAppRefreshTask: BgTaskUtils.androidPeriodicTask);+ final control = MlRunControl();+ unawaited(+ task.stopped.then((reason) {+ control.requestStop(+ reason == BackgroundStopReason.foreground+ ? MlStopReason.foregroundActive+ : MlStopReason.backgroundDeadline,+ );+ }),+ );try {+ final remainingBudget =+ BgTaskUtils.taskTimeoutFor(taskName) - task.elapsed;await runBackgroundTask(taskName,TimeLogger(),- nativeTask: task,+ control: control,mlSelfStop: BgTaskUtils.mlSelfStopFor(taskName) - task.elapsed,mlLockWait: BgTaskUtils.mlLockWaitFor(taskName),+ ).timeout(+ remainingBudget.isNegative ? Duration.zero : remainingBudget,+ onTimeout: () async {+ await BgTaskUtils.releaseResourcesForKill(taskName, prefs);+ throw TimeoutException("Background task timed out");+ },);result = task.isStopping? BackgroundTaskResult.stoppeddiff --git a/mobile/apps/photos/lib/utils/ml_util.dart b/mobile/apps/photos/lib/utils/ml_util.dartindex 74af09916c..022325edc7 100644--- a/mobile/apps/photos/lib/utils/ml_util.dart+++ b/mobile/apps/photos/lib/utils/ml_util.dart@@ -407,15 +407,12 @@ Future<List<FileMLInstruction>> getLocalGalleryFilesForMlIndexing() async {Stream<List<FileMLInstruction>> fetchEmbeddingsAndInstructions(int yieldSize, {required MLMode mode,- MlRunControl? control,}) async* {- if (control?.stopRequested == true) return;if (mode == MLMode.localGallery) {final List<FileMLInstruction> filesToIndex =await getLocalGalleryFilesForMlIndexing();final List<List<FileMLInstruction>> chunks = filesToIndex.chunks(yieldSize);for (final batch in chunks) {- if (control?.stopRequested == true) return;yield batch;}return;@@ -428,12 +425,10 @@ Stream<List<FileMLInstruction>> fetchEmbeddingsAndInstructions(List<FileMLInstruction> batchToYield = [];for (final chunk in chunks) {- if (control?.stopRequested == true) return;if (!localSettings.remoteFetchEnabled) {_logger.warning("remoteFetchEnabled is false, skiping embedding fetch");final batches = chunk.chunks(yieldSize);for (final batch in batches) {- if (control?.stopRequested == true) return;yield batch;}continue;@@ -442,7 +437,6 @@ Stream<List<FileMLInstruction>> fetchEmbeddingsAndInstructions(chunk,mlDataDB: mlDataDB,);- if (control?.stopRequested == true) return;for (final instruction in pendingInstructions) {if (instruction.pendingML) {batchToYield.add(instruction);