Merge changes I96776484,Ic2ea10d9 into main

* changes:
  Add BluetoothLeAudioModePreferenceControllerTest
  Change the switcher to switch LE audio mode with broadcast
This commit is contained in:
Alice Kuo
2024-01-26 23:34:28 +00:00
committed by Android (Google) Code Review
7 changed files with 294 additions and 1 deletions

View File

@@ -233,6 +233,26 @@
<!-- Bluetooth Settings --> <!-- Bluetooth Settings -->
<!-- Bluetooth developer settings: Bluetooth LE Audio modes -->
<string-array name="bluetooth_leaudio_mode">
<!-- Do not translate. -->
<item>Disabled</item>
<!-- Do not translate. -->
<item>Unicast</item>
<!-- Do not translate. -->
<item>Unicast and Broadcast</item>
</string-array>
<!-- Values for Bluetooth LE Audio mode -->
<string-array name="bluetooth_leaudio_mode_values" translatable="false">
<!-- Do not translate. -->
<item>disabled</item>
<!-- Do not translate. -->
<item>unicast</item>
<!-- Do not translate. -->
<item>broadcast</item>
</string-array>
<!-- Bluetooth developer settings: Titles for maximum number of connected audio devices --> <!-- Bluetooth developer settings: Titles for maximum number of connected audio devices -->
<string-array name="bluetooth_max_connected_audio_devices"> <string-array name="bluetooth_max_connected_audio_devices">
<item>Use System Default: <xliff:g id="default_bluetooth_max_connected_audio_devices">%1$d</xliff:g></item> <item>Use System Default: <xliff:g id="default_bluetooth_max_connected_audio_devices">%1$d</xliff:g></item>

View File

@@ -249,7 +249,8 @@
<string name="bluetooth_disable_leaudio">Disable Bluetooth LE audio</string> <string name="bluetooth_disable_leaudio">Disable Bluetooth LE audio</string>
<!-- Summary of toggle for disabling Bluetooth LE audio [CHAR LIMIT=none]--> <!-- Summary of toggle for disabling Bluetooth LE audio [CHAR LIMIT=none]-->
<string name="bluetooth_disable_leaudio_summary">Disables Bluetooth LE audio feature if the device supports LE audio hardware capabilities.</string> <string name="bluetooth_disable_leaudio_summary">Disables Bluetooth LE audio feature if the device supports LE audio hardware capabilities.</string>
<!-- Setting toggle title for switch Bluetooth LE Audio mode. [CHAR LIMIT=40] -->
<string name="bluetooth_leaudio_mode">Bluetooth LE Audio mode</string>
<!-- Setting toggle title for enabling Bluetooth LE Audio toggle in Device Details. [CHAR LIMIT=40] --> <!-- Setting toggle title for enabling Bluetooth LE Audio toggle in Device Details. [CHAR LIMIT=40] -->
<string name="bluetooth_show_leaudio_device_details">Show LE audio toggle in Device Details</string> <string name="bluetooth_show_leaudio_device_details">Show LE audio toggle in Device Details</string>

View File

@@ -373,6 +373,13 @@
android:title="@string/bluetooth_disable_leaudio" android:title="@string/bluetooth_disable_leaudio"
android:summary="@string/bluetooth_disable_leaudio_summary" /> android:summary="@string/bluetooth_disable_leaudio_summary" />
<ListPreference
android:key="bluetooth_leaudio_mode"
android:title="@string/bluetooth_leaudio_mode"
android:summary="@string/summary_placeholder"
android:entries="@array/bluetooth_leaudio_mode"
android:entryValues="@array/bluetooth_leaudio_mode_values"/>
<SwitchPreferenceCompat <SwitchPreferenceCompat
android:key="bluetooth_show_leaudio_device_details" android:key="bluetooth_show_leaudio_device_details"
android:title="@string/bluetooth_show_leaudio_device_details"/> android:title="@string/bluetooth_show_leaudio_device_details"/>

View File

