Glimpse: Add MANAGE_MEDIA permission

Change-Id: I6de0dcff96884d3bbf8e75d32e10a3dc04778a67
This commit is contained in:
Sebastiano Barezzi 2023-08-08 04:23:33 +02:00
parent e62b2090ca
commit 128c7de377
No known key found for this signature in database
GPG Key ID: 763BD3AE91A7A13F
7 changed files with 80 additions and 0 deletions

View File

@ -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",

View File

@ -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

View File

@ -4,9 +4,13 @@
SPDX-License-Identifier: Apache-2.0
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.lineageos.glimpse">
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission
android:name="android.permission.MANAGE_MEDIA"
tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />

View File

@ -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)
}

View File

@ -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()
}
}

View File

@ -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

View File

@ -57,4 +57,8 @@
<item quantity="one">File couldn\'t be restored from trash</item>
<item quantity="many">%d files couldn\'t be restored from trash</item>
</plurals>
<!-- Manage media permission -->
<string name="manage_media_permission_title">Manage media permission</string>
<string name="manage_media_permission_message">To better manage your images and videos, allow the app to manage your media files</string>
</resources>