Skip to content

Commit d18f9a5

Browse files
author
Suneet Srivastava
committed
feat: corrects flow of sd card write permission
1 parent 7c4f5cf commit d18f9a5

File tree

3 files changed

+96
-36
lines changed

3 files changed

+96
-36
lines changed

app/src/main/java/org/fossasia/phimpme/gallery/activities/LFMainActivity.java

Lines changed: 84 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.fossasia.phimpme.gallery.data.base.SortingMode.SIZE;
77
import static org.fossasia.phimpme.utilities.ActivitySwitchHelper.context;
88

9+
import android.Manifest;
910
import android.animation.Animator;
1011
import android.annotation.TargetApi;
1112
import android.app.Dialog;
@@ -128,6 +129,7 @@
128129
import org.fossasia.phimpme.gallery.util.ContentHelper;
129130
import org.fossasia.phimpme.gallery.util.CustomNestedView;
130131
import org.fossasia.phimpme.gallery.util.Measure;
132+
import org.fossasia.phimpme.gallery.util.PermissionUtils;
131133
import org.fossasia.phimpme.gallery.util.PreferenceUtil;
132134
import org.fossasia.phimpme.gallery.util.StringUtils;
133135
import 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, REQUEST_CODE_SD_CARD_PERMISSIONS, 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,42 @@ 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+
this, REQUEST_CODE_SD_CARD_PERMISSIONS, Manifest.permission.WRITE_EXTERNAL_STORAGE);
4658+
}
4659+
});
4660+
4661+
AlertDialog alertDialog = builder.create();
4662+
alertDialog.show();
4663+
}
46154664
}

app/src/main/java/org/fossasia/phimpme/gallery/util/PermissionUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/** Created by dnld on 01/04/16. */
1212
public final class PermissionUtils {
1313

14-
private static boolean checkPermission(Context context, String permission) {
14+
public static boolean checkPermission(Context context, String permission) {
1515
return ContextCompat.checkSelfPermission(context, permission)
1616
== PackageManager.PERMISSION_GRANTED;
1717
}
@@ -25,4 +25,13 @@ public static void requestPermissions(Object o, int permissionId, String... perm
2525
ActivityCompat.requestPermissions((AppCompatActivity) o, permissions, permissionId);
2626
}
2727
}
28+
29+
public static boolean shouldShowPermissionRationale(
30+
Object o, int permissionId, String permission) {
31+
if (o instanceof Activity) {
32+
return ActivityCompat.shouldShowRequestPermissionRationale((AppCompatActivity) o, permission);
33+
} else {
34+
return false;
35+
}
36+
}
2837
}

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,9 @@
12891289
<string name="press_back_again_to_exit">Press back again to exit</string>
12901290
<string name="photos_moved_successfully">Photos moved successfully</string>
12911291
<string name="photo_moved_successfully">Photo moved successfully</string>
1292+
<string name="photo_move_failed">Failed to move photos</string>
12921293
<string name="copied_successfully">Copied successfully</string>
1294+
<string name="error_copying_files">Error copying files</string>
12931295
<string name="photos_already_there">All photos are already there in the folder</string>
12941296
<string name="no_changes_made">No Changes Made!</string>
12951297
<string name="transition_photo">transition_photo</string>

0 commit comments

Comments
 (0)