Fixing issue where we were relaunching each activity from recents instead of bringing them to the front.
diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsTaskLoader.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsTaskLoader.java
index 4601b0b..b497b69 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/RecentsTaskLoader.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsTaskLoader.java
@@ -422,7 +422,7 @@
                     if (t.activityIcon != null) {
                         bd = new BitmapDrawable(res, t.activityIcon);
                     }
-                    Task task = new Task(t.persistentId, t.baseIntent, label, bd);
+                    Task task = new Task(t.persistentId, (t.id > -1), t.baseIntent, label, bd);
 
                     // Load the icon (if possible and not the foremost task, from the cache)
                     if (task.icon != null) {
@@ -477,7 +477,8 @@
                     for (int j = 0; j < Constants.Values.RecentsTaskLoader.TaskEntryMultiplier; j++) {
                         Console.log(Constants.DebugFlags.App.TaskDataLoader,
                                 "  [RecentsTaskLoader|task]", t.baseIntent.getComponent().getPackageName());
-                        stack.addTask(new Task(t.persistentId, t.baseIntent, title, null, null));
+                        stack.addTask(new Task(t.persistentId, (t.id > -1), t.baseIntent, title,
+                                null, null));
                     }
                 }
             }
diff --git a/packages/SystemUI/src/com/android/systemui/recents/model/Task.java b/packages/SystemUI/src/com/android/systemui/recents/model/Task.java
index 7b335af..cda4ab2 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/model/Task.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/model/Task.java
@@ -64,18 +64,21 @@
     public String title;
     public Drawable icon;
     public Bitmap thumbnail;
+    public boolean isActive;
 
     TaskCallbacks mCb;
 
-    public Task(int id, Intent intent, String activityTitle, Drawable icon) {
-        this(id, intent, activityTitle, icon, null);
+    public Task(int id, boolean isActive, Intent intent, String activityTitle, Drawable icon) {
+        this(id, isActive, intent, activityTitle, icon, null);
     }
 
-    public Task(int id, Intent intent, String activityTitle, Drawable icon, Bitmap thumbnail) {
+    public Task(int id, boolean isActive, Intent intent, String activityTitle, Drawable icon,
+                Bitmap thumbnail) {
         this.key = new TaskKey(id, intent);
         this.title = activityTitle;
         this.icon = icon;
         this.thumbnail = thumbnail;
+        this.isActive = isActive;
     }
 
     /** Set the callbacks */
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
index d997222..cc85439 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
@@ -16,6 +16,7 @@
 
 package com.android.systemui.recents.views;
 
+import android.app.ActivityManager;
 import android.app.ActivityOptions;
 import android.content.ActivityNotFoundException;
 import android.content.Context;
@@ -241,19 +242,33 @@
                             b, offsetX, offsetY);
                 }
 
-                // Launch the activity with the desired animation
-                Intent i = new Intent(task.key.intent);
-                i.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
-                        | Intent.FLAG_ACTIVITY_TASK_ON_HOME
-                        | Intent.FLAG_ACTIVITY_NEW_TASK);
-                try {
+
+                if (task.isActive) {
+                    // Bring an active task to the foreground
+                    ActivityManager am = (ActivityManager)
+                            stackView.getContext().getSystemService(Context.ACTIVITY_SERVICE);
                     if (opts != null) {
-                        getContext().startActivityAsUser(i, opts.toBundle(), UserHandle.CURRENT);
+                        am.moveTaskToFront(task.key.id, ActivityManager.MOVE_TASK_WITH_HOME,
+                                opts.toBundle());
                     } else {
-                        getContext().startActivityAsUser(i, UserHandle.CURRENT);
+                        am.moveTaskToFront(task.key.id, ActivityManager.MOVE_TASK_WITH_HOME);
                     }
-                } catch (ActivityNotFoundException anfe) {
-                    Console.logError(getContext(), "Could not start Activity");
+                } else {
+                    // Launch the activity with the desired animation
+                    Intent i = new Intent(task.key.intent);
+                    i.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
+                            | Intent.FLAG_ACTIVITY_TASK_ON_HOME
+                            | Intent.FLAG_ACTIVITY_NEW_TASK);
+                    try {
+                        if (opts != null) {
+                            getContext().startActivityAsUser(i, opts.toBundle(),
+                                    UserHandle.CURRENT);
+                        } else {
+                            getContext().startActivityAsUser(i, UserHandle.CURRENT);
+                        }
+                    } catch (ActivityNotFoundException anfe) {
+                        Console.logError(getContext(), "Could not start Activity");
+                    }
                 }
 
                 Console.logTraceTime(Constants.DebugFlags.App.TimeRecentsLaunchTask,