Change the switcher to switch LE audio mode with broadcast

Change the previous toggle design to list option for broadcast feature.
The new toggle change is only applied as broadcast feature enabled whcih
is behind a feature flag.

Bug: 273153850
Test: make RunSettingsRoboTests ROBOTEST_FILTER=BluetoothLeAudioPreferenceControllerTest
Change-Id: Ic2ea10d9d9529a2d413525c1b660f8fbac371502
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index 357818c..0e35fed 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -233,6 +233,26 @@
 
     <!-- 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 -->
     <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>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index cbc1f95..863b86e 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -249,7 +249,8 @@
     <string name="bluetooth_disable_leaudio">Disable Bluetooth LE audio</string>
     <!-- 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>
-
+    <!-- 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] -->
     <string name="bluetooth_show_leaudio_device_details">Show LE audio toggle in Device Details</string>
 
diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml
index d44927f..fb5e280 100644
--- a/res/xml/development_settings.xml
+++ b/res/xml/development_settings.xml
@@ -373,6 +373,13 @@
             android:title="@string/bluetooth_disable_leaudio"
             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
             android:key="bluetooth_show_leaudio_device_details"
             android:title="@string/bluetooth_show_leaudio_device_details"/>
diff --git a/src/com/android/settings/development/BluetoothLeAudioModePreferenceController.java b/src/com/android/settings/development/BluetoothLeAudioModePreferenceController.java
new file mode 100644
index 0000000..2298c63
--- /dev/null
+++ b/src/com/android/settings/development/BluetoothLeAudioModePreferenceController.java
@@ -0,0 +1,137 @@
+/*
+ * 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.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;
+    @Nullable private String mNewMode;
+
+    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;
+    }
+}
diff --git a/src/com/android/settings/development/BluetoothLeAudioPreferenceController.java b/src/com/android/settings/development/BluetoothLeAudioPreferenceController.java
index f1b81b4..2a544f2 100644
--- a/src/com/android/settings/development/BluetoothLeAudioPreferenceController.java
+++ b/src/com/android/settings/development/BluetoothLeAudioPreferenceController.java
@@ -21,6 +21,7 @@
 import android.bluetooth.BluetoothStatusCodes;
 import android.content.Context;
 import android.os.SystemProperties;
+import android.sysprop.BluetoothProperties;
 
 import androidx.annotation.VisibleForTesting;
 import androidx.preference.Preference;
@@ -65,6 +66,12 @@
     }
 
     @Override
+    public boolean isAvailable() {
+        return BluetoothProperties.isProfileBapUnicastClientEnabled().orElse(false)
+                && !BluetoothProperties.isProfileBapBroadcastSourceEnabled().orElse(false);
+    }
+
+    @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         BluetoothRebootDialog.show(mFragment);
         mChanged = true;
diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
index 73567bc..504eda8 100644
--- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
+++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
@@ -454,6 +454,11 @@
                 getDevelopmentOptionsController(
                         BluetoothLeAudioPreferenceController.class);
         leAudioFeatureController.onRebootDialogConfirmed();
+
+        final BluetoothLeAudioModePreferenceController leAudioModeController =
+                getDevelopmentOptionsController(
+                        BluetoothLeAudioModePreferenceController.class);
+        leAudioModeController.onRebootDialogConfirmed();
     }
 
     @Override
@@ -471,6 +476,11 @@
                 getDevelopmentOptionsController(
                         BluetoothLeAudioPreferenceController.class);
         leAudioFeatureController.onRebootDialogCanceled();
+
+        final BluetoothLeAudioModePreferenceController leAudioModeController =
+                getDevelopmentOptionsController(
+                        BluetoothLeAudioModePreferenceController.class);
+        leAudioModeController.onRebootDialogCanceled();
     }
 
     @Override
@@ -670,6 +680,7 @@
         controllers.add(new BluetoothAvrcpVersionPreferenceController(context));
         controllers.add(new BluetoothMapVersionPreferenceController(context));
         controllers.add(new BluetoothLeAudioPreferenceController(context, fragment));
+        controllers.add(new BluetoothLeAudioModePreferenceController(context, fragment));
         controllers.add(new BluetoothLeAudioDeviceDetailsPreferenceController(context));
         controllers.add(new BluetoothLeAudioAllowListPreferenceController(context, fragment));
         controllers.add(new BluetoothA2dpHwOffloadPreferenceController(context, fragment));