Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Handle unavailable simulator battery state

Neeraj Gupta01b773560f63
diff --git a/mobile/apps/photos/lib/services/machine_learning/device_health_policy.dart b/mobile/apps/photos/lib/services/machine_learning/device_health_policy.dart
index fd6c9d259b..a551436d9d 100644
--- a/mobile/apps/photos/lib/services/machine_learning/device_health_policy.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/device_health_policy.dart
@@ -43,10 +43,15 @@ class DeviceHealthPolicy {
final issues = <DeviceHealthIssue>{};
final battery = snapshot.battery;
- if (battery.status != DeviceSignalStatus.available) {
- issues.add(DeviceHealthIssue.batteryUnavailable);
- } else if (battery.levelPercent! < minimumBatteryLevel) {
- issues.add(DeviceHealthIssue.lowBattery);
+ final batteryUnsupportedOnIOS =
+ snapshot.platform == DeviceHealthPlatform.ios &&
+ battery.status == DeviceSignalStatus.unsupported;
+ if (!batteryUnsupportedOnIOS) {
+ if (battery.status != DeviceSignalStatus.available) {
+ issues.add(DeviceHealthIssue.batteryUnavailable);
+ } else if (battery.levelPercent! < minimumBatteryLevel) {
+ issues.add(DeviceHealthIssue.lowBattery);
+ }
}
if (snapshot.platform == DeviceHealthPlatform.android &&
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/ios/Classes/DeviceHealthService.swift b/mobile/apps/photos/plugins/ente_photos_platform/ios/Classes/DeviceHealthService.swift
index a0b6e09152..69ef9ec7fe 100644
--- a/mobile/apps/photos/plugins/ente_photos_platform/ios/Classes/DeviceHealthService.swift
+++ b/mobile/apps/photos/plugins/ente_photos_platform/ios/Classes/DeviceHealthService.swift
@@ -14,7 +14,9 @@ public final class DeviceHealthService {
public func snapshot() -> DeviceHealthSnapshot {
let device = UIDevice.current
- let restoreMonitoring = !device.isBatteryMonitoringEnabled
+ let restoreMonitoring =
+ Self.supportsBatteryMonitoring
+ && !device.isBatteryMonitoringEnabled
if restoreMonitoring {
device.isBatteryMonitoringEnabled = true
}
@@ -38,35 +40,39 @@ public final class DeviceHealthService {
self.observer = observer
let device = UIDevice.current
- if !device.isBatteryMonitoringEnabled {
+ if Self.supportsBatteryMonitoring && !device.isBatteryMonitoringEnabled {
device.isBatteryMonitoringEnabled = true
enabledBatteryMonitoring = true
}
let center = NotificationCenter.default
observerTokens = [
- center.addObserver(
- forName: UIDevice.batteryLevelDidChangeNotification,
- object: device,
- queue: .main
- ) { [weak self] _ in
- Task { @MainActor [weak self] in self?.emitSnapshot() }
- },
- center.addObserver(
- forName: UIDevice.batteryStateDidChangeNotification,
- object: device,
- queue: .main
- ) { [weak self] _ in
- Task { @MainActor [weak self] in self?.emitSnapshot() }
- },
center.addObserver(
forName: ProcessInfo.thermalStateDidChangeNotification,
object: ProcessInfo.processInfo,
queue: .main
) { [weak self] _ in
Task { @MainActor [weak self] in self?.emitSnapshot() }
- },
+ }
]
+ if Self.supportsBatteryMonitoring {
+ observerTokens += [
+ center.addObserver(
+ forName: UIDevice.batteryLevelDidChangeNotification,
+ object: device,
+ queue: .main
+ ) { [weak self] _ in
+ Task { @MainActor [weak self] in self?.emitSnapshot() }
+ },
+ center.addObserver(
+ forName: UIDevice.batteryStateDidChangeNotification,
+ object: device,
+ queue: .main
+ ) { [weak self] _ in
+ Task { @MainActor [weak self] in self?.emitSnapshot() }
+ },
+ ]
+ }
emitSnapshot()
}
@@ -99,6 +105,7 @@ public final class DeviceHealthService {
}
private func batterySnapshot() -> DeviceSignal<BatteryReading> {
+ guard Self.supportsBatteryMonitoring else { return .unsupported }
let level = UIDevice.current.batteryLevel
guard level >= 0, level <= 1,
let reading = try? BatteryReading(levelPercent: Int(level * 100))
@@ -122,4 +129,12 @@ public final class DeviceHealthService {
let battery: DeviceSignal<BatteryReading>
let thermal: DeviceSignal<ThermalState>
}
+
+ private static var supportsBatteryMonitoring: Bool {
+ #if targetEnvironment(simulator)
+ false
+ #else
+ true
+ #endif
+ }
}
diff --git a/mobile/apps/photos/test/services/machine_learning/device_health_policy_test.dart b/mobile/apps/photos/test/services/machine_learning/device_health_policy_test.dart
index 1a63d2eba3..b1d6920549 100644
--- a/mobile/apps/photos/test/services/machine_learning/device_health_policy_test.dart
+++ b/mobile/apps/photos/test/services/machine_learning/device_health_policy_test.dart
@@ -42,6 +42,21 @@ void main() {
);
});
+ test('accepts unsupported iOS battery evidence', () {
+ expect(
+ issues(
+ _snapshot(
+ now,
+ platform: DeviceHealthPlatform.ios,
+ battery: const DeviceBatterySnapshot(
+ status: DeviceSignalStatus.unsupported,
+ ),
+ ),
+ ),
+ isEmpty,
+ );
+ });
+
test('rejects hot or unknown Android battery evidence', () {
expect(
issues(_snapshot(now, battery: _androidBattery(temperature: 42.1))),