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