[Audiosharing] Always show broadcast name when BT is on.

Also group the broadcast name to Stream settings category.

Test: manual
Bug: 305620450
Change-Id: I12bba9d5199d6770f86dad1089b47c3767285573
diff --git a/res/xml/bluetooth_audio_sharing.xml b/res/xml/bluetooth_audio_sharing.xml
index 9ffa2b2..dc577f6 100644
--- a/res/xml/bluetooth_audio_sharing.xml
+++ b/res/xml/bluetooth_audio_sharing.xml
@@ -38,11 +38,17 @@
         android:title="Play a test sound"
         settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingPlaySoundPreferenceController" />
 
-    <com.android.settings.connecteddevice.audiosharing.AudioSharingNamePreference
-        android:key="audio_sharing_stream_name"
-        android:summary="********"
-        android:title="Stream name"
-        settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingNamePreferenceController" />
+    <PreferenceCategory
+        android:key="audio_sharing_stream_settings_category"
+        android:title="Stream settings"
+        settings:controller="com.android.settings.connecteddevice.audiosharing.StreamSettingsCategoryController">
+
+        <com.android.settings.connecteddevice.audiosharing.AudioSharingNamePreference
+            android:key="audio_sharing_stream_name"
+            android:summary="********"
+            android:title="Stream name"
+            settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingNamePreferenceController" />
+    </PreferenceCategory>
 
     <PreferenceCategory
         android:key="audio_streams_settings_category"
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDashboardFragment.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDashboardFragment.java
index 9105297..7a7f337 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDashboardFragment.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDashboardFragment.java
@@ -35,7 +35,6 @@
     private AudioSharingDeviceVolumeGroupController mAudioSharingDeviceVolumeGroupController;
     private CallsAndAlarmsPreferenceController mCallsAndAlarmsPreferenceController;
     private AudioSharingPlaySoundPreferenceController mAudioSharingPlaySoundPreferenceController;
-    private AudioSharingNamePreferenceController mAudioSharingNamePreferenceController;
     private AudioStreamsCategoryController mAudioStreamsCategoryController;
 
     public AudioSharingDashboardFragment() {
@@ -77,7 +76,6 @@
         mCallsAndAlarmsPreferenceController.init(this);
         mAudioSharingPlaySoundPreferenceController =
                 use(AudioSharingPlaySoundPreferenceController.class);
-        mAudioSharingNamePreferenceController = use(AudioSharingNamePreferenceController.class);
         mAudioStreamsCategoryController = use(AudioStreamsCategoryController.class);
     }
 
@@ -104,7 +102,6 @@
         mAudioSharingDeviceVolumeGroupController.updateVisibility();
         mCallsAndAlarmsPreferenceController.updateVisibility();
         mAudioSharingPlaySoundPreferenceController.updateVisibility();
-        mAudioSharingNamePreferenceController.updateVisibility();
         mAudioStreamsCategoryController.updateVisibility();
     }
 }
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingNamePreferenceController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingNamePreferenceController.java
index 36f66ff..a3eb188 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingNamePreferenceController.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingNamePreferenceController.java
@@ -18,13 +18,12 @@
 
 import android.content.Context;
 
-import androidx.annotation.NonNull;
-import androidx.lifecycle.LifecycleOwner;
 import androidx.preference.Preference;
 
+import com.android.settings.core.BasePreferenceController;
 import com.android.settings.widget.ValidatedEditTextPreference;
 
-public class AudioSharingNamePreferenceController extends AudioSharingBasePreferenceController
+public class AudioSharingNamePreferenceController extends BasePreferenceController
         implements ValidatedEditTextPreference.Validator, Preference.OnPreferenceChangeListener {
 
     private static final String TAG = "AudioSharingNamePreferenceController";
@@ -33,12 +32,17 @@
 
     private AudioSharingNameTextValidator mAudioSharingNameTextValidator;
 
-    public AudioSharingNamePreferenceController(Context context) {
-        super(context, PREF_KEY);
+    public AudioSharingNamePreferenceController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
         mAudioSharingNameTextValidator = new AudioSharingNameTextValidator();
     }
 
     @Override
+    public int getAvailabilityStatus() {
+        return AudioSharingUtils.isFeatureEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
+    }
+
+    @Override
     public String getPreferenceKey() {
         return PREF_KEY;
     }
@@ -53,16 +57,4 @@
     public boolean isTextValid(String value) {
         return mAudioSharingNameTextValidator.isTextValid(value);
     }
-
-    @Override
-    public void onStart(@NonNull LifecycleOwner owner) {
-        super.onStart(owner);
-        // TODO
-    }
-
-    @Override
-    public void onStop(@NonNull LifecycleOwner owner) {
-        super.onStop(owner);
-        // TODO
-    }
 }
diff --git a/src/com/android/settings/connecteddevice/audiosharing/StreamSettingsCategoryController.java b/src/com/android/settings/connecteddevice/audiosharing/StreamSettingsCategoryController.java
new file mode 100644
index 0000000..f62183d
--- /dev/null
+++ b/src/com/android/settings/connecteddevice/audiosharing/StreamSettingsCategoryController.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 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.connecteddevice.audiosharing;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.util.Log;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.lifecycle.DefaultLifecycleObserver;
+import androidx.lifecycle.LifecycleOwner;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.core.BasePreferenceController;
+
+public class StreamSettingsCategoryController extends BasePreferenceController
+        implements DefaultLifecycleObserver {
+    private static final String TAG = "StreamSettingsCategoryController";
+    private final BluetoothAdapter mBluetoothAdapter;
+    private final IntentFilter mIntentFilter;
+    private @Nullable Preference mPreference;
+    private BroadcastReceiver mReceiver =
+            new BroadcastReceiver() {
+                @Override
+                public void onReceive(Context context, Intent intent) {
+                    if (!BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) return;
+                    int adapterState =
+                            intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR);
+                    mContext.getMainExecutor()
+                            .execute(
+                                    () -> {
+                                        if (mPreference == null) {
+                                            Log.w(
+                                                    TAG,
+                                                    "Skip BT state change due to mPreference "
+                                                            + "is null");
+                                        } else {
+                                            mPreference.setVisible(
+                                                    adapterState == BluetoothAdapter.STATE_ON);
+                                        }
+                                    });
+                }
+            };
+
+    public StreamSettingsCategoryController(Context context, String key) {
+        super(context, key);
+        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
+        mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
+    }
+
+    @Override
+    public void onStart(@NonNull LifecycleOwner owner) {
+        mContext.registerReceiver(mReceiver, mIntentFilter, Context.RECEIVER_EXPORTED_UNAUDITED);
+    }
+
+    @Override
+    public void onStop(@NonNull LifecycleOwner owner) {
+        mContext.unregisterReceiver(mReceiver);
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mPreference = screen.findPreference(getPreferenceKey());
+        if (mPreference != null) {
+            mPreference.setVisible(isBluetoothStateOn());
+        }
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AudioSharingUtils.isFeatureEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
+    }
+
+    private boolean isBluetoothStateOn() {
+        return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();
+    }
+}