RecentsImpl.java revision 619e40cd56266a362ab7da80cb9e4eba6c33b204
1190fe3bf88388fcb109af64571e3baa0d01f1c37Winson/*
2190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * Copyright (C) 2015 The Android Open Source Project
3190fe3bf88388fcb109af64571e3baa0d01f1c37Winson *
4190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * Licensed under the Apache License, Version 2.0 (the "License");
5190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * you may not use this file except in compliance with the License.
6190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * You may obtain a copy of the License at
7190fe3bf88388fcb109af64571e3baa0d01f1c37Winson *
8190fe3bf88388fcb109af64571e3baa0d01f1c37Winson *      http://www.apache.org/licenses/LICENSE-2.0
9190fe3bf88388fcb109af64571e3baa0d01f1c37Winson *
10190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * Unless required by applicable law or agreed to in writing, software
11190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * distributed under the License is distributed on an "AS IS" BASIS,
12190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * See the License for the specific language governing permissions and
14190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * limitations under the License.
15190fe3bf88388fcb109af64571e3baa0d01f1c37Winson */
16190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
17190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonpackage com.android.systemui.recents;
18190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
19e693aafe0511c2a7ffc571b22abeefba44046225Winsonimport static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
20e693aafe0511c2a7ffc571b22abeefba44046225Winson
21190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.app.ActivityManager;
22190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.app.ActivityOptions;
23190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.content.ActivityNotFoundException;
24190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.content.Context;
25190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.content.Intent;
26190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.content.res.Resources;
27190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.graphics.Bitmap;
28190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.graphics.Canvas;
29190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.graphics.Rect;
303150e574e6e903a17193e0b90433d69c0e20ae4cWinsonimport android.graphics.RectF;
3122574afa00b7e32b7c4c5a1b98deebd67751b364Winsonimport android.graphics.drawable.Drawable;
32190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.os.Handler;
33190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.os.SystemClock;
34190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.os.UserHandle;
351b58561f66a9453754b1b419296899fdc08b4c93Winsonimport android.util.Log;
36190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.util.MutableBoolean;
37d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynskiimport android.view.AppTransitionAnimationSpec;
38190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.view.LayoutInflater;
39190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.view.View;
40b61e654b9a46e79827355104760a3ac5228380bcWinsonimport android.view.ViewConfiguration;
41c0d7058b14c24cd07912f5629c26b39b7b4673d5Winson
42190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.internal.logging.MetricsLogger;
43190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.R;
44190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.SystemUIApplication;
45412e18058dc2cd5779d2451fce7fd74631f9e237Winsonimport com.android.systemui.recents.events.EventBus;
462adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggiimport com.android.systemui.recents.events.EventBus.Event;
47899327f5cbbfb0eae5562b262ccea860c98f6bc4Jorim Jaggiimport com.android.systemui.recents.events.activity.DockedTopTaskEvent;
481b58561f66a9453754b1b419296899fdc08b4c93Winsonimport com.android.systemui.recents.events.activity.EnterRecentsWindowLastAnimationFrameEvent;
492adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggiimport com.android.systemui.recents.events.activity.ForcedResizableEvent;
50412e18058dc2cd5779d2451fce7fd74631f9e237Winsonimport com.android.systemui.recents.events.activity.HideRecentsEvent;
510d14d4da91c3d8b1221269712d5abf43a7cf9f31Winsonimport com.android.systemui.recents.events.activity.IterateRecentsEvent;
52b61e654b9a46e79827355104760a3ac5228380bcWinsonimport com.android.systemui.recents.events.activity.LaunchNextTaskRequestEvent;
53cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggiimport com.android.systemui.recents.events.activity.RecentsActivityStartingEvent;
54412e18058dc2cd5779d2451fce7fd74631f9e237Winsonimport com.android.systemui.recents.events.activity.ToggleRecentsEvent;
55190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
56190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
57dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggiimport com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
58dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggiimport com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
596b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinsonimport com.android.systemui.recents.misc.DozeTrigger;
60ab84fc56114c0963c6f701de9725f5413ab76da9Winsonimport com.android.systemui.recents.misc.ForegroundThread;
61190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.misc.SystemServicesProxy;
62938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kimimport com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener;
63190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.model.RecentsTaskLoadPlan;
64190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.model.RecentsTaskLoader;
65190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.model.Task;
66190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.model.TaskGrouping;
67190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.model.TaskStack;
6836a5a2c7003ef8157f276b411c3fda47ad2f75e3Winsonimport com.android.systemui.recents.views.TaskStackLayoutAlgorithm;
691b58561f66a9453754b1b419296899fdc08b4c93Winsonimport com.android.systemui.recents.views.TaskStackView;
70e693aafe0511c2a7ffc571b22abeefba44046225Winsonimport com.android.systemui.recents.views.TaskStackViewScroller;
71190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.views.TaskViewHeader;
72190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.views.TaskViewTransform;
73d8b1d63f96580fe961e1751e7b4f56c90c1e0a76Winsonimport com.android.systemui.statusbar.BaseStatusBar;
74cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggiimport com.android.systemui.statusbar.phone.NavigationBarGestureHelper;
75190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.statusbar.phone.PhoneStatusBar;
76190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
77190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport java.util.ArrayList;
78190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
79190fe3bf88388fcb109af64571e3baa0d01f1c37Winson/**
80190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * An implementation of the Recents component for the current user.  For secondary users, this can
81190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * be called remotely from the system user.
82190fe3bf88388fcb109af64571e3baa0d01f1c37Winson */
83cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggipublic class RecentsImpl implements ActivityOptions.OnAnimationFinishedListener {
84190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
85190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    private final static String TAG = "RecentsImpl";
86b61e654b9a46e79827355104760a3ac5228380bcWinson
876b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    // The minimum amount of time between each recents button press that we will handle
886b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    private final static int MIN_TOGGLE_DELAY_MS = 350;
89b61e654b9a46e79827355104760a3ac5228380bcWinson
906b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    // The duration within which the user releasing the alt tab (from when they pressed alt tab)
916b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    // that the fast alt-tab animation will run.  If the user's alt-tab takes longer than this
926b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    // duration, then we will toggle recents after this duration.
936b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    private final static int FAST_ALT_TAB_DELAY_MS = 225;
94190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
95190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public final static String RECENTS_PACKAGE = "com.android.systemui";
96190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public final static String RECENTS_ACTIVITY = "com.android.systemui.recents.RecentsActivity";
97e693aafe0511c2a7ffc571b22abeefba44046225Winson
98190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
99938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kim     * An implementation of TaskStackListener, that allows us to listen for changes to the system
100190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * task stacks and update recents accordingly.
101190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
102938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kim    class TaskStackListenerImpl extends TaskStackListener {
103190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        @Override
104190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        public void onTaskStackChanged() {
105938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kim            // Preloads the next task
106a0fdeec66caa4d70786c7f6f31f79a24eef9da10Jorim Jaggi            RecentsConfiguration config = Recents.getConfiguration();
107190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            if (config.svelteLevel == RecentsConfiguration.SVELTE_NONE) {
108e7f138c7f0a190c86cec10fb32fa106aacae4093Winson                RecentsTaskLoader loader = Recents.getTaskLoader();
109e7f138c7f0a190c86cec10fb32fa106aacae4093Winson                SystemServicesProxy ssp = Recents.getSystemServices();
110190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                ActivityManager.RunningTaskInfo runningTaskInfo = ssp.getTopMostTask();
111190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
112190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // Load the next task only if we aren't svelte
113190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
11465c851e6e9e08656744b6f66d3da188e3283b17dWinson                loader.preloadTasks(plan, -1, true /* isTopTaskHome */);
115190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
116190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // This callback is made when a new activity is launched and the old one is paused
117190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // so ignore the current activity and try and preload the thumbnail for the
118190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // previous one.
119190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                if (runningTaskInfo != null) {
120190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    launchOpts.runningTaskId = runningTaskInfo.id;
121190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
122190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                launchOpts.numVisibleTasks = 2;
123190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                launchOpts.numVisibleTaskThumbnails = 2;
124190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                launchOpts.onlyLoadForCache = true;
125190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                launchOpts.onlyLoadPausedActivities = true;
126190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                loader.loadTasks(mContext, plan, launchOpts);
127190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
128190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
1292adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi
1302adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi        @Override
1312adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi        public void onActivityForcedResizable(String packageName, int taskId) {
1322adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi            EventBus.getDefault().sendOntoMainThread(
1332adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi                    new ForcedResizableEvent(packageName, taskId));
1342adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi
1352adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi        }
136190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
137190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
1384bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected static RecentsTaskLoadPlan sInstanceLoadPlan;
139190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
1404bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected Context mContext;
1414bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected Handler mHandler;
142190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    TaskStackListenerImpl mTaskStackListener;
143dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    boolean mDraggingInRecents;
144e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi    boolean mLaunchedWhileDocking;
145190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
146190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    // Task launching
147190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    Rect mTaskStackBounds = new Rect();
148190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    Rect mLastTaskViewBounds = new Rect();
149190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    TaskViewTransform mTmpTransform = new TaskViewTransform();
150190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    int mStatusBarHeight;
151190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    int mNavBarHeight;
152190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    int mNavBarWidth;
153190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    int mTaskBarHeight;
154190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
155190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    // Header (for transition)
156190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    TaskViewHeader mHeaderBar;
157190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    final Object mHeaderBarLock = new Object();
1584bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected TaskStackView mDummyStackView;
159190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
160190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    // Variables to keep track of if we need to start recents after binding
1614bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected boolean mTriggeredFromAltTab;
1624bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected long mLastToggleTime;
1636b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    DozeTrigger mFastAltTabTrigger = new DozeTrigger(FAST_ALT_TAB_DELAY_MS, new Runnable() {
1646b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        @Override
1656b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        public void run() {
1666b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            // When this fires, then the user has not released alt-tab for at least
1676b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            // FAST_ALT_TAB_DELAY_MS milliseconds
168435b2e43cb131d0b38ba19f335aec78bb83944b2Jorim Jaggi            showRecents(mTriggeredFromAltTab, false /* draggingInRecents */, true /* animate */,
169435b2e43cb131d0b38ba19f335aec78bb83944b2Jorim Jaggi                    false /* reloadTasks */);
1706b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
1716b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    });
172190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
1734bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected Bitmap mThumbnailTransitionBitmapCache;
174190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    Task mThumbnailTransitionBitmapCacheKey;
175190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
176190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public RecentsImpl(Context context) {
177190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        mContext = context;
178190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        mHandler = new Handler();
179190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
180ab84fc56114c0963c6f701de9725f5413ab76da9Winson        // Initialize the static foreground thread
181ab84fc56114c0963c6f701de9725f5413ab76da9Winson        ForegroundThread.get();
182ab84fc56114c0963c6f701de9725f5413ab76da9Winson
183190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Register the task stack listener
184938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kim        mTaskStackListener = new TaskStackListenerImpl();
185e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        SystemServicesProxy ssp = Recents.getSystemServices();
186e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        ssp.registerTaskStackListener(mTaskStackListener);
187190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
188190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Initialize the static configuration resources
189b94443d876d3c9575e88078d0e537bb385f79ca4Winson        reloadHeaderBarLayout();
190008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson        updateHeaderBarLayout(null /* stack */);
191190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
192190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // When we start, preload the data associated with the previous recent tasks.
193190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // We can use a new plan since the caches will be the same.
194e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        RecentsTaskLoader loader = Recents.getTaskLoader();
195190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
19665c851e6e9e08656744b6f66d3da188e3283b17dWinson        loader.preloadTasks(plan, -1, true /* isTopTaskHome */);
197190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
198296278a0679375b8c43962a9e3c9bb4e8ab201e7Winson Chung        launchOpts.numVisibleTasks = loader.getIconCacheSize();
199190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.numVisibleTaskThumbnails = loader.getThumbnailCacheSize();
200190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.onlyLoadForCache = true;
201190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        loader.loadTasks(mContext, plan, launchOpts);
202190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
203190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
204190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void onBootCompleted() {
205008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson        updateHeaderBarLayout(null /* stack */);
206190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
207190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
208190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void onConfigurationChanged() {
209b94443d876d3c9575e88078d0e537bb385f79ca4Winson        reloadHeaderBarLayout();
210008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson        updateHeaderBarLayout(null /* stack */);
21153ec42cb17693b2d631746e1e5d31021a3359163Winson        Recents.getConfiguration().updateOnConfigurationChange();
212190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
213190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
214190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
215190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * This is only called from the system user's Recents.  Secondary users will instead proxy their
216190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * visibility change events through to the system user via
217190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * {@link Recents#onBusEvent(RecentsVisibilityChangedEvent)}.
218190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
219190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void onVisibilityChanged(Context context, boolean visible) {
220190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        SystemUIApplication app = (SystemUIApplication) context;
221190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        PhoneStatusBar statusBar = app.getComponent(PhoneStatusBar.class);
222190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (statusBar != null) {
223190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            statusBar.updateRecentsVisibility(visible);
224190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
225190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
226190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
227190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
228190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * This is only called from the system user's Recents.  Secondary users will instead proxy their
229190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * visibility change events through to the system user via
230190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * {@link Recents#onBusEvent(ScreenPinningRequestEvent)}.
231190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
232190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void onStartScreenPinning(Context context) {
233190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        SystemUIApplication app = (SystemUIApplication) context;
234190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        PhoneStatusBar statusBar = app.getComponent(PhoneStatusBar.class);
235190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (statusBar != null) {
236190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            statusBar.showScreenPinningRequest(false);
237190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
238190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
239190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
240bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi    public void showRecents(boolean triggeredFromAltTab, boolean draggingInRecents,
241e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi            boolean animate, boolean launchedWhileDockingTask) {
242190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        mTriggeredFromAltTab = triggeredFromAltTab;
243dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi        mDraggingInRecents = draggingInRecents;
244e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi        mLaunchedWhileDocking = launchedWhileDockingTask;
245e693aafe0511c2a7ffc571b22abeefba44046225Winson        if (mFastAltTabTrigger.isAsleep()) {
246e693aafe0511c2a7ffc571b22abeefba44046225Winson            // Fast alt-tab duration has elapsed, fall through to showing Recents and reset
247e693aafe0511c2a7ffc571b22abeefba44046225Winson            mFastAltTabTrigger.stopDozing();
2486b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        } else if (mFastAltTabTrigger.isDozing()) {
249e693aafe0511c2a7ffc571b22abeefba44046225Winson            // Fast alt-tab duration has not elapsed.  If this is triggered by a different
250e693aafe0511c2a7ffc571b22abeefba44046225Winson            // showRecents() call, then ignore that call for now.
251e693aafe0511c2a7ffc571b22abeefba44046225Winson            // TODO: We can not handle quick tabs that happen between the initial showRecents() call
252e693aafe0511c2a7ffc571b22abeefba44046225Winson            //       that started the activity and the activity starting up.  The severity of this
253e693aafe0511c2a7ffc571b22abeefba44046225Winson            //       is inversely proportional to the FAST_ALT_TAB_DELAY_MS duration though.
2546b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            if (!triggeredFromAltTab) {
2556b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                return;
2566b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            }
2576b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            mFastAltTabTrigger.stopDozing();
258e693aafe0511c2a7ffc571b22abeefba44046225Winson        } else if (triggeredFromAltTab) {
259e693aafe0511c2a7ffc571b22abeefba44046225Winson            // The fast alt-tab detector is not yet running, so start the trigger and wait for the
260e693aafe0511c2a7ffc571b22abeefba44046225Winson            // hideRecents() call, or for the fast alt-tab duration to elapse
261e693aafe0511c2a7ffc571b22abeefba44046225Winson            mFastAltTabTrigger.startDozing();
262e693aafe0511c2a7ffc571b22abeefba44046225Winson            return;
2636b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
264190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
265190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        try {
266190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            // Check if the top task is in the home stack, and start the recents activity
267e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            SystemServicesProxy ssp = Recents.getSystemServices();
268e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            ActivityManager.RunningTaskInfo topTask = ssp.getTopMostTask();
269190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            MutableBoolean isTopTaskHome = new MutableBoolean(true);
270e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            if (topTask == null || !ssp.isRecentsTopMost(topTask, isTopTaskHome)) {
271bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi                startRecentsActivity(topTask, isTopTaskHome.value, animate);
272190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
273190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        } catch (ActivityNotFoundException e) {
2741b58561f66a9453754b1b419296899fdc08b4c93Winson            Log.e(TAG, "Failed to launch RecentsActivity", e);
275190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
276190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
277190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
278190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void hideRecents(boolean triggeredFromAltTab, boolean triggeredFromHomeKey) {
2792799eca21715390faf10596884b65165528f89c4Winson        if (triggeredFromAltTab && mFastAltTabTrigger.isDozing()) {
2802799eca21715390faf10596884b65165528f89c4Winson            // The user has released alt-tab before the trigger has run, so just show the next
2812799eca21715390faf10596884b65165528f89c4Winson            // task immediately
2822799eca21715390faf10596884b65165528f89c4Winson            showNextTask();
2836b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
2842799eca21715390faf10596884b65165528f89c4Winson            // Cancel the fast alt-tab trigger
2852799eca21715390faf10596884b65165528f89c4Winson            mFastAltTabTrigger.stopDozing();
2862799eca21715390faf10596884b65165528f89c4Winson            return;
287190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
2882799eca21715390faf10596884b65165528f89c4Winson
2892799eca21715390faf10596884b65165528f89c4Winson        // Defer to the activity to handle hiding recents, if it handles it, then it must still
2902799eca21715390faf10596884b65165528f89c4Winson        // be visible
2912799eca21715390faf10596884b65165528f89c4Winson        EventBus.getDefault().post(new HideRecentsEvent(triggeredFromAltTab,
2922799eca21715390faf10596884b65165528f89c4Winson                triggeredFromHomeKey));
293190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
294190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
295190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void toggleRecents() {
2966b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Skip this toggle if we are already waiting to trigger recents via alt-tab
2976b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        if (mFastAltTabTrigger.isDozing()) {
2986b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            return;
2996b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
3006b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
301dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi        mDraggingInRecents = false;
302e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi        mLaunchedWhileDocking = false;
303190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        mTriggeredFromAltTab = false;
304190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
305190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        try {
306e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            SystemServicesProxy ssp = Recents.getSystemServices();
307e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            ActivityManager.RunningTaskInfo topTask = ssp.getTopMostTask();
308190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            MutableBoolean isTopTaskHome = new MutableBoolean(true);
309b61e654b9a46e79827355104760a3ac5228380bcWinson            long elapsedTime = SystemClock.elapsedRealtime() - mLastToggleTime;
310b61e654b9a46e79827355104760a3ac5228380bcWinson
311e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            if (topTask != null && ssp.isRecentsTopMost(topTask, isTopTaskHome)) {
312e693aafe0511c2a7ffc571b22abeefba44046225Winson                RecentsDebugFlags debugFlags = Recents.getDebugFlags();
3135da4347b41b105dc6c6c01bf2810af3ce3013229Winson                RecentsConfiguration config = Recents.getConfiguration();
3145da4347b41b105dc6c6c01bf2810af3ce3013229Winson                RecentsActivityLaunchState launchState = config.getLaunchState();
315ead5c0fc20e5c60a68cd017af0b8eacaf1167a1cWinson Chung                if (!launchState.launchedWithAltTab) {
316b61e654b9a46e79827355104760a3ac5228380bcWinson                    // If the user taps quickly
317e693aafe0511c2a7ffc571b22abeefba44046225Winson                    if (!debugFlags.isPagingEnabled() ||
318e693aafe0511c2a7ffc571b22abeefba44046225Winson                            (ViewConfiguration.getDoubleTapMinTime() < elapsedTime &&
319e693aafe0511c2a7ffc571b22abeefba44046225Winson                                    elapsedTime < ViewConfiguration.getDoubleTapTimeout())) {
320b61e654b9a46e79827355104760a3ac5228380bcWinson                        // Launch the next focused task
321b61e654b9a46e79827355104760a3ac5228380bcWinson                        EventBus.getDefault().post(new LaunchNextTaskRequestEvent());
322b61e654b9a46e79827355104760a3ac5228380bcWinson                    } else {
323b61e654b9a46e79827355104760a3ac5228380bcWinson                        // Notify recents to move onto the next task
324b61e654b9a46e79827355104760a3ac5228380bcWinson                        EventBus.getDefault().post(new IterateRecentsEvent());
325b61e654b9a46e79827355104760a3ac5228380bcWinson                    }
3260d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                } else {
3270d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    // If the user has toggled it too quickly, then just eat up the event here (it's
3280d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    // better than showing a janky screenshot).
3290d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    // NOTE: Ideally, the screenshot mechanism would take the window transform into
3300d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    // account
331b61e654b9a46e79827355104760a3ac5228380bcWinson                    if (elapsedTime < MIN_TOGGLE_DELAY_MS) {
3320d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                        return;
3330d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    }
3340d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson
3350d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    EventBus.getDefault().post(new ToggleRecentsEvent());
3360d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    mLastToggleTime = SystemClock.elapsedRealtime();
3370d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                }
338190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                return;
339190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            } else {
3400d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                // If the user has toggled it too quickly, then just eat up the event here (it's
3410d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                // better than showing a janky screenshot).
3420d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                // NOTE: Ideally, the screenshot mechanism would take the window transform into
3430d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                // account
344b61e654b9a46e79827355104760a3ac5228380bcWinson                if (elapsedTime < MIN_TOGGLE_DELAY_MS) {
3450d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    return;
3460d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                }
3470d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson
348190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // Otherwise, start the recents activity
349bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi                startRecentsActivity(topTask, isTopTaskHome.value, true /* animate */);
350d8b1d63f96580fe961e1751e7b4f56c90c1e0a76Winson
351d8b1d63f96580fe961e1751e7b4f56c90c1e0a76Winson                // Only close the other system windows if we are actually showing recents
352d8b1d63f96580fe961e1751e7b4f56c90c1e0a76Winson                ssp.sendCloseSystemWindows(BaseStatusBar.SYSTEM_DIALOG_REASON_RECENT_APPS);
3530d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                mLastToggleTime = SystemClock.elapsedRealtime();
354190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
355190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        } catch (ActivityNotFoundException e) {
3561b58561f66a9453754b1b419296899fdc08b4c93Winson            Log.e(TAG, "Failed to launch RecentsActivity", e);
357190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
358190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
359190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
360190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void preloadRecents() {
361190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Preload only the raw task list into a new load plan (which will be consumed by the
362190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // RecentsActivity) only if there is a task to animate to.
363e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        SystemServicesProxy ssp = Recents.getSystemServices();
364e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        ActivityManager.RunningTaskInfo topTask = ssp.getTopMostTask();
365190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        MutableBoolean topTaskHome = new MutableBoolean(true);
366e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        if (topTask != null && !ssp.isRecentsTopMost(topTask, topTaskHome)) {
3675b4e0d2b11b2b3579bac36fde5ac3805346618ccWinson            RecentsTaskLoader loader = Recents.getTaskLoader();
3685b4e0d2b11b2b3579bac36fde5ac3805346618ccWinson            sInstanceLoadPlan = loader.createLoadPlan(mContext);
369190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            sInstanceLoadPlan.preloadRawTasks(topTaskHome.value);
37065c851e6e9e08656744b6f66d3da188e3283b17dWinson            loader.preloadTasks(sInstanceLoadPlan, topTask.id, topTaskHome.value);
371190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            TaskStack stack = sInstanceLoadPlan.getTaskStack();
3724b057c6787624b75613769a857ccdf51114bb7f2Winson            if (stack.getTaskCount() > 0) {
373ab84fc56114c0963c6f701de9725f5413ab76da9Winson                // We try and draw the thumbnail transition bitmap in parallel before
374ab84fc56114c0963c6f701de9725f5413ab76da9Winson                // toggle/show recents is called
375190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                preCacheThumbnailTransitionBitmapAsync(topTask, stack, mDummyStackView);
376190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
377190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
378190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
379190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
380190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void cancelPreloadingRecents() {
381190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Do nothing
382190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
383190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
384dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    public void onDraggingInRecents(float distanceFromTop) {
385dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi        EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEvent(distanceFromTop));
386dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    }
387dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi
388dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    public void onDraggingInRecentsEnded(float velocity) {
389dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi        EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEndedEvent(velocity));
390dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    }
391dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi
3926b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    /**
3936b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson     * Transitions to the next recent task in the stack.
3946b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson     */
3956b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    public void showNextTask() {
3966b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        SystemServicesProxy ssp = Recents.getSystemServices();
3976b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        RecentsTaskLoader loader = Recents.getTaskLoader();
3986b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
39965c851e6e9e08656744b6f66d3da188e3283b17dWinson        loader.preloadTasks(plan, -1, true /* isTopTaskHome */);
4006b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        TaskStack focusedStack = plan.getTaskStack();
4016b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4026b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Return early if there are no tasks in the focused stack
4034b057c6787624b75613769a857ccdf51114bb7f2Winson        if (focusedStack == null || focusedStack.getTaskCount() == 0) return;
4046b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4056b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        ActivityManager.RunningTaskInfo runningTask = ssp.getTopMostTask();
4066b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Return early if there is no running task
4076b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        if (runningTask == null) return;
4086b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4096b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Find the task in the recents list
410e86deb8adb022ba7721a1af1112d3a45e757095cWinson        boolean isTopTaskHome = SystemServicesProxy.isHomeStack(runningTask.stackId);
411250608a5cd08862f4752a924d51710805850db8aWinson        ArrayList<Task> tasks = focusedStack.getStackTasks();
4126b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        Task toTask = null;
4136b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        ActivityOptions launchOpts = null;
4146b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        int taskCount = tasks.size();
4156b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        for (int i = taskCount - 1; i >= 1; i--) {
4166b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            Task task = tasks.get(i);
417e86deb8adb022ba7721a1af1112d3a45e757095cWinson            if (isTopTaskHome) {
418e86deb8adb022ba7721a1af1112d3a45e757095cWinson                toTask = tasks.get(i - 1);
419e86deb8adb022ba7721a1af1112d3a45e757095cWinson                launchOpts = ActivityOptions.makeCustomAnimation(mContext,
420e86deb8adb022ba7721a1af1112d3a45e757095cWinson                        R.anim.recents_launch_next_affiliated_task_target,
421e86deb8adb022ba7721a1af1112d3a45e757095cWinson                        R.anim.recents_fast_toggle_app_home_exit);
422e86deb8adb022ba7721a1af1112d3a45e757095cWinson                break;
423e86deb8adb022ba7721a1af1112d3a45e757095cWinson            } else if (task.key.id == runningTask.id) {
4246b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                toTask = tasks.get(i - 1);
4256b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                launchOpts = ActivityOptions.makeCustomAnimation(mContext,
4266b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                        R.anim.recents_launch_prev_affiliated_task_target,
4276b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                        R.anim.recents_launch_prev_affiliated_task_source);
4286b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                break;
4296b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            }
4306b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
4316b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4326b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Return early if there is no next task
4336b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        if (toTask == null) {
4346b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            ssp.startInPlaceAnimationOnFrontMostApplication(
4356b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                    ActivityOptions.makeCustomInPlaceAnimation(mContext,
4366b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                            R.anim.recents_launch_prev_affiliated_task_bounce));
4376b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            return;
4386b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
4396b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4406b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Launch the task
441296278a0679375b8c43962a9e3c9bb4e8ab201e7Winson Chung        ssp.startActivityFromRecents(mContext, toTask.key.id, toTask.title, launchOpts);
4426b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    }
4436b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4446b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    /**
4456b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson     * Transitions to the next affiliated task.
4466b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson     */
447190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void showRelativeAffiliatedTask(boolean showNextTask) {
448e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        SystemServicesProxy ssp = Recents.getSystemServices();
449e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        RecentsTaskLoader loader = Recents.getTaskLoader();
450190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
45165c851e6e9e08656744b6f66d3da188e3283b17dWinson        loader.preloadTasks(plan, -1, true /* isTopTaskHome */);
452190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        TaskStack focusedStack = plan.getTaskStack();
453190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
454190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Return early if there are no tasks in the focused stack
4554b057c6787624b75613769a857ccdf51114bb7f2Winson        if (focusedStack == null || focusedStack.getTaskCount() == 0) return;
456190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
457e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        ActivityManager.RunningTaskInfo runningTask = ssp.getTopMostTask();
458190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Return early if there is no running task (can't determine affiliated tasks in this case)
459190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (runningTask == null) return;
460190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Return early if the running task is in the home stack (optimization)
4615510f6c1b9c20483e1507147eed7b24ac8bb6363Winson        if (SystemServicesProxy.isHomeStack(runningTask.stackId)) return;
462190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
463190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Find the task in the recents list
464250608a5cd08862f4752a924d51710805850db8aWinson        ArrayList<Task> tasks = focusedStack.getStackTasks();
465190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        Task toTask = null;
466190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        ActivityOptions launchOpts = null;
467190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        int taskCount = tasks.size();
468190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        int numAffiliatedTasks = 0;
469190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        for (int i = 0; i < taskCount; i++) {
470190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            Task task = tasks.get(i);
471190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            if (task.key.id == runningTask.id) {
472190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                TaskGrouping group = task.group;
473190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                Task.TaskKey toTaskKey;
474190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                if (showNextTask) {
475190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    toTaskKey = group.getNextTaskInGroup(task);
476190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    launchOpts = ActivityOptions.makeCustomAnimation(mContext,
477190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            R.anim.recents_launch_next_affiliated_task_target,
478190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            R.anim.recents_launch_next_affiliated_task_source);
479190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                } else {
480190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    toTaskKey = group.getPrevTaskInGroup(task);
481190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    launchOpts = ActivityOptions.makeCustomAnimation(mContext,
482190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            R.anim.recents_launch_prev_affiliated_task_target,
483190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            R.anim.recents_launch_prev_affiliated_task_source);
484190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
485190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                if (toTaskKey != null) {
486190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    toTask = focusedStack.findTaskWithId(toTaskKey.id);
487190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
488190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                numAffiliatedTasks = group.getTaskCount();
489190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                break;
490190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
491190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
492190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
493190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Return early if there is no next task
494190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (toTask == null) {
495190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            if (numAffiliatedTasks > 1) {
496190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                if (showNextTask) {
497e7f138c7f0a190c86cec10fb32fa106aacae4093Winson                    ssp.startInPlaceAnimationOnFrontMostApplication(
498190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            ActivityOptions.makeCustomInPlaceAnimation(mContext,
499190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                                    R.anim.recents_launch_next_affiliated_task_bounce));
500190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                } else {
501e7f138c7f0a190c86cec10fb32fa106aacae4093Winson                    ssp.startInPlaceAnimationOnFrontMostApplication(
502190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            ActivityOptions.makeCustomInPlaceAnimation(mContext,
503190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                                    R.anim.recents_launch_prev_affiliated_task_bounce));
504190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
505190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
506190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            return;
507190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
508190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
509190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Keep track of actually launched affiliated tasks
510190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        MetricsLogger.count(mContext, "overview_affiliated_task_launch", 1);
511190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
512190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Launch the task
513296278a0679375b8c43962a9e3c9bb4e8ab201e7Winson Chung        ssp.startActivityFromRecents(mContext, toTask.key.id, toTask.title, launchOpts);
514190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
515190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
516190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void showNextAffiliatedTask() {
517190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Keep track of when the affiliated task is triggered
518190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        MetricsLogger.count(mContext, "overview_affiliated_task_next", 1);
519190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        showRelativeAffiliatedTask(true);
520190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
521190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
522190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void showPrevAffiliatedTask() {
523190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Keep track of when the affiliated task is triggered
524190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        MetricsLogger.count(mContext, "overview_affiliated_task_prev", 1);
525190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        showRelativeAffiliatedTask(false);
526190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
527190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
528cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggi    public void dockTopTask(int topTaskId, int dragMode,
529cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggi            int stackCreateMode, Rect initialBounds) {
53075b2597e5826662c1740d6f3b81966a9e6a9e7b4Jorim Jaggi        SystemServicesProxy ssp = Recents.getSystemServices();
5319511b0f1e9ac629a4a747a0c9373d33ab33cfc32Jorim Jaggi
5329511b0f1e9ac629a4a747a0c9373d33ab33cfc32Jorim Jaggi        // Make sure we inform DividerView before we actually start the activity so we can change
5339511b0f1e9ac629a4a747a0c9373d33ab33cfc32Jorim Jaggi        // the resize mode already.
534e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang        if (ssp.moveTaskToDockedStack(topTaskId, stackCreateMode, initialBounds)) {
535899327f5cbbfb0eae5562b262ccea860c98f6bc4Jorim Jaggi            EventBus.getDefault().send(new DockedTopTaskEvent(dragMode, initialBounds));
536e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang            showRecents(
537e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang                    false /* triggeredFromAltTab */,
538e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang                    dragMode == NavigationBarGestureHelper.DRAG_MODE_RECENTS,
539e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang                    false /* animate */,
540e693aafe0511c2a7ffc571b22abeefba44046225Winson                    true /* launchedWhileDockingTask*/);
541e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang        }
54275b2597e5826662c1740d6f3b81966a9e6a9e7b4Jorim Jaggi    }
54375b2597e5826662c1740d6f3b81966a9e6a9e7b4Jorim Jaggi
544190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
545190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Returns the preloaded load plan and invalidates it.
546190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
547190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public static RecentsTaskLoadPlan consumeInstanceLoadPlan() {
548190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan plan = sInstanceLoadPlan;
549190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        sInstanceLoadPlan = null;
550190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return plan;
551190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
552190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
553190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
554b94443d876d3c9575e88078d0e537bb385f79ca4Winson     * Reloads all the layouts for the header bar transition.
555b94443d876d3c9575e88078d0e537bb385f79ca4Winson     */
556b94443d876d3c9575e88078d0e537bb385f79ca4Winson    private void reloadHeaderBarLayout() {
557b94443d876d3c9575e88078d0e537bb385f79ca4Winson        Resources res = mContext.getResources();
558b94443d876d3c9575e88078d0e537bb385f79ca4Winson        LayoutInflater inflater = LayoutInflater.from(mContext);
559b94443d876d3c9575e88078d0e537bb385f79ca4Winson
560b94443d876d3c9575e88078d0e537bb385f79ca4Winson        mStatusBarHeight = res.getDimensionPixelSize(
561b94443d876d3c9575e88078d0e537bb385f79ca4Winson                com.android.internal.R.dimen.status_bar_height);
562b94443d876d3c9575e88078d0e537bb385f79ca4Winson        mNavBarHeight = res.getDimensionPixelSize(
563b94443d876d3c9575e88078d0e537bb385f79ca4Winson                com.android.internal.R.dimen.navigation_bar_height);
564b94443d876d3c9575e88078d0e537bb385f79ca4Winson        mNavBarWidth = res.getDimensionPixelSize(
565b94443d876d3c9575e88078d0e537bb385f79ca4Winson                com.android.internal.R.dimen.navigation_bar_width);
566217009356efa2b854ab3981dff8d315a0d679c73Winson        mTaskBarHeight = TaskStackLayoutAlgorithm.getDimensionForDevice(res,
567217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height,
568217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height,
569217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height,
570217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height_tablet_land,
571217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height,
572217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height_tablet_land);
5738873754f66527d2cc9feab295dd4eef23298212aWinson        mDummyStackView = new TaskStackView(mContext);
574b94443d876d3c9575e88078d0e537bb385f79ca4Winson        mHeaderBar = (TaskViewHeader) inflater.inflate(R.layout.recents_task_view_header,
575b94443d876d3c9575e88078d0e537bb385f79ca4Winson                null, false);
576b94443d876d3c9575e88078d0e537bb385f79ca4Winson    }
577b94443d876d3c9575e88078d0e537bb385f79ca4Winson
578b94443d876d3c9575e88078d0e537bb385f79ca4Winson    /**
579190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Prepares the header bar layout for the next transition, if the task view bounds has changed
580190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * since the last call, it will attempt to re-measure and layout the header bar to the new size.
581190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     *
582f0d1c44a59a10707baa0cca8dd377302260710c1Winson     * @param stack the stack to initialize the stack layout with
583190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
584008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson    private void updateHeaderBarLayout(TaskStack stack) {
585e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        SystemServicesProxy ssp = Recents.getSystemServices();
586c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        Rect systemInsets = new Rect();
587c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        ssp.getStableInsets(systemInsets);
588e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        Rect windowRect = ssp.getWindowRect();
589c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        calculateWindowStableInsets(systemInsets, windowRect);
590c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        windowRect.offsetTo(0, 0);
591190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
59259924fe0d9136cf349759bea1e06b661603f95feWinson        TaskStackLayoutAlgorithm stackLayout = mDummyStackView.getStackAlgorithm();
59359924fe0d9136cf349759bea1e06b661603f95feWinson        stackLayout.getTaskStackBounds(windowRect, systemInsets.top, systemInsets.right,
594008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson                mTaskStackBounds);
595190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
596190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Rebind the header bar and draw it for the transition
597190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        Rect taskStackBounds = new Rect(mTaskStackBounds);
5988873754f66527d2cc9feab295dd4eef23298212aWinson        stackLayout.setSystemInsets(systemInsets);
599f0d1c44a59a10707baa0cca8dd377302260710c1Winson        if (stack != null) {
60059924fe0d9136cf349759bea1e06b661603f95feWinson            stackLayout.initialize(windowRect, taskStackBounds,
601f0d1c44a59a10707baa0cca8dd377302260710c1Winson                    TaskStackLayoutAlgorithm.StackState.getStackStateForStack(stack));
602a1ededd056d51532ab18354f17f8065ec1134535Winson            mDummyStackView.setTasks(stack, false /* allowNotifyStackChanges */);
603f0d1c44a59a10707baa0cca8dd377302260710c1Winson        }
6048873754f66527d2cc9feab295dd4eef23298212aWinson        Rect taskViewBounds = stackLayout.getUntransformedTaskViewBounds();
605190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (!taskViewBounds.equals(mLastTaskViewBounds)) {
606190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            mLastTaskViewBounds.set(taskViewBounds);
607190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
608190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            int taskViewWidth = taskViewBounds.width();
609190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            synchronized (mHeaderBarLock) {
610190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                mHeaderBar.measure(
611190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    View.MeasureSpec.makeMeasureSpec(taskViewWidth, View.MeasureSpec.EXACTLY),
612190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    View.MeasureSpec.makeMeasureSpec(mTaskBarHeight, View.MeasureSpec.EXACTLY));
613190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                mHeaderBar.layout(0, 0, taskViewWidth, mTaskBarHeight);
614190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
615190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
616190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
617190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
618190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
619c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi     * Given the stable insets and the rect for our window, calculates the insets that affect our
620c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi     * window.
621c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi     */
622c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi    private void calculateWindowStableInsets(Rect inOutInsets, Rect windowRect) {
623c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        Rect displayRect = Recents.getSystemServices().getDisplayRect();
624c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi
625c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        // Display rect without insets - available app space
626c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        Rect appRect = new Rect(displayRect);
627c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        appRect.inset(inOutInsets);
628c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi
629c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        // Our window intersected with available app space
630c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        Rect windowRectWithInsets = new Rect(windowRect);
631c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        windowRectWithInsets.intersect(appRect);
632c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        inOutInsets.left = windowRectWithInsets.left - windowRect.left;
633c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        inOutInsets.top = windowRectWithInsets.top - windowRect.top;
634c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        inOutInsets.right = windowRect.right - windowRectWithInsets.right;
635c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        inOutInsets.bottom = windowRect.bottom - windowRectWithInsets.bottom;
636c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi    }
637c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi
638c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi    /**
639190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Preloads the icon of a task.
640190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
641190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    private void preloadIcon(ActivityManager.RunningTaskInfo task) {
642190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Ensure that we load the running task's icon
643190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
644190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.runningTaskId = task.id;
645190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.loadThumbnails = false;
646190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.onlyLoadForCache = true;
647e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        Recents.getTaskLoader().loadTasks(mContext, sInstanceLoadPlan, launchOpts);
648190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
649190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
650190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
651190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Caches the header thumbnail used for a window animation asynchronously into
652190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * {@link #mThumbnailTransitionBitmapCache}.
653190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
654190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    private void preCacheThumbnailTransitionBitmapAsync(ActivityManager.RunningTaskInfo topTask,
655190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            TaskStack stack, TaskStackView stackView) {
656190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        preloadIcon(topTask);
657190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
658190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Update the header bar if necessary
659008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson        updateHeaderBarLayout(stack);
660190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
661190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Update the destination rect
662190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        final Task toTask = new Task();
663e693aafe0511c2a7ffc571b22abeefba44046225Winson        final TaskViewTransform toTransform = getThumbnailTransitionTransform(stackView, toTask);
6643fb67562c548662910160d5672b1b9558bd6571cWinson        ForegroundThread.getHandler().postAtFrontOfQueue(new Runnable() {
665190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            @Override
666ab84fc56114c0963c6f701de9725f5413ab76da9Winson            public void run() {
667ab84fc56114c0963c6f701de9725f5413ab76da9Winson                final Bitmap transitionBitmap = drawThumbnailTransitionBitmap(toTask, toTransform);
668ab84fc56114c0963c6f701de9725f5413ab76da9Winson                mHandler.post(new Runnable() {
669ab84fc56114c0963c6f701de9725f5413ab76da9Winson                    @Override
670ab84fc56114c0963c6f701de9725f5413ab76da9Winson                    public void run() {
671ab84fc56114c0963c6f701de9725f5413ab76da9Winson                        mThumbnailTransitionBitmapCache = transitionBitmap;
672ab84fc56114c0963c6f701de9725f5413ab76da9Winson                        mThumbnailTransitionBitmapCacheKey = toTask;
673ab84fc56114c0963c6f701de9725f5413ab76da9Winson                    }
674ab84fc56114c0963c6f701de9725f5413ab76da9Winson                });
675190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
676ab84fc56114c0963c6f701de9725f5413ab76da9Winson        });
677190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
678190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
679190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
680190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Creates the activity options for a unknown state->recents transition.
681190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
6824bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected ActivityOptions getUnknownTransitionActivityOptions() {
683190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return ActivityOptions.makeCustomAnimation(mContext,
684190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                R.anim.recents_from_unknown_enter,
685190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                R.anim.recents_from_unknown_exit,
6863fb67562c548662910160d5672b1b9558bd6571cWinson                mHandler, null);
687190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
688190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
689190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
690190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Creates the activity options for a home->recents transition.
691190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
692008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson    protected ActivityOptions getHomeTransitionActivityOptions() {
693190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return ActivityOptions.makeCustomAnimation(mContext,
694190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                R.anim.recents_from_launcher_enter,
695190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                R.anim.recents_from_launcher_exit,
6963fb67562c548662910160d5672b1b9558bd6571cWinson                mHandler, null);
697190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
698190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
699190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
700190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Creates the activity options for an app->recents transition.
701190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
702190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    private ActivityOptions getThumbnailTransitionActivityOptions(
703e693aafe0511c2a7ffc571b22abeefba44046225Winson            ActivityManager.RunningTaskInfo topTask, TaskStackView stackView) {
704d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski        if (topTask.stackId == FREEFORM_WORKSPACE_STACK_ID) {
705d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            ArrayList<AppTransitionAnimationSpec> specs = new ArrayList<>();
706e693aafe0511c2a7ffc571b22abeefba44046225Winson            ArrayList<Task> tasks = stackView.getStack().getStackTasks();
707e693aafe0511c2a7ffc571b22abeefba44046225Winson            TaskStackLayoutAlgorithm stackLayout = stackView.getStackAlgorithm();
708e693aafe0511c2a7ffc571b22abeefba44046225Winson            TaskStackViewScroller stackScroller = stackView.getScroller();
709e693aafe0511c2a7ffc571b22abeefba44046225Winson
710003eda6c230a1b1c4e3709466c46660e7602d740Winson            stackView.updateLayoutAlgorithm(true /* boundScroll */);
711619e40cd56266a362ab7da80cb9e4eba6c33b204Winson            stackView.updateToInitialState(true /* scrollToInitialState */);
712e693aafe0511c2a7ffc571b22abeefba44046225Winson
713d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            for (int i = tasks.size() - 1; i >= 0; i--) {
714d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                Task task = tasks.get(i);
715387aac6ad7bde21f2c2510cb8a2f3af3959721e0Winson                if (task.isFreeformTask()) {
716e693aafe0511c2a7ffc571b22abeefba44046225Winson                    mTmpTransform = stackLayout.getStackTransformScreenCoordinates(task,
717e693aafe0511c2a7ffc571b22abeefba44046225Winson                                    stackScroller.getStackScroll(), mTmpTransform, null);
718d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                    Rect toTaskRect = new Rect();
719d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                    mTmpTransform.rect.round(toTaskRect);
720d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                    Bitmap thumbnail = getThumbnailBitmap(topTask, task, mTmpTransform);
721d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                    specs.add(new AppTransitionAnimationSpec(task.key.id, thumbnail, toTaskRect));
722d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                }
723d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            }
724d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            AppTransitionAnimationSpec[] specsArray = new AppTransitionAnimationSpec[specs.size()];
725d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            specs.toArray(specsArray);
726d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            return ActivityOptions.makeThumbnailAspectScaleDownAnimation(mDummyStackView,
7273fb67562c548662910160d5672b1b9558bd6571cWinson                    specsArray, mHandler, null, this);
728d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski        } else {
729d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            // Update the destination rect
730d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            Task toTask = new Task();
731e693aafe0511c2a7ffc571b22abeefba44046225Winson            TaskViewTransform toTransform = getThumbnailTransitionTransform(stackView, toTask);
732d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            RectF toTaskRect = toTransform.rect;
733d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            Bitmap thumbnail = getThumbnailBitmap(topTask, toTask, toTransform);
734d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            if (thumbnail != null) {
735d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                return ActivityOptions.makeThumbnailAspectScaleDownAnimation(mDummyStackView,
736d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                        thumbnail, (int) toTaskRect.left, (int) toTaskRect.top,
7373fb67562c548662910160d5672b1b9558bd6571cWinson                        (int) toTaskRect.width(), (int) toTaskRect.height(), mHandler, null);
738d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            }
739d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            // If both the screenshot and thumbnail fails, then just fall back to the default transition
740d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            return getUnknownTransitionActivityOptions();
741d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski        }
742d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski    }
743190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
744d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski    private Bitmap getThumbnailBitmap(ActivityManager.RunningTaskInfo topTask, Task toTask,
745d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            TaskViewTransform toTransform) {
746190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        Bitmap thumbnail;
747190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (mThumbnailTransitionBitmapCacheKey != null
748190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                && mThumbnailTransitionBitmapCacheKey.key != null
749190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                && mThumbnailTransitionBitmapCacheKey.key.equals(toTask.key)) {
750190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            thumbnail = mThumbnailTransitionBitmapCache;
751190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            mThumbnailTransitionBitmapCacheKey = null;
752190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            mThumbnailTransitionBitmapCache = null;
753190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        } else {
754190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            preloadIcon(topTask);
755190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            thumbnail = drawThumbnailTransitionBitmap(toTask, toTransform);
756190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
757d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski        return thumbnail;
758190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
759190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
760190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
761190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Returns the transition rect for the given task id.
762190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
763e693aafe0511c2a7ffc571b22abeefba44046225Winson    private TaskViewTransform getThumbnailTransitionTransform(TaskStackView stackView,
764e693aafe0511c2a7ffc571b22abeefba44046225Winson            Task runningTaskOut) {
765190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Find the running task in the TaskStack
766e693aafe0511c2a7ffc571b22abeefba44046225Winson        TaskStack stack = stackView.getStack();
76765c851e6e9e08656744b6f66d3da188e3283b17dWinson        Task launchTask = stack.getLaunchTarget();
76865c851e6e9e08656744b6f66d3da188e3283b17dWinson        if (launchTask != null) {
76965c851e6e9e08656744b6f66d3da188e3283b17dWinson            runningTaskOut.copyFrom(launchTask);
77065c851e6e9e08656744b6f66d3da188e3283b17dWinson        } else {
771190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            // If no task is specified or we can not find the task just use the front most one
77235a8b04140598a5b5c4865254b942adb6a830991Winson            launchTask = stack.getStackFrontMostTask(true /* includeFreeform */);
77365c851e6e9e08656744b6f66d3da188e3283b17dWinson            runningTaskOut.copyFrom(launchTask);
774190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
775190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
776190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Get the transform for the running task
777003eda6c230a1b1c4e3709466c46660e7602d740Winson        stackView.updateLayoutAlgorithm(true /* boundScroll */);
778619e40cd56266a362ab7da80cb9e4eba6c33b204Winson        stackView.updateToInitialState(true /* scrollToInitialState */);
779c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        mTmpTransform = stackView.getStackAlgorithm().getStackTransformScreenCoordinates(launchTask,
780190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                stackView.getScroller().getStackScroll(), mTmpTransform, null);
781190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return mTmpTransform;
782190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
783190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
784190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
785190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Draws the header of a task used for the window animation into a bitmap.
786190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
787190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    private Bitmap drawThumbnailTransitionBitmap(Task toTask, TaskViewTransform toTransform) {
7888be1634a543022bd6ee0c1e974d3201d452981e8Winson        SystemServicesProxy ssp = Recents.getSystemServices();
789190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (toTransform != null && toTask.key != null) {
790190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            Bitmap thumbnail;
791190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            synchronized (mHeaderBarLock) {
792509d0d0c9e2ee165d04e898fea59f8941ac7138dWinson Chung                int toHeaderWidth = (int) toTransform.rect.width();
793190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                int toHeaderHeight = (int) (mHeaderBar.getMeasuredHeight() * toTransform.scale);
7948be1634a543022bd6ee0c1e974d3201d452981e8Winson                boolean disabledInSafeMode = !toTask.isSystemApp && ssp.isInSafeMode();
795509d0d0c9e2ee165d04e898fea59f8941ac7138dWinson Chung                mHeaderBar.onTaskViewSizeChanged((int) toTransform.rect.width(),
796509d0d0c9e2ee165d04e898fea59f8941ac7138dWinson Chung                        (int) toTransform.rect.height());
797190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                thumbnail = Bitmap.createBitmap(toHeaderWidth, toHeaderHeight,
798190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                        Bitmap.Config.ARGB_8888);
799c742f973b1e506732911c156c5869fd377afc5bfWinson                if (RecentsDebugFlags.Static.EnableTransitionThumbnailDebugMode) {
800190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    thumbnail.eraseColor(0xFFff0000);
801190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                } else {
802190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    Canvas c = new Canvas(thumbnail);
803190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    c.scale(toTransform.scale, toTransform.scale);
80422574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    // Workaround for b/27815919, reset the callback so that we do not trigger an
80522574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    // invalidate on the header bar as a result of updating the icon
80622574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    Drawable icon = mHeaderBar.getIconView().getDrawable();
80722574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    if (icon != null) {
80822574afa00b7e32b7c4c5a1b98deebd67751b364Winson                        icon.setCallback(null);
80922574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    }
8108be1634a543022bd6ee0c1e974d3201d452981e8Winson                    mHeaderBar.rebindToTask(toTask, false /* touchExplorationEnabled */,
8118be1634a543022bd6ee0c1e974d3201d452981e8Winson                            disabledInSafeMode);
812e693aafe0511c2a7ffc571b22abeefba44046225Winson                    mHeaderBar.setDimAlpha(toTransform.dimAlpha);
813190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    mHeaderBar.draw(c);
814190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    c.setBitmap(null);
815190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
816190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
817190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            return thumbnail.createAshmemBitmap();
818190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
819190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return null;
820190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
821190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
822190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
823190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Shows the recents activity
824190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
8254bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected void startRecentsActivity(ActivityManager.RunningTaskInfo topTask,
826bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi            boolean isTopTaskHome, boolean animate) {
827e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        RecentsTaskLoader loader = Recents.getTaskLoader();
828c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
829190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
830e5f1faa9f8009a723ab21aed6fe5ab325c61442bWinson        // In the case where alt-tab is triggered, we never get a preloadRecents() call, so we
831435b2e43cb131d0b38ba19f335aec78bb83944b2Jorim Jaggi        // should always preload the tasks now. If we are dragging in recents, reload them as
832435b2e43cb131d0b38ba19f335aec78bb83944b2Jorim Jaggi        // the stacks might have changed.
833e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi        if (mLaunchedWhileDocking || mTriggeredFromAltTab ||sInstanceLoadPlan == null) {
834e5f1faa9f8009a723ab21aed6fe5ab325c61442bWinson            // Create a new load plan if preloadRecents() was never triggered
835190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            sInstanceLoadPlan = loader.createLoadPlan(mContext);
836190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
837e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi        if (mLaunchedWhileDocking || mTriggeredFromAltTab || !sInstanceLoadPlan.hasTasks()) {
83865c851e6e9e08656744b6f66d3da188e3283b17dWinson            loader.preloadTasks(sInstanceLoadPlan, topTask.id, isTopTaskHome);
839190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
840c5b12dd37e964b220c0a108e86ad8a2d12805747Winson
841190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        TaskStack stack = sInstanceLoadPlan.getTaskStack();
842c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        boolean hasRecentTasks = stack.getTaskCount() > 0;
843c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        boolean useThumbnailTransition = (topTask != null) && !isTopTaskHome && hasRecentTasks;
844190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
845f0d1c44a59a10707baa0cca8dd377302260710c1Winson        // Update the header bar if necessary
846008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson        updateHeaderBarLayout(stack);
847f0d1c44a59a10707baa0cca8dd377302260710c1Winson
848190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Prepare the dummy stack for the transition
84936a5a2c7003ef8157f276b411c3fda47ad2f75e3Winson        TaskStackLayoutAlgorithm.VisibilityReport stackVr =
850190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                mDummyStackView.computeStackVisibilityReport();
851bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi
852c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        // Update the launch state
853c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedFromHome = false;
854c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedFromApp = mLaunchedWhileDocking;
855c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedToTaskId = (topTask != null) ? topTask.id : -1;
856c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedFromAppDocked = mLaunchedWhileDocking;
857c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedWithAltTab = mTriggeredFromAltTab;
858c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedNumVisibleTasks = stackVr.numVisibleTasks;
859c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedNumVisibleThumbnails = stackVr.numVisibleThumbnails;
860c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedViaDragGesture = mDraggingInRecents;
861c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedWhileDocking = mLaunchedWhileDocking;
862c5b12dd37e964b220c0a108e86ad8a2d12805747Winson
863bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi        if (!animate) {
864c5b12dd37e964b220c0a108e86ad8a2d12805747Winson            startRecentsActivity(ActivityOptions.makeCustomAnimation(mContext, -1, -1));
865bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi            return;
866bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi        }
867bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi
868190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (useThumbnailTransition) {
869c5b12dd37e964b220c0a108e86ad8a2d12805747Winson            launchState.launchedFromApp = true;
870c5b12dd37e964b220c0a108e86ad8a2d12805747Winson
871190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            // Try starting with a thumbnail transition
872e693aafe0511c2a7ffc571b22abeefba44046225Winson            ActivityOptions opts = getThumbnailTransitionActivityOptions(topTask, mDummyStackView);
873190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            if (opts != null) {
874c5b12dd37e964b220c0a108e86ad8a2d12805747Winson                startRecentsActivity(opts);
875190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            } else {
876190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // Fall through below to the non-thumbnail transition
877fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan                useThumbnailTransition = false;
878fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan            }
879fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan        }
880fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan
881fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan        if (!useThumbnailTransition) {
882c5b12dd37e964b220c0a108e86ad8a2d12805747Winson            launchState.launchedFromHome = true;
883c5b12dd37e964b220c0a108e86ad8a2d12805747Winson
884fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan            // If there is no thumbnail transition, but is launching from home into recents, then
885008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson            // use a quick home transition
886008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson            ActivityOptions opts = hasRecentTasks
887008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson                    ? getHomeTransitionActivityOptions()
888008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson                    : getUnknownTransitionActivityOptions();
889c5b12dd37e964b220c0a108e86ad8a2d12805747Winson            startRecentsActivity(opts);
890fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan        }
891fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan        mLastToggleTime = SystemClock.elapsedRealtime();
892fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan    }
893fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan
894fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan    /**
895190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Starts the recents activity.
896190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
897c5b12dd37e964b220c0a108e86ad8a2d12805747Winson    private void startRecentsActivity(ActivityOptions opts) {
898190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        Intent intent = new Intent();
8994bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan        intent.setClassName(RECENTS_PACKAGE, RECENTS_ACTIVITY);
900190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
901190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
902190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
903b58c46acec2e270f24ca587a64298f06cefb09cbSid Soundararajan
904190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (opts != null) {
905190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            mContext.startActivityAsUser(intent, opts.toBundle(), UserHandle.CURRENT);
906190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        } else {
907190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            mContext.startActivityAsUser(intent, UserHandle.CURRENT);
908190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
909cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggi        EventBus.getDefault().send(new RecentsActivityStartingEvent());
910190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
911190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
9123fb67562c548662910160d5672b1b9558bd6571cWinson    /**** OnAnimationFinishedListener Implementation ****/
9131a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski
9141a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski    @Override
9151a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski    public void onAnimationFinished() {
9161a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski        EventBus.getDefault().post(new EnterRecentsWindowLastAnimationFrameEvent());
9171a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski    }
918190fe3bf88388fcb109af64571e3baa0d01f1c37Winson}
919