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
waitingForWifi,
/// Paused on low battery
lowBattery,
}
class MetadataTaskStateChangedEvent {

View file

@ -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": {

View file

@ -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"
]
}

View file

@ -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) {

View file

@ -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<EventBus>().fire(
const MetadataTaskStateChangedEvent(MetadataTaskState.prcoessing));
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;
bool shouldRun = true;

View file

@ -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(),

View file

@ -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:

View file

@ -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