Migrate Telephony PhoneAccountHandle ID from IccId to SubId in creation

Test: atest PhoneUtilsTest
Bug: 185235527

Change-Id: I6632d85b633e9e28e42e11dfa03dfd9a5399d293
diff --git a/src/com/android/phone/PhoneUtils.java b/src/com/android/phone/PhoneUtils.java
index 35cfdd3..22cff07 100644
--- a/src/com/android/phone/PhoneUtils.java
+++ b/src/com/android/phone/PhoneUtils.java
@@ -716,7 +716,7 @@
         // TODO: Should use some sort of special hidden flag to decorate this account as
         // an emergency-only account
         String id = isEmergency ? EMERGENCY_ACCOUNT_HANDLE_ID : prefix +
-                String.valueOf(phone.getFullIccSerialNumber());
+                String.valueOf(phone.getSubId());
         return makePstnPhoneAccountHandleWithPrefix(id, prefix, isEmergency);
     }
 
@@ -744,7 +744,7 @@
 
     public static Phone getPhoneForPhoneAccountHandle(PhoneAccountHandle handle) {
         if (handle != null && handle.getComponentName().equals(getPstnConnectionServiceName())) {
-            return getPhoneFromIccId(handle.getId());
+            return getPhoneFromSubId(handle.getId());
         }
         return null;
     }
@@ -759,18 +759,18 @@
      * {@code false} otherwise.
      */
     public static boolean isPhoneAccountActive(SubscriptionManager sm, PhoneAccountHandle handle) {
-        return sm.getActiveSubscriptionInfoForIcc(handle.getId()) != null;
+        return sm.getActiveSubscriptionInfo(Integer.parseInt(handle.getId())) != null;
     }
 
     private static ComponentName getPstnConnectionServiceName() {
         return PSTN_CONNECTION_SERVICE_COMPONENT;
     }
 
-    private static Phone getPhoneFromIccId(String iccId) {
-        if (!TextUtils.isEmpty(iccId)) {
+    private static Phone getPhoneFromSubId(String subId) {
+        if (!TextUtils.isEmpty(subId)) {
             for (Phone phone : PhoneFactory.getPhones()) {
-                String phoneIccId = phone.getFullIccSerialNumber();
-                if (iccId.equals(phoneIccId)) {
+                String phoneSubId = Integer.toString(phone.getSubId());
+                if (subId.equals(phoneSubId)) {
                     return phone;
                 }
             }
diff --git a/tests/src/com/android/phone/PhoneUtilsTest.java b/tests/src/com/android/phone/PhoneUtilsTest.java
new file mode 100644
index 0000000..df3a0ac
--- /dev/null
+++ b/tests/src/com/android/phone/PhoneUtilsTest.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.phone;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.when;
+
+import android.content.ComponentName;
+import android.telecom.PhoneAccountHandle;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import com.android.internal.telephony.GsmCdmaPhone;
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneFactory;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+@RunWith(AndroidJUnit4.class)
+public class PhoneUtilsTest {
+    @Mock
+    private SubscriptionManager mMockSubscriptionManager;
+    @Mock
+    private SubscriptionInfo mMockSubscriptionInfo;
+    @Mock
+    private GsmCdmaPhone mMockPhone;
+
+    private final int mPhoneAccountHandleIdInteger = 123;
+    private final String mPhoneAccountHandleIdString = "123";
+    private static final ComponentName PSTN_CONNECTION_SERVICE_COMPONENT = new ComponentName(
+            "com.android.phone", "com.android.services.telephony.TelephonyConnectionService");
+    private PhoneAccountHandle mPhoneAccountHandleTest = new PhoneAccountHandle(
+            PSTN_CONNECTION_SERVICE_COMPONENT, mPhoneAccountHandleIdString);
+
+    private HashMap<InstanceKey, Object> mOldInstances = new HashMap<InstanceKey, Object>();
+
+    private ArrayList<InstanceKey> mInstanceKeys = new ArrayList<InstanceKey>();
+
+    private static class InstanceKey {
+        public final Class mClass;
+        public final String mInstName;
+        public final Object mObj;
+        InstanceKey(final Class c, final String instName, final Object obj) {
+            mClass = c;
+            mInstName = instName;
+            mObj = obj;
+        }
+
+        @Override
+        public int hashCode() {
+            return (mClass.getName().hashCode() * 31 + mInstName.hashCode()) * 31;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (obj == null || !(obj instanceof InstanceKey)) {
+                return false;
+            }
+
+            InstanceKey other = (InstanceKey) obj;
+            return (other.mClass == mClass && other.mInstName.equals(mInstName)
+                    && other.mObj == mObj);
+        }
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        when(mMockSubscriptionManager.getActiveSubscriptionInfo(
+                eq(mPhoneAccountHandleIdInteger))).thenReturn(mMockSubscriptionInfo);
+        when(mMockPhone.getSubId()).thenReturn(mPhoneAccountHandleIdInteger);
+        setSinglePhone();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+    }
+
+    protected synchronized void replaceInstance(final Class c, final String instanceName,
+            final Object obj, final Object newValue)
+            throws Exception {
+        Field field = c.getDeclaredField(instanceName);
+        field.setAccessible(true);
+
+        InstanceKey key = new InstanceKey(c, instanceName, obj);
+        if (!mOldInstances.containsKey(key)) {
+            mOldInstances.put(key, field.get(obj));
+            mInstanceKeys.add(key);
+        }
+        field.set(obj, newValue);
+    }
+
+    private void setSinglePhone() throws Exception {
+        Phone[] mPhones = new Phone[] {mMockPhone};
+        replaceInstance(PhoneFactory.class, "sPhones", null, mPhones);
+    }
+
+    @Test
+    public void testIsPhoneAccountActive() throws Exception {
+        assertTrue(PhoneUtils.isPhoneAccountActive(
+                mMockSubscriptionManager, mPhoneAccountHandleTest));
+    }
+
+    @Test
+    public void testGetPhoneForPhoneAccountHandle() throws Exception {
+        assertEquals(mMockPhone, PhoneUtils.getPhoneForPhoneAccountHandle(
+                mPhoneAccountHandleTest));
+    }
+
+    @Test
+    public void testMakePstnPhoneAccountHandleWithPrefix() throws Exception {
+        PhoneAccountHandle phoneAccountHandleTest = new PhoneAccountHandle(
+                PSTN_CONNECTION_SERVICE_COMPONENT, mPhoneAccountHandleIdString);
+        assertEquals(phoneAccountHandleTest, PhoneUtils.makePstnPhoneAccountHandleWithPrefix(
+                mPhoneAccountHandleIdString, "", false));
+    }
+}