Merge "Add check to verify if separate lock is setup for private space." into main
diff --git a/aconfig/settings_bluetooth_declarations.aconfig b/aconfig/settings_bluetooth_declarations.aconfig
index 8414309..58ddd25 100644
--- a/aconfig/settings_bluetooth_declarations.aconfig
+++ b/aconfig/settings_bluetooth_declarations.aconfig
@@ -6,3 +6,10 @@
   description: "Gates whether to offload bluetooth operations to background thread"
   bug: "305636727"
 }
+
+flag {
+  name: "enable_bluetooth_profile_toggle_visibility_checker"
+  namespace: "pixel_cross_device_control"
+  description: "Gates whether to enable checker for bluetooth profile toggle visibility"
+  bug: "321178209"
+}
\ No newline at end of file
diff --git a/src/com/android/settings/bluetooth/BluetoothDetailsProfilesController.java b/src/com/android/settings/bluetooth/BluetoothDetailsProfilesController.java
index 3b162b6..943d99b 100644
--- a/src/com/android/settings/bluetooth/BluetoothDetailsProfilesController.java
+++ b/src/com/android/settings/bluetooth/BluetoothDetailsProfilesController.java
@@ -37,6 +37,8 @@
 
 import com.android.settings.R;
 import com.android.settings.core.SettingsUIDeviceConfig;
+import com.android.settings.flags.Flags;
+import com.android.settings.overlay.FeatureFactory;
 import com.android.settingslib.bluetooth.A2dpProfile;
 import com.android.settingslib.bluetooth.BluetoothUtils;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -49,11 +51,14 @@
 import com.android.settingslib.bluetooth.PanProfile;
 import com.android.settingslib.bluetooth.PbapServerProfile;
 import com.android.settingslib.core.lifecycle.Lifecycle;
+import com.android.settingslib.utils.ThreadUtils;
 
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * This class adds switches for toggling the individual profiles that a Bluetooth device
@@ -79,6 +84,8 @@
     private static final String LE_AUDIO_TOGGLE_VISIBLE_PROPERTY =
             "persist.bluetooth.leaudio.toggle_visible";
 
+    private final AtomicReference<Set<String>> mInvisiblePreferenceKey = new AtomicReference<>();
+
     private LocalBluetoothManager mManager;
     private LocalBluetoothProfileManager mProfileManager;
     private CachedBluetoothDevice mCachedDevice;
@@ -547,6 +554,22 @@
      */
     @Override
     protected void refresh() {
+        if (Flags.enableBluetoothProfileToggleVisibilityChecker()) {
+            ThreadUtils.postOnBackgroundThread(
+                    () -> {
+                        mInvisiblePreferenceKey.set(
+                                FeatureFactory.getFeatureFactory()
+                                        .getBluetoothFeatureProvider()
+                                        .getInvisibleProfilePreferenceKeys(
+                                                mContext, mCachedDevice.getDevice()));
+                        ThreadUtils.postOnMainThread(this::refreshUi);
+                    });
+        } else {
+            refreshUi();
+        }
+    }
+
+    private void refreshUi() {
         for (LocalBluetoothProfile profile : getProfiles()) {
             if (profile == null || !profile.isProfileReady()) {
                 continue;
@@ -577,6 +600,16 @@
             preference.setSelectable(false);
             mProfilesContainer.addPreference(preference);
         }
+
+        if (Flags.enableBluetoothProfileToggleVisibilityChecker()) {
+            Set<String> invisibleKeys = mInvisiblePreferenceKey.get();
+            if (invisibleKeys != null) {
+                for (int i = 0; i < mProfilesContainer.getPreferenceCount(); ++i) {
+                    Preference pref = mProfilesContainer.getPreference(i);
+                    pref.setVisible(pref.isVisible() && !invisibleKeys.contains(pref.getKey()));
+                }
+            }
+        }
     }
 
     @Override
diff --git a/src/com/android/settings/bluetooth/BluetoothFeatureProvider.java b/src/com/android/settings/bluetooth/BluetoothFeatureProvider.java
index d44e4b2..1751082 100644
--- a/src/com/android/settings/bluetooth/BluetoothFeatureProvider.java
+++ b/src/com/android/settings/bluetooth/BluetoothFeatureProvider.java
@@ -27,6 +27,7 @@
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
 
 import java.util.List;
+import java.util.Set;
 
 /**
  * Provider for bluetooth related features.
@@ -73,4 +74,14 @@
      * @return the extra bluetooth preference list
      */
     List<Preference> getBluetoothExtraOptions(Context context, CachedBluetoothDevice device);
+
+    /**
+     * Gets the bluetooth profile preference keys which should be hidden in the device details page.
+     *
+     * @param context         Context
+     * @param bluetoothDevice the bluetooth device
+     * @return the profiles which should be hidden
+     */
+    Set<String> getInvisibleProfilePreferenceKeys(
+            Context context, BluetoothDevice bluetoothDevice);
 }
