From 7a3d195a360b10a23016ecd73980619fc09f147d Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Sat, 9 Apr 2022 11:23:37 +0800 Subject: [PATCH] Pause sservice when battery is low --- app/lib/event/event.dart | 3 ++ app/lib/l10n/app_en.arb | 4 ++ app/lib/l10n/untranslated-messages.txt | 14 ++++-- app/lib/service.dart | 7 +++ app/lib/use_case/update_missing_metadata.dart | 13 +++++ app/lib/widget/home_photos.dart | 15 ++++++ app/pubspec.lock | 50 +++++++++++++++++-- app/pubspec.yaml | 1 + 8 files changed, 100 insertions(+), 7 deletions(-) diff --git a/app/lib/event/event.dart b/app/lib/event/event.dart index 7cceac91..f8f6e8ba 100644 --- a/app/lib/event/event.dart +++ b/app/lib/event/event.dart @@ -130,6 +130,9 @@ enum MetadataTaskState { /// Paused on data network waitingForWifi, + + /// Paused on low battery + lowBattery, } class MetadataTaskStateChangedEvent { diff --git a/app/lib/l10n/app_en.arb b/app/lib/l10n/app_en.arb index 62396e05..85489c66 100644 --- a/app/lib/l10n/app_en.arb +++ b/app/lib/l10n/app_en.arb @@ -1171,6 +1171,10 @@ "@backgroundServiceStopping": { "description": "The background service is stopping itself" }, + "metadataTaskPauseLowBatteryNotification": "Battery is low", + "@metadataTaskPauseLowBatteryNotification": { + "description": "Shown when the app has paused reading image metadata due to low battery" + }, "errorUnauthenticated": "Unauthenticated access. Please sign-in again if the problem continues", "@errorUnauthenticated": { diff --git a/app/lib/l10n/untranslated-messages.txt b/app/lib/l10n/untranslated-messages.txt index 4a65f9a1..035a6fff 100644 --- a/app/lib/l10n/untranslated-messages.txt +++ b/app/lib/l10n/untranslated-messages.txt @@ -83,6 +83,7 @@ "addTagInputHint", "tagPickerNoTagSelectedNotification", "backgroundServiceStopping", + "metadataTaskPauseLowBatteryNotification", "errorAlbumDowngrade" ], @@ -184,6 +185,7 @@ "addTagInputHint", "tagPickerNoTagSelectedNotification", "backgroundServiceStopping", + "metadataTaskPauseLowBatteryNotification", "errorAlbumDowngrade" ], @@ -340,17 +342,20 @@ "addTagInputHint", "tagPickerNoTagSelectedNotification", "backgroundServiceStopping", + "metadataTaskPauseLowBatteryNotification", "errorAlbumDowngrade" ], "es": [ "rootPickerSkipConfirmationDialogContent2", "helpButtonLabel", - "backgroundServiceStopping" + "backgroundServiceStopping", + "metadataTaskPauseLowBatteryNotification" ], "fi": [ - "backgroundServiceStopping" + "backgroundServiceStopping", + "metadataTaskPauseLowBatteryNotification" ], "fr": [ @@ -486,6 +491,7 @@ "addTagInputHint", "tagPickerNoTagSelectedNotification", "backgroundServiceStopping", + "metadataTaskPauseLowBatteryNotification", "errorAlbumDowngrade" ], @@ -509,7 +515,8 @@ "createCollectionDialogTagDescription", "addTagInputHint", "tagPickerNoTagSelectedNotification", - "backgroundServiceStopping" + "backgroundServiceStopping", + "metadataTaskPauseLowBatteryNotification" ], "ru": [ @@ -618,6 +625,7 @@ "addTagInputHint", "tagPickerNoTagSelectedNotification", "backgroundServiceStopping", + "metadataTaskPauseLowBatteryNotification", "errorAlbumDowngrade" ] } diff --git a/app/lib/service.dart b/app/lib/service.dart index e69b5af0..37bf42b8 100644 --- a/app/lib/service.dart +++ b/app/lib/service.dart @@ -125,6 +125,13 @@ class _Service { ) ..pauseWakeLock(); _isPaused = true; + } else if (ev.state == MetadataTaskState.lowBattery) { + FlutterBackgroundService() + ..setNotificationInfo( + title: _L10n.global().metadataTaskPauseLowBatteryNotification, + ) + ..pauseWakeLock(); + _isPaused = true; } } else { if (ev.state == MetadataTaskState.prcoessing) { diff --git a/app/lib/use_case/update_missing_metadata.dart b/app/lib/use_case/update_missing_metadata.dart index 18eb2620..c0255023 100644 --- a/app/lib/use_case/update_missing_metadata.dart +++ b/app/lib/use_case/update_missing_metadata.dart @@ -1,3 +1,4 @@ +import 'package:battery_plus/battery_plus.dart'; import 'package:event_bus/event_bus.dart'; import 'package:kiwi/kiwi.dart'; import 'package:logging/logging.dart'; @@ -53,6 +54,7 @@ class UpdateMissingMetadata { // since we need to download multiple images in their original size, // we only do it with WiFi await ensureWifi(); + await ensureBattery(); KiwiContainer().resolve().fire( const MetadataTaskStateChangedEvent(MetadataTaskState.prcoessing)); if (!shouldRun) { @@ -116,6 +118,17 @@ class UpdateMissingMetadata { } } + Future ensureBattery() async { + while (await Battery().batteryLevel <= 15) { + if (!shouldRun) { + throw const InterruptedException(); + } + KiwiContainer().resolve().fire( + const MetadataTaskStateChangedEvent(MetadataTaskState.lowBattery)); + await Future.delayed(const Duration(seconds: 5)); + } + } + final FileRepo fileRepo; bool shouldRun = true; diff --git a/app/lib/widget/home_photos.dart b/app/lib/widget/home_photos.dart index 78f0270f..00b43ae1 100644 --- a/app/lib/widget/home_photos.dart +++ b/app/lib/widget/home_photos.dart @@ -692,6 +692,21 @@ class _Web { style: const TextStyle(fontSize: 12), ), ], + ) + else if (_metadataTaskState == MetadataTaskState.lowBattery) + Row( + mainAxisSize: MainAxisSize.min, + children: [ + const Icon( + Icons.sync_problem, + size: 16, + ), + const SizedBox(width: 4), + Text( + L10n.global().metadataTaskPauseLowBatteryNotification, + style: const TextStyle(fontSize: 12), + ), + ], ), Expanded( child: Container(), diff --git a/app/pubspec.lock b/app/pubspec.lock index 20aaccc5..e81f8701 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -36,6 +36,48 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.8.2" + battery_plus: + dependency: "direct main" + description: + name: battery_plus + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.3" + battery_plus_linux: + dependency: transitive + description: + name: battery_plus_linux + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.1" + battery_plus_macos: + dependency: transitive + description: + name: battery_plus_macos + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + battery_plus_platform_interface: + dependency: transitive + description: + name: battery_plus_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.1" + battery_plus_web: + dependency: transitive + description: + name: battery_plus_web + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + battery_plus_windows: + dependency: transitive + description: + name: battery_plus_windows + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" bloc: dependency: "direct main" description: @@ -126,7 +168,7 @@ packages: name: connectivity_plus_linux url: "https://pub.dartlang.org" source: hosted - version: "1.1.1" + version: "1.3.0" connectivity_plus_macos: dependency: transitive description: @@ -140,7 +182,7 @@ packages: name: connectivity_plus_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "1.1.1" + version: "1.2.0" connectivity_plus_web: dependency: transitive description: @@ -189,7 +231,7 @@ packages: name: dbus url: "https://pub.dartlang.org" source: hosted - version: "0.6.8" + version: "0.7.3" device_info_plus: dependency: "direct main" description: @@ -625,7 +667,7 @@ packages: name: nm url: "https://pub.dartlang.org" source: hosted - version: "0.4.4" + version: "0.5.0" node_preamble: dependency: transitive description: diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 00a2f0cf..54e06582 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -29,6 +29,7 @@ dependencies: # android only android_intent_plus: ^3.0.1 + battery_plus: ^2.1.3 bloc: ^7.0.0 cached_network_image: ^3.0.0 collection: ^1.15.0