1 /*
2  * Copyright (C) 2017 The Android Open Source 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.settingslib.development;
18 
19 import static com.android.settingslib.RestrictedLockUtilsInternal.checkIfUsbDataSignalingIsDisabled;
20 
21 import android.app.ActivityManager;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 import android.provider.Settings;
28 import android.text.TextUtils;
29 
30 import androidx.annotation.VisibleForTesting;
31 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
32 import androidx.preference.Preference;
33 import androidx.preference.PreferenceScreen;
34 import androidx.preference.TwoStatePreference;
35 
36 import com.android.settingslib.RestrictedSwitchPreference;
37 import com.android.settingslib.core.ConfirmationDialogController;
38 
39 public abstract class AbstractEnableAdbPreferenceController extends
40         DeveloperOptionsPreferenceController implements ConfirmationDialogController {
41     private static final String KEY_ENABLE_ADB = "enable_adb";
42     public static final String ACTION_ENABLE_ADB_STATE_CHANGED =
43             "com.android.settingslib.development.AbstractEnableAdbController."
44                     + "ENABLE_ADB_STATE_CHANGED";
45 
46     public static final int ADB_SETTING_ON = 1;
47     public static final int ADB_SETTING_OFF = 0;
48 
49 
50     protected RestrictedSwitchPreference mPreference;
51 
AbstractEnableAdbPreferenceController(Context context)52     public AbstractEnableAdbPreferenceController(Context context) {
53         super(context);
54     }
55 
56     @Override
displayPreference(PreferenceScreen screen)57     public void displayPreference(PreferenceScreen screen) {
58         super.displayPreference(screen);
59         if (isAvailable()) {
60             mPreference = (RestrictedSwitchPreference) screen.findPreference(KEY_ENABLE_ADB);
61         }
62     }
63 
64     @Override
isAvailable()65     public boolean isAvailable() {
66         return mContext.getSystemService(UserManager.class).isAdminUser();
67     }
68 
69     @Override
getPreferenceKey()70     public String getPreferenceKey() {
71         return KEY_ENABLE_ADB;
72     }
73 
isAdbEnabled()74     private boolean isAdbEnabled() {
75         final ContentResolver cr = mContext.getContentResolver();
76         return Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, ADB_SETTING_OFF)
77                 != ADB_SETTING_OFF;
78     }
79 
80     @Override
updateState(Preference preference)81     public void updateState(Preference preference) {
82         ((TwoStatePreference) preference).setChecked(isAdbEnabled());
83         if (isAvailable()) {
84             ((RestrictedSwitchPreference) preference).setDisabledByAdmin(
85                     checkIfUsbDataSignalingIsDisabled(mContext, UserHandle.myUserId()));
86         }
87     }
88 
89     @Override
onDeveloperOptionsSwitchEnabled()90     protected void onDeveloperOptionsSwitchEnabled() {
91         super.onDeveloperOptionsSwitchEnabled();
92         if (isAvailable()) {
93             mPreference.setDisabledByAdmin(
94                     checkIfUsbDataSignalingIsDisabled(mContext, UserHandle.myUserId()));
95         }
96     }
97 
enablePreference(boolean enabled)98     public void enablePreference(boolean enabled) {
99         if (isAvailable()) {
100             mPreference.setEnabled(enabled);
101         }
102     }
103 
resetPreference()104     public void resetPreference() {
105         if (mPreference.isChecked()) {
106             mPreference.setChecked(false);
107             handlePreferenceTreeClick(mPreference);
108         }
109     }
110 
haveDebugSettings()111     public boolean haveDebugSettings() {
112         return isAdbEnabled();
113     }
114 
115     @Override
handlePreferenceTreeClick(Preference preference)116     public boolean handlePreferenceTreeClick(Preference preference) {
117         if (isUserAMonkey()) {
118             return false;
119         }
120 
121         if (TextUtils.equals(KEY_ENABLE_ADB, preference.getKey())) {
122             if (!isAdbEnabled()) {
123                 showConfirmationDialog(preference);
124             } else {
125                 writeAdbSetting(false);
126             }
127             return true;
128         } else {
129             return false;
130         }
131     }
132 
writeAdbSetting(boolean enabled)133     protected void writeAdbSetting(boolean enabled) {
134         Settings.Global.putInt(mContext.getContentResolver(),
135                 Settings.Global.ADB_ENABLED, enabled ? ADB_SETTING_ON : ADB_SETTING_OFF);
136         notifyStateChanged();
137     }
138 
notifyStateChanged()139     private void notifyStateChanged() {
140         LocalBroadcastManager.getInstance(mContext)
141                 .sendBroadcast(new Intent(ACTION_ENABLE_ADB_STATE_CHANGED));
142     }
143 
144     @VisibleForTesting
isUserAMonkey()145     boolean isUserAMonkey() {
146         return ActivityManager.isUserAMonkey();
147     }
148 }
149