OSDN Git Service

805dd22ec9d452435dfcb16d58715e3459250825
[android-x86/packages-apps-Settings.git] / src / com / android / settings / development / RootAccessPreferenceController.java
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 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;
29
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;
34
35 import java.io.File;
36
37 public class RootAccessPreferenceController extends DeveloperOptionsPreferenceController
38         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
39
40     private static final String TAG = "RootAccessPreferenceController";
41     private static final String PREF_KEY = "root_access";
42
43     private static final String ROOT_ACCESS_PROPERTY = "persist.sys.root_access";
44
45     private final DevelopmentSettingsDashboardFragment mFragment;
46     private Object mPendingRootAccessValue;
47
48     public RootAccessPreferenceController(Context context,
49             DevelopmentSettingsDashboardFragment fragment) {
50         super(context);
51
52         mFragment = fragment;
53     }
54
55     @Override
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);
59     }
60
61     @Override
62     public String getPreferenceKey() {
63         return PREF_KEY;
64     }
65
66     @Override
67     public void displayPreference(PreferenceScreen screen) {
68         super.displayPreference(screen);
69
70         final File file = new File("/system/xbin/su");
71         if (file.exists()) {
72             ((ListPreference) mPreference).setEntries(R.array.root_access_entries);
73             ((ListPreference) mPreference).setEntryValues(R.array.root_access_values);
74         } else {
75             ((ListPreference) mPreference).setEntries(R.array.root_access_entries_adb);
76             ((ListPreference) mPreference).setEntryValues(R.array.root_access_values_adb);
77         }
78
79         updatePreference();
80
81         if (!isAdminUser()) {
82             mPreference.setEnabled(false);
83         }
84     }
85
86     @Override
87     public boolean onPreferenceChange(Preference preference, Object newValue) {
88         if ("0".equals(newValue.toString())) {
89             writeRootAccessOptions(newValue);
90         } else {
91             mPendingRootAccessValue = newValue;
92             RootAccessWarningDialog.show(mFragment);
93         }
94         return true;
95     }
96
97     @Override
98     protected void onDeveloperOptionsSwitchEnabled() {
99         if (isAdminUser()) {
100             mPreference.setEnabled(true);
101         }
102     }
103
104     public void onRootAccessDialogConfirmed() {
105         writeRootAccessOptions(mPendingRootAccessValue);
106     }
107
108     public void onRootAccessDialogDismissed() {
109         updatePreference();
110     }
111
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);
122         }
123         updatePreference();
124     }
125
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)]);
131     }
132
133     @VisibleForTesting
134     boolean isAdminUser() {
135         return ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).isAdminUser();
136     }
137 }