Merge "Update Stream Manager with stream Id"
diff --git a/service/res/values/config.xml b/service/res/values/config.xml
index 1cb8b17..c0ef110 100644
--- a/service/res/values/config.xml
+++ b/service/res/values/config.xml
@@ -64,7 +64,7 @@
           The current implementations expects the following system packages/activities to be
           whitelisted. For general guidelines to design distraction optimized apps, please refer
           to Android Auto Driver Distraction Guidelines. -->
-    <string name="systemActivityWhitelist" translatable="false">com.android.systemui,com.google.android.permissioncontroller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity,com.android.permissioncontroller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity,android/com.android.internal.app.ResolverActivity,com.android.mtp/com.android.mtp.ReceiverActivity</string>
+    <string name="systemActivityWhitelist" translatable="false">com.android.systemui,com.google.android.permissioncontroller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity,com.android.permissioncontroller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity,android/com.android.internal.app.ResolverActivity,com.android.mtp/com.android.mtp.ReceiverActivity,com.android.server.telecom/com.android.server.telecom.components.UserCallActivity</string>
     <!--  Comma separated list of activities that will be blocked during restricted state.
           Format of each entry is either to specify package name to whitelist the whole package
           or use format of "packagename/activity_classname" for tagging each activities.-->
diff --git a/service/src/com/android/car/CarBluetoothService.java b/service/src/com/android/car/CarBluetoothService.java
index 94ff99f..0a454af 100644
--- a/service/src/com/android/car/CarBluetoothService.java
+++ b/service/src/com/android/car/CarBluetoothService.java
@@ -25,6 +25,7 @@
 import android.content.pm.PackageManager;
 import android.os.IBinder;
 import android.os.RemoteException;
+import android.os.UserHandle;
 import android.util.Log;
 import android.util.SparseArray;
 
@@ -100,25 +101,29 @@
             synchronized (mPerUserLock) {
                 // Explicitly clear out existing per-user objects since we can't rely on the
                 // onServiceDisconnected and onPreUnbind calls to always be called before this
-                destroyUser();
+                destroyUserLocked();
 
                 mPerUserCarService = perUserCarService;
 
                 // Create new objects with our new set of profile proxies
-                initializeUser();
+                initializeUserLocked();
             }
         }
 
         @Override
         public void onPreUnbind() {
             logd("Before Unbinding from PerUserCarService");
-            destroyUser();
+            synchronized (mPerUserLock) {
+                destroyUserLocked();
+            }
         }
 
         @Override
         public void onServiceDisconnected() {
             logd("Disconnected from PerUserCarService");
-            destroyUser();
+            synchronized (mPerUserLock) {
+                destroyUserLocked();
+            }
         }
     };
 
