Assign only non-zero publisher IDs.

Other changes in this CL:
- Publisher IDs generated/indexed by an ArrayList.
- Publisher Info indexed by an ArrayMap.

Bug: 139421764
Test: Updated VmsPublishersInfoTest
Test: atest AndroidCarApiTest CarServiceTest CarServiceUnitTest
Change-Id: I5dbc1abefc2f0aed82ebaa104fa4660580658821
(cherry picked from commit 5bce91ac61f4ced865cc843e749ae792a6ceeebf)
diff --git a/service/src/com/android/car/VmsPublishersInfo.java b/service/src/com/android/car/VmsPublishersInfo.java
index 4a75953..78ec9fb 100644
--- a/service/src/com/android/car/VmsPublishersInfo.java
+++ b/service/src/com/android/car/VmsPublishersInfo.java
@@ -16,13 +16,13 @@
 
 package com.android.car;
 
+import android.util.ArrayMap;
 import android.util.Log;
 
 import com.android.internal.annotations.GuardedBy;
 
+import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
 
 public class VmsPublishersInfo {
     private static final String TAG = "VmsPublishersInfo";
@@ -31,9 +31,9 @@
 
     private final Object mLock = new Object();
     @GuardedBy("mLock")
-    private final Map<InfoWrapper, Integer> mPublishersIds = new HashMap<>();
+    private final ArrayMap<InfoWrapper, Integer> mPublishersIds = new ArrayMap<>();
     @GuardedBy("mLock")
-    private final Map<Integer, byte[]> mPublishersInfo = new HashMap<>();
+    private final ArrayList<InfoWrapper> mPublishersInfo = new ArrayList<>();
 
     private static class InfoWrapper {
         private final byte[] mInfo;
@@ -43,7 +43,7 @@
         }
 
         public byte[] getInfo() {
-            return mInfo;
+            return mInfo.clone();
         }
 
         @Override
@@ -62,15 +62,24 @@
     }
 
     /**
-     * Returns the ID associated with the publisher info. When called for the first time for a
-     * publisher info will store the info and assign an ID
+     * Retrieves the publisher ID for the given publisher information. If the publisher information
+     * has not previously been seen, it will be assigned a new publisher ID.
+     *
+     * @param publisherInfo Publisher information to query or register.
+     * @return Publisher ID for the given publisher information.
      */
     public int getIdForInfo(byte[] publisherInfo) {
         Integer publisherId;
         InfoWrapper wrappedPublisherInfo = new InfoWrapper(publisherInfo);
         synchronized (mLock) {
-            maybeAddPublisherInfoLocked(wrappedPublisherInfo);
+            // Check if publisher is already registered
             publisherId = mPublishersIds.get(wrappedPublisherInfo);
+            if (publisherId == null) {
+                // Add the new publisher and assign it the next ID
+                mPublishersInfo.add(wrappedPublisherInfo);
+                publisherId = mPublishersInfo.size();
+                mPublishersIds.put(wrappedPublisherInfo, publisherId);
+            }
         }
         if (DBG) {
             Log.i(TAG, "Publisher ID is: " + publisherId);
@@ -78,22 +87,16 @@
         return publisherId;
     }
 
+    /**
+     * Returns the publisher info associated with the given publisher ID.
+     * @param publisherId Publisher ID to query.
+     * @return Publisher info associated with the ID, or an empty array if publisher ID is unknown.
+     */
     public byte[] getPublisherInfo(int publisherId) {
         synchronized (mLock) {
-            return mPublishersInfo.containsKey(publisherId)
-                    ? mPublishersInfo.get(publisherId).clone()
-                    : EMPTY_RESPONSE;
-        }
-    }
-
-    @GuardedBy("mLock")
-    private void maybeAddPublisherInfoLocked(InfoWrapper wrappedPublisherInfo) {
-        if (!mPublishersIds.containsKey(wrappedPublisherInfo)) {
-            // Assign ID to the info
-            Integer publisherId = mPublishersIds.size();
-
-            mPublishersIds.put(wrappedPublisherInfo, publisherId);
-            mPublishersInfo.put(publisherId, wrappedPublisherInfo.getInfo());
+            return publisherId < 1 || publisherId > mPublishersInfo.size()
+                    ? EMPTY_RESPONSE
+                    : mPublishersInfo.get(publisherId - 1).getInfo();
         }
     }
 }
