π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile][photos] simplify device trash error handling
Priyanshu Dangarecab37d07e458
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicetrash/DeviceTrashService.kt b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicetrash/DeviceTrashService.ktindex e512408a33..ebf86b1397 100644--- a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicetrash/DeviceTrashService.kt+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicetrash/DeviceTrashService.kt@@ -2,7 +2,6 @@ package io.ente.photos.platform.devicetrashimport android.content.ContentResolverimport android.content.Context-import android.os.Buildimport android.os.Bundleimport android.os.Handlerimport android.os.Looper@@ -22,16 +21,20 @@ internal class DeviceTrashService(context: Context) {@Volatileprivate var isClosed = false- fun getFiles(callback: (Result<List<DeviceTrashFile>>) -> Unit) {+ fun getFiles(+ onSuccess: (List<DeviceTrashFile>) -> Unit,+ onFailure: (Exception) -> Unit,+ ) {executor.execute {- val result =- try {- Result.success(queryFiles())- } catch (error: Exception) {- Result.failure(error)+ try {+ val files = queryFiles()+ mainHandler.post {+ if (!isClosed) onSuccess(files)+ }+ } catch (error: Exception) {+ mainHandler.post {+ if (!isClosed) onFailure(error)}- mainHandler.post {- if (!isClosed) callback(result)}}}@@ -43,10 +46,6 @@ internal class DeviceTrashService(context: Context) {}private fun queryFiles(): List<DeviceTrashFile> {- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {- throw UnsupportedOperationException("Device trash requires Android 11 or newer")- }-val projection =arrayOf(MediaStore.Files.FileColumns._ID,diff --git a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/DeviceTrashChannelAdapter.kt b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/DeviceTrashChannelAdapter.ktindex b54ce9061d..2f103dda0f 100644--- a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/DeviceTrashChannelAdapter.kt+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/DeviceTrashChannelAdapter.kt@@ -1,5 +1,6 @@package io.ente.photos.platform.flutter+import android.os.Buildimport io.ente.photos.platform.devicetrash.DeviceTrashFileimport io.ente.photos.platform.devicetrash.DeviceTrashServiceimport io.flutter.embedding.engine.plugins.FlutterPlugin@@ -15,23 +16,17 @@ internal class DeviceTrashChannelAdapter : MethodChannel.MethodCallHandler {override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) =when (call.method) {- "deviceTrash.getFiles" -> service.getFiles { filesResult ->- filesResult.fold(- onSuccess = { files -> result.success(files.map { it.toChannelMap() }) },- onFailure = { error ->- result.error(- when (error) {- is UnsupportedOperationException ->- "device_trash_unsupported"-- else -> "device_trash_query_failed"- },- error.message,- null,- )- },- )- }+ "deviceTrash.getFiles" ->+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {+ result.error("device_trash_unsupported", null, null)+ } else {+ service.getFiles(+ onSuccess = { files -> result.success(files.map { it.toChannelMap() }) },+ onFailure = { error ->+ result.error("device_trash_query_failed", error.message, null)+ },+ )+ }else -> result.notImplemented()}diff --git a/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_trash.dart b/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_trash.dartindex 1bad10e915..a5c92a8b90 100644--- a/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_trash.dart+++ b/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_trash.dart@@ -23,27 +23,23 @@ class DeviceTrashFile {final String deviceFolder;}-abstract interface class DeviceTrashSource {- Future<List<DeviceTrashFile>> getFiles();-}--class DeviceTrashClient implements DeviceTrashSource {- DeviceTrashClient({MethodChannel? methodChannel})- : _methodChannel = methodChannel ?? const MethodChannel(_methodChannelName);-+class DeviceTrashClient {static final instance = DeviceTrashClient();- static const _methodChannelName = 'io.ente.photos.platform';+ static const _methodChannel = MethodChannel('io.ente.photos.platform');- final MethodChannel _methodChannel;-- @overrideFuture<List<DeviceTrashFile>> getFiles() async {if (!Platform.isAndroid) {throw UnsupportedError('Device trash is only supported on Android');}- final result = await _methodChannel.invokeListMethod<Map<dynamic, dynamic>>(- 'deviceTrash.getFiles',- );- return List.unmodifiable(result!.map(DeviceTrashFile.fromMap));+ try {+ final result = await _methodChannel+ .invokeListMethod<Map<dynamic, dynamic>>('deviceTrash.getFiles');+ return List.unmodifiable(result!.map(DeviceTrashFile.fromMap));+ } on PlatformException catch (error) {+ if (error.code == 'device_trash_unsupported') {+ throw UnsupportedError('Device trash requires Android 11 or newer');+ }+ rethrow;+ }}}