Home | History | Annotate | Download | only in development
      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.support.annotation.VisibleForTesting;
     26 import android.support.v14.preference.SwitchPreference;
     27 import android.support.v4.content.LocalBroadcastManager;
     28 import android.support.v7.preference.Preference;
     29 import android.support.v7.preference.PreferenceScreen;
     30 import android.support.v7.preference.TwoStatePreference;
     31 import android.text.TextUtils;
     32 
     33 import com.android.settingslib.core.ConfirmationDialogController;
     34 
     35 public abstract class AbstractEnableAdbPreferenceController extends
     36         DeveloperOptionsPreferenceController implements ConfirmationDialogController {
     37     private static final String KEY_ENABLE_ADB = "enable_adb";
     38     public static final String ACTION_ENABLE_ADB_STATE_CHANGED =
     39             "com.android.settingslib.development.AbstractEnableAdbController."
     40                     + "ENABLE_ADB_STATE_CHANGED";
     41 
     42     public static final int ADB_SETTING_ON = 1;
     43     public static final int ADB_SETTING_OFF = 0;
     44 
     45 
     46     protected SwitchPreference mPreference;
     47 
     48     public AbstractEnableAdbPreferenceController(Context context) {
     49         super(context);
     50     }
     51 
     52     @Override
     53     public void displayPreference(PreferenceScreen screen) {
     54         super.displayPreference(screen);
     55         if (isAvailable()) {
     56             mPreference = (SwitchPreference) screen.findPreference(KEY_ENABLE_ADB);
     57         }
     58     }
     59 
     60     @Override
     61     public boolean isAvailable() {
     62         final UserManager um = mContext.getSystemService(UserManager.class);
     63         return um != null && (um.isAdminUser() || um.isDemoUser());
     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