diff --git a/tests/carservice_unit_test/src/com/android/car/VmsPublishersInfoTest.java b/tests/carservice_unit_test/src/com/android/car/VmsPublishersInfoTest.java
index 2c7b9f1..1938a19 100644
--- a/tests/carservice_unit_test/src/com/android/car/VmsPublishersInfoTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/VmsPublishersInfoTest.java
@@ -23,9 +23,9 @@
 import org.junit.Test;
 
 public class VmsPublishersInfoTest {
-    public static final byte[] MOCK_INFO_0 = new byte[]{2, 3, 5, 7, 11, 13, 17};
-    public static final byte[] SAME_MOCK_INFO_0 = new byte[]{2, 3, 5, 7, 11, 13, 17};
-    public static final byte[] MOCK_INFO_1 = new byte[]{2, 3, 5, 7, 11, 13, 17, 19};
+    public static final byte[] MOCK_INFO_1 = new byte[]{2, 3, 5, 7, 11, 13, 17};
+    public static final byte[] SAME_MOCK_INFO_1 = new byte[]{2, 3, 5, 7, 11, 13, 17};
+    public static final byte[] MOCK_INFO_2 = new byte[]{2, 3, 5, 7, 11, 13, 17, 19};
 
     private VmsPublishersInfo mVmsPublishersInfo;
 
@@ -36,9 +36,9 @@
 
     @Test
     public void testSingleInfo() throws Exception {
-        int id = mVmsPublishersInfo.getIdForInfo(MOCK_INFO_0);
-        assertEquals(0, id);
-        assertArrayEquals(MOCK_INFO_0, mVmsPublishersInfo.getPublisherInfo(id));
+        int id = mVmsPublishersInfo.getIdForInfo(MOCK_INFO_1);
+        assertEquals(1, id);
+        assertArrayEquals(MOCK_INFO_1, mVmsPublishersInfo.getPublisherInfo(id));
     }
 
     @Test
@@ -48,20 +48,20 @@
 
     @Test
     public void testTwoInfos() throws Exception {
-        int id0 = mVmsPublishersInfo.getIdForInfo(MOCK_INFO_0);
         int id1 = mVmsPublishersInfo.getIdForInfo(MOCK_INFO_1);
-        assertEquals(0, id0);
+        int id2 = mVmsPublishersInfo.getIdForInfo(MOCK_INFO_2);
         assertEquals(1, id1);
-        assertArrayEquals(MOCK_INFO_0, mVmsPublishersInfo.getPublisherInfo(id0));
+        assertEquals(2, id2);
         assertArrayEquals(MOCK_INFO_1, mVmsPublishersInfo.getPublisherInfo(id1));
+        assertArrayEquals(MOCK_INFO_2, mVmsPublishersInfo.getPublisherInfo(id2));
     }
 
     @Test
     public void testSingleInfoInsertedTwice() throws Exception {
-        int id = mVmsPublishersInfo.getIdForInfo(MOCK_INFO_0);
-        assertEquals(0, id);
+        int id = mVmsPublishersInfo.getIdForInfo(MOCK_INFO_1);
+        assertEquals(1, id);
 
-        int sameId = mVmsPublishersInfo.getIdForInfo(SAME_MOCK_INFO_0);
+        int sameId = mVmsPublishersInfo.getIdForInfo(SAME_MOCK_INFO_1);
         assertEquals(sameId, id);
     }
 }