@@ -266,38 +266,75 @@ public static void selectDocument(Activity activity, int requestCode) {
266
266
}
267
267
268
268
public static void selectGallery (Activity activity , int requestCode , @ NonNull Address recipient , @ NonNull String body ) {
269
-
270
269
Context c = activity .getApplicationContext ();
270
+ Runnable openGallery = () ->
271
+ activity .startActivityForResult (
272
+ MediaSendActivity .buildGalleryIntent (activity , recipient , body ),
273
+ requestCode
274
+ );
275
+
276
+ // Android 14+ : if we already have partial OR full access, skip asking
277
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) {
278
+ if (Permissions .hasAll (activity , Manifest .permission .READ_MEDIA_IMAGES ) ||
279
+ Permissions .hasAll (activity , Manifest .permission .READ_MEDIA_VISUAL_USER_SELECTED )) {
280
+ openGallery .run ();
281
+ return ;
282
+ }
271
283
272
- Permissions .PermissionsBuilder builder = Permissions .with (activity );
273
-
274
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) { // API 34+
275
- builder = builder .request (Manifest .permission .READ_MEDIA_VIDEO ,
276
- Manifest .permission .READ_MEDIA_IMAGES ,
277
- Manifest .permission .READ_MEDIA_VISUAL_USER_SELECTED )
284
+ Permissions .with (activity )
285
+ .request (
286
+ Manifest .permission .READ_MEDIA_IMAGES ,
287
+ Manifest .permission .READ_MEDIA_VIDEO ,
288
+ Manifest .permission .READ_MEDIA_VISUAL_USER_SELECTED
289
+ )
290
+ .onAllGranted (openGallery ) // full access granted
291
+ .onSomeGranted (granted -> { // treat partial access as success
292
+ if (granted .contains (Manifest .permission .READ_MEDIA_VISUAL_USER_SELECTED )) {
293
+ openGallery .run ();
294
+ }
295
+ })
278
296
.withPermanentDenialDialog (
279
297
Phrase .from (c , R .string .permissionsStorageDenied )
280
- .put (APP_NAME_KEY , c .getString (R .string .app_name ))
298
+ .put (APP_NAME_KEY , activity .getString (R .string .app_name ))
281
299
.format ().toString ()
282
- );
283
- } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) { // API 33
284
- builder = builder .request (Manifest .permission .READ_MEDIA_VIDEO ,
285
- Manifest .permission .READ_MEDIA_IMAGES )
300
+ )
301
+ .execute ();
302
+ return ;
303
+ }
304
+
305
+ // Android 13
306
+ if (Build .VERSION .SDK_INT == Build .VERSION_CODES .TIRAMISU ) {
307
+ if (Permissions .hasAll (activity ,
308
+ Manifest .permission .READ_MEDIA_IMAGES , Manifest .permission .READ_MEDIA_VIDEO )) {
309
+ openGallery .run ();
310
+ return ;
311
+ }
312
+ Permissions .with (activity )
313
+ .request (Manifest .permission .READ_MEDIA_IMAGES , Manifest .permission .READ_MEDIA_VIDEO )
314
+ .onAllGranted (openGallery )
286
315
.withPermanentDenialDialog (
287
316
Phrase .from (c , R .string .permissionsStorageDenied )
288
- .put (APP_NAME_KEY , c .getString (R .string .app_name ))
289
- .format ().toString ()
290
- );
291
- } else {
292
- builder = builder .request (Manifest .permission .READ_EXTERNAL_STORAGE )
293
- .withPermanentDenialDialog (
294
- Phrase .from (c , R .string .permissionsStorageDeniedLegacy )
295
- .put (APP_NAME_KEY , c .getString (R .string .app_name ))
317
+ .put (APP_NAME_KEY , activity .getString (R .string .app_name ))
296
318
.format ().toString ()
297
- );
319
+ )
320
+ .execute ();
321
+ return ;
298
322
}
299
323
300
- builder .onAllGranted (() -> activity .startActivityForResult (MediaSendActivity .buildGalleryIntent (activity , recipient , body ), requestCode ))
324
+ // Android 12 and below
325
+ if (Permissions .hasAll (activity , Manifest .permission .READ_EXTERNAL_STORAGE )) {
326
+ openGallery .run ();
327
+ return ;
328
+ }
329
+
330
+ Permissions .with (activity )
331
+ .request (Manifest .permission .READ_EXTERNAL_STORAGE )
332
+ .onAllGranted (openGallery )
333
+ .withPermanentDenialDialog (
334
+ Phrase .from (c , R .string .permissionsStorageDeniedLegacy )
335
+ .put (APP_NAME_KEY , activity .getString (R .string .app_name ))
336
+ .format ().toString ()
337
+ )
301
338
.execute ();
302
339
}
303
340
0 commit comments