@@ -0,0 +1,139 @@
/*
* Copyright 2024 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;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothStatusCodes;
import android.content.Context;
import android.os.SystemProperties;
import android.sysprop.BluetoothProperties;
import android.text.TextUtils;
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;
/**
* Preference controller to control Bluetooth LE audio mode
*/
public class BluetoothLeAudioModePreferenceController
extends DeveloperOptionsPreferenceController
implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
private static final String PREFERENCE_KEY = "bluetooth_leaudio_mode";
static final String LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY =
"persist.bluetooth.leaudio_dynamic_switcher.mode";
@Nullable private final DevelopmentSettingsDashboardFragment mFragment;
private final String[] mListValues;
private final String[] mListSummaries;
@VisibleForTesting
@Nullable String mNewMode;
@VisibleForTesting
BluetoothAdapter mBluetoothAdapter;
boolean mChanged = false;
public BluetoothLeAudioModePreferenceController(@NonNull Context context,
@Nullable DevelopmentSettingsDashboardFragment fragment) {
super(context);
mFragment = fragment;
mBluetoothAdapter = context.getSystemService(BluetoothManager.class).getAdapter();
mListValues = context.getResources().getStringArray(R.array.bluetooth_leaudio_mode_values);
mListSummaries = context.getResources().getStringArray(R.array.bluetooth_leaudio_mode);
}
@Override
@NonNull public String getPreferenceKey() {
return PREFERENCE_KEY;
}
@Override
public boolean isAvailable() {
return BluetoothProperties.isProfileBapBroadcastSourceEnabled().orElse(false);
}
@Override
public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
if (mFragment == null) {
return false;
}
BluetoothRebootDialog.show(mFragment);
mChanged = true;
mNewMode = newValue.toString();
return false;
}
@Override
public void updateState(@NonNull Preference preference) {
if (mBluetoothAdapter == null) {
return;
}
if (mBluetoothAdapter.isLeAudioBroadcastSourceSupported()
== BluetoothStatusCodes.FEATURE_SUPPORTED) {
SystemProperties.set(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, "broadcast");
} else if (mBluetoothAdapter.isLeAudioSupported()
== BluetoothStatusCodes.FEATURE_SUPPORTED) {
SystemProperties.set(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, "unicast");
} else {
SystemProperties.set(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, "disabled");
}
final String currentValue = SystemProperties.get(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY);
int index = 0;
for (int i = 0; i < mListValues.length; i++) {
if (TextUtils.equals(currentValue, mListValues[i])) {
index = i;
break;
}
}
final ListPreference listPreference = (ListPreference) preference;
listPreference.setValue(mListValues[index]);
listPreference.setSummary(mListSummaries[index]);
}
/**
* Called when the RebootDialog confirm is clicked.
*/
public void onRebootDialogConfirmed() {
if (!mChanged) {
return;
}
SystemProperties.set(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mNewMode);
}
/**
* Called when the RebootDialog cancel is clicked.
*/
public void onRebootDialogCanceled() {
mChanged = false;
}
}

View File

@@ -21,6 +21,7 @@ import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothStatusCodes; import android.bluetooth.BluetoothStatusCodes;
import android.content.Context; import android.content.Context;
import android.os.SystemProperties; import android.os.SystemProperties;
import android.sysprop.BluetoothProperties;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
@@ -64,6 +65,12 @@ public class BluetoothLeAudioPreferenceController
return PREFERENCE_KEY; return PREFERENCE_KEY;
} }
@Override
public boolean isAvailable() {
return BluetoothProperties.isProfileBapUnicastClientEnabled().orElse(false)
&& !BluetoothProperties.isProfileBapBroadcastSourceEnabled().orElse(false);
}
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
BluetoothRebootDialog.show(mFragment); BluetoothRebootDialog.show(mFragment);

View File

