[Audiosharing] Impl a base controller for audio sharing page.

The preference controllers attached to audio sharing page will extends
this base controller so that their visibility will be updated together
depending on the main switch state.

Flagged with enable_le_audio_sharing

Bug: 305620450
Test: Manual
Change-Id: I3d37c87a128098a1b1a38cb8621be9a4d4e8c15e
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingBasePreferenceController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingBasePreferenceController.java
new file mode 100644
index 0000000..9ebe26d
--- /dev/null
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingBasePreferenceController.java
@@ -0,0 +1,70 @@
+/*
+ * 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.connecteddevice.audiosharing;
+
+import android.content.Context;
+
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.bluetooth.Utils;
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.flags.Flags;
+import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
+import com.android.settingslib.bluetooth.LocalBluetoothManager;
+
+public abstract class AudioSharingBasePreferenceController extends BasePreferenceController {
+    private final LocalBluetoothManager mBtManager;
+    protected final LocalBluetoothLeBroadcast mBroadcast;
+    protected Preference mPreference;
+
+    public AudioSharingBasePreferenceController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mBtManager = Utils.getLocalBtManager(context);
+        mBroadcast =
+                mBtManager == null
+                        ? null
+                        : mBtManager.getProfileManager().getLeAudioBroadcastProfile();
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return mBtManager != null && Flags.enableLeAudioSharing()
+                ? AVAILABLE
+                : UNSUPPORTED_ON_DEVICE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mPreference = screen.findPreference(getPreferenceKey());
+        updateVisibility(isBroadcasting());
+    }
+
+    /**
+     * Update the visibility of the preference.
+     *
+     * @param isVisible the latest visibility state for the preference.
+     */
+    public void updateVisibility(boolean isVisible) {
+        mPreference.setVisible(isVisible);
+    }
+
+    private boolean isBroadcasting() {
+        return mBroadcast != null && mBroadcast.isEnabled(null);
+    }
+}
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDashboardFragment.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDashboardFragment.java
index b3b7a2c..40207be 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDashboardFragment.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDashboardFragment.java
@@ -25,11 +25,14 @@
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.widget.SettingsMainSwitchBar;
 
-public class AudioSharingDashboardFragment extends DashboardFragment {
+public class AudioSharingDashboardFragment extends DashboardFragment
+        implements AudioSharingSwitchBarController.OnSwitchBarChangedListener {
     private static final String TAG = "AudioSharingDashboardFrag";
 
     SettingsMainSwitchBar mMainSwitchBar;
     private AudioSharingSwitchBarController mSwitchBarController;
+    private CallsAndAlarmsPreferenceController mCallsAndAlarmsPreferenceController;
+    private AudioSharingNamePreferenceController mAudioSharingNamePreferenceController;
 
     public AudioSharingDashboardFragment() {
         super();
@@ -63,7 +66,9 @@
     @Override
     public void onAttach(Context context) {
         super.onAttach(context);
-        use(CallsAndAlarmsPreferenceController.class).init(this);
+        mCallsAndAlarmsPreferenceController = use(CallsAndAlarmsPreferenceController.class);
+        mCallsAndAlarmsPreferenceController.init(this);
+        mAudioSharingNamePreferenceController = use(AudioSharingNamePreferenceController.class);
     }
 
     @Override
@@ -74,9 +79,19 @@
         final SettingsActivity activity = (SettingsActivity) getActivity();
         mMainSwitchBar = activity.getSwitchBar();
         mMainSwitchBar.setTitle(getText(R.string.audio_sharing_switch_title));
-        mSwitchBarController = new AudioSharingSwitchBarController(activity, mMainSwitchBar);
+        mSwitchBarController = new AudioSharingSwitchBarController(activity, mMainSwitchBar, this);
         mSwitchBarController.init(this);
         getSettingsLifecycle().addObserver(mSwitchBarController);
         mMainSwitchBar.show();
     }
+
+    @Override
+    public void onSwitchBarChanged(boolean newState) {
+        updateVisibilityForAttachedPreferences(newState);
+    }
+
+    private void updateVisibilityForAttachedPreferences(boolean isVisible) {
+        mCallsAndAlarmsPreferenceController.updateVisibility(isVisible);
+        mAudioSharingNamePreferenceController.updateVisibility(isVisible);
+    }
 }
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingNamePreferenceController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingNamePreferenceController.java
index 18c9bfd..8336691 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingNamePreferenceController.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingNamePreferenceController.java
@@ -22,13 +22,10 @@
 import androidx.lifecycle.DefaultLifecycleObserver;
 import androidx.lifecycle.LifecycleOwner;
 import androidx.preference.Preference;
-import androidx.preference.PreferenceScreen;
 
-import com.android.settings.core.BasePreferenceController;
-import com.android.settings.flags.Flags;
 import com.android.settings.widget.ValidatedEditTextPreference;
 
-public class AudioSharingNamePreferenceController extends BasePreferenceController
+public class AudioSharingNamePreferenceController extends AudioSharingBasePreferenceController
         implements ValidatedEditTextPreference.Validator,
                 Preference.OnPreferenceChangeListener,
                 DefaultLifecycleObserver {
@@ -37,8 +34,6 @@
 
     private static final String PREF_KEY = "audio_sharing_stream_name";
 
-    protected Preference mPreference;
-
     private AudioSharingNameTextValidator mAudioSharingNameTextValidator;
 
     public AudioSharingNamePreferenceController(Context context) {
@@ -47,11 +42,6 @@
     }
 
     @Override
-    public int getAvailabilityStatus() {
-        return Flags.enableLeAudioSharing() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
-    }
-
-    @Override
     public String getPreferenceKey() {
         return PREF_KEY;
     }
@@ -63,12 +53,6 @@
     }
 
     @Override
-    public void displayPreference(PreferenceScreen screen) {
-        super.displayPreference(screen);
-        mPreference = screen.findPreference(getPreferenceKey());
-    }
-
-    @Override
     public boolean isTextValid(String value) {
         return mAudioSharingNameTextValidator.isTextValid(value);
     }
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java
index 83367ae..3f9f48e 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingSwitchBarController.java
@@ -58,11 +58,17 @@
         implements DefaultLifecycleObserver, OnCheckedChangeListener {
     private static final String TAG = "AudioSharingSwitchBarCtl";
     private static final String PREF_KEY = "audio_sharing_main_switch";
+
+    interface OnSwitchBarChangedListener {
+        void onSwitchBarChanged(boolean newState);
+    }
+
     private final SettingsMainSwitchBar mSwitchBar;
     private final LocalBluetoothManager mBtManager;
     private final LocalBluetoothLeBroadcast mBroadcast;
     private final LocalBluetoothLeBroadcastAssistant mAssistant;
     private final Executor mExecutor;
+    private final OnSwitchBarChangedListener mListener;
     private DashboardFragment mFragment;
     private List<BluetoothDevice> mTargetSinks = new ArrayList<>();
 
@@ -196,9 +202,11 @@
                         BluetoothLeBroadcastReceiveState state) {}
             };
 
