1 /*
2  * Copyright (C) 2020 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.provider.Settings;
21 
22 import androidx.preference.Preference;
23 import androidx.preference.TwoStatePreference;
24 
25 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
26 
27 /**
28  * Preference controller for the developer option to disable the automatic revocation of adb
29  * authorizations.
30  */
31 public class AdbAuthorizationTimeoutPreferenceController extends
32         DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener {
33     private static final String ADB_AUTHORIZATION_TIMEOUT_KEY = "adb_authorization_timeout";
34 
35     private final Context mContext;
36 
AdbAuthorizationTimeoutPreferenceController(Context context)37     public AdbAuthorizationTimeoutPreferenceController(Context context) {
38         super(context);
39         mContext = context;
40     }
41 
42     @Override
getPreferenceKey()43     public String getPreferenceKey() {
44         return ADB_AUTHORIZATION_TIMEOUT_KEY;
45     }
46 
47     @Override
updateState(Preference preference)48     public void updateState(Preference preference) {
49         final long authTimeout = Settings.Global.getLong(mContext.getContentResolver(),
50                 Settings.Global.ADB_ALLOWED_CONNECTION_TIME,
51                 Settings.Global.DEFAULT_ADB_ALLOWED_CONNECTION_TIME);
52         // An authTimeout of 0 indicates this preference is enabled and adb authorizations will not
53         // be automatically revoked.
54         ((TwoStatePreference) mPreference).setChecked(authTimeout == 0);
55     }
56 
57     @Override
onPreferenceChange(Preference preference, Object newValue)58     public boolean onPreferenceChange(Preference preference, Object newValue) {
59         writeSetting((boolean) newValue);
60         return true;
61     }
62 
63     @Override
onDeveloperOptionsSwitchDisabled()64     public void onDeveloperOptionsSwitchDisabled() {
65         super.onDeveloperOptionsSwitchDisabled();
66         writeSetting(false);
67         ((TwoStatePreference) mPreference).setChecked(false);
68     }
69 
writeSetting(boolean isEnabled)70     private void writeSetting(boolean isEnabled) {
71         long authTimeout = 0;
72         if (!isEnabled) {
73             authTimeout = Settings.Global.DEFAULT_ADB_ALLOWED_CONNECTION_TIME;
74         }
75         Settings.Global.putLong(mContext.getContentResolver(),
76                 Settings.Global.ADB_ALLOWED_CONNECTION_TIME,
77                 authTimeout);
78     }
79 }
80