@@ -454,6 +454,11 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
getDevelopmentOptionsController( getDevelopmentOptionsController(
BluetoothLeAudioPreferenceController.class); BluetoothLeAudioPreferenceController.class);
leAudioFeatureController.onRebootDialogConfirmed(); leAudioFeatureController.onRebootDialogConfirmed();
final BluetoothLeAudioModePreferenceController leAudioModeController =
getDevelopmentOptionsController(
BluetoothLeAudioModePreferenceController.class);
leAudioModeController.onRebootDialogConfirmed();
} }
@Override @Override
@@ -471,6 +476,11 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
getDevelopmentOptionsController( getDevelopmentOptionsController(
BluetoothLeAudioPreferenceController.class); BluetoothLeAudioPreferenceController.class);
leAudioFeatureController.onRebootDialogCanceled(); leAudioFeatureController.onRebootDialogCanceled();
final BluetoothLeAudioModePreferenceController leAudioModeController =
getDevelopmentOptionsController(
BluetoothLeAudioModePreferenceController.class);
leAudioModeController.onRebootDialogCanceled();
} }
@Override @Override
@@ -670,6 +680,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
controllers.add(new BluetoothAvrcpVersionPreferenceController(context)); controllers.add(new BluetoothAvrcpVersionPreferenceController(context));
controllers.add(new BluetoothMapVersionPreferenceController(context)); controllers.add(new BluetoothMapVersionPreferenceController(context));
controllers.add(new BluetoothLeAudioPreferenceController(context, fragment)); controllers.add(new BluetoothLeAudioPreferenceController(context, fragment));
controllers.add(new BluetoothLeAudioModePreferenceController(context, fragment));
controllers.add(new BluetoothLeAudioDeviceDetailsPreferenceController(context)); controllers.add(new BluetoothLeAudioDeviceDetailsPreferenceController(context));
controllers.add(new BluetoothLeAudioAllowListPreferenceController(context, fragment)); controllers.add(new BluetoothLeAudioAllowListPreferenceController(context, fragment));
controllers.add(new BluetoothA2dpHwOffloadPreferenceController(context, fragment)); controllers.add(new BluetoothA2dpHwOffloadPreferenceController(context, fragment));

View File

@@ -0,0 +1,108 @@
/*
* Copyright (C) 2022 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;
import static com.android.settings.development.BluetoothLeAudioModePreferenceController
.LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.os.SystemProperties;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class BluetoothLeAudioModePreferenceControllerTest {
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private DevelopmentSettingsDashboardFragment mFragment;
@Mock
private BluetoothAdapter mBluetoothAdapter;
@Mock
private ListPreference mPreference;
private Context mContext;
private BluetoothLeAudioModePreferenceController mController;
private String[] mListValues;
private String[] mListSummaries;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mListValues = mContext.getResources().getStringArray(
R.array.bluetooth_leaudio_mode_values);
mListSummaries = mContext.getResources().getStringArray(
R.array.bluetooth_leaudio_mode);
mController = spy(new BluetoothLeAudioModePreferenceController(mContext, mFragment));
when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mPreference);
mController.mBluetoothAdapter = mBluetoothAdapter;
mController.displayPreference(mPreferenceScreen);
}
@Test
public void onRebootDialogConfirmed_changeLeAudioMode_shouldSetLeAudioMode() {
mController.mChanged = true;
SystemProperties.set(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0]);
mController.mNewMode = mListValues[1];
mController.onRebootDialogConfirmed();
assertThat(SystemProperties.get(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0])
.equals(mController.mNewMode)).isTrue();
}
@Test
public void onRebootDialogConfirmed_notChangeLeAudioMode_shouldNotSetLeAudioMode() {
mController.mChanged = false;
SystemProperties.set(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0]);
mController.mNewMode = mListValues[1];
mController.onRebootDialogConfirmed();
assertThat(SystemProperties.get(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0])
.equals(mController.mNewMode)).isFalse();
}
@Test
public void onRebootDialogCanceled_shouldNotSetLeAudioMode() {
mController.mChanged = true;
SystemProperties.set(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0]);
mController.mNewMode = mListValues[1];
mController.onRebootDialogCanceled();
assertThat(SystemProperties.get(LE_AUDIO_DYNAMIC_SWITCHER_MODE_PROPERTY, mListValues[0])
.equals(mController.mNewMode)).isFalse();
}
}