Merge "Enable Bluetooth stack logging from Developer Options UI Element" into main
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index dd2d7e3..fe3f429 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -232,6 +232,23 @@
<!-- Bluetooth Settings -->
+ <!-- Titles for Bluetooth Stack Logging -->
+ <string-array name="bt_stack_log_level_entries">
+ <item>Verbose</item>
+ <item>Debug</item>
+ <item>Info</item>
+ <item>Warn</item>
+ <item>Error</item>
+ </string-array>
+
+ <!-- Values for Bluetooth Stack Logging -->
+ <string-array name="bt_stack_log_level_values" translatable="false">
+ <item>verbose</item>
+ <item>debug</item>
+ <item>info</item>
+ <item>warn</item>
+ <item>error</item>
+ </string-array>
<!-- Bluetooth developer settings: Bluetooth LE Audio modes -->
<string-array name="bluetooth_leaudio_mode">
diff --git a/res/values/strings.xml b/res/values/strings.xml
index fc1b425..24ae75f 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -4676,6 +4676,10 @@
<string name="experimental_category_title">Experimental</string>
<!-- Title for feature flags dashboard where developers can turn on experimental features [CHAR LIMIT=50] -->
<string name="feature_flags_dashboard_title">Feature flags</string>
+ <!-- Setting Checkbox title whether to enable Bluetooth stack log -->
+ <string name="bt_stack_log_level">Enable Bluetooth stack log</string>
+ <!-- setting Checkbox summary to set log level [CHAR_LIMIT=100] -->
+ <string name="bt_stack_log_level_summary">Change log level of Bluetooth Stack Logging (Toggle Bluetooth after changing this setting)</string>
<!-- Title for snoop logger filters dashboard where developers can turn on filters [CHAR LIMIT=100] -->
<string name="bt_hci_snoop_log_filters_dashboard_title">Bluetooth HCI snoop log filtering</string>
<!-- Summary for the snoop logger filters [CHAR LIMIT=100] -->
diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml
index 1fdfcaf..b1ebbb8 100644
--- a/res/xml/development_settings.xml
+++ b/res/xml/development_settings.xml
@@ -75,6 +75,13 @@
android:entryValues="@array/hdcp_checking_values" />
<ListPreference
+ android:key="bt_stack_log_level"
+ android:title="@string/bt_stack_log_level"
+ android:dialogTitle="@string/bt_stack_log_level_summary"
+ android:entries="@array/bt_stack_log_level_entries"
+ android:entryValues="@array/bt_stack_log_level_values" />
+
+ <ListPreference
android:key="bt_hci_snoop_log"
android:title="@string/bt_hci_snoop_log"
android:dialogTitle="@string/bt_hci_snoop_log_summary"
diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
index cfa4a58..3e211de 100644
--- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
+++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
@@ -69,6 +69,7 @@
import com.android.settings.development.bluetooth.BluetoothHDAudioPreferenceController;
import com.android.settings.development.bluetooth.BluetoothQualityDialogPreferenceController;
import com.android.settings.development.bluetooth.BluetoothSampleRateDialogPreferenceController;
+import com.android.settings.development.bluetooth.BluetoothStackLogPreferenceController;
import com.android.settings.development.graphicsdriver.GraphicsDriverEnableAngleAsSystemDriverController;
import com.android.settings.development.qstile.DevelopmentTiles;
import com.android.settings.development.storage.SharedDataPreferenceController;
@@ -637,6 +638,7 @@
controllers.add(new StayAwakePreferenceController(context, lifecycle));
controllers.add(new HdcpCheckingPreferenceController(context));
controllers.add(new BluetoothSnoopLogPreferenceController(context, fragment));
+ controllers.add(new BluetoothStackLogPreferenceController(context));
controllers.add(new DefaultLaunchPreferenceController(context,
"snoop_logger_filters_dashboard"));
controllers.add(new BluetoothSnoopLogFilterProfilePbapPreferenceController(context));
diff --git a/src/com/android/settings/development/bluetooth/BluetoothStackLogPreferenceController.java b/src/com/android/settings/development/bluetooth/BluetoothStackLogPreferenceController.java
new file mode 100644
index 0000000..23d4cc6
--- /dev/null
+++ b/src/com/android/settings/development/bluetooth/BluetoothStackLogPreferenceController.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.development.bluetooth;
+
+import android.content.Context;
+import android.os.SystemProperties;
+import android.util.Log;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.annotation.VisibleForTesting;
+import androidx.preference.ListPreference;
+import androidx.preference.Preference;
+
+import com.android.settings.R;
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settingslib.development.DeveloperOptionsPreferenceController;
+
+public class BluetoothStackLogPreferenceController extends DeveloperOptionsPreferenceController
+ implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
+
+ /* Ensure that the indexes match with bt_stack_log_values and bt_stack_log_entries ordering */
+ private static final String PREFERENCE_KEY = "bt_stack_log_level";
+ @VisibleForTesting static final int BTSTACK_LOG_MODE_VERBOSE_INDEX = 0;
+ @VisibleForTesting static final int BTSTACK_LOG_MODE_DEBUG_INDEX = 1;
+ @VisibleForTesting static final int BTSTACK_LOG_MODE_INFO_INDEX = 2;
+ @VisibleForTesting static final int BTSTACK_LOG_MODE_WARN_INDEX = 3;
+ @VisibleForTesting static final int BTSTACK_LOG_MODE_ERROR_INDEX = 4;
+
+ @VisibleForTesting
+ static final String BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY_PERSIST = "persist.log.tag.bluetooth";
+ static final String BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY = "log.tag.bluetooth";
+ static final String BLUETOOTH_STRING_NAME = "bluetooth";
+ static final int DEFAULT_MODE = BTSTACK_LOG_MODE_INFO_INDEX;
+
+ private final String[] mListValues;
+ private final String[] mListEntries;
+
+
+ public BluetoothStackLogPreferenceController(@NonNull Context context) {
+ super(context);
+ mListValues = context.getResources().getStringArray(R.array.bt_stack_log_level_values);
+ mListEntries = context.getResources().getStringArray(R.array.bt_stack_log_level_entries);
+ }
+
+ /** returns default log level index of INFO */
+ public int getDefaultModeIndex() {
+ return DEFAULT_MODE;
+ }
+
+ @Override
+ @Nullable
+ public String getPreferenceKey() {
+ return PREFERENCE_KEY;
+ }
+
+ @Override
+ public boolean onPreferenceChange(@NonNull Preference preference, @NonNull Object newValue) {
+ SystemProperties.set(BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY_PERSIST, newValue.toString());
+ SystemProperties.set(BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY, newValue.toString());
+ updateState(mPreference);
+ return true;
+ }
+
+ @Override
+ public void updateState(@NonNull Preference preference) {
+ final ListPreference listPreference = (ListPreference) preference;
+ int index = getBluetoothLogLevelIndex();
+ listPreference.setValue(mListValues[index]);
+ listPreference.setSummary(mListEntries[index]);
+ }
+
+ /**
+ * Returns the current log level from Log.isLoggable().
+ */
+ @VisibleForTesting
+ public int getBluetoothLogLevelIndex() {
+ if (Log.isLoggable(BLUETOOTH_STRING_NAME, Log.VERBOSE)) {
+ return BTSTACK_LOG_MODE_VERBOSE_INDEX;
+ } else if (Log.isLoggable(BLUETOOTH_STRING_NAME, Log.DEBUG)) {
+ return BTSTACK_LOG_MODE_DEBUG_INDEX;
+ } else if (Log.isLoggable(BLUETOOTH_STRING_NAME, Log.INFO)) {
+ return BTSTACK_LOG_MODE_INFO_INDEX;
+ } else if (Log.isLoggable(BLUETOOTH_STRING_NAME, Log.WARN)) {
+ return BTSTACK_LOG_MODE_WARN_INDEX;
+ } else if (Log.isLoggable(BLUETOOTH_STRING_NAME, Log.ERROR)) {
+ return BTSTACK_LOG_MODE_ERROR_INDEX;
+ }
+ return BTSTACK_LOG_MODE_INFO_INDEX;
+ }
+
+ @Override
+ protected void onDeveloperOptionsSwitchDisabled() {
+ super.onDeveloperOptionsSwitchDisabled();
+ SystemProperties.set(BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY_PERSIST, null);
+ SystemProperties.set(BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY, null);
+ ((ListPreference) mPreference).setValue(mListValues[getDefaultModeIndex()]);
+ ((ListPreference) mPreference).setSummary(mListEntries[getDefaultModeIndex()]);
+ }
+}
diff --git a/tests/unit/src/com/android/settings/development/bluetooth/BluetoothStackLogPreferenceControllerTest.java b/tests/unit/src/com/android/settings/development/bluetooth/BluetoothStackLogPreferenceControllerTest.java
new file mode 100644
index 0000000..0811f04
--- /dev/null
+++ b/tests/unit/src/com/android/settings/development/bluetooth/BluetoothStackLogPreferenceControllerTest.java
@@ -0,0 +1,217 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.development.bluetooth;
+
+import static com.android.settings.development.bluetooth.BluetoothStackLogPreferenceController.BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY;
+import static com.android.settings.development.bluetooth.BluetoothStackLogPreferenceController.BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY_PERSIST;
+import static com.android.settings.development.bluetooth.BluetoothStackLogPreferenceController.BTSTACK_LOG_MODE_VERBOSE_INDEX;
+import static com.android.settings.development.bluetooth.BluetoothStackLogPreferenceController.BTSTACK_LOG_MODE_DEBUG_INDEX;
+import static com.android.settings.development.bluetooth.BluetoothStackLogPreferenceController.BTSTACK_LOG_MODE_INFO_INDEX;
+import static com.android.settings.development.bluetooth.BluetoothStackLogPreferenceController.BTSTACK_LOG_MODE_WARN_INDEX;
+import static com.android.settings.development.bluetooth.BluetoothStackLogPreferenceController.BTSTACK_LOG_MODE_ERROR_INDEX;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.os.Looper;
+import android.os.SystemProperties;
+
+import androidx.preference.ListPreference;
+import androidx.preference.PreferenceManager;
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@RunWith(AndroidJUnit4.class)
+public class BluetoothStackLogPreferenceControllerTest {
+ private static final String TAG = "BluetoothStackLogPreferenceControllerTest";
+
+ @Mock private Context mContext;
+
+ private ListPreference mPreference;
+ private PreferenceManager mPreferenceManager;
+ private PreferenceScreen mPreferenceScreen;
+
+ private BluetoothStackLogPreferenceController mController;
+
+ private CharSequence[] mListValues;
+ private CharSequence[] mListEntries;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ mContext = ApplicationProvider.getApplicationContext();
+
+ if (Looper.myLooper() == null) {
+ Looper.prepare();
+ }
+
+ mPreferenceManager = new PreferenceManager(mContext);
+ mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
+ mPreference = new ListPreference(mContext);
+
+ mController = new BluetoothStackLogPreferenceController(mContext);
+
+ mPreference.setKey(mController.getPreferenceKey());
+ mPreference.setEntries(com.android.settings.R.array.bt_stack_log_level_entries);
+ mPreference.setEntryValues(com.android.settings.R.array.bt_stack_log_level_values);
+
+ mPreferenceScreen.addPreference(mPreference);
+ mController.displayPreference(mPreferenceScreen);
+
+ mListValues = mPreference.getEntryValues();
+ mListEntries = mPreference.getEntries();
+ }
+
+ /**
+ * Test that default log level is set to INFO
+ */
+ @Test
+ public void verifyDefaultState_enablesDefaultLogLevelEntriesAndValuesSameSize() {
+ mController.onPreferenceChange(mPreference, mController.getDefaultModeIndex());
+ assertThat(mPreference.getValue().toString()).isEqualTo(mListValues
+ [BTSTACK_LOG_MODE_INFO_INDEX].toString());
+ assertThat(mPreference.getSummary().toString()).isEqualTo(mListEntries
+ [BTSTACK_LOG_MODE_INFO_INDEX].toString());
+ }
+
+ /**
+ * Test that log level is changed to VERBOSE when VERBOSE is selected
+ */
+ @Test
+ public void onPreferenceChanged_enableBluetoothStackVerboseLogLevel() {
+ mController.onPreferenceChange(mPreference, mListValues[BTSTACK_LOG_MODE_VERBOSE_INDEX]
+ .toString());
+
+ final String persistedLogLevel = SystemProperties.get(
+ BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY_PERSIST);
+ final String logLevel = SystemProperties.get(BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY);
+ assertThat(persistedLogLevel).isEqualTo(mListValues[BTSTACK_LOG_MODE_VERBOSE_INDEX]
+ .toString());
+ assertThat(logLevel).isEqualTo(mListValues[BTSTACK_LOG_MODE_VERBOSE_INDEX].toString());
+
+ assertThat(mPreference.getValue().toString()).isEqualTo(mListValues
+ [BTSTACK_LOG_MODE_VERBOSE_INDEX].toString());
+ assertThat(mPreference.getSummary().toString()).isEqualTo(mListEntries
+ [BTSTACK_LOG_MODE_VERBOSE_INDEX].toString());
+ }
+
+ /**
+ * Test that log level is changed to DEBUG when DEBUG is selected
+ */
+ @Test
+ public void onPreferenceChanged_enableBluetoothStackDebugLogLevel() {
+ mController.onPreferenceChange(mPreference, mListValues[BTSTACK_LOG_MODE_DEBUG_INDEX]
+ .toString());
+
+ final String persistedLogLevel = SystemProperties.get(
+ BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY_PERSIST);
+ final String logLevel = SystemProperties.get(BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY);
+ assertThat(persistedLogLevel).isEqualTo(mListValues[BTSTACK_LOG_MODE_DEBUG_INDEX]
+ .toString());
+ assertThat(logLevel).isEqualTo(mListValues[BTSTACK_LOG_MODE_DEBUG_INDEX].toString());
+
+ assertThat(mPreference.getValue().toString()).isEqualTo(mListValues
+ [BTSTACK_LOG_MODE_DEBUG_INDEX].toString());
+ assertThat(mPreference.getSummary().toString()).isEqualTo(mListEntries
+ [BTSTACK_LOG_MODE_DEBUG_INDEX].toString());
+ }
+
+ /**
+ * Test that log level is changed to INFO when INFO is selected
+ */
+ @Test
+ public void onPreferenceChanged_enableBluetoothStackInfoLogLevel() {
+ mController.onPreferenceChange(mPreference, mListValues[BTSTACK_LOG_MODE_INFO_INDEX]
+ .toString());
+
+ final String persistedLogLevel = SystemProperties.get(
+ BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY_PERSIST);
+ final String logLevel = SystemProperties.get(BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY);
+ assertThat(persistedLogLevel).isEqualTo(mListValues[BTSTACK_LOG_MODE_INFO_INDEX]
+ .toString());
+ assertThat(logLevel).isEqualTo(mListValues[BTSTACK_LOG_MODE_INFO_INDEX].toString());
+
+ assertThat(mPreference.getValue().toString()).isEqualTo(mListValues
+ [BTSTACK_LOG_MODE_INFO_INDEX].toString());
+ assertThat(mPreference.getSummary().toString()).isEqualTo(mListEntries
+ [BTSTACK_LOG_MODE_INFO_INDEX].toString());
+ }
+
+ /**
+ * Test that log level is changed to WARN when WARN is selected
+ */
+ @Test
+ public void onPreferenceChanged_enableBluetoothStackWarnLogLevel() {
+ mController.onPreferenceChange(mPreference, mListValues[BTSTACK_LOG_MODE_WARN_INDEX]
+ .toString());
+
+ final String persistedLogLevel = SystemProperties.get(
+ BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY_PERSIST);
+ final String logLevel = SystemProperties.get(BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY);
+ assertThat(persistedLogLevel).isEqualTo(mListValues[BTSTACK_LOG_MODE_WARN_INDEX]
+ .toString());
+ assertThat(logLevel).isEqualTo(mListValues[BTSTACK_LOG_MODE_WARN_INDEX].toString());
+
+ assertThat(mPreference.getValue().toString()).isEqualTo(mListValues
+
+ [BTSTACK_LOG_MODE_WARN_INDEX].toString());
+ assertThat(mPreference.getSummary().toString()).isEqualTo(mListEntries
+ [BTSTACK_LOG_MODE_WARN_INDEX].toString());
+ }
+
+ /**
+ * Test that log level is changed to ERROR when ERROR is selected
+ */
+ @Test
+ public void onPreferenceChanged_enableBluetoothStackErrorLogLevel() {
+ mController.onPreferenceChange(mPreference, mListValues[BTSTACK_LOG_MODE_ERROR_INDEX]
+ .toString());
+
+ final String persistedLogLevel = SystemProperties.get(
+ BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY_PERSIST);
+ final String logLevel = SystemProperties.get(BLUETOOTH_BTSTACK_LOG_MODE_PROPERTY);
+ assertThat(persistedLogLevel).isEqualTo(mListValues[BTSTACK_LOG_MODE_ERROR_INDEX]
+ .toString());
+ assertThat(logLevel).isEqualTo(mListValues[BTSTACK_LOG_MODE_ERROR_INDEX].toString());
+
+ assertThat(mPreference.getValue().toString()).isEqualTo(mListValues
+ [BTSTACK_LOG_MODE_ERROR_INDEX].toString());
+ assertThat(mPreference.getSummary().toString()).isEqualTo(mListEntries
+ [BTSTACK_LOG_MODE_ERROR_INDEX].toString());
+ }
+
+ /**
+ * Test that preference is disabled when developer options is disabled
+ * Log level is also reset to default
+ */
+ @Test
+ public void onDeveloperOptionsDisabled_shouldDisablePreference() {
+ mController.onDeveloperOptionsDisabled();
+ assertThat(mPreference.isEnabled()).isFalse();
+ assertThat(mPreference.getValue().toString()).isEqualTo(mListValues[mController
+ .getDefaultModeIndex()].toString());
+ assertThat(mPreference.getSummary().toString()).isEqualTo(mListEntries[mController
+ .getDefaultModeIndex()].toString());
+ }
+}