Fix replying from "combined account"

A bug to tracking the addition of the unit test has been filed
in bug 9823857

Bug: 9595801
Change-Id: Ie5eb8b5ea4cfddf9c111144c7b5b8ad2844e31b3
diff --git a/src/com/android/mail/compose/ComposeActivity.java b/src/com/android/mail/compose/ComposeActivity.java
index ed6ae53..fa31833 100644
--- a/src/com/android/mail/compose/ComposeActivity.java
+++ b/src/com/android/mail/compose/ComposeActivity.java
@@ -667,7 +667,7 @@
         // Update the from spinner as other accounts
         // may now be available.
         if (mFromSpinner != null && mAccount != null) {
-            mFromSpinner.asyncInitFromSpinner(mComposeMode, mAccount, mAccounts);
+            mFromSpinner.initialize(mComposeMode, mAccount, mAccounts, mRefMessage);
         }
     }
 
@@ -863,17 +863,17 @@
     }
 
     private void initFromSpinner(Bundle bundle, int action) {
-        String accountString = null;
         if (action == EDIT_DRAFT && mDraft.draftType == UIProvider.DraftType.COMPOSE) {
             action = COMPOSE;
         }
-        mFromSpinner.asyncInitFromSpinner(action, mAccount, mAccounts);
+        mFromSpinner.initialize(action, mAccount, mAccounts, mRefMessage);
+
         if (bundle != null) {
             if (bundle.containsKey(EXTRA_SELECTED_REPLY_FROM_ACCOUNT)) {
                 mReplyFromAccount = ReplyFromAccount.deserialize(mAccount,
                         bundle.getString(EXTRA_SELECTED_REPLY_FROM_ACCOUNT));
             } else if (bundle.containsKey(EXTRA_FROM_ACCOUNT_STRING)) {
-                accountString = bundle.getString(EXTRA_FROM_ACCOUNT_STRING);
+                final String accountString = bundle.getString(EXTRA_FROM_ACCOUNT_STRING);
                 mReplyFromAccount = mFromSpinner.getMatchingReplyFromAccount(accountString);
             }
         }
@@ -895,11 +895,11 @@
             // Otherwise, give the user the ability to choose which account to
             // send mail from / save drafts to.
             mFromStatic.setVisibility(View.GONE);
-            mFromStaticText.setText(mAccount.name);
+            mFromStaticText.setText(mReplyFromAccount.name);
             mFromSpinnerWrapper.setVisibility(View.VISIBLE);
         } else {
             mFromStatic.setVisibility(View.VISIBLE);
-            mFromStaticText.setText(mAccount.name);
+            mFromStaticText.setText(mReplyFromAccount.name);
             mFromSpinnerWrapper.setVisibility(View.GONE);
         }
     }
diff --git a/src/com/android/mail/compose/FromAddressSpinner.java b/src/com/android/mail/compose/FromAddressSpinner.java
index 94efe30..20d29f6 100644
--- a/src/com/android/mail/compose/FromAddressSpinner.java
+++ b/src/com/android/mail/compose/FromAddressSpinner.java
@@ -24,8 +24,10 @@
 import android.widget.Spinner;
 
 import com.android.mail.providers.Account;
+import com.android.mail.providers.Message;
 import com.android.mail.providers.ReplyFromAccount;
 import com.android.mail.utils.AccountUtils;
+import com.android.mail.utils.LogUtils;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
@@ -86,16 +88,29 @@
      *            accounts. Otherwise, show just the account this was launched
      *            with.
      * @param currentAccount Account used to launch activity.
-     * @param syncing accounts
+     * @param syncingAccounts
      */
-    public void asyncInitFromSpinner(int action, Account currentAccount,
-            Account[] syncingAccounts) {
+    public void initialize(int action, Account currentAccount, Account[] syncingAccounts,
+                Message refMessage) {
+        final List<Account> accounts = AccountUtils.mergeAccountLists(mAccounts,
+                syncingAccounts, true /* prioritizeAccountList */);
         if (action == ComposeActivity.COMPOSE) {
-            Account[] result = syncingAccounts;
-            mAccounts = AccountUtils
-                    .mergeAccountLists(mAccounts, result, true /* prioritizeAccountList */);
+            mAccounts = accounts;
         } else {
-            mAccounts = ImmutableList.of(currentAccount);
+            // First assume that we are going to use the current account as the reply account
+            Account replyAccount = currentAccount;
+
+            if (refMessage != null && refMessage.accountUri != null) {
+                // This is a reply or forward of a message access through the "combined" account.
+                // We want to make sure that the real account is in the spinner
+                for (Account account : accounts) {
+                    if (account.uri.equals(refMessage.accountUri)) {
+                        replyAccount = account;
+                        break;
+                    }
+                }
+            }
+            mAccounts = ImmutableList.of(replyAccount);
         }
         initFromSpinner();
     }