Merge "Show only APNs belonging to carrier (mno/mvno) under apn settings." into mnc-dev
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 4bf8399..c006ce3 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -346,12 +346,14 @@
             </intent-filter>
         </activity-alias>
 
+        <!-- Runs in the phone process since it needs access to UiccController -->
         <activity android:name="Settings$ApnSettingsActivity"
                 android:label="@string/apn_settings"
                 android:launchMode="singleTask"
                 android:taskAffinity="com.android.settings"
                 android:configChanges="orientation|keyboardHidden|screenSize"
-                android:parentActivityName="Settings$WirelessSettingsActivity">
+                android:parentActivityName="Settings$WirelessSettingsActivity"
+                android:process="com.android.phone">
             <intent-filter android:priority="1">
                 <action android:name="android.settings.APN_SETTINGS" />
                 <category android:name="android.intent.category.DEFAULT" />
diff --git a/src/com/android/settings/ApnEditor.java b/src/com/android/settings/ApnEditor.java
index a040a91..7142fb6 100644
--- a/src/com/android/settings/ApnEditor.java
+++ b/src/com/android/settings/ApnEditor.java
@@ -98,6 +98,8 @@
     private Resources mRes;
     private TelephonyManager mTelephonyManager;
     private int mBearerInitialVal = 0;
