Added error for set zone for uid API

Added more informative error to setZoneIdForUid and clearZoneIdForUid,
instead of silently failing to changed routing. Error will inform user
that the APIs do not work in conjuction wil occupant zone mapping.

Bug: 162864560
Test: atest CarAudioManagerSystemTest
Change-Id: Id5ddfffd75788a39a885541b7e828bf5a119ce6e
Merged-In: Id5ddfffd75788a39a885541b7e828bf5a119ce6e
(cherry picked from commit f2faa5de7d8ad7e15e9e3149d245054c9c9fa721)
diff --git a/service/src/com/android/car/audio/CarAudioService.java b/service/src/com/android/car/audio/CarAudioService.java
index db081eb..bc67085 100644
--- a/service/src/com/android/car/audio/CarAudioService.java
+++ b/service/src/com/android/car/audio/CarAudioService.java
@@ -892,6 +892,10 @@
      *
      * @param zoneId The audio zone id
      * @param uid The uid to map
+     *
+     * <p><b>Note:</b> Will throw if occupant zone mapping exist, as uid and occupant zone mapping
+     * do not work in conjunction.
+     *
      * @return true if the device affinities, for devices in zone, are successfully set
      */
     @Override
@@ -905,6 +909,9 @@
                     + uid + " mapped to : "
                     + zoneId);
 
+            // If occupant mapping exist uid routing can not be used
+            requiredOccupantZoneMappingDisabledLocked();
+
             // Figure out if anything is currently holding focus,
             // This will change the focus to transient loss while we are switching zones
             Integer currentZoneId = mUidToZoneMap.get(uid);
@@ -985,6 +992,10 @@
     /**
      * Removes the current mapping of the uid, focus will be lost in zone
      * @param uid The uid to remove
+     *
+     * <p><b>Note:</b> Will throw if occupant zone mapping exist, as uid and occupant zone mapping
+     * do not work in conjunction.
+     *
      * return true if all the devices affinities currently
      *            mapped to uid are successfully removed
      */
@@ -993,6 +1004,10 @@
         enforcePermission(Car.PERMISSION_CAR_CONTROL_AUDIO_SETTINGS);
         requireDynamicRouting();
         synchronized (mImplLock) {
+            // Throw so as to not set the wrong expectation,
+            // that routing will be changed if clearZoneIdForUid is called.
+            requiredOccupantZoneMappingDisabledLocked();
+
             return checkAndRemoveUidLocked(uid);
         }
     }
@@ -1083,6 +1098,13 @@
         Preconditions.checkState(mUseDynamicRouting, "Dynamic routing is required");
     }
 
+    private void requiredOccupantZoneMappingDisabledLocked() {
+        if (isOccupantZoneMappingAvailableLocked()) {
+            throw new IllegalStateException(
+                    "UID based routing is not supported while using occupant zone mapping");
+        }
+    }
+
     /**
      * @return {@link AudioDevicePort} that handles the given car audio usage.
      * Multiple usages may share one {@link AudioDevicePort}
@@ -1123,7 +1145,7 @@
     private void handleOccupantZoneUserChanged() {
         int driverUserId = mOccupantZoneService.getDriverUserId();
         synchronized (mImplLock) {
-            if (!isOccupantZoneMappingAvailable()) {
+            if (!isOccupantZoneMappingAvailableLocked()) {
                 //No occupant zone to audio zone mapping, re-adjust to settings driver.
                 for (int index = 0; index < mCarAudioZones.length; index++) {
                     CarAudioZone zone = mCarAudioZones[index];
@@ -1142,7 +1164,7 @@
         }
     }
 
-    private boolean isOccupantZoneMappingAvailable() {
+    private boolean isOccupantZoneMappingAvailableLocked() {
         return mAudioZoneIdToOccupantZoneIdMapping.size() > 0;
     }