66import static org .fossasia .phimpme .gallery .data .base .SortingMode .SIZE ;
77import static org .fossasia .phimpme .utilities .ActivitySwitchHelper .context ;
88
9+ import android .Manifest ;
910import android .animation .Animator ;
1011import android .annotation .TargetApi ;
1112import android .app .Dialog ;
6465import androidx .appcompat .widget .SwitchCompat ;
6566import androidx .appcompat .widget .Toolbar ;
6667import androidx .cardview .widget .CardView ;
68+ import androidx .core .app .ActivityCompat ;
6769import androidx .core .app .ActivityOptionsCompat ;
6870import androidx .core .content .ContextCompat ;
6971import androidx .core .view .GravityCompat ;
128130import org .fossasia .phimpme .gallery .util .ContentHelper ;
129131import org .fossasia .phimpme .gallery .util .CustomNestedView ;
130132import org .fossasia .phimpme .gallery .util .Measure ;
133+ import org .fossasia .phimpme .gallery .util .PermissionUtils ;
131134import org .fossasia .phimpme .gallery .util .PreferenceUtil ;
132135import org .fossasia .phimpme .gallery .util .StringUtils ;
133136import org .fossasia .phimpme .gallery .util .ThemeHelper ;
@@ -1219,33 +1222,15 @@ public final void onActivityResult(
12191222 // endregion
12201223
12211224 private void requestSdCardPermissions () {
1222- final AlertDialog .Builder dialogBuilder =
1223- new AlertDialog .Builder (LFMainActivity .this , getDialogStyle ());
1224-
1225- AlertDialogsHelper .getTextDialog (
1226- LFMainActivity .this ,
1227- dialogBuilder ,
1228- R .string .sd_card_write_permission_title ,
1229- R .string .sd_card_permissions_message ,
1230- null );
1231-
1232- dialogBuilder .setPositiveButton (
1233- getString (R .string .ok_action ).toUpperCase (),
1234- new DialogInterface .OnClickListener () {
1235- @ Override
1236- public void onClick (DialogInterface dialogInterface , int i ) {
1237- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP )
1238- startActivityForResult (
1239- new Intent (Intent .ACTION_OPEN_DOCUMENT_TREE ), REQUEST_CODE_SD_CARD_PERMISSIONS );
1240- }
1241- });
1242- dialogBuilder .setNegativeButton (getString (R .string .cancel ).toUpperCase (), null );
1243- AlertDialog alertDialog = dialogBuilder .create ();
1244- alertDialog .show ();
1245- AlertDialogsHelper .setButtonTextColor (
1246- new int [] {DialogInterface .BUTTON_POSITIVE , DialogInterface .BUTTON_NEGATIVE },
1247- getAccentColor (),
1248- alertDialog );
1225+ if (ActivityCompat .shouldShowRequestPermissionRationale (
1226+ this , Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
1227+ showPermissionAlertDialog ();
1228+ } else {
1229+ ActivityCompat .requestPermissions (
1230+ this ,
1231+ new String [] {Manifest .permission .WRITE_EXTERNAL_STORAGE },
1232+ REQUEST_CODE_SD_CARD_PERMISSIONS );
1233+ }
12491234 }
12501235
12511236 // region UI/GRAPHIC
@@ -2040,6 +2025,12 @@ public void onClick(DialogInterface dialog, int which) {
20402025
20412026 case R .id .delete_action :
20422027 getNavigationBar ();
2028+ if (!PermissionUtils .checkPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
2029+ requestSdCardPermissions ();
2030+ swipeRefreshLayout .setRefreshing (false );
2031+ invalidateOptionsMenu ();
2032+ return true ;
2033+ }
20432034 class DeletePhotos extends AsyncTask <String , Integer , Boolean > {
20442035
20452036 private boolean succ = false ;
@@ -2249,7 +2240,11 @@ mDrawerLayout, getResources().getString(R.string.single_image_removed))
22492240 new FavouritePhotos (activityContext ).execute ();
22502241 }
22512242 }
2252- } else requestSdCardPermissions ();
2243+ } else {
2244+ SnackBarHandler .create (
2245+ mDrawerLayout , getResources ().getString (R .string .photo_deletion_failed ))
2246+ .show ();
2247+ }
22532248
22542249 invalidateOptionsMenu ();
22552250 checkNothing ();
@@ -2737,6 +2732,12 @@ public void onClick(DialogInterface dialog, int id) {
27372732 // endregion
27382733
27392734 case R .id .action_move :
2735+ if (!PermissionUtils .checkPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
2736+ requestSdCardPermissions ();
2737+ swipeRefreshLayout .setRefreshing (false );
2738+ invalidateOptionsMenu ();
2739+ return true ;
2740+ }
27402741 final Snackbar [] snackbar = {null };
27412742 final ArrayList <Media > dr = getselecteditems ();
27422743 final String [] pathofalbum = {null };
@@ -2854,7 +2855,12 @@ public void onClick(DialogInterface dialog, int id) {
28542855 getAccentColor (),
28552856 alert );
28562857
2857- } else requestSdCardPermissions ();
2858+ } else {
2859+ SnackBarHandler .showWithBottomMargin (
2860+ mDrawerLayout ,
2861+ getString (R .string .photo_move_failed ),
2862+ navigationView .getHeight ());
2863+ }
28582864
28592865 swipeRefreshLayout .setRefreshing (false );
28602866 bottomSheetDialogFragment .dismiss ();
@@ -2905,9 +2911,10 @@ public void folderSelected(String path) {
29052911 mDrawerLayout .setDrawerLockMode (DrawerLayout .LOCK_MODE_UNLOCKED );
29062912 new PrepareAlbumTask (activityContext ).execute ();
29072913 } else {
2908- requestSdCardPermissions ();
2909- swipeRefreshLayout .setRefreshing (false );
2910- invalidateOptionsMenu ();
2914+ SnackBarHandler .showWithBottomMargin (
2915+ mDrawerLayout ,
2916+ getString (R .string .photo_move_failed ),
2917+ navigationView .getHeight ());
29112918 }
29122919 bottomSheetDialogFragment .dismiss ();
29132920 }
@@ -2930,6 +2937,12 @@ public void folderSelected(String path) {
29302937 return true ;
29312938
29322939 case R .id .action_copy :
2940+ if (!PermissionUtils .checkPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
2941+ requestSdCardPermissions ();
2942+ swipeRefreshLayout .setRefreshing (false );
2943+ invalidateOptionsMenu ();
2944+ return true ;
2945+ }
29332946 bottomSheetDialogFragment = new SelectAlbumBottomSheet ();
29342947 bottomSheetDialogFragment .setTitle (getString (R .string .copy_to ));
29352948 bottomSheetDialogFragment .setSelectAlbumInterface (
@@ -2945,6 +2958,12 @@ public void folderSelected(String path) {
29452958 return true ;
29462959
29472960 case R .id .renameAlbum :
2961+ if (!PermissionUtils .checkPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
2962+ requestSdCardPermissions ();
2963+ swipeRefreshLayout .setRefreshing (false );
2964+ invalidateOptionsMenu ();
2965+ return true ;
2966+ }
29482967 AlertDialog .Builder renameDialogBuilder =
29492968 new AlertDialog .Builder (LFMainActivity .this , getDialogStyle ());
29502969 final EditText editTextNewName = new EditText (getApplicationContext ());
@@ -3087,7 +3106,6 @@ public void onClick(View dialog) {
30873106 mDrawerLayout ,
30883107 getString (R .string .rename_error ),
30893108 navigationView .getHeight ());
3090- requestSdCardPermissions ();
30913109 }
30923110 swipeRefreshLayout .setRefreshing (false );
30933111 } else {
@@ -3157,7 +3175,6 @@ public boolean onKey(
31573175 mDrawerLayout ,
31583176 getString (R .string .rename_error ),
31593177 navigationView .getHeight ());
3160- requestSdCardPermissions ();
31613178 }
31623179 swipeRefreshLayout .setRefreshing (false );
31633180 } else {
@@ -4609,7 +4626,44 @@ public void onClick(View view) {
46094626 snackbar .show ();
46104627 }
46114628
4612- } else asyncActivityRef .requestSdCardPermissions ();
4629+ } else {
4630+ SnackBarHandler .showWithBottomMargin2 (
4631+ asyncActivityRef .mDrawerLayout ,
4632+ asyncActivityRef .getString (R .string .error_copying_files ),
4633+ asyncActivityRef .navigationView .getHeight (),
4634+ Snackbar .LENGTH_SHORT );
4635+ }
46134636 }
46144637 }
4638+
4639+ private void showPermissionAlertDialog () {
4640+ AlertDialog .Builder builder = new AlertDialog .Builder (this );
4641+ builder .setIcon (R .drawable .ic_caution );
4642+ builder .setTitle (R .string .permission_rationale_title );
4643+ builder .setMessage (R .string .permission_rationale_storage );
4644+ builder .setCancelable (false );
4645+ builder .setPositiveButton (
4646+ R .string .exit ,
4647+ new DialogInterface .OnClickListener () {
4648+ @ Override
4649+ public void onClick (DialogInterface dialog , int which ) {
4650+ finish ();
4651+ }
4652+ });
4653+
4654+ builder .setNegativeButton (
4655+ R .string .grant_permission ,
4656+ new DialogInterface .OnClickListener () {
4657+ @ Override
4658+ public void onClick (DialogInterface dialog , int which ) {
4659+ ActivityCompat .requestPermissions (
4660+ LFMainActivity .this ,
4661+ new String [] {Manifest .permission .WRITE_EXTERNAL_STORAGE },
4662+ REQUEST_CODE_SD_CARD_PERMISSIONS );
4663+ }
4664+ });
4665+
4666+ AlertDialog alertDialog = builder .create ();
4667+ alertDialog .show ();
4668+ }
46154669}
0 commit comments