@@ -99,7 +99,6 @@ object PermissionUtil {
99
99
ActivityCompat .checkSelfPermission(context, it) == PackageManager .PERMISSION_GRANTED
100
100
}
101
101
102
- // TODO Rename the function to requestExternalStoragePermissionIfNeeded to avoid confusion.
103
102
/* *
104
103
* Request relevant external storage permission depending on SDK, if needed.
105
104
*
@@ -133,6 +132,10 @@ object PermissionUtil {
133
132
}
134
133
}
135
134
135
+ /* *
136
+ * Request a storage permission
137
+ */
138
+ // TODO inject this class to avoid passing ViewThemeUtils around
136
139
private fun requestStoragePermission (
137
140
activity : Activity ,
138
141
readOnly : Boolean ,
@@ -142,8 +145,10 @@ object PermissionUtil {
142
145
val preferences: AppPreferences = AppPreferencesImpl .fromContext(activity)
143
146
144
147
if (permissionRequired || ! preferences.isStoragePermissionRequested) {
148
+ // determine required permissions
145
149
val permissions = if (readOnly && Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
146
150
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
151
+ // use granular media permissions
147
152
arrayOf(
148
153
Manifest .permission.READ_MEDIA_IMAGES ,
149
154
Manifest .permission.READ_MEDIA_VIDEO
@@ -155,39 +160,29 @@ object PermissionUtil {
155
160
arrayOf(Manifest .permission.WRITE_EXTERNAL_STORAGE )
156
161
}
157
162
158
- val grantedPermissions = permissions.all {
159
- ContextCompat .checkSelfPermission(activity, it) == PackageManager .PERMISSION_GRANTED
160
- }
161
-
162
- if (grantedPermissions) {
163
- // Permissions already granted
164
- return
165
- }
166
-
167
- val permanentlyDeniedPermissions = permissions.filter {
168
- ! ActivityCompat .shouldShowRequestPermissionRationale(activity, it) &&
169
- ContextCompat .checkSelfPermission(activity, it) != PackageManager .PERMISSION_GRANTED
163
+ fun doRequest () {
164
+ ActivityCompat .requestPermissions(
165
+ activity,
166
+ permissions,
167
+ PERMISSIONS_EXTERNAL_STORAGE
168
+ )
169
+ preferences.isStoragePermissionRequested = true
170
170
}
171
171
172
- if (permissions.any { ActivityCompat .shouldShowRequestPermissionRationale(activity, it) } ||
173
- permanentlyDeniedPermissions.isNotEmpty()
174
- ) {
172
+ // Check if we should show an explanation
173
+ if (permissions.any { shouldShowRequestPermissionRationale(activity, it) }) {
174
+ // Show explanation to the user and then request permission
175
175
Snackbar .make(
176
176
activity.findViewById(android.R .id.content),
177
177
R .string.permission_storage_access,
178
- Snackbar .LENGTH_LONG
179
- ).setAction(R .string.common_settings) {
180
- val intent = Intent (Settings .ACTION_APPLICATION_DETAILS_SETTINGS ).apply {
181
- data = Uri .fromParts(" package" , activity.packageName, null )
182
- }
183
- activity.startActivity(intent)
178
+ Snackbar .LENGTH_INDEFINITE
179
+ ).setAction(R .string.common_ok) {
180
+ doRequest()
184
181
}.also { viewThemeUtils.material.themeSnackbar(it) }.show()
185
182
} else {
186
- ActivityCompat .requestPermissions(activity, permissions, PERMISSIONS_EXTERNAL_STORAGE )
183
+ // No explanation needed, request the permission.
184
+ doRequest()
187
185
}
188
-
189
- // Only mark as requested after actual request
190
- preferences.isStoragePermissionRequested = true
191
186
}
192
187
}
193
188
@@ -256,10 +251,16 @@ object PermissionUtil {
256
251
activity,
257
252
listener
258
253
)
259
- }
260
254
261
- val dialogFragment = StoragePermissionDialogFragment .newInstance(permissionRequired)
262
- dialogFragment.show(activity.supportFragmentManager, PERMISSION_CHOICE_DIALOG_TAG )
255
+ // Check if the dialog is already added to the FragmentManager.
256
+ val existingDialog = activity.supportFragmentManager.findFragmentByTag(PERMISSION_CHOICE_DIALOG_TAG )
257
+
258
+ // Only show the dialog if it's not already shown.
259
+ if (existingDialog == null ) {
260
+ val dialogFragment = StoragePermissionDialogFragment .newInstance(permissionRequired)
261
+ dialogFragment.show(activity.supportFragmentManager, PERMISSION_CHOICE_DIALOG_TAG )
262
+ }
263
+ }
263
264
}
264
265
}
265
266
0 commit comments