Skip to content

armgit / ente

Public Imported from GitHub
0

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

Improve Android gallery review intents

laurenspriem7e1c466ef3b2
diff --git a/mobile/apps/photos/android/app/src/main/AndroidManifest.xml b/mobile/apps/photos/android/app/src/main/AndroidManifest.xml
index dd90f290b4..5bcd7a2087 100644
--- a/mobile/apps/photos/android/app/src/main/AndroidManifest.xml
+++ b/mobile/apps/photos/android/app/src/main/AndroidManifest.xml
@@ -123,7 +123,7 @@
</activity>
<activity android:name=".MediaReviewActivity"
- android:theme="@style/LaunchTheme"
+ android:theme="@android:style/Theme.NoDisplay"
android:exported="true"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
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 b80d9acff3..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
@@ -1,5 +1,11 @@
package io.ente.photos
+import android.content.Intent
import io.flutter.embedding.android.FlutterFragmentActivity
-class MainActivity : FlutterFragmentActivity() {}
+class MainActivity : FlutterFragmentActivity() {
+ override fun onNewIntent(intent: Intent) {
+ setIntent(intent)
+ super.onNewIntent(intent)
+ }
+}
diff --git a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/MediaReviewActivity.kt b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/MediaReviewActivity.kt
index 08e81cb490..68de1c5a17 100644
--- a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/MediaReviewActivity.kt
+++ b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/MediaReviewActivity.kt
@@ -1,45 +1,47 @@
package io.ente.photos
+import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.webkit.MimeTypeMap
-import io.flutter.embedding.android.FlutterFragmentActivity
import java.util.Locale
-class MediaReviewActivity : FlutterFragmentActivity() {
+class MediaReviewActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
- val shouldHandleIntent = normalizeReviewIntent(intent)
super.onCreate(savedInstanceState)
- if (!shouldHandleIntent) {
- finish()
- }
+ forwardIntent(intent)
+ finish()
}
override fun onNewIntent(intent: Intent) {
- val shouldHandleIntent = normalizeReviewIntent(intent)
super.onNewIntent(intent)
- if (shouldHandleIntent) {
- setIntent(intent)
- } else {
- finish()
- }
+ forwardIntent(intent)
+ finish()
}
- private fun normalizeReviewIntent(intent: Intent?): Boolean {
- val reviewIntent = intent ?: return false
+ private fun forwardIntent(intent: Intent?) {
+ val viewIntent = normalizedViewIntent(intent) ?: return
+ viewIntent.setClass(this, MainActivity::class.java)
+ viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
+ startActivity(viewIntent)
+ }
+
+ private fun normalizedViewIntent(intent: Intent?): Intent? {
+ val reviewIntent = intent ?: return null
if (reviewIntent.action !in reviewActions) {
- return true
+ return Intent(reviewIntent)
}
- val uri = reviewIntent.data ?: reviewIntent.streamUri ?: return false
- val type = reviewIntent.type ?: resolveMimeType(uri) ?: return false
+ val uri = reviewIntent.data ?: reviewIntent.streamUri ?: return null
+ val type = reviewIntent.type ?: resolveMimeType(uri) ?: return null
if (!type.isSupportedReviewMimeType()) {
- return false
+ return null
}
- reviewIntent.action = Intent.ACTION_VIEW
- reviewIntent.setDataAndType(uri, type)
- return true
+ return Intent(reviewIntent).apply {
+ action = Intent.ACTION_VIEW
+ setDataAndType(uri, type)
+ }
}
private fun resolveMimeType(uri: Uri): String? {
diff --git a/mobile/apps/photos/lib/app.dart b/mobile/apps/photos/lib/app.dart
index 7e93e5fc9d..6d71df536d 100644
--- a/mobile/apps/photos/lib/app.dart
+++ b/mobile/apps/photos/lib/app.dart
@@ -8,6 +8,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:home_widget/home_widget.dart' as hw;
import 'package:logging/logging.dart';
+import "package:media_extension/media_extension.dart";
import 'package:media_extension/media_extension_action_types.dart';
import "package:photos/core/event_bus.dart";
import 'package:photos/ente_theme_data.dart';
@@ -52,6 +53,7 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
final _logger = Logger("EnteAppState");
late StreamSubscription<PeopleChangedEvent> _peopleChangedSubscription;
late Debouncer _changeCallbackDebouncer;
+ StreamSubscription<MediaExtentionAction>? _intentActionSubscription;
StreamSubscription<Uri?>? _widgetClickedSubscription;
bool _didInitWidgetLaunchHandling = false;
late Future<Widget> _initialAndroidHome;
@@ -62,6 +64,19 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
super.initState();
locale = widget.locale;
_initialAndroidHome = _resolveInitialAndroidHome();
+ if (Platform.isAndroid) {
+ _intentActionSubscription = MediaExtension().intentActionStream.listen(
+ (mediaExtentionAction) =>
+ unawaited(_handleAndroidIntentAction(mediaExtentionAction)),
+ onError: (Object error, StackTrace stackTrace) {
+ _logger.warning(
+ "Failed to handle Android intent action",
+ error,
+ stackTrace,
+ );
+ },
+ );
+ }
WidgetsBinding.instance.addObserver(this);
setupSubscription();
}
@@ -116,8 +131,9 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
final mediaExtentionAction = Platform.isAndroid
? await initIntentAction()
: MediaExtentionAction(action: IntentAction.main);
- AppLifecycleService.instance.setMediaExtensionAction(mediaExtentionAction);
- if (mediaExtentionAction.action == IntentAction.main) {
+ final lifecycleAction = _appLifecycleActionFor(mediaExtentionAction);
+ AppLifecycleService.instance.setMediaExtensionAction(lifecycleAction);
+ if (lifecycleAction.action == IntentAction.main) {
unawaited(BgTaskUtils.configureWorkmanager());
}
if (_shouldOpenFileViewer(mediaExtentionAction)) {
@@ -132,6 +148,28 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
mediaExtentionAction.type == MediaType.video);
}
+ MediaExtentionAction _appLifecycleActionFor(
+ MediaExtentionAction mediaExtentionAction,
+ ) {
+ if (mediaExtentionAction.action == IntentAction.view &&
+ !_shouldOpenFileViewer(mediaExtentionAction)) {
+ return MediaExtentionAction(action: IntentAction.main);
+ }
+ return mediaExtentionAction;
+ }
+
+ Future<void> _handleAndroidIntentAction(
+ MediaExtentionAction mediaExtentionAction,
+ ) async {
+ AppLifecycleService.instance.setMediaExtensionAction(
+ _appLifecycleActionFor(mediaExtentionAction),
+ );
+ if (!_shouldOpenFileViewer(mediaExtentionAction)) {
+ return;
+ }
+ await AppNavigationService.instance.pushPage(const FileViewer());
+ }
+
Widget _buildInitialAndroidHome() {
return FutureBuilder<Widget>(
future: _initialAndroidHome,
@@ -211,6 +249,7 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
_memoriesChangedSubscription.cancel();
_peopleChangedSubscription.cancel();
_changeCallbackDebouncer.cancelDebounceTimer();
+ _intentActionSubscription?.cancel();
_widgetClickedSubscription?.cancel();
super.dispose();
}
diff --git a/mobile/apps/photos/lib/ui/viewer/actions/file_viewer.dart b/mobile/apps/photos/lib/ui/viewer/actions/file_viewer.dart
index 2b17d07a6c..7c1a8206db 100644
--- a/mobile/apps/photos/lib/ui/viewer/actions/file_viewer.dart
+++ b/mobile/apps/photos/lib/ui/viewer/actions/file_viewer.dart
@@ -1,3 +1,4 @@
+import 'dart:async';
import 'dart:convert';
import "dart:io";
@@ -6,6 +7,8 @@ import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:logging/logging.dart";
import "package:media_extension/media_extension_action_types.dart";
+import "package:photo_manager/photo_manager.dart";
+import "package:photo_manager_image_provider/photo_manager_image_provider.dart";
import "package:photo_view/photo_view.dart";
import "package:photos/services/app_lifecycle_service.dart";
import "package:photos/utils/exif_util.dart";
@@ -28,6 +31,11 @@ class FileViewerState extends State<FileViewer> {
VideoPlayerController? videoController;
final Logger _logger = Logger("FileViewer");
double? aspectRatio;
+ Future<AssetEntity?>? mediaStoreAssetFuture;
+ bool get _isExternalView =>
+ widget.sharedMediaFile == null &&
+ action.action == IntentAction.view &&
+ (action.type == MediaType.image || action.type == MediaType.video);
@override
void initState() {
@@ -36,6 +44,8 @@ class FileViewerState extends State<FileViewer> {
if (action.type == MediaType.video ||
widget.sharedMediaFile?.type == SharedMediaType.video) {
_initializeVideoController();
+ } else if (action.type == MediaType.image) {
+ mediaStoreAssetFuture = _loadMediaStoreAsset(action.data);
}
}
@@ -118,15 +128,90 @@ class FileViewerState extends State<FileViewer> {
}
}
+ Future<AssetEntity?>? _loadMediaStoreAsset(String? data) {
+ final uri = data == null ? null : Uri.tryParse(data);
+ final id = uri == null ? null : _mediaStoreAssetId(uri);
+ if (id == null) {
+ return null;
+ }
+ return AssetEntity.fromId(id);
+ }
+
+ String? _mediaStoreAssetId(Uri uri) {
+ if (uri.scheme != "content") {
+ return null;
+ }
+ if (uri.authority == "media") {
+ final mediaSegmentIndex = uri.pathSegments.lastIndexOf("media");
+ if (mediaSegmentIndex >= 0 &&
+ mediaSegmentIndex < uri.pathSegments.length - 1) {
+ return uri.pathSegments[mediaSegmentIndex + 1];
+ }
+ }
+ if (uri.authority == "com.android.providers.media.documents" &&
+ uri.pathSegments.isNotEmpty) {
+ final documentId = uri.pathSegments.last;
+ final id = documentId.split(":").last;
+ if (id.isNotEmpty) {
+ return id;
+ }
+ }
+ return null;
+ }
+
+ Widget _buildImageViewer() {
+ final sharedMediaPath = widget.sharedMediaFile?.path;
+ if (sharedMediaPath != null) {
+ return PhotoView(
+ imageProvider: Image.file(File(sharedMediaPath)).image,
+ );
+ }
+
+ final data = action.data;
+ if (data == null) {
+ _logger.severe("image data is null");
+ return const Icon(Icons.error);
+ }
+
+ final uri = Uri.tryParse(data);
+ if (uri?.scheme == "file") {
+ return PhotoView(
+ imageProvider: FileImage(File(uri!.toFilePath())),
+ );
+ }
+
+ final assetFuture = mediaStoreAssetFuture;
+ if (assetFuture != null) {
+ return FutureBuilder<AssetEntity?>(
+ future: assetFuture,
+ builder: (context, snapshot) {
+ final asset = snapshot.data;
+ if (asset == null) {
+ if (snapshot.connectionState == ConnectionState.done) {
+ _logger.severe("failed to resolve media store image $data");
+ return const Icon(Icons.error);
+ }
+ return const CircularProgressIndicator();
+ }
+ return PhotoView(
+ imageProvider: AssetEntityImageProvider(asset),
+ );
+ },
+ );
+ }
+
+ return PhotoView(
+ imageProvider: MemoryImage(base64Decode(data)),
+ );
+ }
+
@override
Widget build(BuildContext context) {
_logger.info("Building FileViewer");
- return Scaffold(
+ final scaffold = Scaffold(
appBar: AppBar(
leading: IconButton(
- onPressed: () {
- SystemChannels.platform.invokeMethod('SystemNavigator.pop');
- },
+ onPressed: _closeViewer,
icon: const Icon(Icons.arrow_back),
),
),
@@ -137,11 +222,7 @@ class FileViewerState extends State<FileViewer> {
child: (() {
if (action.type == MediaType.image ||
widget.sharedMediaFile?.type == SharedMediaType.image) {
- return PhotoView(
- imageProvider: widget.sharedMediaFile?.path != null
- ? Image.file(File(widget.sharedMediaFile!.path)).image
- : MemoryImage(base64Decode(action.data!)),
- );
+ return _buildImageViewer();
} else if (action.type == MediaType.video ||
widget.sharedMediaFile?.type == SharedMediaType.video) {
return controller != null
@@ -159,5 +240,21 @@ class FileViewerState extends State<FileViewer> {
],
),
);
+ if (!_isExternalView) {
+ return scaffold;
+ }
+ return PopScope(
+ canPop: false,
+ onPopInvokedWithResult: (didPop, _) {
+ if (!didPop) {
+ unawaited(_closeViewer());
+ }
+ },
+ child: scaffold,
+ );
+ }
+
+ Future<void> _closeViewer() async {
+ await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
}
}
diff --git a/mobile/apps/photos/pubspec.lock b/mobile/apps/photos/pubspec.lock
index 7579643b09..ea43f0ef03 100644
--- a/mobile/apps/photos/pubspec.lock
+++ b/mobile/apps/photos/pubspec.lock
@@ -1698,9 +1698,9 @@ packages:
dependency: "direct main"
description:
path: "."
- ref: "324f8fbdbc7ae6d326a6d541751b7a895a249089"
- resolved-ref: "324f8fbdbc7ae6d326a6d541751b7a895a249089"
- url: "https://github.com/ente-io/media_extension.git"
+ ref: "78772f7b8de59c3e8a942fec15d5510dc2e5ab98"
+ resolved-ref: "78772f7b8de59c3e8a942fec15d5510dc2e5ab98"
+ url: "https://github.com/laurens-pilot/media_extension.git"
source: git
version: "1.0.2"
media_kit:
@@ -2137,7 +2137,7 @@ packages:
source: hosted
version: "3.7.1"
photo_manager_image_provider:
- dependency: transitive
+ dependency: "direct main"
description:
name: photo_manager_image_provider
sha256: b6015b67b32f345f57cf32c126f871bced2501236c405aafaefa885f7c821e4f
diff --git a/mobile/apps/photos/pubspec.yaml b/mobile/apps/photos/pubspec.yaml
index 38ebe611a9..826e275529 100644
--- a/mobile/apps/photos/pubspec.yaml
+++ b/mobile/apps/photos/pubspec.yaml
@@ -148,8 +148,8 @@ dependencies:
maps_launcher: 3.0.0+1
media_extension:
git:
- url: "https://github.com/ente-io/media_extension.git"
- ref: 324f8fbdbc7ae6d326a6d541751b7a895a249089
+ url: "https://github.com/laurens-pilot/media_extension.git"
+ ref: 78772f7b8de59c3e8a942fec15d5510dc2e5ab98
media_kit:
git:
url: https://github.com/media-kit/media-kit
@@ -210,6 +210,7 @@ dependencies:
path_provider_foundation: 2.4.1
permission_handler: 12.0.1
photo_manager: 3.7.1
+ photo_manager_image_provider: 2.2.0
photo_view: 0.15.0
pinput: 5.0.2
pointycastle: 3.9.1