diff --git a/src/com/android/settings/bluetooth/BluetoothFeatureProviderImpl.java b/src/com/android/settings/bluetooth/BluetoothFeatureProviderImpl.java
index 014a0f3..2d4ac49 100644
--- a/src/com/android/settings/bluetooth/BluetoothFeatureProviderImpl.java
+++ b/src/com/android/settings/bluetooth/BluetoothFeatureProviderImpl.java
@@ -29,8 +29,10 @@
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 
 import java.util.List;
+import java.util.Set;
 
 /**
  * Impl of {@link BluetoothFeatureProvider}
@@ -65,4 +67,10 @@
             CachedBluetoothDevice device) {
         return ImmutableList.of();
     }
+
+    @Override
+    public Set<String> getInvisibleProfilePreferenceKeys(
+            Context context, BluetoothDevice bluetoothDevice) {
+        return ImmutableSet.of();
+    }
 }
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDevicePreferenceController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDevicePreferenceController.java
index 84b63b1..3fd5127 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDevicePreferenceController.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDevicePreferenceController.java
@@ -418,7 +418,7 @@
         if (isBroadcasting()) {
             // Show stop audio sharing dialog when an ineligible (non LE audio) remote device
             // connected during a sharing session.
-            ThreadUtils.postOnMainThread(
+            postOnMainThread(
                     () -> {
                         closeOpeningDialogs();
                         AudioSharingStopDialogFragment.show(
@@ -442,8 +442,9 @@
         Map<Integer, List<CachedBluetoothDevice>> groupedDevices =
                 AudioSharingUtils.fetchConnectedDevicesByGroupId(mLocalBtManager);
         if (isBroadcasting()) {
-            if (groupedDevices.containsKey(cachedDevice.getGroupId())
-                    && groupedDevices.get(cachedDevice.getGroupId()).stream()
+            int groupId = AudioSharingUtils.getGroupId(cachedDevice);
+            if (groupedDevices.containsKey(groupId)
+                    && groupedDevices.get(groupId).stream()
                             .anyMatch(
                                     device ->
                                             AudioSharingUtils.hasBroadcastSource(
@@ -463,7 +464,7 @@
             // Show audio sharing switch dialog when the third eligible (LE audio) remote device
             // connected during a sharing session.
             if (deviceItemsInSharingSession.size() >= 2) {
-                ThreadUtils.postOnMainThread(
+                postOnMainThread(
                         () -> {
                             closeOpeningDialogs();
                             AudioSharingDisconnectDialogFragment.show(
@@ -494,7 +495,7 @@
             } else {
                 // Show audio sharing join dialog when the first or second eligible (LE audio)
                 // remote device connected during a sharing session.
-                ThreadUtils.postOnMainThread(
+                postOnMainThread(
                         () -> {
                             closeOpeningDialogs();
                             AudioSharingJoinDialogFragment.show(
@@ -515,7 +516,8 @@
             for (List<CachedBluetoothDevice> devices : groupedDevices.values()) {
                 // Use random device in the group within the sharing session to represent the group.
                 CachedBluetoothDevice device = devices.get(0);
-                if (device.getGroupId() == cachedDevice.getGroupId()) {
+                if (AudioSharingUtils.getGroupId(device)
+                        == AudioSharingUtils.getGroupId(cachedDevice)) {
                     continue;
                 }
                 deviceItems.add(AudioSharingUtils.buildAudioSharingDeviceItem(device));
@@ -523,7 +525,7 @@
             // Show audio sharing join dialog when the second eligible (LE audio) remote
             // device connect and no sharing session.
             if (deviceItems.size() == 1) {
-                ThreadUtils.postOnMainThread(
+                postOnMainThread(
                         () -> {
                             closeOpeningDialogs();
                             AudioSharingJoinDialogFragment.show(
@@ -599,4 +601,8 @@
             }
         }
     }
+
+    private void postOnMainThread(@NonNull Runnable runnable) {
+        mContext.getMainExecutor().execute(runnable);
+    }
 }
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumeGroupController.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumeGroupController.java
index 3396b8b..edd1caf 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumeGroupController.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDeviceVolumeGroupController.java
@@ -77,11 +77,12 @@
                     CachedBluetoothDevice cachedDevice =
                             mLocalBtManager.getCachedDeviceManager().findDevice(device);
                     if (cachedDevice == null) return;
-                    mValueMap.put(cachedDevice.getGroupId(), volume);
+                    int groupId = AudioSharingUtils.getGroupId(cachedDevice);
+                    mValueMap.put(groupId, volume);
                     for (AudioSharingDeviceVolumePreference preference : mVolumePreferences) {
                         if (preference.getCachedDevice() != null
-                                && preference.getCachedDevice().getGroupId()
-                                        == cachedDevice.getGroupId()) {
+                                && AudioSharingUtils.getGroupId(preference.getCachedDevice())
+                                        == groupId) {
                             // If the callback return invalid volume, try to
                             // get the volume from AudioManager.STREAM_MUSIC
                             int finalVolume = getAudioVolumeIfNeeded(volume);
@@ -270,7 +271,7 @@
             if (volumePref.getProgress() > 0) return;
             CachedBluetoothDevice device = volumePref.getCachedDevice();
             if (device == null) return;
-            int volume = mValueMap.getOrDefault(device.getGroupId(), -1);
+            int volume = mValueMap.getOrDefault(AudioSharingUtils.getGroupId(device), -1);
             // If the volume is invalid, try to get the volume from AudioManager.STREAM_MUSIC
             int finalVolume = getAudioVolumeIfNeeded(volume);
             Log.d(
diff --git a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingUtils.java b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingUtils.java
index 3d4ef82..924b04d 100644
--- a/src/com/android/settings/connecteddevice/audiosharing/AudioSharingUtils.java
+++ b/src/com/android/settings/connecteddevice/audiosharing/AudioSharingUtils.java
@@ -29,10 +29,11 @@
 import com.android.settingslib.bluetooth.BluetoothUtils;
 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
+import com.android.settingslib.bluetooth.LeAudioProfile;
 import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
 import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
-import com.android.settingslib.utils.ThreadUtils;
+import com.android.settingslib.bluetooth.LocalBluetoothProfile;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -57,10 +58,16 @@
     public static Map<Integer, List<CachedBluetoothDevice>> fetchConnectedDevicesByGroupId(
             LocalBluetoothManager localBtManager) {
         Map<Integer, List<CachedBluetoothDevice>> groupedDevices = new HashMap<>();
+        if (localBtManager == null) {
+            Log.d(TAG, "Skip fetchConnectedDevicesByGroupId due to bt manager is null");
+            return groupedDevices;
+        }
         LocalBluetoothLeBroadcastAssistant assistant =
                 localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
-        if (assistant == null) return groupedDevices;
-        // TODO: filter out devices with le audio disabled.
+        if (assistant == null) {
+            Log.d(TAG, "Skip fetchConnectedDevicesByGroupId due to assistant profile is null");
+            return groupedDevices;
+        }
         List<BluetoothDevice> connectedDevices = assistant.getConnectedDevices();
         CachedBluetoothDeviceManager cacheManager = localBtManager.getCachedDeviceManager();
         for (BluetoothDevice device : connectedDevices) {
@@ -69,7 +76,7 @@
                 Log.d(TAG, "Skip device due to not being cached: " + device.getAnonymizedAddress());
                 continue;
             }
-            int groupId = cachedDevice.getGroupId();
+            int groupId = getGroupId(cachedDevice);
             if (groupId == BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
                 Log.d(
                         TAG,
@@ -105,9 +112,6 @@
             Map<Integer, List<CachedBluetoothDevice>> groupedConnectedDevices,
             boolean filterByInSharing) {
         List<CachedBluetoothDevice> orderedDevices = new ArrayList<>();
-        LocalBluetoothLeBroadcastAssistant assistant =
-                localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
-        if (assistant == null) return orderedDevices;
         for (List<CachedBluetoothDevice> devices : groupedConnectedDevices.values()) {
             CachedBluetoothDevice leadDevice = null;
             for (CachedBluetoothDevice device : devices) {
@@ -191,7 +195,7 @@
             CachedBluetoothDevice cachedDevice) {
         return new AudioSharingDeviceItem(
                 cachedDevice.getName(),
-                cachedDevice.getGroupId(),
+                getGroupId(cachedDevice),
                 isActiveLeAudioDevice(cachedDevice));
     }
 
@@ -204,19 +208,36 @@
      */
     public static boolean hasBroadcastSource(
             CachedBluetoothDevice cachedDevice, LocalBluetoothManager localBtManager) {
+        if (localBtManager == null) {
+            Log.d(TAG, "Skip check hasBroadcastSource due to bt manager is null");
+            return false;
+        }
         LocalBluetoothLeBroadcastAssistant assistant =
                 localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
         if (assistant == null) {
+            Log.d(TAG, "Skip check hasBroadcastSource due to assistant profile is null");
             return false;
         }
         List<BluetoothLeBroadcastReceiveState> sourceList =
                 assistant.getAllSources(cachedDevice.getDevice());
-        if (!sourceList.isEmpty()) return true;
+        if (!sourceList.isEmpty()) {
+            Log.d(
+                    TAG,
+                    "Lead device has broadcast source, device = "
+                            + cachedDevice.getDevice().getAnonymizedAddress());
+            return true;
+        }
         // Return true if member device is in broadcast.
         for (CachedBluetoothDevice device : cachedDevice.getMemberDevice()) {
             List<BluetoothLeBroadcastReceiveState> list =
                     assistant.getAllSources(device.getDevice());
-            if (!list.isEmpty()) return true;
+            if (!list.isEmpty()) {
+                Log.d(
+                        TAG,
+                        "Member device has broadcast source, device = "
+                                + device.getDevice().getAnonymizedAddress());
+                return true;
+            }
         }
         return false;
     }
