Fast-fail when enabledMMS config is not true am: c13b57a4c1

Original change: https://android-review.googlesource.com/c/platform/packages/services/Mms/+/1197862

Change-Id: I079f0a97d70e7a4e4724caa7c74029817077859a
diff --git a/src/com/android/mms/service/MmsRequest.java b/src/com/android/mms/service/MmsRequest.java
index f8c882b..e24ae2d 100644
--- a/src/com/android/mms/service/MmsRequest.java
+++ b/src/com/android/mms/service/MmsRequest.java
@@ -36,7 +36,6 @@
 import android.telephony.ims.stub.ImsRegistrationImplBase;
 import android.telephony.SmsManager;
 import android.telephony.TelephonyManager;
-import android.text.TextUtils;
 
 import com.android.mms.service.exception.ApnException;
 import com.android.mms.service.exception.MmsHttpException;
@@ -99,8 +98,6 @@
     protected String mCreator;
     // MMS config
     protected Bundle mMmsConfig;
-    // MMS config overrides that will be applied to mMmsConfig when we eventually load it.
-    protected Bundle mMmsConfigOverrides;
     // Context used to get TelephonyManager.
     protected Context mContext;
     protected long mMessageId;
@@ -125,12 +122,11 @@
     }
 
     public MmsRequest(RequestManager requestManager, int subId, String creator,
-            Bundle configOverrides, Context context, long messageId) {
+            Bundle mmsConfig, Context context, long messageId) {
         mRequestManager = requestManager;
         mSubId = subId;
         mCreator = creator;
-        mMmsConfigOverrides = configOverrides;
-        mMmsConfig = null;
+        mMmsConfig = mmsConfig;
         mContext = context;
         mMessageId = messageId;
     }
@@ -139,34 +135,6 @@
         return mSubId;
     }
 
