Apply system property on sSupportedProfile

Based on the system property of LE_AUDIO_SWITCHER_DISABLED_PROPERTY,
supported profile can be changed. This CL make sSupportedProfile follows
the value of system property.

Bug: 239486150
Test: atest ConfigTest
Change-Id: If601eec4ca013bda9fd37e39fb97f74e08379ed9
diff --git a/android/app/src/com/android/bluetooth/btservice/Config.java b/android/app/src/com/android/bluetooth/btservice/Config.java
index bc0ed84..caa1a00 100644
--- a/android/app/src/com/android/bluetooth/btservice/Config.java
+++ b/android/app/src/com/android/bluetooth/btservice/Config.java
@@ -56,6 +56,7 @@
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
 public class Config {
@@ -171,9 +172,14 @@
                 profile.mSupported = enabled;
             }
         }
+        if (enabled) {
+            sSupportedProfiles.add(profileClass);
+        } else {
+            sSupportedProfiles.remove(profileClass);
+        }
     }
 
-    private static Class[] sSupportedProfiles = new Class[0];
+    private static List<Class> sSupportedProfiles = new ArrayList<>();
 
     private static boolean sIsGdEnabledUptoScanningLayer = false;
 
@@ -211,15 +217,20 @@
             setProfileEnabled(HearingAidService.class, false);
         }
 
-        ArrayList<Class> profiles = new ArrayList<>(PROFILE_SERVICES_AND_FLAGS.length);
-        for (ProfileConfig config : PROFILE_SERVICES_AND_FLAGS) {
-            Log.i(TAG, "init: profile=" + config.mClass.getSimpleName() + ", enabled="
-                    + config.mSupported);
-            if (config.mSupported) {
-                profiles.add(config.mClass);
+        synchronized (sSupportedProfiles) {
+            sSupportedProfiles.clear();
+            for (ProfileConfig config : PROFILE_SERVICES_AND_FLAGS) {
+                Log.i(
+                        TAG,
+                        "init: profile="
+                                + config.mClass.getSimpleName()
+                                + ", enabled="
+                                + config.mSupported);
+                if (config.mSupported) {
+                    sSupportedProfiles.add(config.mClass);
+                }
             }
         }
-        sSupportedProfiles = profiles.toArray(new Class[profiles.size()]);
 
         if (ctx == null) {
             return;
@@ -253,19 +264,17 @@
      * Remove the input profiles from the supported list.
      */
     static void removeProfileFromSupportedList(HashSet<Class> nonSupportedProfiles) {
-        ArrayList<Class> profilesList = new ArrayList<Class>(Arrays.asList(sSupportedProfiles));
-        Iterator<Class> iter = profilesList.iterator();
+        synchronized (sSupportedProfiles) {
+            Iterator<Class> iter = sSupportedProfiles.iterator();
+            while (iter.hasNext()) {
+                Class profileClass = iter.next();
 
-        while (iter.hasNext()) {
-            Class profileClass = iter.next();
-
-            if (nonSupportedProfiles.contains(profileClass)) {
-                iter.remove();
-                Log.v(TAG, "Remove " + profileClass.getSimpleName() + " from supported list.");
+                if (nonSupportedProfiles.contains(profileClass)) {
+                    iter.remove();
+                    Log.v(TAG, "Remove " + profileClass.getSimpleName() + " from supported list.");
+                }
             }
         }
-
-        sSupportedProfiles = profilesList.toArray(new Class[profilesList.size()]);
     }
 
     static void updateSupportedProfileMask(Boolean enable, Class profile, int supportedProfile) {
@@ -286,7 +295,9 @@
     }
 
     static Class[] getSupportedProfiles() {
-        return sSupportedProfiles;
+        synchronized (sSupportedProfiles) {
+            return sSupportedProfiles.toArray(new Class[0]);
+        }
     }
 
     static boolean isGdEnabledUpToScanningLayer() {
diff --git a/android/app/tests/unit/src/com/android/bluetooth/btservice/ConfigTest.java b/android/app/tests/unit/src/com/android/bluetooth/btservice/ConfigTest.java
new file mode 100644
index 0000000..7b97035
--- /dev/null
+++ b/android/app/tests/unit/src/com/android/bluetooth/btservice/ConfigTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 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.bluetooth.btservice;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.android.bluetooth.csip.CsipSetCoordinatorService;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.util.Arrays;
+
+@RunWith(JUnit4.class)
+public final class ConfigTest {
+    @Test
+    public void setProfileEnabled() {
+        boolean enabled =
+                Arrays.stream(Config.getSupportedProfiles())
+                        .anyMatch(cls -> cls == CsipSetCoordinatorService.class);
+
+        Config.setProfileEnabled(CsipSetCoordinatorService.class, false);
+        assertThat(
+                        Arrays.stream(Config.getSupportedProfiles())
+                                .anyMatch(cls -> cls == CsipSetCoordinatorService.class))
+                .isFalse();
+
+        Config.setProfileEnabled(CsipSetCoordinatorService.class, true);
+        assertThat(
+                        Arrays.stream(Config.getSupportedProfiles())
+                                .anyMatch(cls -> cls == CsipSetCoordinatorService.class))
+                .isTrue();
+
+        Config.setProfileEnabled(CsipSetCoordinatorService.class, enabled);
+    }
+}