π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
wakelock setup
AmanRajSinghMouryaa292ccd94523
diff --git a/mobile/apps/photos/lib/services/sync/large_backup_session_tracker.dart b/mobile/apps/photos/lib/services/sync/large_backup_session_tracker.dartnew file mode 100644index 0000000000..867fe4d01e--- /dev/null+++ b/mobile/apps/photos/lib/services/sync/large_backup_session_tracker.dart@@ -0,0 +1,70 @@+import "package:logging/logging.dart";+import "package:photos/events/sync_status_update_event.dart";+import "package:photos/extensions/logger_extension.dart";++/// Tracks whether the current upload session is large enough to offer the+/// foreground standby screen.+class LargeBackupSessionTracker {+ static final _logger = Logger("LargeBackupSessionTracker");+ static const int minimumFileCount = 2;++ bool _isEligible = false;+ bool _isUploading = false;+ int _remainingCount = 0;++ bool get shouldOfferStandby =>+ _isUploading && _isEligible && _remainingCount > 0;++ int get remainingCount => _remainingCount;++ void update(SyncStatusUpdate event) {+ switch (event.status) {+ case SyncStatus.preparingForUpload:+ final total = event.total ?? 0;+ _isUploading = total > 0;+ _isEligible = _isEligible || total >= minimumFileCount;+ _remainingCount = total;+ break;+ case SyncStatus.inProgress:+ final total = event.total ?? 0;+ final completed = event.completed ?? 0;+ _isUploading = total > 0;+ _isEligible = _isEligible || total >= minimumFileCount;+ _remainingCount = (total - completed).clamp(0, total);+ break;+ case SyncStatus.paused:+ case SyncStatus.completedBackup:+ case SyncStatus.completedFirstGalleryImport:+ case SyncStatus.error:+ _reset();+ break;+ case SyncStatus.startedFirstGalleryImport:+ case SyncStatus.applyingRemoteDiff:+ break;+ }++ if (_shouldLog(event)) {+ _logger.internalInfo(+ "status=${event.status.name}, total=${event.total}, "+ "completed=${event.completed}, uploading=$_isUploading, "+ "eligible=$_isEligible, remaining=$_remainingCount, "+ "offerStandby=$shouldOfferStandby, "+ "threshold=$minimumFileCount",+ );+ }+ }++ bool _shouldLog(SyncStatusUpdate event) {+ if (event.status != SyncStatus.inProgress) {+ return true;+ }+ final completed = event.completed ?? 0;+ return completed <= 1 || completed % 100 == 0 || _remainingCount == 0;+ }++ void _reset() {+ _isEligible = false;+ _isUploading = false;+ _remainingCount = 0;+ }+}diff --git a/mobile/apps/photos/lib/services/wake_lock_service.dart b/mobile/apps/photos/lib/services/wake_lock_service.dartindex aab9a0d8df..d409645675 100644--- a/mobile/apps/photos/lib/services/wake_lock_service.dart+++ b/mobile/apps/photos/lib/services/wake_lock_service.dart@@ -7,6 +7,7 @@ enum WakeLockFor {machineLearningSettingsScreen,handlingMediaKitEdgeCase,rewindViewer,+ largeBackupStandbyScreen,}/// Use this wrapper to use wakelock. This class makes sure that the wakelockdiff --git a/mobile/apps/photos/lib/ui/home/backup_standby_screen.dart b/mobile/apps/photos/lib/ui/home/backup_standby_screen.dartnew file mode 100644index 0000000000..d23daf3621--- /dev/null+++ b/mobile/apps/photos/lib/ui/home/backup_standby_screen.dart@@ -0,0 +1,224 @@+import "dart:async";++import "package:ente_components/ente_components.dart";+import "package:ente_strings/ente_strings.dart";+import "package:flutter/material.dart";+import "package:intl/intl.dart";+import "package:logging/logging.dart";+import "package:photos/core/event_bus.dart";+import "package:photos/events/sync_status_update_event.dart";+import "package:photos/extensions/logger_extension.dart";+import "package:photos/service_locator.dart";+import "package:photos/services/wake_lock_service.dart";++class BackupStandbyScreen extends StatefulWidget {+ const BackupStandbyScreen({+ required this.initialRemainingCount,+ required this.isBackupActive,+ super.key,+ });++ final int initialRemainingCount;+ final bool Function() isBackupActive;++ @override+ State<BackupStandbyScreen> createState() => _BackupStandbyScreenState();+}++class _BackupStandbyScreenState extends State<BackupStandbyScreen>+ with WidgetsBindingObserver {+ static final _logger = Logger("BackupStandbyScreen");++ late final StreamSubscription<SyncStatusUpdate> _syncSubscription;+ late int _remainingCount;+ bool _isClosing = false;+ bool _isWakeLockEnabled = false;++ @override+ void initState() {+ super.initState();+ _remainingCount = widget.initialRemainingCount;+ WidgetsBinding.instance.addObserver(this);+ _syncSubscription = Bus.instance.on<SyncStatusUpdate>().listen(+ _handleSyncStatus,+ );+ final isBackupActive = widget.isBackupActive();+ _logger.internalInfo(+ "Opened with remaining=$_remainingCount, "+ "backupActive=$isBackupActive, "+ "persistentAutoLock="+ "${wakeLockService.shouldKeepAppAwakeAcrossSessions}",+ );+ if (isBackupActive) {+ _setWakeLock(enable: true);+ } else {+ _logger.internalInfo("Closing because backup finished before opening");+ _isClosing = true;+ WidgetsBinding.instance.addPostFrameCallback((_) => _popIfPossible());+ }+ }++ @override+ void didChangeAppLifecycleState(AppLifecycleState state) {+ final isBackupActive = widget.isBackupActive();+ _logger.internalInfo(+ "Lifecycle=${state.name}, backupActive=$isBackupActive, "+ "closing=$_isClosing, wakeLockRequested=$_isWakeLockEnabled",+ );+ switch (state) {+ case AppLifecycleState.resumed:+ if (!_isClosing && isBackupActive) {+ _setWakeLock(enable: true);+ } else if (!_isClosing) {+ _dismiss(reason: "backupInactiveOnResume");+ }+ break;+ case AppLifecycleState.inactive:+ case AppLifecycleState.paused:+ case AppLifecycleState.hidden:+ case AppLifecycleState.detached:+ _setWakeLock(enable: false);+ break;+ }+ }++ @override+ void dispose() {+ _logger.internalInfo(+ "Disposing with remaining=$_remainingCount, "+ "wakeLockRequested=$_isWakeLockEnabled",+ );+ _setWakeLock(enable: false);+ _syncSubscription.cancel();+ WidgetsBinding.instance.removeObserver(this);+ super.dispose();+ }++ @override+ Widget build(BuildContext context) {+ final formattedCount = NumberFormat().format(_remainingCount);+ final memoryLabel = _remainingCount == 1 ? "memory" : "memories";++ return GestureDetector(+ key: const ValueKey("large-backup-standby-screen"),+ behavior: HitTestBehavior.opaque,+ onTap: () => _dismiss(reason: "userTap"),+ child: Scaffold(+ backgroundColor: Colors.black,+ body: Center(+ child: Column(+ mainAxisSize: MainAxisSize.min,+ children: [+ Text(+ pendingTranslation("Preserving $formattedCount $memoryLabel"),+ style: TextStyles.large.copyWith(color: Colors.white),+ ),+ const SizedBox(height: 17),+ Text(+ pendingTranslation("Tap to wake the screen"),+ style: TextStyles.body.copyWith(color: const Color(0xFFD6D6D6)),+ ),+ ],+ ),+ ),+ ),+ );+ }++ void _handleSyncStatus(SyncStatusUpdate event) {+ if (!mounted || _isClosing) {+ return;+ }++ switch (event.status) {+ case SyncStatus.preparingForUpload:+ _updateRemainingCount(event.total ?? _remainingCount);+ _logSyncStatus(event);+ break;+ case SyncStatus.inProgress:+ final total = event.total ?? _remainingCount;+ final completed = event.completed ?? 0;+ _updateRemainingCount((total - completed).clamp(0, total));+ if (_shouldLogProgress(event)) {+ _logSyncStatus(event);+ }+ break;+ case SyncStatus.paused:+ case SyncStatus.completedBackup:+ case SyncStatus.completedFirstGalleryImport:+ case SyncStatus.error:+ _logSyncStatus(event);+ _dismiss(reason: "syncStatus:${event.status.name}");+ break;+ case SyncStatus.startedFirstGalleryImport:+ case SyncStatus.applyingRemoteDiff:+ _logSyncStatus(event);+ break;+ }+ }++ void _logSyncStatus(SyncStatusUpdate event) {+ _logger.internalInfo(+ "Sync status=${event.status.name}, total=${event.total}, "+ "completed=${event.completed}, remaining=$_remainingCount",+ );+ }++ bool _shouldLogProgress(SyncStatusUpdate event) {+ if (event.status != SyncStatus.inProgress) {+ return true;+ }+ final completed = event.completed ?? 0;+ return completed <= 1 || completed % 100 == 0;+ }++ void _updateRemainingCount(int count) {+ if (_remainingCount == count) {+ return;+ }+ setState(() {+ _remainingCount = count;+ });+ }++ void _dismiss({required String reason}) {+ if (_isClosing) {+ _logger.internalInfo("Ignored duplicate dismissal: $reason");+ return;+ }+ _logger.internalInfo(+ "Dismissing: reason=$reason, remaining=$_remainingCount",+ );+ _isClosing = true;+ _setWakeLock(enable: false);+ _popIfPossible();+ }++ void _popIfPossible() {+ if (!mounted) {+ return;+ }+ final navigator = Navigator.of(context);+ if (navigator.canPop()) {+ navigator.pop();+ } else {+ _logger.internalWarning("Could not pop standby screen");+ }+ }++ void _setWakeLock({required bool enable}) {+ if (_isWakeLockEnabled == enable) {+ return;+ }+ _isWakeLockEnabled = enable;+ _logger.internalInfo(+ "Wake lock request: enable=$enable, "+ "persistentAutoLock="+ "${wakeLockService.shouldKeepAppAwakeAcrossSessions}",+ );+ wakeLockService.updateWakeLock(+ enable: enable,+ wakeLockFor: WakeLockFor.largeBackupStandbyScreen,+ );+ }+}diff --git a/mobile/apps/photos/lib/ui/home/status_bar_widget.dart b/mobile/apps/photos/lib/ui/home/status_bar_widget.dartindex 729c62fd3b..9abf0113cc 100644--- a/mobile/apps/photos/lib/ui/home/status_bar_widget.dart+++ b/mobile/apps/photos/lib/ui/home/status_bar_widget.dart@@ -1,4 +1,5 @@import 'dart:async';+import 'dart:io';import "package:ente_components/ente_components.dart";import 'package:ente_pure_utils/ente_pure_utils.dart';@@ -13,11 +14,14 @@ import 'package:photos/ente_theme_data.dart';import 'package:photos/events/christmas_banner_event.dart';import 'package:photos/events/notification_event.dart';import 'package:photos/events/sync_status_update_event.dart';+import "package:photos/extensions/logger_extension.dart";import "package:photos/service_locator.dart";+import "package:photos/services/sync/large_backup_session_tracker.dart";import 'package:photos/services/sync/sync_service.dart';import "package:photos/theme/ente_theme.dart";import 'package:photos/ui/account/verify_recovery_page.dart';import 'package:photos/ui/components/home_header_widget.dart';+import "package:photos/ui/home/backup_standby_screen.dart";import 'package:photos/ui/home/christmas/christmas_lights_banner.dart';import 'package:photos/ui/home/christmas/christmas_utils.dart';import 'package:photos/ui/home/header_error_widget.dart';@@ -47,12 +51,31 @@ class _StatusBarWidgetState extends State<StatusBarWidget> {!hasGrantedMLConsent &&(isLocalGalleryMode || flagService.hasSyncedAccountFlags()) &&!localSettings.hasSeenMLEnablingBanner;+ final LargeBackupSessionTracker _largeBackupSession =+ LargeBackupSessionTracker();Error? _syncError;@overridevoid initState() {+ _logger.internalInfo(+ "Large backup standby enabled=$_isLargeBackupStandbyEnabled, "+ "platform=${Platform.operatingSystem}, "+ "threshold=${LargeBackupSessionTracker.minimumFileCount}",+ );+ if (_isLargeBackupStandbyEnabled) {+ final lastSyncStatus = SyncService.instance.getLastSyncStatusEvent();+ if (lastSyncStatus != null) {+ _logger.internalInfo(+ "Restoring large backup state from ${lastSyncStatus.status.name}",+ );+ _largeBackupSession.update(lastSyncStatus);+ }+ }_subscription = Bus.instance.on<SyncStatusUpdate>().listen((event) {_logger.info("Received event " + event.status.toString());+ if (_isLargeBackupStandbyEnabled) {+ _largeBackupSession.update(event);+ }_isPausedDueToNetwork = event.status == SyncStatus.paused;if (event.status == SyncStatus.error) {setState(() {@@ -142,12 +165,55 @@ class _StatusBarWidgetState extends State<StatusBarWidget> {? Divider(height: 8, color: getEnteColorScheme(context).strokeFaint): const SizedBox.shrink(),if (_showErrorBanner) HeaderErrorWidget(error: _syncError),+ if (_shouldShowLargeBackupBanner) _largeBackupBanner(context),if (_showMlBanner && !_showErrorBanner) _mlBanner(context),if (_showVerificationBanner()) _recoveryKeyBanner(context),],);}+ bool get _isLargeBackupStandbyEnabled => flagService.internalUser;++ bool get _shouldShowLargeBackupBanner =>+ _isLargeBackupStandbyEnabled &&+ Platform.isIOS &&+ !_showErrorBanner &&+ _largeBackupSession.shouldOfferStandby;++ Widget _largeBackupBanner(BuildContext context) {+ return _bannerPadding(+ BannerComponent(+ key: const ValueKey("large-backup-standby-banner"),+ leadingIcon: HugeIcons.strokeRoundedMoon02,+ title: pendingTranslation("Keep the app open"),+ subtitle: pendingTranslation(+ "Screen will dim automatically to save battery",+ ),+ state: BannerComponentState.success,+ onTap: () async {+ if (!_largeBackupSession.shouldOfferStandby) {+ _logger.internalInfo(+ "Ignored standby banner tap because backup is no longer active",+ );+ return;+ }+ _logger.internalInfo(+ "Opening standby screen with "+ "remaining=${_largeBackupSession.remainingCount}",+ );+ await routeToPage(+ context,+ BackupStandbyScreen(+ initialRemainingCount: _largeBackupSession.remainingCount,+ isBackupActive: () => _largeBackupSession.shouldOfferStandby,+ ),+ forceCustomPageRoute: true,+ );+ },+ ),+ );+ }+Widget _mlBanner(BuildContext context) {final l10n = context.strings;return _bannerPadding(