• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.bluetooth;
18 
19 import android.content.Context;
20 import android.os.SystemProperties;
21 import android.util.Log;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.ListPreference;
27 import androidx.preference.Preference;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.PreferenceControllerMixin;
31 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
32 
33 /**
34  * This preference represents the default log level for the Bluetooth stack
35  *
36  * The default log level is captured and held in an Android Log Framework log tag, using "bluetooth"
37  * as the tag name. The Log framework does not provide methods to directly write a log tag value,
38  * but instead leverages special system properties to hold the value of a log tag.
39  *
40  * This preferences aims to keep the selection in sync with the currently set log tag value. It
41  * writes directly to the system properties that hold the level associated with the bluetooth log
42  * tag. It leverages the Log.isLoggable("bluetooth", level) function to discern the current value.
43  * The default level is INFO.
44  *
45  * This value is read once at start of the Bluetooth stack. To use a new value once setting it, be
46  * sure to turn Bluetooth off and back on again.
47  */
48 public class BluetoothStackLogPreferenceController extends DeveloperOptionsPreferenceController
49         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
50     private static final String TAG = BluetoothStackLogPreferenceController.class.getSimpleName();
51 
52     private static final String PREFERENCE_KEY = "bt_stack_log_level";
53 
54     /* Ensure that the indexes match with bt_stack_log_values and bt_stack_log_entries ordering */
55     private static final int BT_LOG_LEVEL_VERBOSE_INDEX = 0;
56     private static final int BT_LOG_LEVEL_DEBUG_INDEX = 1;
57     private static final int BT_LOG_LEVEL_INFO_INDEX = 2;
58     private static final int BT_LOG_LEVEL_WARN_INDEX = 3;
59     private static final int BT_LOG_LEVEL_ERROR_INDEX = 4;
60     @VisibleForTesting static final int BT_LOG_LEVEL_DEFAULT_INDEX = BT_LOG_LEVEL_INFO_INDEX;
61 
62     private static final String BT_LOG_TAG = "bluetooth";
63     @VisibleForTesting static final String BT_LOG_LEVEL_PROP_PERSIST = "persist.log.tag.bluetooth";
64     @VisibleForTesting static final String BT_LOG_LEVEL_PROP = "log.tag.bluetooth";
65 
66     // Values represents the untranslatable log level strings that should be used for writing to
67     // system properties. Entries represents the translatable log level strings that should be used
68     // in the UI to communicate to the user their options for this preference.
69     private String[] mListValues;
70     private String[] mListEntries;
71 
72     /**
73      * Create a BluetoothStackLogPreferenceController instance
74      */
BluetoothStackLogPreferenceController(@onNull Context context)75     public BluetoothStackLogPreferenceController(@NonNull Context context) {
76         super(context);
77         mListValues = context.getResources().getStringArray(R.array.bt_stack_log_level_values);
78         mListEntries = context.getResources().getStringArray(R.array.bt_stack_log_level_entries);
79     }
80 
81     /**
82      * Returns the preference key associated with this preference
83      *
84      * Note that this key is _usually_ a system property in and of itself, which is expected to hold
85      * the value of the preference. In this case though, this key *does not* hold the preference. It
86      * is only really used to tie this controller to the list preference defined in the XML file.
87      *
88      * @return the preference key associated with this preference
89      */
90     @Override
91     @Nullable
getPreferenceKey()92     public String getPreferenceKey() {
93         return PREFERENCE_KEY;
94     }
95 
96     /**
97      * Update the state of the preference based on what the user has selected
98      *
99      * This function is invoked when the user has selected a new value for this preference. The new
100      * value is the entry value at the index of the list the user has selected. This value will be
101      * one of the values from the array returned in getEntryValues(). Specifically, this array is
102      * set using R.array.bt_stack_log_level_values
103      *
104      * @param preference - the preference object to set the value of
105      * @param newValue - the value the user has selected, as an Object
106      * @return True when updated successfully
107      */
108     @Override
onPreferenceChange(@onNull Preference preference, @NonNull Object newValue)109     public boolean onPreferenceChange(@NonNull Preference preference, @NonNull Object newValue) {
110         Log.v(TAG, "onPreferenceChange(pref=" + preference + "value=" + newValue.toString() + ")");
111         setBluetoothLogTag(newValue.toString());
112         setBluetoothLogLevelIndex(getBluetoothLogLevelIndex());
113         return true;
114     }
115 
116     /**
117      * Refresh the state of this preference based on the state stored on the system
118      *
119      * Read the Bluetooth stack log level from the underlying system property/log tag, and map that
120      * level to the proper index in the values and entries array. Use those strings to set the value
121      * and summary of the preference.
122      *
123      * @param preference - the preference object to refresh the state of
124      */
125     @Override
updateState(@onNull Preference preference)126     public void updateState(@NonNull Preference preference) {
127         Log.v(TAG, "updateState(pref=" + preference + "): refresh preference state");
128         setBluetoothLogLevelIndex(getBluetoothLogLevelIndex());
129     }
130 
131     /**
132      * Notify this developer options preference of a change to developer options visibility
133      *
134      * We developer options are closed, we should clear out the value of this developer option
135      * preference and revert it back to the default state of INFO.
136      */
137     @Override
onDeveloperOptionsSwitchDisabled()138     protected void onDeveloperOptionsSwitchDisabled() {
139         super.onDeveloperOptionsSwitchDisabled();
140         Log.v(TAG, "onDeveloperOptionsSwitchDisabled(): Revert stack log to default");
141         setBluetoothLogTag(null);
142         setBluetoothLogLevelIndex(BT_LOG_LEVEL_DEFAULT_INDEX);
143     }
144 
145     /**
146      * Set the system property values used by the Log framework to read the "bluetooth" log tag
147      *
148      * @param logLevel - the log level to set the Bluetooth stack minimum log level to
149      */
setBluetoothLogTag(@ullable String logLevel)150     private void setBluetoothLogTag(@Nullable String logLevel) {
151         Log.i(TAG, "setBluetoothLogTag(logLevel=" + logLevel + "): Set properties for log tag");
152         SystemProperties.set(BT_LOG_LEVEL_PROP_PERSIST, logLevel);
153         SystemProperties.set(BT_LOG_LEVEL_PROP, logLevel);
154     }
155 
156     /**
157      * Get the entry and value index corresponding to the current Bluetooth stack log level
158      *
159      * Since this preference uses an actual log tag and not a specific/private system property, we
160      * can read the value using the Log.isLoggable() function with our "bluetooth" log tag that
161      * represents the log level of the Bluetooth stack. This is safer than trying to replacate the
162      * logic used in the Log framework around the various persist, ro, and blank variants of the tag
163      *
164      * If no value is present, INFO is used.
165      *
166      * @return the entry/value index corresponding to the current log level of the tag "bluetooth"
167      */
168     @VisibleForTesting
getBluetoothLogLevelIndex()169     public int getBluetoothLogLevelIndex() {
170         int level = BT_LOG_LEVEL_DEFAULT_INDEX;
171         if (Log.isLoggable(BT_LOG_TAG, Log.VERBOSE)) {
172             level = BT_LOG_LEVEL_VERBOSE_INDEX;
173         } else if (Log.isLoggable(BT_LOG_TAG, Log.DEBUG)) {
174             level = BT_LOG_LEVEL_DEBUG_INDEX;
175         } else if (Log.isLoggable(BT_LOG_TAG, Log.INFO)) {
176             level = BT_LOG_LEVEL_INFO_INDEX;
177         } else if (Log.isLoggable(BT_LOG_TAG, Log.WARN)) {
178             level = BT_LOG_LEVEL_WARN_INDEX;
179         } else if (Log.isLoggable(BT_LOG_TAG, Log.ERROR)) {
180             level = BT_LOG_LEVEL_ERROR_INDEX;
181         }
182         Log.v(TAG, "getBluetoothLogLevelIndex() -> " + level);
183         return level;
184     }
185 
186     /**
187      * Set the current Bluetooth stack log level displayed in the list for this preference
188      *
189      * @param index - the index representing the log level choice of this preference
190      */
setBluetoothLogLevelIndex(int index)191     private void setBluetoothLogLevelIndex(int index) {
192         if (index < BT_LOG_LEVEL_VERBOSE_INDEX || index > BT_LOG_LEVEL_ERROR_INDEX) {
193             Log.e(TAG, "setBluetoothLogLevelIndex(index=" + index + "): Log level invalid");
194             return;
195         }
196 
197         String value = mListValues[index];
198         String entryValue = mListEntries[index];
199 
200         ListPreference preference = ((ListPreference) mPreference);
201         if (preference == null) {
202             Log.e(TAG, "setBluetoothLogLevelIndex(index=" + index + "): mPreference is null");
203             return;
204         }
205 
206         preference.setValue(value);
207         preference.setSummary(entryValue);
208 
209         Log.i(TAG, "setBluetoothLogLevelIndex(index=" + index
210                 + "): Updated Bluetooth stack log level to value='" + value + "', entryValue='"
211                 + entryValue + "'");
212     }
213 }
214