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