Skip to content

Commit fde48bf

Browse files
committed
Add description for storage permission
Signed-off-by: Fung Gwo <fython@163.com>
1 parent e8a0754 commit fde48bf

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

app/src/main/java/app/gwo/safenhancer/lite/SettingsActivity.java

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import androidx.annotation.NonNull;
2121
import androidx.annotation.Nullable;
22+
import androidx.annotation.RequiresApi;
23+
2224
import app.gwo.safenhancer.lite.compat.Optional;
2325
import app.gwo.safenhancer.lite.util.BuildUtils;
2426
import app.gwo.safenhancer.lite.util.Settings;
@@ -32,16 +34,6 @@ public final class SettingsActivity extends BaseActivity {
3234
protected void onCreate(@Nullable Bundle savedInstanceState) {
3335
super.onCreate(savedInstanceState);
3436

35-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
36-
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
37-
!= PackageManager.PERMISSION_GRANTED) {
38-
requestPermissions(new String[] {
39-
Manifest.permission.READ_EXTERNAL_STORAGE,
40-
Manifest.permission.WRITE_EXTERNAL_STORAGE
41-
}, 0);
42-
}
43-
}
44-
4537
if (savedInstanceState == null) {
4638
getFragmentManager()
4739
.beginTransaction()
@@ -52,6 +44,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5244

5345
public static final class SettingsFragment extends PreferenceFragment {
5446

47+
private static final String KEY_STORAGE_PERMISSION = "storage_permission";
5548
private static final String KEY_PREFERRED_CAMERA_CLEAR = "clear_preferred_camera";
5649
private static final String KEY_HANDLED_APPS_CHOOSE = "handled_apps_choose";
5750
private static final String KEY_ABOUT_VERSION = "version";
@@ -61,7 +54,10 @@ public static final class SettingsFragment extends PreferenceFragment {
6154

6255
private static final int REQUEST_CODE_SR_PERMISSION = 1;
6356
private static final int REQUEST_CODE_OPEN_ROOT_URI = 2;
57+
private static final int REQUEST_CODE_STORAGE_PERMISSION = 3;
6458

59+
@Nullable
60+
private Preference mStoragePermission;
6561
private Preference mHandledAppsChoose;
6662
private SwitchPreference mSRPermission;
6763
private SwitchPreference mQIsolatedSupport;
@@ -73,6 +69,26 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
7369
addPreferencesFromResource(R.xml.settings_screen);
7470
PackageManager pm = getActivity().getPackageManager();
7571

72+
mStoragePermission = findPreference(KEY_STORAGE_PERMISSION);
73+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
74+
if (getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
75+
== PackageManager.PERMISSION_GRANTED) {
76+
if (mStoragePermission != null) {
77+
getPreferenceScreen().removePreference(mStoragePermission);
78+
}
79+
} else {
80+
startRequestStoragePermission();
81+
}
82+
}
83+
if (mStoragePermission != null) {
84+
mStoragePermission.setOnPreferenceClickListener(pref -> {
85+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
86+
startRequestStoragePermission();
87+
}
88+
return true;
89+
});
90+
}
91+
7692
findPreference(KEY_PREFERRED_CAMERA_CLEAR).setOnPreferenceClickListener(p -> {
7793
Settings.getInstance().setPreferredCamera(null);
7894
Toast.makeText(getActivity(), R.string.toast_cleared, Toast.LENGTH_LONG).show();
@@ -94,9 +110,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
94110
boolean newBool = (boolean) newValue;
95111
if (newBool) {
96112
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
97-
requestPermissions(new String[] {
98-
StorageRedirectManager.PERMISSION
99-
}, REQUEST_CODE_SR_PERMISSION);
113+
startRequestSRPermission();
100114
}
101115
} else {
102116
try {
@@ -177,6 +191,21 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
177191
});
178192
}
179193

194+
@RequiresApi(api = Build.VERSION_CODES.M)
195+
private void startRequestStoragePermission() {
196+
requestPermissions(new String[] {
197+
Manifest.permission.READ_EXTERNAL_STORAGE,
198+
Manifest.permission.WRITE_EXTERNAL_STORAGE
199+
}, REQUEST_CODE_STORAGE_PERMISSION);
200+
}
201+
202+
@RequiresApi(api = Build.VERSION_CODES.M)
203+
private void startRequestSRPermission() {
204+
requestPermissions(new String[] {
205+
StorageRedirectManager.PERMISSION
206+
}, REQUEST_CODE_SR_PERMISSION);
207+
}
208+
180209
@Override
181210
public void onActivityResult(int requestCode, int resultCode, Intent data) {
182211
if (REQUEST_CODE_PACKAGES_SELECTOR == requestCode
@@ -228,6 +257,20 @@ public void onRequestPermissionsResult(int requestCode,
228257
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
229258
mSRPermission.setChecked(true);
230259
}
260+
} else if (REQUEST_CODE_STORAGE_PERMISSION == requestCode) {
261+
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
262+
if (mStoragePermission != null) {
263+
getPreferenceScreen().removePreference(mStoragePermission);
264+
}
265+
} else {
266+
if (!isDetached()) {
267+
new AlertDialog.Builder(getActivity())
268+
.setTitle(R.string.storage_permission_dialog_failed_title)
269+
.setMessage(R.string.storage_permission_dialog_failed_message)
270+
.setPositiveButton(android.R.string.ok, null)
271+
.show();
272+
}
273+
}
231274
}
232275
}
233276
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ If they use their own interface only, it will have no effect even if apps are ch
2424
<string name="action_done">Done</string>
2525
<string name="packages_selector_search_hint">Search title/package name…</string>
2626

