RecentsImpl.java revision 94bc4f2238d8eefb77c63a560aa0de1335ee57ab
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;
2094bc4f2238d8eefb77c63a560aa0de1335ee57abWinsonimport static android.view.View.MeasureSpec;
21e693aafe0511c2a7ffc571b22abeefba44046225Winson
22190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.app.ActivityManager;
23190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.app.ActivityOptions;
24190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.content.ActivityNotFoundException;
25190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.content.Context;
26190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.content.Intent;
27190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.content.res.Resources;
28190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.graphics.Bitmap;
29190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.graphics.Canvas;
30190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.graphics.Rect;
313150e574e6e903a17193e0b90433d69c0e20ae4cWinsonimport android.graphics.RectF;
3222574afa00b7e32b7c4c5a1b98deebd67751b364Winsonimport android.graphics.drawable.Drawable;
33190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.os.Handler;
34190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.os.SystemClock;
35190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.os.UserHandle;
361b58561f66a9453754b1b419296899fdc08b4c93Winsonimport android.util.Log;
37190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.util.MutableBoolean;
38d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynskiimport android.view.AppTransitionAnimationSpec;
39190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport android.view.LayoutInflater;
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;
46899327f5cbbfb0eae5562b262ccea860c98f6bc4Jorim Jaggiimport com.android.systemui.recents.events.activity.DockedTopTaskEvent;
471b58561f66a9453754b1b419296899fdc08b4c93Winsonimport com.android.systemui.recents.events.activity.EnterRecentsWindowLastAnimationFrameEvent;
482adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggiimport com.android.systemui.recents.events.activity.ForcedResizableEvent;
49412e18058dc2cd5779d2451fce7fd74631f9e237Winsonimport com.android.systemui.recents.events.activity.HideRecentsEvent;
500d14d4da91c3d8b1221269712d5abf43a7cf9f31Winsonimport com.android.systemui.recents.events.activity.IterateRecentsEvent;
51b61e654b9a46e79827355104760a3ac5228380bcWinsonimport com.android.systemui.recents.events.activity.LaunchNextTaskRequestEvent;
52cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggiimport com.android.systemui.recents.events.activity.RecentsActivityStartingEvent;
53412e18058dc2cd5779d2451fce7fd74631f9e237Winsonimport com.android.systemui.recents.events.activity.ToggleRecentsEvent;
54190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
55190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
56dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggiimport com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
57dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggiimport com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
586b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinsonimport com.android.systemui.recents.misc.DozeTrigger;
59ab84fc56114c0963c6f701de9725f5413ab76da9Winsonimport com.android.systemui.recents.misc.ForegroundThread;
60190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.misc.SystemServicesProxy;
61938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kimimport com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener;
62190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.model.RecentsTaskLoadPlan;
63190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.model.RecentsTaskLoader;
64190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.model.Task;
65190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.model.TaskGrouping;
66190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.model.TaskStack;
6736a5a2c7003ef8157f276b411c3fda47ad2f75e3Winsonimport com.android.systemui.recents.views.TaskStackLayoutAlgorithm;
681b58561f66a9453754b1b419296899fdc08b4c93Winsonimport com.android.systemui.recents.views.TaskStackView;
69e693aafe0511c2a7ffc571b22abeefba44046225Winsonimport com.android.systemui.recents.views.TaskStackViewScroller;
70190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.views.TaskViewHeader;
71190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.recents.views.TaskViewTransform;
72d8b1d63f96580fe961e1751e7b4f56c90c1e0a76Winsonimport com.android.systemui.statusbar.BaseStatusBar;
73cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggiimport com.android.systemui.statusbar.phone.NavigationBarGestureHelper;
74190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.statusbar.phone.PhoneStatusBar;
75190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
76190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport java.util.ArrayList;
77190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
78190fe3bf88388fcb109af64571e3baa0d01f1c37Winson/**
79190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * An implementation of the Recents component for the current user.  For secondary users, this can
80190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * be called remotely from the system user.
81190fe3bf88388fcb109af64571e3baa0d01f1c37Winson */
82cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggipublic class RecentsImpl implements ActivityOptions.OnAnimationFinishedListener {
83190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
84190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    private final static String TAG = "RecentsImpl";
85b61e654b9a46e79827355104760a3ac5228380bcWinson
866b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    // The minimum amount of time between each recents button press that we will handle
876b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    private final static int MIN_TOGGLE_DELAY_MS = 350;
88b61e654b9a46e79827355104760a3ac5228380bcWinson
896b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    // The duration within which the user releasing the alt tab (from when they pressed alt tab)
906b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    // that the fast alt-tab animation will run.  If the user's alt-tab takes longer than this
916b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    // duration, then we will toggle recents after this duration.
926b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    private final static int FAST_ALT_TAB_DELAY_MS = 225;
93190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
94190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public final static String RECENTS_PACKAGE = "com.android.systemui";
95190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public final static String RECENTS_ACTIVITY = "com.android.systemui.recents.RecentsActivity";
96e693aafe0511c2a7ffc571b22abeefba44046225Winson
97190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
98938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kim     * An implementation of TaskStackListener, that allows us to listen for changes to the system
99190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * task stacks and update recents accordingly.
100190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
101938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kim    class TaskStackListenerImpl extends TaskStackListener {
102190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        @Override
103190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        public void onTaskStackChanged() {
104938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kim            // Preloads the next task
105a0fdeec66caa4d70786c7f6f31f79a24eef9da10Jorim Jaggi            RecentsConfiguration config = Recents.getConfiguration();
106190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            if (config.svelteLevel == RecentsConfiguration.SVELTE_NONE) {
107e7f138c7f0a190c86cec10fb32fa106aacae4093Winson                RecentsTaskLoader loader = Recents.getTaskLoader();
108e7f138c7f0a190c86cec10fb32fa106aacae4093Winson                SystemServicesProxy ssp = Recents.getSystemServices();
109190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                ActivityManager.RunningTaskInfo runningTaskInfo = ssp.getTopMostTask();
110190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
111190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // Load the next task only if we aren't svelte
112190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
11365c851e6e9e08656744b6f66d3da188e3283b17dWinson                loader.preloadTasks(plan, -1, true /* isTopTaskHome */);
114190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
115190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // This callback is made when a new activity is launched and the old one is paused
116190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // so ignore the current activity and try and preload the thumbnail for the
117190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // previous one.
118190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                if (runningTaskInfo != null) {
119190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    launchOpts.runningTaskId = runningTaskInfo.id;
120190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
121190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                launchOpts.numVisibleTasks = 2;
122190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                launchOpts.numVisibleTaskThumbnails = 2;
123190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                launchOpts.onlyLoadForCache = true;
124190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                launchOpts.onlyLoadPausedActivities = true;
125190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                loader.loadTasks(mContext, plan, launchOpts);
126190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
127190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
1282adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi
1292adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi        @Override
1302adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi        public void onActivityForcedResizable(String packageName, int taskId) {
1312adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi            EventBus.getDefault().sendOntoMainThread(
1322adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi                    new ForcedResizableEvent(packageName, taskId));
1332adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi
1342adba07d75419462873dfeef40d4c983d832ed99Jorim Jaggi        }
135190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
136190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
1374bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected static RecentsTaskLoadPlan sInstanceLoadPlan;
138190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
1394bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected Context mContext;
1404bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected Handler mHandler;
141190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    TaskStackListenerImpl mTaskStackListener;
142dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    boolean mDraggingInRecents;
143e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi    boolean mLaunchedWhileDocking;
144190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
145190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    // Task launching
146190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    Rect mTaskStackBounds = new Rect();
147190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    TaskViewTransform mTmpTransform = new TaskViewTransform();
148190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    int mStatusBarHeight;
149190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    int mNavBarHeight;
150190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    int mNavBarWidth;
151190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    int mTaskBarHeight;
152190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
153190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    // Header (for transition)
154190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    TaskViewHeader mHeaderBar;
155190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    final Object mHeaderBarLock = new Object();
1564bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected TaskStackView mDummyStackView;
157190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
158190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    // Variables to keep track of if we need to start recents after binding
1594bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected boolean mTriggeredFromAltTab;
1604bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected long mLastToggleTime;
1616b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    DozeTrigger mFastAltTabTrigger = new DozeTrigger(FAST_ALT_TAB_DELAY_MS, new Runnable() {
1626b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        @Override
1636b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        public void run() {
1646b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            // When this fires, then the user has not released alt-tab for at least
1656b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            // FAST_ALT_TAB_DELAY_MS milliseconds
166435b2e43cb131d0b38ba19f335aec78bb83944b2Jorim Jaggi            showRecents(mTriggeredFromAltTab, false /* draggingInRecents */, true /* animate */,
167435b2e43cb131d0b38ba19f335aec78bb83944b2Jorim Jaggi                    false /* reloadTasks */);
1686b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
1696b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    });
170190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
171aeb298c4db913b376fb6053ec30c132a93e04635Winson    protected Bitmap mThumbTransitionBitmapCache;
172190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
173190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public RecentsImpl(Context context) {
174190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        mContext = context;
175190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        mHandler = new Handler();
176190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
177ab84fc56114c0963c6f701de9725f5413ab76da9Winson        // Initialize the static foreground thread
178ab84fc56114c0963c6f701de9725f5413ab76da9Winson        ForegroundThread.get();
179ab84fc56114c0963c6f701de9725f5413ab76da9Winson
180190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Register the task stack listener
181938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kim        mTaskStackListener = new TaskStackListenerImpl();
182e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        SystemServicesProxy ssp = Recents.getSystemServices();
183e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        ssp.registerTaskStackListener(mTaskStackListener);
184190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
185190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Initialize the static configuration resources
186b94443d876d3c9575e88078d0e537bb385f79ca4Winson        reloadHeaderBarLayout();
187190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
188190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // When we start, preload the data associated with the previous recent tasks.
189190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // We can use a new plan since the caches will be the same.
190e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        RecentsTaskLoader loader = Recents.getTaskLoader();
191190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
19265c851e6e9e08656744b6f66d3da188e3283b17dWinson        loader.preloadTasks(plan, -1, true /* isTopTaskHome */);
193190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
194296278a0679375b8c43962a9e3c9bb4e8ab201e7Winson Chung        launchOpts.numVisibleTasks = loader.getIconCacheSize();
195190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.numVisibleTaskThumbnails = loader.getThumbnailCacheSize();
196190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.onlyLoadForCache = true;
197190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        loader.loadTasks(mContext, plan, launchOpts);
198190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
199190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
200190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void onBootCompleted() {
201aeb298c4db913b376fb6053ec30c132a93e04635Winson        // Do nothing
202190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
203190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
204190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void onConfigurationChanged() {
205b94443d876d3c9575e88078d0e537bb385f79ca4Winson        reloadHeaderBarLayout();
206190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
207190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
208190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
209190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * This is only called from the system user's Recents.  Secondary users will instead proxy their
210190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * visibility change events through to the system user via
211190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * {@link Recents#onBusEvent(RecentsVisibilityChangedEvent)}.
212190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
213190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void onVisibilityChanged(Context context, boolean visible) {
214190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        SystemUIApplication app = (SystemUIApplication) context;
215190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        PhoneStatusBar statusBar = app.getComponent(PhoneStatusBar.class);
216190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (statusBar != null) {
217190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            statusBar.updateRecentsVisibility(visible);
218190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
219190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
220190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
221190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
222190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * This is only called from the system user's Recents.  Secondary users will instead proxy their
223190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * visibility change events through to the system user via
224190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * {@link Recents#onBusEvent(ScreenPinningRequestEvent)}.
225190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
226190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void onStartScreenPinning(Context context) {
227190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        SystemUIApplication app = (SystemUIApplication) context;
228190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        PhoneStatusBar statusBar = app.getComponent(PhoneStatusBar.class);
229190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (statusBar != null) {
230190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            statusBar.showScreenPinningRequest(false);
231190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
232190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
233190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
234bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi    public void showRecents(boolean triggeredFromAltTab, boolean draggingInRecents,
235e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi            boolean animate, boolean launchedWhileDockingTask) {
236190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        mTriggeredFromAltTab = triggeredFromAltTab;
237dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi        mDraggingInRecents = draggingInRecents;
238e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi        mLaunchedWhileDocking = launchedWhileDockingTask;
239e693aafe0511c2a7ffc571b22abeefba44046225Winson        if (mFastAltTabTrigger.isAsleep()) {
240e693aafe0511c2a7ffc571b22abeefba44046225Winson            // Fast alt-tab duration has elapsed, fall through to showing Recents and reset
241e693aafe0511c2a7ffc571b22abeefba44046225Winson            mFastAltTabTrigger.stopDozing();
2426b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        } else if (mFastAltTabTrigger.isDozing()) {
243e693aafe0511c2a7ffc571b22abeefba44046225Winson            // Fast alt-tab duration has not elapsed.  If this is triggered by a different
244e693aafe0511c2a7ffc571b22abeefba44046225Winson            // showRecents() call, then ignore that call for now.
245e693aafe0511c2a7ffc571b22abeefba44046225Winson            // TODO: We can not handle quick tabs that happen between the initial showRecents() call
246e693aafe0511c2a7ffc571b22abeefba44046225Winson            //       that started the activity and the activity starting up.  The severity of this
247e693aafe0511c2a7ffc571b22abeefba44046225Winson            //       is inversely proportional to the FAST_ALT_TAB_DELAY_MS duration though.
2486b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            if (!triggeredFromAltTab) {
2496b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                return;
2506b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            }
2516b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            mFastAltTabTrigger.stopDozing();
252e693aafe0511c2a7ffc571b22abeefba44046225Winson        } else if (triggeredFromAltTab) {
253e693aafe0511c2a7ffc571b22abeefba44046225Winson            // The fast alt-tab detector is not yet running, so start the trigger and wait for the
254e693aafe0511c2a7ffc571b22abeefba44046225Winson            // hideRecents() call, or for the fast alt-tab duration to elapse
255e693aafe0511c2a7ffc571b22abeefba44046225Winson            mFastAltTabTrigger.startDozing();
256e693aafe0511c2a7ffc571b22abeefba44046225Winson            return;
2576b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
258190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
259190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        try {
260190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            // Check if the top task is in the home stack, and start the recents activity
261e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            SystemServicesProxy ssp = Recents.getSystemServices();
262e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            ActivityManager.RunningTaskInfo topTask = ssp.getTopMostTask();
263190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            MutableBoolean isTopTaskHome = new MutableBoolean(true);
264e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            if (topTask == null || !ssp.isRecentsTopMost(topTask, isTopTaskHome)) {
265bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi                startRecentsActivity(topTask, isTopTaskHome.value, animate);
266190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
267190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        } catch (ActivityNotFoundException e) {
2681b58561f66a9453754b1b419296899fdc08b4c93Winson            Log.e(TAG, "Failed to launch RecentsActivity", e);
269190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
270190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
271190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
272190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void hideRecents(boolean triggeredFromAltTab, boolean triggeredFromHomeKey) {
2732799eca21715390faf10596884b65165528f89c4Winson        if (triggeredFromAltTab && mFastAltTabTrigger.isDozing()) {
2742799eca21715390faf10596884b65165528f89c4Winson            // The user has released alt-tab before the trigger has run, so just show the next
2752799eca21715390faf10596884b65165528f89c4Winson            // task immediately
2762799eca21715390faf10596884b65165528f89c4Winson            showNextTask();
2776b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
2782799eca21715390faf10596884b65165528f89c4Winson            // Cancel the fast alt-tab trigger
2792799eca21715390faf10596884b65165528f89c4Winson            mFastAltTabTrigger.stopDozing();
2802799eca21715390faf10596884b65165528f89c4Winson            return;
281190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
2822799eca21715390faf10596884b65165528f89c4Winson
2832799eca21715390faf10596884b65165528f89c4Winson        // Defer to the activity to handle hiding recents, if it handles it, then it must still
2842799eca21715390faf10596884b65165528f89c4Winson        // be visible
2852799eca21715390faf10596884b65165528f89c4Winson        EventBus.getDefault().post(new HideRecentsEvent(triggeredFromAltTab,
2862799eca21715390faf10596884b65165528f89c4Winson                triggeredFromHomeKey));
287190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
288190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
289190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void toggleRecents() {
2906b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Skip this toggle if we are already waiting to trigger recents via alt-tab
2916b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        if (mFastAltTabTrigger.isDozing()) {
2926b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            return;
2936b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
2946b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
295dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi        mDraggingInRecents = false;
296e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi        mLaunchedWhileDocking = false;
297190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        mTriggeredFromAltTab = false;
298190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
299190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        try {
300e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            SystemServicesProxy ssp = Recents.getSystemServices();
301e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            ActivityManager.RunningTaskInfo topTask = ssp.getTopMostTask();
302190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            MutableBoolean isTopTaskHome = new MutableBoolean(true);
303b61e654b9a46e79827355104760a3ac5228380bcWinson            long elapsedTime = SystemClock.elapsedRealtime() - mLastToggleTime;
304b61e654b9a46e79827355104760a3ac5228380bcWinson
305e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            if (topTask != null && ssp.isRecentsTopMost(topTask, isTopTaskHome)) {
306e693aafe0511c2a7ffc571b22abeefba44046225Winson                RecentsDebugFlags debugFlags = Recents.getDebugFlags();
3075da4347b41b105dc6c6c01bf2810af3ce3013229Winson                RecentsConfiguration config = Recents.getConfiguration();
3085da4347b41b105dc6c6c01bf2810af3ce3013229Winson                RecentsActivityLaunchState launchState = config.getLaunchState();
309ead5c0fc20e5c60a68cd017af0b8eacaf1167a1cWinson Chung                if (!launchState.launchedWithAltTab) {
310b61e654b9a46e79827355104760a3ac5228380bcWinson                    // If the user taps quickly
311e693aafe0511c2a7ffc571b22abeefba44046225Winson                    if (!debugFlags.isPagingEnabled() ||
312e693aafe0511c2a7ffc571b22abeefba44046225Winson                            (ViewConfiguration.getDoubleTapMinTime() < elapsedTime &&
313e693aafe0511c2a7ffc571b22abeefba44046225Winson                                    elapsedTime < ViewConfiguration.getDoubleTapTimeout())) {
314b61e654b9a46e79827355104760a3ac5228380bcWinson                        // Launch the next focused task
315b61e654b9a46e79827355104760a3ac5228380bcWinson                        EventBus.getDefault().post(new LaunchNextTaskRequestEvent());
316b61e654b9a46e79827355104760a3ac5228380bcWinson                    } else {
317b61e654b9a46e79827355104760a3ac5228380bcWinson                        // Notify recents to move onto the next task
318b61e654b9a46e79827355104760a3ac5228380bcWinson                        EventBus.getDefault().post(new IterateRecentsEvent());
319b61e654b9a46e79827355104760a3ac5228380bcWinson                    }
3200d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                } else {
3210d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    // If the user has toggled it too quickly, then just eat up the event here (it's
3220d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    // better than showing a janky screenshot).
3230d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    // NOTE: Ideally, the screenshot mechanism would take the window transform into
3240d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    // account
325b61e654b9a46e79827355104760a3ac5228380bcWinson                    if (elapsedTime < MIN_TOGGLE_DELAY_MS) {
3260d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                        return;
3270d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    }
3280d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson
3290d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    EventBus.getDefault().post(new ToggleRecentsEvent());
3300d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    mLastToggleTime = SystemClock.elapsedRealtime();
3310d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                }
332190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                return;
333190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            } else {
3340d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                // If the user has toggled it too quickly, then just eat up the event here (it's
3350d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                // better than showing a janky screenshot).
3360d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                // NOTE: Ideally, the screenshot mechanism would take the window transform into
3370d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                // account
338b61e654b9a46e79827355104760a3ac5228380bcWinson                if (elapsedTime < MIN_TOGGLE_DELAY_MS) {
3390d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    return;
3400d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                }
3410d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson
342190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // Otherwise, start the recents activity
343bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi                startRecentsActivity(topTask, isTopTaskHome.value, true /* animate */);
344d8b1d63f96580fe961e1751e7b4f56c90c1e0a76Winson
345d8b1d63f96580fe961e1751e7b4f56c90c1e0a76Winson                // Only close the other system windows if we are actually showing recents
346d8b1d63f96580fe961e1751e7b4f56c90c1e0a76Winson                ssp.sendCloseSystemWindows(BaseStatusBar.SYSTEM_DIALOG_REASON_RECENT_APPS);
3470d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                mLastToggleTime = SystemClock.elapsedRealtime();
348190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
349190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        } catch (ActivityNotFoundException e) {
3501b58561f66a9453754b1b419296899fdc08b4c93Winson            Log.e(TAG, "Failed to launch RecentsActivity", e);
351190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
352190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
353190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
354190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void preloadRecents() {
355190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Preload only the raw task list into a new load plan (which will be consumed by the
356190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // RecentsActivity) only if there is a task to animate to.
357e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        SystemServicesProxy ssp = Recents.getSystemServices();
358e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        ActivityManager.RunningTaskInfo topTask = ssp.getTopMostTask();
359190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        MutableBoolean topTaskHome = new MutableBoolean(true);
360e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        if (topTask != null && !ssp.isRecentsTopMost(topTask, topTaskHome)) {
3615b4e0d2b11b2b3579bac36fde5ac3805346618ccWinson            RecentsTaskLoader loader = Recents.getTaskLoader();
3625b4e0d2b11b2b3579bac36fde5ac3805346618ccWinson            sInstanceLoadPlan = loader.createLoadPlan(mContext);
363190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            sInstanceLoadPlan.preloadRawTasks(topTaskHome.value);
36465c851e6e9e08656744b6f66d3da188e3283b17dWinson            loader.preloadTasks(sInstanceLoadPlan, topTask.id, topTaskHome.value);
365190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            TaskStack stack = sInstanceLoadPlan.getTaskStack();
3664b057c6787624b75613769a857ccdf51114bb7f2Winson            if (stack.getTaskCount() > 0) {
367aeb298c4db913b376fb6053ec30c132a93e04635Winson                // Only preload the icon (but not the thumbnail since it may not have been taken for
368aeb298c4db913b376fb6053ec30c132a93e04635Winson                // the pausing activity)
369aeb298c4db913b376fb6053ec30c132a93e04635Winson                preloadIcon(topTask);
370aeb298c4db913b376fb6053ec30c132a93e04635Winson
371aeb298c4db913b376fb6053ec30c132a93e04635Winson                // At this point, we don't know anything about the stack state.  So only calculate
372aeb298c4db913b376fb6053ec30c132a93e04635Winson                // the dimensions of the thumbnail that we need for the transition into Recents, but
373aeb298c4db913b376fb6053ec30c132a93e04635Winson                // do not draw it until we construct the activity options when we start Recents
374aeb298c4db913b376fb6053ec30c132a93e04635Winson                updateHeaderBarLayout(stack);
375190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
376190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
377190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
378190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
379190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void cancelPreloadingRecents() {
380190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Do nothing
381190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
382190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
383dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    public void onDraggingInRecents(float distanceFromTop) {
384dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi        EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEvent(distanceFromTop));
385dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    }
386dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi
387dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    public void onDraggingInRecentsEnded(float velocity) {
388dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi        EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEndedEvent(velocity));
389dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    }
390dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi
3916b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    /**
3926b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson     * Transitions to the next recent task in the stack.
3936b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson     */
3946b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    public void showNextTask() {
3956b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        SystemServicesProxy ssp = Recents.getSystemServices();
3966b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        RecentsTaskLoader loader = Recents.getTaskLoader();
3976b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
39865c851e6e9e08656744b6f66d3da188e3283b17dWinson        loader.preloadTasks(plan, -1, true /* isTopTaskHome */);
3996b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        TaskStack focusedStack = plan.getTaskStack();
4006b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4016b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Return early if there are no tasks in the focused stack
4024b057c6787624b75613769a857ccdf51114bb7f2Winson        if (focusedStack == null || focusedStack.getTaskCount() == 0) return;
4036b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4046b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        ActivityManager.RunningTaskInfo runningTask = ssp.getTopMostTask();
4056b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Return early if there is no running task
4066b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        if (runningTask == null) return;
4076b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4086b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Find the task in the recents list
409e86deb8adb022ba7721a1af1112d3a45e757095cWinson        boolean isTopTaskHome = SystemServicesProxy.isHomeStack(runningTask.stackId);
410250608a5cd08862f4752a924d51710805850db8aWinson        ArrayList<Task> tasks = focusedStack.getStackTasks();
4116b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        Task toTask = null;
4126b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        ActivityOptions launchOpts = null;
4136b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        int taskCount = tasks.size();
4146b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        for (int i = taskCount - 1; i >= 1; i--) {
4156b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            Task task = tasks.get(i);
416e86deb8adb022ba7721a1af1112d3a45e757095cWinson            if (isTopTaskHome) {
417e86deb8adb022ba7721a1af1112d3a45e757095cWinson                toTask = tasks.get(i - 1);
418e86deb8adb022ba7721a1af1112d3a45e757095cWinson                launchOpts = ActivityOptions.makeCustomAnimation(mContext,
419e86deb8adb022ba7721a1af1112d3a45e757095cWinson                        R.anim.recents_launch_next_affiliated_task_target,
420e86deb8adb022ba7721a1af1112d3a45e757095cWinson                        R.anim.recents_fast_toggle_app_home_exit);
421e86deb8adb022ba7721a1af1112d3a45e757095cWinson                break;
422e86deb8adb022ba7721a1af1112d3a45e757095cWinson            } else if (task.key.id == runningTask.id) {
4236b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                toTask = tasks.get(i - 1);
4246b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                launchOpts = ActivityOptions.makeCustomAnimation(mContext,
4256b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                        R.anim.recents_launch_prev_affiliated_task_target,
4266b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                        R.anim.recents_launch_prev_affiliated_task_source);
4276b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                break;
4286b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            }
4296b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
4306b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4316b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Return early if there is no next task
4326b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        if (toTask == null) {
4336b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            ssp.startInPlaceAnimationOnFrontMostApplication(
4346b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                    ActivityOptions.makeCustomInPlaceAnimation(mContext,
4356b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                            R.anim.recents_launch_prev_affiliated_task_bounce));
4366b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            return;
4376b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
4386b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4396b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Launch the task
44064ae08aa16fdf7ee33073979400afa40692342f1Wale Ogunwale        ssp.startActivityFromRecents(mContext, toTask.key, toTask.title, launchOpts);
4416b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    }
4426b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4436b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    /**
4446b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson     * Transitions to the next affiliated task.
4456b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson     */
446190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void showRelativeAffiliatedTask(boolean showNextTask) {
447e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        SystemServicesProxy ssp = Recents.getSystemServices();
448e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        RecentsTaskLoader loader = Recents.getTaskLoader();
449190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
45065c851e6e9e08656744b6f66d3da188e3283b17dWinson        loader.preloadTasks(plan, -1, true /* isTopTaskHome */);
451190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        TaskStack focusedStack = plan.getTaskStack();
452190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
453190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Return early if there are no tasks in the focused stack
4544b057c6787624b75613769a857ccdf51114bb7f2Winson        if (focusedStack == null || focusedStack.getTaskCount() == 0) return;
455190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
456e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        ActivityManager.RunningTaskInfo runningTask = ssp.getTopMostTask();
457190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Return early if there is no running task (can't determine affiliated tasks in this case)
458190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (runningTask == null) return;
459190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Return early if the running task is in the home stack (optimization)
4605510f6c1b9c20483e1507147eed7b24ac8bb6363Winson        if (SystemServicesProxy.isHomeStack(runningTask.stackId)) return;
461190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
462190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Find the task in the recents list
463250608a5cd08862f4752a924d51710805850db8aWinson        ArrayList<Task> tasks = focusedStack.getStackTasks();
464190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        Task toTask = null;
465190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        ActivityOptions launchOpts = null;
466190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        int taskCount = tasks.size();
467190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        int numAffiliatedTasks = 0;
468190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        for (int i = 0; i < taskCount; i++) {
469190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            Task task = tasks.get(i);
470190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            if (task.key.id == runningTask.id) {
471190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                TaskGrouping group = task.group;
472190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                Task.TaskKey toTaskKey;
473190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                if (showNextTask) {
474190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    toTaskKey = group.getNextTaskInGroup(task);
475190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    launchOpts = ActivityOptions.makeCustomAnimation(mContext,
476190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            R.anim.recents_launch_next_affiliated_task_target,
477190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            R.anim.recents_launch_next_affiliated_task_source);
478190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                } else {
479190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    toTaskKey = group.getPrevTaskInGroup(task);
480190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    launchOpts = ActivityOptions.makeCustomAnimation(mContext,
481190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            R.anim.recents_launch_prev_affiliated_task_target,
482190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            R.anim.recents_launch_prev_affiliated_task_source);
483190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
484190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                if (toTaskKey != null) {
485190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    toTask = focusedStack.findTaskWithId(toTaskKey.id);
486190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
487190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                numAffiliatedTasks = group.getTaskCount();
488190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                break;
489190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
490190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
491190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
492190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Return early if there is no next task
493190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (toTask == null) {
494190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            if (numAffiliatedTasks > 1) {
495190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                if (showNextTask) {
496e7f138c7f0a190c86cec10fb32fa106aacae4093Winson                    ssp.startInPlaceAnimationOnFrontMostApplication(
497190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            ActivityOptions.makeCustomInPlaceAnimation(mContext,
498190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                                    R.anim.recents_launch_next_affiliated_task_bounce));
499190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                } else {
500e7f138c7f0a190c86cec10fb32fa106aacae4093Winson                    ssp.startInPlaceAnimationOnFrontMostApplication(
501190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            ActivityOptions.makeCustomInPlaceAnimation(mContext,
502190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                                    R.anim.recents_launch_prev_affiliated_task_bounce));
503190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
504190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
505190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            return;
506190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
507190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
508190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Keep track of actually launched affiliated tasks
509190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        MetricsLogger.count(mContext, "overview_affiliated_task_launch", 1);
510190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
511190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Launch the task
51264ae08aa16fdf7ee33073979400afa40692342f1Wale Ogunwale        ssp.startActivityFromRecents(mContext, toTask.key, toTask.title, launchOpts);
513190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
514190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
515190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void showNextAffiliatedTask() {
516190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Keep track of when the affiliated task is triggered
517190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        MetricsLogger.count(mContext, "overview_affiliated_task_next", 1);
518190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        showRelativeAffiliatedTask(true);
519190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
520190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
521190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void showPrevAffiliatedTask() {
522190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Keep track of when the affiliated task is triggered
523190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        MetricsLogger.count(mContext, "overview_affiliated_task_prev", 1);
524190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        showRelativeAffiliatedTask(false);
525190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
526190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
527cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggi    public void dockTopTask(int topTaskId, int dragMode,
528cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggi            int stackCreateMode, Rect initialBounds) {
52975b2597e5826662c1740d6f3b81966a9e6a9e7b4Jorim Jaggi        SystemServicesProxy ssp = Recents.getSystemServices();
5309511b0f1e9ac629a4a747a0c9373d33ab33cfc32Jorim Jaggi
5319511b0f1e9ac629a4a747a0c9373d33ab33cfc32Jorim Jaggi        // Make sure we inform DividerView before we actually start the activity so we can change
5329511b0f1e9ac629a4a747a0c9373d33ab33cfc32Jorim Jaggi        // the resize mode already.
533e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang        if (ssp.moveTaskToDockedStack(topTaskId, stackCreateMode, initialBounds)) {
534899327f5cbbfb0eae5562b262ccea860c98f6bc4Jorim Jaggi            EventBus.getDefault().send(new DockedTopTaskEvent(dragMode, initialBounds));
535e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang            showRecents(
536e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang                    false /* triggeredFromAltTab */,
537e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang                    dragMode == NavigationBarGestureHelper.DRAG_MODE_RECENTS,
538e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang                    false /* animate */,
539e693aafe0511c2a7ffc571b22abeefba44046225Winson                    true /* launchedWhileDockingTask*/);
540e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang        }
54175b2597e5826662c1740d6f3b81966a9e6a9e7b4Jorim Jaggi    }
54275b2597e5826662c1740d6f3b81966a9e6a9e7b4Jorim Jaggi
543190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
544190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Returns the preloaded load plan and invalidates it.
545190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
546190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public static RecentsTaskLoadPlan consumeInstanceLoadPlan() {
547190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan plan = sInstanceLoadPlan;
548190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        sInstanceLoadPlan = null;
549190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return plan;
550190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
551190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
552190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
553b94443d876d3c9575e88078d0e537bb385f79ca4Winson     * Reloads all the layouts for the header bar transition.
554b94443d876d3c9575e88078d0e537bb385f79ca4Winson     */
555b94443d876d3c9575e88078d0e537bb385f79ca4Winson    private void reloadHeaderBarLayout() {
556b94443d876d3c9575e88078d0e537bb385f79ca4Winson        Resources res = mContext.getResources();
557b94443d876d3c9575e88078d0e537bb385f79ca4Winson        LayoutInflater inflater = LayoutInflater.from(mContext);
558b94443d876d3c9575e88078d0e537bb385f79ca4Winson
559b94443d876d3c9575e88078d0e537bb385f79ca4Winson        mStatusBarHeight = res.getDimensionPixelSize(
560b94443d876d3c9575e88078d0e537bb385f79ca4Winson                com.android.internal.R.dimen.status_bar_height);
561b94443d876d3c9575e88078d0e537bb385f79ca4Winson        mNavBarHeight = res.getDimensionPixelSize(
562b94443d876d3c9575e88078d0e537bb385f79ca4Winson                com.android.internal.R.dimen.navigation_bar_height);
563b94443d876d3c9575e88078d0e537bb385f79ca4Winson        mNavBarWidth = res.getDimensionPixelSize(
564b94443d876d3c9575e88078d0e537bb385f79ca4Winson                com.android.internal.R.dimen.navigation_bar_width);
565217009356efa2b854ab3981dff8d315a0d679c73Winson        mTaskBarHeight = TaskStackLayoutAlgorithm.getDimensionForDevice(res,
566217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height,
567217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height,
568217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height,
569217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height_tablet_land,
570217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height,
571217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height_tablet_land);
5728873754f66527d2cc9feab295dd4eef23298212aWinson        mDummyStackView = new TaskStackView(mContext);
573b94443d876d3c9575e88078d0e537bb385f79ca4Winson        mHeaderBar = (TaskViewHeader) inflater.inflate(R.layout.recents_task_view_header,
574b94443d876d3c9575e88078d0e537bb385f79ca4Winson                null, false);
575b94443d876d3c9575e88078d0e537bb385f79ca4Winson    }
576b94443d876d3c9575e88078d0e537bb385f79ca4Winson
577b94443d876d3c9575e88078d0e537bb385f79ca4Winson    /**
578190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Prepares the header bar layout for the next transition, if the task view bounds has changed
579190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * since the last call, it will attempt to re-measure and layout the header bar to the new size.
580190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     *
581f0d1c44a59a10707baa0cca8dd377302260710c1Winson     * @param stack the stack to initialize the stack layout with
582190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
583008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson    private void updateHeaderBarLayout(TaskStack stack) {
584e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        SystemServicesProxy ssp = Recents.getSystemServices();
585c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        Rect systemInsets = new Rect();
586c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        ssp.getStableInsets(systemInsets);
587e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        Rect windowRect = ssp.getWindowRect();
588cf9b8326cecfca3917779c3701aa08eaaad56505Winson        // When docked, the nav bar insets are consumed and the activity is measured without insets.
589cf9b8326cecfca3917779c3701aa08eaaad56505Winson        // However, the window bounds include the insets, so we need to subtract them here to make
590cf9b8326cecfca3917779c3701aa08eaaad56505Winson        // them identical.
591cf9b8326cecfca3917779c3701aa08eaaad56505Winson        if (ssp.hasDockedTask()) {
592cf9b8326cecfca3917779c3701aa08eaaad56505Winson            windowRect.bottom -= systemInsets.bottom;
593cf9b8326cecfca3917779c3701aa08eaaad56505Winson            systemInsets.bottom = 0;
594cf9b8326cecfca3917779c3701aa08eaaad56505Winson        }
595c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        calculateWindowStableInsets(systemInsets, windowRect);
596c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        windowRect.offsetTo(0, 0);
597190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
59859924fe0d9136cf349759bea1e06b661603f95feWinson        TaskStackLayoutAlgorithm stackLayout = mDummyStackView.getStackAlgorithm();
599190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
600190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Rebind the header bar and draw it for the transition
6018873754f66527d2cc9feab295dd4eef23298212aWinson        stackLayout.setSystemInsets(systemInsets);
602f0d1c44a59a10707baa0cca8dd377302260710c1Winson        if (stack != null) {
603cf9b8326cecfca3917779c3701aa08eaaad56505Winson            stackLayout.getTaskStackBounds(windowRect, systemInsets.top, systemInsets.right,
604cf9b8326cecfca3917779c3701aa08eaaad56505Winson                    mTaskStackBounds);
605aeb298c4db913b376fb6053ec30c132a93e04635Winson            stackLayout.reset();
606cf9b8326cecfca3917779c3701aa08eaaad56505Winson            stackLayout.initialize(windowRect, mTaskStackBounds,
607f0d1c44a59a10707baa0cca8dd377302260710c1Winson                    TaskStackLayoutAlgorithm.StackState.getStackStateForStack(stack));
608a1ededd056d51532ab18354f17f8065ec1134535Winson            mDummyStackView.setTasks(stack, false /* allowNotifyStackChanges */);
609190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
610aeb298c4db913b376fb6053ec30c132a93e04635Winson            Rect taskViewBounds = stackLayout.getUntransformedTaskViewBounds();
61194bc4f2238d8eefb77c63a560aa0de1335ee57abWinson            if (!taskViewBounds.isEmpty()) {
61294bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                int taskViewWidth = taskViewBounds.width();
61394bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                synchronized (mHeaderBarLock) {
61494bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                    if (mHeaderBar.getMeasuredWidth() != taskViewWidth ||
61594bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                            mHeaderBar.getMeasuredHeight() != mTaskBarHeight) {
61694bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                        mHeaderBar.measure(
61794bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                                MeasureSpec.makeMeasureSpec(taskViewWidth, MeasureSpec.EXACTLY),
61894bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                                MeasureSpec.makeMeasureSpec(mTaskBarHeight, MeasureSpec.EXACTLY));
61994bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                    }
62094bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                    mHeaderBar.layout(0, 0, taskViewWidth, mTaskBarHeight);
621aeb298c4db913b376fb6053ec30c132a93e04635Winson                }
622aeb298c4db913b376fb6053ec30c132a93e04635Winson
62394bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                // Update the transition bitmap to match the new header bar height
62494bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                if (mThumbTransitionBitmapCache == null ||
62594bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                        (mThumbTransitionBitmapCache.getWidth() != taskViewWidth) ||
62694bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                        (mThumbTransitionBitmapCache.getHeight() != mTaskBarHeight)) {
62794bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                    mThumbTransitionBitmapCache = Bitmap.createBitmap(taskViewWidth,
62894bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                            mTaskBarHeight, Bitmap.Config.ARGB_8888);
62994bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                }
630aeb298c4db913b376fb6053ec30c132a93e04635Winson            }
631190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
632190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
633190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
634190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
635c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi     * Given the stable insets and the rect for our window, calculates the insets that affect our
636c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi     * window.
637c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi     */
638c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi    private void calculateWindowStableInsets(Rect inOutInsets, Rect windowRect) {
639c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        Rect displayRect = Recents.getSystemServices().getDisplayRect();
640c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi
641c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        // Display rect without insets - available app space
642c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        Rect appRect = new Rect(displayRect);
643c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        appRect.inset(inOutInsets);
644c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi
645c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        // Our window intersected with available app space
646c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        Rect windowRectWithInsets = new Rect(windowRect);
647c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        windowRectWithInsets.intersect(appRect);
648c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        inOutInsets.left = windowRectWithInsets.left - windowRect.left;
649c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        inOutInsets.top = windowRectWithInsets.top - windowRect.top;
650c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        inOutInsets.right = windowRect.right - windowRectWithInsets.right;
651c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        inOutInsets.bottom = windowRect.bottom - windowRectWithInsets.bottom;
652c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi    }
653c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi
654c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi    /**
655190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Preloads the icon of a task.
656190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
657190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    private void preloadIcon(ActivityManager.RunningTaskInfo task) {
658190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Ensure that we load the running task's icon
659190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
660190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.runningTaskId = task.id;
661190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.loadThumbnails = false;
662190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.onlyLoadForCache = true;
663e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        Recents.getTaskLoader().loadTasks(mContext, sInstanceLoadPlan, launchOpts);
664190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
665190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
666190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
667190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Creates the activity options for a unknown state->recents transition.
668190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
6694bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected ActivityOptions getUnknownTransitionActivityOptions() {
670190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return ActivityOptions.makeCustomAnimation(mContext,
671190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                R.anim.recents_from_unknown_enter,
672190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                R.anim.recents_from_unknown_exit,
6733fb67562c548662910160d5672b1b9558bd6571cWinson                mHandler, null);
674190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
675190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
676190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
677190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Creates the activity options for a home->recents transition.
678190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
679008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson    protected ActivityOptions getHomeTransitionActivityOptions() {
680190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return ActivityOptions.makeCustomAnimation(mContext,
681190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                R.anim.recents_from_launcher_enter,
682190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                R.anim.recents_from_launcher_exit,
6833fb67562c548662910160d5672b1b9558bd6571cWinson                mHandler, null);
684190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
685190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
686190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
687190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Creates the activity options for an app->recents transition.
688190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
689190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    private ActivityOptions getThumbnailTransitionActivityOptions(
690e693aafe0511c2a7ffc571b22abeefba44046225Winson            ActivityManager.RunningTaskInfo topTask, TaskStackView stackView) {
691d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski        if (topTask.stackId == FREEFORM_WORKSPACE_STACK_ID) {
692d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            ArrayList<AppTransitionAnimationSpec> specs = new ArrayList<>();
693e693aafe0511c2a7ffc571b22abeefba44046225Winson            ArrayList<Task> tasks = stackView.getStack().getStackTasks();
694e693aafe0511c2a7ffc571b22abeefba44046225Winson            TaskStackLayoutAlgorithm stackLayout = stackView.getStackAlgorithm();
695e693aafe0511c2a7ffc571b22abeefba44046225Winson            TaskStackViewScroller stackScroller = stackView.getScroller();
696e693aafe0511c2a7ffc571b22abeefba44046225Winson
697003eda6c230a1b1c4e3709466c46660e7602d740Winson            stackView.updateLayoutAlgorithm(true /* boundScroll */);
698619e40cd56266a362ab7da80cb9e4eba6c33b204Winson            stackView.updateToInitialState(true /* scrollToInitialState */);
699e693aafe0511c2a7ffc571b22abeefba44046225Winson
700d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            for (int i = tasks.size() - 1; i >= 0; i--) {
701d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                Task task = tasks.get(i);
702387aac6ad7bde21f2c2510cb8a2f3af3959721e0Winson                if (task.isFreeformTask()) {
703e693aafe0511c2a7ffc571b22abeefba44046225Winson                    mTmpTransform = stackLayout.getStackTransformScreenCoordinates(task,
704e693aafe0511c2a7ffc571b22abeefba44046225Winson                                    stackScroller.getStackScroll(), mTmpTransform, null);
705aeb298c4db913b376fb6053ec30c132a93e04635Winson                    Bitmap thumbnail = drawThumbnailTransitionBitmap(task, mTmpTransform,
706aeb298c4db913b376fb6053ec30c132a93e04635Winson                            mThumbTransitionBitmapCache);
707d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                    Rect toTaskRect = new Rect();
708d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                    mTmpTransform.rect.round(toTaskRect);
709d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                    specs.add(new AppTransitionAnimationSpec(task.key.id, thumbnail, toTaskRect));
710d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                }
711d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            }
712d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            AppTransitionAnimationSpec[] specsArray = new AppTransitionAnimationSpec[specs.size()];
713d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            specs.toArray(specsArray);
714d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            return ActivityOptions.makeThumbnailAspectScaleDownAnimation(mDummyStackView,
7153fb67562c548662910160d5672b1b9558bd6571cWinson                    specsArray, mHandler, null, this);
716d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski        } else {
717d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            // Update the destination rect
718d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            Task toTask = new Task();
719e693aafe0511c2a7ffc571b22abeefba44046225Winson            TaskViewTransform toTransform = getThumbnailTransitionTransform(stackView, toTask);
720aeb298c4db913b376fb6053ec30c132a93e04635Winson            Bitmap thumbnail = drawThumbnailTransitionBitmap(toTask, toTransform,
721aeb298c4db913b376fb6053ec30c132a93e04635Winson                    mThumbTransitionBitmapCache);
722d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            if (thumbnail != null) {
723aeb298c4db913b376fb6053ec30c132a93e04635Winson                RectF toTaskRect = toTransform.rect;
724d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                return ActivityOptions.makeThumbnailAspectScaleDownAnimation(mDummyStackView,
725d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                        thumbnail, (int) toTaskRect.left, (int) toTaskRect.top,
7263fb67562c548662910160d5672b1b9558bd6571cWinson                        (int) toTaskRect.width(), (int) toTaskRect.height(), mHandler, null);
727d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            }
728d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            // If both the screenshot and thumbnail fails, then just fall back to the default transition
729d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            return getUnknownTransitionActivityOptions();
730d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski        }
731d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski    }
732190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
733190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
734190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Returns the transition rect for the given task id.
735190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
736e693aafe0511c2a7ffc571b22abeefba44046225Winson    private TaskViewTransform getThumbnailTransitionTransform(TaskStackView stackView,
737e693aafe0511c2a7ffc571b22abeefba44046225Winson            Task runningTaskOut) {
738190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Find the running task in the TaskStack
739e693aafe0511c2a7ffc571b22abeefba44046225Winson        TaskStack stack = stackView.getStack();
74065c851e6e9e08656744b6f66d3da188e3283b17dWinson        Task launchTask = stack.getLaunchTarget();
74165c851e6e9e08656744b6f66d3da188e3283b17dWinson        if (launchTask != null) {
74265c851e6e9e08656744b6f66d3da188e3283b17dWinson            runningTaskOut.copyFrom(launchTask);
74365c851e6e9e08656744b6f66d3da188e3283b17dWinson        } else {
744190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            // If no task is specified or we can not find the task just use the front most one
74535a8b04140598a5b5c4865254b942adb6a830991Winson            launchTask = stack.getStackFrontMostTask(true /* includeFreeform */);
74665c851e6e9e08656744b6f66d3da188e3283b17dWinson            runningTaskOut.copyFrom(launchTask);
747190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
748190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
749190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Get the transform for the running task
750003eda6c230a1b1c4e3709466c46660e7602d740Winson        stackView.updateLayoutAlgorithm(true /* boundScroll */);
751619e40cd56266a362ab7da80cb9e4eba6c33b204Winson        stackView.updateToInitialState(true /* scrollToInitialState */);
7527845e8c4946f6b5dadfcd9c1d64e826bacc50edbWinson        stackView.getStackAlgorithm().getStackTransformScreenCoordinates(launchTask,
753190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                stackView.getScroller().getStackScroll(), mTmpTransform, null);
754190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return mTmpTransform;
755190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
756190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
757190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
758190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Draws the header of a task used for the window animation into a bitmap.
759190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
760aeb298c4db913b376fb6053ec30c132a93e04635Winson    private Bitmap drawThumbnailTransitionBitmap(Task toTask, TaskViewTransform toTransform,
761aeb298c4db913b376fb6053ec30c132a93e04635Winson            Bitmap thumbnail) {
7628be1634a543022bd6ee0c1e974d3201d452981e8Winson        SystemServicesProxy ssp = Recents.getSystemServices();
763190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (toTransform != null && toTask.key != null) {
764190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            synchronized (mHeaderBarLock) {
7658be1634a543022bd6ee0c1e974d3201d452981e8Winson                boolean disabledInSafeMode = !toTask.isSystemApp && ssp.isInSafeMode();
766509d0d0c9e2ee165d04e898fea59f8941ac7138dWinson Chung                mHeaderBar.onTaskViewSizeChanged((int) toTransform.rect.width(),
767509d0d0c9e2ee165d04e898fea59f8941ac7138dWinson Chung                        (int) toTransform.rect.height());
768c742f973b1e506732911c156c5869fd377afc5bfWinson                if (RecentsDebugFlags.Static.EnableTransitionThumbnailDebugMode) {
769190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    thumbnail.eraseColor(0xFFff0000);
770190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                } else {
771aeb298c4db913b376fb6053ec30c132a93e04635Winson                    thumbnail.eraseColor(0);
772190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    Canvas c = new Canvas(thumbnail);
77322574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    // Workaround for b/27815919, reset the callback so that we do not trigger an
77422574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    // invalidate on the header bar as a result of updating the icon
77522574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    Drawable icon = mHeaderBar.getIconView().getDrawable();
77622574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    if (icon != null) {
77722574afa00b7e32b7c4c5a1b98deebd67751b364Winson                        icon.setCallback(null);
77822574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    }
7798be1634a543022bd6ee0c1e974d3201d452981e8Winson                    mHeaderBar.rebindToTask(toTask, false /* touchExplorationEnabled */,
7808be1634a543022bd6ee0c1e974d3201d452981e8Winson                            disabledInSafeMode);
781e693aafe0511c2a7ffc571b22abeefba44046225Winson                    mHeaderBar.setDimAlpha(toTransform.dimAlpha);
782190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    mHeaderBar.draw(c);
783190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    c.setBitmap(null);
784190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
785190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
786190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            return thumbnail.createAshmemBitmap();
787190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
788190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return null;
789190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
790190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
791190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
792190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Shows the recents activity
793190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
7944bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected void startRecentsActivity(ActivityManager.RunningTaskInfo topTask,
795bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi            boolean isTopTaskHome, boolean animate) {
796e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        RecentsTaskLoader loader = Recents.getTaskLoader();
797c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
798190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
799e5f1faa9f8009a723ab21aed6fe5ab325c61442bWinson        // In the case where alt-tab is triggered, we never get a preloadRecents() call, so we
800435b2e43cb131d0b38ba19f335aec78bb83944b2Jorim Jaggi        // should always preload the tasks now. If we are dragging in recents, reload them as
801435b2e43cb131d0b38ba19f335aec78bb83944b2Jorim Jaggi        // the stacks might have changed.
802e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi        if (mLaunchedWhileDocking || mTriggeredFromAltTab ||sInstanceLoadPlan == null) {
803e5f1faa9f8009a723ab21aed6fe5ab325c61442bWinson            // Create a new load plan if preloadRecents() was never triggered
804190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            sInstanceLoadPlan = loader.createLoadPlan(mContext);
805190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
806e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi        if (mLaunchedWhileDocking || mTriggeredFromAltTab || !sInstanceLoadPlan.hasTasks()) {
80765c851e6e9e08656744b6f66d3da188e3283b17dWinson            loader.preloadTasks(sInstanceLoadPlan, topTask.id, isTopTaskHome);
808190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
809c5b12dd37e964b220c0a108e86ad8a2d12805747Winson
810190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        TaskStack stack = sInstanceLoadPlan.getTaskStack();
811c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        boolean hasRecentTasks = stack.getTaskCount() > 0;
812c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        boolean useThumbnailTransition = (topTask != null) && !isTopTaskHome && hasRecentTasks;
813190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
814aeb298c4db913b376fb6053ec30c132a93e04635Winson        // Update the launch state that we need in updateHeaderBarLayout()
815aeb298c4db913b376fb6053ec30c132a93e04635Winson        launchState.launchedFromHome = !useThumbnailTransition;
816aeb298c4db913b376fb6053ec30c132a93e04635Winson        launchState.launchedFromApp = useThumbnailTransition || mLaunchedWhileDocking;
817aeb298c4db913b376fb6053ec30c132a93e04635Winson        launchState.launchedViaDockGesture = mLaunchedWhileDocking;
818aeb298c4db913b376fb6053ec30c132a93e04635Winson        launchState.launchedViaDragGesture = mDraggingInRecents;
819aeb298c4db913b376fb6053ec30c132a93e04635Winson        launchState.launchedToTaskId = (topTask != null) ? topTask.id : -1;
820aeb298c4db913b376fb6053ec30c132a93e04635Winson        launchState.launchedWithAltTab = mTriggeredFromAltTab;
821aeb298c4db913b376fb6053ec30c132a93e04635Winson
822aeb298c4db913b376fb6053ec30c132a93e04635Winson        // Preload the icon (this will be a null-op if we have preloaded the icon already in
823aeb298c4db913b376fb6053ec30c132a93e04635Winson        // preloadRecents())
824aeb298c4db913b376fb6053ec30c132a93e04635Winson        preloadIcon(topTask);
825aeb298c4db913b376fb6053ec30c132a93e04635Winson
826f0d1c44a59a10707baa0cca8dd377302260710c1Winson        // Update the header bar if necessary
827008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson        updateHeaderBarLayout(stack);
828f0d1c44a59a10707baa0cca8dd377302260710c1Winson
829190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Prepare the dummy stack for the transition
83036a5a2c7003ef8157f276b411c3fda47ad2f75e3Winson        TaskStackLayoutAlgorithm.VisibilityReport stackVr =
831190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                mDummyStackView.computeStackVisibilityReport();
832bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi
833aeb298c4db913b376fb6053ec30c132a93e04635Winson        // Update the remaining launch state
834c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedNumVisibleTasks = stackVr.numVisibleTasks;
835c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedNumVisibleThumbnails = stackVr.numVisibleThumbnails;
836c5b12dd37e964b220c0a108e86ad8a2d12805747Winson
837bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi        if (!animate) {
838c5b12dd37e964b220c0a108e86ad8a2d12805747Winson            startRecentsActivity(ActivityOptions.makeCustomAnimation(mContext, -1, -1));
839bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi            return;
840bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi        }
841bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi
842aeb298c4db913b376fb6053ec30c132a93e04635Winson        ActivityOptions opts;
843190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (useThumbnailTransition) {
844190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            // Try starting with a thumbnail transition
845aeb298c4db913b376fb6053ec30c132a93e04635Winson            opts = getThumbnailTransitionActivityOptions(topTask, mDummyStackView);
846aeb298c4db913b376fb6053ec30c132a93e04635Winson        } else {
847fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan            // If there is no thumbnail transition, but is launching from home into recents, then
848008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson            // use a quick home transition
849aeb298c4db913b376fb6053ec30c132a93e04635Winson            opts = hasRecentTasks
850aeb298c4db913b376fb6053ec30c132a93e04635Winson                ? getHomeTransitionActivityOptions()
851aeb298c4db913b376fb6053ec30c132a93e04635Winson                : getUnknownTransitionActivityOptions();
852fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan        }
853aeb298c4db913b376fb6053ec30c132a93e04635Winson        startRecentsActivity(opts);
854fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan        mLastToggleTime = SystemClock.elapsedRealtime();
855fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan    }
856fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan
857fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan    /**
858190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Starts the recents activity.
859190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
860c5b12dd37e964b220c0a108e86ad8a2d12805747Winson    private void startRecentsActivity(ActivityOptions opts) {
861190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        Intent intent = new Intent();
8624bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan        intent.setClassName(RECENTS_PACKAGE, RECENTS_ACTIVITY);
863190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
864190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
865190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
866b58c46acec2e270f24ca587a64298f06cefb09cbSid Soundararajan
867190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (opts != null) {
868190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            mContext.startActivityAsUser(intent, opts.toBundle(), UserHandle.CURRENT);
869190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        } else {
870190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            mContext.startActivityAsUser(intent, UserHandle.CURRENT);
871190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
872cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggi        EventBus.getDefault().send(new RecentsActivityStartingEvent());
873190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
874190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
8753fb67562c548662910160d5672b1b9558bd6571cWinson    /**** OnAnimationFinishedListener Implementation ****/
8761a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski
8771a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski    @Override
8781a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski    public void onAnimationFinished() {
8791a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski        EventBus.getDefault().post(new EnterRecentsWindowLastAnimationFrameEvent());
8801a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski    }
881190fe3bf88388fcb109af64571e3baa0d01f1c37Winson}
882