2 * Copyright (C) 2018 The LineageOS Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.development;
19 import android.content.Context;
20 import android.os.Build;
21 import android.os.ServiceManager;
22 import android.os.SystemProperties;
23 import android.os.UserManager;
24 import android.provider.Settings;
25 import android.support.annotation.VisibleForTesting;
26 import android.support.v7.preference.ListPreference;
27 import android.support.v7.preference.Preference;
28 import android.support.v7.preference.PreferenceScreen;
30 import com.android.settings.R;
31 import com.android.settings.Utils;
32 import com.android.settings.core.PreferenceControllerMixin;
33 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
37 public class RootAccessPreferenceController extends DeveloperOptionsPreferenceController
38 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
40 private static final String TAG = "RootAccessPreferenceController";
41 private static final String PREF_KEY = "root_access";
43 private static final String ROOT_ACCESS_PROPERTY = "persist.sys.root_access";
45 private final DevelopmentSettingsDashboardFragment mFragment;
46 private Object mPendingRootAccessValue;
48 public RootAccessPreferenceController(Context context,
49 DevelopmentSettingsDashboardFragment fragment) {
56 public boolean isAvailable() {
57 // User builds don't get root, and eng always gets root
58 return Build.IS_DEBUGGABLE || "eng".equals(Build.TYPE);
62 public String getPreferenceKey() {
67 public void displayPreference(PreferenceScreen screen) {
68 super.displayPreference(screen);
70 final File file = new File("/system/xbin/su");
72 ((ListPreference) mPreference).setEntries(R.array.root_access_entries);
73 ((ListPreference) mPreference).setEntryValues(R.array.root_access_values);
75 ((ListPreference) mPreference).setEntries(R.array.root_access_entries_adb);
76 ((ListPreference) mPreference).setEntryValues(R.array.root_access_values_adb);
82 mPreference.setEnabled(false);
87 public boolean onPreferenceChange(Preference preference, Object newValue) {
88 if ("0".equals(newValue.toString())) {
89 writeRootAccessOptions(newValue);
91 mPendingRootAccessValue = newValue;
92 RootAccessWarningDialog.show(mFragment);
98 protected void onDeveloperOptionsSwitchEnabled() {
100 mPreference.setEnabled(true);
104 public void onRootAccessDialogConfirmed() {
105 writeRootAccessOptions(mPendingRootAccessValue);
108 public void onRootAccessDialogDismissed() {
112 private void writeRootAccessOptions(Object newValue) {
113 String oldValue = SystemProperties.get(ROOT_ACCESS_PROPERTY, "0");
114 SystemProperties.set(ROOT_ACCESS_PROPERTY, newValue.toString());
115 if (Integer.valueOf(newValue.toString()) < 2 && !oldValue.equals(newValue)
116 && SystemProperties.getInt("service.adb.root", 0) == 1) {
117 SystemProperties.set("service.adb.root", "0");
118 Settings.Secure.putInt(mContext.getContentResolver(),
119 Settings.Secure.ADB_ENABLED, 0);
120 Settings.Secure.putInt(mContext.getContentResolver(),
121 Settings.Secure.ADB_ENABLED, 1);
126 private void updatePreference() {
127 String value = SystemProperties.get(ROOT_ACCESS_PROPERTY, "0");
128 ((ListPreference) mPreference).setValue(value);
129 ((ListPreference) mPreference).setSummary(mContext.getResources()
130 .getStringArray(R.array.root_access_entries)[Integer.valueOf(value)]);
134 boolean isAdminUser() {
135 return ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).isAdminUser();