Added support for a package to have multiple carrierIds associated with it.

CTS and unit tests have been updated to reflect the new functionality.Changes where made in the CarrierAllowListInfo, PhoneInterfaceManager and CarrierRestrictionOperatorDetails.JSON

Test:atest cts/tests/tests/telephony/current/src/android/telephony/cts/TelephonyManagerTest.java
Test2:atest cts/tests/tests/telephony/current/src/android/telephony/cts/TelephonyManagerTestOnMockModem.java
Bug: 324021134
Change-Id: I565c6b367a6351e5718ff2ea3d2182ebe3d54df3
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 1751778..6118640 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -1588,7 +1588,7 @@
                         // This is for the implementation of carrierRestrictionStatus.
                         CallerCallbackInfo callbackInfo = (CallerCallbackInfo) request.argument;
                         Consumer<Integer> callback = callbackInfo.getConsumer();
-                        int callerCarrierId = callbackInfo.getCarrierId();
+                        Set<Integer> callerCarrierIds = callbackInfo.getCarrierIds();
                         int lockStatus = TelephonyManager.CARRIER_RESTRICTION_STATUS_UNKNOWN;
                         if (ar.exception == null && ar.result instanceof CarrierRestrictionRules) {
                             CarrierRestrictionRules carrierRestrictionRules =
@@ -1603,8 +1603,10 @@
                                 Rlog.e(LOG_TAG, "CarrierIdentifier exception = " + ex);
                             }
                             lockStatus = carrierRestrictionRules.getCarrierRestrictionStatus();