27-
<string name="isolated_storage_support_title">Isolated storage support</string>
27+
<string name="isolated_storage_support_title">Isolated (Scoped) storage support</string>
2828
<string name="isolated_storage_support_for_sr">Enable "Storage Redirect" support</string>
2929
<string name="isolated_storage_support_for_sr_summary"><![CDATA[
3030
If apps handled by SAFEnhancer is controlled by Storage Redirect and you cannot choose images
3131
normally, you need enable this feature.
3232
]]></string>
33-
<string name="isolated_storage_support_for_q">Enable Stock Isolated storage support</string>
33+
<string name="isolated_storage_support_for_q">Enable Stock Scoped storage support</string>
3434
<string name="isolated_storage_support_for_q_summary_disabled"><![CDATA[
3535
This feature is enabled for Android Q+ devices only.
3636
]]></string>
@@ -58,4 +58,16 @@ Now we will open Documents UI to pick root storage. Please be sure to choose the
5858
https://github.com/fython-tools/DocUIProxy-Android
5959
]]></string>
6060

61+
<string name="storage_permission_pref_title">Storage permission required</string>
62+
<string name="storage_permission_pref_summary"><![CDATA[
63+
If you see this option, it means you haven\'t grant storage permission to SAF Enhancer Lite.
64+
For Android pre-Q users, you should click this option to request permission. For Android Q+ users,
65+
please enable Stock Scoped storage support.
66+
]]></string>
67+
<string name="storage_permission_dialog_failed_title">Storage permission required</string>
68+
<string name="storage_permission_dialog_failed_message"><![CDATA[
69+
Permission request was denied. SAF Enhancer Lite may not work properly without storage permission
70+
before Android Q.
71+
]]></string>
72+
6173
</resources>

app/src/main/res/xml/settings_screen.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools">
44

5+
<Preference android:key="storage_permission"
6+
android:title="@string/storage_permission_pref_title"
7+
android:summary="@string/storage_permission_pref_summary"/>
8+
59
<PreferenceCategory android:title="@string/handled_apps">
610

711
<Preference android:key="handled_apps_choose"

0 commit comments

Comments
 (0)