Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile][photos] add hint to open media management settings if annoyed

Priyanshu Dangare09fdf4b00076
diff --git a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/NativeChannels.kt b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/NativeChannels.kt
index 2e34013968..3c3af41f38 100644
--- a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/NativeChannels.kt
+++ b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/NativeChannels.kt
@@ -3,6 +3,9 @@ package io.ente.photos
import android.os.Build
import android.provider.MediaStore
import android.content.Context
+import android.content.Intent
+import android.net.Uri
+import android.provider.Settings
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
@@ -49,14 +52,33 @@ private class MediaStoreChannel(private val context: Context) {
private fun handle(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
+ "isMediaManagementSupported" -> result.success(isMediaManagementSupported())
"canManageMedia" -> result.success(canManageMedia())
+ "openManageMediaSettings" -> {
+ openManageMediaSettings()
+ result.success(null)
+ }
else -> result.notImplemented()
}
}
+ private fun isMediaManagementSupported(): Boolean {
+ return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
+ }
+
private fun canManageMedia(): Boolean {
- return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
- MediaStore.canManageMedia(context)
+ return isMediaManagementSupported() && MediaStore.canManageMedia(context)
+ }
+
+ private fun openManageMediaSettings() {
+ if (!isMediaManagementSupported()) {
+ return
+ }
+ val intent = Intent(Settings.ACTION_REQUEST_MANAGE_MEDIA).apply {
+ data = Uri.parse("package:${context.packageName}")
+ addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ }
+ context.startActivity(intent)
}
private companion object {
diff --git a/mobile/apps/photos/lib/services/media_store_service.dart b/mobile/apps/photos/lib/services/media_store_service.dart
index fda28a5f63..8f31803c28 100644
--- a/mobile/apps/photos/lib/services/media_store_service.dart
+++ b/mobile/apps/photos/lib/services/media_store_service.dart
@@ -4,6 +4,17 @@ import "package:flutter/services.dart";
class MediaStoreService {
static const _methodChannel = MethodChannel("io.ente.photos/media_store");
+ /// Returns whether Android media management settings are available.
+ static Future<bool> isMediaManagementSupported() async {
+ final result = await _methodChannel.invokeMethod<bool>(
+ "isMediaManagementSupported",
+ );
+ if (result == null) {
+ throw AssertionError("isMediaManagementSupported returned null");
+ }
+ return result;
+ }
+
/// Returns whether Ente can manage shared media without user confirmation.
static Future<bool> canManageMedia() async {
final result = await _methodChannel.invokeMethod<bool>("canManageMedia");
@@ -12,4 +23,9 @@ class MediaStoreService {
}
return result;
}
+
+ /// Opens Android settings for granting media management access.
+ static Future<void> openManageMediaSettings() async {
+ await _methodChannel.invokeMethod<void>("openManageMediaSettings");
+ }
}
diff --git a/mobile/apps/photos/lib/settings/local_settings.dart b/mobile/apps/photos/lib/settings/local_settings.dart
index 7eb526c663..5997846151 100644
--- a/mobile/apps/photos/lib/settings/local_settings.dart
+++ b/mobile/apps/photos/lib/settings/local_settings.dart
@@ -108,6 +108,12 @@ class LocalSettings {
static const _kAppMode = "ls.app_mode";
static const _kShowLocalGalleryModeOption = "ls.show_offline_mode_option";
static const _kDeletePreference = "delete_preference";
+ static const _kMediaManagementHintDeleteAttempts =
+ "media_management_hint_delete_attempts";
+ static const _kMediaManagementHintDismissedAt =
+ "media_management_hint_dismissed_at";
+ static const _kMediaManagementHintDismissDuration = Duration(days: 7);
+ static const _kMediaManagementHintDeleteAttemptThreshold = 3;
static const _kWidgetHideTextFlags = "ls.widget_hide_text_flags";
@@ -623,6 +629,40 @@ class LocalSettings {
Future<void> setLocalGallerySettingsBannerDismissed(bool value) =>
_setFlag(LocalGalleryFlag.localGallerySettingsBannerDismissed, value);
+ int get _mediaManagementHintDeleteAttempts =>
+ _prefs.getInt(_kMediaManagementHintDeleteAttempts) ?? 0;
+
+ bool hasMediaManagementHintDeleteAttemptsReached() {
+ return _mediaManagementHintDeleteAttempts >=
+ _kMediaManagementHintDeleteAttemptThreshold;
+ }
+
+ bool get isMediaManagementHintDismissed {
+ final dismissedAtMs = _prefs.getInt(_kMediaManagementHintDismissedAt) ?? 0;
+ if (dismissedAtMs == 0) return false;
+ final elapsed = DateTime.now().millisecondsSinceEpoch - dismissedAtMs;
+ return elapsed >= 0 &&
+ elapsed < _kMediaManagementHintDismissDuration.inMilliseconds;
+ }
+
+ Future<void> incrementMediaManagementHintDeleteAttempts() async {
+ await _prefs.setInt(
+ _kMediaManagementHintDeleteAttempts,
+ _mediaManagementHintDeleteAttempts + 1,
+ );
+ }
+
+ Future<void> resetMediaManagementHintDeleteAttempts() async {
+ await _prefs.setInt(_kMediaManagementHintDeleteAttempts, 0);
+ }
+
+ Future<void> setMediaManagementHintDismissed() async {
+ await _prefs.setInt(
+ _kMediaManagementHintDismissedAt,
+ DateTime.now().millisecondsSinceEpoch,
+ );
+ }
+
DeletePreference? getDeletePreference() {
if (!_prefs.containsKey(_kDeletePreference)) {
return null;
diff --git a/mobile/apps/photos/lib/ui/actions/file/file_actions.dart b/mobile/apps/photos/lib/ui/actions/file/file_actions.dart
index 0b731fc47d..b128c8f75f 100644
--- a/mobile/apps/photos/lib/ui/actions/file/file_actions.dart
+++ b/mobile/apps/photos/lib/ui/actions/file/file_actions.dart
@@ -101,7 +101,9 @@ Future<void> showSingleFileDeleteSheet(
},
),
);
-
+ if (isLocal) {
+ await showMediaManagementHintSheet(context);
+ }
if (actionResult?.action != null &&
actionResult!.action == ButtonAction.error) {
await showGenericErrorDialog(
diff --git a/mobile/apps/photos/lib/utils/delete_file_util.dart b/mobile/apps/photos/lib/utils/delete_file_util.dart
index 8cff84c2f5..5095cad3da 100644
--- a/mobile/apps/photos/lib/utils/delete_file_util.dart
+++ b/mobile/apps/photos/lib/utils/delete_file_util.dart
@@ -780,6 +780,62 @@ Future<bool> shouldProceedWithDeletion(BuildContext context) async {
}
}
+Future<void> showMediaManagementHintSheet(BuildContext context) async {
+ final l10n = AppLocalizations.of(context);
+ if (!Platform.isAndroid) {
+ return;
+ }
+ if (!await MediaStoreService.isMediaManagementSupported()) {
+ return;
+ }
+ if (await MediaStoreService.canManageMedia()) {
+ return;
+ }
+ if (localSettings.isMediaManagementHintDismissed) {
+ return;
+ }
+ await localSettings.incrementMediaManagementHintDeleteAttempts();
+ if (!localSettings.hasMediaManagementHintDeleteAttemptsReached()) {
+ return;
+ }
+ final shouldDismissHint = await showBottomSheetComponent<bool>(
+ context: context,
+ useRootNavigator: Platform.isIOS,
+ builder: (sheetContext) => BottomSheetComponent(
+ title: "Tired of confirming every delete?",
+ message:
+ "Turn on media management for Ente in settings, and the prompts stop.",
+ illustration: Image.asset("assets/ducky_smart_feature.png"),
+ closeTooltip: l10n.close,
+ closeResult: true,
+ actions: [
+ ButtonComponent(
+ label: l10n.openSettings,
+ shouldSurfaceExecutionStates: false,
+ onTap: () async {
+ await MediaStoreService.openManageMediaSettings();
+ if (sheetContext.mounted) {
+ Navigator.of(sheetContext).pop(false);
+ }
+ },
+ ),
+ ButtonComponent(
+ label: l10n.skip,
+ variant: ButtonComponentVariant.secondary,
+ shouldSurfaceExecutionStates: false,
+ onTap: () {
+ Navigator.of(sheetContext).pop(true);
+ },
+ ),
+ ],
+ ),
+ );
+ if (shouldDismissHint == true) {
+ await localSettings.resetMediaManagementHintDeleteAttempts();
+ await localSettings.setMediaManagementHintDismissed();
+ }
+}
+
Future<void> showDeleteSheet(
BuildContext context,
SelectedFiles selectedFiles,
@@ -826,6 +882,7 @@ Future<void> showDeleteSheet(
showShortToast(context, l10n.noDeviceThatCanBeDeleted);
return;
}
+ ButtonResult? actionResult;
if (Platform.isAndroid && await MediaStoreService.canManageMedia()) {
final hasVideos = localGalleryDeletableFiles.any(
(file) => file.fileType == FileType.video,
@@ -833,7 +890,7 @@ Future<void> showDeleteSheet(
final hasPhotos = localGalleryDeletableFiles.any(
(file) => file.fileType != FileType.video,
);
- await showBottomSheetComponent<ButtonResult>(
+ actionResult = await showBottomSheetComponent<ButtonResult>(
context: context,
useRootNavigator: Platform.isIOS,
builder: (_) => DeleteConfirmationSheet(
@@ -856,7 +913,15 @@ Future<void> showDeleteSheet(
} else {
await deleteOnDeviceOnlyAction(context, localGalleryDeletableFiles);
}
+ if (actionResult?.action == ButtonAction.error) {
+ await showGenericErrorDialog(
+ context: context,
+ error: actionResult!.exception,
+ );
+ return;
+ }
selectedFiles.unSelectAll(localGalleryDeletableFiles.toSet());
+ await showMediaManagementHintSheet(context);
return;
}
final hasRemoteFiles = deletableFiles.any((f) => f.isUploaded);
@@ -906,6 +971,9 @@ Future<void> showDeleteSheet(
},
),
);
+ if (hasLocalFiles) {
+ await showMediaManagementHintSheet(context);
+ }
if (actionResult?.action != null &&
actionResult!.action == ButtonAction.error) {
await showGenericErrorDialog(