• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.development;
18 
19 import android.content.Context;
20 import android.hardware.usb.IUsbManager;
21 import android.os.RemoteException;
22 import android.os.ServiceManager;
23 import android.os.SystemProperties;
24 import android.os.UserManager;
25 import android.support.annotation.VisibleForTesting;
26 import android.support.v7.preference.Preference;
27 import android.support.v7.preference.PreferenceScreen;
28 import android.text.TextUtils;
29 import android.util.Log;
30 
31 import com.android.settings.Utils;
32 import com.android.settings.core.PreferenceControllerMixin;
33 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
34 
35 public class ClearAdbKeysPreferenceController extends DeveloperOptionsPreferenceController
36         implements PreferenceControllerMixin {
37 
38     private static final String TAG = "ClearAdbPrefCtrl";
39     private static final String CLEAR_ADB_KEYS = "clear_adb_keys";
40 
41     @VisibleForTesting
42     static final String RO_ADB_SECURE_PROPERTY_KEY = "ro.adb.secure";
43 
44     private final IUsbManager mUsbManager;
45     private final DevelopmentSettingsDashboardFragment mFragment;
46 
ClearAdbKeysPreferenceController(Context context, DevelopmentSettingsDashboardFragment fragment)47     public ClearAdbKeysPreferenceController(Context context,
48             DevelopmentSettingsDashboardFragment fragment) {
49         super(context);
50 
51         mFragment = fragment;
52         mUsbManager = IUsbManager.Stub.asInterface(ServiceManager.getService(Context.USB_SERVICE));
53     }
54 
55     @Override
isAvailable()56     public boolean isAvailable() {
57         return SystemProperties.getBoolean(RO_ADB_SECURE_PROPERTY_KEY, false /* default */);
58     }
59 
60     @Override
getPreferenceKey()61     public String getPreferenceKey() {
62         return CLEAR_ADB_KEYS;
63     }
64 
65     @Override
displayPreference(PreferenceScreen screen)66     public void displayPreference(PreferenceScreen screen) {
67         super.displayPreference(screen);
68 
69         if (mPreference != null && !isAdminUser()) {
70             mPreference.setEnabled(false);
71         }
72     }
73 
74     @Override
handlePreferenceTreeClick(Preference preference)75     public boolean handlePreferenceTreeClick(Preference preference) {
76         if (Utils.isMonkeyRunning()) {
77             return false;
78         }
79 
80         if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
81             ClearAdbKeysWarningDialog.show(mFragment);
82             return true;
83         }
84         return false;
85     }
86 
87     @Override
onDeveloperOptionsSwitchEnabled()88     protected void onDeveloperOptionsSwitchEnabled() {
89         if (isAdminUser()) {
90             mPreference.setEnabled(true);
91         }
92     }
93 
onClearAdbKeysConfirmed()94     public void onClearAdbKeysConfirmed() {
95         try {
96             mUsbManager.clearUsbDebuggingKeys();
97         } catch (RemoteException e) {
98             Log.e(TAG, "Unable to clear adb keys", e);
99         }
100     }
101 
102     @VisibleForTesting
isAdminUser()103     boolean isAdminUser() {
104         return ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).isAdminUser();
105     }
106 }
107