From dfee6bec1a8811c26be989e2969ec45a75b736af Mon Sep 17 00:00:00 2001
From: Chalard Jean <jchalard@google.com>
Date: Fri, 27 Jan 2023 18:46:00 +0900
Subject: [PATCH] Change mCarrierServiceUid from int[] to SparseIntArray

Because followup changes will stop using synchronous getters
for getting the service package UIDs, it will be a lot more
convenient if this array is automatically sized.

Test: FrameworksNetTests
Bug: 236669534
Change-Id: I2df8442727b5f17bd2d4e55a8795dc9210beb74b
---
 .../connectivity/CarrierPrivilegeAuthenticator.java  | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/service/src/com/android/server/connectivity/CarrierPrivilegeAuthenticator.java b/service/src/com/android/server/connectivity/CarrierPrivilegeAuthenticator.java
index 4325763c5e..be2c0dea36 100644
--- a/service/src/com/android/server/connectivity/CarrierPrivilegeAuthenticator.java
+++ b/service/src/com/android/server/connectivity/CarrierPrivilegeAuthenticator.java
@@ -35,6 +35,7 @@ import android.os.Process;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.util.Log;
+import android.util.SparseIntArray;
 
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.annotations.VisibleForTesting;
@@ -63,7 +64,7 @@ public class CarrierPrivilegeAuthenticator extends BroadcastReceiver {
     private final TelephonyManagerShim mTelephonyManagerShim;
     private final TelephonyManager mTelephonyManager;
     @GuardedBy("mLock")
-    private int[] mCarrierServiceUid;
+    private final SparseIntArray mCarrierServiceUid = new SparseIntArray(2 /* initialCapacity */);
     @GuardedBy("mLock")
     private int mModemCount = 0;
     private final Object mLock = new Object();
@@ -233,9 +234,9 @@ public class CarrierPrivilegeAuthenticator extends BroadcastReceiver {
     @VisibleForTesting
     void updateCarrierServiceUid() {
         synchronized (mLock) {
-            mCarrierServiceUid = new int[mModemCount];
+            mCarrierServiceUid.clear();
             for (int i = 0; i < mModemCount; i++) {
-                mCarrierServiceUid[i] = getCarrierServicePackageUidForSlot(i);
+                mCarrierServiceUid.put(i, getCarrierServicePackageUidForSlot(i));
             }
         }
     }
@@ -244,11 +245,8 @@ public class CarrierPrivilegeAuthenticator extends BroadcastReceiver {
     int getCarrierServiceUidForSubId(int subId) {
         final int slotId = getSlotIndex(subId);
         synchronized (mLock) {
-            if (slotId != SubscriptionManager.INVALID_SIM_SLOT_INDEX && slotId < mModemCount) {
-                return mCarrierServiceUid[slotId];
-            }
+            return mCarrierServiceUid.get(slotId, Process.INVALID_UID);
         }
-        return Process.INVALID_UID;
     }
 
     @VisibleForTesting
-- 
GitLab