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 ;
128129import org .fossasia .phimpme .gallery .util .ContentHelper ;
129130import org .fossasia .phimpme .gallery .util .CustomNestedView ;
130131import org .fossasia .phimpme .gallery .util .Measure ;
132+ import org .fossasia .phimpme .gallery .util .PermissionUtils ;
131133import org .fossasia .phimpme .gallery .util .PreferenceUtil ;
132134import org .fossasia .phimpme .gallery .util .StringUtils ;
133135import org .fossasia .phimpme .gallery .util .ThemeHelper ;
@@ -1219,33 +1221,13 @@ public final void onActivityResult(
12191221 // endregion
12201222
12211223 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 );
1224+ if (PermissionUtils .shouldShowPermissionRationale (
1225+ this , Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
1226+ showPermissionAlertDialog ();
1227+ } else {
1228+ PermissionUtils .requestPermissions (
1229+ this , REQUEST_CODE_SD_CARD_PERMISSIONS , Manifest .permission .WRITE_EXTERNAL_STORAGE );
1230+ }
12491231 }
12501232
12511233 // region UI/GRAPHIC
@@ -2040,6 +2022,12 @@ public void onClick(DialogInterface dialog, int which) {
20402022
20412023 case R .id .delete_action :
20422024 getNavigationBar ();
2025+ if (!PermissionUtils .checkPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
2026+ requestSdCardPermissions ();
2027+ swipeRefreshLayout .setRefreshing (false );
2028+ invalidateOptionsMenu ();
2029+ return true ;
2030+ }
20432031 class DeletePhotos extends AsyncTask <String , Integer , Boolean > {
20442032
20452033 private boolean succ = false ;
@@ -2249,7 +2237,11 @@ mDrawerLayout, getResources().getString(R.string.single_image_removed))
22492237 new FavouritePhotos (activityContext ).execute ();
22502238 }
22512239 }
2252- } else requestSdCardPermissions ();
2240+ } else {
2241+ SnackBarHandler .create (
2242+ mDrawerLayout , getResources ().getString (R .string .photo_deletion_failed ))
2243+ .show ();
2244+ }
22532245
22542246 invalidateOptionsMenu ();
22552247 checkNothing ();
@@ -2737,6 +2729,12 @@ public void onClick(DialogInterface dialog, int id) {
27372729 // endregion
27382730
27392731 case R .id .action_move :
2732+ if (!PermissionUtils .checkPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
2733+ requestSdCardPermissions ();
2734+ swipeRefreshLayout .setRefreshing (false );
2735+ invalidateOptionsMenu ();
2736+ return true ;
2737+ }
27402738 final Snackbar [] snackbar = {null };
27412739 final ArrayList <Media > dr = getselecteditems ();
27422740 final String [] pathofalbum = {null };
@@ -2854,7 +2852,12 @@ public void onClick(DialogInterface dialog, int id) {
28542852 getAccentColor (),
28552853 alert );
28562854
2857- } else requestSdCardPermissions ();
2855+ } else {
2856+ SnackBarHandler .showWithBottomMargin (
2857+ mDrawerLayout ,
2858+ getString (R .string .photo_move_failed ),
2859+ navigationView .getHeight ());
2860+ }
28582861
28592862 swipeRefreshLayout .setRefreshing (false );
28602863 bottomSheetDialogFragment .dismiss ();
@@ -2905,9 +2908,10 @@ public void folderSelected(String path) {
29052908 mDrawerLayout .setDrawerLockMode (DrawerLayout .LOCK_MODE_UNLOCKED );
29062909 new PrepareAlbumTask (activityContext ).execute ();
29072910 } else {
2908- requestSdCardPermissions ();
2909- swipeRefreshLayout .setRefreshing (false );
2910- invalidateOptionsMenu ();
2911+ SnackBarHandler .showWithBottomMargin (
2912+ mDrawerLayout ,
2913+ getString (R .string .photo_move_failed ),
2914+ navigationView .getHeight ());
29112915 }
29122916 bottomSheetDialogFragment .dismiss ();
29132917 }
@@ -2930,6 +2934,12 @@ public void folderSelected(String path) {
29302934 return true ;
29312935
29322936 case R .id .action_copy :
2937+ if (!PermissionUtils .checkPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
2938+ requestSdCardPermissions ();
2939+ swipeRefreshLayout .setRefreshing (false );
2940+ invalidateOptionsMenu ();
2941+ return true ;
2942+ }
29332943 bottomSheetDialogFragment = new SelectAlbumBottomSheet ();
29342944 bottomSheetDialogFragment .setTitle (getString (R .string .copy_to ));
29352945 bottomSheetDialogFragment .setSelectAlbumInterface (
@@ -2945,6 +2955,12 @@ public void folderSelected(String path) {
29452955 return true ;
29462956
29472957 case R .id .renameAlbum :
2958+ if (!PermissionUtils .checkPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
2959+ requestSdCardPermissions ();
2960+ swipeRefreshLayout .setRefreshing (false );
2961+ invalidateOptionsMenu ();
2962+ return true ;
2963+ }
29482964 AlertDialog .Builder renameDialogBuilder =
29492965 new AlertDialog .Builder (LFMainActivity .this , getDialogStyle ());
29502966 final EditText editTextNewName = new EditText (getApplicationContext ());
@@ -3087,7 +3103,6 @@ public void onClick(View dialog) {
30873103 mDrawerLayout ,
30883104 getString (R .string .rename_error ),
30893105 navigationView .getHeight ());
3090- requestSdCardPermissions ();
30913106 }
30923107 swipeRefreshLayout .setRefreshing (false );
30933108 } else {
@@ -3157,7 +3172,6 @@ public boolean onKey(
31573172 mDrawerLayout ,
31583173 getString (R .string .rename_error ),
31593174 navigationView .getHeight ());
3160- requestSdCardPermissions ();
31613175 }
31623176 swipeRefreshLayout .setRefreshing (false );
31633177 } else {
@@ -4609,7 +4623,44 @@ public void onClick(View view) {
46094623 snackbar .show ();
46104624 }
46114625
4612- } else asyncActivityRef .requestSdCardPermissions ();
4626+ } else {
4627+ SnackBarHandler .showWithBottomMargin2 (
4628+ asyncActivityRef .mDrawerLayout ,
4629+ asyncActivityRef .getString (R .string .error_copying_files ),
4630+ asyncActivityRef .navigationView .getHeight (),
4631+ Snackbar .LENGTH_SHORT );
4632+ }
46134633 }
46144634 }
4635+
4636+ private void showPermissionAlertDialog () {
4637+ AlertDialog .Builder builder = new AlertDialog .Builder (this );
4638+ builder .setIcon (R .drawable .ic_caution );
4639+ builder .setTitle (R .string .permission_rationale_title );
4640+ builder .setMessage (R .string .permission_rationale_storage );
4641+ builder .setCancelable (false );
4642+ builder .setPositiveButton (
4643+ R .string .exit ,
4644+ new DialogInterface .OnClickListener () {
4645+ @ Override
4646+ public void onClick (DialogInterface dialog , int which ) {
4647+ finish ();
4648+ }
4649+ });
4650+
4651+ builder .setNegativeButton (
4652+ R .string .grant_permission ,
4653+ new DialogInterface .OnClickListener () {
4654+ @ Override
4655+ public void onClick (DialogInterface dialog , int which ) {
4656+ PermissionUtils .requestPermissions (
4657+ LFMainActivity .this ,
4658+ REQUEST_CODE_SD_CARD_PERMISSIONS ,
4659+ Manifest .permission .WRITE_EXTERNAL_STORAGE );
4660+ }
4661+ });
4662+
4663+ AlertDialog alertDialog = builder .create ();
4664+ alertDialog .show ();
4665+ }
46154666}
0 commit comments