In reference to message

fixes b/6292922 "Reply by gmail / Forward" options in Gtalk search doesn't populate the data for To:, From:, subject & quoted text
Change-Id: If453783f56ebffad02630ba8d949ed1bbbf43532
diff --git a/src/com/android/mail/compose/ComposeActivity.java b/src/com/android/mail/compose/ComposeActivity.java
index 5e13e2f..9d49b78 100644
--- a/src/com/android/mail/compose/ComposeActivity.java
+++ b/src/com/android/mail/compose/ComposeActivity.java
@@ -22,11 +22,14 @@
 import android.app.ActivityManager;
 import android.app.AlertDialog;
 import android.app.Dialog;
+import android.app.LoaderManager;
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
+import android.content.CursorLoader;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.Loader;
 import android.content.pm.ActivityInfo;
 import android.database.Cursor;
 import android.net.Uri;
@@ -100,7 +103,7 @@
 
 public class ComposeActivity extends Activity implements OnClickListener, OnNavigationListener,
         RespondInlineListener, DialogInterface.OnClickListener, TextWatcher,
-        AttachmentDeletedListener, OnAccountChangedListener {
+        AttachmentDeletedListener, OnAccountChangedListener, LoaderManager.LoaderCallbacks<Cursor> {
     // Identifiers for which type of composition this is
     static final int COMPOSE = -1;
     static final int REPLY = 0;
@@ -109,7 +112,7 @@
     static final int EDIT_DRAFT = 3;
 
     // Integer extra holding one of the above compose action
-    private static final String EXTRA_ACTION = "action";
+    protected static final String EXTRA_ACTION = "action";
 
     private static final String EXTRA_SHOW_CC = "showCc";
     private static final String EXTRA_SHOW_BCC = "showBcc";
@@ -181,6 +184,7 @@
     private static final String EXTRA_FOCUS_SELECTION_START = "focusSelectionStart";
     private static final String EXTRA_FOCUS_SELECTION_END = null;
     private static final String EXTRA_MESSAGE = "extraMessage";
+    private static final int REFERENCE_MESSAGE_LOADER = 0;
 
     /**
      * A single thread for running tasks in the background.
@@ -344,7 +348,11 @@
             }
         }
 
-        if (message != null && action != EDIT_DRAFT) {
+        if (mRefMessageUri != null) {
+            // We have a referenced message that we must look up.
+            getLoaderManager().initLoader(REFERENCE_MESSAGE_LOADER, null, this);
+            return;
+        } else if (message != null && action != EDIT_DRAFT) {
             initFromDraftMessage(message);
             initQuotedTextFromRefMessage(mRefMessage, action);
             showCcBcc(savedInstanceState);
@@ -375,21 +383,16 @@
         } else if ((action == REPLY || action == REPLY_ALL || action == FORWARD)) {
             if (mRefMessage != null) {
                 initFromRefMessage(action, mAccount.name);
-                if (mRefMessage != null) {
-                    // CC field only gets populated when doing REPLY_ALL.
-                    // BCC never gets auto-populated, unless the user is editing
-                    // a draft with one.
-                    if (!TextUtils.isEmpty(mRefMessage.cc) && action == REPLY_ALL) {
-                        mCcBccView.show(false, true, false);
-                    }
-                }
-                updateHideOrShowCcBcc();
                 showQuotedText = true;
             }
         } else {
             initFromExtras(intent);
         }
+        finishSetup(action, intent, savedInstanceState, showQuotedText);
+    }
 
+    private void finishSetup(int action, Intent intent, Bundle savedInstanceState,
+            boolean showQuotedText) {
         if (action == COMPOSE) {
             mQuotedTextView.setVisibility(View.GONE);
         }
@@ -873,6 +876,19 @@
     }
 
     private void initFromRefMessage(int action, String recipientAddress) {
+        setFieldsFromRefMessage(action, recipientAddress);
+        if (mRefMessage != null) {
+            // CC field only gets populated when doing REPLY_ALL.
+            // BCC never gets auto-populated, unless the user is editing
+            // a draft with one.
+            if (!TextUtils.isEmpty(mRefMessage.cc) && action == REPLY_ALL) {
+                mCcBccView.show(false, true, false);
+            }
+        }
+        updateHideOrShowCcBcc();
+    }
+
+    private void setFieldsFromRefMessage(int action, String recipientAddress) {
         setSubject(mRefMessage, action);
         // Setup recipients
         if (action == FORWARD) {
@@ -1528,7 +1544,7 @@
         Folder defaultInbox = new Folder();
         defaultInbox.uri = mAccount.settings.defaultInbox;
         final Intent mailIntent =
-                Utils.createViewFolderIntent(defaultInbox, mAccount, false);
+                Utils.createViewFolderIntent(defaultInbox, mAccount);
 
         mailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
                 Intent.FLAG_ACTIVITY_TASK_ON_HOME);
@@ -2236,7 +2252,7 @@
         if (initialComposeMode != mComposeMode) {
             resetMessageForModeChange();
             if (mDraft == null && mRefMessage != null) {
-                initFromRefMessage(mComposeMode, mAccount.name);
+                setFieldsFromRefMessage(mComposeMode, mAccount.name);
             }
             boolean showCc = false;
             boolean showBcc = false;
@@ -2584,4 +2600,43 @@
     protected ArrayList<Attachment> getAttachments() {
         return mAttachmentsView.getAttachments();
     }
-}
+
+    @Override
+    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
+        switch (id) {
+            case REFERENCE_MESSAGE_LOADER:
+                return new CursorLoader(this, mRefMessageUri, UIProvider.MESSAGE_PROJECTION, null,
+                        null, null);
+        }
+        return null;
+    }
+
+    @Override
+    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
+        if (data != null && data.moveToFirst()) {
+            mRefMessage = new Message(data);
+            // We set these based on EXTRA_TO.
+            mRefMessage.to = null;
+            mRefMessage.from = null;
+            Intent intent = getIntent();
+            int action = intent.getIntExtra(EXTRA_ACTION, COMPOSE);
+            initFromRefMessage(action, mAccount.name);
+            finishSetup(action, intent, null, true);
+            if (action != FORWARD) {
+                String to = intent.getStringExtra(EXTRA_TO);
+                if (!TextUtils.isEmpty(to)) {
+                    clearChangeListeners();
+                    mTo.append(to);
+                    initChangeListeners();
+                }
+            }
+        } else {
+            finish();
+        }
+    }
+
+    @Override
+    public void onLoaderReset(Loader<Cursor> arg0) {
+        // Do nothing.
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/mail/ui/FolderSelectionActivity.java b/src/com/android/mail/ui/FolderSelectionActivity.java
index c101ec0..e6b605e 100644
--- a/src/com/android/mail/ui/FolderSelectionActivity.java
+++ b/src/com/android/mail/ui/FolderSelectionActivity.java
@@ -174,8 +174,8 @@
                  * account, calculate the human readable name of the folder and
                  * use it as the shortcut name, etc...
                  */
-                final Intent clickIntent = Utils.createViewFolderIntent(mSelectedFolder, mAccount,
-                        true);
+                final Intent clickIntent = Utils.createViewFolderIntent(mSelectedFolder,
+                        mAccount);
                 resultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, clickIntent);
                 resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                         Intent.ShortcutIconResource.fromContext(this,
diff --git a/src/com/android/mail/utils/Utils.java b/src/com/android/mail/utils/Utils.java
index 1156db0..6b1670f 100644
--- a/src/com/android/mail/utils/Utils.java
+++ b/src/com/android/mail/utils/Utils.java
@@ -629,12 +629,9 @@
      * @param folderUri Folder uri.
      * @param account
      * @param folder Folder to open.
-     * @param pendingIntent If this will be used as a pending intent we need to
-     *            send strings not parcelables.
      * @return
      */
-    public static Intent createViewFolderIntent(Folder folder, Account account,
-            boolean pendingIntent) {
+    public static Intent createViewFolderIntent(Folder folder, Account account) {
         final Intent intent = new Intent(Intent.ACTION_VIEW);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK
                 | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
diff --git a/src/com/android/mail/widget/BaseWidgetProvider.java b/src/com/android/mail/widget/BaseWidgetProvider.java
index 5e74d23..dee4c1d 100644
--- a/src/com/android/mail/widget/BaseWidgetProvider.java
+++ b/src/com/android/mail/widget/BaseWidgetProvider.java
@@ -344,7 +344,7 @@
         intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
         remoteViews.setRemoteAdapter(R.id.conversation_list, intent);
         // Open mail app when click on header
-        final Intent mailIntent = Utils.createViewFolderIntent(folder, account, false);
+        final Intent mailIntent = Utils.createViewFolderIntent(folder, account);
         PendingIntent clickIntent = PendingIntent.getActivity(context, 0, mailIntent,
                 PendingIntent.FLAG_UPDATE_CURRENT);
         remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);
diff --git a/src/com/android/mail/widget/WidgetService.java b/src/com/android/mail/widget/WidgetService.java
index 0e3b83e..182f8ed 100644
--- a/src/com/android/mail/widget/WidgetService.java
+++ b/src/com/android/mail/widget/WidgetService.java
@@ -249,7 +249,7 @@
             view.setTextViewText(
                     R.id.loading_text, mContext.getText(R.string.view_more_conversations));
             view.setOnClickFillInIntent(R.id.widget_loading,
-                    Utils.createViewFolderIntent(mFolder, mAccount, false));
+                    Utils.createViewFolderIntent(mFolder, mAccount));
             return view;
         }