Add ADB enable/disable PreferenceController
Bug: 34203528 Test: EnableAdbControllerTest Change-Id: I086403f7c4c9cf2f6f04b246e29b0d7ec4ab5b51
This commit is contained in:
@@ -61,6 +61,7 @@ import android.provider.Settings;
|
|||||||
import android.service.oemlock.OemLockManager;
|
import android.service.oemlock.OemLockManager;
|
||||||
import android.support.annotation.VisibleForTesting;
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.support.v14.preference.SwitchPreference;
|
import android.support.v14.preference.SwitchPreference;
|
||||||
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
import android.support.v7.preference.ListPreference;
|
import android.support.v7.preference.ListPreference;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.Preference.OnPreferenceChangeListener;
|
import android.support.v7.preference.Preference.OnPreferenceChangeListener;
|
||||||
@@ -96,6 +97,7 @@ import com.android.settings.widget.SwitchBar;
|
|||||||
import com.android.settingslib.RestrictedLockUtils;
|
import com.android.settingslib.RestrictedLockUtils;
|
||||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||||
import com.android.settingslib.RestrictedSwitchPreference;
|
import com.android.settingslib.RestrictedSwitchPreference;
|
||||||
|
import com.android.settingslib.development.AbstractEnableAdbPreferenceController;
|
||||||
import com.android.settingslib.drawer.CategoryKey;
|
import com.android.settingslib.drawer.CategoryKey;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -121,7 +123,6 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
*/
|
*/
|
||||||
public static final String PREF_SHOW = "show";
|
public static final String PREF_SHOW = "show";
|
||||||
|
|
||||||
private static final String ENABLE_ADB = "enable_adb";
|
|
||||||
private static final String CLEAR_ADB_KEYS = "clear_adb_keys";
|
private static final String CLEAR_ADB_KEYS = "clear_adb_keys";
|
||||||
private static final String ENABLE_TERMINAL = "enable_terminal";
|
private static final String ENABLE_TERMINAL = "enable_terminal";
|
||||||
private static final String KEEP_SCREEN_ON = "keep_screen_on";
|
private static final String KEEP_SCREEN_ON = "keep_screen_on";
|
||||||
@@ -257,7 +258,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
|
|
||||||
private boolean mHaveDebugSettings;
|
private boolean mHaveDebugSettings;
|
||||||
private boolean mDontPokeProperties;
|
private boolean mDontPokeProperties;
|
||||||
private SwitchPreference mEnableAdb;
|
private EnableAdbPreferenceController mEnableAdbController;
|
||||||
private Preference mClearAdbKeys;
|
private Preference mClearAdbKeys;
|
||||||
private SwitchPreference mEnableTerminal;
|
private SwitchPreference mEnableTerminal;
|
||||||
private RestrictedSwitchPreference mKeepScreenOn;
|
private RestrictedSwitchPreference mKeepScreenOn;
|
||||||
@@ -343,7 +344,6 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
// To track whether a confirmation dialog was clicked.
|
// To track whether a confirmation dialog was clicked.
|
||||||
private boolean mDialogClicked;
|
private boolean mDialogClicked;
|
||||||
private Dialog mEnableDialog;
|
private Dialog mEnableDialog;
|
||||||
private Dialog mAdbDialog;
|
|
||||||
|
|
||||||
private Dialog mAdbKeysDialog;
|
private Dialog mAdbKeysDialog;
|
||||||
private boolean mUnavailable;
|
private boolean mUnavailable;
|
||||||
@@ -357,6 +357,8 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
private TelephonyMonitorPreferenceController mTelephonyMonitorController;
|
private TelephonyMonitorPreferenceController mTelephonyMonitorController;
|
||||||
private CameraHalHdrplusPreferenceController mCameraHalHdrplusController;
|
private CameraHalHdrplusPreferenceController mCameraHalHdrplusController;
|
||||||
|
|
||||||
|
private BroadcastReceiver mEnableAdbReceiver;
|
||||||
|
|
||||||
public DevelopmentSettings() {
|
public DevelopmentSettings() {
|
||||||
super(UserManager.DISALLOW_DEBUGGING_FEATURES);
|
super(UserManager.DISALLOW_DEBUGGING_FEATURES);
|
||||||
}
|
}
|
||||||
@@ -409,7 +411,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
|
|
||||||
final PreferenceGroup debugDebuggingCategory = (PreferenceGroup)
|
final PreferenceGroup debugDebuggingCategory = (PreferenceGroup)
|
||||||
findPreference(DEBUG_DEBUGGING_CATEGORY_KEY);
|
findPreference(DEBUG_DEBUGGING_CATEGORY_KEY);
|
||||||
mEnableAdb = findAndInitSwitchPref(ENABLE_ADB);
|
mEnableAdbController = new EnableAdbPreferenceController(getActivity());
|
||||||
mClearAdbKeys = findPreference(CLEAR_ADB_KEYS);
|
mClearAdbKeys = findPreference(CLEAR_ADB_KEYS);
|
||||||
if (!SystemProperties.getBoolean("ro.adb.secure", false)) {
|
if (!SystemProperties.getBoolean("ro.adb.secure", false)) {
|
||||||
if (debugDebuggingCategory != null) {
|
if (debugDebuggingCategory != null) {
|
||||||
@@ -428,6 +430,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
mTelephonyMonitorController.displayPreference(getPreferenceScreen());
|
mTelephonyMonitorController.displayPreference(getPreferenceScreen());
|
||||||
mWebViewAppPrefController.displayPreference(getPreferenceScreen());
|
mWebViewAppPrefController.displayPreference(getPreferenceScreen());
|
||||||
mCameraHalHdrplusController.displayPreference(getPreferenceScreen());
|
mCameraHalHdrplusController.displayPreference(getPreferenceScreen());
|
||||||
|
mEnableAdbController.displayPreference(getPreferenceScreen());
|
||||||
|
|
||||||
mKeepScreenOn = (RestrictedSwitchPreference) findAndInitSwitchPref(KEEP_SCREEN_ON);
|
mKeepScreenOn = (RestrictedSwitchPreference) findAndInitSwitchPref(KEEP_SCREEN_ON);
|
||||||
mBtHciSnoopLog = findAndInitSwitchPref(BT_HCI_SNOOP_LOG);
|
mBtHciSnoopLog = findAndInitSwitchPref(BT_HCI_SNOOP_LOG);
|
||||||
@@ -443,7 +446,6 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
mAllPrefs.add(mPassword);
|
mAllPrefs.add(mPassword);
|
||||||
|
|
||||||
if (!mUm.isAdminUser()) {
|
if (!mUm.isAdminUser()) {
|
||||||
disableForUser(mEnableAdb);
|
|
||||||
disableForUser(mClearAdbKeys);
|
disableForUser(mClearAdbKeys);
|
||||||
disableForUser(mEnableTerminal);
|
disableForUser(mEnableTerminal);
|
||||||
disableForUser(mPassword);
|
disableForUser(mPassword);
|
||||||
@@ -647,6 +649,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
Preference pref = mAllPrefs.get(i);
|
Preference pref = mAllPrefs.get(i);
|
||||||
pref.setEnabled(enabled && !mDisabledPrefs.contains(pref));
|
pref.setEnabled(enabled && !mDisabledPrefs.contains(pref));
|
||||||
}
|
}
|
||||||
|
mEnableAdbController.enablePreference(enabled);
|
||||||
mBugReportInPowerController.enablePreference(enabled);
|
mBugReportInPowerController.enablePreference(enabled);
|
||||||
mTelephonyMonitorController.enablePreference(enabled);
|
mTelephonyMonitorController.enablePreference(enabled);
|
||||||
mWebViewAppPrefController.enablePreference(enabled);
|
mWebViewAppPrefController.enablePreference(enabled);
|
||||||
@@ -730,6 +733,17 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
updateBluetoothA2dpConfigurationValues();
|
updateBluetoothA2dpConfigurationValues();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mEnableAdbReceiver = new BroadcastReceiver() {
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
mVerifyAppsOverUsbController.updatePreference();
|
||||||
|
updateBugreportOptions();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
LocalBroadcastManager.getInstance(getContext())
|
||||||
|
.registerReceiver(mEnableAdbReceiver, new IntentFilter(
|
||||||
|
AbstractEnableAdbPreferenceController.ACTION_ENABLE_ADB_STATE_CHANGED));
|
||||||
|
|
||||||
return super.onCreateView(inflater, container, savedInstanceState);
|
return super.onCreateView(inflater, container, savedInstanceState);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -749,6 +763,11 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
adapter.closeProfileProxy(BluetoothProfile.A2DP, mBluetoothA2dp);
|
adapter.closeProfileProxy(BluetoothProfile.A2DP, mBluetoothA2dp);
|
||||||
mBluetoothA2dp = null;
|
mBluetoothA2dp = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mEnableAdbReceiver != null) {
|
||||||
|
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mEnableAdbReceiver);
|
||||||
|
mEnableAdbReceiver = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateSwitchPreference(SwitchPreference switchPreference, boolean value) {
|
void updateSwitchPreference(SwitchPreference switchPreference, boolean value) {
|
||||||
@@ -760,8 +779,9 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
final Context context = getActivity();
|
final Context context = getActivity();
|
||||||
final ContentResolver cr = context.getContentResolver();
|
final ContentResolver cr = context.getContentResolver();
|
||||||
mHaveDebugSettings = false;
|
mHaveDebugSettings = false;
|
||||||
updateSwitchPreference(mEnableAdb, Settings.Global.getInt(cr,
|
final Preference enableAdb = findPreference(mEnableAdbController.getPreferenceKey());
|
||||||
Settings.Global.ADB_ENABLED, 0) != 0);
|
mEnableAdbController.updateState(enableAdb);
|
||||||
|
mHaveDebugSettings |= mEnableAdbController.haveDebugSettings();
|
||||||
if (mEnableTerminal != null) {
|
if (mEnableTerminal != null) {
|
||||||
updateSwitchPreference(mEnableTerminal,
|
updateSwitchPreference(mEnableTerminal,
|
||||||
context.getPackageManager().getApplicationEnabledSetting(TERMINAL_APP_PACKAGE)
|
context.getPackageManager().getApplicationEnabledSetting(TERMINAL_APP_PACKAGE)
|
||||||
@@ -836,6 +856,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
mBugReportInPowerController.resetPreference();
|
mBugReportInPowerController.resetPreference();
|
||||||
|
mEnableAdbController.resetPreference();
|
||||||
resetDebuggerOptions();
|
resetDebuggerOptions();
|
||||||
writeLogpersistOption(null, true);
|
writeLogpersistOption(null, true);
|
||||||
writeLogdSizeOption(null);
|
writeLogdSizeOption(null);
|
||||||
@@ -2403,24 +2424,11 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (preference == mEnableAdb) {
|
if (mEnableAdbController.handlePreferenceTreeClick(preference)) {
|
||||||
if (mEnableAdb.isChecked()) {
|
return true;
|
||||||
mDialogClicked = false;
|
|
||||||
if (mAdbDialog != null) dismissDialogs();
|
|
||||||
mAdbDialog = new AlertDialog.Builder(getActivity()).setMessage(
|
|
||||||
getActivity().getResources().getString(R.string.adb_warning_message))
|
|
||||||
.setTitle(R.string.adb_warning_title)
|
|
||||||
.setPositiveButton(android.R.string.yes, this)
|
|
||||||
.setNegativeButton(android.R.string.no, this)
|
|
||||||
.show();
|
|
||||||
mAdbDialog.setOnDismissListener(this);
|
|
||||||
} else {
|
|
||||||
Settings.Global.putInt(getActivity().getContentResolver(),
|
|
||||||
Settings.Global.ADB_ENABLED, 0);
|
|
||||||
mVerifyAppsOverUsbController.updatePreference();
|
|
||||||
updateBugreportOptions();
|
|
||||||
}
|
}
|
||||||
} else if (preference == mClearAdbKeys) {
|
|
||||||
|
if (preference == mClearAdbKeys) {
|
||||||
if (mAdbKeysDialog != null) dismissDialogs();
|
if (mAdbKeysDialog != null) dismissDialogs();
|
||||||
mAdbKeysDialog = new AlertDialog.Builder(getActivity())
|
mAdbKeysDialog = new AlertDialog.Builder(getActivity())
|
||||||
.setMessage(R.string.adb_keys_warning_message)
|
.setMessage(R.string.adb_keys_warning_message)
|
||||||
@@ -2592,10 +2600,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void dismissDialogs() {
|
private void dismissDialogs() {
|
||||||
if (mAdbDialog != null) {
|
mEnableAdbController.dismissDialogs();
|
||||||
mAdbDialog.dismiss();
|
|
||||||
mAdbDialog = null;
|
|
||||||
}
|
|
||||||
if (mAdbKeysDialog != null) {
|
if (mAdbKeysDialog != null) {
|
||||||
mAdbKeysDialog.dismiss();
|
mAdbKeysDialog.dismiss();
|
||||||
mAdbKeysDialog = null;
|
mAdbKeysDialog = null;
|
||||||
@@ -2611,18 +2616,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
if (dialog == mAdbDialog) {
|
if (dialog == mAdbKeysDialog) {
|
||||||
if (which == DialogInterface.BUTTON_POSITIVE) {
|
|
||||||
mDialogClicked = true;
|
|
||||||
Settings.Global.putInt(getActivity().getContentResolver(),
|
|
||||||
Settings.Global.ADB_ENABLED, 1);
|
|
||||||
mVerifyAppsOverUsbController.updatePreference();
|
|
||||||
updateBugreportOptions();
|
|
||||||
} else {
|
|
||||||
// Reset the toggle
|
|
||||||
mEnableAdb.setChecked(false);
|
|
||||||
}
|
|
||||||
} else if (dialog == mAdbKeysDialog) {
|
|
||||||
if (which == DialogInterface.BUTTON_POSITIVE) {
|
if (which == DialogInterface.BUTTON_POSITIVE) {
|
||||||
try {
|
try {
|
||||||
IBinder b = ServiceManager.getService(Context.USB_SERVICE);
|
IBinder b = ServiceManager.getService(Context.USB_SERVICE);
|
||||||
@@ -2652,12 +2646,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
|
|||||||
|
|
||||||
public void onDismiss(DialogInterface dialog) {
|
public void onDismiss(DialogInterface dialog) {
|
||||||
// Assuming that onClick gets called first
|
// Assuming that onClick gets called first
|
||||||
if (dialog == mAdbDialog) {
|
if (dialog == mEnableDialog) {
|
||||||
if (!mDialogClicked) {
|
|
||||||
mEnableAdb.setChecked(false);
|
|
||||||
}
|
|
||||||
mAdbDialog = null;
|
|
||||||
} else if (dialog == mEnableDialog) {
|
|
||||||
if (!mDialogClicked) {
|
if (!mDialogClicked) {
|
||||||
mSwitchBar.setChecked(false);
|
mSwitchBar.setChecked(false);
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.development;
|
||||||
|
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.app.Dialog;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.provider.Settings;
|
||||||
|
import android.support.v14.preference.SwitchPreference;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
|
import com.android.settingslib.development.AbstractEnableAdbPreferenceController;
|
||||||
|
|
||||||
|
public class EnableAdbPreferenceController extends AbstractEnableAdbPreferenceController
|
||||||
|
implements PreferenceControllerMixin {
|
||||||
|
|
||||||
|
private Dialog mAdbDialog;
|
||||||
|
private boolean mDialogClicked;
|
||||||
|
|
||||||
|
public EnableAdbPreferenceController(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showConfirmationDialog(SwitchPreference preference) {
|
||||||
|
mDialogClicked = false;
|
||||||
|
dismissDialogs();
|
||||||
|
mAdbDialog = new AlertDialog.Builder(mContext).setMessage(
|
||||||
|
mContext.getString(R.string.adb_warning_message))
|
||||||
|
.setTitle(R.string.adb_warning_title)
|
||||||
|
.setPositiveButton(android.R.string.yes, (dialog, which) -> {
|
||||||
|
mDialogClicked = true;
|
||||||
|
writeAdbSetting(true);
|
||||||
|
})
|
||||||
|
.setNegativeButton(android.R.string.no, (dialog, which) -> {
|
||||||
|
preference.setChecked(false);
|
||||||
|
})
|
||||||
|
.show();
|
||||||
|
mAdbDialog.setOnDismissListener(dialog -> {
|
||||||
|
// Assuming that onClick gets called first
|
||||||
|
if (!mDialogClicked) {
|
||||||
|
preference.setChecked(false);
|
||||||
|
}
|
||||||
|
mAdbDialog = null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dismissDialogs() {
|
||||||
|
if (mAdbDialog != null) {
|
||||||
|
mAdbDialog.dismiss();
|
||||||
|
mAdbDialog = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user