浏览代码

Listen to device health stats

vishnukvmd 1 年之前
父节点
当前提交
2bb5730791

+ 7 - 0
lib/events/machine_learning_control_event.dart

@@ -0,0 +1,7 @@
+import "package:photos/events/event.dart";
+
+class MachineLearningControlEvent extends Event {
+  final bool shouldRun;
+
+  MachineLearningControlEvent(this.shouldRun);
+}

+ 2 - 0
lib/main.dart

@@ -30,6 +30,7 @@ import 'package:photos/services/feature_flag_service.dart';
 import 'package:photos/services/local_file_update_service.dart';
 import 'package:photos/services/local_sync_service.dart';
 import "package:photos/services/location_service.dart";
+import "package:photos/services/machine_learning/machine_learning_controller.dart";
 import 'package:photos/services/machine_learning/semantic_search/semantic_search_service.dart';
 import 'package:photos/services/memories_service.dart';
 import 'package:photos/services/push_service.dart';
@@ -194,6 +195,7 @@ Future<void> _init(bool isBackground, {String via = ''}) async {
   }
   unawaited(FeatureFlagService.instance.init());
   unawaited(SemanticSearchService.instance.init(isInBackground: isBackground));
+  MachineLearningController.instance.init();
   // Can not including existing tf/ml binaries as they are not being built
   // from source.
   // See https://gitlab.com/fdroid/fdroiddata/-/merge_requests/12671#note_1294346819

+ 54 - 0
lib/services/machine_learning/machine_learning_controller.dart

@@ -0,0 +1,54 @@
+import "dart:io";
+
+import "package:battery_info/battery_info_plugin.dart";
+import "package:battery_info/enums/charging_status.dart";
+import "package:battery_info/model/android_battery_info.dart";
+import "package:logging/logging.dart";
+import "package:photos/core/event_bus.dart";
+import "package:photos/events/machine_learning_control_event.dart";
+
+class MachineLearningController {
+  MachineLearningController._privateConstructor();
+
+  static final MachineLearningController instance =
+      MachineLearningController._privateConstructor();
+
+  final _logger = Logger("MachineLearningController");
+
+  static const kMaximumTemperature = 36; // 36 degree celsius
+  static const kMinimumBatteryLevel = 20; // 20%
+
+  void init() {
+    if (Platform.isAndroid) {
+      BatteryInfoPlugin()
+          .androidBatteryInfoStream
+          .listen((AndroidBatteryInfo? batteryInfo) {
+        _logger.info("Battery info: ${batteryInfo!.toJson()}");
+        if (_shouldRunMachineLearning(batteryInfo)) {
+          Bus.instance.fire(MachineLearningControlEvent(true));
+        } else {
+          Bus.instance.fire(MachineLearningControlEvent(false));
+        }
+      });
+    }
+  }
+
+  bool _shouldRunMachineLearning(AndroidBatteryInfo info) {
+    if (info.chargingStatus == ChargingStatus.Charging ||
+        info.chargingStatus == ChargingStatus.Full) {
+      return _isAcceptableTemperature(
+        info.temperature ?? kMaximumTemperature,
+      );
+    }
+    return _hasSufficientBattery(info.batteryLevel ?? kMinimumBatteryLevel) &&
+        _isAcceptableTemperature(info.temperature ?? kMaximumTemperature);
+  }
+
+  bool _hasSufficientBattery(int batteryLevel) {
+    return batteryLevel >= kMinimumBatteryLevel;
+  }
+
+  bool _isAcceptableTemperature(int temperature) {
+    return temperature <= kMaximumTemperature;
+  }
+}