Pause sservice when battery is low

This commit is contained in:
Ming Ming 2022-04-09 11:23:37 +08:00
parent 853fd91ea0
commit 7a3d195a36
8 changed files with 100 additions and 7 deletions

View file

@ -130,6 +130,9 @@ enum MetadataTaskState {
/// Paused on data network /// Paused on data network
waitingForWifi, waitingForWifi,
/// Paused on low battery
lowBattery,
} }
class MetadataTaskStateChangedEvent { class MetadataTaskStateChangedEvent {

View file

@ -1171,6 +1171,10 @@
"@backgroundServiceStopping": { "@backgroundServiceStopping": {
"description": "The background service is stopping itself" "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": "Unauthenticated access. Please sign-in again if the problem continues",
"@errorUnauthenticated": { "@errorUnauthenticated": {

View file

@ -83,6 +83,7 @@
"addTagInputHint", "addTagInputHint",
"tagPickerNoTagSelectedNotification", "tagPickerNoTagSelectedNotification",
"backgroundServiceStopping", "backgroundServiceStopping",
"metadataTaskPauseLowBatteryNotification",
"errorAlbumDowngrade" "errorAlbumDowngrade"
], ],
@ -184,6 +185,7 @@
"addTagInputHint", "addTagInputHint",
"tagPickerNoTagSelectedNotification", "tagPickerNoTagSelectedNotification",
"backgroundServiceStopping", "backgroundServiceStopping",
"metadataTaskPauseLowBatteryNotification",
"errorAlbumDowngrade" "errorAlbumDowngrade"
], ],
@ -340,17 +342,20 @@
"addTagInputHint", "addTagInputHint",
"tagPickerNoTagSelectedNotification", "tagPickerNoTagSelectedNotification",
"backgroundServiceStopping", "backgroundServiceStopping",
"metadataTaskPauseLowBatteryNotification",
"errorAlbumDowngrade" "errorAlbumDowngrade"
], ],
"es": [ "es": [
"rootPickerSkipConfirmationDialogContent2", "rootPickerSkipConfirmationDialogContent2",
"helpButtonLabel", "helpButtonLabel",
"backgroundServiceStopping" "backgroundServiceStopping",
"metadataTaskPauseLowBatteryNotification"
], ],
"fi": [ "fi": [
"backgroundServiceStopping" "backgroundServiceStopping",
"metadataTaskPauseLowBatteryNotification"
], ],
"fr": [ "fr": [
@ -486,6 +491,7 @@
"addTagInputHint", "addTagInputHint",
"tagPickerNoTagSelectedNotification", "tagPickerNoTagSelectedNotification",
"backgroundServiceStopping", "backgroundServiceStopping",
"metadataTaskPauseLowBatteryNotification",
"errorAlbumDowngrade" "errorAlbumDowngrade"
], ],
@ -509,7 +515,8 @@
"createCollectionDialogTagDescription", "createCollectionDialogTagDescription",
"addTagInputHint", "addTagInputHint",
"tagPickerNoTagSelectedNotification", "tagPickerNoTagSelectedNotification",
"backgroundServiceStopping" "backgroundServiceStopping",
"metadataTaskPauseLowBatteryNotification"
], ],
"ru": [ "ru": [
@ -618,6 +625,7 @@
"addTagInputHint", "addTagInputHint",
"tagPickerNoTagSelectedNotification", "tagPickerNoTagSelectedNotification",
"backgroundServiceStopping", "backgroundServiceStopping",
"metadataTaskPauseLowBatteryNotification",
"errorAlbumDowngrade" "errorAlbumDowngrade"
] ]
} }

View file

@ -125,6 +125,13 @@ class _Service {
) )
..pauseWakeLock(); ..pauseWakeLock();
_isPaused = true; _isPaused = true;
} else if (ev.state == MetadataTaskState.lowBattery) {
FlutterBackgroundService()
..setNotificationInfo(
title: _L10n.global().metadataTaskPauseLowBatteryNotification,
)
..pauseWakeLock();
_isPaused = true;
} }
} else { } else {
if (ev.state == MetadataTaskState.prcoessing) { if (ev.state == MetadataTaskState.prcoessing) {

View file

@ -1,3 +1,4 @@
import 'package:battery_plus/battery_plus.dart';
import 'package:event_bus/event_bus.dart'; import 'package:event_bus/event_bus.dart';
import 'package:kiwi/kiwi.dart'; import 'package:kiwi/kiwi.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
@ -53,6 +54,7 @@ class UpdateMissingMetadata {
// since we need to download multiple images in their original size, // since we need to download multiple images in their original size,
// we only do it with WiFi // we only do it with WiFi
await ensureWifi(); await ensureWifi();
await ensureBattery();
KiwiContainer().resolve<EventBus>().fire( KiwiContainer().resolve<EventBus>().fire(
const MetadataTaskStateChangedEvent(MetadataTaskState.prcoessing)); const MetadataTaskStateChangedEvent(MetadataTaskState.prcoessing));
if (!shouldRun) { if (!shouldRun) {
@ -116,6 +118,17 @@ class UpdateMissingMetadata {
} }
} }
Future<void> ensureBattery() async {
while (await Battery().batteryLevel <= 15) {
if (!shouldRun) {
throw const InterruptedException();
}
KiwiContainer().resolve<EventBus>().fire(
const MetadataTaskStateChangedEvent(MetadataTaskState.lowBattery));
await Future.delayed(const Duration(seconds: 5));
}
}
final FileRepo fileRepo; final FileRepo fileRepo;
bool shouldRun = true; bool shouldRun = true;

View file

@ -692,6 +692,21 @@ class _Web {
style: const TextStyle(fontSize: 12), 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( Expanded(
child: Container(), child: Container(),

View file

@ -36,6 +36,48 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.8.2" 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: bloc:
dependency: "direct main" dependency: "direct main"
description: description:
@ -126,7 +168,7 @@ packages:
name: connectivity_plus_linux name: connectivity_plus_linux
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.1" version: "1.3.0"
connectivity_plus_macos: connectivity_plus_macos:
dependency: transitive dependency: transitive
description: description:
@ -140,7 +182,7 @@ packages:
name: connectivity_plus_platform_interface name: connectivity_plus_platform_interface
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.1.1" version: "1.2.0"
connectivity_plus_web: connectivity_plus_web:
dependency: transitive dependency: transitive
description: description:
@ -189,7 +231,7 @@ packages:
name: dbus name: dbus
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.6.8" version: "0.7.3"
device_info_plus: device_info_plus:
dependency: "direct main" dependency: "direct main"
description: description:
@ -625,7 +667,7 @@ packages:
name: nm name: nm
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.4.4" version: "0.5.0"
node_preamble: node_preamble:
dependency: transitive dependency: transitive
description: description:

View file

@ -29,6 +29,7 @@ dependencies:
# android only # android only
android_intent_plus: ^3.0.1 android_intent_plus: ^3.0.1
battery_plus: ^2.1.3
bloc: ^7.0.0 bloc: ^7.0.0
cached_network_image: ^3.0.0 cached_network_image: ^3.0.0
collection: ^1.15.0 collection: ^1.15.0