blob: 47cbb77a6d16111281b32aa1872999c4a7c8364a [file] [log] [blame]
Tony Mantlerec4bae92017-06-28 15:10:54 -07001/*
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
17package com.android.settingslib.development;
18
19import android.content.ContentResolver;
20import android.content.Context;
21import android.content.Intent;
22import android.os.UserManager;
23import android.provider.Settings;
24import android.support.v14.preference.SwitchPreference;
25import android.support.v4.content.LocalBroadcastManager;
26import android.support.v7.preference.Preference;
27import android.support.v7.preference.PreferenceScreen;
28import android.support.v7.preference.TwoStatePreference;
29import android.text.TextUtils;
30
31import com.android.settingslib.core.AbstractPreferenceController;
Tony Mantler265e7fb2017-08-09 14:18:19 -070032import com.android.settingslib.core.ConfirmationDialogController;
Tony Mantlerec4bae92017-06-28 15:10:54 -070033
Tony Mantler265e7fb2017-08-09 14:18:19 -070034public abstract class AbstractEnableAdbPreferenceController extends AbstractPreferenceController
35 implements ConfirmationDialogController {
Tony Mantlerec4bae92017-06-28 15:10:54 -070036 private static final String KEY_ENABLE_ADB = "enable_adb";
37 public static final String ACTION_ENABLE_ADB_STATE_CHANGED =
38 "com.android.settingslib.development.AbstractEnableAdbController."
39 + "ENABLE_ADB_STATE_CHANGED";
40
41 private SwitchPreference mPreference;
42
43 public AbstractEnableAdbPreferenceController(Context context) {
44 super(context);
45 }
46
47 @Override
48 public void displayPreference(PreferenceScreen screen) {
49 super.displayPreference(screen);
50 if (isAvailable()) {
51 mPreference = (SwitchPreference) screen.findPreference(KEY_ENABLE_ADB);
52 }
53 }
54
55 @Override
56 public boolean isAvailable() {
57 return mContext.getSystemService(UserManager.class).isAdminUser();
58 }
59
60 @Override
61 public String getPreferenceKey() {
62 return KEY_ENABLE_ADB;
63 }
64
65 private boolean isAdbEnabled() {
66 final ContentResolver cr = mContext.getContentResolver();
67 return Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, 0) != 0;
68 }
69
70 @Override
71 public void updateState(Preference preference) {
72 ((TwoStatePreference)preference).setChecked(isAdbEnabled());
73 }
74
75 public void enablePreference(boolean enabled) {
76 if (isAvailable()) {
77 mPreference.setEnabled(enabled);
78 }
79 }
80
81 public void resetPreference() {
82 if (mPreference.isChecked()) {
83 mPreference.setChecked(false);
84 handlePreferenceTreeClick(mPreference);
85 }
86 }
87
88 public boolean haveDebugSettings() {
89 return isAdbEnabled();
90 }
91
92 @Override
93 public boolean handlePreferenceTreeClick(Preference preference) {
94 if (TextUtils.equals(KEY_ENABLE_ADB, preference.getKey())) {
95 if (!isAdbEnabled()) {
Tony Mantler265e7fb2017-08-09 14:18:19 -070096 showConfirmationDialog(preference);
Tony Mantlerec4bae92017-06-28 15:10:54 -070097 } else {
98 writeAdbSetting(false);
99 }
100 return true;
101 } else {
102 return false;
103 }
104 }
105
106 protected void writeAdbSetting(boolean enabled) {
107 Settings.Global.putInt(mContext.getContentResolver(),
108 Settings.Global.ADB_ENABLED, enabled ? 1 : 0);
109 notifyStateChanged();
110 }
111
Tony Mantler265e7fb2017-08-09 14:18:19 -0700112 private void notifyStateChanged() {
Tony Mantlerec4bae92017-06-28 15:10:54 -0700113 LocalBroadcastManager.getInstance(mContext)
114 .sendBroadcast(new Intent(ACTION_ENABLE_ADB_STATE_CHANGED));
115 }
Tony Mantlerec4bae92017-06-28 15:10:54 -0700116}