π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Harden native device health observation
Neeraj Guptad4b6b53e9a04
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicehealth/DeviceHealthService.kt b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicehealth/DeviceHealthService.ktindex ad8fb0584e..9e4b9bc8a7 100644--- a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicehealth/DeviceHealthService.kt+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicehealth/DeviceHealthService.kt@@ -80,7 +80,7 @@ class DeviceHealthService(context: Context) {context.registerReceiver(batteryReceiver,IntentFilter(Intent.ACTION_BATTERY_CHANGED),- Context.RECEIVER_NOT_EXPORTED,+ Context.RECEIVER_EXPORTED,)} else {@Suppress("DEPRECATION")@@ -182,7 +182,7 @@ class DeviceHealthService(context: Context) {private fun stickyBatteryIntent(): Intent? {val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {- context.registerReceiver(null, filter, Context.RECEIVER_NOT_EXPORTED)+ context.registerReceiver(null, filter, Context.RECEIVER_EXPORTED)} else {@Suppress("DEPRECATION")context.registerReceiver(null, filter)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.swiftindex 69ef9ec7fe..eeeee31705 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@@ -7,24 +7,15 @@ public final class DeviceHealthService {private var observer: Observer?private var observerTokens: [NSObjectProtocol] = []- private var enabledBatteryMonitoring = false+ private var hasBatteryMonitoringLease = falseprivate var lastState: HealthState?public init() {}public func snapshot() -> DeviceHealthSnapshot {- let device = UIDevice.current- let restoreMonitoring =- Self.supportsBatteryMonitoring- && !device.isBatteryMonitoringEnabled- if restoreMonitoring {- device.isBatteryMonitoringEnabled = true- }- defer {- if restoreMonitoring {- device.isBatteryMonitoringEnabled = false- }- }+ guard Self.supportsBatteryMonitoring else { return currentSnapshot() }+ Self.acquireBatteryMonitoring()+ defer { Self.releaseBatteryMonitoring() }return currentSnapshot()}@@ -40,16 +31,18 @@ public final class DeviceHealthService {self.observer = observerlet device = UIDevice.current- if Self.supportsBatteryMonitoring && !device.isBatteryMonitoringEnabled {- device.isBatteryMonitoringEnabled = true- enabledBatteryMonitoring = true+ if Self.supportsBatteryMonitoring {+ Self.acquireBatteryMonitoring()+ hasBatteryMonitoringLease = true}let center = NotificationCenter.default+ let processInfo = ProcessInfo.processInfo+ _ = processInfo.thermalStateobserverTokens = [center.addObserver(forName: ProcessInfo.thermalStateDidChangeNotification,- object: ProcessInfo.processInfo,+ object: processInfo,queue: .main) { [weak self] _ inTask { @MainActor [weak self] in self?.emitSnapshot() }@@ -81,9 +74,9 @@ public final class DeviceHealthService {lastState = nilobserverTokens.forEach(NotificationCenter.default.removeObserver)observerTokens.removeAll()- if enabledBatteryMonitoring {- UIDevice.current.isBatteryMonitoringEnabled = false- enabledBatteryMonitoring = false+ if hasBatteryMonitoringLease {+ Self.releaseBatteryMonitoring()+ hasBatteryMonitoringLease = false}}@@ -137,4 +130,25 @@ public final class DeviceHealthService {true#endif}++ private static var batteryMonitoringLeaseCount = 0+ private static var ownsBatteryMonitoring = false++ private static func acquireBatteryMonitoring() {+ let device = UIDevice.current+ if !device.isBatteryMonitoringEnabled {+ device.isBatteryMonitoringEnabled = true+ ownsBatteryMonitoring = true+ }+ batteryMonitoringLeaseCount += 1+ }++ private static func releaseBatteryMonitoring() {+ precondition(batteryMonitoringLeaseCount > 0)+ batteryMonitoringLeaseCount -= 1+ if batteryMonitoringLeaseCount == 0 && ownsBatteryMonitoring {+ UIDevice.current.isBatteryMonitoringEnabled = false+ ownsBatteryMonitoring = false+ }+ }}