-    private boolean ensureMmsConfigLoaded() {
-        if (mMmsConfig == null) {
-            // Not yet retrieved from mms config manager. Try getting it.
-            final Bundle config = MmsConfigManager.getInstance().getMmsConfigBySubId(mSubId);
-            if (config != null) {
-                mMmsConfig = config;
-                // TODO: Make MmsConfigManager authoritative for user agent and don't consult
-                // TelephonyManager.
-                final TelephonyManager telephonyManager = ((TelephonyManager) mContext
-                        .getSystemService(Context.TELEPHONY_SERVICE))
-                        .createForSubscriptionId(mSubId);
-                final String userAgent = telephonyManager.getMmsUserAgent();
-                if (!TextUtils.isEmpty(userAgent)) {
-                    config.putString(SmsManager.MMS_CONFIG_USER_AGENT, userAgent);
-                }
-                final String userAgentProfileUrl = telephonyManager.getMmsUAProfUrl();
-                if (!TextUtils.isEmpty(userAgentProfileUrl)) {
-                    config.putString(SmsManager.MMS_CONFIG_UA_PROF_URL, userAgentProfileUrl);
-                }
-                // Apply overrides
-                if (mMmsConfigOverrides != null) {
-                    mMmsConfig.putAll(mMmsConfigOverrides);
-                }
-            }
-        }
-        return mMmsConfig != null;
-    }
-
     /**
      * Execute the request
      *
@@ -181,10 +149,7 @@
         byte[] response = null;
         // TODO: add mms data channel check back to fast fail if no way to send mms,
         // when telephony provides such API.
-        if (!ensureMmsConfigLoaded()) { // Check mms config
-            LogUtil.e(requestId, "mms config is not loaded yet");
-            result = SmsManager.MMS_ERROR_CONFIGURATION_ERROR;
-        } else if (!prepareForHttpRequest()) { // Prepare request, like reading pdu data from user
+        if (!prepareForHttpRequest()) { // Prepare request, like reading pdu data from user
             LogUtil.e(requestId, "Failed to prepare for request");
             result = SmsManager.MMS_ERROR_IO_ERROR;
         } else { // Execute
diff --git a/src/com/android/mms/service/MmsService.java b/src/com/android/mms/service/MmsService.java
index 40f13d9..a37ebe5 100644
--- a/src/com/android/mms/service/MmsService.java
+++ b/src/com/android/mms/service/MmsService.java
@@ -185,6 +185,24 @@
         }
     }
 
+    private Bundle loadMmsConfig(int subId) {
+        final Bundle config = MmsConfigManager.getInstance().getMmsConfigBySubId(subId);
+        if (config != null) {
+            // TODO: Make MmsConfigManager authoritative for user agent and don't consult
+            // TelephonyManager.
+            final TelephonyManager telephonyManager = getTelephonyManager(subId);
+            final String userAgent = telephonyManager.getMmsUserAgent();
+            if (!TextUtils.isEmpty(userAgent)) {
+                config.putString(SmsManager.MMS_CONFIG_USER_AGENT, userAgent);
+            }
+            final String userAgentProfileUrl = telephonyManager.getMmsUAProfUrl();
+            if (!TextUtils.isEmpty(userAgentProfileUrl)) {
+                config.putString(SmsManager.MMS_CONFIG_UA_PROF_URL, userAgentProfileUrl);
+            }
+        }
+        return config;
+    }
+
     private IMms.Stub mStub = new IMms.Stub() {
         @Override
         public void sendMessage(int subId, String callingPkg, Uri contentUri,
@@ -196,7 +214,7 @@
             // Make sure the subId is correct
             if (!SubscriptionManager.isValidSubscriptionId(subId)) {
                 LogUtil.e("Invalid subId " + subId);
-                sendErrorInPendingIntent(sentIntent);
+                sendErrorInPendingIntent(sentIntent, SmsManager.MMS_ERROR_NO_DATA_NETWORK);
                 return;
             }
             if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
@@ -205,13 +223,32 @@
 
             // Make sure the subId is active
             if (!isActiveSubId(subId)) {
-                sendErrorInPendingIntent(sentIntent);
+                sendErrorInPendingIntent(sentIntent, SmsManager.MMS_ERROR_NO_DATA_NETWORK);
+                return;
+            }
+
+            // Load MMS config
+            Bundle mmsConfig = loadMmsConfig(subId);
+            if (mmsConfig == null) {
+                LogUtil.e("MMS config is not loaded yet for subId " + subId);
+                sendErrorInPendingIntent(sentIntent, SmsManager.MMS_ERROR_CONFIGURATION_ERROR);
+                return;
+            }
+
+            // Apply overrides
+            if (configOverrides != null) {
+                mmsConfig.putAll(configOverrides);
+            }
+
+            // Make sure MMS is enabled
+            if (!mmsConfig.getBoolean(SmsManager.MMS_CONFIG_MMS_ENABLED)) {
+                LogUtil.e("MMS is not enabled for subId " + subId);
+                sendErrorInPendingIntent(sentIntent, SmsManager.MMS_ERROR_CONFIGURATION_ERROR);
                 return;
             }
 
             final SendRequest request = new SendRequest(MmsService.this, subId, contentUri,
-                    locationUrl, sentIntent, callingPkg, configOverrides, MmsService.this,
-                    messageId);
+                    locationUrl, sentIntent, callingPkg, mmsConfig, MmsService.this, messageId);
 
             final String carrierMessagingServicePackage =
                     getCarrierMessagingServicePackageIfExists(subId);
@@ -231,7 +268,7 @@
                 // AcknowledgeInd and NotifyRespInd are parts of downloading sequence.
                 // TODO: Should consider ReadRecInd(Read Report)?
                 sendSettingsIntentForFailedMms(!isRawPduSendReq(contentUri), subId);
-                sendErrorInPendingIntent(sentIntent);
+                sendErrorInPendingIntent(sentIntent, SmsManager.MMS_ERROR_NO_DATA_NETWORK);
                 return;
             }
 
@@ -253,7 +290,7 @@
             // Make sure the subId is correct
             if (!SubscriptionManager.isValidSubscriptionId(subId)) {
                 LogUtil.e("Invalid subId " + subId);
-                sendErrorInPendingIntent(downloadedIntent);
+                sendErrorInPendingIntent(downloadedIntent, SmsManager.MMS_ERROR_NO_DATA_NETWORK);
                 return;
             }
             if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
@@ -263,7 +300,8 @@
             if (!isActiveSubId(subId)) {
                 List<SubscriptionInfo> activeSubList = getActiveSubscriptionsInGroup(subId);
                 if (activeSubList.isEmpty()) {
-                    sendErrorInPendingIntent(downloadedIntent);
+                    sendErrorInPendingIntent(
+                            downloadedIntent, SmsManager.MMS_ERROR_NO_DATA_NETWORK);
                     return;
                 }
 
@@ -278,8 +316,30 @@
                 }
             }
 
+            // Load MMS config
+            Bundle mmsConfig = loadMmsConfig(subId);
+            if (mmsConfig == null) {
+                LogUtil.e("MMS config is not loaded yet for subId " + subId);
+                sendErrorInPendingIntent(
+                        downloadedIntent, SmsManager.MMS_ERROR_CONFIGURATION_ERROR);
+                return;
+            }
+
+            // Apply overrides
+            if (configOverrides != null) {
+                mmsConfig.putAll(configOverrides);
+            }
+
+            // Make sure MMS is enabled
+            if (!mmsConfig.getBoolean(SmsManager.MMS_CONFIG_MMS_ENABLED)) {
+                LogUtil.e("MMS is not enabled for subId " + subId);
+                sendErrorInPendingIntent(
+                        downloadedIntent, SmsManager.MMS_ERROR_CONFIGURATION_ERROR);
+                return;
+            }
+
             final DownloadRequest request = new DownloadRequest(MmsService.this, subId, locationUrl,
-                    contentUri, downloadedIntent, callingPkg, configOverrides, MmsService.this,
+                    contentUri, downloadedIntent, callingPkg, mmsConfig, MmsService.this,
                     messageId);
 
             final String carrierMessagingServicePackage =
@@ -294,7 +354,7 @@
             // Make sure subId has MMS data
             if (!getTelephonyManager(subId).isDataEnabledForApn(ApnSetting.TYPE_MMS)) {
                 sendSettingsIntentForFailedMms(/*isIncoming=*/ true, subId);
-                sendErrorInPendingIntent(downloadedIntent);
+                sendErrorInPendingIntent(downloadedIntent, SmsManager.MMS_ERROR_NO_DATA_NETWORK);
                 return;
             }
 
@@ -484,13 +544,15 @@
                 .isActiveSubscriptionId(subId);
         }
 
-        /*
-         * Calls the pending intent with <code>MMS_ERROR_NO_DATA_NETWORK</code>.
+        /**
+         * Calls the pending intent with one of these result codes:
+         * <code>MMS_ERROR_CONFIGURATION_ERROR</code>
+         * <code>MMS_ERROR_NO_DATA_NETWORK</code>.
          */
-        private void sendErrorInPendingIntent(@Nullable PendingIntent intent) {
+        private void sendErrorInPendingIntent(@Nullable PendingIntent intent, int resultCode) {
             if (intent != null) {
                 try {
-                    intent.send(SmsManager.MMS_ERROR_NO_DATA_NETWORK);
+                    intent.send(resultCode);
                 } catch (PendingIntent.CanceledException ex) {
                 }
             }