@@ -257,8 +278,8 @@
 
     /** Toast message on main thread. */
     public static void toastMessage(Context context, String message) {
-        ThreadUtils.postOnMainThread(
-                () -> Toast.makeText(context, message, Toast.LENGTH_LONG).show());
+        context.getMainExecutor()
+                .execute(() -> Toast.makeText(context, message, Toast.LENGTH_LONG).show());
     }
 
     /** Returns if the le audio sharing is enabled. */
@@ -273,7 +294,10 @@
 
     /** Automatically update active device if needed. */
     public static void updateActiveDeviceIfNeeded(LocalBluetoothManager localBtManager) {
-        if (localBtManager == null) return;
+        if (localBtManager == null) {
+            Log.d(TAG, "Skip updateActiveDeviceIfNeeded due to bt manager is null");
+            return;
+        }
         Map<Integer, List<CachedBluetoothDevice>> groupedConnectedDevices =
                 fetchConnectedDevicesByGroupId(localBtManager);
         List<CachedBluetoothDevice> devicesInSharing =
@@ -283,6 +307,7 @@
         List<BluetoothDevice> devices =
                 BluetoothAdapter.getDefaultAdapter().getMostRecentlyConnectedDevices();
         CachedBluetoothDevice targetDevice = null;
+        // Find the earliest connected device in sharing session.
         int targetDeviceIdx = -1;
         for (CachedBluetoothDevice device : devicesInSharing) {
             if (devices.contains(device.getDevice())) {
@@ -299,6 +324,14 @@
                     "updateActiveDeviceIfNeeded, set active device: "
                             + targetDevice.getDevice().getAnonymizedAddress());
             targetDevice.setActive();
+        } else {
+            Log.d(
+                    TAG,
+                    "updateActiveDeviceIfNeeded, skip set active device: "
+                            + (targetDevice == null
+                                    ? "null"
+                                    : (targetDevice.getDevice().getAnonymizedAddress()
+                                            + " is already active")));
         }
     }
 
