Look up MMS APN using the active MMS APN name

Get the active MMS APN name from ConnectivityManager and use
that when retrieving the APN details (e.g. MMSC address).
The query will then return the APN that matches both the SIM
MCC/MNC and the active APN name.
If no match is found then fallback to a query based on MCC/MNC
only.
This fixes an issue where host network MMS APN details could be
used for MVNOs, or vice versa, preventing MMS from being sent or
received.
Bug: 17884903/18519308

Change-Id: I09e32d327e22ad31612a90e09c4aedfaf63870cc
diff --git a/src/com/android/mms/service/MmsNetworkManager.java b/src/com/android/mms/service/MmsNetworkManager.java
index 0f74c78..011549d 100644
--- a/src/com/android/mms/service/MmsNetworkManager.java
+++ b/src/com/android/mms/service/MmsNetworkManager.java
@@ -21,6 +21,7 @@
 import android.net.Network;
 import android.net.NetworkCapabilities;
 import android.net.NetworkRequest;
+import android.net.NetworkInfo;
 import android.os.SystemClock;
 import android.provider.Settings;
 import android.util.Log;
@@ -270,4 +271,28 @@
             return mMmsHttpClient;
         }
     }
+
+    /**
+     * Get the APN name for the active network
+     *
+     * @return The APN name if available, otherwise null
+     */
+    public String getApnName() {
+        Network network = null;
+        synchronized (this) {
+            if (mNetwork == null) {
+                Log.d(MmsService.TAG, "MmsNetworkManager: getApnName: network not available");
+                return null;
+            }
+            network = mNetwork;
+        }
+        String apnName = null;
+        final ConnectivityManager connectivityManager = getConnectivityManager();
+        NetworkInfo mmsNetworkInfo = connectivityManager.getNetworkInfo(network);
+        if (mmsNetworkInfo != null) {
+            apnName = mmsNetworkInfo.getExtraInfo();
+        }
+        Log.d(MmsService.TAG, "MmsNetworkManager: getApnName: " + apnName);
+        return apnName;
+    }
 }
diff --git a/src/com/android/mms/service/MmsRequest.java b/src/com/android/mms/service/MmsRequest.java
index 5314e67..842e2a0 100644
--- a/src/com/android/mms/service/MmsRequest.java
+++ b/src/com/android/mms/service/MmsRequest.java
@@ -132,8 +132,19 @@
             for (int i = 0; i < RETRY_TIMES; i++) {
                 try {
                     networkManager.acquireNetwork();
+                    final String apnName = networkManager.getApnName();
                     try {
-                        final ApnSettings apn = ApnSettings.load(context, null/*apnName*/, mSubId);
+                        ApnSettings apn = null;
+                        try {
+                            apn = ApnSettings.load(context, apnName, mSubId);
+                        } catch (ApnException e) {
+                            // If no APN could be found, fall back to trying without the APN name
+                            if (apnName == null) {
+                                throw (e); // If the APN name was already null then throw the exception on
+                            }
+                            Log.i(MmsService.TAG, "MmsRequest: No match with APN name:" + apnName + ", try with no name");
+                            apn = ApnSettings.load(context, null, mSubId);
+                        }
                         Log.i(MmsService.TAG, "MmsRequest: using " + apn.toString());
                         response = doHttp(context, networkManager, apn);
                         result = Activity.RESULT_OK;