Store config file with gid1

Store and look up for config file with iccid
and gid1.

Bug: 111755653
Bug: 73538013
Test: Carrier config loading on various carriers

Change-Id: If349bbf2b7bad42738d8b08f3c7e8365062e55b3
diff --git a/src/com/android/phone/CarrierConfigLoader.java b/src/com/android/phone/CarrierConfigLoader.java
index 0424a03..f216b79 100644
--- a/src/com/android/phone/CarrierConfigLoader.java
+++ b/src/com/android/phone/CarrierConfigLoader.java
@@ -72,6 +72,7 @@
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.lang.Math;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -137,6 +138,9 @@
 
     private static final int BIND_TIMEOUT_MILLIS = 30000;
 
+    // Length limit of gid1 for naming config file.
+    private static final int GID1_LENGTH_LIMIT = 20;
+
     // Tags used for saving and restoring XML documents.
     private static final String TAG_DOCUMENT = "carrier_config";
     private static final String TAG_VERSION = "package_version";
@@ -218,8 +222,9 @@
                 case EVENT_DO_FETCH_DEFAULT:
                 {
                     final String iccid = getIccIdForPhoneId(phoneId);
+                    final String gid1 = getGid1ForPhoneId(phoneId);
                     final PersistableBundle config =
-                            restoreConfigFromXml(mPlatformCarrierConfigPackage, iccid);
+                            restoreConfigFromXml(mPlatformCarrierConfigPackage, iccid, gid1);
                     if (config != null) {
                         log(
                                 "Loaded config from XML. package="
@@ -260,6 +265,7 @@
                     }
                     final CarrierIdentifier carrierId = getCarrierIdForPhoneId(phoneId);
                     final String iccid = getIccIdForPhoneId(phoneId);
+                    final String gid1 = getGid1ForPhoneId(phoneId);
                     // ResultReceiver callback will execute in this Handler's thread.
                     final ResultReceiver resultReceiver =
                             new ResultReceiver(this) {
@@ -280,8 +286,8 @@
                                     }
                                     PersistableBundle config =
                                             resultData.getParcelable(KEY_CONFIG_BUNDLE);
-                                    saveConfigToXml(
-                                            mPlatformCarrierConfigPackage, iccid, config);
+                                    saveConfigToXml(mPlatformCarrierConfigPackage,
+                                            iccid, gid1, config);
                                     mConfigFromDefaultApp[phoneId] = config;
                                     sendMessage(
                                             obtainMessage(
@@ -337,8 +343,9 @@
                 {
                     final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
                     final String iccid = getIccIdForPhoneId(phoneId);
+                    final String gid1 = getGid1ForPhoneId(phoneId);
                     final PersistableBundle config =
-                            restoreConfigFromXml(carrierPackageName, iccid);
+                            restoreConfigFromXml(carrierPackageName, iccid, gid1);
                     if (config != null) {
                         log(
                                 "Loaded config from XML. package="
@@ -378,6 +385,7 @@
                     }
                     final CarrierIdentifier carrierId = getCarrierIdForPhoneId(phoneId);
                     final String iccid = getIccIdForPhoneId(phoneId);
+                    final String gid1 = getGid1ForPhoneId(phoneId);
                     // ResultReceiver callback will execute in this Handler's thread.
                     final ResultReceiver resultReceiver =
                             new ResultReceiver(this) {
@@ -398,8 +406,8 @@
                                     }
                                     PersistableBundle config =
                                             resultData.getParcelable(KEY_CONFIG_BUNDLE);
-                                    saveConfigToXml(
-                                            getCarrierPackageForPhoneId(phoneId), iccid, config);
+                                    saveConfigToXml(getCarrierPackageForPhoneId(phoneId),
+                                            iccid, gid1, config);
                                     mConfigFromCarrierApp[phoneId] = config;
                                     sendMessage(
                                             obtainMessage(
@@ -601,6 +609,21 @@
         return phone.getIccSerialNumber();
     }
 
+    private String getGid1ForPhoneId(int phoneId) {
+        if (!SubscriptionManager.isValidPhoneId(phoneId)) {
+            return null;
+        }
+        Phone phone = PhoneFactory.getPhone(phoneId);
+        if (phone == null) {
+            return null;
+        }
+        String gid1 = phone.getGroupIdLevel1();
+        if (gid1 == null) {
+            return null;
+        }
+        return gid1.substring(0, Math.min(gid1.length(), GID1_LENGTH_LIMIT));
+    }
+
     /**
      * Writes a bundle to an XML file.
      *
@@ -612,9 +635,12 @@
      *
      * @param packageName the name of the package from which we fetched this bundle.
      * @param iccid the ICCID of the subscription for which this bundle was fetched.
+     * @param extras First 20 characters of gid1 of the subscription for which the bundle
+     *               was fetched.
      * @param config the bundle to be written. Null will be treated as an empty bundle.
      */
-    private void saveConfigToXml(String packageName, String iccid, PersistableBundle config) {
+    private void saveConfigToXml(String packageName, String iccid, String extras,
+            PersistableBundle config) {
         if (packageName == null || iccid == null) {
             loge("Cannot save config with null packageName or iccid.");
             return;
@@ -637,7 +663,8 @@
         FileOutputStream outFile = null;
         try {
             outFile = new FileOutputStream(
-                    new File(mContext.getFilesDir(), getFilenameForConfig(packageName, iccid)));
+                    new File(mContext.getFilesDir(), getFilenameForConfig(packageName,
+                            iccid, extras)));
             FastXmlSerializer out = new FastXmlSerializer();
             out.setOutput(outFile, "utf-8");
             out.startDocument("utf-8", true);
@@ -672,10 +699,13 @@
      *
      * @param packageName the name of the package from which we fetched this bundle.
      * @param iccid the ICCID of the subscription for which this bundle was fetched.
+     * @param extras First 20 characters of gid1 of the subscription for which the bundle
+     *               was fetched.
      * @return the bundle from the XML file. Returns null if there is no saved config, the saved
      *         version does not match, or reading config fails.
      */
-    private PersistableBundle restoreConfigFromXml(String packageName, String iccid) {
+    private PersistableBundle restoreConfigFromXml(String packageName, String iccid,
+            String extras) {
         final String version = getPackageVersion(packageName);
         if (version == null) {
             loge("Failed to get package version for: " + packageName);
@@ -690,7 +720,8 @@
         FileInputStream inFile = null;
         try {
             inFile = new FileInputStream(
-                    new File(mContext.getFilesDir(), getFilenameForConfig(packageName, iccid)));
+                    new File(mContext.getFilesDir(), getFilenameForConfig(packageName, iccid,
+                            extras)));
             XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
             parser.setInput(inFile, "utf-8");
 
@@ -753,7 +784,11 @@
     }
 
     /** Builds a canonical file name for a config file. */
-    private String getFilenameForConfig(@NonNull String packageName, @NonNull String iccid) {
+    private String getFilenameForConfig(@NonNull String packageName, @NonNull String iccid,
+            String extras) {
+        if (extras != null) {
+            return "carrierconfig-" + packageName + "-" + iccid + "-" + extras + ".xml";
+        }
         return "carrierconfig-" + packageName + "-" + iccid + ".xml";
     }