Merge a2a96d47281bb319324c310ba87a6ae5ca052d04 on remote branch

Change-Id: I2cc24a9eeff9adc91737eff7bab91715cf487333
diff --git a/MmsWrapper/src/com/android/mmswrapper/TelephonyManagerWrapper.java b/MmsWrapper/src/com/android/mmswrapper/TelephonyManagerWrapper.java
index 84ef11d..4a9b7d3 100755
--- a/MmsWrapper/src/com/android/mmswrapper/TelephonyManagerWrapper.java
+++ b/MmsWrapper/src/com/android/mmswrapper/TelephonyManagerWrapper.java
@@ -162,4 +162,10 @@
         LogUtils.logi(TAG, "isNetworkRoaming subId=" + subId + "flag=" + flag);
         return flag;
     }
+
+    public static String getMccMnc(Context context) {
+        TelephonyManager telephonyManager =
+                (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
+        return telephonyManager.getSimOperator();
+    }
 }
diff --git a/res/xml-mcc313-mnc340/sub_mms_config.xml b/res/xml-mcc313-mnc340/sub_mms_config.xml
new file mode 100755
index 0000000..8f6536c
--- /dev/null
+++ b/res/xml-mcc313-mnc340/sub_mms_config.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--Copyright (c) 2021, The Linux Foundation. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+    * Neither the name of The Linux Foundation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -->
+
+<mms_config version="4">
+   <string name="httpParams">X-MDN: ##LINE1##</string>
+</mms_config>
diff --git a/res/xml-mcc313-mnc350/sub_mms_config.xml b/res/xml-mcc313-mnc350/sub_mms_config.xml
new file mode 100755
index 0000000..8f6536c
--- /dev/null
+++ b/res/xml-mcc313-mnc350/sub_mms_config.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--Copyright (c) 2021, The Linux Foundation. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+    * Neither the name of The Linux Foundation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -->
+
+<mms_config version="4">
+   <string name="httpParams">X-MDN: ##LINE1##</string>
+</mms_config>
diff --git a/res/xml/sub_mms_config.xml b/res/xml/sub_mms_config.xml
new file mode 100755
index 0000000..9bb2503
--- /dev/null
+++ b/res/xml/sub_mms_config.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--Copyright (c) 2021, The Linux Foundation. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+    * Neither the name of The Linux Foundation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -->
+
+<mms_config version="4">
+    <string name="httpParams"></string>
+</mms_config>
diff --git a/src/com/android/mms/MmsConfig.java b/src/com/android/mms/MmsConfig.java
old mode 100644
new mode 100755
index 26eac2b..4bed0e5
--- a/src/com/android/mms/MmsConfig.java
+++ b/src/com/android/mms/MmsConfig.java
@@ -31,8 +31,12 @@
 import android.util.Log;
 
 import com.android.mms.ui.MessageUtils;
-import com.android.mms.ui.MessagingPreferenceActivity;
+import com.android.mmswrapper.SubscriptionManagerWrapper;
 import com.android.mmswrapper.ConstantsWrapper;
+import com.android.mmswrapper.TelephonyManagerWrapper;
+import android.content.res.Configuration;
+import android.telephony.SubscriptionManager;
+import android.telephony.SubscriptionInfo;
 
 public class MmsConfig {
     private static final String TAG = LogTag.TAG;
@@ -130,6 +134,7 @@
 
     private static final String MMS_DESTINATION = "9798";
 
+    private static final String[] mMdnInfoArray = new String[MessageUtils.getPhoneCount()];
     public static void init(Context context) {
         if (LOCAL_LOGV) {
             Log.v(TAG, "MmsConfig.init()");
@@ -139,7 +144,7 @@
                 android.os.SystemProperties.get(ConstantsWrapper.TelephonyProperty.PROPERTY_ICC_OPERATOR_NUMERIC));
 
         loadMmsSettings(context);
-
+        loadCarrierHttpSettings(context);
         MAX_SLIDE_NUM = context.getResources().getInteger(R.integer.max_slide_num);
     }
 
@@ -504,6 +509,128 @@
         }
     }
 
