packages/apps/Settings
修訂 | eb6e50ee4c21b4c7a09eb3c483156d486443d2b7 (tree) |
---|---|
時間 | 2020-04-14 23:49:48 |
作者 | Steve Kondik <steve@cyng...> |
Commiter | Chih-Wei Huang |
Settings: Add developer setting for root access
Also includes following change:
Change-Id: If96219d893c0dfdcf4ad36e1cd8de3a413db0e8b
@@ -0,0 +1,27 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<!-- | |
3 | + Copyright (C) 2012-2016 The CyanogenMod Project | |
4 | + Copyright (C) 2017 The LineageOS Project | |
5 | + | |
6 | + Licensed under the Apache License, Version 2.0 (the "License"); | |
7 | + you may not use this file except in compliance with the License. | |
8 | + You may obtain a copy of the License at | |
9 | + | |
10 | + http://www.apache.org/licenses/LICENSE-2.0 | |
11 | + | |
12 | + Unless required by applicable law or agreed to in writing, software | |
13 | + distributed under the License is distributed on an "AS IS" BASIS, | |
14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 | + See the License for the specific language governing permissions and | |
16 | + limitations under the License. | |
17 | +--> | |
18 | +<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> | |
19 | + <!-- Setting checkbox title for root access --> | |
20 | + <string name="root_access">Root access</string> | |
21 | + <string name="root_access_warning_title">Allow root access?</string> | |
22 | + <string name="root_access_warning_message">Allowing apps to request root access is very dangerous and could compromise the security of your system!</string> | |
23 | + <string name="root_access_none">Disabled</string> | |
24 | + <string name="root_access_apps">Apps only</string> | |
25 | + <string name="root_access_adb">ADB only</string> | |
26 | + <string name="root_access_all">Apps and ADB</string> | |
27 | +</resources> |
@@ -0,0 +1,43 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<!-- | |
3 | + Copyright (C) 2012-2015 The CyanogenMod Project | |
4 | + Copyright (C) 2018 The LinegeOS Project | |
5 | + | |
6 | + Licensed under the Apache License, Version 2.0 (the "License"); | |
7 | + you may not use this file except in compliance with the License. | |
8 | + You may obtain a copy of the License at | |
9 | + | |
10 | + http://www.apache.org/licenses/LICENSE-2.0 | |
11 | + | |
12 | + Unless required by applicable law or agreed to in writing, software | |
13 | + distributed under the License is distributed on an "AS IS" BASIS, | |
14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 | + See the License for the specific language governing permissions and | |
16 | + limitations under the License. | |
17 | +--> | |
18 | +<resources> | |
19 | + <!-- Arrays for root access capability --> | |
20 | + <string-array name="root_access_entries" translatable="false"> | |
21 | + <item>@string/root_access_none</item> | |
22 | + <item>@string/root_access_apps</item> | |
23 | + <item>@string/root_access_adb</item> | |
24 | + <item>@string/root_access_all</item> | |
25 | + </string-array> | |
26 | + | |
27 | + <string-array name="root_access_values" translatable="false"> | |
28 | + <item>0</item> | |
29 | + <item>1</item> | |
30 | + <item>2</item> | |
31 | + <item>3</item> | |
32 | + </string-array> | |
33 | + | |
34 | + <string-array name="root_access_entries_adb" translatable="false"> | |
35 | + <item>@string/root_access_none</item> | |
36 | + <item>@string/root_access_adb</item> | |
37 | + </string-array> | |
38 | + | |
39 | + <string-array name="root_access_values_adb" translatable="false"> | |
40 | + <item>0</item> | |
41 | + <item>2</item> | |
42 | + </string-array> | |
43 | +</resources> |
@@ -120,6 +120,11 @@ | ||
120 | 120 | android:fragment="com.android.settings.development.qstile.DevelopmentTileConfigFragment" |
121 | 121 | settings:searchable="false" /> |
122 | 122 | |
123 | + <ListPreference | |
124 | + android:key="root_access" | |
125 | + android:title="@string/root_access" | |
126 | + android:persistent="false" /> | |
127 | + | |
123 | 128 | <!-- Configure trust agent behavior --> |
124 | 129 | <SwitchPreference |
125 | 130 | android:key="security_setting_trust_agents_extend_unlock" |
@@ -62,7 +62,8 @@ import java.util.List; | ||
62 | 62 | public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFragment |
63 | 63 | implements SwitchBar.OnSwitchChangeListener, OemUnlockDialogHost, AdbDialogHost, |
64 | 64 | AdbClearKeysDialogHost, LogPersistDialogHost, |
65 | - BluetoothA2dpHwOffloadRebootDialog.OnA2dpHwDialogConfirmedListener { | |
65 | + BluetoothA2dpHwOffloadRebootDialog.OnA2dpHwDialogConfirmedListener, | |
66 | + RootAccessDialogHost { | |
66 | 67 | |
67 | 68 | private static final String TAG = "DevSettingsDashboard"; |
68 | 69 |
@@ -293,6 +294,20 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra | ||
293 | 294 | } |
294 | 295 | |
295 | 296 | @Override |
297 | + public void onRootAccessDialogConfirmed() { | |
298 | + final RootAccessPreferenceController controller = | |
299 | + getDevelopmentOptionsController(RootAccessPreferenceController.class); | |
300 | + controller.onRootAccessDialogConfirmed(); | |
301 | + } | |
302 | + | |
303 | + @Override | |
304 | + public void onRootAccessDialogDismissed() { | |
305 | + final RootAccessPreferenceController controller = | |
306 | + getDevelopmentOptionsController(RootAccessPreferenceController.class); | |
307 | + controller.onRootAccessDialogDismissed(); | |
308 | + } | |
309 | + | |
310 | + @Override | |
296 | 311 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
297 | 312 | boolean handledResult = false; |
298 | 313 | for (AbstractPreferenceController controller : mPreferenceControllers) { |
@@ -483,6 +498,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra | ||
483 | 498 | controllers.add(new ShortcutManagerThrottlingPreferenceController(context)); |
484 | 499 | controllers.add(new BubbleGlobalPreferenceController(context)); |
485 | 500 | controllers.add(new EnableGnssRawMeasFullTrackingPreferenceController(context)); |
501 | + controllers.add(new RootAccessPreferenceController(context, fragment)); | |
486 | 502 | controllers.add(new DefaultLaunchPreferenceController(context, "running_apps")); |
487 | 503 | controllers.add(new DefaultLaunchPreferenceController(context, "demo_mode")); |
488 | 504 | controllers.add(new DefaultLaunchPreferenceController(context, "quick_settings_tiles")); |
@@ -0,0 +1,33 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2018 The LineageOS Project | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | + | |
17 | +package com.android.settings.development; | |
18 | + | |
19 | +/** | |
20 | + * Interface for RootAccessWarningDialogFragment callbacks. | |
21 | + */ | |
22 | +public interface RootAccessDialogHost { | |
23 | + | |
24 | + /** | |
25 | + * Called when the user presses ok on the warning dialog. | |
26 | + */ | |
27 | + void onRootAccessDialogConfirmed(); | |
28 | + | |
29 | + /** | |
30 | + * Called when the user dismisses or cancels the warning dialog. | |
31 | + */ | |
32 | + void onRootAccessDialogDismissed(); | |
33 | +} |
@@ -0,0 +1,136 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2018 The LineageOS Project | |
3 | + * Copyright (C) 2019 The Android-x86 Open Source Project | |
4 | + * | |
5 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | + * you may not use this file except in compliance with the License. | |
7 | + * You may obtain a copy of the License at | |
8 | + * | |
9 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | + * | |
11 | + * Unless required by applicable law or agreed to in writing, software | |
12 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | + * See the License for the specific language governing permissions and | |
15 | + * limitations under the License. | |
16 | + */ | |
17 | + | |
18 | +package com.android.settings.development; | |
19 | + | |
20 | +import android.content.Context; | |
21 | +import android.os.Build; | |
22 | +import android.os.SystemProperties; | |
23 | +import android.os.UserManager; | |
24 | +import android.provider.Settings; | |
25 | +import androidx.annotation.VisibleForTesting; | |
26 | +import androidx.preference.ListPreference; | |
27 | +import androidx.preference.Preference; | |
28 | +import androidx.preference.PreferenceScreen; | |
29 | + | |
30 | +import com.android.settings.R; | |
31 | +import com.android.settings.core.PreferenceControllerMixin; | |
32 | +import com.android.settingslib.development.DeveloperOptionsPreferenceController; | |
33 | + | |
34 | +import java.io.File; | |
35 | + | |
36 | +public class RootAccessPreferenceController extends DeveloperOptionsPreferenceController | |
37 | + implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { | |
38 | + | |
39 | + private static final String TAG = "RootAccessPreferenceController"; | |
40 | + private static final String PREF_KEY = "root_access"; | |
41 | + | |
42 | + private static final String ROOT_ACCESS_PROPERTY = "persist.sys.root_access"; | |
43 | + | |
44 | + private final DevelopmentSettingsDashboardFragment mFragment; | |
45 | + private Object mPendingRootAccessValue; | |
46 | + | |
47 | + public RootAccessPreferenceController(Context context, | |
48 | + DevelopmentSettingsDashboardFragment fragment) { | |
49 | + super(context); | |
50 | + | |
51 | + mFragment = fragment; | |
52 | + } | |
53 | + | |
54 | + @Override | |
55 | + public boolean isAvailable() { | |
56 | + // User builds don't get root, and eng always gets root | |
57 | + return Build.IS_DEBUGGABLE || "eng".equals(Build.TYPE); | |
58 | + } | |
59 | + | |
60 | + @Override | |
61 | + public String getPreferenceKey() { | |
62 | + return PREF_KEY; | |
63 | + } | |
64 | + | |
65 | + @Override | |
66 | + public void displayPreference(PreferenceScreen screen) { | |
67 | + super.displayPreference(screen); | |
68 | + | |
69 | + final File file = new File("/system/xbin/su"); | |
70 | + if (file.exists()) { | |
71 | + ((ListPreference) mPreference).setEntries(R.array.root_access_entries); | |
72 | + ((ListPreference) mPreference).setEntryValues(R.array.root_access_values); | |
73 | + } else { | |
74 | + ((ListPreference) mPreference).setEntries(R.array.root_access_entries_adb); | |
75 | + ((ListPreference) mPreference).setEntryValues(R.array.root_access_values_adb); | |
76 | + } | |
77 | + | |
78 | + updatePreference(); | |
79 | + | |
80 | + if (!isAdminUser()) { | |
81 | + mPreference.setEnabled(false); | |
82 | + } | |
83 | + } | |
84 | + | |
85 | + @Override | |
86 | + public boolean onPreferenceChange(Preference preference, Object newValue) { | |
87 | + if ("0".equals(newValue.toString())) { | |
88 | + writeRootAccessOptions(newValue); | |
89 | + } else { | |
90 | + mPendingRootAccessValue = newValue; | |
91 | + RootAccessWarningDialog.show(mFragment, this); | |
92 | + } | |
93 | + return true; | |
94 | + } | |
95 | + | |
96 | + @Override | |
97 | + protected void onDeveloperOptionsSwitchEnabled() { | |
98 | + if (isAdminUser()) { | |
99 | + mPreference.setEnabled(true); | |
100 | + } | |
101 | + } | |
102 | + | |
103 | + public void onRootAccessDialogConfirmed() { | |
104 | + writeRootAccessOptions(mPendingRootAccessValue); | |
105 | + } | |
106 | + | |
107 | + public void onRootAccessDialogDismissed() { | |
108 | + updatePreference(); | |
109 | + } | |
110 | + | |
111 | + private void writeRootAccessOptions(Object newValue) { | |
112 | + String oldValue = SystemProperties.get(ROOT_ACCESS_PROPERTY, "0"); | |
113 | + SystemProperties.set(ROOT_ACCESS_PROPERTY, newValue.toString()); | |
114 | + if (Integer.valueOf(newValue.toString()) < 2 && !oldValue.equals(newValue) | |
115 | + && SystemProperties.getInt("service.adb.root", 0) == 1) { | |
116 | + SystemProperties.set("service.adb.root", "0"); | |
117 | + Settings.Secure.putInt(mContext.getContentResolver(), | |
118 | + Settings.Secure.ADB_ENABLED, 0); | |
119 | + Settings.Secure.putInt(mContext.getContentResolver(), | |
120 | + Settings.Secure.ADB_ENABLED, 1); | |
121 | + } | |
122 | + updatePreference(); | |
123 | + } | |
124 | + | |
125 | + private void updatePreference() { | |
126 | + String value = SystemProperties.get(ROOT_ACCESS_PROPERTY, "0"); | |
127 | + ((ListPreference) mPreference).setValue(value); | |
128 | + ((ListPreference) mPreference).setSummary(mContext.getResources() | |
129 | + .getStringArray(R.array.root_access_entries)[Integer.valueOf(value)]); | |
130 | + } | |
131 | + | |
132 | + @VisibleForTesting | |
133 | + boolean isAdminUser() { | |
134 | + return ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).isAdminUser(); | |
135 | + } | |
136 | +} |
@@ -0,0 +1,81 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2018 The LineageOS Project | |
3 | + * Copyright (C) 2019 The Android-x86 Open Source Project | |
4 | + * | |
5 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | + * you may not use this file except in compliance with the License. | |
7 | + * You may obtain a copy of the License at | |
8 | + * | |
9 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | + * | |
11 | + * Unless required by applicable law or agreed to in writing, software | |
12 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | + * See the License for the specific language governing permissions and | |
15 | + * limitations under the License. | |
16 | + */ | |
17 | + | |
18 | +package com.android.settings.development; | |
19 | + | |
20 | +import android.app.Dialog; | |
21 | +import android.content.DialogInterface; | |
22 | +import android.os.Bundle; | |
23 | + | |
24 | +import androidx.appcompat.app.AlertDialog; | |
25 | +import androidx.fragment.app.FragmentManager; | |
26 | + | |
27 | +import com.android.internal.logging.nano.MetricsProto; | |
28 | +import com.android.settings.R; | |
29 | +import com.android.settings.core.instrumentation.InstrumentedDialogFragment; | |
30 | + | |
31 | +public class RootAccessWarningDialog extends InstrumentedDialogFragment implements | |
32 | + DialogInterface.OnClickListener, DialogInterface.OnDismissListener { | |
33 | + | |
34 | + public static final String TAG = "RootAccessWarningDialog"; | |
35 | + | |
36 | + public static void show(DevelopmentSettingsDashboardFragment host, | |
37 | + RootAccessPreferenceController controller) { | |
38 | + final FragmentManager manager = host.getActivity().getSupportFragmentManager(); | |
39 | + if (manager.findFragmentByTag(TAG) == null) { | |
40 | + final RootAccessWarningDialog dialog = new RootAccessWarningDialog(); | |
41 | + dialog.setTargetFragment(host, 0 /* requestCode */); | |
42 | + dialog.show(manager, TAG); | |
43 | + } | |
44 | + } | |
45 | + | |
46 | + @Override | |
47 | + public int getMetricsCategory() { | |
48 | + return MetricsProto.MetricsEvent.TYPE_UNKNOWN; | |
49 | + } | |
50 | + | |
51 | + @Override | |
52 | + public Dialog onCreateDialog(Bundle savedInstanceState) { | |
53 | + return new AlertDialog.Builder(getActivity()) | |
54 | + .setTitle(R.string.root_access_warning_title) | |
55 | + .setMessage(R.string.root_access_warning_message) | |
56 | + .setPositiveButton(android.R.string.ok, this /* onClickListener */) | |
57 | + .setNegativeButton(android.R.string.cancel, this /* onClickListener */) | |
58 | + .create(); | |
59 | + } | |
60 | + | |
61 | + @Override | |
62 | + public void onClick(DialogInterface dialog, int which) { | |
63 | + final RootAccessDialogHost host = (RootAccessDialogHost) getTargetFragment(); | |
64 | + if (host != null) { | |
65 | + if (which == DialogInterface.BUTTON_POSITIVE) { | |
66 | + host.onRootAccessDialogConfirmed(); | |
67 | + } else { | |
68 | + host.onRootAccessDialogDismissed(); | |
69 | + } | |
70 | + } | |
71 | + } | |
72 | + | |
73 | + @Override | |
74 | + public void onDismiss(DialogInterface dialog) { | |
75 | + super.onDismiss(dialog); | |
76 | + final RootAccessDialogHost host = (RootAccessDialogHost) getTargetFragment(); | |
77 | + if (host != null) { | |
78 | + host.onRootAccessDialogDismissed(); | |
79 | + } | |
80 | + } | |
81 | +} |