blob: 75b6696d923c2549a78a36666af90e8e7be0b3ca [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;
32
33public abstract class AbstractEnableAdbPreferenceController extends AbstractPreferenceController {
34 private static final String KEY_ENABLE_ADB = "enable_adb";
35 public static final String ACTION_ENABLE_ADB_STATE_CHANGED =
36 "com.android.settingslib.development.AbstractEnableAdbController."
37 + "ENABLE_ADB_STATE_CHANGED";
38
39 private SwitchPreference mPreference;
40
41 public AbstractEnableAdbPreferenceController(Context context) {
42 super(context);
43 }
44
45 @Override
46 public void displayPreference(PreferenceScreen screen) {
47 super.displayPreference(screen);
48 if (isAvailable()) {
49 mPreference = (SwitchPreference) screen.findPreference(KEY_ENABLE_ADB);
50 }
51 }
52
53 @Override
54 public boolean isAvailable() {
55 return mContext.getSystemService(UserManager.class).isAdminUser();
56 }
57
58 @Override
59 public String getPreferenceKey() {
60 return KEY_ENABLE_ADB;
61 }
62
63 private boolean isAdbEnabled() {
64 final ContentResolver cr = mContext.getContentResolver();
65 return Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, 0) != 0;
66 }
67
68 @Override
69 public void updateState(Preference preference) {
70 ((TwoStatePreference)preference).setChecked(isAdbEnabled());
71 }
72
73 public void enablePreference(boolean enabled) {
74 if (isAvailable()) {
75 mPreference.setEnabled(enabled);
76 }
77 }
78
79 public void resetPreference() {
80 if (mPreference.isChecked()) {
81 mPreference.setChecked(false);
82 handlePreferenceTreeClick(mPreference);
83 }
84 }
85
86 public boolean haveDebugSettings() {
87 return isAdbEnabled();
88 }
89
90 @Override
91 public boolean handlePreferenceTreeClick(Preference preference) {
92 if (TextUtils.equals(KEY_ENABLE_ADB, preference.getKey())) {
93 if (!isAdbEnabled()) {
94 showConfirmationDialog((SwitchPreference) preference);
95 } else {
96 writeAdbSetting(false);
97 }
98 return true;
99 } else {
100 return false;
101 }
102 }
103
104 protected void writeAdbSetting(boolean enabled) {
105 Settings.Global.putInt(mContext.getContentResolver(),
106 Settings.Global.ADB_ENABLED, enabled ? 1 : 0);
107 notifyStateChanged();
108 }
109
110 protected void notifyStateChanged() {
111 LocalBroadcastManager.getInstance(mContext)
112 .sendBroadcast(new Intent(ACTION_ENABLE_ADB_STATE_CHANGED));
113 }
114
115 public abstract void showConfirmationDialog(SwitchPreference preference);
116}