11package app .gwo .safenhancer .lite ;
22
33import android .Manifest ;
4+ import android .annotation .SuppressLint ;
5+ import android .app .AlertDialog ;
46import android .content .Intent ;
57import android .content .pm .PackageInfo ;
68import android .content .pm .PackageManager ;
1012import android .preference .Preference ;
1113import android .preference .PreferenceFragment ;
1214import android .preference .SwitchPreference ;
13- import android .util . Log ;
15+ import android .provider . DocumentsContract ;
1416import android .widget .Toast ;
1517
16- import java .util .Arrays ;
1718import java .util .List ;
1819
1920import androidx .annotation .NonNull ;
2021import androidx .annotation .Nullable ;
22+ import app .gwo .safenhancer .lite .compat .Optional ;
23+ import app .gwo .safenhancer .lite .util .BuildUtils ;
2124import app .gwo .safenhancer .lite .util .Settings ;
2225import moe .shizuku .redirectstorage .StorageRedirectManager ;
2326
@@ -54,12 +57,16 @@ public static final class SettingsFragment extends PreferenceFragment {
5457 private static final String KEY_ABOUT_VERSION = "version" ;
5558 private static final String KEY_ABOUT_GITHUB = "github" ;
5659 private static final String KEY_SR_API_PERMISSION = "sr_api_permission" ;
60+ private static final String KEY_Q_ISOLATED_SUPPORT = "q_isolated_support" ;
5761
5862 private static final int REQUEST_CODE_SR_PERMISSION = 1 ;
63+ private static final int REQUEST_CODE_OPEN_ROOT_URI = 2 ;
5964
6065 private Preference mHandledAppsChoose ;
6166 private SwitchPreference mSRPermission ;
67+ private SwitchPreference mQIsolatedSupport ;
6268
69+ @ SuppressLint ("InlinedApi" )
6370 @ Override
6471 public void onCreate (@ Nullable Bundle savedInstanceState ) {
6572 super .onCreate (savedInstanceState );
@@ -92,7 +99,16 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
9299 }, REQUEST_CODE_SR_PERMISSION );
93100 }
94101 } else {
95- // TODO Jump to settings
102+ try {
103+ Intent intent = new Intent (
104+ android .provider .Settings .ACTION_APPLICATION_DETAILS_SETTINGS );
105+ intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
106+ intent .setData (Uri .fromParts ("package" ,
107+ getActivity ().getPackageName (), null ));
108+ startActivity (intent );
109+ } catch (Exception e ) {
110+ e .printStackTrace ();
111+ }
96112 }
97113 return false ;
98114 });
@@ -103,6 +119,46 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
103119 == PackageManager .PERMISSION_GRANTED
104120 );
105121
122+ mQIsolatedSupport = (SwitchPreference ) findPreference (KEY_Q_ISOLATED_SUPPORT );
123+ mQIsolatedSupport .setEnabled (BuildUtils .isAtLeastQ ());
124+ if (!BuildUtils .isAtLeastQ ()) {
125+ mQIsolatedSupport .setSummary (
126+ R .string .isolated_storage_support_for_q_summary_disabled );
127+ }
128+ mQIsolatedSupport .setOnPreferenceChangeListener ((pref , newValue ) -> {
129+ boolean newBool = (boolean ) newValue ;
130+ if (newBool ) {
131+ new AlertDialog .Builder (getActivity ())
132+ .setTitle (R .string .isolated_storage_support_for_q_dialog_title )
133+ .setMessage (R .string .isolated_storage_support_for_q_dialog_message )
134+ .setPositiveButton (android .R .string .ok , (dialog , which ) -> {
135+ Intent intent = new Intent (Intent .ACTION_OPEN_DOCUMENT_TREE );
136+ intent .putExtra (DocumentsContract .EXTRA_INITIAL_URI ,
137+ Uri .parse ("content://" +
138+ "com.android.externalstorage.documents" +
139+ "/tree/primary%3A" ));
140+ if (intent .resolveActivity (pm ) != null ) {
141+ startActivityForResult (intent , REQUEST_CODE_OPEN_ROOT_URI );
142+ } else {
143+ // TODO Show error
144+ }
145+ })
146+ .show ();
147+ return false ;
148+ } else {
149+ Settings .getInstance ().setRootStorageUri (null );
150+ return true ;
151+ }
152+ });
153+ final Optional <Uri > rootStorageUri = Settings .getInstance ().getRootStorageUri ();
154+ mQIsolatedSupport .setChecked (rootStorageUri .isPresent ());
155+ if (rootStorageUri .isPresent ()) {
156+ mQIsolatedSupport .setSummaryOn (getString (
157+ R .string .isolated_storage_support_for_q_summary_checked ,
158+ rootStorageUri .get ().toString ()
159+ ));
160+ }
161+
106162 Preference versionPref = findPreference (KEY_ABOUT_VERSION );
107163 String version = "Unknown" ;
108164 try {
@@ -129,6 +185,21 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
129185 final List <String > result = PackagesSelectorActivity .getResult (data );
130186 Settings .getInstance ().setHandledApps (result );
131187 updateHandledAppsSummary (result .size ());
188+ } else if (REQUEST_CODE_OPEN_ROOT_URI == requestCode
189+ && RESULT_OK == resultCode
190+ && data != null ) {
191+ Uri uri = data .getData ();
192+ if (uri != null ) {
193+ getActivity ().getContentResolver ().takePersistableUriPermission (uri ,
194+ Intent .FLAG_GRANT_READ_URI_PERMISSION |
195+ Intent .FLAG_GRANT_WRITE_URI_PERMISSION );
196+ Settings .getInstance ().setRootStorageUri (uri );
197+ mQIsolatedSupport .setChecked (true );
198+ mQIsolatedSupport .setSummaryOn (getString (
199+ R .string .isolated_storage_support_for_q_summary_checked ,
200+ uri .toString ()
201+ ));
202+ }
132203 }
133204 }
134205
0 commit comments