-                            if (carrierId != -1 && callerCarrierId == carrierId && lockStatus
-                                    == TelephonyManager.CARRIER_RESTRICTION_STATUS_RESTRICTED) {
+                            int restrictedStatus =
+                                    TelephonyManager.CARRIER_RESTRICTION_STATUS_RESTRICTED;
+                            if (carrierId != -1 && callerCarrierIds.contains(carrierId) &&
+                                    lockStatus == restrictedStatus) {
                                 lockStatus = TelephonyManager
                                         .CARRIER_RESTRICTION_STATUS_RESTRICTED_TO_CALLER;
                             }
@@ -9241,15 +9243,15 @@
         enforceTelephonyFeatureWithException(packageName,
                 PackageManager.FEATURE_TELEPHONY_SUBSCRIPTION, "getCarrierRestrictionStatus");
 
-        int carrierId = validateCallerAndGetCarrierId(packageName);
-        if (carrierId == CarrierAllowListInfo.INVALID_CARRIER_ID) {
+        Set<Integer> carrierIds = validateCallerAndGetCarrierIds(packageName);
+        if (carrierIds.contains(CarrierAllowListInfo.INVALID_CARRIER_ID)) {
             Rlog.e(LOG_TAG, "getCarrierRestrictionStatus: caller is not registered");
             throw new SecurityException("Not an authorized caller");
         }
         final long identity = Binder.clearCallingIdentity();
         try {
             Consumer<Integer> consumer = FunctionalUtils.ignoreRemoteException(callback::accept);
-            CallerCallbackInfo callbackInfo = new CallerCallbackInfo(consumer, carrierId);
+            CallerCallbackInfo callbackInfo = new CallerCallbackInfo(consumer, carrierIds);
             sendRequestAsync(CMD_GET_ALLOWED_CARRIERS, callbackInfo);
         } finally {
             Binder.restoreCallingIdentity(identity);
@@ -9264,9 +9266,9 @@
     }
 
     @VisibleForTesting
-    public int validateCallerAndGetCarrierId(String packageName) {
+    public Set<Integer> validateCallerAndGetCarrierIds(String packageName) {
         CarrierAllowListInfo allowListInfo = CarrierAllowListInfo.loadInstance(mApp);
-        return allowListInfo.validateCallerAndGetCarrierId(packageName);
+        return allowListInfo.validateCallerAndGetCarrierIds(packageName);
     }
 
     /**
@@ -14000,19 +14002,19 @@
      */
     private static class CallerCallbackInfo {
         private final Consumer<Integer> mConsumer;
-        private final int mCarrierId;
+        private final Set<Integer> mCarrierIds;
 
-        public CallerCallbackInfo(Consumer<Integer> consumer, int carrierId) {
+        public CallerCallbackInfo(Consumer<Integer> consumer, Set<Integer> carrierIds) {
             mConsumer = consumer;
-            mCarrierId = carrierId;
+            mCarrierIds = carrierIds;
         }
 
         public Consumer<Integer> getConsumer() {
             return mConsumer;
         }
 
-        public int getCarrierId() {
-            return mCarrierId;
+        public Set<Integer> getCarrierIds() {
+            return mCarrierIds;
         }
     }
 
diff --git a/src/com/android/phone/TelephonyShellCommand.java b/src/com/android/phone/TelephonyShellCommand.java
index c55cc6c..429b7cc 100644
--- a/src/com/android/phone/TelephonyShellCommand.java
+++ b/src/com/android/phone/TelephonyShellCommand.java
@@ -3883,7 +3883,7 @@
     /**
      * Building the string that can be used to build the JsonObject which supports to stub the data
      * in CarrierAllowListInfo for CTS testing. sample format is like
-     * {"com.android.example":{"carrierId":"10000","callerSHA1Id":["XXXXXXXXXXXXXX"]}}
+     * {"com.android.example":{"carrierIds":[10000],"callerSHA1Id":["XXXXXXXXXXXXXX"]}}
      */
     private String convertToJsonString(int index, String param) {
 
@@ -3895,7 +3895,7 @@
                 break;
             case 1:
                 jSonString =
-                        "{" + QUOTES + token[0] + QUOTES + ":" + QUOTES + token[1] + QUOTES + ",";
+                        "{" + QUOTES + token[0] + QUOTES + ":" + "[" + token[1] + "],";
                 break;
             case 2:
                 jSonString =
diff --git a/src/com/android/phone/utils/CarrierAllowListInfo.java b/src/com/android/phone/utils/CarrierAllowListInfo.java
index 8e22cb9..62b71ff 100644
--- a/src/com/android/phone/utils/CarrierAllowListInfo.java
+++ b/src/com/android/phone/utils/CarrierAllowListInfo.java
@@ -37,6 +37,7 @@
 import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
@@ -47,7 +48,7 @@
     private static final String JSON_CHARSET = "UTF-8";
     private static final String MESSAGE_DIGEST_ALGORITHM = "SHA1";
     private static final String CALLER_SHA_1_ID = "callerSHA1Id";
-    private static final String CALLER_CARRIER_ID = "carrierId";
+    private static final String CALLER_CARRIER_ID = "carrierIds";
     public static final int INVALID_CARRIER_ID = -1;
 
     private static final String CARRIER_RESTRICTION_OPERATOR_REGISTERED_FILE =
@@ -68,11 +69,12 @@
         return mInstance;
     }
 
-    public int validateCallerAndGetCarrierId(String packageName) {
+    public Set<Integer> validateCallerAndGetCarrierIds(String packageName) {
         CarrierInfo carrierInfo = parseJsonForCallerInfo(packageName);
         boolean isValid = (carrierInfo != null) && validateCallerSignature(mContext, packageName,
                 carrierInfo.getSHAIdList());
-        return (isValid) ? carrierInfo.getCallerCarrierId() : INVALID_CARRIER_ID;
+        return (isValid) ? carrierInfo.getCallerCarrierIdList() : Collections.singleton(
+                INVALID_CARRIER_ID);
     }
 
     private void loadJsonFile(Context context) {
@@ -95,12 +97,18 @@
             if (mDataJSON != null && callerPackage != null) {
                 JSONObject callerJSON = mDataJSON.getJSONObject(callerPackage.trim());
                 JSONArray callerJSONArray = callerJSON.getJSONArray(CALLER_SHA_1_ID);
-                int carrierId = callerJSON.getInt(CALLER_CARRIER_ID);
+                JSONArray carrierIdArray = callerJSON.getJSONArray(CALLER_CARRIER_ID);
+
+                Set<Integer> carrierIds = new HashSet<>();
+                for (int index = 0; index < carrierIdArray.length(); index++) {
+                    carrierIds.add(carrierIdArray.getInt(index));
+                }
+
                 List<String> appSignatures = new ArrayList<>();
                 for (int index = 0; index < callerJSONArray.length(); index++) {
                     appSignatures.add((String) callerJSONArray.get(index));
                 }
-                return new CarrierInfo(carrierId, appSignatures);
+                return new CarrierInfo(carrierIds, appSignatures);
             }
         } catch (JSONException ex) {
             Rlog.e(LOG_TAG, "getCallerSignatureInfo: JSONException = " + ex);
@@ -183,16 +191,16 @@
     }
 
     private static class CarrierInfo {
-        final private int mCallerCarrierId;
+        final private Set<Integer> mCallerCarrierIdList;
         final private List<String> mSHAIdList;
 
-        public CarrierInfo(int carrierId, List<String> SHAIds) {
-            mCallerCarrierId = carrierId;
+        public CarrierInfo(Set<Integer> carrierIds, List<String> SHAIds) {
+            mCallerCarrierIdList = carrierIds;
             mSHAIdList = SHAIds;
         }
 
-        public int getCallerCarrierId() {
-            return mCallerCarrierId;
+        public Set<Integer> getCallerCarrierIdList() {
+            return mCallerCarrierIdList;
         }
 
         public List<String> getSHAIdList() {
@@ -203,7 +211,7 @@
     @TestApi
     public List<String> getShaIdList(String srcPkg, int carrierId) {
         CarrierInfo carrierInfo = parseJsonForCallerInfo(srcPkg);
-        if (carrierInfo != null && carrierInfo.getCallerCarrierId() == carrierId) {
+        if (carrierInfo != null && carrierInfo.getCallerCarrierIdList().contains(carrierId)) {
             return carrierInfo.getSHAIdList();
         }
         Rlog.e(LOG_TAG, "getShaIdList carrierId or shaIdList is empty");