+    private String mMvnoTypeStr;
+    private String mMvnoMatchDataStr;
 
     /**
      * Standard projection for the interesting columns of a normal note.
@@ -195,7 +197,8 @@
 
         final Intent intent = getIntent();
         final String action = intent.getAction();
-        mSubId = intent.getIntExtra("sub_id", SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+        mSubId = intent.getIntExtra(ApnSettings.SUB_ID,
+                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
 
         mFirstTime = icicle == null;
 
@@ -209,6 +212,8 @@
                         icicle.getInt(SAVED_POS));
             }
             mNewApn = true;
+            mMvnoTypeStr = intent.getStringExtra(ApnSettings.MVNO_TYPE);
+            mMvnoMatchDataStr = intent.getStringExtra(ApnSettings.MVNO_MATCH_DATA);
             // If we were unable to create a new note, then just finish
             // this activity.  A RESULT_CANCELED will be sent back to the
             // original activity if they requested a result.
@@ -325,6 +330,10 @@
             mMvnoType.setValue(mCursor.getString(MVNO_TYPE_INDEX));
             mMvnoMatchData.setEnabled(false);
             mMvnoMatchData.setText(mCursor.getString(MVNO_MATCH_DATA_INDEX));
+            if (mNewApn && mMvnoTypeStr != null && mMvnoMatchDataStr != null) {
+                mMvnoType.setValue(mMvnoTypeStr);
+                mMvnoMatchData.setText(mMvnoMatchDataStr);
+            }
         }
 
         mName.setSummary(checkNull(mName.getText()));
diff --git a/src/com/android/settings/ApnSettings.java b/src/com/android/settings/ApnSettings.java
index 1b32b86..0a7e84c 100644
--- a/src/com/android/settings/ApnSettings.java
+++ b/src/com/android/settings/ApnSettings.java
@@ -40,6 +40,7 @@
 import android.provider.Telephony;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuInflater;
@@ -50,6 +51,10 @@
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.telephony.PhoneConstants;
 import com.android.internal.telephony.TelephonyIntents;
+import com.android.internal.telephony.dataconnection.ApnSetting;
+import com.android.internal.telephony.uicc.IccRecords;
+import com.android.internal.telephony.uicc.UiccController;
+
 import android.telephony.TelephonyManager;
 
 import java.util.ArrayList;
@@ -65,11 +70,16 @@
         "content://telephony/carriers/preferapn";
 
     public static final String APN_ID = "apn_id";
+    public static final String SUB_ID = "sub_id";
+    public static final String MVNO_TYPE = "mvno_type";
+    public static final String MVNO_MATCH_DATA = "mvno_match_data";
 
     private static final int ID_INDEX = 0;
     private static final int NAME_INDEX = 1;
     private static final int APN_INDEX = 2;
     private static final int TYPES_INDEX = 3;
+    private static final int MVNO_TYPE_INDEX = 4;
+    private static final int MVNO_MATCH_DATA_INDEX = 5;
 
     private static final int MENU_NEW = Menu.FIRST;
     private static final int MENU_RESTORE = Menu.FIRST + 1;
@@ -88,6 +98,9 @@
     private RestoreApnProcessHandler mRestoreApnProcessHandler;
     private HandlerThread mRestoreDefaultApnThread;
     private SubscriptionInfo mSubscriptionInfo;
+    private UiccController mUiccController;
+    private String mMvnoType;
+    private String mMvnoMatchData;
 
     private UserManager mUm;
 
@@ -134,7 +147,7 @@
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         final Activity activity = getActivity();
-        final int subId = activity.getIntent().getIntExtra("sub_id",
+        final int subId = activity.getIntent().getIntExtra(SUB_ID,
                 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
 
         mUm = (UserManager) getSystemService(Context.USER_SERVICE);
@@ -147,6 +160,7 @@
         }
 
         mSubscriptionInfo = SubscriptionManager.from(activity).getActiveSubscriptionInfo(subId);
+        mUiccController = UiccController.getInstance();
     }
 
     @Override
@@ -215,14 +229,22 @@
             + "\" AND NOT (type='ia' AND (apn=\"\" OR apn IS NULL))";
 
         Cursor cursor = getContentResolver().query(Telephony.Carriers.CONTENT_URI, new String[] {
-                "_id", "name", "apn", "type"}, where, null,
+                "_id", "name", "apn", "type", "mvno_type", "mvno_match_data"}, where, null,
                 Telephony.Carriers.DEFAULT_SORT_ORDER);
 
         if (cursor != null) {
+            IccRecords r = null;
+            if (mUiccController != null && mSubscriptionInfo != null) {
+                r = mUiccController.getIccRecords(SubscriptionManager.getPhoneId(
+                        mSubscriptionInfo.getSubscriptionId()), UiccController.APP_FAM_3GPP);
+            }
             PreferenceGroup apnList = (PreferenceGroup) findPreference("apn_list");
             apnList.removeAll();
 
-            ArrayList<Preference> mmsApnList = new ArrayList<Preference>();
+            ArrayList<ApnPreference> mnoApnList = new ArrayList<ApnPreference>();
+            ArrayList<ApnPreference> mvnoApnList = new ArrayList<ApnPreference>();
+            ArrayList<ApnPreference> mnoMmsApnList = new ArrayList<ApnPreference>();
+            ArrayList<ApnPreference> mvnoMmsApnList = new ArrayList<ApnPreference>();
 
             mSelectedKey = getSelectedApnKey();
             cursor.moveToFirst();
@@ -231,6 +253,8 @@
                 String apn = cursor.getString(APN_INDEX);
                 String key = cursor.getString(ID_INDEX);
                 String type = cursor.getString(TYPES_INDEX);
+                String mvnoType = cursor.getString(MVNO_TYPE_INDEX);
+                String mvnoMatchData = cursor.getString(MVNO_MATCH_DATA_INDEX);
 
                 ApnPreference pref = new ApnPreference(getActivity());
 
@@ -246,17 +270,42 @@
                     if ((mSelectedKey != null) && mSelectedKey.equals(key)) {
                         pref.setChecked();
                     }
-                    apnList.addPreference(pref);
+                    addApnToList(pref, mnoApnList, mvnoApnList, r, mvnoType, mvnoMatchData);
                 } else {
-                    mmsApnList.add(pref);
+                    addApnToList(pref, mnoMmsApnList, mvnoMmsApnList, r, mvnoType, mvnoMatchData);
                 }
                 cursor.moveToNext();
             }
             cursor.close();
 
-            for (Preference preference : mmsApnList) {
+            if (!mvnoApnList.isEmpty()) {
+                mnoApnList = mvnoApnList;
+                mnoMmsApnList = mvnoMmsApnList;
+
+                // Also save the mvno info
+            }
+
+            for (Preference preference : mnoApnList) {
                 apnList.addPreference(preference);
             }
+            for (Preference preference : mnoMmsApnList) {
+                apnList.addPreference(preference);
+            }
+        }
+    }
+
+    private void addApnToList(ApnPreference pref, ArrayList<ApnPreference> mnoList,
+                              ArrayList<ApnPreference> mvnoList, IccRecords r, String mvnoType,
+                              String mvnoMatchData) {
+        if (r != null && !TextUtils.isEmpty(mvnoType) && !TextUtils.isEmpty(mvnoMatchData)) {
+            if (ApnSetting.mvnoMatches(r, mvnoType, mvnoMatchData)) {
+                mvnoList.add(pref);
+                // Since adding to mvno list, save mvno info
+                mMvnoType = mvnoType;
+                mMvnoMatchData = mvnoMatchData;
+            }
+        } else {
+            mnoList.add(pref);
         }
     }
 
@@ -293,7 +342,11 @@
         Intent intent = new Intent(Intent.ACTION_INSERT, Telephony.Carriers.CONTENT_URI);
         int subId = mSubscriptionInfo != null ? mSubscriptionInfo.getSubscriptionId()
                 : SubscriptionManager.INVALID_SUBSCRIPTION_ID;
-        intent.putExtra("sub_id", subId);
+        intent.putExtra(SUB_ID, subId);
+        if (!TextUtils.isEmpty(mMvnoType) && !TextUtils.isEmpty(mMvnoMatchData)) {
+            intent.putExtra(MVNO_TYPE, mMvnoType);
+            intent.putExtra(MVNO_MATCH_DATA, mMvnoMatchData);
+        }
         startActivity(intent);
     }