+    public static String getHttpParaBySubId(int subId) {
+        int phoneId = SubscriptionManagerWrapper.getPhoneId(subId);
+        if ((phoneId >= 0) && (phoneId < mMdnInfoArray.length)) {
+            return mMdnInfoArray[phoneId];
+        } else {
+            return null;
+        }
+
+    }
+
+    private static void loadCarrierHttpSettings(Context context) {
+        for (int i= 0; i< mMdnInfoArray.length; i++) {
+            if (MessageUtils.isIccCardActivated(i)) {
+                Log.d(TAG, "MDN: loadCarrierHttpSettings phone i " + i + " is active");
+                loadCarrierHttpSetting(context, i);
+            } else {
+                Log.d(TAG, "MDN: loadCarrierHttpSettings phone i " + i + " isn't active");
+            }
+        }
+    }
+
+    public static void loadCarrierHttpSetting(final Context context, final int phoneId) {
+        int subId = SubscriptionManagerWrapper.getSubIdBySlotId(phoneId);
+        Context subContext = getSubContext(context, subId);
+        Log.d(TAG, "MDN: loadCarrierHttpSetting: context " + context
+                + "; subContext: " + subContext
+                + "; phoneId: " + phoneId
+                + "; subId: " + subId);
+        if (subContext == null) {
+            Log.d(TAG, "MDN: loadCarrierHttpSetting subId " + subId + " context is null  " );
+            return;
+        }
+        XmlResourceParser parser = subContext.getResources().getXml(R.xml.sub_mms_config);
+        try {
+            beginDocument(parser, "mms_config");
+
+            while (true) {
+                nextElement(parser);
+                String tag = parser.getName();
+                if (tag == null) {
+                    break;
+                }
+                String name = parser.getAttributeName(0);
+                String value = parser.getAttributeValue(0);
+                String text = null;
+                if (parser.next() == XmlPullParser.TEXT) {
+                    text = parser.getText();
+                }
+
+                Log.v(TAG, "MDN: tag: " + tag + " value: " + value + " - " +
+                            text);
+
+                if ("name".equalsIgnoreCase(name)) {
+                    if ("string".equals(tag)) {
+                        if ("httpParams".equalsIgnoreCase(value)) {
+                            Log.d(TAG, "MDN: loadCarrierHttpSetting httpParams is   " + text);
+                            if ((phoneId >= 0) && (phoneId < mMdnInfoArray.length)) {
+                                mMdnInfoArray[phoneId] = text;
+                            }
+                        }
+                    }
+                }
+            }
+        } catch (XmlPullParserException e) {
+            Log.e(TAG, "loadMmsSettings caught ", e);
+        } catch (NumberFormatException e) {
+            Log.e(TAG, "loadMmsSettings caught ", e);
+        } catch (IOException e) {
+            Log.e(TAG, "loadMmsSettings caught ", e);
+        } finally {
+            parser.close();
+        }
+    }
+
+    private static Context getSubContext(final Context context, final int subId) {
+        final String[] mccMnc = getMccMnc(context, subId);
+        if (TextUtils.isEmpty(mccMnc[0]) || TextUtils.isEmpty(mccMnc[1])) {
+            return null;
+        }
+        final Configuration subConfig = new Configuration();
+        subConfig.mcc = Integer.parseInt(mccMnc[0]);
+        subConfig.mnc = Integer.parseInt(mccMnc[1]);
+        return context.createConfigurationContext(subConfig);
+    }
+
+    static String[] getMccMnc(final Context context, final int subId) {
+        String[] mccMnc =  new String[2];
+        String mcc;
+        String mnc;
+        StringBuilder sb =  new StringBuilder();
+        if (MessageUtils.isMultiSimEnabledMms()) {
+            Log.d(TAG,"MDN: getMccMnc: Support multi sim.");
+            final SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
+            final SubscriptionInfo subInfo = subscriptionManager.getActiveSubscriptionInfo(subId);
+            if (subInfo != null) {
+                mcc = subInfo.getMccString();
+                mnc = subInfo.getMncString();
+                Log.d(TAG,"MDN: getMccMnc: Mcc is: " + mcc
+                        + "; Mnc is: " + mnc);
+                mccMnc[0] = mcc;
+                mccMnc[1] = mnc;
+            }
+        } else {
+            Log.d(TAG,"MDN: getMccMnc: Do't support multi sim.");
+            final String mccMncString = TelephonyManagerWrapper.getMccMnc(context);
+            Log.d(TAG,"MDN: getMccMnc: MccMnc is: " + mccMncString);
+            try {
+                mcc = mccMncString.substring(0, 3);
+                mnc = mccMncString.substring(3);
+                Log.d(TAG,"MDN: getMccMnc: Mcc is: " + mcc
+                        + "; Mnc is: " + mnc);
+                mccMnc[0] = mcc;
+                mccMnc[1] = mnc;
+            } catch (Exception e) {
+                Log.w(TAG, "MDN: Invalid mcc/mnc from system " + mccMncString + ": " + e);
+            }
+        }
+        Log.d(TAG,"MDN: getMccMnc: final Mcc_mnc is: "
+                + mccMnc[0] + "_" + mccMnc[1]);
+        return mccMnc;
+    }
+
     public static String getMmsDestination() {
         return MMS_DESTINATION;
     }
