blob: 10181481b0af6f879f77247b9492c32f7a59b620 [file] [log] [blame]
Winson190fe3bf2015-10-20 14:57:24 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.recents;
18
Winsone693aaf2016-03-01 12:05:59 -080019import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Vladislav Kaznacheev1f6b7092016-05-04 17:18:56 -070020import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson94bc4f22016-04-07 14:22:12 -070021import static android.view.View.MeasureSpec;
Winsone693aaf2016-03-01 12:05:59 -080022
Winson190fe3bf2015-10-20 14:57:24 -070023import android.app.ActivityManager;
24import android.app.ActivityOptions;
Winson190fe3bf2015-10-20 14:57:24 -070025import android.content.ActivityNotFoundException;
Winson190fe3bf2015-10-20 14:57:24 -070026import android.content.Context;
27import android.content.Intent;
28import android.content.res.Resources;
29import android.graphics.Bitmap;
30import android.graphics.Canvas;
31import android.graphics.Rect;
Winson3150e572015-10-23 15:07:24 -070032import android.graphics.RectF;
Winson22574af2016-03-23 19:00:28 -070033import android.graphics.drawable.Drawable;
Winson190fe3bf2015-10-20 14:57:24 -070034import android.os.Handler;
35import android.os.SystemClock;
36import android.os.UserHandle;
Winson1b585612015-11-06 09:16:26 -080037import android.util.Log;
Winson190fe3bf2015-10-20 14:57:24 -070038import android.util.MutableBoolean;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -070039import android.view.AppTransitionAnimationSpec;
Winson190fe3bf2015-10-20 14:57:24 -070040import android.view.LayoutInflater;
Winsonb61e6542016-02-04 14:37:18 -080041import android.view.ViewConfiguration;
Jorim Jaggidb21bbd2016-04-18 15:32:07 -070042import android.view.WindowManager;
Winsonc0d70582016-01-29 10:24:39 -080043
Winson190fe3bf2015-10-20 14:57:24 -070044import com.android.internal.logging.MetricsLogger;
Jorim Jaggidb21bbd2016-04-18 15:32:07 -070045import com.android.internal.policy.DockedDividerUtils;
Winson190fe3bf2015-10-20 14:57:24 -070046import com.android.systemui.R;
47import com.android.systemui.SystemUIApplication;
Winson412e1802015-10-20 16:57:57 -070048import com.android.systemui.recents.events.EventBus;
Jorim Jaggi899327f2016-02-25 20:44:18 -050049import com.android.systemui.recents.events.activity.DockedTopTaskEvent;
Winson1b585612015-11-06 09:16:26 -080050import com.android.systemui.recents.events.activity.EnterRecentsWindowLastAnimationFrameEvent;
Winson412e1802015-10-20 16:57:57 -070051import com.android.systemui.recents.events.activity.HideRecentsEvent;
Winson0d14d4d2015-10-26 17:05:04 -070052import com.android.systemui.recents.events.activity.IterateRecentsEvent;
Winsonb61e6542016-02-04 14:37:18 -080053import com.android.systemui.recents.events.activity.LaunchNextTaskRequestEvent;
Jorim Jaggicdb06ca2016-01-25 19:15:12 -080054import com.android.systemui.recents.events.activity.RecentsActivityStartingEvent;
Winson412e1802015-10-20 16:57:57 -070055import com.android.systemui.recents.events.activity.ToggleRecentsEvent;
Winson190fe3bf2015-10-20 14:57:24 -070056import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
57import com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
Jorim Jaggidd98d412015-11-18 15:57:38 -080058import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
59import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
Winson6b92c6e2015-11-06 13:11:16 -080060import com.android.systemui.recents.misc.DozeTrigger;
Winsonab84fc52015-10-23 11:52:07 -070061import com.android.systemui.recents.misc.ForegroundThread;
Winson190fe3bf2015-10-20 14:57:24 -070062import com.android.systemui.recents.misc.SystemServicesProxy;
Jaewan Kim938a50b2016-03-14 17:35:43 +090063import com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener;
Winson190fe3bf2015-10-20 14:57:24 -070064import com.android.systemui.recents.model.RecentsTaskLoadPlan;
65import com.android.systemui.recents.model.RecentsTaskLoader;
66import com.android.systemui.recents.model.Task;
67import com.android.systemui.recents.model.TaskGrouping;
68import com.android.systemui.recents.model.TaskStack;
Winson36a5a2c2015-10-29 18:04:39 -070069import com.android.systemui.recents.views.TaskStackLayoutAlgorithm;
Winson1b585612015-11-06 09:16:26 -080070import com.android.systemui.recents.views.TaskStackView;
Winsone693aaf2016-03-01 12:05:59 -080071import com.android.systemui.recents.views.TaskStackViewScroller;
Winson190fe3bf2015-10-20 14:57:24 -070072import com.android.systemui.recents.views.TaskViewHeader;
73import com.android.systemui.recents.views.TaskViewTransform;
Jorim Jaggidb21bbd2016-04-18 15:32:07 -070074import com.android.systemui.stackdivider.DividerView;
Winsond8b1d632016-01-04 17:51:18 -080075import com.android.systemui.statusbar.BaseStatusBar;
Jorim Jaggicdb06ca2016-01-25 19:15:12 -080076import com.android.systemui.statusbar.phone.NavigationBarGestureHelper;
Winson190fe3bf2015-10-20 14:57:24 -070077import com.android.systemui.statusbar.phone.PhoneStatusBar;
78
79import java.util.ArrayList;
80
81/**
82 * An implementation of the Recents component for the current user. For secondary users, this can
83 * be called remotely from the system user.
84 */
Jorim Jaggicdb06ca2016-01-25 19:15:12 -080085public class RecentsImpl implements ActivityOptions.OnAnimationFinishedListener {
Winson190fe3bf2015-10-20 14:57:24 -070086
87 private final static String TAG = "RecentsImpl";
Winsonb61e6542016-02-04 14:37:18 -080088
Winson6b92c6e2015-11-06 13:11:16 -080089 // The minimum amount of time between each recents button press that we will handle
90 private final static int MIN_TOGGLE_DELAY_MS = 350;
Winsonb61e6542016-02-04 14:37:18 -080091
Winson6b92c6e2015-11-06 13:11:16 -080092 // The duration within which the user releasing the alt tab (from when they pressed alt tab)
93 // that the fast alt-tab animation will run. If the user's alt-tab takes longer than this
94 // duration, then we will toggle recents after this duration.
95 private final static int FAST_ALT_TAB_DELAY_MS = 225;
Winson190fe3bf2015-10-20 14:57:24 -070096
97 public final static String RECENTS_PACKAGE = "com.android.systemui";
98 public final static String RECENTS_ACTIVITY = "com.android.systemui.recents.RecentsActivity";
Winsone693aaf2016-03-01 12:05:59 -080099
Winson190fe3bf2015-10-20 14:57:24 -0700100 /**
Jaewan Kim938a50b2016-03-14 17:35:43 +0900101 * An implementation of TaskStackListener, that allows us to listen for changes to the system
Winson190fe3bf2015-10-20 14:57:24 -0700102 * task stacks and update recents accordingly.
103 */
Jaewan Kim938a50b2016-03-14 17:35:43 +0900104 class TaskStackListenerImpl extends TaskStackListener {
Winson190fe3bf2015-10-20 14:57:24 -0700105 @Override
106 public void onTaskStackChanged() {
Jaewan Kim938a50b2016-03-14 17:35:43 +0900107 // Preloads the next task
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100108 RecentsConfiguration config = Recents.getConfiguration();
Winson190fe3bf2015-10-20 14:57:24 -0700109 if (config.svelteLevel == RecentsConfiguration.SVELTE_NONE) {
Winsone7f138c2015-10-22 16:15:21 -0700110 RecentsTaskLoader loader = Recents.getTaskLoader();
111 SystemServicesProxy ssp = Recents.getSystemServices();
Winsond46b7272016-04-20 11:54:27 -0700112 ActivityManager.RunningTaskInfo runningTaskInfo = ssp.getRunningTask();
Winson190fe3bf2015-10-20 14:57:24 -0700113
114 // Load the next task only if we aren't svelte
115 RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
Winsondfd7be02016-05-10 13:30:37 -0700116 loader.preloadTasks(plan, -1, false /* includeFrontMostExcludedTask */);
Winson190fe3bf2015-10-20 14:57:24 -0700117 RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
118 // This callback is made when a new activity is launched and the old one is paused
119 // so ignore the current activity and try and preload the thumbnail for the
120 // previous one.
121 if (runningTaskInfo != null) {
122 launchOpts.runningTaskId = runningTaskInfo.id;
123 }
124 launchOpts.numVisibleTasks = 2;
125 launchOpts.numVisibleTaskThumbnails = 2;
126 launchOpts.onlyLoadForCache = true;
127 launchOpts.onlyLoadPausedActivities = true;
128 loader.loadTasks(mContext, plan, launchOpts);
129 }
Winson190fe3bf2015-10-20 14:57:24 -0700130 }
131 }
132
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700133 protected static RecentsTaskLoadPlan sInstanceLoadPlan;
Winson190fe3bf2015-10-20 14:57:24 -0700134
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700135 protected Context mContext;
136 protected Handler mHandler;
Winson190fe3bf2015-10-20 14:57:24 -0700137 TaskStackListenerImpl mTaskStackListener;
Jorim Jaggidd98d412015-11-18 15:57:38 -0800138 boolean mDraggingInRecents;
Jorim Jaggie161f082016-02-05 14:26:16 -0800139 boolean mLaunchedWhileDocking;
Winson190fe3bf2015-10-20 14:57:24 -0700140
141 // Task launching
Winson190fe3bf2015-10-20 14:57:24 -0700142 Rect mTaskStackBounds = new Rect();
Winson190fe3bf2015-10-20 14:57:24 -0700143 TaskViewTransform mTmpTransform = new TaskViewTransform();
144 int mStatusBarHeight;
145 int mNavBarHeight;
146 int mNavBarWidth;
147 int mTaskBarHeight;
148
149 // Header (for transition)
150 TaskViewHeader mHeaderBar;
151 final Object mHeaderBarLock = new Object();
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700152 protected TaskStackView mDummyStackView;
Winson190fe3bf2015-10-20 14:57:24 -0700153
154 // Variables to keep track of if we need to start recents after binding
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700155 protected boolean mTriggeredFromAltTab;
156 protected long mLastToggleTime;
Winson6b92c6e2015-11-06 13:11:16 -0800157 DozeTrigger mFastAltTabTrigger = new DozeTrigger(FAST_ALT_TAB_DELAY_MS, new Runnable() {
158 @Override
159 public void run() {
160 // When this fires, then the user has not released alt-tab for at least
161 // FAST_ALT_TAB_DELAY_MS milliseconds
Jorim Jaggi435b2e42015-11-24 15:09:30 -0800162 showRecents(mTriggeredFromAltTab, false /* draggingInRecents */, true /* animate */,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700163 false /* reloadTasks */, false /* fromHome */,
164 DividerView.INVALID_RECENTS_GROW_TARGET);
Winson6b92c6e2015-11-06 13:11:16 -0800165 }
166 });
Winson190fe3bf2015-10-20 14:57:24 -0700167
Winsonaeb298c2016-04-05 13:08:11 -0700168 protected Bitmap mThumbTransitionBitmapCache;
Winson190fe3bf2015-10-20 14:57:24 -0700169
Winson190fe3bf2015-10-20 14:57:24 -0700170 public RecentsImpl(Context context) {
171 mContext = context;
Winson190fe3bf2015-10-20 14:57:24 -0700172 mHandler = new Handler();
Winson190fe3bf2015-10-20 14:57:24 -0700173
Winsonab84fc52015-10-23 11:52:07 -0700174 // Initialize the static foreground thread
175 ForegroundThread.get();
176
Winson190fe3bf2015-10-20 14:57:24 -0700177 // Register the task stack listener
Jaewan Kim938a50b2016-03-14 17:35:43 +0900178 mTaskStackListener = new TaskStackListenerImpl();
Winsone7f138c2015-10-22 16:15:21 -0700179 SystemServicesProxy ssp = Recents.getSystemServices();
180 ssp.registerTaskStackListener(mTaskStackListener);
Winson190fe3bf2015-10-20 14:57:24 -0700181
182 // Initialize the static configuration resources
Winson670ea712016-04-12 17:02:26 -0700183 LayoutInflater inflater = LayoutInflater.from(mContext);
184 mDummyStackView = new TaskStackView(mContext);
185 mHeaderBar = (TaskViewHeader) inflater.inflate(R.layout.recents_task_view_header,
186 null, false);
187 reloadResources();
Winson16ef39a2016-06-28 18:25:15 -0700188 }
Winson190fe3bf2015-10-20 14:57:24 -0700189
Winson16ef39a2016-06-28 18:25:15 -0700190 public void onBootCompleted() {
Winson190fe3bf2015-10-20 14:57:24 -0700191 // When we start, preload the data associated with the previous recent tasks.
192 // We can use a new plan since the caches will be the same.
Winsone7f138c2015-10-22 16:15:21 -0700193 RecentsTaskLoader loader = Recents.getTaskLoader();
Winson190fe3bf2015-10-20 14:57:24 -0700194 RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
Winsondfd7be02016-05-10 13:30:37 -0700195 loader.preloadTasks(plan, -1, false /* includeFrontMostExcludedTask */);
Winson190fe3bf2015-10-20 14:57:24 -0700196 RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
Winson Chung296278a2015-12-17 12:09:02 -0500197 launchOpts.numVisibleTasks = loader.getIconCacheSize();
Winson190fe3bf2015-10-20 14:57:24 -0700198 launchOpts.numVisibleTaskThumbnails = loader.getThumbnailCacheSize();
199 launchOpts.onlyLoadForCache = true;
200 loader.loadTasks(mContext, plan, launchOpts);
201 }
202
Winson190fe3bf2015-10-20 14:57:24 -0700203 public void onConfigurationChanged() {
Winson670ea712016-04-12 17:02:26 -0700204 reloadResources();
205 mDummyStackView.reloadOnConfigurationChange();
206 mHeaderBar.onConfigurationChanged();
Winson190fe3bf2015-10-20 14:57:24 -0700207 }
208
209 /**
210 * This is only called from the system user's Recents. Secondary users will instead proxy their
211 * visibility change events through to the system user via
212 * {@link Recents#onBusEvent(RecentsVisibilityChangedEvent)}.
213 */
214 public void onVisibilityChanged(Context context, boolean visible) {
215 SystemUIApplication app = (SystemUIApplication) context;
216 PhoneStatusBar statusBar = app.getComponent(PhoneStatusBar.class);
217 if (statusBar != null) {
218 statusBar.updateRecentsVisibility(visible);
219 }
220 }
221
222 /**
223 * This is only called from the system user's Recents. Secondary users will instead proxy their
224 * visibility change events through to the system user via
225 * {@link Recents#onBusEvent(ScreenPinningRequestEvent)}.
226 */
Andrii Kulian0f051f52016-04-14 00:41:51 -0700227 public void onStartScreenPinning(Context context, int taskId) {
Winson190fe3bf2015-10-20 14:57:24 -0700228 SystemUIApplication app = (SystemUIApplication) context;
229 PhoneStatusBar statusBar = app.getComponent(PhoneStatusBar.class);
230 if (statusBar != null) {
Andrii Kulian0f051f52016-04-14 00:41:51 -0700231 statusBar.showScreenPinningRequest(taskId, false);
Winson190fe3bf2015-10-20 14:57:24 -0700232 }
233 }
234
Jorim Jaggibb42a462015-11-20 16:27:16 -0800235 public void showRecents(boolean triggeredFromAltTab, boolean draggingInRecents,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700236 boolean animate, boolean launchedWhileDockingTask, boolean fromHome,
237 int growTarget) {
Winson190fe3bf2015-10-20 14:57:24 -0700238 mTriggeredFromAltTab = triggeredFromAltTab;
Jorim Jaggidd98d412015-11-18 15:57:38 -0800239 mDraggingInRecents = draggingInRecents;
Jorim Jaggie161f082016-02-05 14:26:16 -0800240 mLaunchedWhileDocking = launchedWhileDockingTask;
Winsone693aaf2016-03-01 12:05:59 -0800241 if (mFastAltTabTrigger.isAsleep()) {
242 // Fast alt-tab duration has elapsed, fall through to showing Recents and reset
243 mFastAltTabTrigger.stopDozing();
Winson6b92c6e2015-11-06 13:11:16 -0800244 } else if (mFastAltTabTrigger.isDozing()) {
Winsone693aaf2016-03-01 12:05:59 -0800245 // Fast alt-tab duration has not elapsed. If this is triggered by a different
246 // showRecents() call, then ignore that call for now.
247 // TODO: We can not handle quick tabs that happen between the initial showRecents() call
248 // that started the activity and the activity starting up. The severity of this
249 // is inversely proportional to the FAST_ALT_TAB_DELAY_MS duration though.
Winson6b92c6e2015-11-06 13:11:16 -0800250 if (!triggeredFromAltTab) {
251 return;
252 }
253 mFastAltTabTrigger.stopDozing();
Winsone693aaf2016-03-01 12:05:59 -0800254 } else if (triggeredFromAltTab) {
255 // The fast alt-tab detector is not yet running, so start the trigger and wait for the
256 // hideRecents() call, or for the fast alt-tab duration to elapse
257 mFastAltTabTrigger.startDozing();
258 return;
Winson6b92c6e2015-11-06 13:11:16 -0800259 }
Winson190fe3bf2015-10-20 14:57:24 -0700260
261 try {
262 // Check if the top task is in the home stack, and start the recents activity
Winsone7f138c2015-10-22 16:15:21 -0700263 SystemServicesProxy ssp = Recents.getSystemServices();
Winsond7cb5172016-05-04 16:14:19 -0700264 boolean forceVisible = launchedWhileDockingTask || draggingInRecents;
265 MutableBoolean isHomeStackVisible = new MutableBoolean(forceVisible);
266 if (forceVisible || !ssp.isRecentsActivityVisible(isHomeStackVisible)) {
Winsond46b7272016-04-20 11:54:27 -0700267 ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
268 startRecentsActivity(runningTask, isHomeStackVisible.value || fromHome, animate,
269 growTarget);
Winson190fe3bf2015-10-20 14:57:24 -0700270 }
271 } catch (ActivityNotFoundException e) {
Winson1b585612015-11-06 09:16:26 -0800272 Log.e(TAG, "Failed to launch RecentsActivity", e);
Winson190fe3bf2015-10-20 14:57:24 -0700273 }
274 }
275
Winson190fe3bf2015-10-20 14:57:24 -0700276 public void hideRecents(boolean triggeredFromAltTab, boolean triggeredFromHomeKey) {
Winson2799eca2016-02-25 12:10:42 -0800277 if (triggeredFromAltTab && mFastAltTabTrigger.isDozing()) {
278 // The user has released alt-tab before the trigger has run, so just show the next
279 // task immediately
280 showNextTask();
Winson6b92c6e2015-11-06 13:11:16 -0800281
Winson2799eca2016-02-25 12:10:42 -0800282 // Cancel the fast alt-tab trigger
283 mFastAltTabTrigger.stopDozing();
Winson2799eca2016-02-25 12:10:42 -0800284 return;
Winson190fe3bf2015-10-20 14:57:24 -0700285 }
Winson2799eca2016-02-25 12:10:42 -0800286
287 // Defer to the activity to handle hiding recents, if it handles it, then it must still
288 // be visible
289 EventBus.getDefault().post(new HideRecentsEvent(triggeredFromAltTab,
290 triggeredFromHomeKey));
Winson190fe3bf2015-10-20 14:57:24 -0700291 }
292
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700293 public void toggleRecents(int growTarget) {
Winson6b92c6e2015-11-06 13:11:16 -0800294 // Skip this toggle if we are already waiting to trigger recents via alt-tab
295 if (mFastAltTabTrigger.isDozing()) {
296 return;
297 }
298
Jorim Jaggidd98d412015-11-18 15:57:38 -0800299 mDraggingInRecents = false;
Jorim Jaggie161f082016-02-05 14:26:16 -0800300 mLaunchedWhileDocking = false;
Winson190fe3bf2015-10-20 14:57:24 -0700301 mTriggeredFromAltTab = false;
302
303 try {
Winsone7f138c2015-10-22 16:15:21 -0700304 SystemServicesProxy ssp = Recents.getSystemServices();
Winsond46b7272016-04-20 11:54:27 -0700305 MutableBoolean isHomeStackVisible = new MutableBoolean(true);
Winsonb61e6542016-02-04 14:37:18 -0800306 long elapsedTime = SystemClock.elapsedRealtime() - mLastToggleTime;
307
Winsond46b7272016-04-20 11:54:27 -0700308 if (ssp.isRecentsActivityVisible(isHomeStackVisible)) {
Winsone693aaf2016-03-01 12:05:59 -0800309 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
Winson5da43472015-11-04 17:39:55 -0800310 RecentsConfiguration config = Recents.getConfiguration();
311 RecentsActivityLaunchState launchState = config.getLaunchState();
Winson Chungead5c0f2015-12-14 11:18:57 -0500312 if (!launchState.launchedWithAltTab) {
Winsonb61e6542016-02-04 14:37:18 -0800313 // If the user taps quickly
Winsone693aaf2016-03-01 12:05:59 -0800314 if (!debugFlags.isPagingEnabled() ||
315 (ViewConfiguration.getDoubleTapMinTime() < elapsedTime &&
316 elapsedTime < ViewConfiguration.getDoubleTapTimeout())) {
Winsonb61e6542016-02-04 14:37:18 -0800317 // Launch the next focused task
318 EventBus.getDefault().post(new LaunchNextTaskRequestEvent());
319 } else {
320 // Notify recents to move onto the next task
321 EventBus.getDefault().post(new IterateRecentsEvent());
322 }
Winson0d14d4d2015-10-26 17:05:04 -0700323 } else {
324 // If the user has toggled it too quickly, then just eat up the event here (it's
325 // better than showing a janky screenshot).
326 // NOTE: Ideally, the screenshot mechanism would take the window transform into
327 // account
Winsonb61e6542016-02-04 14:37:18 -0800328 if (elapsedTime < MIN_TOGGLE_DELAY_MS) {
Winson0d14d4d2015-10-26 17:05:04 -0700329 return;
330 }
331
332 EventBus.getDefault().post(new ToggleRecentsEvent());
333 mLastToggleTime = SystemClock.elapsedRealtime();
334 }
Winson190fe3bf2015-10-20 14:57:24 -0700335 return;
336 } else {
Winson0d14d4d2015-10-26 17:05:04 -0700337 // If the user has toggled it too quickly, then just eat up the event here (it's
338 // better than showing a janky screenshot).
339 // NOTE: Ideally, the screenshot mechanism would take the window transform into
340 // account
Winsonb61e6542016-02-04 14:37:18 -0800341 if (elapsedTime < MIN_TOGGLE_DELAY_MS) {
Winson0d14d4d2015-10-26 17:05:04 -0700342 return;
343 }
344
Winson190fe3bf2015-10-20 14:57:24 -0700345 // Otherwise, start the recents activity
Winsond46b7272016-04-20 11:54:27 -0700346 ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
347 startRecentsActivity(runningTask, isHomeStackVisible.value, true /* animate */,
348 growTarget);
Winsond8b1d632016-01-04 17:51:18 -0800349
350 // Only close the other system windows if we are actually showing recents
351 ssp.sendCloseSystemWindows(BaseStatusBar.SYSTEM_DIALOG_REASON_RECENT_APPS);
Winson0d14d4d2015-10-26 17:05:04 -0700352 mLastToggleTime = SystemClock.elapsedRealtime();
Winson190fe3bf2015-10-20 14:57:24 -0700353 }
354 } catch (ActivityNotFoundException e) {
Winson1b585612015-11-06 09:16:26 -0800355 Log.e(TAG, "Failed to launch RecentsActivity", e);
Winson190fe3bf2015-10-20 14:57:24 -0700356 }
357 }
358
Winson190fe3bf2015-10-20 14:57:24 -0700359 public void preloadRecents() {
360 // Preload only the raw task list into a new load plan (which will be consumed by the
361 // RecentsActivity) only if there is a task to animate to.
Winsone7f138c2015-10-22 16:15:21 -0700362 SystemServicesProxy ssp = Recents.getSystemServices();
Winsond46b7272016-04-20 11:54:27 -0700363 MutableBoolean isHomeStackVisible = new MutableBoolean(true);
364 if (!ssp.isRecentsActivityVisible(isHomeStackVisible)) {
365 ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
Winson5b4e0d22016-02-16 18:11:35 -0800366 RecentsTaskLoader loader = Recents.getTaskLoader();
367 sInstanceLoadPlan = loader.createLoadPlan(mContext);
Winsondfd7be02016-05-10 13:30:37 -0700368 sInstanceLoadPlan.preloadRawTasks(!isHomeStackVisible.value);
369 loader.preloadTasks(sInstanceLoadPlan, runningTask.id, !isHomeStackVisible.value);
Winson190fe3bf2015-10-20 14:57:24 -0700370 TaskStack stack = sInstanceLoadPlan.getTaskStack();
Winson4b057c62016-01-12 15:01:52 -0800371 if (stack.getTaskCount() > 0) {
Winsonaeb298c2016-04-05 13:08:11 -0700372 // Only preload the icon (but not the thumbnail since it may not have been taken for
373 // the pausing activity)
Winsonc4a038a2016-05-26 16:42:15 -0700374 preloadIcon(runningTask.id);
Winsonaeb298c2016-04-05 13:08:11 -0700375
376 // At this point, we don't know anything about the stack state. So only calculate
377 // the dimensions of the thumbnail that we need for the transition into Recents, but
378 // do not draw it until we construct the activity options when we start Recents
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700379 updateHeaderBarLayout(stack, null /* window rect override*/);
Winson190fe3bf2015-10-20 14:57:24 -0700380 }
381 }
382 }
383
Winson190fe3bf2015-10-20 14:57:24 -0700384 public void cancelPreloadingRecents() {
385 // Do nothing
386 }
387
Jorim Jaggidd98d412015-11-18 15:57:38 -0800388 public void onDraggingInRecents(float distanceFromTop) {
389 EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEvent(distanceFromTop));
390 }
391
Jorim Jaggidd98d412015-11-18 15:57:38 -0800392 public void onDraggingInRecentsEnded(float velocity) {
393 EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEndedEvent(velocity));
394 }
395
Winson6b92c6e2015-11-06 13:11:16 -0800396 /**
397 * Transitions to the next recent task in the stack.
398 */
399 public void showNextTask() {
Winsone7f138c2015-10-22 16:15:21 -0700400 SystemServicesProxy ssp = Recents.getSystemServices();
Winson6b92c6e2015-11-06 13:11:16 -0800401 RecentsTaskLoader loader = Recents.getTaskLoader();
402 RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
Winsondfd7be02016-05-10 13:30:37 -0700403 loader.preloadTasks(plan, -1, false /* includeFrontMostExcludedTask */);
Winson6b92c6e2015-11-06 13:11:16 -0800404 TaskStack focusedStack = plan.getTaskStack();
405
406 // Return early if there are no tasks in the focused stack
Winson4b057c62016-01-12 15:01:52 -0800407 if (focusedStack == null || focusedStack.getTaskCount() == 0) return;
Winson6b92c6e2015-11-06 13:11:16 -0800408
Winson6b92c6e2015-11-06 13:11:16 -0800409 // Return early if there is no running task
Winsond46b7272016-04-20 11:54:27 -0700410 ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
Winson6b92c6e2015-11-06 13:11:16 -0800411 if (runningTask == null) return;
Winson6b92c6e2015-11-06 13:11:16 -0800412
413 // Find the task in the recents list
Winsond46b7272016-04-20 11:54:27 -0700414 boolean isRunningTaskInHomeStack = SystemServicesProxy.isHomeStack(runningTask.stackId);
Winson250608a2015-11-24 15:00:31 -0800415 ArrayList<Task> tasks = focusedStack.getStackTasks();
Winson6b92c6e2015-11-06 13:11:16 -0800416 Task toTask = null;
417 ActivityOptions launchOpts = null;
418 int taskCount = tasks.size();
419 for (int i = taskCount - 1; i >= 1; i--) {
420 Task task = tasks.get(i);
Winsond46b7272016-04-20 11:54:27 -0700421 if (isRunningTaskInHomeStack) {
Winsone86deb82015-11-12 09:32:10 -0800422 toTask = tasks.get(i - 1);
423 launchOpts = ActivityOptions.makeCustomAnimation(mContext,
424 R.anim.recents_launch_next_affiliated_task_target,
425 R.anim.recents_fast_toggle_app_home_exit);
426 break;
427 } else if (task.key.id == runningTask.id) {
Winson6b92c6e2015-11-06 13:11:16 -0800428 toTask = tasks.get(i - 1);
429 launchOpts = ActivityOptions.makeCustomAnimation(mContext,
430 R.anim.recents_launch_prev_affiliated_task_target,
431 R.anim.recents_launch_prev_affiliated_task_source);
432 break;
433 }
434 }
435
436 // Return early if there is no next task
437 if (toTask == null) {
438 ssp.startInPlaceAnimationOnFrontMostApplication(
439 ActivityOptions.makeCustomInPlaceAnimation(mContext,
440 R.anim.recents_launch_prev_affiliated_task_bounce));
441 return;
442 }
443
444 // Launch the task
Vladislav Kaznacheev1f6b7092016-05-04 17:18:56 -0700445 ssp.startActivityFromRecents(
446 mContext, toTask.key, toTask.title, launchOpts, INVALID_STACK_ID);
Winson6b92c6e2015-11-06 13:11:16 -0800447 }
448
449 /**
450 * Transitions to the next affiliated task.
451 */
452 public void showRelativeAffiliatedTask(boolean showNextTask) {
453 SystemServicesProxy ssp = Recents.getSystemServices();
Winsone7f138c2015-10-22 16:15:21 -0700454 RecentsTaskLoader loader = Recents.getTaskLoader();
Winson190fe3bf2015-10-20 14:57:24 -0700455 RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
Winsondfd7be02016-05-10 13:30:37 -0700456 loader.preloadTasks(plan, -1, false /* includeFrontMostExcludedTask */);
Winson190fe3bf2015-10-20 14:57:24 -0700457 TaskStack focusedStack = plan.getTaskStack();
458
459 // Return early if there are no tasks in the focused stack
Winson4b057c62016-01-12 15:01:52 -0800460 if (focusedStack == null || focusedStack.getTaskCount() == 0) return;
Winson190fe3bf2015-10-20 14:57:24 -0700461
Winson190fe3bf2015-10-20 14:57:24 -0700462 // Return early if there is no running task (can't determine affiliated tasks in this case)
Winsond46b7272016-04-20 11:54:27 -0700463 ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
Winson190fe3bf2015-10-20 14:57:24 -0700464 if (runningTask == null) return;
465 // Return early if the running task is in the home stack (optimization)
Winson5510f6c2015-10-27 12:11:26 -0700466 if (SystemServicesProxy.isHomeStack(runningTask.stackId)) return;
Winson190fe3bf2015-10-20 14:57:24 -0700467
468 // Find the task in the recents list
Winson250608a2015-11-24 15:00:31 -0800469 ArrayList<Task> tasks = focusedStack.getStackTasks();
Winson190fe3bf2015-10-20 14:57:24 -0700470 Task toTask = null;
471 ActivityOptions launchOpts = null;
472 int taskCount = tasks.size();
473 int numAffiliatedTasks = 0;
474 for (int i = 0; i < taskCount; i++) {
475 Task task = tasks.get(i);
476 if (task.key.id == runningTask.id) {
477 TaskGrouping group = task.group;
478 Task.TaskKey toTaskKey;
479 if (showNextTask) {
480 toTaskKey = group.getNextTaskInGroup(task);
481 launchOpts = ActivityOptions.makeCustomAnimation(mContext,
482 R.anim.recents_launch_next_affiliated_task_target,
483 R.anim.recents_launch_next_affiliated_task_source);
484 } else {
485 toTaskKey = group.getPrevTaskInGroup(task);
486 launchOpts = ActivityOptions.makeCustomAnimation(mContext,
487 R.anim.recents_launch_prev_affiliated_task_target,
488 R.anim.recents_launch_prev_affiliated_task_source);
489 }
490 if (toTaskKey != null) {
491 toTask = focusedStack.findTaskWithId(toTaskKey.id);
492 }
493 numAffiliatedTasks = group.getTaskCount();
494 break;
495 }
496 }
497
498 // Return early if there is no next task
499 if (toTask == null) {
500 if (numAffiliatedTasks > 1) {
501 if (showNextTask) {
Winsone7f138c2015-10-22 16:15:21 -0700502 ssp.startInPlaceAnimationOnFrontMostApplication(
Winson190fe3bf2015-10-20 14:57:24 -0700503 ActivityOptions.makeCustomInPlaceAnimation(mContext,
504 R.anim.recents_launch_next_affiliated_task_bounce));
505 } else {
Winsone7f138c2015-10-22 16:15:21 -0700506 ssp.startInPlaceAnimationOnFrontMostApplication(
Winson190fe3bf2015-10-20 14:57:24 -0700507 ActivityOptions.makeCustomInPlaceAnimation(mContext,
508 R.anim.recents_launch_prev_affiliated_task_bounce));
509 }
510 }
511 return;
512 }
513
514 // Keep track of actually launched affiliated tasks
515 MetricsLogger.count(mContext, "overview_affiliated_task_launch", 1);
516
517 // Launch the task
Vladislav Kaznacheev1f6b7092016-05-04 17:18:56 -0700518 ssp.startActivityFromRecents(
519 mContext, toTask.key, toTask.title, launchOpts, INVALID_STACK_ID);
Winson190fe3bf2015-10-20 14:57:24 -0700520 }
521
522 public void showNextAffiliatedTask() {
523 // Keep track of when the affiliated task is triggered
524 MetricsLogger.count(mContext, "overview_affiliated_task_next", 1);
525 showRelativeAffiliatedTask(true);
526 }
527
528 public void showPrevAffiliatedTask() {
529 // Keep track of when the affiliated task is triggered
530 MetricsLogger.count(mContext, "overview_affiliated_task_prev", 1);
531 showRelativeAffiliatedTask(false);
532 }
533
Jorim Jaggicdb06ca2016-01-25 19:15:12 -0800534 public void dockTopTask(int topTaskId, int dragMode,
535 int stackCreateMode, Rect initialBounds) {
Jorim Jaggi75b25972015-10-21 14:51:10 +0200536 SystemServicesProxy ssp = Recents.getSystemServices();
Jorim Jaggi9511b0f2016-01-29 19:12:44 -0800537
538 // Make sure we inform DividerView before we actually start the activity so we can change
539 // the resize mode already.
Chong Zhange4fbd322016-03-01 14:44:03 -0800540 if (ssp.moveTaskToDockedStack(topTaskId, stackCreateMode, initialBounds)) {
Jorim Jaggi899327f2016-02-25 20:44:18 -0500541 EventBus.getDefault().send(new DockedTopTaskEvent(dragMode, initialBounds));
Chong Zhange4fbd322016-03-01 14:44:03 -0800542 showRecents(
543 false /* triggeredFromAltTab */,
544 dragMode == NavigationBarGestureHelper.DRAG_MODE_RECENTS,
545 false /* animate */,
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700546 true /* launchedWhileDockingTask*/,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700547 false /* fromHome */,
548 DividerView.INVALID_RECENTS_GROW_TARGET);
Chong Zhange4fbd322016-03-01 14:44:03 -0800549 }
Jorim Jaggi75b25972015-10-21 14:51:10 +0200550 }
551
Winson190fe3bf2015-10-20 14:57:24 -0700552 /**
553 * Returns the preloaded load plan and invalidates it.
554 */
555 public static RecentsTaskLoadPlan consumeInstanceLoadPlan() {
556 RecentsTaskLoadPlan plan = sInstanceLoadPlan;
557 sInstanceLoadPlan = null;
558 return plan;
559 }
560
561 /**
Winson670ea712016-04-12 17:02:26 -0700562 * Reloads all the resources for the current configuration.
Winsonb94443d2016-01-07 15:34:13 -0800563 */
Winson670ea712016-04-12 17:02:26 -0700564 private void reloadResources() {
Winsonb94443d2016-01-07 15:34:13 -0800565 Resources res = mContext.getResources();
Winsonb94443d2016-01-07 15:34:13 -0800566
567 mStatusBarHeight = res.getDimensionPixelSize(
568 com.android.internal.R.dimen.status_bar_height);
569 mNavBarHeight = res.getDimensionPixelSize(
570 com.android.internal.R.dimen.navigation_bar_height);
571 mNavBarWidth = res.getDimensionPixelSize(
572 com.android.internal.R.dimen.navigation_bar_width);
Jorim Jaggi25160db2016-04-18 16:03:36 -0700573 mTaskBarHeight = TaskStackLayoutAlgorithm.getDimensionForDevice(mContext,
Winson21700932016-03-24 17:26:23 -0700574 R.dimen.recents_task_view_header_height,
575 R.dimen.recents_task_view_header_height,
576 R.dimen.recents_task_view_header_height,
577 R.dimen.recents_task_view_header_height_tablet_land,
578 R.dimen.recents_task_view_header_height,
579 R.dimen.recents_task_view_header_height_tablet_land);
Winsonb94443d2016-01-07 15:34:13 -0800580 }
581
582 /**
Winson190fe3bf2015-10-20 14:57:24 -0700583 * Prepares the header bar layout for the next transition, if the task view bounds has changed
584 * since the last call, it will attempt to re-measure and layout the header bar to the new size.
585 *
Winsonf0d1c442015-12-01 11:04:45 -0800586 * @param stack the stack to initialize the stack layout with
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700587 * @param windowRectOverride the rectangle to use when calculating the stack state which can
588 * be different from the current window rect if recents is resizing
589 * while being launched
Winson190fe3bf2015-10-20 14:57:24 -0700590 */
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700591 private void updateHeaderBarLayout(TaskStack stack, Rect windowRectOverride) {
Winsone7f138c2015-10-22 16:15:21 -0700592 SystemServicesProxy ssp = Recents.getSystemServices();
Winsonfc48b072016-04-21 11:20:11 -0700593 Rect displayRect = ssp.getDisplayRect();
Jorim Jaggic6c89a82016-01-28 17:48:21 -0800594 Rect systemInsets = new Rect();
595 ssp.getStableInsets(systemInsets);
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700596 Rect windowRect = windowRectOverride != null
597 ? new Rect(windowRectOverride)
598 : ssp.getWindowRect();
Winsoncf9b8322016-03-31 15:36:07 -0700599 // When docked, the nav bar insets are consumed and the activity is measured without insets.
600 // However, the window bounds include the insets, so we need to subtract them here to make
601 // them identical.
602 if (ssp.hasDockedTask()) {
603 windowRect.bottom -= systemInsets.bottom;
604 systemInsets.bottom = 0;
605 }
Jorim Jaggic6c89a82016-01-28 17:48:21 -0800606 calculateWindowStableInsets(systemInsets, windowRect);
607 windowRect.offsetTo(0, 0);
Winson190fe3bf2015-10-20 14:57:24 -0700608
Winson59924fe2016-03-17 14:13:18 -0700609 TaskStackLayoutAlgorithm stackLayout = mDummyStackView.getStackAlgorithm();
Winson190fe3bf2015-10-20 14:57:24 -0700610
611 // Rebind the header bar and draw it for the transition
Winson88737542016-02-17 13:27:33 -0800612 stackLayout.setSystemInsets(systemInsets);
Winsonf0d1c442015-12-01 11:04:45 -0800613 if (stack != null) {
Winsonfc48b072016-04-21 11:20:11 -0700614 stackLayout.getTaskStackBounds(displayRect, windowRect, systemInsets.top,
615 systemInsets.right, mTaskStackBounds);
Winsonaeb298c2016-04-05 13:08:11 -0700616 stackLayout.reset();
Winsonfc48b072016-04-21 11:20:11 -0700617 stackLayout.initialize(displayRect, windowRect, mTaskStackBounds,
Winsonf0d1c442015-12-01 11:04:45 -0800618 TaskStackLayoutAlgorithm.StackState.getStackStateForStack(stack));
Winsona1ededd2016-03-25 12:23:12 -0700619 mDummyStackView.setTasks(stack, false /* allowNotifyStackChanges */);
Winson190fe3bf2015-10-20 14:57:24 -0700620
Winsonaeb298c2016-04-05 13:08:11 -0700621 Rect taskViewBounds = stackLayout.getUntransformedTaskViewBounds();
Winson94bc4f22016-04-07 14:22:12 -0700622 if (!taskViewBounds.isEmpty()) {
623 int taskViewWidth = taskViewBounds.width();
624 synchronized (mHeaderBarLock) {
625 if (mHeaderBar.getMeasuredWidth() != taskViewWidth ||
626 mHeaderBar.getMeasuredHeight() != mTaskBarHeight) {
627 mHeaderBar.measure(
628 MeasureSpec.makeMeasureSpec(taskViewWidth, MeasureSpec.EXACTLY),
629 MeasureSpec.makeMeasureSpec(mTaskBarHeight, MeasureSpec.EXACTLY));
630 }
631 mHeaderBar.layout(0, 0, taskViewWidth, mTaskBarHeight);
Winsonaeb298c2016-04-05 13:08:11 -0700632 }
Winsonaeb298c2016-04-05 13:08:11 -0700633
Winson94bc4f22016-04-07 14:22:12 -0700634 // Update the transition bitmap to match the new header bar height
635 if (mThumbTransitionBitmapCache == null ||
636 (mThumbTransitionBitmapCache.getWidth() != taskViewWidth) ||
637 (mThumbTransitionBitmapCache.getHeight() != mTaskBarHeight)) {
638 mThumbTransitionBitmapCache = Bitmap.createBitmap(taskViewWidth,
639 mTaskBarHeight, Bitmap.Config.ARGB_8888);
640 }
Winsonaeb298c2016-04-05 13:08:11 -0700641 }
Winson190fe3bf2015-10-20 14:57:24 -0700642 }
643 }
644
645 /**
Jorim Jaggic6c89a82016-01-28 17:48:21 -0800646 * Given the stable insets and the rect for our window, calculates the insets that affect our
647 * window.
648 */
649 private void calculateWindowStableInsets(Rect inOutInsets, Rect windowRect) {
650 Rect displayRect = Recents.getSystemServices().getDisplayRect();
651
652 // Display rect without insets - available app space
653 Rect appRect = new Rect(displayRect);
654 appRect.inset(inOutInsets);
655
656 // Our window intersected with available app space
657 Rect windowRectWithInsets = new Rect(windowRect);
658 windowRectWithInsets.intersect(appRect);
659 inOutInsets.left = windowRectWithInsets.left - windowRect.left;
660 inOutInsets.top = windowRectWithInsets.top - windowRect.top;
661 inOutInsets.right = windowRect.right - windowRectWithInsets.right;
662 inOutInsets.bottom = windowRect.bottom - windowRectWithInsets.bottom;
663 }
664
665 /**
Winson190fe3bf2015-10-20 14:57:24 -0700666 * Preloads the icon of a task.
667 */
Winsonc4a038a2016-05-26 16:42:15 -0700668 private void preloadIcon(int runningTaskId) {
Winson190fe3bf2015-10-20 14:57:24 -0700669 // Ensure that we load the running task's icon
670 RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
Winsonc4a038a2016-05-26 16:42:15 -0700671 launchOpts.runningTaskId = runningTaskId;
Winson190fe3bf2015-10-20 14:57:24 -0700672 launchOpts.loadThumbnails = false;
673 launchOpts.onlyLoadForCache = true;
Winsone7f138c2015-10-22 16:15:21 -0700674 Recents.getTaskLoader().loadTasks(mContext, sInstanceLoadPlan, launchOpts);
Winson190fe3bf2015-10-20 14:57:24 -0700675 }
676
677 /**
Winson190fe3bf2015-10-20 14:57:24 -0700678 * Creates the activity options for a unknown state->recents transition.
679 */
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700680 protected ActivityOptions getUnknownTransitionActivityOptions() {
Winson190fe3bf2015-10-20 14:57:24 -0700681 return ActivityOptions.makeCustomAnimation(mContext,
682 R.anim.recents_from_unknown_enter,
683 R.anim.recents_from_unknown_exit,
Winson3fb67562015-11-11 10:39:03 -0800684 mHandler, null);
Winson190fe3bf2015-10-20 14:57:24 -0700685 }
686
687 /**
688 * Creates the activity options for a home->recents transition.
689 */
Winson008ee15f2016-03-18 17:17:25 -0700690 protected ActivityOptions getHomeTransitionActivityOptions() {
Winson190fe3bf2015-10-20 14:57:24 -0700691 return ActivityOptions.makeCustomAnimation(mContext,
692 R.anim.recents_from_launcher_enter,
693 R.anim.recents_from_launcher_exit,
Winson3fb67562015-11-11 10:39:03 -0800694 mHandler, null);
Winson190fe3bf2015-10-20 14:57:24 -0700695 }
696
697 /**
698 * Creates the activity options for an app->recents transition.
699 */
700 private ActivityOptions getThumbnailTransitionActivityOptions(
Winsond46b7272016-04-20 11:54:27 -0700701 ActivityManager.RunningTaskInfo runningTask, TaskStackView stackView,
702 Rect windowOverrideRect) {
Winsonc4a038a2016-05-26 16:42:15 -0700703 if (runningTask != null && runningTask.stackId == FREEFORM_WORKSPACE_STACK_ID) {
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700704 ArrayList<AppTransitionAnimationSpec> specs = new ArrayList<>();
Winsone693aaf2016-03-01 12:05:59 -0800705 ArrayList<Task> tasks = stackView.getStack().getStackTasks();
706 TaskStackLayoutAlgorithm stackLayout = stackView.getStackAlgorithm();
707 TaskStackViewScroller stackScroller = stackView.getScroller();
708
Winson003eda62016-03-11 14:56:00 -0800709 stackView.updateLayoutAlgorithm(true /* boundScroll */);
Winson67c79572016-04-13 14:02:18 -0700710 stackView.updateToInitialState();
Winsone693aaf2016-03-01 12:05:59 -0800711
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700712 for (int i = tasks.size() - 1; i >= 0; i--) {
713 Task task = tasks.get(i);
Winson387aac62015-11-25 11:18:56 -0800714 if (task.isFreeformTask()) {
Winsone693aaf2016-03-01 12:05:59 -0800715 mTmpTransform = stackLayout.getStackTransformScreenCoordinates(task,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700716 stackScroller.getStackScroll(), mTmpTransform, null,
717 windowOverrideRect);
Winsonaeb298c2016-04-05 13:08:11 -0700718 Bitmap thumbnail = drawThumbnailTransitionBitmap(task, mTmpTransform,
719 mThumbTransitionBitmapCache);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700720 Rect toTaskRect = new Rect();
721 mTmpTransform.rect.round(toTaskRect);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700722 specs.add(new AppTransitionAnimationSpec(task.key.id, thumbnail, toTaskRect));
723 }
724 }
725 AppTransitionAnimationSpec[] specsArray = new AppTransitionAnimationSpec[specs.size()];
726 specs.toArray(specsArray);
727 return ActivityOptions.makeThumbnailAspectScaleDownAnimation(mDummyStackView,
Winson3fb67562015-11-11 10:39:03 -0800728 specsArray, mHandler, null, this);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700729 } else {
730 // Update the destination rect
731 Task toTask = new Task();
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700732 TaskViewTransform toTransform = getThumbnailTransitionTransform(stackView, toTask,
733 windowOverrideRect);
Winsonaeb298c2016-04-05 13:08:11 -0700734 Bitmap thumbnail = drawThumbnailTransitionBitmap(toTask, toTransform,
735 mThumbTransitionBitmapCache);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700736 if (thumbnail != null) {
Winsonaeb298c2016-04-05 13:08:11 -0700737 RectF toTaskRect = toTransform.rect;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700738 return ActivityOptions.makeThumbnailAspectScaleDownAnimation(mDummyStackView,
739 thumbnail, (int) toTaskRect.left, (int) toTaskRect.top,
Winson3fb67562015-11-11 10:39:03 -0800740 (int) toTaskRect.width(), (int) toTaskRect.height(), mHandler, null);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700741 }
742 // If both the screenshot and thumbnail fails, then just fall back to the default transition
743 return getUnknownTransitionActivityOptions();
744 }
745 }
Winson190fe3bf2015-10-20 14:57:24 -0700746
Winson190fe3bf2015-10-20 14:57:24 -0700747 /**
748 * Returns the transition rect for the given task id.
749 */
Winsone693aaf2016-03-01 12:05:59 -0800750 private TaskViewTransform getThumbnailTransitionTransform(TaskStackView stackView,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700751 Task runningTaskOut, Rect windowOverrideRect) {
Winson190fe3bf2015-10-20 14:57:24 -0700752 // Find the running task in the TaskStack
Winsone693aaf2016-03-01 12:05:59 -0800753 TaskStack stack = stackView.getStack();
Winson65c851e2016-01-20 12:43:35 -0800754 Task launchTask = stack.getLaunchTarget();
755 if (launchTask != null) {
756 runningTaskOut.copyFrom(launchTask);
757 } else {
Winson190fe3bf2015-10-20 14:57:24 -0700758 // If no task is specified or we can not find the task just use the front most one
Winson35a8b042016-01-22 09:41:09 -0800759 launchTask = stack.getStackFrontMostTask(true /* includeFreeform */);
Winson65c851e2016-01-20 12:43:35 -0800760 runningTaskOut.copyFrom(launchTask);
Winson190fe3bf2015-10-20 14:57:24 -0700761 }
762
763 // Get the transform for the running task
Winson003eda62016-03-11 14:56:00 -0800764 stackView.updateLayoutAlgorithm(true /* boundScroll */);
Winson67c79572016-04-13 14:02:18 -0700765 stackView.updateToInitialState();
Winson7845e8c2016-03-29 10:23:19 -0700766 stackView.getStackAlgorithm().getStackTransformScreenCoordinates(launchTask,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700767 stackView.getScroller().getStackScroll(), mTmpTransform, null, windowOverrideRect);
Winson190fe3bf2015-10-20 14:57:24 -0700768 return mTmpTransform;
769 }
770
771 /**
772 * Draws the header of a task used for the window animation into a bitmap.
773 */
Winsonaeb298c2016-04-05 13:08:11 -0700774 private Bitmap drawThumbnailTransitionBitmap(Task toTask, TaskViewTransform toTransform,
775 Bitmap thumbnail) {
Winson8be16342016-02-09 11:53:27 -0800776 SystemServicesProxy ssp = Recents.getSystemServices();
Winson190fe3bf2015-10-20 14:57:24 -0700777 if (toTransform != null && toTask.key != null) {
Winson190fe3bf2015-10-20 14:57:24 -0700778 synchronized (mHeaderBarLock) {
Winson8be16342016-02-09 11:53:27 -0800779 boolean disabledInSafeMode = !toTask.isSystemApp && ssp.isInSafeMode();
Winson Chung509d0d02015-12-16 15:43:12 -0500780 mHeaderBar.onTaskViewSizeChanged((int) toTransform.rect.width(),
781 (int) toTransform.rect.height());
Winsonc742f972015-11-12 11:32:21 -0800782 if (RecentsDebugFlags.Static.EnableTransitionThumbnailDebugMode) {
Winson190fe3bf2015-10-20 14:57:24 -0700783 thumbnail.eraseColor(0xFFff0000);
784 } else {
Winsonaeb298c2016-04-05 13:08:11 -0700785 thumbnail.eraseColor(0);
Winson190fe3bf2015-10-20 14:57:24 -0700786 Canvas c = new Canvas(thumbnail);
Winson22574af2016-03-23 19:00:28 -0700787 // Workaround for b/27815919, reset the callback so that we do not trigger an
788 // invalidate on the header bar as a result of updating the icon
789 Drawable icon = mHeaderBar.getIconView().getDrawable();
790 if (icon != null) {
791 icon.setCallback(null);
792 }
Winsond2a03062016-04-15 11:19:07 -0700793 mHeaderBar.bindToTask(toTask, false /* touchExplorationEnabled */,
Winson8be16342016-02-09 11:53:27 -0800794 disabledInSafeMode);
Winsond2a03062016-04-15 11:19:07 -0700795 mHeaderBar.onTaskDataLoaded();
Winsone693aaf2016-03-01 12:05:59 -0800796 mHeaderBar.setDimAlpha(toTransform.dimAlpha);
Winson190fe3bf2015-10-20 14:57:24 -0700797 mHeaderBar.draw(c);
798 c.setBitmap(null);
799 }
800 }
801 return thumbnail.createAshmemBitmap();
802 }
803 return null;
804 }
805
806 /**
807 * Shows the recents activity
808 */
Winsond46b7272016-04-20 11:54:27 -0700809 protected void startRecentsActivity(ActivityManager.RunningTaskInfo runningTask,
810 boolean isHomeStackVisible, boolean animate, int growTarget) {
Winsone7f138c2015-10-22 16:15:21 -0700811 RecentsTaskLoader loader = Recents.getTaskLoader();
Winsonc5b12dd2016-03-23 20:25:12 -0700812 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
Winson190fe3bf2015-10-20 14:57:24 -0700813
Winsonc4a038a2016-05-26 16:42:15 -0700814 int runningTaskId = !mLaunchedWhileDocking && (runningTask != null)
815 ? runningTask.id
816 : -1;
817
Winsone5f1faa2015-11-20 12:26:23 -0800818 // In the case where alt-tab is triggered, we never get a preloadRecents() call, so we
Jorim Jaggi435b2e42015-11-24 15:09:30 -0800819 // should always preload the tasks now. If we are dragging in recents, reload them as
820 // the stacks might have changed.
Jorim Jaggie161f082016-02-05 14:26:16 -0800821 if (mLaunchedWhileDocking || mTriggeredFromAltTab ||sInstanceLoadPlan == null) {
Winsone5f1faa2015-11-20 12:26:23 -0800822 // Create a new load plan if preloadRecents() was never triggered
Winson190fe3bf2015-10-20 14:57:24 -0700823 sInstanceLoadPlan = loader.createLoadPlan(mContext);
824 }
Jorim Jaggie161f082016-02-05 14:26:16 -0800825 if (mLaunchedWhileDocking || mTriggeredFromAltTab || !sInstanceLoadPlan.hasTasks()) {
Winsonc4a038a2016-05-26 16:42:15 -0700826 loader.preloadTasks(sInstanceLoadPlan, runningTaskId, !isHomeStackVisible);
Winson190fe3bf2015-10-20 14:57:24 -0700827 }
Winsonc5b12dd2016-03-23 20:25:12 -0700828
Winson190fe3bf2015-10-20 14:57:24 -0700829 TaskStack stack = sInstanceLoadPlan.getTaskStack();
Winsonc5b12dd2016-03-23 20:25:12 -0700830 boolean hasRecentTasks = stack.getTaskCount() > 0;
Winsond46b7272016-04-20 11:54:27 -0700831 boolean useThumbnailTransition = (runningTask != null) && !isHomeStackVisible && hasRecentTasks;
Winson190fe3bf2015-10-20 14:57:24 -0700832
Winsonaeb298c2016-04-05 13:08:11 -0700833 // Update the launch state that we need in updateHeaderBarLayout()
Jorim Jaggic5887ea2016-05-13 18:21:48 -0700834 launchState.launchedFromHome = !useThumbnailTransition && !mLaunchedWhileDocking;
Winsonaeb298c2016-04-05 13:08:11 -0700835 launchState.launchedFromApp = useThumbnailTransition || mLaunchedWhileDocking;
836 launchState.launchedViaDockGesture = mLaunchedWhileDocking;
837 launchState.launchedViaDragGesture = mDraggingInRecents;
Winsonc4a038a2016-05-26 16:42:15 -0700838 launchState.launchedToTaskId = runningTaskId;
Winsonaeb298c2016-04-05 13:08:11 -0700839 launchState.launchedWithAltTab = mTriggeredFromAltTab;
840
841 // Preload the icon (this will be a null-op if we have preloaded the icon already in
842 // preloadRecents())
Winsonc4a038a2016-05-26 16:42:15 -0700843 preloadIcon(runningTaskId);
Winsonaeb298c2016-04-05 13:08:11 -0700844
Winsonf0d1c442015-12-01 11:04:45 -0800845 // Update the header bar if necessary
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700846 Rect windowOverrideRect = getWindowRectOverride(growTarget);
847 updateHeaderBarLayout(stack, windowOverrideRect);
Winsonf0d1c442015-12-01 11:04:45 -0800848
Winson190fe3bf2015-10-20 14:57:24 -0700849 // Prepare the dummy stack for the transition
Winson36a5a2c2015-10-29 18:04:39 -0700850 TaskStackLayoutAlgorithm.VisibilityReport stackVr =
Winson190fe3bf2015-10-20 14:57:24 -0700851 mDummyStackView.computeStackVisibilityReport();
Jorim Jaggibb42a462015-11-20 16:27:16 -0800852
Winsonaeb298c2016-04-05 13:08:11 -0700853 // Update the remaining launch state
Winsonc5b12dd2016-03-23 20:25:12 -0700854 launchState.launchedNumVisibleTasks = stackVr.numVisibleTasks;
855 launchState.launchedNumVisibleThumbnails = stackVr.numVisibleThumbnails;
Winsonc5b12dd2016-03-23 20:25:12 -0700856
Jorim Jaggibb42a462015-11-20 16:27:16 -0800857 if (!animate) {
Winsonc5b12dd2016-03-23 20:25:12 -0700858 startRecentsActivity(ActivityOptions.makeCustomAnimation(mContext, -1, -1));
Jorim Jaggibb42a462015-11-20 16:27:16 -0800859 return;
860 }
861
Winsonaeb298c2016-04-05 13:08:11 -0700862 ActivityOptions opts;
Winson190fe3bf2015-10-20 14:57:24 -0700863 if (useThumbnailTransition) {
864 // Try starting with a thumbnail transition
Winsond46b7272016-04-20 11:54:27 -0700865 opts = getThumbnailTransitionActivityOptions(runningTask, mDummyStackView,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700866 windowOverrideRect);
Winsonaeb298c2016-04-05 13:08:11 -0700867 } else {
Winson190fe3bf2015-10-20 14:57:24 -0700868 // If there is no thumbnail transition, but is launching from home into recents, then
Winson008ee15f2016-03-18 17:17:25 -0700869 // use a quick home transition
Winsonaeb298c2016-04-05 13:08:11 -0700870 opts = hasRecentTasks
871 ? getHomeTransitionActivityOptions()
872 : getUnknownTransitionActivityOptions();
Winson190fe3bf2015-10-20 14:57:24 -0700873 }
Winsonaeb298c2016-04-05 13:08:11 -0700874 startRecentsActivity(opts);
Winson190fe3bf2015-10-20 14:57:24 -0700875 mLastToggleTime = SystemClock.elapsedRealtime();
876 }
877
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700878 private Rect getWindowRectOverride(int growTarget) {
879 if (growTarget == DividerView.INVALID_RECENTS_GROW_TARGET) {
880 return null;
881 }
882 Rect result = new Rect();
883 Rect displayRect = Recents.getSystemServices().getDisplayRect();
884 DockedDividerUtils.calculateBoundsForPosition(growTarget, WindowManager.DOCKED_BOTTOM,
885 result, displayRect.width(), displayRect.height(),
886 Recents.getSystemServices().getDockedDividerSize(mContext));
887 return result;
888 }
889
Winson190fe3bf2015-10-20 14:57:24 -0700890 /**
891 * Starts the recents activity.
892 */
Winsonc5b12dd2016-03-23 20:25:12 -0700893 private void startRecentsActivity(ActivityOptions opts) {
Winson190fe3bf2015-10-20 14:57:24 -0700894 Intent intent = new Intent();
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700895 intent.setClassName(RECENTS_PACKAGE, RECENTS_ACTIVITY);
Winson190fe3bf2015-10-20 14:57:24 -0700896 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
897 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
898 | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
Sid Soundararajanb58c46a2016-01-26 15:39:27 -0800899
Winson190fe3bf2015-10-20 14:57:24 -0700900 if (opts != null) {
901 mContext.startActivityAsUser(intent, opts.toBundle(), UserHandle.CURRENT);
902 } else {
903 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
904 }
Jorim Jaggicdb06ca2016-01-25 19:15:12 -0800905 EventBus.getDefault().send(new RecentsActivityStartingEvent());
Winson190fe3bf2015-10-20 14:57:24 -0700906 }
907
Winson3fb67562015-11-11 10:39:03 -0800908 /**** OnAnimationFinishedListener Implementation ****/
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -0700909
910 @Override
911 public void onAnimationFinished() {
912 EventBus.getDefault().post(new EnterRecentsWindowLastAnimationFrameEvent());
913 }
Winson190fe3bf2015-10-20 14:57:24 -0700914}