From 128c7de3776d4891a8fe4ce9c07ae3e880ef0e64 Mon Sep 17 00:00:00 2001 From: Sebastiano Barezzi Date: Tue, 8 Aug 2023 04:23:33 +0200 Subject: [PATCH] Glimpse: Add MANAGE_MEDIA permission Change-Id: I6de0dcff96884d3bbf8e75d32e10a3dc04778a67 --- app/Android.bp | 1 + app/build.gradle.kts | 1 + app/src/main/AndroidManifest.xml | 4 ++ .../glimpse/ext/SharedPreferences.kt | 22 +++++++++ .../glimpse/fragments/ReelsFragment.kt | 2 + .../glimpse/utils/PermissionsUtils.kt | 46 +++++++++++++++++++ app/src/main/res/values/strings.xml | 4 ++ 7 files changed, 80 insertions(+) create mode 100644 app/src/main/java/org/lineageos/glimpse/ext/SharedPreferences.kt diff --git a/app/Android.bp b/app/Android.bp index a7af632..803c18f 100644 --- a/app/Android.bp +++ b/app/Android.bp @@ -19,6 +19,7 @@ android_app { "androidx.core_core-ktx", "androidx.appcompat_appcompat", "androidx-constraintlayout_constraintlayout", + "androidx.preference_preference", "Glimpse_com.google.android.material_material", "Glimpse_androidx.media3_media3-exoplayer", "Glimpse_androidx.media3_media3-ui", diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 9b6d6d0..f43492c 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -62,6 +62,7 @@ dependencies { implementation("androidx.core:core-ktx:1.10.1") implementation("androidx.appcompat:appcompat:1.6.1") implementation("androidx.constraintlayout:constraintlayout:2.1.4") + implementation("androidx.preference:preference:1.2.1") implementation("com.google.android.material:material:1.9.0") // Media3 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 167f98f..6584301 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,9 +4,13 @@ SPDX-License-Identifier: Apache-2.0 --> + diff --git a/app/src/main/java/org/lineageos/glimpse/ext/SharedPreferences.kt b/app/src/main/java/org/lineageos/glimpse/ext/SharedPreferences.kt new file mode 100644 index 0000000..da55503 --- /dev/null +++ b/app/src/main/java/org/lineageos/glimpse/ext/SharedPreferences.kt @@ -0,0 +1,22 @@ +/* + * SPDX-FileCopyrightText: 2023 The LineageOS Project + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.lineageos.glimpse.ext + +import android.content.SharedPreferences +import androidx.core.content.edit + +// All files access dialog dismissed +private const val MANAGE_MEDIA_PERMISSION_DIALOG_DISMISSED_KEY = + "manage_media_permission_dialog_dismissed" +private const val MANAGE_MEDIA_PERMISSION_DIALOG_DISMISSED_DEFAULT = false +var SharedPreferences.manageMediaPermissionDialogDismissed: Boolean + get() = getBoolean( + MANAGE_MEDIA_PERMISSION_DIALOG_DISMISSED_KEY, + MANAGE_MEDIA_PERMISSION_DIALOG_DISMISSED_DEFAULT + ) + set(value) = edit { + putBoolean(MANAGE_MEDIA_PERMISSION_DIALOG_DISMISSED_KEY, value) + } diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/ReelsFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/ReelsFragment.kt index e719535..a7057e7 100644 --- a/app/src/main/java/org/lineageos/glimpse/fragments/ReelsFragment.kt +++ b/app/src/main/java/org/lineageos/glimpse/fragments/ReelsFragment.kt @@ -60,6 +60,7 @@ class ReelsFragment : Fragment(R.layout.fragment_reels), LoaderManager.LoaderCal requireActivity().finish() } else { initCursorLoader() + permissionsUtils.showManageMediaPermissionDialogIfNeeded() } } } @@ -101,6 +102,7 @@ class ReelsFragment : Fragment(R.layout.fragment_reels), LoaderManager.LoaderCal mainPermissionsRequestLauncher.launch(PermissionsUtils.mainPermissions) } else { initCursorLoader() + permissionsUtils.showManageMediaPermissionDialogIfNeeded() } } diff --git a/app/src/main/java/org/lineageos/glimpse/utils/PermissionsUtils.kt b/app/src/main/java/org/lineageos/glimpse/utils/PermissionsUtils.kt index ed434fe..761c598 100644 --- a/app/src/main/java/org/lineageos/glimpse/utils/PermissionsUtils.kt +++ b/app/src/main/java/org/lineageos/glimpse/utils/PermissionsUtils.kt @@ -7,16 +7,47 @@ package org.lineageos.glimpse.utils import android.Manifest import android.content.Context +import android.content.Intent import android.content.pm.PackageManager +import android.net.Uri import android.os.Build +import android.provider.MediaStore +import android.provider.Settings +import androidx.annotation.RequiresApi import androidx.core.content.ContextCompat +import androidx.preference.PreferenceManager +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import org.lineageos.glimpse.R +import org.lineageos.glimpse.ext.* /** * App's permissions utils. */ class PermissionsUtils(private val context: Context) { + private val sharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(context) } + fun mainPermissionsGranted() = permissionsGranted(mainPermissions) + fun showManageMediaPermissionDialogIfNeeded() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S + || canManageMedia() + || sharedPreferences.manageMediaPermissionDialogDismissed + ) { + return + } + + MaterialAlertDialogBuilder(context) + .setTitle(R.string.manage_media_permission_title) + .setMessage(R.string.manage_media_permission_message) + .setPositiveButton(android.R.string.ok) { _, _ -> + requestManageMediaPermission() + } + .setNeutralButton(android.R.string.cancel) { _, _ -> + sharedPreferences.manageMediaPermissionDialogDismissed = true + } + .show() + } + private fun permissionGranted(permission: String) = ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED @@ -24,6 +55,21 @@ class PermissionsUtils(private val context: Context) { permissionGranted(it) } + private fun canManageMedia() = + Build.VERSION.SDK_INT < Build.VERSION_CODES.S || MediaStore.canManageMedia(context) + + @RequiresApi(Build.VERSION_CODES.S) + private fun requestManageMediaPermission() { + if (canManageMedia()) { + return + } + + val intent = Intent(Settings.ACTION_REQUEST_MANAGE_MEDIA).apply { + data = Uri.fromParts("package", context.packageName, null) + } + context.startActivity(intent) + } + companion object { /** * Permissions required to run the app diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f8622b7..88181f2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -57,4 +57,8 @@ File couldn\'t be restored from trash %d files couldn\'t be restored from trash + + + Manage media permission + To better manage your images and videos, allow the app to manage your media files