-    AudioSharingSwitchBarController(Context context, SettingsMainSwitchBar switchBar) {
+    AudioSharingSwitchBarController(
+            Context context, SettingsMainSwitchBar switchBar, OnSwitchBarChangedListener listener) {
         super(context, PREF_KEY);
         mSwitchBar = switchBar;
+        mListener = listener;
         mBtManager = Utils.getLocalBtManager(context);
         mBroadcast = mBtManager.getProfileManager().getLeAudioBroadcastProfile();
         mAssistant = mBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
@@ -326,8 +334,12 @@
     private void updateSwitch() {
         ThreadUtils.postOnMainThread(
                 () -> {
-                    mSwitchBar.setChecked(isBroadcasting());
+                    boolean isBroadcasting = isBroadcasting();
+                    if (mSwitchBar.isChecked() != isBroadcasting) {
+                        mSwitchBar.setChecked(isBroadcasting);
+                    }
                     mSwitchBar.setEnabled(true);
+                    mListener.onSwitchBarChanged(isBroadcasting);
                 });
     }
 
diff --git a/src/com/android/settings/connecteddevice/audiosharing/CallsAndAlarmsPreferenceController.java b/src/com/android/settings/connecteddevice/audiosharing/CallsAndAlarmsPreferenceController.java
index 480b257..44e75ec 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/CallsAndAlarmsPreferenceController.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/CallsAndAlarmsPreferenceController.java
@@ -19,21 +19,16 @@
 import android.content.Context;
 import android.util.Log;
 
-import androidx.preference.Preference;
 import androidx.preference.PreferenceScreen;
 
-import com.android.settings.core.BasePreferenceController;
 import com.android.settings.dashboard.DashboardFragment;
-import com.android.settings.flags.Flags;
 
 /** PreferenceController to control the dialog to choose the active device for calls and alarms */
-public class CallsAndAlarmsPreferenceController extends BasePreferenceController {
+public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferenceController {
 
     private static final String TAG = "CallsAndAlarmsPreferenceController";
 
     private static final String PREF_KEY = "calls_and_alarms";
-
-    private Preference mPreference;
     private DashboardFragment mFragment;
 
     public CallsAndAlarmsPreferenceController(Context context) {
@@ -41,11 +36,6 @@
     }
 
     @Override
-    public int getAvailabilityStatus() {
-        return Flags.enableLeAudioSharing() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
-    }
-
-    @Override
     public String getPreferenceKey() {
         return PREF_KEY;
     }
@@ -53,7 +43,6 @@
     @Override
     public void displayPreference(PreferenceScreen screen) {
         super.displayPreference(screen);
-        mPreference = screen.findPreference(getPreferenceKey());
         mPreference.setOnPreferenceClickListener(
                 preference -> {
                     if (mFragment != null) {