1 /*
2  * Copyright (C) 2018 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.autofill;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.provider.Settings;
22 import android.util.Log;
23 import android.view.autofill.AutofillManager;
24 
25 import androidx.preference.ListPreference;
26 import androidx.preference.Preference;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 import com.android.settingslib.core.lifecycle.LifecycleObserver;
32 import com.android.settingslib.core.lifecycle.events.OnDestroy;
33 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
34 
35 public final class AutofillLoggingLevelPreferenceController
36         extends DeveloperOptionsPreferenceController
37         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
38         LifecycleObserver, OnDestroy {
39 
40     private static final String TAG = "AutofillLoggingLevelPreferenceController";
41     private static final String AUTOFILL_LOGGING_LEVEL_KEY = "autofill_logging_level";
42 
43     private final String[] mListValues;
44     private final String[] mListSummaries;
45     private final AutofillDeveloperSettingsObserver mObserver;
46 
AutofillLoggingLevelPreferenceController(Context context, Lifecycle lifecycle)47     public AutofillLoggingLevelPreferenceController(Context context, Lifecycle lifecycle) {
48         super(context);
49 
50         Resources resources = context.getResources();
51         mListValues = resources.getStringArray(R.array.autofill_logging_level_values);
52         mListSummaries = resources.getStringArray(R.array.autofill_logging_level_entries);
53         mObserver = new AutofillDeveloperSettingsObserver(mContext, () -> updateOptions());
54         mObserver.register();
55 
56         if (lifecycle != null) {
57             lifecycle.addObserver(this);
58         }
59     }
60 
61     @Override
onDestroy()62     public void onDestroy() {
63         mObserver.unregister();
64     }
65 
66     @Override
getPreferenceKey()67     public String getPreferenceKey() {
68         return AUTOFILL_LOGGING_LEVEL_KEY;
69     }
70 
71     @Override
onPreferenceChange(Preference preference, Object newValue)72     public boolean onPreferenceChange(Preference preference, Object newValue) {
73         writeLevel(newValue);
74         updateOptions();
75         return true;
76     }
77 
78     @Override
updateState(Preference preference)79     public void updateState(Preference preference) {
80         updateOptions();
81     }
82 
83     @Override
onDeveloperOptionsSwitchDisabled()84     protected void onDeveloperOptionsSwitchDisabled() {
85         super.onDeveloperOptionsSwitchDisabled();
86         writeLevel(null);
87     }
88 
updateOptions()89     private void updateOptions() {
90         if (mPreference == null) {
91             // TODO: there should be a hook on AbstractPreferenceController where we could
92             // unregister mObserver and avoid this check
93             Log.v(TAG, "ignoring Settings update because UI is gone");
94             return;
95         }
96         final int level = Settings.Global.getInt(mContext.getContentResolver(),
97                 Settings.Global.AUTOFILL_LOGGING_LEVEL, AutofillManager.DEFAULT_LOGGING_LEVEL);
98 
99         final int index;
100         if (level == AutofillManager.FLAG_ADD_CLIENT_DEBUG) {
101             index = 1;
102         } else if (level == AutofillManager.FLAG_ADD_CLIENT_VERBOSE) {
103             index = 2;
104         } else {
105             index = 0;
106         }
107         final ListPreference listPreference = (ListPreference) mPreference;
108         listPreference.setValue(mListValues[index]);
109         listPreference.setSummary(mListSummaries[index]);
110     }
111 
writeLevel(Object newValue)112     private void writeLevel(Object newValue) {
113         int level = AutofillManager.NO_LOGGING;
114         if (newValue instanceof String) {
115             level = Integer.parseInt((String) newValue);
116         }
117         Settings.Global.putInt(mContext.getContentResolver(),
118                 Settings.Global.AUTOFILL_LOGGING_LEVEL, level);
119     }
120 }
121