1919
2020import androidx .annotation .NonNull ;
2121import androidx .annotation .Nullable ;
22+ import androidx .annotation .RequiresApi ;
23+
2224import app .gwo .safenhancer .lite .compat .Optional ;
2325import app .gwo .safenhancer .lite .util .BuildUtils ;
2426import 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 }
0 commit comments