Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile][photos] add missing device trash sources

Priyanshu Dangare38ccec274666
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.kt
new file mode 100644
index 0000000000..ec0654b93f
--- /dev/null
+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/devicetrash/DeviceTrashService.kt
@@ -0,0 +1,107 @@
+package io.ente.photos.platform.devicetrash
+
+import android.content.ContentResolver
+import android.content.Context
+import android.os.Build
+import android.os.Bundle
+import android.os.Handler
+import android.os.Looper
+import android.provider.MediaStore
+import java.util.concurrent.Executors
+
+internal data class DeviceTrashFile(
+ val localID: Long,
+ val deleteBy: Long,
+ val deviceFolder: String,
+)
+
+internal class DeviceTrashService(context: Context) {
+ private val contentResolver = context.applicationContext.contentResolver
+ private val executor = Executors.newSingleThreadExecutor()
+ private val mainHandler = Handler(Looper.getMainLooper())
+ @Volatile
+ private var isClosed = false
+
+ fun getFiles(callback: (Result<List<DeviceTrashFile>>) -> Unit) {
+ executor.execute {
+ val result =
+ try {
+ Result.success(queryFiles())
+ } catch (error: Exception) {
+ Result.failure(error)
+ }
+ mainHandler.post {
+ if (!isClosed) callback(result)
+ }
+ }
+ }
+
+ fun close() {
+ isClosed = true
+ executor.shutdownNow()
+ mainHandler.removeCallbacksAndMessages(null)
+ }
+
+ 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,
+ MediaStore.MediaColumns.DATE_EXPIRES,
+ MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
+ )
+ val selection =
+ "${MediaStore.MediaColumns.IS_TRASHED} = ? AND " +
+ "${MediaStore.Files.FileColumns.MEDIA_TYPE} IN (?, ?)"
+ val queryArgs =
+ Bundle().apply {
+ putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection)
+ putStringArray(
+ ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS,
+ arrayOf(
+ "1",
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString(),
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO.toString(),
+ ),
+ )
+ putString(
+ ContentResolver.QUERY_ARG_SQL_SORT_ORDER,
+ "${MediaStore.MediaColumns.DATE_EXPIRES} DESC",
+ )
+ putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_ONLY)
+ }
+ val files = mutableListOf<DeviceTrashFile>()
+ contentResolver.query(
+ MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL),
+ projection,
+ queryArgs,
+ null,
+ )?.use { cursor ->
+ val localIDColumn = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID)
+ val deleteByColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_EXPIRES)
+ val deviceFolderColumn =
+ cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME)
+ while (cursor.moveToNext()) {
+ files +=
+ DeviceTrashFile(
+ localID = cursor.getLong(localIDColumn),
+ deleteBy =
+ cursor.getLong(deleteByColumn) *
+ MICROSECONDS_PER_SECOND,
+ deviceFolder =
+ cursor.getString(deviceFolderColumn)
+ ?.takeUnless(String::isBlank)
+ ?: "Unknown Folder",
+ )
+ }
+ }
+ return files
+ }
+
+ private companion object {
+ const val MICROSECONDS_PER_SECOND = 1_000_000L
+ }
+}
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.kt
new file mode 100644
index 0000000000..de42c52e5a
--- /dev/null
+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/DeviceTrashChannelAdapter.kt
@@ -0,0 +1,52 @@
+package io.ente.photos.platform.flutter
+
+import io.ente.photos.platform.devicetrash.DeviceTrashFile
+import io.ente.photos.platform.devicetrash.DeviceTrashService
+import io.flutter.embedding.engine.plugins.FlutterPlugin
+import io.flutter.plugin.common.MethodCall
+import io.flutter.plugin.common.MethodChannel
+
+internal class DeviceTrashChannelAdapter {
+ private lateinit var service: DeviceTrashService
+
+ fun attach(binding: FlutterPlugin.FlutterPluginBinding) {
+ service = DeviceTrashService(binding.applicationContext)
+ }
+
+ fun handleMethodCall(call: MethodCall, result: MethodChannel.Result): Boolean =
+ 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,
+ )
+ },
+ )
+ }
+ true
+ }
+
+ else -> false
+ }
+
+ fun detach() {
+ service.close()
+ }
+
+ private fun DeviceTrashFile.toChannelMap(): Map<String, Any> =
+ mapOf(
+ "localID" to localID,
+ "deleteBy" to deleteBy,
+ "deviceFolder" to deviceFolder,
+ )
+}
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/PhotosPlatformChannelAdapter.kt b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/PhotosPlatformChannelAdapter.kt
new file mode 100644
index 0000000000..30670333ee
--- /dev/null
+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/flutter/PhotosPlatformChannelAdapter.kt
@@ -0,0 +1,34 @@
+package io.ente.photos.platform.flutter
+
+import io.flutter.embedding.engine.plugins.FlutterPlugin
+import io.flutter.plugin.common.MethodCall
+import io.flutter.plugin.common.MethodChannel
+
+internal class PhotosPlatformChannelAdapter : MethodChannel.MethodCallHandler {
+ private val deviceHealthAdapter = DeviceHealthChannelAdapter()
+ private val deviceTrashAdapter = DeviceTrashChannelAdapter()
+ private lateinit var methodChannel: MethodChannel
+
+ fun attach(binding: FlutterPlugin.FlutterPluginBinding) {
+ deviceHealthAdapter.attach(binding)
+ deviceTrashAdapter.attach(binding)
+ methodChannel = MethodChannel(binding.binaryMessenger, METHOD_CHANNEL)
+ methodChannel.setMethodCallHandler(this)
+ }
+
+ override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
+ if (deviceHealthAdapter.handleMethodCall(call, result)) return
+ if (deviceTrashAdapter.handleMethodCall(call, result)) return
+ result.notImplemented()
+ }
+
+ fun detach() {
+ methodChannel.setMethodCallHandler(null)
+ deviceHealthAdapter.detach()
+ deviceTrashAdapter.detach()
+ }
+
+ private companion object {
+ const val METHOD_CHANNEL = "io.ente.photos.platform"
+ }
+}
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.dart
new file mode 100644
index 0000000000..1bad10e915
--- /dev/null
+++ b/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_trash.dart
@@ -0,0 +1,49 @@
+import 'dart:io';
+
+import 'package:flutter/services.dart';
+
+class DeviceTrashFile {
+ const DeviceTrashFile({
+ required this.localID,
+ required this.deleteBy,
+ required this.deviceFolder,
+ });
+
+ factory DeviceTrashFile.fromMap(Map<dynamic, dynamic> map) => DeviceTrashFile(
+ localID: map['localID'],
+ deleteBy: map['deleteBy'],
+ deviceFolder: map['deviceFolder'],
+ );
+
+ final int localID;
+
+ /// The absolute deletion deadline in microseconds since the Unix epoch.
+ final int deleteBy;
+
+ final String deviceFolder;
+}
+
+abstract interface class DeviceTrashSource {
+ Future<List<DeviceTrashFile>> getFiles();
+}
+
+class DeviceTrashClient implements DeviceTrashSource {
+ DeviceTrashClient({MethodChannel? methodChannel})
+ : _methodChannel = methodChannel ?? const MethodChannel(_methodChannelName);
+
+ static final instance = DeviceTrashClient();
+ static const _methodChannelName = 'io.ente.photos.platform';
+
+ final MethodChannel _methodChannel;
+
+ @override
+ Future<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));
+ }
+}