@@ -312,9 +345,38 @@
 
     /** Stops the latest broadcast. */
     public static void stopBroadcasting(LocalBluetoothManager manager) {
-        if (manager == null) return;
+        if (manager == null) {
+            Log.d(TAG, "Skip stop broadcasting due to bt manager is null");
+            return;
+        }
         LocalBluetoothLeBroadcast broadcast =
                 manager.getProfileManager().getLeAudioBroadcastProfile();
+        if (broadcast == null) {
+            Log.d(TAG, "Skip stop broadcasting due to broadcast profile is null");
+        }
         broadcast.stopBroadcast(broadcast.getLatestBroadcastId());
     }
+
+    /**
+     * Get CSIP group id for {@link CachedBluetoothDevice}.
+     *
+     * <p>If CachedBluetoothDevice#getGroupId is invalid, fetch group id from
+     * LeAudioProfile#getGroupId.
+     */
+    public static int getGroupId(CachedBluetoothDevice cachedDevice) {
+        int groupId = cachedDevice.getGroupId();
+        String anonymizedAddress = cachedDevice.getDevice().getAnonymizedAddress();
+        if (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
+            Log.d(TAG, "getGroupId by CSIP profile for device: " + anonymizedAddress);
+            return groupId;
+        }
+        for (LocalBluetoothProfile profile : cachedDevice.getProfiles()) {
+            if (profile instanceof LeAudioProfile) {
+                Log.d(TAG, "getGroupId by LEA profile for device: " + anonymizedAddress);
+                return ((LeAudioProfile) profile).getGroupId(cachedDevice.getDevice());
+            }
+        }
+        Log.d(TAG, "getGroupId return invalid id for device: " + anonymizedAddress);
+        return BluetoothCsipSetCoordinator.GROUP_ID_INVALID;
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsProfilesControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsProfilesControllerTest.java
index ad7c984..9b1466b 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsProfilesControllerTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsProfilesControllerTest.java
@@ -18,6 +18,7 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
@@ -28,23 +29,33 @@
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothProfile;
 import android.content.Context;
+import android.platform.test.flag.junit.CheckFlagsRule;
+import android.platform.test.flag.junit.DeviceFlagsValueProvider;
+import android.platform.test.flag.junit.SetFlagsRule;
 import android.sysprop.BluetoothProperties;
 
 import androidx.preference.Preference;
 import androidx.preference.PreferenceCategory;
-import androidx.preference.SwitchPreference;
+import androidx.preference.SwitchPreferenceCompat;
 
+import com.android.settings.flags.Flags;
+import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settings.testutils.shadow.ShadowBluetoothDevice;
+import com.android.settingslib.R;
 import com.android.settingslib.bluetooth.A2dpProfile;
+import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
+import com.android.settingslib.bluetooth.LeAudioProfile;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
 import com.android.settingslib.bluetooth.LocalBluetoothProfile;
 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
 import com.android.settingslib.bluetooth.MapProfile;
 import com.android.settingslib.bluetooth.PbapServerProfile;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Lists;
 
-import org.junit.Ignore;
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
@@ -59,30 +70,41 @@
 import java.util.Set;
 
 @RunWith(RobolectricTestRunner.class)
-@Ignore
 @Config(shadows = ShadowBluetoothDevice.class)
 public class BluetoothDetailsProfilesControllerTest extends BluetoothDetailsControllerTestBase {
+    @Rule
+    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
+    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
 
     private static final String LE_DEVICE_MODEL = "le_audio_headset";
     private static final String NON_LE_DEVICE_MODEL = "non_le_audio_headset";
     private BluetoothDetailsProfilesController mController;
     private List<LocalBluetoothProfile> mConnectableProfiles;
     private PreferenceCategory mProfiles;
+    private BluetoothFeatureProvider mFeatureProvider;
 
     @Mock
     private LocalBluetoothManager mLocalManager;
     @Mock
     private LocalBluetoothProfileManager mProfileManager;
+    @Mock
+    private CachedBluetoothDeviceManager mCachedBluetoothDeviceManager;
 
     @Override
     public void setUp() {
         super.setUp();
 
+        FakeFeatureFactory fakeFeatureFactory = FakeFeatureFactory.setupForTest();
+        mFeatureProvider = fakeFeatureFactory.getBluetoothFeatureProvider();
+
         mProfiles = spy(new PreferenceCategory(mContext));
         when(mProfiles.getPreferenceManager()).thenReturn(mPreferenceManager);
 
         mConnectableProfiles = new ArrayList<>();
         when(mLocalManager.getProfileManager()).thenReturn(mProfileManager);
+        when(mLocalManager.getCachedDeviceManager()).thenReturn(mCachedBluetoothDeviceManager);
+        when(mCachedBluetoothDeviceManager.getCachedDevicesCopy())
+                .thenReturn(ImmutableList.of(mCachedDevice));
         when(mCachedDevice.getConnectableProfiles()).thenAnswer(invocation ->
             new ArrayList<>(mConnectableProfiles)
         );
@@ -196,25 +218,26 @@
         return profile;
     }
 
-    /** Returns the list of SwitchPreference objects added to the screen - there should be one per
-     *  Bluetooth profile.
+    /**
+     * Returns the list of SwitchPreferenceCompat objects added to the screen - there should be one
+     * per Bluetooth profile.
      */
-    private List<SwitchPreference> getProfileSwitches(boolean expectOnlyMConnectable) {
+    private List<SwitchPreferenceCompat> getProfileSwitches(boolean expectOnlyMConnectable) {
         if (expectOnlyMConnectable) {
             assertThat(mConnectableProfiles).isNotEmpty();
             assertThat(mProfiles.getPreferenceCount() - 1).isEqualTo(mConnectableProfiles.size());
         }
-        List<SwitchPreference> result = new ArrayList<>();
+        List<SwitchPreferenceCompat> result = new ArrayList<>();
         for (int i = 0; i < mProfiles.getPreferenceCount(); i++) {
             final Preference preference = mProfiles.getPreference(i);
-            if (preference instanceof SwitchPreference) {
-                result.add((SwitchPreference) preference);
+            if (preference instanceof SwitchPreferenceCompat) {
+                result.add((SwitchPreferenceCompat) preference);
             }
         }
         return result;
     }
 
-     private void verifyProfileSwitchTitles(List<SwitchPreference> switches) {
+    private void verifyProfileSwitchTitles(List<SwitchPreferenceCompat> switches) {
         for (int i = 0; i < switches.size(); i++) {
             String expectedTitle =
                 mContext.getString(mConnectableProfiles.get(i).getNameResource(mDevice));
@@ -234,7 +257,7 @@
         addFakeProfile(com.android.settingslib.R.string.bluetooth_profile_a2dp, true);
         addFakeProfile(com.android.settingslib.R.string.bluetooth_profile_headset, false);
         showScreen(mController);
-        List<SwitchPreference> switches = getProfileSwitches(true);
+        List<SwitchPreferenceCompat> switches = getProfileSwitches(true);
         verifyProfileSwitchTitles(switches);
         assertThat(switches.get(0).isChecked()).isTrue();
         assertThat(switches.get(1).isChecked()).isFalse();
@@ -260,8 +283,8 @@
         addFakeProfile(com.android.settingslib.R.string.bluetooth_profile_a2dp, true);
         addFakeProfile(com.android.settingslib.R.string.bluetooth_profile_headset, true);
         showScreen(mController);
-        List<SwitchPreference> switches = getProfileSwitches(true);
-        SwitchPreference pref = switches.get(0);
+        List<SwitchPreferenceCompat> switches = getProfileSwitches(true);
+        SwitchPreferenceCompat pref = switches.get(0);
 
         // Clicking the pref should cause the profile to become not-preferred.
         assertThat(pref.isChecked()).isTrue();
@@ -296,14 +319,16 @@
         PbapServerProfile psp = mock(PbapServerProfile.class);
         when(psp.getNameResource(mDevice))
                 .thenReturn(com.android.settingslib.R.string.bluetooth_profile_pbap);
+        when(psp.getSummaryResourceForDevice(mDevice))
+                .thenReturn(R.string.bluetooth_profile_pbap_summary);
         when(psp.toString()).thenReturn(PbapServerProfile.NAME);
         when(psp.isProfileReady()).thenReturn(true);
         when(mProfileManager.getPbapProfile()).thenReturn(psp);
 
         showScreen(mController);
-        List<SwitchPreference> switches = getProfileSwitches(false);
+        List<SwitchPreferenceCompat> switches = getProfileSwitches(false);
         assertThat(switches.size()).isEqualTo(1);
-        SwitchPreference pref = switches.get(0);
+        SwitchPreferenceCompat pref = switches.get(0);
         assertThat(pref.getTitle()).isEqualTo(
                 mContext.getString(com.android.settingslib.R.string.bluetooth_profile_pbap));
         assertThat(pref.isChecked()).isTrue();
@@ -321,14 +346,16 @@
         PbapServerProfile psp = mock(PbapServerProfile.class);
         when(psp.getNameResource(mDevice))
                 .thenReturn(com.android.settingslib.R.string.bluetooth_profile_pbap);
+        when(psp.getSummaryResourceForDevice(mDevice))
+                .thenReturn(R.string.bluetooth_profile_pbap_summary);
         when(psp.toString()).thenReturn(PbapServerProfile.NAME);
         when(psp.isProfileReady()).thenReturn(true);
         when(mProfileManager.getPbapProfile()).thenReturn(psp);
 
         showScreen(mController);
-        List<SwitchPreference> switches = getProfileSwitches(false);
+        List<SwitchPreferenceCompat> switches = getProfileSwitches(false);
         assertThat(switches.size()).isEqualTo(1);
-        SwitchPreference pref = switches.get(0);
+        SwitchPreferenceCompat pref = switches.get(0);
         assertThat(pref.getTitle()).isEqualTo(
                 mContext.getString(com.android.settingslib.R.string.bluetooth_profile_pbap));
         assertThat(pref.isChecked()).isFalse();
@@ -350,9 +377,9 @@
         when(mProfileManager.getProfileByName(eq(mapProfile.toString()))).thenReturn(mapProfile);
         mDevice.setMessageAccessPermission(BluetoothDevice.ACCESS_REJECTED);
         showScreen(mController);
-        List<SwitchPreference> switches = getProfileSwitches(false);
+        List<SwitchPreferenceCompat> switches = getProfileSwitches(false);
         assertThat(switches.size()).isEqualTo(1);
-        SwitchPreference pref = switches.get(0);
+        SwitchPreferenceCompat pref = switches.get(0);
         assertThat(pref.getTitle()).isEqualTo(
                 mContext.getString(com.android.settingslib.R.string.bluetooth_profile_map));
         assertThat(pref.isChecked()).isFalse();
@@ -379,8 +406,8 @@
         return profile;
     }
 
-    private SwitchPreference getHighQualityAudioPref() {
-        return (SwitchPreference) mScreen.findPreference(
+    private SwitchPreferenceCompat getHighQualityAudioPref() {
+        return (SwitchPreferenceCompat) mScreen.findPreference(
                 BluetoothDetailsProfilesController.HIGH_QUALITY_AUDIO_PREF_TAG);
     }
 
@@ -389,7 +416,7 @@
         setupDevice(makeDefaultDeviceConfig());
         addMockA2dpProfile(true, true, true);
         showScreen(mController);
-        SwitchPreference pref = getHighQualityAudioPref();
+        SwitchPreferenceCompat pref = getHighQualityAudioPref();
         assertThat(pref.getKey()).isEqualTo(
                 BluetoothDetailsProfilesController.HIGH_QUALITY_AUDIO_PREF_TAG);
 
@@ -407,7 +434,7 @@
         addMockA2dpProfile(true, false, false);
         showScreen(mController);
         assertThat(mProfiles.getPreferenceCount()).isEqualTo(2);
-        SwitchPreference pref = (SwitchPreference) mProfiles.getPreference(0);
+        SwitchPreferenceCompat pref = (SwitchPreferenceCompat) mProfiles.getPreference(0);
         assertThat(pref.getKey())
             .isNotEqualTo(BluetoothDetailsProfilesController.HIGH_QUALITY_AUDIO_PREF_TAG);
         assertThat(pref.getTitle()).isEqualTo(
@@ -420,7 +447,7 @@
         addMockA2dpProfile(true, true, true);
         when(mCachedDevice.isBusy()).thenReturn(true);
         showScreen(mController);
-        SwitchPreference pref = getHighQualityAudioPref();
+        SwitchPreferenceCompat pref = getHighQualityAudioPref();
         assertThat(pref.isEnabled()).isFalse();
     }
 
@@ -433,14 +460,14 @@
 
         // Disabling media audio should cause the high quality audio switch to disappear, but not
         // the regular audio one.
-        SwitchPreference audioPref =
-            (SwitchPreference) mScreen.findPreference(audioProfile.toString());
+        SwitchPreferenceCompat audioPref =
+                (SwitchPreferenceCompat) mScreen.findPreference(audioProfile.toString());
         audioPref.performClick();
         verify(audioProfile).setEnabled(mDevice, false);
         when(audioProfile.isEnabled(mDevice)).thenReturn(false);
         mController.onDeviceAttributesChanged();
         assertThat(audioPref.isVisible()).isTrue();
-        SwitchPreference highQualityAudioPref = getHighQualityAudioPref();
+        SwitchPreferenceCompat highQualityAudioPref = getHighQualityAudioPref();
         assertThat(highQualityAudioPref.isVisible()).isFalse();
 
         // And re-enabling media audio should make high quality switch to reappear.
@@ -457,8 +484,8 @@
         setupDevice(makeDefaultDeviceConfig());
         A2dpProfile audioProfile = addMockA2dpProfile(false, true, true);
         showScreen(mController);
-        SwitchPreference audioPref = mScreen.findPreference(audioProfile.toString());
-        SwitchPreference highQualityAudioPref = getHighQualityAudioPref();
+        SwitchPreferenceCompat audioPref = mScreen.findPreference(audioProfile.toString());
+        SwitchPreferenceCompat highQualityAudioPref = getHighQualityAudioPref();
         assertThat(audioPref).isNotNull();
         assertThat(audioPref.isChecked()).isFalse();
         assertThat(highQualityAudioPref).isNotNull();
@@ -489,4 +516,46 @@
         assertThat(mController.isModelNameInAllowList(null)).isFalse();
         assertThat(mController.isModelNameInAllowList(NON_LE_DEVICE_MODEL)).isFalse();
     }
+
+    @Test
+    public void prefKeyInBlockingList_hideToggle() {
+        mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_BLUETOOTH_PROFILE_TOGGLE_VISIBILITY_CHECKER);
+        setupDevice(makeDefaultDeviceConfig());
+
+        LeAudioProfile leAudioProfile = mock(LeAudioProfile.class);
+        when(leAudioProfile.getNameResource(mDevice))
+                .thenReturn(com.android.settingslib.R.string.bluetooth_profile_le_audio);
+        when(leAudioProfile.isProfileReady()).thenReturn(true);
+        when(leAudioProfile.toString()).thenReturn("LE_AUDIO");
+        when(mProfileManager.getLeAudioProfile()).thenReturn(leAudioProfile);
+        when(mFeatureProvider.getInvisibleProfilePreferenceKeys(any(), any()))
+                .thenReturn(ImmutableSet.of("LE_AUDIO"));
+        mConnectableProfiles.add(leAudioProfile);
+
+        showScreen(mController);
+
+        List<SwitchPreferenceCompat> switches = getProfileSwitches(false);
+        assertThat(switches.get(0).isVisible()).isFalse();
+    }
+
+    @Test
+    public void prefKeyNotInBlockingList_showToggle() {
+        mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_BLUETOOTH_PROFILE_TOGGLE_VISIBILITY_CHECKER);
+        setupDevice(makeDefaultDeviceConfig());
+
+        LeAudioProfile leAudioProfile = mock(LeAudioProfile.class);
+        when(leAudioProfile.getNameResource(mDevice))
+                .thenReturn(com.android.settingslib.R.string.bluetooth_profile_le_audio);
+        when(leAudioProfile.isProfileReady()).thenReturn(true);
+        when(leAudioProfile.toString()).thenReturn("LE_AUDIO");
+        when(mProfileManager.getLeAudioProfile()).thenReturn(leAudioProfile);
+        when(mFeatureProvider.getInvisibleProfilePreferenceKeys(any(), any()))
+                .thenReturn(ImmutableSet.of("A2DP"));
+        mConnectableProfiles.add(leAudioProfile);
+
+        showScreen(mController);
+
+        List<SwitchPreferenceCompat> switches = getProfileSwitches(false);
+        assertThat(switches.get(0).isVisible()).isTrue();
+    }
 }