Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile][photos] Move system trash to platform plugin

Priyanshu Dangaref02761d17160
diff --git a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/MainActivity.kt b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/MainActivity.kt
index e60dbc6c9c..7422f67826 100644
--- a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/MainActivity.kt
+++ b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/MainActivity.kt
@@ -2,17 +2,8 @@ package io.ente.photos
import android.content.Intent
import io.flutter.embedding.android.FlutterFragmentActivity
-import io.flutter.embedding.engine.FlutterEngine
class MainActivity : FlutterFragmentActivity() {
- override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
- super.configureFlutterEngine(flutterEngine)
- NativeChannel.register(
- flutterEngine.dartExecutor.binaryMessenger,
- contentResolver,
- )
- }
-
override fun onNewIntent(intent: Intent) {
setIntent(intent)
super.onNewIntent(intent)
diff --git a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/NativeChannel.kt b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/NativeChannel.kt
deleted file mode 100644
index 3e7977df35..0000000000
--- a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/NativeChannel.kt
+++ /dev/null
@@ -1,89 +0,0 @@
-package io.ente.photos
-
-import android.content.ContentResolver
-import android.os.Build
-import android.os.Bundle
-import android.os.Handler
-import android.os.Looper
-import android.provider.MediaStore
-import com.fluttercandies.photo_manager.core.PhotoManagerPlugin
-import io.flutter.plugin.common.BinaryMessenger
-import io.flutter.plugin.common.MethodCall
-import io.flutter.plugin.common.MethodChannel
-
-object NativeChannel {
- private const val CHANNEL_NAME = "ente.io/photos/native"
- private val mainHandler = Handler(Looper.getMainLooper())
-
- fun register(messenger: BinaryMessenger, contentResolver: ContentResolver) {
- MethodChannel(messenger, CHANNEL_NAME).setMethodCallHandler { call, result ->
- handle(call, result, contentResolver)
- }
- }
-
- private fun handle(
- call: MethodCall,
- result: MethodChannel.Result,
- contentResolver: ContentResolver,
- ) {
- when (call.method) {
- "getTrash" -> PhotoManagerPlugin.runOnBackground {
- try {
- val files = getTrash(contentResolver)
- mainHandler.post { result.success(files) }
- } catch (error: Exception) {
- mainHandler.post {
- result.error(
- "media_store_query_failed",
- error.message,
- null,
- )
- }
- }
- }
- else -> result.notImplemented()
- }
- }
-
- private fun getTrash(contentResolver: ContentResolver): List<Map<String, Any>> {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return emptyList()
-
- val files = mutableListOf<Map<String, Any>>()
- val uri = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL)
- val queryArgs = Bundle().apply {
- putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_ONLY)
- putString(
- ContentResolver.QUERY_ARG_SQL_SELECTION,
- "${MediaStore.Files.FileColumns.MEDIA_TYPE} IN " +
- "(${MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE}, " +
- "${MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO})",
- )
- putString(
- ContentResolver.QUERY_ARG_SQL_SORT_ORDER,
- "${MediaStore.Files.FileColumns.DATE_EXPIRES} DESC",
- )
- }
-
- contentResolver.query(
- uri,
- arrayOf(
- MediaStore.Files.FileColumns._ID,
- MediaStore.Files.FileColumns.DATE_EXPIRES,
- MediaStore.Files.FileColumns.BUCKET_DISPLAY_NAME,
- ),
- queryArgs,
- null,
- )?.use { cursor ->
- while (cursor.moveToNext()) {
- files.add(
- mapOf(
- "localID" to cursor.getLong(0),
- "deleteBy" to cursor.getLong(1) * 1_000_000,
- "deviceFolder" to (cursor.getString(2) ?: "Unknown Folder"),
- ),
- )
- }
- }
- return files
- }
-}
diff --git a/mobile/apps/photos/lib/services/native_service.dart b/mobile/apps/photos/lib/services/native_service.dart
deleted file mode 100644
index 20549adef3..0000000000
--- a/mobile/apps/photos/lib/services/native_service.dart
+++ /dev/null
@@ -1,35 +0,0 @@
-import "dart:io";
-
-import "package:flutter/services.dart";
-
-class NativeTrashFile {
- final int localID;
- final int deleteBy;
- final String deviceFolder;
-
- const NativeTrashFile({
- required this.localID,
- required this.deleteBy,
- required this.deviceFolder,
- });
-}
-
-class NativeService {
- NativeService._();
-
- static const _channel = MethodChannel("ente.io/photos/native");
-
- static Future<List<NativeTrashFile>> getTrash() async {
- if (!Platform.isAndroid) return const [];
-
- final files = await _channel.invokeListMethod<Object?>("getTrash") ?? [];
- return files.map((file) {
- final map = file! as Map<Object?, Object?>;
- return NativeTrashFile(
- localID: map["localID"]! as int,
- deleteBy: map["deleteBy"]! as int,
- deviceFolder: map["deviceFolder"]! as String,
- );
- }).toList();
- }
-}
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/systemtrash/SystemTrashChannelAdapter.kt b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/systemtrash/SystemTrashChannelAdapter.kt
new file mode 100644
index 0000000000..80acf316f7
--- /dev/null
+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/systemtrash/SystemTrashChannelAdapter.kt
@@ -0,0 +1,48 @@
+package io.ente.photos.platform.systemtrash
+
+import android.os.Handler
+import android.os.Looper
+import io.flutter.plugin.common.MethodChannel
+import java.util.concurrent.Executors
+
+class SystemTrashChannelAdapter(private val service: SystemTrashService) {
+ private val mainHandler = Handler(Looper.getMainLooper())
+ private val executor = Executors.newSingleThreadExecutor()
+
+ @Volatile
+ private var closed = false
+
+ fun getFiles(result: MethodChannel.Result) {
+ executor.execute {
+ try {
+ val files = service.getFiles().map { it.toChannelMap() }
+ mainHandler.post {
+ if (!closed) result.success(files)
+ }
+ } catch (error: Exception) {
+ mainHandler.post {
+ if (!closed) {
+ result.error(
+ "media_store_query_failed",
+ error.message,
+ null,
+ )
+ }
+ }
+ }
+ }
+ }
+
+ fun close() {
+ closed = true
+ mainHandler.removeCallbacksAndMessages(null)
+ executor.shutdownNow()
+ }
+
+ private fun SystemTrashFile.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/systemtrash/SystemTrashService.kt b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/systemtrash/SystemTrashService.kt
new file mode 100644
index 0000000000..866b97e23b
--- /dev/null
+++ b/mobile/apps/photos/plugins/ente_photos_platform/android/src/main/kotlin/io/ente/photos/platform/systemtrash/SystemTrashService.kt
@@ -0,0 +1,54 @@
+package io.ente.photos.platform.systemtrash
+
+import android.content.ContentResolver
+import android.os.Build
+import android.os.Bundle
+import android.provider.MediaStore
+
+class SystemTrashService(private val contentResolver: ContentResolver) {
+ fun getFiles(): List<SystemTrashFile> {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return emptyList()
+
+ val queryArgs = Bundle().apply {
+ putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_ONLY)
+ putString(
+ ContentResolver.QUERY_ARG_SQL_SELECTION,
+ "${MediaStore.Files.FileColumns.MEDIA_TYPE} IN " +
+ "(${MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE}, " +
+ "${MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO})",
+ )
+ putString(
+ ContentResolver.QUERY_ARG_SQL_SORT_ORDER,
+ "${MediaStore.Files.FileColumns.DATE_EXPIRES} DESC",
+ )
+ }
+ return buildList {
+ contentResolver.query(
+ MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL),
+ arrayOf(
+ MediaStore.Files.FileColumns._ID,
+ MediaStore.Files.FileColumns.DATE_EXPIRES,
+ MediaStore.Files.FileColumns.BUCKET_DISPLAY_NAME,
+ ),
+ queryArgs,
+ null,
+ )?.use { cursor ->
+ while (cursor.moveToNext()) {
+ add(
+ SystemTrashFile(
+ localID = cursor.getLong(0),
+ deleteBy = cursor.getLong(1) * 1_000_000L,
+ deviceFolder = cursor.getString(2) ?: "Unknown Folder",
+ ),
+ )
+ }
+ }
+ }
+ }
+}
+
+data class SystemTrashFile(
+ val localID: Long,
+ val deleteBy: Long,
+ val deviceFolder: String,
+)
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/lib/src/system_trash.dart b/mobile/apps/photos/plugins/ente_photos_platform/lib/src/system_trash.dart
new file mode 100644
index 0000000000..ff25cd47b8
--- /dev/null
+++ b/mobile/apps/photos/plugins/ente_photos_platform/lib/src/system_trash.dart
@@ -0,0 +1,67 @@
+import 'package:flutter/foundation.dart';
+import 'package:flutter/services.dart';
+
+class SystemTrashFile {
+ const SystemTrashFile({
+ required this.localID,
+ required this.deleteBy,
+ required this.deviceFolder,
+ });
+
+ factory SystemTrashFile.fromMap(Map<dynamic, dynamic> map) {
+ return SystemTrashFile(
+ localID: _requiredInt(map['localID'], 'localID'),
+ deleteBy: _requiredInt(map['deleteBy'], 'deleteBy'),
+ deviceFolder: _requiredString(map['deviceFolder'], 'deviceFolder'),
+ );
+ }
+
+ final int localID;
+ final int deleteBy;
+ final String deviceFolder;
+}
+
+abstract interface class SystemTrashSource {
+ Future<List<SystemTrashFile>> getFiles();
+}
+
+class SystemTrashClient implements SystemTrashSource {
+ SystemTrashClient({MethodChannel? methodChannel, TargetPlatform? platform})
+ : _methodChannel =
+ methodChannel ?? const MethodChannel('io.ente.photos.platform'),
+ _platform = platform ?? defaultTargetPlatform;
+
+ static final instance = SystemTrashClient();
+
+ final MethodChannel _methodChannel;
+ final TargetPlatform _platform;
+
+ @override
+ Future<List<SystemTrashFile>> getFiles() async {
+ if (_platform != TargetPlatform.android) return const [];
+
+ final result =
+ await _methodChannel.invokeListMethod<Object?>(
+ 'systemTrash.getFiles',
+ ) ??
+ const [];
+ return result.map((file) {
+ if (file is! Map<dynamic, dynamic>) {
+ throw const FormatException('System trash file is not a map');
+ }
+ return SystemTrashFile.fromMap(file);
+ }).toList();
+ }
+}
+
+int _requiredInt(Object? value, String name) {
+ if (value is! int) throw FormatException('$name must be an integer');
+ return value;
+}
+
+String _requiredString(Object? value, String name) {
+ if (value is! String || value.isEmpty) {
+ throw FormatException('$name must be a non-empty string');
+ }
+ return value;
+}