@@ -308,18 +308,24 @@ protected void onCreate(Bundle savedInstanceState) {
308308 }
309309
310310 c = findViewById (R .id .writeStorage );
311- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
312- if (Environment .isExternalStorageManager ()) {
313- c .setChecked (true );
314- c .setEnabled (false );
315- } else {
316- c .setChecked (false );
317- c .setEnabled (true );
318- }
311+
312+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
313+ // Android 11+ (API 30 and above, including Android 13)
314+ if (Environment .isExternalStorageManager ()) {
315+ // If MANAGE_EXTERNAL_STORAGE permission is granted, disable the checkbox
316+ c .setChecked (true );
317+ c .setEnabled (false );
319318 } else {
320- c .setChecked (context .checkSelfPermission (Manifest .permission .WRITE_EXTERNAL_STORAGE ) == PackageManager .PERMISSION_GRANTED );
321- c .setEnabled (context .checkSelfPermission (Manifest .permission .WRITE_EXTERNAL_STORAGE ) != PackageManager .PERMISSION_GRANTED );
319+ // If not granted, enable checkbox to request permission
320+ c .setChecked (false );
321+ c .setEnabled (true );
322322 }
323+ } else {
324+ // Android 10 and below: Check WRITE_EXTERNAL_STORAGE permission
325+ boolean hasWritePermission = context .checkSelfPermission (Manifest .permission .WRITE_EXTERNAL_STORAGE ) == PackageManager .PERMISSION_GRANTED ;
326+ c .setChecked (hasWritePermission );
327+ c .setEnabled (!hasWritePermission );
328+ }
323329 String configFileContent = readConfigFileContents ();
324330
325331 if (configFileContent .isEmpty ()) {
@@ -512,20 +518,43 @@ public void checkOverlayPermission(View v){
512518 }
513519 }
514520
515- public void checkStoragePermission (View v ){
516- if (android .os .Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
517- if (! Environment .isExternalStorageManager ()) {
521+ public void checkStoragePermission (View v ) {
522+ if (android .os .Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
523+ // Android 13+ (API 33): Request permissions for media access
524+ if (!Environment .isExternalStorageManager ()) {
525+ // Request MANAGE_EXTERNAL_STORAGE permission if not granted
518526 Intent intent = new Intent (Settings .ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION );
519527 Uri uri = Uri .fromParts ("package" , getPackageName (), null );
520528 intent .setData (uri );
521- context .startActivity (intent );
529+ startActivityForResult (intent , 4710 ); // Start activity to manage permission
530+ } else {
531+ // If MANAGE_EXTERNAL_STORAGE is already granted, request media access permissions
532+ ActivityCompat .requestPermissions (SettingsActivity .this , new String []{
533+ Manifest .permission .READ_MEDIA_IMAGES ,
534+ Manifest .permission .READ_MEDIA_VIDEO
535+ }, 4710 );
536+ }
537+ } else if (android .os .Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
538+ // Android 11+ (API 30): Request MANAGE_EXTERNAL_STORAGE permission
539+ if (!Environment .isExternalStorageManager ()) {
540+ try {
541+ Intent intent = new Intent (Settings .ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION );
542+ Uri uri = Uri .fromParts ("package" , getPackageName (), null );
543+ intent .setData (uri );
544+ startActivityForResult (intent , 4710 ); // Start activity to manage permission
545+ } catch (Exception e ) {
546+ e .printStackTrace ();
547+ }
548+ } else {
549+ // If MANAGE_EXTERNAL_STORAGE is granted, no need for further permissions
522550 }
523551 } else {
524- ActivityCompat .requestPermissions (SettingsActivity .this ,new String []{
525- Manifest .permission .WRITE_EXTERNAL_STORAGE }, 4710 );
552+ // Android 10 and below: Request WRITE_EXTERNAL_STORAGE permission
553+ ActivityCompat .requestPermissions (SettingsActivity .this , new String []{
554+ Manifest .permission .WRITE_EXTERNAL_STORAGE
555+ }, 4710 );
526556 }
527557 }
528-
529558 public void checkWritePermission (View v ){
530559 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
531560 if (! Settings .System .canWrite (this ))
0 commit comments