Skip to content

Commit a2db043

Browse files
codedsuniamareebjamal
authored andcommitted
feat: corrects flow of sd card permission (#2898)
1 parent 9d671ac commit a2db043

File tree

4 files changed

+95
-47
lines changed

4 files changed

+95
-47
lines changed

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

Lines changed: 89 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;
@@ -64,6 +65,7 @@
6465
import androidx.appcompat.widget.SwitchCompat;
6566
import androidx.appcompat.widget.Toolbar;
6667
import androidx.cardview.widget.CardView;
68+
import androidx.core.app.ActivityCompat;
6769
import androidx.core.app.ActivityOptionsCompat;
6870
import androidx.core.content.ContextCompat;
6971
import androidx.core.view.GravityCompat;
@@ -128,6 +130,7 @@
128130
import org.fossasia.phimpme.gallery.util.ContentHelper;
129131
import org.fossasia.phimpme.gallery.util.CustomNestedView;
130132
import org.fossasia.phimpme.gallery.util.Measure;
133+
import org.fossasia.phimpme.gallery.util.PermissionUtils;
131134
import org.fossasia.phimpme.gallery.util.PreferenceUtil;
132135
import org.fossasia.phimpme.gallery.util.StringUtils;
133136
import 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
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import android.widget.RelativeLayout;
1616
import androidx.annotation.NonNull;
1717
import androidx.appcompat.app.AlertDialog;
18+
import androidx.core.app.ActivityCompat;
1819
import androidx.core.content.ContextCompat;
1920
import butterknife.BindView;
2021
import butterknife.ButterKnife;
@@ -120,8 +121,8 @@ public void onAnimationCompleted(int loopNumber) {
120121
}
121122

122123
private void askForPermission() {
123-
String[] permissions = new String[] {Manifest.permission.READ_EXTERNAL_STORAGE};
124-
PermissionUtils.requestPermissions(this, READ_EXTERNAL_STORAGE_ID, permissions);
124+
ActivityCompat.requestPermissions(
125+
this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE_ID);
125126
}
126127

127128
@Override
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
package org.fossasia.phimpme.gallery.util;
22

33
import android.Manifest;
4-
import android.app.Activity;
54
import android.content.Context;
65
import android.content.pm.PackageManager;
7-
import androidx.appcompat.app.AppCompatActivity;
8-
import androidx.core.app.ActivityCompat;
96
import androidx.core.content.ContextCompat;
107

118
/** Created by dnld on 01/04/16. */
129
public final class PermissionUtils {
1310

14-
private static boolean checkPermission(Context context, String permission) {
11+
public static boolean checkPermission(Context context, String permission) {
1512
return ContextCompat.checkSelfPermission(context, permission)
1613
== PackageManager.PERMISSION_GRANTED;
1714
}
1815

1916
public static boolean isDeviceInfoGranted(Context context) {
2017
return checkPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
2118
}
22-
23-
public static void requestPermissions(Object o, int permissionId, String... permissions) {
24-
if (o instanceof Activity) {
25-
ActivityCompat.requestPermissions((AppCompatActivity) o, permissions, permissionId);
26-
}
27-
}
2819
}

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)