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;
41db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggiimport android.view.WindowManager;
42c0d7058b14c24cd07912f5629c26b39b7b4673d5Winson
43190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.internal.logging.MetricsLogger;
44db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggiimport com.android.internal.policy.DockedDividerUtils;
45190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.R;
46190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.SystemUIApplication;
47412e18058dc2cd5779d2451fce7fd74631f9e237Winsonimport com.android.systemui.recents.events.EventBus;
48899327f5cbbfb0eae5562b262ccea860c98f6bc4Jorim Jaggiimport com.android.systemui.recents.events.activity.DockedTopTaskEvent;
491b58561f66a9453754b1b419296899fdc08b4c93Winsonimport com.android.systemui.recents.events.activity.EnterRecentsWindowLastAnimationFrameEvent;
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;
73db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggiimport com.android.systemui.stackdivider.DividerView;
74d8b1d63f96580fe961e1751e7b4f56c90c1e0a76Winsonimport com.android.systemui.statusbar.BaseStatusBar;
75cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggiimport com.android.systemui.statusbar.phone.NavigationBarGestureHelper;
76190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport com.android.systemui.statusbar.phone.PhoneStatusBar;
77190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
78190fe3bf88388fcb109af64571e3baa0d01f1c37Winsonimport java.util.ArrayList;
79190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
80190fe3bf88388fcb109af64571e3baa0d01f1c37Winson/**
81190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * An implementation of the Recents component for the current user.  For secondary users, this can
82190fe3bf88388fcb109af64571e3baa0d01f1c37Winson * be called remotely from the system user.
83190fe3bf88388fcb109af64571e3baa0d01f1c37Winson */
84cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggipublic class RecentsImpl implements ActivityOptions.OnAnimationFinishedListener {
85190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
86190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    private final static String TAG = "RecentsImpl";
87b61e654b9a46e79827355104760a3ac5228380bcWinson
886b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    // The minimum amount of time between each recents button press that we will handle
896b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    private final static int MIN_TOGGLE_DELAY_MS = 350;
90b61e654b9a46e79827355104760a3ac5228380bcWinson
916b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    // The duration within which the user releasing the alt tab (from when they pressed alt tab)
926b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    // that the fast alt-tab animation will run.  If the user's alt-tab takes longer than this
936b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    // duration, then we will toggle recents after this duration.
946b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    private final static int FAST_ALT_TAB_DELAY_MS = 225;
95190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
96190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public final static String RECENTS_PACKAGE = "com.android.systemui";
97190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public final static String RECENTS_ACTIVITY = "com.android.systemui.recents.RecentsActivity";
98e693aafe0511c2a7ffc571b22abeefba44046225Winson
99190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
100938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kim     * An implementation of TaskStackListener, that allows us to listen for changes to the system
101190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * task stacks and update recents accordingly.
102190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
103938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kim    class TaskStackListenerImpl extends TaskStackListener {
104190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        @Override
105190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        public void onTaskStackChanged() {
106938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kim            // Preloads the next task
107a0fdeec66caa4d70786c7f6f31f79a24eef9da10Jorim Jaggi            RecentsConfiguration config = Recents.getConfiguration();
108190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            if (config.svelteLevel == RecentsConfiguration.SVELTE_NONE) {
109e7f138c7f0a190c86cec10fb32fa106aacae4093Winson                RecentsTaskLoader loader = Recents.getTaskLoader();
110e7f138c7f0a190c86cec10fb32fa106aacae4093Winson                SystemServicesProxy ssp = Recents.getSystemServices();
111d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson                ActivityManager.RunningTaskInfo runningTaskInfo = ssp.getRunningTask();
112190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
113190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // Load the next task only if we aren't svelte
114190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
115dfd7be012b889f92d3e7e6d80ff7fe6b55c75901Winson                loader.preloadTasks(plan, -1, false /* includeFrontMostExcludedTask */);
116190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
117190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // This callback is made when a new activity is launched and the old one is paused
118190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // so ignore the current activity and try and preload the thumbnail for the
119190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // previous one.
120190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                if (runningTaskInfo != null) {
121190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    launchOpts.runningTaskId = runningTaskInfo.id;
122190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
123190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                launchOpts.numVisibleTasks = 2;
124190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                launchOpts.numVisibleTaskThumbnails = 2;
125190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                launchOpts.onlyLoadForCache = true;
126190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                launchOpts.onlyLoadPausedActivities = true;
127190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                loader.loadTasks(mContext, plan, launchOpts);
128190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
129190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
130190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
131190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
1324bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected static RecentsTaskLoadPlan sInstanceLoadPlan;
133190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
1344bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected Context mContext;
1354bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected Handler mHandler;
136190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    TaskStackListenerImpl mTaskStackListener;
137dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    boolean mDraggingInRecents;
138e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi    boolean mLaunchedWhileDocking;
139190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
140190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    // Task launching
141190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    Rect mTaskStackBounds = new Rect();
142190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    TaskViewTransform mTmpTransform = new TaskViewTransform();
143190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    int mStatusBarHeight;
144190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    int mNavBarHeight;
145190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    int mNavBarWidth;
146190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    int mTaskBarHeight;
147190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
148190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    // Header (for transition)
149190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    TaskViewHeader mHeaderBar;
150190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    final Object mHeaderBarLock = new Object();
1514bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected TaskStackView mDummyStackView;
152190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
153190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    // Variables to keep track of if we need to start recents after binding
1544bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected boolean mTriggeredFromAltTab;
1554bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected long mLastToggleTime;
1566b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    DozeTrigger mFastAltTabTrigger = new DozeTrigger(FAST_ALT_TAB_DELAY_MS, new Runnable() {
1576b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        @Override
1586b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        public void run() {
1596b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            // When this fires, then the user has not released alt-tab for at least
1606b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            // FAST_ALT_TAB_DELAY_MS milliseconds
161435b2e43cb131d0b38ba19f335aec78bb83944b2Jorim Jaggi            showRecents(mTriggeredFromAltTab, false /* draggingInRecents */, true /* animate */,
162db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                    false /* reloadTasks */, false /* fromHome */,
163db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                    DividerView.INVALID_RECENTS_GROW_TARGET);
1646b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
1656b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    });
166190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
167aeb298c4db913b376fb6053ec30c132a93e04635Winson    protected Bitmap mThumbTransitionBitmapCache;
168190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
169190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public RecentsImpl(Context context) {
170190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        mContext = context;
171190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        mHandler = new Handler();
172190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
173ab84fc56114c0963c6f701de9725f5413ab76da9Winson        // Initialize the static foreground thread
174ab84fc56114c0963c6f701de9725f5413ab76da9Winson        ForegroundThread.get();
175ab84fc56114c0963c6f701de9725f5413ab76da9Winson
176190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Register the task stack listener
177938a50b424eb73ee96d34c96b5e2cbd11f733d7aJaewan Kim        mTaskStackListener = new TaskStackListenerImpl();
178e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        SystemServicesProxy ssp = Recents.getSystemServices();
179e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        ssp.registerTaskStackListener(mTaskStackListener);
180190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
181190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Initialize the static configuration resources
182670ea71f1b4ae59a0cd6608ae44b0fb78a1144c4Winson        LayoutInflater inflater = LayoutInflater.from(mContext);
183670ea71f1b4ae59a0cd6608ae44b0fb78a1144c4Winson        mDummyStackView = new TaskStackView(mContext);
184670ea71f1b4ae59a0cd6608ae44b0fb78a1144c4Winson        mHeaderBar = (TaskViewHeader) inflater.inflate(R.layout.recents_task_view_header,
185670ea71f1b4ae59a0cd6608ae44b0fb78a1144c4Winson                null, false);
186670ea71f1b4ae59a0cd6608ae44b0fb78a1144c4Winson        reloadResources();
18716ef39a7f6f000af761cf2c2a3e2823a0fbd0c4cWinson    }
188190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
18916ef39a7f6f000af761cf2c2a3e2823a0fbd0c4cWinson    public void onBootCompleted() {
190190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // When we start, preload the data associated with the previous recent tasks.
191190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // We can use a new plan since the caches will be the same.
192e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        RecentsTaskLoader loader = Recents.getTaskLoader();
193190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
194dfd7be012b889f92d3e7e6d80ff7fe6b55c75901Winson        loader.preloadTasks(plan, -1, false /* includeFrontMostExcludedTask */);
195190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
196296278a0679375b8c43962a9e3c9bb4e8ab201e7Winson Chung        launchOpts.numVisibleTasks = loader.getIconCacheSize();
197190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.numVisibleTaskThumbnails = loader.getThumbnailCacheSize();
198190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.onlyLoadForCache = true;
199190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        loader.loadTasks(mContext, plan, launchOpts);
200190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
201190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
202190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void onConfigurationChanged() {
203670ea71f1b4ae59a0cd6608ae44b0fb78a1144c4Winson        reloadResources();
204670ea71f1b4ae59a0cd6608ae44b0fb78a1144c4Winson        mDummyStackView.reloadOnConfigurationChange();
205670ea71f1b4ae59a0cd6608ae44b0fb78a1144c4Winson        mHeaderBar.onConfigurationChanged();
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     */
2260f051f5abdb31024d862a954cc4c4716b1dde616Andrii Kulian    public void onStartScreenPinning(Context context, int taskId) {
227190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        SystemUIApplication app = (SystemUIApplication) context;
228190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        PhoneStatusBar statusBar = app.getComponent(PhoneStatusBar.class);
229190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (statusBar != null) {
2300f051f5abdb31024d862a954cc4c4716b1dde616Andrii Kulian            statusBar.showScreenPinningRequest(taskId, false);
231190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
232190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
233190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
234bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi    public void showRecents(boolean triggeredFromAltTab, boolean draggingInRecents,
235db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi            boolean animate, boolean launchedWhileDockingTask, boolean fromHome,
236db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi            int growTarget) {
237190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        mTriggeredFromAltTab = triggeredFromAltTab;
238dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi        mDraggingInRecents = draggingInRecents;
239e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi        mLaunchedWhileDocking = launchedWhileDockingTask;
240e693aafe0511c2a7ffc571b22abeefba44046225Winson        if (mFastAltTabTrigger.isAsleep()) {
241e693aafe0511c2a7ffc571b22abeefba44046225Winson            // Fast alt-tab duration has elapsed, fall through to showing Recents and reset
242e693aafe0511c2a7ffc571b22abeefba44046225Winson            mFastAltTabTrigger.stopDozing();
2436b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        } else if (mFastAltTabTrigger.isDozing()) {
244e693aafe0511c2a7ffc571b22abeefba44046225Winson            // Fast alt-tab duration has not elapsed.  If this is triggered by a different
245e693aafe0511c2a7ffc571b22abeefba44046225Winson            // showRecents() call, then ignore that call for now.
246e693aafe0511c2a7ffc571b22abeefba44046225Winson            // TODO: We can not handle quick tabs that happen between the initial showRecents() call
247e693aafe0511c2a7ffc571b22abeefba44046225Winson            //       that started the activity and the activity starting up.  The severity of this
248e693aafe0511c2a7ffc571b22abeefba44046225Winson            //       is inversely proportional to the FAST_ALT_TAB_DELAY_MS duration though.
2496b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            if (!triggeredFromAltTab) {
2506b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                return;
2516b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            }
2526b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            mFastAltTabTrigger.stopDozing();
253e693aafe0511c2a7ffc571b22abeefba44046225Winson        } else if (triggeredFromAltTab) {
254e693aafe0511c2a7ffc571b22abeefba44046225Winson            // The fast alt-tab detector is not yet running, so start the trigger and wait for the
255e693aafe0511c2a7ffc571b22abeefba44046225Winson            // hideRecents() call, or for the fast alt-tab duration to elapse
256e693aafe0511c2a7ffc571b22abeefba44046225Winson            mFastAltTabTrigger.startDozing();
257e693aafe0511c2a7ffc571b22abeefba44046225Winson            return;
2586b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
259190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
260190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        try {
261190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            // Check if the top task is in the home stack, and start the recents activity
262e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            SystemServicesProxy ssp = Recents.getSystemServices();
263d7cb517b4ce10fa98213da18ccdb2261fb797f7cWinson            boolean forceVisible = launchedWhileDockingTask || draggingInRecents;
264d7cb517b4ce10fa98213da18ccdb2261fb797f7cWinson            MutableBoolean isHomeStackVisible = new MutableBoolean(forceVisible);
265d7cb517b4ce10fa98213da18ccdb2261fb797f7cWinson            if (forceVisible || !ssp.isRecentsActivityVisible(isHomeStackVisible)) {
266d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson                ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
267d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson                startRecentsActivity(runningTask, isHomeStackVisible.value || fromHome, animate,
268d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson                        growTarget);
269190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
270190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        } catch (ActivityNotFoundException e) {
2711b58561f66a9453754b1b419296899fdc08b4c93Winson            Log.e(TAG, "Failed to launch RecentsActivity", e);
272190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
273190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
274190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
275190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void hideRecents(boolean triggeredFromAltTab, boolean triggeredFromHomeKey) {
2762799eca21715390faf10596884b65165528f89c4Winson        if (triggeredFromAltTab && mFastAltTabTrigger.isDozing()) {
2772799eca21715390faf10596884b65165528f89c4Winson            // The user has released alt-tab before the trigger has run, so just show the next
2782799eca21715390faf10596884b65165528f89c4Winson            // task immediately
2792799eca21715390faf10596884b65165528f89c4Winson            showNextTask();
2806b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
2812799eca21715390faf10596884b65165528f89c4Winson            // Cancel the fast alt-tab trigger
2822799eca21715390faf10596884b65165528f89c4Winson            mFastAltTabTrigger.stopDozing();
2832799eca21715390faf10596884b65165528f89c4Winson            return;
284190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
2852799eca21715390faf10596884b65165528f89c4Winson
2862799eca21715390faf10596884b65165528f89c4Winson        // Defer to the activity to handle hiding recents, if it handles it, then it must still
2872799eca21715390faf10596884b65165528f89c4Winson        // be visible
2882799eca21715390faf10596884b65165528f89c4Winson        EventBus.getDefault().post(new HideRecentsEvent(triggeredFromAltTab,
2892799eca21715390faf10596884b65165528f89c4Winson                triggeredFromHomeKey));
290190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
291190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
292db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi    public void toggleRecents(int growTarget) {
2936b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Skip this toggle if we are already waiting to trigger recents via alt-tab
2946b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        if (mFastAltTabTrigger.isDozing()) {
2956b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            return;
2966b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
2976b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
298dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi        mDraggingInRecents = false;
299e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi        mLaunchedWhileDocking = false;
300190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        mTriggeredFromAltTab = false;
301190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
302190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        try {
303e7f138c7f0a190c86cec10fb32fa106aacae4093Winson            SystemServicesProxy ssp = Recents.getSystemServices();
304d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson            MutableBoolean isHomeStackVisible = new MutableBoolean(true);
305b61e654b9a46e79827355104760a3ac5228380bcWinson            long elapsedTime = SystemClock.elapsedRealtime() - mLastToggleTime;
306b61e654b9a46e79827355104760a3ac5228380bcWinson
307d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson            if (ssp.isRecentsActivityVisible(isHomeStackVisible)) {
308e693aafe0511c2a7ffc571b22abeefba44046225Winson                RecentsDebugFlags debugFlags = Recents.getDebugFlags();
3095da4347b41b105dc6c6c01bf2810af3ce3013229Winson                RecentsConfiguration config = Recents.getConfiguration();
3105da4347b41b105dc6c6c01bf2810af3ce3013229Winson                RecentsActivityLaunchState launchState = config.getLaunchState();
311ead5c0fc20e5c60a68cd017af0b8eacaf1167a1cWinson Chung                if (!launchState.launchedWithAltTab) {
312b61e654b9a46e79827355104760a3ac5228380bcWinson                    // If the user taps quickly
313e693aafe0511c2a7ffc571b22abeefba44046225Winson                    if (!debugFlags.isPagingEnabled() ||
314e693aafe0511c2a7ffc571b22abeefba44046225Winson                            (ViewConfiguration.getDoubleTapMinTime() < elapsedTime &&
315e693aafe0511c2a7ffc571b22abeefba44046225Winson                                    elapsedTime < ViewConfiguration.getDoubleTapTimeout())) {
316b61e654b9a46e79827355104760a3ac5228380bcWinson                        // Launch the next focused task
317b61e654b9a46e79827355104760a3ac5228380bcWinson                        EventBus.getDefault().post(new LaunchNextTaskRequestEvent());
318b61e654b9a46e79827355104760a3ac5228380bcWinson                    } else {
319b61e654b9a46e79827355104760a3ac5228380bcWinson                        // Notify recents to move onto the next task
320b61e654b9a46e79827355104760a3ac5228380bcWinson                        EventBus.getDefault().post(new IterateRecentsEvent());
321b61e654b9a46e79827355104760a3ac5228380bcWinson                    }
3220d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                } else {
3230d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    // If the user has toggled it too quickly, then just eat up the event here (it's
3240d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    // better than showing a janky screenshot).
3250d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    // NOTE: Ideally, the screenshot mechanism would take the window transform into
3260d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    // account
327b61e654b9a46e79827355104760a3ac5228380bcWinson                    if (elapsedTime < MIN_TOGGLE_DELAY_MS) {
3280d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                        return;
3290d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    }
3300d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson
3310d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    EventBus.getDefault().post(new ToggleRecentsEvent());
3320d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    mLastToggleTime = SystemClock.elapsedRealtime();
3330d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                }
334190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                return;
335190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            } else {
3360d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                // If the user has toggled it too quickly, then just eat up the event here (it's
3370d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                // better than showing a janky screenshot).
3380d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                // NOTE: Ideally, the screenshot mechanism would take the window transform into
3390d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                // account
340b61e654b9a46e79827355104760a3ac5228380bcWinson                if (elapsedTime < MIN_TOGGLE_DELAY_MS) {
3410d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                    return;
3420d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                }
3430d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson
344190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                // Otherwise, start the recents activity
345d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson                ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
346d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson                startRecentsActivity(runningTask, isHomeStackVisible.value, true /* animate */,
347d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson                        growTarget);
348d8b1d63f96580fe961e1751e7b4f56c90c1e0a76Winson
349d8b1d63f96580fe961e1751e7b4f56c90c1e0a76Winson                // Only close the other system windows if we are actually showing recents
350d8b1d63f96580fe961e1751e7b4f56c90c1e0a76Winson                ssp.sendCloseSystemWindows(BaseStatusBar.SYSTEM_DIALOG_REASON_RECENT_APPS);
3510d14d4da91c3d8b1221269712d5abf43a7cf9f31Winson                mLastToggleTime = SystemClock.elapsedRealtime();
352190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
353190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        } catch (ActivityNotFoundException e) {
3541b58561f66a9453754b1b419296899fdc08b4c93Winson            Log.e(TAG, "Failed to launch RecentsActivity", e);
355190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
356190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
357190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
358190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void preloadRecents() {
359190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Preload only the raw task list into a new load plan (which will be consumed by the
360190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // RecentsActivity) only if there is a task to animate to.
361e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        SystemServicesProxy ssp = Recents.getSystemServices();
362d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson        MutableBoolean isHomeStackVisible = new MutableBoolean(true);
363d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson        if (!ssp.isRecentsActivityVisible(isHomeStackVisible)) {
364d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson            ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
3655b4e0d2b11b2b3579bac36fde5ac3805346618ccWinson            RecentsTaskLoader loader = Recents.getTaskLoader();
3665b4e0d2b11b2b3579bac36fde5ac3805346618ccWinson            sInstanceLoadPlan = loader.createLoadPlan(mContext);
367dfd7be012b889f92d3e7e6d80ff7fe6b55c75901Winson            sInstanceLoadPlan.preloadRawTasks(!isHomeStackVisible.value);
368dfd7be012b889f92d3e7e6d80ff7fe6b55c75901Winson            loader.preloadTasks(sInstanceLoadPlan, runningTask.id, !isHomeStackVisible.value);
369190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            TaskStack stack = sInstanceLoadPlan.getTaskStack();
3704b057c6787624b75613769a857ccdf51114bb7f2Winson            if (stack.getTaskCount() > 0) {
371aeb298c4db913b376fb6053ec30c132a93e04635Winson                // Only preload the icon (but not the thumbnail since it may not have been taken for
372aeb298c4db913b376fb6053ec30c132a93e04635Winson                // the pausing activity)
373c4a038ad28009eb5840cf9f6aefbd2c873cee02eWinson                preloadIcon(runningTask.id);
374aeb298c4db913b376fb6053ec30c132a93e04635Winson
375aeb298c4db913b376fb6053ec30c132a93e04635Winson                // At this point, we don't know anything about the stack state.  So only calculate
376aeb298c4db913b376fb6053ec30c132a93e04635Winson                // the dimensions of the thumbnail that we need for the transition into Recents, but
377aeb298c4db913b376fb6053ec30c132a93e04635Winson                // do not draw it until we construct the activity options when we start Recents
378db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                updateHeaderBarLayout(stack, null /* window rect override*/);
379190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
380190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
381190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
382190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
383190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void cancelPreloadingRecents() {
384190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Do nothing
385190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
386190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
387dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    public void onDraggingInRecents(float distanceFromTop) {
388dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi        EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEvent(distanceFromTop));
389dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    }
390dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi
391dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    public void onDraggingInRecentsEnded(float velocity) {
392dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi        EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEndedEvent(velocity));
393dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi    }
394dd98d41e3a65b3bcb37007ea5b29371cf013f563Jorim Jaggi
3956b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    /**
3966b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson     * Transitions to the next recent task in the stack.
3976b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson     */
3986b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    public void showNextTask() {
3996b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        SystemServicesProxy ssp = Recents.getSystemServices();
4006b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        RecentsTaskLoader loader = Recents.getTaskLoader();
4016b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
402dfd7be012b889f92d3e7e6d80ff7fe6b55c75901Winson        loader.preloadTasks(plan, -1, false /* includeFrontMostExcludedTask */);
4036b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        TaskStack focusedStack = plan.getTaskStack();
4046b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4056b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Return early if there are no tasks in the focused stack
4064b057c6787624b75613769a857ccdf51114bb7f2Winson        if (focusedStack == null || focusedStack.getTaskCount() == 0) return;
4076b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4086b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Return early if there is no running task
409d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson        ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
4106b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        if (runningTask == null) return;
4116b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4126b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Find the task in the recents list
413d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson        boolean isRunningTaskInHomeStack = SystemServicesProxy.isHomeStack(runningTask.stackId);
414250608a5cd08862f4752a924d51710805850db8aWinson        ArrayList<Task> tasks = focusedStack.getStackTasks();
4156b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        Task toTask = null;
4166b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        ActivityOptions launchOpts = null;
4176b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        int taskCount = tasks.size();
4186b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        for (int i = taskCount - 1; i >= 1; i--) {
4196b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            Task task = tasks.get(i);
420d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson            if (isRunningTaskInHomeStack) {
421e86deb8adb022ba7721a1af1112d3a45e757095cWinson                toTask = tasks.get(i - 1);
422e86deb8adb022ba7721a1af1112d3a45e757095cWinson                launchOpts = ActivityOptions.makeCustomAnimation(mContext,
423e86deb8adb022ba7721a1af1112d3a45e757095cWinson                        R.anim.recents_launch_next_affiliated_task_target,
424e86deb8adb022ba7721a1af1112d3a45e757095cWinson                        R.anim.recents_fast_toggle_app_home_exit);
425e86deb8adb022ba7721a1af1112d3a45e757095cWinson                break;
426e86deb8adb022ba7721a1af1112d3a45e757095cWinson            } else if (task.key.id == runningTask.id) {
4276b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                toTask = tasks.get(i - 1);
4286b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                launchOpts = ActivityOptions.makeCustomAnimation(mContext,
4296b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                        R.anim.recents_launch_prev_affiliated_task_target,
4306b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                        R.anim.recents_launch_prev_affiliated_task_source);
4316b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                break;
4326b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            }
4336b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
4346b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4356b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Return early if there is no next task
4366b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        if (toTask == null) {
4376b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            ssp.startInPlaceAnimationOnFrontMostApplication(
4386b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                    ActivityOptions.makeCustomInPlaceAnimation(mContext,
4396b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson                            R.anim.recents_launch_prev_affiliated_task_bounce));
4406b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson            return;
4416b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        }
4426b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4436b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson        // Launch the task
44464ae08aa16fdf7ee33073979400afa40692342f1Wale Ogunwale        ssp.startActivityFromRecents(mContext, toTask.key, toTask.title, launchOpts);
4456b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    }
4466b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson
4476b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson    /**
4486b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson     * Transitions to the next affiliated task.
4496b92c6e524dbb7598fbeb4aefd98b69e6c13c03aWinson     */
450190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void showRelativeAffiliatedTask(boolean showNextTask) {
451e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        SystemServicesProxy ssp = Recents.getSystemServices();
452e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        RecentsTaskLoader loader = Recents.getTaskLoader();
453190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
454dfd7be012b889f92d3e7e6d80ff7fe6b55c75901Winson        loader.preloadTasks(plan, -1, false /* includeFrontMostExcludedTask */);
455190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        TaskStack focusedStack = plan.getTaskStack();
456190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
457190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Return early if there are no tasks in the focused stack
4584b057c6787624b75613769a857ccdf51114bb7f2Winson        if (focusedStack == null || focusedStack.getTaskCount() == 0) return;
459190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
460190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Return early if there is no running task (can't determine affiliated tasks in this case)
461d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson        ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
462190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (runningTask == null) return;
463190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Return early if the running task is in the home stack (optimization)
4645510f6c1b9c20483e1507147eed7b24ac8bb6363Winson        if (SystemServicesProxy.isHomeStack(runningTask.stackId)) return;
465190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
466190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Find the task in the recents list
467250608a5cd08862f4752a924d51710805850db8aWinson        ArrayList<Task> tasks = focusedStack.getStackTasks();
468190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        Task toTask = null;
469190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        ActivityOptions launchOpts = null;
470190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        int taskCount = tasks.size();
471190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        int numAffiliatedTasks = 0;
472190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        for (int i = 0; i < taskCount; i++) {
473190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            Task task = tasks.get(i);
474190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            if (task.key.id == runningTask.id) {
475190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                TaskGrouping group = task.group;
476190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                Task.TaskKey toTaskKey;
477190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                if (showNextTask) {
478190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    toTaskKey = group.getNextTaskInGroup(task);
479190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    launchOpts = ActivityOptions.makeCustomAnimation(mContext,
480190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            R.anim.recents_launch_next_affiliated_task_target,
481190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            R.anim.recents_launch_next_affiliated_task_source);
482190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                } else {
483190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    toTaskKey = group.getPrevTaskInGroup(task);
484190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    launchOpts = ActivityOptions.makeCustomAnimation(mContext,
485190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            R.anim.recents_launch_prev_affiliated_task_target,
486190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            R.anim.recents_launch_prev_affiliated_task_source);
487190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
488190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                if (toTaskKey != null) {
489190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    toTask = focusedStack.findTaskWithId(toTaskKey.id);
490190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
491190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                numAffiliatedTasks = group.getTaskCount();
492190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                break;
493190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
494190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
495190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
496190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Return early if there is no next task
497190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (toTask == null) {
498190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            if (numAffiliatedTasks > 1) {
499190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                if (showNextTask) {
500e7f138c7f0a190c86cec10fb32fa106aacae4093Winson                    ssp.startInPlaceAnimationOnFrontMostApplication(
501190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            ActivityOptions.makeCustomInPlaceAnimation(mContext,
502190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                                    R.anim.recents_launch_next_affiliated_task_bounce));
503190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                } else {
504e7f138c7f0a190c86cec10fb32fa106aacae4093Winson                    ssp.startInPlaceAnimationOnFrontMostApplication(
505190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                            ActivityOptions.makeCustomInPlaceAnimation(mContext,
506190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                                    R.anim.recents_launch_prev_affiliated_task_bounce));
507190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
508190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
509190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            return;
510190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
511190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
512190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Keep track of actually launched affiliated tasks
513190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        MetricsLogger.count(mContext, "overview_affiliated_task_launch", 1);
514190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
515190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Launch the task
51664ae08aa16fdf7ee33073979400afa40692342f1Wale Ogunwale        ssp.startActivityFromRecents(mContext, toTask.key, toTask.title, launchOpts);
517190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
518190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
519190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void showNextAffiliatedTask() {
520190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Keep track of when the affiliated task is triggered
521190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        MetricsLogger.count(mContext, "overview_affiliated_task_next", 1);
522190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        showRelativeAffiliatedTask(true);
523190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
524190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
525190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public void showPrevAffiliatedTask() {
526190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Keep track of when the affiliated task is triggered
527190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        MetricsLogger.count(mContext, "overview_affiliated_task_prev", 1);
528190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        showRelativeAffiliatedTask(false);
529190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
530190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
531cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggi    public void dockTopTask(int topTaskId, int dragMode,
532cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggi            int stackCreateMode, Rect initialBounds) {
53375b2597e5826662c1740d6f3b81966a9e6a9e7b4Jorim Jaggi        SystemServicesProxy ssp = Recents.getSystemServices();
5349511b0f1e9ac629a4a747a0c9373d33ab33cfc32Jorim Jaggi
5359511b0f1e9ac629a4a747a0c9373d33ab33cfc32Jorim Jaggi        // Make sure we inform DividerView before we actually start the activity so we can change
5369511b0f1e9ac629a4a747a0c9373d33ab33cfc32Jorim Jaggi        // the resize mode already.
537e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang        if (ssp.moveTaskToDockedStack(topTaskId, stackCreateMode, initialBounds)) {
538899327f5cbbfb0eae5562b262ccea860c98f6bc4Jorim Jaggi            EventBus.getDefault().send(new DockedTopTaskEvent(dragMode, initialBounds));
539e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang            showRecents(
540e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang                    false /* triggeredFromAltTab */,
541e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang                    dragMode == NavigationBarGestureHelper.DRAG_MODE_RECENTS,
542e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang                    false /* animate */,
543681fc7b2670542aae0f3b9ef8f6c7a88db984ea9Jorim Jaggi                    true /* launchedWhileDockingTask*/,
544db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                    false /* fromHome */,
545db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                    DividerView.INVALID_RECENTS_GROW_TARGET);
546e4fbd328f83ea0ee086731f852cd6c56a3deb733Chong Zhang        }
54775b2597e5826662c1740d6f3b81966a9e6a9e7b4Jorim Jaggi    }
54875b2597e5826662c1740d6f3b81966a9e6a9e7b4Jorim Jaggi
549190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
550190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Returns the preloaded load plan and invalidates it.
551190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
552190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    public static RecentsTaskLoadPlan consumeInstanceLoadPlan() {
553190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan plan = sInstanceLoadPlan;
554190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        sInstanceLoadPlan = null;
555190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return plan;
556190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
557190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
558190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
559670ea71f1b4ae59a0cd6608ae44b0fb78a1144c4Winson     * Reloads all the resources for the current configuration.
560b94443d876d3c9575e88078d0e537bb385f79ca4Winson     */
561670ea71f1b4ae59a0cd6608ae44b0fb78a1144c4Winson    private void reloadResources() {
562b94443d876d3c9575e88078d0e537bb385f79ca4Winson        Resources res = mContext.getResources();
563b94443d876d3c9575e88078d0e537bb385f79ca4Winson
564b94443d876d3c9575e88078d0e537bb385f79ca4Winson        mStatusBarHeight = res.getDimensionPixelSize(
565b94443d876d3c9575e88078d0e537bb385f79ca4Winson                com.android.internal.R.dimen.status_bar_height);
566b94443d876d3c9575e88078d0e537bb385f79ca4Winson        mNavBarHeight = res.getDimensionPixelSize(
567b94443d876d3c9575e88078d0e537bb385f79ca4Winson                com.android.internal.R.dimen.navigation_bar_height);
568b94443d876d3c9575e88078d0e537bb385f79ca4Winson        mNavBarWidth = res.getDimensionPixelSize(
569b94443d876d3c9575e88078d0e537bb385f79ca4Winson                com.android.internal.R.dimen.navigation_bar_width);
57025160db73f37619a26a3d87a06625925d881b0fdJorim Jaggi        mTaskBarHeight = TaskStackLayoutAlgorithm.getDimensionForDevice(mContext,
571217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height,
572217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height,
573217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height,
574217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height_tablet_land,
575217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height,
576217009356efa2b854ab3981dff8d315a0d679c73Winson                R.dimen.recents_task_view_header_height_tablet_land);
577b94443d876d3c9575e88078d0e537bb385f79ca4Winson    }
578b94443d876d3c9575e88078d0e537bb385f79ca4Winson
579b94443d876d3c9575e88078d0e537bb385f79ca4Winson    /**
580190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Prepares the header bar layout for the next transition, if the task view bounds has changed
581190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * since the last call, it will attempt to re-measure and layout the header bar to the new size.
582190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     *
583f0d1c44a59a10707baa0cca8dd377302260710c1Winson     * @param stack the stack to initialize the stack layout with
584db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi     * @param windowRectOverride the rectangle to use when calculating the stack state which can
585db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi     *                           be different from the current window rect if recents is resizing
586db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi     *                           while being launched
587190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
588db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi    private void updateHeaderBarLayout(TaskStack stack, Rect windowRectOverride) {
589e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        SystemServicesProxy ssp = Recents.getSystemServices();
590fc48b072e8caa62849ee305f89f38d248d4a1dedWinson        Rect displayRect = ssp.getDisplayRect();
591c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        Rect systemInsets = new Rect();
592c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        ssp.getStableInsets(systemInsets);
593db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi        Rect windowRect = windowRectOverride != null
594db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                ? new Rect(windowRectOverride)
595db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                : ssp.getWindowRect();
596cf9b8326cecfca3917779c3701aa08eaaad56505Winson        // When docked, the nav bar insets are consumed and the activity is measured without insets.
597cf9b8326cecfca3917779c3701aa08eaaad56505Winson        // However, the window bounds include the insets, so we need to subtract them here to make
598cf9b8326cecfca3917779c3701aa08eaaad56505Winson        // them identical.
599cf9b8326cecfca3917779c3701aa08eaaad56505Winson        if (ssp.hasDockedTask()) {
600cf9b8326cecfca3917779c3701aa08eaaad56505Winson            windowRect.bottom -= systemInsets.bottom;
601cf9b8326cecfca3917779c3701aa08eaaad56505Winson            systemInsets.bottom = 0;
602cf9b8326cecfca3917779c3701aa08eaaad56505Winson        }
603c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        calculateWindowStableInsets(systemInsets, windowRect);
604c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        windowRect.offsetTo(0, 0);
605190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
60659924fe0d9136cf349759bea1e06b661603f95feWinson        TaskStackLayoutAlgorithm stackLayout = mDummyStackView.getStackAlgorithm();
607190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
608190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Rebind the header bar and draw it for the transition
6098873754f66527d2cc9feab295dd4eef23298212aWinson        stackLayout.setSystemInsets(systemInsets);
610f0d1c44a59a10707baa0cca8dd377302260710c1Winson        if (stack != null) {
611fc48b072e8caa62849ee305f89f38d248d4a1dedWinson            stackLayout.getTaskStackBounds(displayRect, windowRect, systemInsets.top,
612fc48b072e8caa62849ee305f89f38d248d4a1dedWinson                    systemInsets.right, mTaskStackBounds);
613aeb298c4db913b376fb6053ec30c132a93e04635Winson            stackLayout.reset();
614fc48b072e8caa62849ee305f89f38d248d4a1dedWinson            stackLayout.initialize(displayRect, windowRect, mTaskStackBounds,
615f0d1c44a59a10707baa0cca8dd377302260710c1Winson                    TaskStackLayoutAlgorithm.StackState.getStackStateForStack(stack));
616a1ededd056d51532ab18354f17f8065ec1134535Winson            mDummyStackView.setTasks(stack, false /* allowNotifyStackChanges */);
617190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
618aeb298c4db913b376fb6053ec30c132a93e04635Winson            Rect taskViewBounds = stackLayout.getUntransformedTaskViewBounds();
61994bc4f2238d8eefb77c63a560aa0de1335ee57abWinson            if (!taskViewBounds.isEmpty()) {
62094bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                int taskViewWidth = taskViewBounds.width();
62194bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                synchronized (mHeaderBarLock) {
62294bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                    if (mHeaderBar.getMeasuredWidth() != taskViewWidth ||
62394bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                            mHeaderBar.getMeasuredHeight() != mTaskBarHeight) {
62494bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                        mHeaderBar.measure(
62594bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                                MeasureSpec.makeMeasureSpec(taskViewWidth, MeasureSpec.EXACTLY),
62694bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                                MeasureSpec.makeMeasureSpec(mTaskBarHeight, MeasureSpec.EXACTLY));
62794bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                    }
62894bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                    mHeaderBar.layout(0, 0, taskViewWidth, mTaskBarHeight);
629aeb298c4db913b376fb6053ec30c132a93e04635Winson                }
630aeb298c4db913b376fb6053ec30c132a93e04635Winson
63194bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                // Update the transition bitmap to match the new header bar height
63294bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                if (mThumbTransitionBitmapCache == null ||
63394bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                        (mThumbTransitionBitmapCache.getWidth() != taskViewWidth) ||
63494bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                        (mThumbTransitionBitmapCache.getHeight() != mTaskBarHeight)) {
63594bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                    mThumbTransitionBitmapCache = Bitmap.createBitmap(taskViewWidth,
63694bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                            mTaskBarHeight, Bitmap.Config.ARGB_8888);
63794bc4f2238d8eefb77c63a560aa0de1335ee57abWinson                }
638aeb298c4db913b376fb6053ec30c132a93e04635Winson            }
639190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
640190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
641190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
642190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
643c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi     * Given the stable insets and the rect for our window, calculates the insets that affect our
644c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi     * window.
645c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi     */
646c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi    private void calculateWindowStableInsets(Rect inOutInsets, Rect windowRect) {
647c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        Rect displayRect = Recents.getSystemServices().getDisplayRect();
648c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi
649c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        // Display rect without insets - available app space
650c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        Rect appRect = new Rect(displayRect);
651c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        appRect.inset(inOutInsets);
652c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi
653c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        // Our window intersected with available app space
654c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        Rect windowRectWithInsets = new Rect(windowRect);
655c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        windowRectWithInsets.intersect(appRect);
656c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        inOutInsets.left = windowRectWithInsets.left - windowRect.left;
657c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        inOutInsets.top = windowRectWithInsets.top - windowRect.top;
658c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        inOutInsets.right = windowRect.right - windowRectWithInsets.right;
659c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi        inOutInsets.bottom = windowRect.bottom - windowRectWithInsets.bottom;
660c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi    }
661c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi
662c6c89a82144f59475242c75d67529fed943ae30bJorim Jaggi    /**
663190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Preloads the icon of a task.
664190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
665c4a038ad28009eb5840cf9f6aefbd2c873cee02eWinson    private void preloadIcon(int runningTaskId) {
666190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Ensure that we load the running task's icon
667190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
668c4a038ad28009eb5840cf9f6aefbd2c873cee02eWinson        launchOpts.runningTaskId = runningTaskId;
669190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.loadThumbnails = false;
670190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        launchOpts.onlyLoadForCache = true;
671e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        Recents.getTaskLoader().loadTasks(mContext, sInstanceLoadPlan, launchOpts);
672190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
673190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
674190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
675190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Creates the activity options for a unknown state->recents transition.
676190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
6774bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan    protected ActivityOptions getUnknownTransitionActivityOptions() {
678190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return ActivityOptions.makeCustomAnimation(mContext,
679190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                R.anim.recents_from_unknown_enter,
680190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                R.anim.recents_from_unknown_exit,
6813fb67562c548662910160d5672b1b9558bd6571cWinson                mHandler, null);
682190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
683190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
684190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
685190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Creates the activity options for a home->recents transition.
686190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
687008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson    protected ActivityOptions getHomeTransitionActivityOptions() {
688190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return ActivityOptions.makeCustomAnimation(mContext,
689190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                R.anim.recents_from_launcher_enter,
690190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                R.anim.recents_from_launcher_exit,
6913fb67562c548662910160d5672b1b9558bd6571cWinson                mHandler, null);
692190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
693190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
694190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
695190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Creates the activity options for an app->recents transition.
696190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
697190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    private ActivityOptions getThumbnailTransitionActivityOptions(
698d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson            ActivityManager.RunningTaskInfo runningTask, TaskStackView stackView,
699d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson                    Rect windowOverrideRect) {
700c4a038ad28009eb5840cf9f6aefbd2c873cee02eWinson        if (runningTask != null && runningTask.stackId == FREEFORM_WORKSPACE_STACK_ID) {
701d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            ArrayList<AppTransitionAnimationSpec> specs = new ArrayList<>();
702e693aafe0511c2a7ffc571b22abeefba44046225Winson            ArrayList<Task> tasks = stackView.getStack().getStackTasks();
703e693aafe0511c2a7ffc571b22abeefba44046225Winson            TaskStackLayoutAlgorithm stackLayout = stackView.getStackAlgorithm();
704e693aafe0511c2a7ffc571b22abeefba44046225Winson            TaskStackViewScroller stackScroller = stackView.getScroller();
705e693aafe0511c2a7ffc571b22abeefba44046225Winson
706003eda6c230a1b1c4e3709466c46660e7602d740Winson            stackView.updateLayoutAlgorithm(true /* boundScroll */);
70767c79578f94646bade5d336af0d4491179c1b37dWinson            stackView.updateToInitialState();
708e693aafe0511c2a7ffc571b22abeefba44046225Winson
709d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            for (int i = tasks.size() - 1; i >= 0; i--) {
710d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                Task task = tasks.get(i);
711387aac6ad7bde21f2c2510cb8a2f3af3959721e0Winson                if (task.isFreeformTask()) {
712e693aafe0511c2a7ffc571b22abeefba44046225Winson                    mTmpTransform = stackLayout.getStackTransformScreenCoordinates(task,
713db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                            stackScroller.getStackScroll(), mTmpTransform, null,
714db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                            windowOverrideRect);
715aeb298c4db913b376fb6053ec30c132a93e04635Winson                    Bitmap thumbnail = drawThumbnailTransitionBitmap(task, mTmpTransform,
716aeb298c4db913b376fb6053ec30c132a93e04635Winson                            mThumbTransitionBitmapCache);
717d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                    Rect toTaskRect = new Rect();
718d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                    mTmpTransform.rect.round(toTaskRect);
719d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                    specs.add(new AppTransitionAnimationSpec(task.key.id, thumbnail, toTaskRect));
720d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski                }
721d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            }
722d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            AppTransitionAnimationSpec[] specsArray = new AppTransitionAnimationSpec[specs.size()];
723d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            specs.toArray(specsArray);
724d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            return ActivityOptions.makeThumbnailAspectScaleDownAnimation(mDummyStackView,
7253fb67562c548662910160d5672b1b9558bd6571cWinson                    specsArray, mHandler, null, this);
726d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski        } else {
727d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            // Update the destination rect
728d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            Task toTask = new Task();
729db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi            TaskViewTransform toTransform = getThumbnailTransitionTransform(stackView, toTask,
730db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                    windowOverrideRect);
731aeb298c4db913b376fb6053ec30c132a93e04635Winson            Bitmap thumbnail = drawThumbnailTransitionBitmap(toTask, toTransform,
732aeb298c4db913b376fb6053ec30c132a93e04635Winson                    mThumbTransitionBitmapCache);
733d64ef3ef33c50a03b4be3b2baaa93aab7319fca8Filip Gruszczynski            if (thumbnail != null) {
734aeb298c4db913b376fb6053ec30c132a93e04635Winson                RectF toTaskRect = toTransform.rect;
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
744190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
745190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Returns the transition rect for the given task id.
746190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
747e693aafe0511c2a7ffc571b22abeefba44046225Winson    private TaskViewTransform getThumbnailTransitionTransform(TaskStackView stackView,
748db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi            Task runningTaskOut, Rect windowOverrideRect) {
749190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Find the running task in the TaskStack
750e693aafe0511c2a7ffc571b22abeefba44046225Winson        TaskStack stack = stackView.getStack();
75165c851e6e9e08656744b6f66d3da188e3283b17dWinson        Task launchTask = stack.getLaunchTarget();
75265c851e6e9e08656744b6f66d3da188e3283b17dWinson        if (launchTask != null) {
75365c851e6e9e08656744b6f66d3da188e3283b17dWinson            runningTaskOut.copyFrom(launchTask);
75465c851e6e9e08656744b6f66d3da188e3283b17dWinson        } else {
755190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            // If no task is specified or we can not find the task just use the front most one
75635a8b04140598a5b5c4865254b942adb6a830991Winson            launchTask = stack.getStackFrontMostTask(true /* includeFreeform */);
75765c851e6e9e08656744b6f66d3da188e3283b17dWinson            runningTaskOut.copyFrom(launchTask);
758190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
759190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
760190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Get the transform for the running task
761003eda6c230a1b1c4e3709466c46660e7602d740Winson        stackView.updateLayoutAlgorithm(true /* boundScroll */);
76267c79578f94646bade5d336af0d4491179c1b37dWinson        stackView.updateToInitialState();
7637845e8c4946f6b5dadfcd9c1d64e826bacc50edbWinson        stackView.getStackAlgorithm().getStackTransformScreenCoordinates(launchTask,
764db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                stackView.getScroller().getStackScroll(), mTmpTransform, null, windowOverrideRect);
765190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return mTmpTransform;
766190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
767190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
768190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
769190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Draws the header of a task used for the window animation into a bitmap.
770190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
771aeb298c4db913b376fb6053ec30c132a93e04635Winson    private Bitmap drawThumbnailTransitionBitmap(Task toTask, TaskViewTransform toTransform,
772aeb298c4db913b376fb6053ec30c132a93e04635Winson            Bitmap thumbnail) {
7738be1634a543022bd6ee0c1e974d3201d452981e8Winson        SystemServicesProxy ssp = Recents.getSystemServices();
774190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (toTransform != null && toTask.key != null) {
775190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            synchronized (mHeaderBarLock) {
7768be1634a543022bd6ee0c1e974d3201d452981e8Winson                boolean disabledInSafeMode = !toTask.isSystemApp && ssp.isInSafeMode();
777509d0d0c9e2ee165d04e898fea59f8941ac7138dWinson Chung                mHeaderBar.onTaskViewSizeChanged((int) toTransform.rect.width(),
778509d0d0c9e2ee165d04e898fea59f8941ac7138dWinson Chung                        (int) toTransform.rect.height());
779c742f973b1e506732911c156c5869fd377afc5bfWinson                if (RecentsDebugFlags.Static.EnableTransitionThumbnailDebugMode) {
780190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    thumbnail.eraseColor(0xFFff0000);
781190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                } else {
782aeb298c4db913b376fb6053ec30c132a93e04635Winson                    thumbnail.eraseColor(0);
783190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    Canvas c = new Canvas(thumbnail);
78422574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    // Workaround for b/27815919, reset the callback so that we do not trigger an
78522574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    // invalidate on the header bar as a result of updating the icon
78622574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    Drawable icon = mHeaderBar.getIconView().getDrawable();
78722574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    if (icon != null) {
78822574afa00b7e32b7c4c5a1b98deebd67751b364Winson                        icon.setCallback(null);
78922574afa00b7e32b7c4c5a1b98deebd67751b364Winson                    }
790d2a030613154e2007d9816e090c39e9726e7adbaWinson                    mHeaderBar.bindToTask(toTask, false /* touchExplorationEnabled */,
7918be1634a543022bd6ee0c1e974d3201d452981e8Winson                            disabledInSafeMode);
792d2a030613154e2007d9816e090c39e9726e7adbaWinson                    mHeaderBar.onTaskDataLoaded();
793e693aafe0511c2a7ffc571b22abeefba44046225Winson                    mHeaderBar.setDimAlpha(toTransform.dimAlpha);
794190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    mHeaderBar.draw(c);
795190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                    c.setBitmap(null);
796190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                }
797190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            }
798190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            return thumbnail.createAshmemBitmap();
799190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
800190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        return null;
801190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
802190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
803190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    /**
804190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Shows the recents activity
805190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
806d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson    protected void startRecentsActivity(ActivityManager.RunningTaskInfo runningTask,
807d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson            boolean isHomeStackVisible, boolean animate, int growTarget) {
808e7f138c7f0a190c86cec10fb32fa106aacae4093Winson        RecentsTaskLoader loader = Recents.getTaskLoader();
809c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
810190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
811c4a038ad28009eb5840cf9f6aefbd2c873cee02eWinson        int runningTaskId = !mLaunchedWhileDocking && (runningTask != null)
812c4a038ad28009eb5840cf9f6aefbd2c873cee02eWinson                ? runningTask.id
813c4a038ad28009eb5840cf9f6aefbd2c873cee02eWinson                : -1;
814c4a038ad28009eb5840cf9f6aefbd2c873cee02eWinson
815e5f1faa9f8009a723ab21aed6fe5ab325c61442bWinson        // In the case where alt-tab is triggered, we never get a preloadRecents() call, so we
816435b2e43cb131d0b38ba19f335aec78bb83944b2Jorim Jaggi        // should always preload the tasks now. If we are dragging in recents, reload them as
817435b2e43cb131d0b38ba19f335aec78bb83944b2Jorim Jaggi        // the stacks might have changed.
818e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi        if (mLaunchedWhileDocking || mTriggeredFromAltTab ||sInstanceLoadPlan == null) {
819e5f1faa9f8009a723ab21aed6fe5ab325c61442bWinson            // Create a new load plan if preloadRecents() was never triggered
820190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            sInstanceLoadPlan = loader.createLoadPlan(mContext);
821190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
822e161f08e98fdbe6fe83f70ffa1ea11142a027ebeJorim Jaggi        if (mLaunchedWhileDocking || mTriggeredFromAltTab || !sInstanceLoadPlan.hasTasks()) {
823c4a038ad28009eb5840cf9f6aefbd2c873cee02eWinson            loader.preloadTasks(sInstanceLoadPlan, runningTaskId, !isHomeStackVisible);
824190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
825c5b12dd37e964b220c0a108e86ad8a2d12805747Winson
826190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        TaskStack stack = sInstanceLoadPlan.getTaskStack();
827c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        boolean hasRecentTasks = stack.getTaskCount() > 0;
828d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson        boolean useThumbnailTransition = (runningTask != null) && !isHomeStackVisible && hasRecentTasks;
829190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
830aeb298c4db913b376fb6053ec30c132a93e04635Winson        // Update the launch state that we need in updateHeaderBarLayout()
831c5887ea7a0b82cc5909743ce14cbda1dcf1dfc82Jorim Jaggi        launchState.launchedFromHome = !useThumbnailTransition && !mLaunchedWhileDocking;
832aeb298c4db913b376fb6053ec30c132a93e04635Winson        launchState.launchedFromApp = useThumbnailTransition || mLaunchedWhileDocking;
833aeb298c4db913b376fb6053ec30c132a93e04635Winson        launchState.launchedViaDockGesture = mLaunchedWhileDocking;
834aeb298c4db913b376fb6053ec30c132a93e04635Winson        launchState.launchedViaDragGesture = mDraggingInRecents;
835c4a038ad28009eb5840cf9f6aefbd2c873cee02eWinson        launchState.launchedToTaskId = runningTaskId;
836aeb298c4db913b376fb6053ec30c132a93e04635Winson        launchState.launchedWithAltTab = mTriggeredFromAltTab;
837aeb298c4db913b376fb6053ec30c132a93e04635Winson
838aeb298c4db913b376fb6053ec30c132a93e04635Winson        // Preload the icon (this will be a null-op if we have preloaded the icon already in
839aeb298c4db913b376fb6053ec30c132a93e04635Winson        // preloadRecents())
840c4a038ad28009eb5840cf9f6aefbd2c873cee02eWinson        preloadIcon(runningTaskId);
841aeb298c4db913b376fb6053ec30c132a93e04635Winson
842f0d1c44a59a10707baa0cca8dd377302260710c1Winson        // Update the header bar if necessary
843db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi        Rect windowOverrideRect = getWindowRectOverride(growTarget);
844db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi        updateHeaderBarLayout(stack, windowOverrideRect);
845f0d1c44a59a10707baa0cca8dd377302260710c1Winson
846190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        // Prepare the dummy stack for the transition
84736a5a2c7003ef8157f276b411c3fda47ad2f75e3Winson        TaskStackLayoutAlgorithm.VisibilityReport stackVr =
848190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                mDummyStackView.computeStackVisibilityReport();
849bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi
850aeb298c4db913b376fb6053ec30c132a93e04635Winson        // Update the remaining launch state
851c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedNumVisibleTasks = stackVr.numVisibleTasks;
852c5b12dd37e964b220c0a108e86ad8a2d12805747Winson        launchState.launchedNumVisibleThumbnails = stackVr.numVisibleThumbnails;
853c5b12dd37e964b220c0a108e86ad8a2d12805747Winson
854bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi        if (!animate) {
855c5b12dd37e964b220c0a108e86ad8a2d12805747Winson            startRecentsActivity(ActivityOptions.makeCustomAnimation(mContext, -1, -1));
856bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi            return;
857bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi        }
858bb42a46631b4cecdd03bd628feeb8270d412ebd3Jorim Jaggi
859aeb298c4db913b376fb6053ec30c132a93e04635Winson        ActivityOptions opts;
860190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (useThumbnailTransition) {
861190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            // Try starting with a thumbnail transition
862d46b72788e8811d2e423ea1e5b2ff104ecb7f721Winson            opts = getThumbnailTransitionActivityOptions(runningTask, mDummyStackView,
863db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                    windowOverrideRect);
864aeb298c4db913b376fb6053ec30c132a93e04635Winson        } else {
865fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan            // If there is no thumbnail transition, but is launching from home into recents, then
866008ee15fe82c8216e8e4f67e3032de468cc6c92aWinson            // use a quick home transition
867aeb298c4db913b376fb6053ec30c132a93e04635Winson            opts = hasRecentTasks
868aeb298c4db913b376fb6053ec30c132a93e04635Winson                ? getHomeTransitionActivityOptions()
869aeb298c4db913b376fb6053ec30c132a93e04635Winson                : getUnknownTransitionActivityOptions();
870fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan        }
871aeb298c4db913b376fb6053ec30c132a93e04635Winson        startRecentsActivity(opts);
872fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan        mLastToggleTime = SystemClock.elapsedRealtime();
873fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan    }
874fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan
875db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi    private Rect getWindowRectOverride(int growTarget) {
876db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi        if (growTarget == DividerView.INVALID_RECENTS_GROW_TARGET) {
877db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi            return null;
878db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi        }
879db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi        Rect result = new Rect();
880db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi        Rect displayRect = Recents.getSystemServices().getDisplayRect();
881db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi        DockedDividerUtils.calculateBoundsForPosition(growTarget, WindowManager.DOCKED_BOTTOM,
882db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                result, displayRect.width(), displayRect.height(),
883db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi                Recents.getSystemServices().getDockedDividerSize(mContext));
884db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi        return result;
885db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi    }
886db21bbd2caf05322864f09ec45a0c572cf071123Jorim Jaggi
887fa61ce343e91e11bb06545eae00e0659a2942a7dSid Soundararajan    /**
888190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     * Starts the recents activity.
889190fe3bf88388fcb109af64571e3baa0d01f1c37Winson     */
890c5b12dd37e964b220c0a108e86ad8a2d12805747Winson    private void startRecentsActivity(ActivityOptions opts) {
891190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        Intent intent = new Intent();
8924bdb6879bd5f1b7e93e7e9ac236a073921862075Sid Soundararajan        intent.setClassName(RECENTS_PACKAGE, RECENTS_ACTIVITY);
893190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
894190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
895190fe3bf88388fcb109af64571e3baa0d01f1c37Winson                | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
896b58c46acec2e270f24ca587a64298f06cefb09cbSid Soundararajan
897190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        if (opts != null) {
898190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            mContext.startActivityAsUser(intent, opts.toBundle(), UserHandle.CURRENT);
899190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        } else {
900190fe3bf88388fcb109af64571e3baa0d01f1c37Winson            mContext.startActivityAsUser(intent, UserHandle.CURRENT);
901190fe3bf88388fcb109af64571e3baa0d01f1c37Winson        }
902cdb06caebb5f6f554b2ed8c76963970d8cc0ab54Jorim Jaggi        EventBus.getDefault().send(new RecentsActivityStartingEvent());
903190fe3bf88388fcb109af64571e3baa0d01f1c37Winson    }
904190fe3bf88388fcb109af64571e3baa0d01f1c37Winson
9053fb67562c548662910160d5672b1b9558bd6571cWinson    /**** OnAnimationFinishedListener Implementation ****/
9061a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski
9071a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski    @Override
9081a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski    public void onAnimationFinished() {
9091a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski        EventBus.getDefault().post(new EnterRecentsWindowLastAnimationFrameEvent());
9101a5203dfd5264104db018b8a09d50075b1af9b2dFilip Gruszczynski    }
911190fe3bf88388fcb109af64571e3baa0d01f1c37Winson}
912