diff --git a/src/com/android/mms/transaction/HttpUtils.java b/src/com/android/mms/transaction/HttpUtils.java
index 95df40b..0183e65 100755
--- a/src/com/android/mms/transaction/HttpUtils.java
+++ b/src/com/android/mms/transaction/HttpUtils.java
@@ -339,10 +339,9 @@
                 connection.setRequestMethod(METHOD_POST);
                 connection.setRequestProperty(HEADER_CONTENT_TYPE,
                     HEADER_VALUE_CONTENT_TYPE_WITHOUT_CHARSET);
-
-                if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
+             //   if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
                     logHttpHeaders(connection.getRequestProperties());
-                }
+                //}
                 connection.setFixedLengthStreamingMode(pdu.length);
                 // Sending request body
                 final OutputStream out = new BufferedOutputStream(connection.getOutputStream());
@@ -417,8 +416,8 @@
 
     private static void addExtraHeaders(Context context,
             HttpURLConnection connection, int subId) {
-        final String extraHttpParams = MmsConfig.getHttpParams();
-
+        final String extraHttpParams = MmsConfig.getHttpParaBySubId(subId);
+        Log.d(TAG,"HttpUtils.addExtraHeaders: httpParamters is " + extraHttpParams);
         if (!TextUtils.isEmpty(extraHttpParams)) {
             // Parse the parameter list
             String paramList[] = extraHttpParams.split("\\|");
@@ -426,7 +425,10 @@
                 String splitPair[] = paramPair.split(":", 2);
                 if (splitPair.length == 2) {
                     final String name = splitPair[0].trim();
+                    Log.d(TAG,"HttpUtils.addExtraHeaders: name is " + name
+                            + "; value is " + splitPair[1].trim());
                     final String value = resolveMacro(context, splitPair[1].trim(), subId);
+                    Log.d(TAG,"HttpUtils.addExtraHeaders: resolver value is " + value);
                     if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(value)) {
                         // Add the header if the param is valid
                        connection.setRequestProperty(name, value);
@@ -585,9 +587,13 @@
                     for (String value : values) {
                         sb.append(key).append('=').append(value).append('\n');
                     }
+                    Log.v(TAG, "HTTP: headers key is " + key
+                            + "; value is " + String.join(",",values));
                 }
             }
             Log.v(TAG, "HTTP: headers\n" + sb.toString());
+        } else {
+            Log.d("Mms","headers is null");
         }
     }
 
diff --git a/src/com/android/mms/transaction/MmsSystemEventReceiver.java b/src/com/android/mms/transaction/MmsSystemEventReceiver.java
index 4ec90d0..d9a3423 100755
--- a/src/com/android/mms/transaction/MmsSystemEventReceiver.java
+++ b/src/com/android/mms/transaction/MmsSystemEventReceiver.java
@@ -38,6 +38,8 @@
 import com.android.mmswrapper.ConstantsWrapper;
 import com.android.mmswrapper.ConnectivityManagerWrapper;
 
+import com.android.mmswrapper.SubscriptionManagerWrapper;
+import com.android.mms.MmsConfig;
 /**
  * MmsSystemEventReceiver receives the
  * {@link android.content.intent.ACTION_BOOT_COMPLETED},
@@ -72,6 +74,7 @@
         }
 
         String action = intent.getAction();
+        Log.d(TAG, "MDN: SIM state change 1: " + action);
         if (action.equals(Mms.Intents.CONTENT_CHANGED_ACTION)) {
             Uri changed = (Uri) intent.getParcelableExtra(Mms.Intents.DELETED_CONTENTS);
             MmsApp.getApplication().getPduLoaderManager().removePdu(changed);
@@ -98,12 +101,20 @@
             }
         } else if (action.equals(ConstantsWrapper.TelephonyIntent.ACTION_SIM_STATE_CHANGED)) {
             String stateExtra = intent.getStringExtra(ConstantsWrapper.IccCard.INTENT_KEY_ICC_STATE);
+            int slotId = intent.getIntExtra(ConstantsWrapper.Phone.PHONE_KEY,
+                    SubscriptionManagerWrapper.INVALID_PHONE_INDEX);
+            Log.d(TAG, "MDN: SIM state slotId: " + slotId
+                    + "; stateExtra: " + stateExtra
+                    + "; action: " + action);
             if (ConstantsWrapper.IccCard.INTENT_VALUE_ICC_LOADED.equals(stateExtra)) {
                 MessagingNotification.nonBlockingUpdateNewMessageIndicator(
                         context, MessagingNotification.THREAD_NONE, false);
                 if (isMmsNetworkReadyForPendingMsg(context)) {
                     wakeUpService(context);
                 }
+                if (slotId != SubscriptionManagerWrapper.INVALID_PHONE_INDEX) {
+                    MmsConfig.loadCarrierHttpSetting(context, slotId);
+                }
             }
         }
     }