@@ -130,7 +135,7 @@
      *                            to in order to receive user switch events
      */
     public CarBluetoothService(Context context, PerUserCarServiceHelper userSwitchService) {
-        mUserId = -1;
+        mUserId = UserHandle.USER_NULL;
         mContext = context;
         mUserServiceHelper = userSwitchService;
         mUseDefaultPolicy = mContext.getResources().getBoolean(
@@ -157,7 +162,9 @@
     public void release() {
         logd("release()");
         mUserServiceHelper.unregisterServiceCallback(mUserServiceCallback);
-        destroyUser();
+        synchronized (mPerUserLock) {
+            destroyUserLocked();
+        }
     }
 
     /**
@@ -174,37 +181,33 @@
      *
      * Only call this following a known user switch once we've connected to the user service helper.
      */
-    private void initializeUser() {
+    private void initializeUserLocked() {
         logd("Initializing new user");
-        synchronized (mPerUserLock) {
-            mUserId = ActivityManager.getCurrentUser();
-            createBluetoothUserService();
-            createBluetoothProfileDeviceManagers();
-            createBluetoothProfileInhibitManager();
+        mUserId = ActivityManager.getCurrentUser();
+        createBluetoothUserServiceLocked();
+        createBluetoothProfileDeviceManagersLocked();
+        createBluetoothProfileInhibitManagerLocked();
 
-            // Determine if we need to begin the default policy
-            mBluetoothDeviceConnectionPolicy = null;
-            if (mUseDefaultPolicy) {
-                createBluetoothDeviceConnectionPolicy();
-            }
-            logd("Switched to user " + mUserId);
+        // Determine if we need to begin the default policy
+        mBluetoothDeviceConnectionPolicy = null;
+        if (mUseDefaultPolicy) {
+            createBluetoothDeviceConnectionPolicyLocked();
         }
+        logd("Switched to user " + mUserId);
     }
 
     /**
      * Destroy the current user context, defined by the set of profile proxies, profile device
      * managers, inhibit manager and the policy.
      */
-    private void destroyUser() {
+    private void destroyUserLocked() {
         logd("Destroying user " + mUserId);
-        synchronized (mPerUserLock) {
-            destroyBluetoothDeviceConnectionPolicy();
-            destroyBluetoothProfileInhibitManager();
-            destroyBluetoothProfileDeviceManagers();
-            destroyBluetoothUserService();
-            mPerUserCarService = null;
-            mUserId = -1;
-        }
+        destroyBluetoothDeviceConnectionPolicyLocked();
+        destroyBluetoothProfileInhibitManagerLocked();
+        destroyBluetoothProfileDeviceManagersLocked();
+        destroyBluetoothUserServiceLocked();
+        mPerUserCarService = null;
+        mUserId = UserHandle.USER_NULL;
     }
 
     /**
@@ -213,19 +216,17 @@
      * Also sets up the connection proxy objects required to communicate with the Bluetooth
      * Profile Services.
      */
-    private void createBluetoothUserService() {
-        synchronized (mPerUserLock) {
-            if (mPerUserCarService != null) {
-                try {
-                    mCarBluetoothUserService = mPerUserCarService.getBluetoothUserService();
-                    mCarBluetoothUserService.setupBluetoothConnectionProxies();
-                } catch (RemoteException e) {
-                    Log.e(TAG, "Remote Service Exception on ServiceConnection Callback: "
-                            + e.getMessage());
-                }
-            } else {
-                logd("PerUserCarService not connected. Cannot get bluetooth user proxy objects");
+    private void createBluetoothUserServiceLocked() {
+        if (mPerUserCarService != null) {
+            try {
+                mCarBluetoothUserService = mPerUserCarService.getBluetoothUserService();
+                mCarBluetoothUserService.setupBluetoothConnectionProxies();
+            } catch (RemoteException e) {
+                Log.e(TAG, "Remote Service Exception on ServiceConnection Callback: "
+                        + e.getMessage());
             }
+        } else {
+            logd("PerUserCarService not connected. Cannot get bluetooth user proxy objects");
         }
     }
 
@@ -233,131 +234,116 @@
      * Close out the Per User Car Bluetooth profile proxy connections and destroys the Car Bluetooth
      * User Service object.
      */
-    private void destroyBluetoothUserService() {
-        synchronized (mPerUserLock) {
-            if (mCarBluetoothUserService == null) return;
-            try {
-                mCarBluetoothUserService.closeBluetoothConnectionProxies();
-            } catch (RemoteException e) {
-                Log.e(TAG, "Remote Service Exception on ServiceConnection Callback: "
-                        + e.getMessage());
-            }
-            mCarBluetoothUserService = null;
+    private void destroyBluetoothUserServiceLocked() {
+        if (mCarBluetoothUserService == null) return;
+        try {
+            mCarBluetoothUserService.closeBluetoothConnectionProxies();
+        } catch (RemoteException e) {
+            Log.e(TAG, "Remote Service Exception on ServiceConnection Callback: "
+                    + e.getMessage());
         }
+        mCarBluetoothUserService = null;
     }
 
     /**
      * Clears out Profile Device Managers and re-creates them for the current user.
      */
-    private void createBluetoothProfileDeviceManagers() {
-        synchronized (mPerUserLock) {
-            if (mUserId == -1) {
-                logd("No foreground user, cannot create profile device managers");
-                return;
-            }
-            for (int profileId : sManagedProfiles) {
-                if (mProfileDeviceManagers.get(profileId) != null) {
-                    BluetoothProfileDeviceManager deviceManager =
-                            mProfileDeviceManagers.get(profileId);
-                    deviceManager.stop();
-                    mProfileDeviceManagers.remove(profileId);
-                    logd("Existing device manager removed for profile "
-                            + Utils.getProfileName(profileId));
-                }
-
-                BluetoothProfileDeviceManager deviceManager = BluetoothProfileDeviceManager.create(
-                        mContext, mUserId, mCarBluetoothUserService, profileId);
-                if (deviceManager == null) {
-                    logd("Failed to create profile device manager for "
-                            + Utils.getProfileName(profileId));
-                    continue;
-                }
-                mProfileDeviceManagers.put(profileId, deviceManager);
-                logd("Created profile device manager for " + Utils.getProfileName(profileId));
+    private void createBluetoothProfileDeviceManagersLocked() {
+        if (mUserId == UserHandle.USER_NULL) {
+            logd("No foreground user, cannot create profile device managers");
+            return;
+        }
+        for (int profileId : sManagedProfiles) {
+            BluetoothProfileDeviceManager deviceManager = mProfileDeviceManagers.get(profileId);
+            if (deviceManager != null) {
+                deviceManager.stop();
+                mProfileDeviceManagers.remove(profileId);
+                logd("Existing device manager removed for profile "
+                        + Utils.getProfileName(profileId));
             }
 
-            for (int i = 0; i < mProfileDeviceManagers.size(); i++) {
-                int key = mProfileDeviceManagers.keyAt(i);
-                BluetoothProfileDeviceManager deviceManager =
-                        (BluetoothProfileDeviceManager) mProfileDeviceManagers.get(key);
-                deviceManager.start();
+            deviceManager = BluetoothProfileDeviceManager.create(mContext, mUserId,
+                    mCarBluetoothUserService, profileId);
+            if (deviceManager == null) {
+                logd("Failed to create profile device manager for "
+                        + Utils.getProfileName(profileId));
+                continue;
             }
+            mProfileDeviceManagers.put(profileId, deviceManager);
+            logd("Created profile device manager for " + Utils.getProfileName(profileId));
+        }
+
+        for (int i = 0; i < mProfileDeviceManagers.size(); i++) {
+            int key = mProfileDeviceManagers.keyAt(i);
+            BluetoothProfileDeviceManager deviceManager =
+                    (BluetoothProfileDeviceManager) mProfileDeviceManagers.get(key);
+            deviceManager.start();
         }
     }
 
     /**
      * Stops and clears the entire set of Profile Device Managers.
      */
-    private void destroyBluetoothProfileDeviceManagers() {
-        synchronized (mPerUserLock) {
-            for (int i = 0; i < mProfileDeviceManagers.size(); i++) {
-                int key = mProfileDeviceManagers.keyAt(i);
-                BluetoothProfileDeviceManager deviceManager =
-                        (BluetoothProfileDeviceManager) mProfileDeviceManagers.get(key);
-                deviceManager.stop();
-            }
-            mProfileDeviceManagers.clear();
+    private void destroyBluetoothProfileDeviceManagersLocked() {
+        for (int i = 0; i < mProfileDeviceManagers.size(); i++) {
+            int key = mProfileDeviceManagers.keyAt(i);
+            BluetoothProfileDeviceManager deviceManager =
+                    (BluetoothProfileDeviceManager) mProfileDeviceManagers.get(key);
+            deviceManager.stop();
         }
+        mProfileDeviceManagers.clear();
     }
 
     /**
      * Creates an instance of a BluetoothProfileInhibitManager under the current user
      */
-    private void createBluetoothProfileInhibitManager() {
+    private void createBluetoothProfileInhibitManagerLocked() {
         logd("Creating inhibit manager");
-        synchronized (mPerUserLock) {
-            if (mUserId == -1) {
-                logd("No foreground user, cannot create profile inhibit manager");
-                return;
-            }
-            mInhibitManager = new BluetoothProfileInhibitManager(mContext, mUserId,
-                    mCarBluetoothUserService);
-            mInhibitManager.start();
+        if (mUserId == UserHandle.USER_NULL) {
+            logd("No foreground user, cannot create profile inhibit manager");
+            return;
         }
+        mInhibitManager = new BluetoothProfileInhibitManager(mContext, mUserId,
+                mCarBluetoothUserService);
+        mInhibitManager.start();
     }
 
     /**
      * Destroys the current instance of a BluetoothProfileInhibitManager, if one exists
      */
-    private void destroyBluetoothProfileInhibitManager() {
+    private void destroyBluetoothProfileInhibitManagerLocked() {
         logd("Destroying inhibit manager");
-        synchronized (mPerUserLock) {
-            if (mInhibitManager == null) return;
-            mInhibitManager.stop();
-            mInhibitManager = null;
-        }
+        if (mInhibitManager == null) return;
+        mInhibitManager.stop();
+        mInhibitManager = null;
     }
 
     /**
      * Creates an instance of a BluetoothDeviceConnectionPolicy under the current user
      */
-    private void createBluetoothDeviceConnectionPolicy() {
+    private void createBluetoothDeviceConnectionPolicyLocked() {
         logd("Creating device connection policy");
-        synchronized (mPerUserLock) {
-            if (mUserId == -1) {
-                logd("No foreground user, cannot create device connection policy");
-                return;
-            }
-            mBluetoothDeviceConnectionPolicy = BluetoothDeviceConnectionPolicy.create(mContext,
-                    mUserId, this);
-            if (mBluetoothDeviceConnectionPolicy == null) {
-                logd("Failed to create default Bluetooth device connection policy.");
-                return;
-            }
-            mBluetoothDeviceConnectionPolicy.init();
+        if (mUserId == UserHandle.USER_NULL) {
+            logd("No foreground user, cannot create device connection policy");
+            return;
         }
+        mBluetoothDeviceConnectionPolicy = BluetoothDeviceConnectionPolicy.create(mContext,
+                mUserId, this);
+        if (mBluetoothDeviceConnectionPolicy == null) {
+            logd("Failed to create default Bluetooth device connection policy.");
+            return;
+        }
+        mBluetoothDeviceConnectionPolicy.init();
     }
 
     /**
      * Destroys the current instance of a BluetoothDeviceConnectionPolicy, if one exists
      */
-    private void destroyBluetoothDeviceConnectionPolicy() {
+    private void destroyBluetoothDeviceConnectionPolicyLocked() {
         logd("Destroying device connection policy");
-        synchronized (mPerUserLock) {
-            if (mBluetoothDeviceConnectionPolicy != null) {
-                mBluetoothDeviceConnectionPolicy.release();
-                mBluetoothDeviceConnectionPolicy = null;
-            }
+        if (mBluetoothDeviceConnectionPolicy != null) {
+            mBluetoothDeviceConnectionPolicy.release();
+            mBluetoothDeviceConnectionPolicy = null;
         }
     }
 
diff --git a/service/src/com/android/car/CarProjectionService.java b/service/src/com/android/car/CarProjectionService.java
index 69d3b05..82cb029 100644
--- a/service/src/com/android/car/CarProjectionService.java
+++ b/service/src/com/android/car/CarProjectionService.java
@@ -612,7 +612,7 @@
             ensureApConfiguration();
         }
 
-        if (!mWifiManager.startSoftAp(null /* use existing config*/)) {
+        if (!mWifiManager.startTetheredHotspot(null /* use existing config*/)) {
             // The indicates that AP might be already started.
             if (mWifiManager.getWifiApState() == WIFI_AP_STATE_ENABLED) {
                 sendApStarted(mWifiManager.getWifiApConfiguration());
@@ -716,7 +716,6 @@
         Log.i(TAG, "Sending PROJECTION_AP_STARTED, ssid: "
                 + localWifiConfig.getPrintableSsid()
                 + ", apBand: " + localWifiConfig.apBand
-                + ", apChannel: " + localWifiConfig.apChannel
                 + ", bssid: " + localWifiConfig.BSSID);
         sendApStatusMessage(message);
     }
diff --git a/tests/ThemePlayground/Android.bp b/tests/ThemePlayground/Android.bp
index 07133d4..09677dd 100644
--- a/tests/ThemePlayground/Android.bp
+++ b/tests/ThemePlayground/Android.bp
@@ -28,7 +28,7 @@
         "androidx.legacy_legacy-support-v4",
         "androidx-constraintlayout_constraintlayout",
         "androidx-constraintlayout_constraintlayout-solver",
-        "car-apps-common-bp",
+        "car-apps-common",
     ],
 
     owner: "google",