Cherry pick from jb-dev

https://android-git.corp.google.com/g/185790
https://android-git.corp.google.com/g/185965
This makes sure the task stack is correct from the widget

Change-Id: I595139a1601777e559a322fa71a8a2efc12ae2a9
diff --git a/src/com/android/mail/compose/ComposeActivity.java b/src/com/android/mail/compose/ComposeActivity.java
index 0dca32a..a28ca84 100644
--- a/src/com/android/mail/compose/ComposeActivity.java
+++ b/src/com/android/mail/compose/ComposeActivity.java
@@ -150,7 +150,7 @@
      *
      * @see #onAppUpPressed
      */
-    private static final String EXTRA_FROM_EMAIL_TASK = "fromemail";
+    public static final String EXTRA_FROM_EMAIL_TASK = "fromemail";
 
     static final String EXTRA_ATTACHMENTS = "attachments";
 
@@ -217,6 +217,12 @@
     private ImageView mAttachmentsButton;
 
     /**
+     * Boolean indicating whether ComposeActivity was launched from a Gmail controlled view.
+     */
+    private boolean mLaunchedFromEmail = false;
+
+
+    /**
      * Can be called from a non-UI thread.
      */
     public static void editDraft(Context launcher, Account account, Message message) {
@@ -299,6 +305,17 @@
             return;
         }
 
+        if (intent.getBooleanExtra(EXTRA_FROM_EMAIL_TASK, false)) {
+            mLaunchedFromEmail = true;
+        } else if (Intent.ACTION_SEND.equals(intent.getAction())) {
+            final Uri dataUri = intent.getData();
+            if (dataUri != null) {
+                final String dataScheme = intent.getData().getScheme();
+                final String accountScheme = mAccount.composeIntentUri.getScheme();
+                mLaunchedFromEmail = TextUtils.equals(dataScheme, accountScheme);
+            }
+        }
+
         if (message != null && action != EDIT_DRAFT) {
             initFromDraftMessage(message);
             initQuotedTextFromRefMessage(mRefMessage, action);
@@ -1389,7 +1406,7 @@
                 Utils.showSettings(this, mAccount);
                 break;
             case android.R.id.home:
-                finish();
+                onAppUpPressed();
                 break;
             case R.id.help_info_menu_item:
                 // TODO: enable context sensitive help
@@ -1405,6 +1422,26 @@
         return !handled ? super.onOptionsItemSelected(item) : handled;
     }
 
+    private void onAppUpPressed() {
+        if (mLaunchedFromEmail) {
+            // If this was started from Gmail, simply treat app up as the system back button, so
+            // that the last view is restored.
+            onBackPressed();
+            return;
+        }
+
+        // Fire the main activity to ensure it launches the "top" screen of mail.
+        // Since the main Activity is singleTask, it should revive that task if it was already
+        // started.
+        final Intent mailIntent =
+                Utils.createViewFolderIntent(mAccount.settings.defaultInbox, mAccount, null, false);
+
+        mailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
+                Intent.FLAG_ACTIVITY_TASK_ON_HOME);
+        startActivity(mailIntent);
+        finish();
+    }
+
     private void doSend() {
         sendOrSaveWithSanityChecks(false, true, false);
     }
diff --git a/src/com/android/mail/utils/Utils.java b/src/com/android/mail/utils/Utils.java
index b8e73cf..ef3dc6c 100644
--- a/src/com/android/mail/utils/Utils.java
+++ b/src/com/android/mail/utils/Utils.java
@@ -609,16 +609,35 @@
      */
     public static Intent createViewFolderIntent(Folder folder, Account account,
             boolean pendingIntent) {
+        return createViewFolderIntent(folder.uri, account, folder, pendingIntent);
+    }
+
+    /**
+     * Create an intent to open a folder.
+     *
+     * @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(Uri folderUri, Account account, Folder folder,
+            boolean pendingIntent) {
         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);
-        intent.setDataAndType(folder.uri, account.mimeType);
+        intent.setDataAndType(folderUri, account.mimeType);
         if (pendingIntent) {
             intent.putExtra(Utils.EXTRA_ACCOUNT_STRING, account.serialize());
-            intent.putExtra(Utils.EXTRA_FOLDER_STRING, folder.serialize());
+            if (folder != null) {
+                intent.putExtra(Utils.EXTRA_FOLDER_STRING, folder.serialize());
+            }
         } else {
             intent.putExtra(Utils.EXTRA_ACCOUNT, account);
-            intent.putExtra(Utils.EXTRA_FOLDER, folder);
+            if (folder != null) {
+                intent.putExtra(Utils.EXTRA_FOLDER, folder);
+            }
         }
         return intent;
     }
diff --git a/src/com/android/mail/widget/BaseWidgetProvider.java b/src/com/android/mail/widget/BaseWidgetProvider.java
index cab13a0..7cac558 100644
--- a/src/com/android/mail/widget/BaseWidgetProvider.java
+++ b/src/com/android/mail/widget/BaseWidgetProvider.java
@@ -27,11 +27,13 @@
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
+import android.support.v4.app.TaskStackBuilder;
 import android.text.TextUtils;
 import android.view.View;
 import android.widget.RemoteViews;
 
 import com.android.mail.R;
+import com.android.mail.compose.ComposeActivity;
 import com.android.mail.persistence.Persistence;
 import com.android.mail.providers.Account;
 import com.android.mail.providers.Folder;
@@ -351,12 +353,19 @@
         composeIntent.setAction(Intent.ACTION_SEND);
         composeIntent.putExtra(Utils.EXTRA_ACCOUNT, account);
         composeIntent.setData(account.composeIntentUri);
+        composeIntent.putExtra(ComposeActivity.EXTRA_FROM_EMAIL_TASK, true);
         if (account.composeIntentUri != null) {
             composeIntent.putExtra(Utils.EXTRA_COMPOSE_URI, account.composeIntentUri);
         }
-        clickIntent = PendingIntent.getActivity(context, 0, composeIntent,
-                PendingIntent.FLAG_UPDATE_CURRENT);
+
+        // Build a task stack that forces the conversation list on the stack before the compose
+        // activity.
+        final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
+        clickIntent = taskStackBuilder.addNextIntent(mailIntent)
+                .addNextIntent(composeIntent)
+                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
         remoteViews.setOnClickPendingIntent(R.id.widget_compose, clickIntent);
+
         // On click intent for Conversation
         final Intent conversationIntent = new Intent();
         conversationIntent.setAction(Intent.ACTION_VIEW);