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