Store subscriber ID / IMSI into telephony database

Bug: 131916175
Test: manual
Change-Id: I64cd12a6737b7ef7fb31b851c1cbb5b7f46e6ca7
Merged-In: I64cd12a6737b7ef7fb31b851c1cbb5b7f46e6ca7
diff --git a/src/java/com/android/internal/telephony/PhoneSubInfoController.java b/src/java/com/android/internal/telephony/PhoneSubInfoController.java
index 8d9a3d0..b33c7ce 100644
--- a/src/java/com/android/internal/telephony/PhoneSubInfoController.java
+++ b/src/java/com/android/internal/telephony/PhoneSubInfoController.java
@@ -125,8 +125,23 @@
     }
 
     public String getSubscriberIdForSubscriber(int subId, String callingPackage) {
-        return callPhoneMethodForSubIdWithReadSubscriberIdentifiersCheck(subId, callingPackage,
-                "getSubscriberId", (phone) -> phone.getSubscriberId());
+        Phone thePhone = getPhone(subId);
+        String message = "getSubscriberId";
+        if (thePhone != null) {
+            return callPhoneMethodForSubIdWithReadSubscriberIdentifiersCheck(subId, callingPackage,
+                    message, (phone) -> phone.getSubscriberId());
+        } else {
+            if (!TelephonyPermissions.checkCallingOrSelfReadSubscriberIdentifiers(
+                    mContext, subId, callingPackage, message)) {
+                return null;
+            }
+            final long identity = Binder.clearCallingIdentity();
+            try {
+                return SubscriptionController.getInstance().getImsi(subId);
+            } finally {
+                Binder.restoreCallingIdentity(identity);
+            }
+        }
     }
 
     /**
diff --git a/src/java/com/android/internal/telephony/SubscriptionController.java b/src/java/com/android/internal/telephony/SubscriptionController.java
index 1c86341..ba566a4 100644
--- a/src/java/com/android/internal/telephony/SubscriptionController.java
+++ b/src/java/com/android/internal/telephony/SubscriptionController.java
@@ -1823,6 +1823,53 @@
     }
 
     /**
+     * Set IMSI by subscription ID
+     * @param imsi IMSI (International Mobile Subscriber Identity)
+     * @return the number of records updated
+     */
+    public int setImsi(String imsi, int subId) {
+        if (DBG) logd("[setImsi]+ imsi:" + imsi + " subId:" + subId);
+        ContentValues value = new ContentValues(1);
+        value.put(SubscriptionManager.IMSI, imsi);
+
+        int result = mContext.getContentResolver().update(
+                SubscriptionManager.getUriForSubscriptionId(subId), value, null, null);
+
+        // Refresh the Cache of Active Subscription Info List
+        refreshCachedActiveSubscriptionInfoList();
+
+        notifySubscriptionInfoChanged();
+
+        return result;
+    }
+
+    /**
+     * Get IMSI by subscription ID
+     * @return imsi
+     */
+    public String getImsi(int subId) {
+        Cursor cursor = mContext.getContentResolver().query(
+                SubscriptionManager.getUriForSubscriptionId(subId), null,
+                SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID + "=?",
+                new String[]{String.valueOf(subId)}, null);
+
+        String imsi = null;
+        try {
+            if (cursor != null) {
+                if (cursor.moveToNext()) {
+                    imsi = getOptionalStringFromCursor(cursor, SubscriptionManager.IMSI,
+                            /*defaultVal*/ null);
+                }
+            }
+        } finally {
+            if (cursor != null) {
+                cursor.close();
+            }
+        }
+        return imsi;
+    }
+
+    /**
      * Set ISO country code by subscription ID
      * @param iso iso country code associated with the subscription
      * @param subId the unique SubInfoRecord index in database
@@ -2922,7 +2969,6 @@
     }
 
     /**
-     *
      * @param groupUuid a UUID assigned to the subscription group.
      * @param callingPackage the package making the IPC.
      * @return if callingPackage has carrier privilege on sublist.
diff --git a/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java b/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java
index 3ebe696..b6edcfa 100644
--- a/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java
+++ b/src/java/com/android/internal/telephony/SubscriptionInfoUpdater.java
@@ -453,7 +453,8 @@
         } else {
             for (SubscriptionInfo sub : subscriptionInfos) {
                 int subId = sub.getSubscriptionId();
-                TelephonyManager tm = TelephonyManager.getDefault();
+                TelephonyManager tm = (TelephonyManager)
+                        mContext.getSystemService(Context.TELEPHONY_SERVICE);
                 String operator = tm.getSimOperatorNumeric(subId);
 
                 if (!TextUtils.isEmpty(operator)) {
@@ -478,6 +479,11 @@
                     SubscriptionController.getInstance().setDisplayNumber(msisdn, subId);
                 }
 
+                String imsi = tm.createForSubscriptionId(subId).getSubscriberId();
+                if (imsi != null) {
+                    SubscriptionController.getInstance().setImsi(imsi, subId);
+                }
+
                 String[] ehplmns = records.getEhplmns();
                 String[] hplmns = records.getPlmnsFromHplmnActRecord();
                 if (ehplmns != null || hplmns != null) {
diff --git a/tests/telephonytests/src/com/android/internal/telephony/FakeTelephonyProvider.java b/tests/telephonytests/src/com/android/internal/telephony/FakeTelephonyProvider.java
index 9753722..48926b4 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/FakeTelephonyProvider.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/FakeTelephonyProvider.java
@@ -110,6 +110,7 @@
                     + SubscriptionManager.WHITE_LISTED_APN_DATA + " INTEGER DEFAULT 0,"
                     + SubscriptionManager.GROUP_OWNER + " TEXT,"
                     + SubscriptionManager.DATA_ENABLED_OVERRIDE_RULES + " TEXT"
+                    + SubscriptionManager.IMSI + " TEXT"
                     + ");";
         }
 
diff --git a/tests/telephonytests/src/com/android/internal/telephony/PhoneSubInfoControllerTest.java b/tests/telephonytests/src/com/android/internal/telephony/PhoneSubInfoControllerTest.java
index 989ba73..c6ef7fb 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/PhoneSubInfoControllerTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/PhoneSubInfoControllerTest.java
@@ -348,6 +348,13 @@
 
     @Test
     @SmallTest
+    public void testGetSubscriberIdWithInactiveSubId() {
+        //IMSI
+        assertNull(mPhoneSubInfoControllerUT.getSubscriberIdForSubscriber(2, TAG));
+    }
+
+    @Test
+    @SmallTest
     public void testGetSubscriberIdWithOutPermission() {
         // The READ_PRIVILEGED_PHONE_STATE permission, carrier privileges, or passing a device /
         // profile owner access check is required to access subscriber identifiers. Since none of