π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Refactor Photos platform plugin structure
Neeraj Gupta2b952482a247
diff --git a/mobile/apps/photos/ios/Podfile.lock b/mobile/apps/photos/ios/Podfile.lockindex a463a44e1c..06c5de2b45 100644--- a/mobile/apps/photos/ios/Podfile.lock+++ b/mobile/apps/photos/ios/Podfile.lock@@ -456,7 +456,7 @@ SPEC CHECKSUMS:ente_cast: a25feb3127397e5fc6aff03a12120916707757d4ente_mail: 90753dd584f49ee092759a32d4143ca51db69641ente_panorama_viewer: 87bb22e7868c3005314f80c05ee0787aa07edcd8- ente_photos_platform: 9a5d62b4008563bdfefe6a42cc4297c5c19bf70c+ ente_photos_platform: 08757c7e4977863732f75ce0f85cc6144600e3d2ente_photos_rust: 2db1f31cbe3f13ef0f52909a52b1f885b880a6ecente_qr: 86beaf5d2644705db9b565b32dd8b82c05fb36afente_rust: d0197ebd5460e8f67576579452b073d698404abediff --git a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/PhotosPlatformPlugin.kt b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/PhotosPlatformPlugin.ktindex 038f1cb6a3..75087f9a4c 100644--- a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/PhotosPlatformPlugin.kt+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/PhotosPlatformPlugin.kt@@ -1,132 +1,16 @@package io.ente.photos.platform-import io.ente.photos.platform.devicehealth.BatteryHealth-import io.ente.photos.platform.devicehealth.BatteryReading-import io.ente.photos.platform.devicehealth.DeviceHealthError-import io.ente.photos.platform.devicehealth.DeviceHealthService-import io.ente.photos.platform.devicehealth.DeviceHealthSnapshot-import io.ente.photos.platform.devicehealth.DeviceSignal-import io.ente.photos.platform.devicehealth.ThermalState+import io.ente.photos.platform.flutter.DeviceHealthChannelAdapterimport io.flutter.embedding.engine.plugins.FlutterPlugin-import io.flutter.plugin.common.EventChannel-import io.flutter.plugin.common.MethodCall-import io.flutter.plugin.common.MethodChannel-class PhotosPlatformPlugin :- FlutterPlugin,- MethodChannel.MethodCallHandler,- EventChannel.StreamHandler {- private lateinit var methodChannel: MethodChannel- private lateinit var eventChannel: EventChannel- private lateinit var healthService: DeviceHealthService- private var eventSink: EventChannel.EventSink? = null+class PhotosPlatformPlugin : FlutterPlugin {+ private val deviceHealthAdapter = DeviceHealthChannelAdapter()override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {- healthService = DeviceHealthService(binding.applicationContext)- methodChannel = MethodChannel(binding.binaryMessenger, METHOD_CHANNEL)- eventChannel = EventChannel(binding.binaryMessenger, EVENT_CHANNEL)- methodChannel.setMethodCallHandler(this)- eventChannel.setStreamHandler(this)- }-- override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {- when (call.method) {- "deviceHealth.getSnapshot" -> result.success(healthService.snapshot().toChannelMap())- "deviceHealth.getMemorySnapshot" ->- result.success(healthService.memorySnapshot().toMemoryChannelMap())- else -> result.notImplemented()- }- }-- override fun onListen(arguments: Any?, events: EventChannel.EventSink) {- eventSink = events- healthService.startObserving { snapshot ->- eventSink?.success(snapshot.toChannelMap())- }- }-- override fun onCancel(arguments: Any?) {- eventSink = null- healthService.stopObserving()+ deviceHealthAdapter.attach(binding)}override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {- eventSink = null- healthService.stopObserving()- methodChannel.setMethodCallHandler(null)- eventChannel.setStreamHandler(null)- }-- private fun DeviceHealthSnapshot.toChannelMap(): Map<String, Any?> =- mapOf(- "platform" to "android",- "observedAtMs" to observedAtMs,- "battery" to battery.toBatteryChannelMap(),- "thermal" to thermal.toThermalChannelMap(),- )-- private fun DeviceSignal<BatteryReading>.toBatteryChannelMap(): Map<String, Any?> =- when (this) {- is DeviceSignal.Available ->- buildMap {- put("status", "available")- put("levelPercent", value.levelPercent)- value.temperatureCelsius?.let { put("temperatureCelsius", it) }- put("health", value.health.channelValue)- }- DeviceSignal.Unsupported -> mapOf("status" to "unsupported")- is DeviceSignal.Unavailable -> unavailableChannelMap(error)- }-- private fun DeviceSignal<ThermalState>.toThermalChannelMap(): Map<String, Any?> =- when (this) {- is DeviceSignal.Available ->- mapOf("status" to "available", "state" to value.channelValue)- DeviceSignal.Unsupported -> mapOf("status" to "unsupported")- is DeviceSignal.Unavailable -> unavailableChannelMap(error)- }-- private fun DeviceSignal<Long>.toMemoryChannelMap(): Map<String, Any?> =- when (this) {- is DeviceSignal.Available -> mapOf("status" to "available", "totalBytes" to value)- DeviceSignal.Unsupported -> mapOf("status" to "unsupported")- is DeviceSignal.Unavailable -> unavailableChannelMap(error)- }-- private val BatteryHealth.channelValue: String- get() =- when (this) {- BatteryHealth.GOOD -> "good"- BatteryHealth.COLD -> "cold"- BatteryHealth.OVERHEATING -> "overheating"- BatteryHealth.OVER_VOLTAGE -> "overVoltage"- BatteryHealth.DEAD -> "dead"- BatteryHealth.FAILURE -> "failure"- BatteryHealth.UNKNOWN -> "unknown"- }-- private val ThermalState.channelValue: String- get() = name.lowercase()-- private fun unavailableChannelMap(error: DeviceHealthError): Map<String, Any?> =- mapOf("status" to "unavailable", "errorCode" to error.channelValue)-- private val DeviceHealthError.channelValue: String- get() =- when (this) {- DeviceHealthError.BATTERY_INTENT_MISSING -> "battery_intent_missing"- DeviceHealthError.BATTERY_NOT_PRESENT -> "battery_not_present"- DeviceHealthError.BATTERY_LEVEL_MISSING -> "battery_level_missing"- DeviceHealthError.BATTERY_ACCESS_DENIED -> "battery_access_denied"- DeviceHealthError.BATTERY_READ_FAILED -> "battery_read_failed"- DeviceHealthError.THERMAL_STATUS_UNKNOWN -> "thermal_status_unknown"- DeviceHealthError.THERMAL_READ_FAILED -> "thermal_read_failed"- DeviceHealthError.MEMORY_TOTAL_MISSING -> "memory_total_missing"- DeviceHealthError.MEMORY_READ_FAILED -> "memory_read_failed"- }-- private companion object {- const val METHOD_CHANNEL = "io.ente.photos.platform"- const val EVENT_CHANNEL = "io.ente.photos.platform/device_health_events"+ deviceHealthAdapter.detach()}}diff --git a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/DeviceHealthChannelAdapter.kt b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/DeviceHealthChannelAdapter.ktnew file mode 100644index 0000000000..87c70b433a--- /dev/null+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/DeviceHealthChannelAdapter.kt@@ -0,0 +1,134 @@+package io.ente.photos.platform.flutter++import io.ente.photos.platform.devicehealth.BatteryHealth+import io.ente.photos.platform.devicehealth.BatteryReading+import io.ente.photos.platform.devicehealth.DeviceHealthError+import io.ente.photos.platform.devicehealth.DeviceHealthService+import io.ente.photos.platform.devicehealth.DeviceHealthSnapshot+import io.ente.photos.platform.devicehealth.DeviceSignal+import io.ente.photos.platform.devicehealth.ThermalState+import io.flutter.embedding.engine.plugins.FlutterPlugin+import io.flutter.plugin.common.EventChannel+import io.flutter.plugin.common.MethodCall+import io.flutter.plugin.common.MethodChannel++internal class DeviceHealthChannelAdapter :+ MethodChannel.MethodCallHandler,+ EventChannel.StreamHandler {+ private lateinit var methodChannel: MethodChannel+ private lateinit var eventChannel: EventChannel+ private lateinit var service: DeviceHealthService+ private var eventSink: EventChannel.EventSink? = null++ fun attach(binding: FlutterPlugin.FlutterPluginBinding) {+ service = DeviceHealthService(binding.applicationContext)+ methodChannel = MethodChannel(binding.binaryMessenger, METHOD_CHANNEL)+ eventChannel = EventChannel(binding.binaryMessenger, EVENT_CHANNEL)+ methodChannel.setMethodCallHandler(this)+ eventChannel.setStreamHandler(this)+ }++ override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {+ when (call.method) {+ "deviceHealth.getSnapshot" -> result.success(service.snapshot().toChannelMap())+ "deviceHealth.getMemorySnapshot" ->+ result.success(service.memorySnapshot().toMemoryChannelMap())++ else -> result.notImplemented()+ }+ }++ override fun onListen(arguments: Any?, events: EventChannel.EventSink) {+ eventSink = events+ service.startObserving { snapshot ->+ eventSink?.success(snapshot.toChannelMap())+ }+ }++ override fun onCancel(arguments: Any?) {+ eventSink = null+ service.stopObserving()+ }++ fun detach() {+ eventSink = null+ service.stopObserving()+ methodChannel.setMethodCallHandler(null)+ eventChannel.setStreamHandler(null)+ }++ private fun DeviceHealthSnapshot.toChannelMap(): Map<String, Any?> =+ mapOf(+ "platform" to "android",+ "observedAtMs" to observedAtMs,+ "battery" to battery.toBatteryChannelMap(),+ "thermal" to thermal.toThermalChannelMap(),+ )++ private fun DeviceSignal<BatteryReading>.toBatteryChannelMap(): Map<String, Any?> =+ when (this) {+ is DeviceSignal.Available ->+ buildMap {+ put("status", "available")+ put("levelPercent", value.levelPercent)+ value.temperatureCelsius?.let { put("temperatureCelsius", it) }+ put("health", value.health.channelValue)+ }++ DeviceSignal.Unsupported -> mapOf("status" to "unsupported")+ is DeviceSignal.Unavailable -> unavailableChannelMap(error)+ }++ private fun DeviceSignal<ThermalState>.toThermalChannelMap(): Map<String, Any?> =+ when (this) {+ is DeviceSignal.Available ->+ mapOf("status" to "available", "state" to value.channelValue)++ DeviceSignal.Unsupported -> mapOf("status" to "unsupported")+ is DeviceSignal.Unavailable -> unavailableChannelMap(error)+ }++ private fun DeviceSignal<Long>.toMemoryChannelMap(): Map<String, Any?> =+ when (this) {+ is DeviceSignal.Available -> mapOf("status" to "available", "totalBytes" to value)+ DeviceSignal.Unsupported -> mapOf("status" to "unsupported")+ is DeviceSignal.Unavailable -> unavailableChannelMap(error)+ }++ private val BatteryHealth.channelValue: String+ get() =+ when (this) {+ BatteryHealth.GOOD -> "good"+ BatteryHealth.COLD -> "cold"+ BatteryHealth.OVERHEATING -> "overheating"+ BatteryHealth.OVER_VOLTAGE -> "overVoltage"+ BatteryHealth.DEAD -> "dead"+ BatteryHealth.FAILURE -> "failure"+ BatteryHealth.UNKNOWN -> "unknown"+ }++ private val ThermalState.channelValue: String+ get() = name.lowercase()++ private fun unavailableChannelMap(error: DeviceHealthError): Map<String, Any?> =+ mapOf("status" to "unavailable", "errorCode" to error.channelValue)++ private val DeviceHealthError.channelValue: String+ get() =+ when (this) {+ DeviceHealthError.BATTERY_INTENT_MISSING -> "battery_intent_missing"+ DeviceHealthError.BATTERY_NOT_PRESENT -> "battery_not_present"+ DeviceHealthError.BATTERY_LEVEL_MISSING -> "battery_level_missing"+ DeviceHealthError.BATTERY_ACCESS_DENIED -> "battery_access_denied"+ DeviceHealthError.BATTERY_READ_FAILED -> "battery_read_failed"+ DeviceHealthError.THERMAL_STATUS_UNKNOWN -> "thermal_status_unknown"+ DeviceHealthError.THERMAL_READ_FAILED -> "thermal_read_failed"+ DeviceHealthError.MEMORY_TOTAL_MISSING -> "memory_total_missing"+ DeviceHealthError.MEMORY_READ_FAILED -> "memory_read_failed"+ }++ private companion object {+ const val METHOD_CHANNEL = "io.ente.photos.platform"+ const val EVENT_CHANNEL = "io.ente.photos.platform/device_health_events"+ }+}diff --git a/mobile/apps/photos/plugins/ente_photos_platform/ios/Classes/PhotosPlatformPlugin.swift b/mobile/apps/photos/plugins/ente_photos_platform/ios/Flutter/DeviceHealthChannelAdapter.swiftsimilarity index 76%rename from mobile/apps/photos/plugins/ente_photos_platform/ios/Classes/PhotosPlatformPlugin.swiftrename to mobile/apps/photos/plugins/ente_photos_platform/ios/Flutter/DeviceHealthChannelAdapter.swiftindex 5dee8677f6..aa9082e864 100644--- a/mobile/apps/photos/plugins/ente_photos_platform/ios/Classes/PhotosPlatformPlugin.swift+++ b/mobile/apps/photos/plugins/ente_photos_platform/ios/Flutter/DeviceHealthChannelAdapter.swift@@ -2,13 +2,14 @@import Foundation@MainActor-public final class PhotosPlatformPlugin: NSObject, @preconcurrency FlutterPlugin {+final class DeviceHealthChannelAdapter: NSObject {private let methodChannel: FlutterMethodChannelprivate let eventChannel: FlutterEventChannel- private let healthService = DeviceHealthService()+ private let service = DeviceHealthService()private var eventSink: FlutterEventSink?+ private var isAttached = true- private init(registrar: FlutterPluginRegistrar) {+ init(registrar: FlutterPluginRegistrar) {methodChannel = FlutterMethodChannel(name: Self.methodChannelName,binaryMessenger: registrar.messenger()@@ -18,52 +19,53 @@ public final class PhotosPlatformPlugin: NSObject, @preconcurrency FlutterPluginbinaryMessenger: registrar.messenger())super.init()+ methodChannel.setMethodCallHandler { [weak self] call, result in+ self?.handle(call, result: result)+ }+ eventChannel.setStreamHandler(self)}- public static func register(with registrar: FlutterPluginRegistrar) {- let instance = PhotosPlatformPlugin(registrar: registrar)- registrar.addMethodCallDelegate(instance, channel: instance.methodChannel)- instance.eventChannel.setStreamHandler(instance)- registrar.publish(instance)+ func detach() {+ guard isAttached else { return }+ isAttached = false+ eventSink = nil+ service.stopObserving()+ methodChannel.setMethodCallHandler(nil)+ eventChannel.setStreamHandler(nil)}- public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {+ private func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {+ guard isAttached else { return }switch call.method {case "deviceHealth.getSnapshot":- result(healthService.snapshot().channelValue)+ result(service.snapshot().channelValue)case "deviceHealth.getMemorySnapshot":- result(healthService.memorySnapshot().memoryChannelValue)+ result(service.memorySnapshot().memoryChannelValue)default:result(FlutterMethodNotImplemented)}}- public func detachFromEngine(for registrar: FlutterPluginRegistrar) {- eventSink = nil- healthService.stopObserving()- methodChannel.setMethodCallHandler(nil)- eventChannel.setStreamHandler(nil)- }-private static let methodChannelName = "io.ente.photos.platform"private static let eventChannelName = "io.ente.photos.platform/device_health_events"}-extension PhotosPlatformPlugin: @preconcurrency FlutterStreamHandler {- public func onListen(+extension DeviceHealthChannelAdapter: @preconcurrency FlutterStreamHandler {+ func onListen(withArguments arguments: Any?,eventSink events: @escaping FlutterEventSink) -> FlutterError? {+ guard isAttached else { return nil }eventSink = events- healthService.startObserving { [weak self] snapshot in+ service.startObserving { [weak self] snapshot inself?.eventSink?(snapshot.channelValue)}return nil}- public func onCancel(withArguments arguments: Any?) -> FlutterError? {+ func onCancel(withArguments arguments: Any?) -> FlutterError? {eventSink = nil- healthService.stopObserving()+ service.stopObserving()return nil}}diff --git a/mobile/apps/photos/plugins/ente_photos_platform/ios/Flutter/PhotosPlatformPlugin.swift b/mobile/apps/photos/plugins/ente_photos_platform/ios/Flutter/PhotosPlatformPlugin.swiftnew file mode 100644index 0000000000..71290b3f9d--- /dev/null+++ b/mobile/apps/photos/plugins/ente_photos_platform/ios/Flutter/PhotosPlatformPlugin.swift@@ -0,0 +1,20 @@+@preconcurrency import Flutter+import Foundation++@MainActor+public final class PhotosPlatformPlugin: NSObject, @preconcurrency FlutterPlugin {+ private let deviceHealthAdapter: DeviceHealthChannelAdapter++ private init(registrar: FlutterPluginRegistrar) {+ deviceHealthAdapter = DeviceHealthChannelAdapter(registrar: registrar)+ super.init()+ }++ public static func register(with registrar: FlutterPluginRegistrar) {+ registrar.publish(PhotosPlatformPlugin(registrar: registrar))+ }++ public func detachFromEngine(for registrar: FlutterPluginRegistrar) {+ deviceHealthAdapter.detach()+ }+}diff --git a/mobile/apps/photos/plugins/ente_photos_platform/ios/Package.swift b/mobile/apps/photos/plugins/ente_photos_platform/ios/Package.swiftindex 8f59a09032..84590aae5f 100644--- a/mobile/apps/photos/plugins/ente_photos_platform/ios/Package.swift+++ b/mobile/apps/photos/plugins/ente_photos_platform/ios/Package.swift@@ -11,8 +11,7 @@ let package = Package(targets: [.target(name: "PhotosPlatformCore",- path: "Classes",- exclude: ["PhotosPlatformPlugin.swift"]+ path: "Sources/PhotosPlatformCore")],swiftLanguageVersions: [.v5]diff --git a/mobile/apps/photos/plugins/ente_photos_platform/ios/Classes/DeviceHealthModels.swift b/mobile/apps/photos/plugins/ente_photos_platform/ios/Sources/PhotosPlatformCore/DeviceHealthModels.swiftsimilarity index 100%rename from mobile/apps/photos/plugins/ente_photos_platform/ios/Classes/DeviceHealthModels.swiftrename to mobile/apps/photos/plugins/ente_photos_platform/ios/Sources/PhotosPlatformCore/DeviceHealthModels.swiftdiff --git a/mobile/apps/photos/plugins/ente_photos_platform/ios/Classes/DeviceHealthService.swift b/mobile/apps/photos/plugins/ente_photos_platform/ios/Sources/PhotosPlatformCore/DeviceHealthService.swiftsimilarity index 100%rename from mobile/apps/photos/plugins/ente_photos_platform/ios/Classes/DeviceHealthService.swiftrename to mobile/apps/photos/plugins/ente_photos_platform/ios/Sources/PhotosPlatformCore/DeviceHealthService.swiftdiff --git a/mobile/apps/photos/plugins/ente_photos_platform/ios/ente_photos_platform.podspec b/mobile/apps/photos/plugins/ente_photos_platform/ios/ente_photos_platform.podspecindex 85f43936bd..bcd9bcc484 100644--- a/mobile/apps/photos/plugins/ente_photos_platform/ios/ente_photos_platform.podspec+++ b/mobile/apps/photos/plugins/ente_photos_platform/ios/ente_photos_platform.podspec@@ -6,7 +6,7 @@ Pod::Spec.new do |s|s.license = { :type => 'AGPL-3.0-only' }s.author = { 'Ente' => '[email protected]' }s.source = { :path => '.' }- s.source_files = 'Classes/**/*'+ s.source_files = '{Flutter,Sources}/**/*'s.dependency 'Flutter's.platform = :ios, '15.1's.swift_version = '5.0'