Merge "Implemented a new manually network selection API" am: 7a1f37a5cd
am: 870ecb3db7

Change-Id: I743d300f2b505fbaa6c5458eab8a829dc7264402
diff --git a/src/com/android/phone/CarrierConfigLoader.java b/src/com/android/phone/CarrierConfigLoader.java
index f20d1ec..0d04835 100644
--- a/src/com/android/phone/CarrierConfigLoader.java
+++ b/src/com/android/phone/CarrierConfigLoader.java
@@ -70,6 +70,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;
@@ -135,6 +136,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";
@@ -216,8 +220,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="
@@ -258,6 +263,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) {
@@ -278,8 +284,8 @@
                                     }
                                     PersistableBundle config =
                                             resultData.getParcelable(KEY_CONFIG_BUNDLE);
-                                    saveConfigToXml(
-                                            mPlatformCarrierConfigPackage, iccid, config);
+                                    saveConfigToXml(mPlatformCarrierConfigPackage,
+                                            iccid, gid1, config);
                                     mConfigFromDefaultApp[phoneId] = config;
                                     sendMessage(
                                             obtainMessage(
@@ -335,8 +341,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="
@@ -376,6 +383,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) {
@@ -396,8 +404,8 @@
                                     }
                                     PersistableBundle config =
                                             resultData.getParcelable(KEY_CONFIG_BUNDLE);
-                                    saveConfigToXml(
-                                            getCarrierPackageForPhoneId(phoneId), iccid, config);
+                                    saveConfigToXml(getCarrierPackageForPhoneId(phoneId),
+                                            iccid, gid1, config);
                                     mConfigFromCarrierApp[phoneId] = config;
                                     sendMessage(
                                             obtainMessage(
@@ -599,6 +607,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.
      *
@@ -610,9 +633,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;
@@ -635,7 +661,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);
@@ -670,10 +697,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);
@@ -688,7 +718,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");
 
@@ -751,7 +782,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";
     }
 
diff --git a/src/com/android/phone/NetworkSelectListPreference.java b/src/com/android/phone/NetworkSelectListPreference.java
index cec914a..8d947f7 100644
--- a/src/com/android/phone/NetworkSelectListPreference.java
+++ b/src/com/android/phone/NetworkSelectListPreference.java
@@ -96,7 +96,8 @@
         new AsyncTask<Void, Void, List<String>>() {
             @Override
             protected List<String> doInBackground(Void... voids) {
-                return Arrays.asList(telephonyManager.getForbiddenPlmns());
+                String[] forbiddenPlmns = telephonyManager.getForbiddenPlmns();
+                return forbiddenPlmns != null ? Arrays.asList(forbiddenPlmns) : null;
             }
 
             @Override
diff --git a/src/com/android/phone/NetworkSelectSetting.java b/src/com/android/phone/NetworkSelectSetting.java
index 56f0187..6aaaf9d 100644
--- a/src/com/android/phone/NetworkSelectSetting.java
+++ b/src/com/android/phone/NetworkSelectSetting.java
@@ -155,7 +155,8 @@
         new AsyncTask<Void, Void, List<String>>() {
             @Override
             protected List<String> doInBackground(Void... voids) {
-                return Arrays.asList(mTelephonyManager.getForbiddenPlmns());
+                String[] forbiddenPlmns = mTelephonyManager.getForbiddenPlmns();
+                return forbiddenPlmns != null ? Arrays.asList(forbiddenPlmns) : null;
             }
 
             @Override