blob: 957c94b124cbd63f0625c2d75f012219a3609694 [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;
Winson94bc4f22016-04-07 14:22:12 -070020import static android.view.View.MeasureSpec;
Winsone693aaf2016-03-01 12:05:59 -080021
Winson190fe3bf2015-10-20 14:57:24 -070022import android.app.ActivityManager;
23import android.app.ActivityOptions;
Winson190fe3bf2015-10-20 14:57:24 -070024import android.content.ActivityNotFoundException;
Winson190fe3bf2015-10-20 14:57:24 -070025import android.content.Context;
26import android.content.Intent;
27import android.content.res.Resources;
28import android.graphics.Bitmap;
29import android.graphics.Canvas;
30import android.graphics.Rect;
Winson3150e572015-10-23 15:07:24 -070031import android.graphics.RectF;
Winson22574af2016-03-23 19:00:28 -070032import android.graphics.drawable.Drawable;
Winson190fe3bf2015-10-20 14:57:24 -070033import android.os.Handler;
34import android.os.SystemClock;
35import android.os.UserHandle;
Winson1b585612015-11-06 09:16:26 -080036import android.util.Log;
Winson190fe3bf2015-10-20 14:57:24 -070037import android.util.MutableBoolean;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -070038import android.view.AppTransitionAnimationSpec;
Winson190fe3bf2015-10-20 14:57:24 -070039import android.view.LayoutInflater;
Winsonb61e6542016-02-04 14:37:18 -080040import android.view.ViewConfiguration;
Winsonc0d70582016-01-29 10:24:39 -080041
Winson190fe3bf2015-10-20 14:57:24 -070042import com.android.internal.logging.MetricsLogger;
Winson190fe3bf2015-10-20 14:57:24 -070043import com.android.systemui.R;
44import com.android.systemui.SystemUIApplication;
Winson412e1802015-10-20 16:57:57 -070045import com.android.systemui.recents.events.EventBus;
Jorim Jaggi899327f2016-02-25 20:44:18 -050046import com.android.systemui.recents.events.activity.DockedTopTaskEvent;
Winson1b585612015-11-06 09:16:26 -080047import com.android.systemui.recents.events.activity.EnterRecentsWindowLastAnimationFrameEvent;
Jorim Jaggi2adba072016-03-03 13:43:39 +010048import com.android.systemui.recents.events.activity.ForcedResizableEvent;
Winson412e1802015-10-20 16:57:57 -070049import com.android.systemui.recents.events.activity.HideRecentsEvent;
Winson0d14d4d2015-10-26 17:05:04 -070050import com.android.systemui.recents.events.activity.IterateRecentsEvent;
Winsonb61e6542016-02-04 14:37:18 -080051import com.android.systemui.recents.events.activity.LaunchNextTaskRequestEvent;
Jorim Jaggicdb06ca2016-01-25 19:15:12 -080052import com.android.systemui.recents.events.activity.RecentsActivityStartingEvent;
Winson412e1802015-10-20 16:57:57 -070053import com.android.systemui.recents.events.activity.ToggleRecentsEvent;
Winson190fe3bf2015-10-20 14:57:24 -070054import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
55import com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
Jorim Jaggidd98d412015-11-18 15:57:38 -080056import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
57import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
Winson6b92c6e2015-11-06 13:11:16 -080058import com.android.systemui.recents.misc.DozeTrigger;
Winsonab84fc52015-10-23 11:52:07 -070059import com.android.systemui.recents.misc.ForegroundThread;
Winson190fe3bf2015-10-20 14:57:24 -070060import com.android.systemui.recents.misc.SystemServicesProxy;
Jaewan Kim938a50b2016-03-14 17:35:43 +090061import com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener;
Winson190fe3bf2015-10-20 14:57:24 -070062import com.android.systemui.recents.model.RecentsTaskLoadPlan;
63import com.android.systemui.recents.model.RecentsTaskLoader;
64import com.android.systemui.recents.model.Task;
65import com.android.systemui.recents.model.TaskGrouping;
66import com.android.systemui.recents.model.TaskStack;
Winson36a5a2c2015-10-29 18:04:39 -070067import com.android.systemui.recents.views.TaskStackLayoutAlgorithm;
Winson1b585612015-11-06 09:16:26 -080068import com.android.systemui.recents.views.TaskStackView;
Winsone693aaf2016-03-01 12:05:59 -080069import com.android.systemui.recents.views.TaskStackViewScroller;
Winson190fe3bf2015-10-20 14:57:24 -070070import com.android.systemui.recents.views.TaskViewHeader;
71import com.android.systemui.recents.views.TaskViewTransform;
Winsond8b1d632016-01-04 17:51:18 -080072import com.android.systemui.statusbar.BaseStatusBar;
Jorim Jaggicdb06ca2016-01-25 19:15:12 -080073import com.android.systemui.statusbar.phone.NavigationBarGestureHelper;
Winson190fe3bf2015-10-20 14:57:24 -070074import com.android.systemui.statusbar.phone.PhoneStatusBar;
75
76import java.util.ArrayList;
77
78/**
79 * An implementation of the Recents component for the current user. For secondary users, this can
80 * be called remotely from the system user.
81 */
Jorim Jaggicdb06ca2016-01-25 19:15:12 -080082public class RecentsImpl implements ActivityOptions.OnAnimationFinishedListener {
Winson190fe3bf2015-10-20 14:57:24 -070083
84 private final static String TAG = "RecentsImpl";
Winsonb61e6542016-02-04 14:37:18 -080085
Winson6b92c6e2015-11-06 13:11:16 -080086 // The minimum amount of time between each recents button press that we will handle
87 private final static int MIN_TOGGLE_DELAY_MS = 350;
Winsonb61e6542016-02-04 14:37:18 -080088
Winson6b92c6e2015-11-06 13:11:16 -080089 // The duration within which the user releasing the alt tab (from when they pressed alt tab)
90 // that the fast alt-tab animation will run. If the user's alt-tab takes longer than this
91 // duration, then we will toggle recents after this duration.
92 private final static int FAST_ALT_TAB_DELAY_MS = 225;
Winson190fe3bf2015-10-20 14:57:24 -070093
94 public final static String RECENTS_PACKAGE = "com.android.systemui";
95 public final static String RECENTS_ACTIVITY = "com.android.systemui.recents.RecentsActivity";
Winsone693aaf2016-03-01 12:05:59 -080096
Winson190fe3bf2015-10-20 14:57:24 -070097 /**
Jaewan Kim938a50b2016-03-14 17:35:43 +090098 * An implementation of TaskStackListener, that allows us to listen for changes to the system
Winson190fe3bf2015-10-20 14:57:24 -070099 * task stacks and update recents accordingly.
100 */
Jaewan Kim938a50b2016-03-14 17:35:43 +0900101 class TaskStackListenerImpl extends TaskStackListener {
Winson190fe3bf2015-10-20 14:57:24 -0700102 @Override
103 public void onTaskStackChanged() {
Jaewan Kim938a50b2016-03-14 17:35:43 +0900104 // Preloads the next task
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100105 RecentsConfiguration config = Recents.getConfiguration();
Winson190fe3bf2015-10-20 14:57:24 -0700106 if (config.svelteLevel == RecentsConfiguration.SVELTE_NONE) {
Winsone7f138c2015-10-22 16:15:21 -0700107 RecentsTaskLoader loader = Recents.getTaskLoader();
108 SystemServicesProxy ssp = Recents.getSystemServices();
Winson190fe3bf2015-10-20 14:57:24 -0700109 ActivityManager.RunningTaskInfo runningTaskInfo = ssp.getTopMostTask();
110
111 // Load the next task only if we aren't svelte
112 RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
Winson65c851e2016-01-20 12:43:35 -0800113 loader.preloadTasks(plan, -1, true /* isTopTaskHome */);
Winson190fe3bf2015-10-20 14:57:24 -0700114 RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
115 // This callback is made when a new activity is launched and the old one is paused
116 // so ignore the current activity and try and preload the thumbnail for the
117 // previous one.
118 if (runningTaskInfo != null) {
119 launchOpts.runningTaskId = runningTaskInfo.id;
120 }
121 launchOpts.numVisibleTasks = 2;
122 launchOpts.numVisibleTaskThumbnails = 2;
123 launchOpts.onlyLoadForCache = true;
124 launchOpts.onlyLoadPausedActivities = true;
125 loader.loadTasks(mContext, plan, launchOpts);
126 }
Winson190fe3bf2015-10-20 14:57:24 -0700127 }
Jorim Jaggi2adba072016-03-03 13:43:39 +0100128
129 @Override
130 public void onActivityForcedResizable(String packageName, int taskId) {
131 EventBus.getDefault().sendOntoMainThread(
132 new ForcedResizableEvent(packageName, taskId));
133
134 }
Winson190fe3bf2015-10-20 14:57:24 -0700135 }
136
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700137 protected static RecentsTaskLoadPlan sInstanceLoadPlan;
Winson190fe3bf2015-10-20 14:57:24 -0700138
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700139 protected Context mContext;
140 protected Handler mHandler;
Winson190fe3bf2015-10-20 14:57:24 -0700141 TaskStackListenerImpl mTaskStackListener;
Jorim Jaggidd98d412015-11-18 15:57:38 -0800142 boolean mDraggingInRecents;
Jorim Jaggie161f082016-02-05 14:26:16 -0800143 boolean mLaunchedWhileDocking;
Winson190fe3bf2015-10-20 14:57:24 -0700144
145 // Task launching
Winson190fe3bf2015-10-20 14:57:24 -0700146 Rect mTaskStackBounds = new Rect();
Winson190fe3bf2015-10-20 14:57:24 -0700147 TaskViewTransform mTmpTransform = new TaskViewTransform();
148 int mStatusBarHeight;
149 int mNavBarHeight;
150 int mNavBarWidth;
151 int mTaskBarHeight;
152
153 // Header (for transition)
154 TaskViewHeader mHeaderBar;
155 final Object mHeaderBarLock = new Object();
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700156 protected TaskStackView mDummyStackView;
Winson190fe3bf2015-10-20 14:57:24 -0700157
158 // Variables to keep track of if we need to start recents after binding
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700159 protected boolean mTriggeredFromAltTab;
160 protected long mLastToggleTime;
Winson6b92c6e2015-11-06 13:11:16 -0800161 DozeTrigger mFastAltTabTrigger = new DozeTrigger(FAST_ALT_TAB_DELAY_MS, new Runnable() {
162 @Override
163 public void run() {
164 // When this fires, then the user has not released alt-tab for at least
165 // FAST_ALT_TAB_DELAY_MS milliseconds
Jorim Jaggi435b2e42015-11-24 15:09:30 -0800166 showRecents(mTriggeredFromAltTab, false /* draggingInRecents */, true /* animate */,
167 false /* reloadTasks */);
Winson6b92c6e2015-11-06 13:11:16 -0800168 }
169 });
Winson190fe3bf2015-10-20 14:57:24 -0700170
Winsonaeb298c2016-04-05 13:08:11 -0700171 protected Bitmap mThumbTransitionBitmapCache;
Winson190fe3bf2015-10-20 14:57:24 -0700172
Winson190fe3bf2015-10-20 14:57:24 -0700173 public RecentsImpl(Context context) {
174 mContext = context;
Winson190fe3bf2015-10-20 14:57:24 -0700175 mHandler = new Handler();
Winson190fe3bf2015-10-20 14:57:24 -0700176
Winsonab84fc52015-10-23 11:52:07 -0700177 // Initialize the static foreground thread
178 ForegroundThread.get();
179
Winson190fe3bf2015-10-20 14:57:24 -0700180 // Register the task stack listener
Jaewan Kim938a50b2016-03-14 17:35:43 +0900181 mTaskStackListener = new TaskStackListenerImpl();
Winsone7f138c2015-10-22 16:15:21 -0700182 SystemServicesProxy ssp = Recents.getSystemServices();
183 ssp.registerTaskStackListener(mTaskStackListener);
Winson190fe3bf2015-10-20 14:57:24 -0700184
185 // Initialize the static configuration resources
Winsonb94443d2016-01-07 15:34:13 -0800186 reloadHeaderBarLayout();
Winson190fe3bf2015-10-20 14:57:24 -0700187
188 // When we start, preload the data associated with the previous recent tasks.
189 // We can use a new plan since the caches will be the same.
Winsone7f138c2015-10-22 16:15:21 -0700190 RecentsTaskLoader loader = Recents.getTaskLoader();
Winson190fe3bf2015-10-20 14:57:24 -0700191 RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
Winson65c851e2016-01-20 12:43:35 -0800192 loader.preloadTasks(plan, -1, true /* isTopTaskHome */);
Winson190fe3bf2015-10-20 14:57:24 -0700193 RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
Winson Chung296278a2015-12-17 12:09:02 -0500194 launchOpts.numVisibleTasks = loader.getIconCacheSize();
Winson190fe3bf2015-10-20 14:57:24 -0700195 launchOpts.numVisibleTaskThumbnails = loader.getThumbnailCacheSize();
196 launchOpts.onlyLoadForCache = true;
197 loader.loadTasks(mContext, plan, launchOpts);
198 }
199
200 public void onBootCompleted() {
Winsonaeb298c2016-04-05 13:08:11 -0700201 // Do nothing
Winson190fe3bf2015-10-20 14:57:24 -0700202 }
203
Winson190fe3bf2015-10-20 14:57:24 -0700204 public void onConfigurationChanged() {
Winsonb94443d2016-01-07 15:34:13 -0800205 reloadHeaderBarLayout();
Winson190fe3bf2015-10-20 14:57:24 -0700206 }
207
208 /**
209 * This is only called from the system user's Recents. Secondary users will instead proxy their
210 * visibility change events through to the system user via
211 * {@link Recents#onBusEvent(RecentsVisibilityChangedEvent)}.
212 */
213 public void onVisibilityChanged(Context context, boolean visible) {
214 SystemUIApplication app = (SystemUIApplication) context;
215 PhoneStatusBar statusBar = app.getComponent(PhoneStatusBar.class);
216 if (statusBar != null) {
217 statusBar.updateRecentsVisibility(visible);
218 }
219 }
220
221 /**
222 * This is only called from the system user's Recents. Secondary users will instead proxy their
223 * visibility change events through to the system user via
224 * {@link Recents#onBusEvent(ScreenPinningRequestEvent)}.
225 */
226 public void onStartScreenPinning(Context context) {
227 SystemUIApplication app = (SystemUIApplication) context;
228 PhoneStatusBar statusBar = app.getComponent(PhoneStatusBar.class);
229 if (statusBar != null) {
230 statusBar.showScreenPinningRequest(false);
231 }
232 }
233
Jorim Jaggibb42a462015-11-20 16:27:16 -0800234 public void showRecents(boolean triggeredFromAltTab, boolean draggingInRecents,
Jorim Jaggie161f082016-02-05 14:26:16 -0800235 boolean animate, boolean launchedWhileDockingTask) {
Winson190fe3bf2015-10-20 14:57:24 -0700236 mTriggeredFromAltTab = triggeredFromAltTab;
Jorim Jaggidd98d412015-11-18 15:57:38 -0800237 mDraggingInRecents = draggingInRecents;
Jorim Jaggie161f082016-02-05 14:26:16 -0800238 mLaunchedWhileDocking = launchedWhileDockingTask;
Winsone693aaf2016-03-01 12:05:59 -0800239 if (mFastAltTabTrigger.isAsleep()) {
240 // Fast alt-tab duration has elapsed, fall through to showing Recents and reset
241 mFastAltTabTrigger.stopDozing();
Winson6b92c6e2015-11-06 13:11:16 -0800242 } else if (mFastAltTabTrigger.isDozing()) {
Winsone693aaf2016-03-01 12:05:59 -0800243 // Fast alt-tab duration has not elapsed. If this is triggered by a different
244 // showRecents() call, then ignore that call for now.
245 // TODO: We can not handle quick tabs that happen between the initial showRecents() call
246 // that started the activity and the activity starting up. The severity of this
247 // is inversely proportional to the FAST_ALT_TAB_DELAY_MS duration though.
Winson6b92c6e2015-11-06 13:11:16 -0800248 if (!triggeredFromAltTab) {
249 return;
250 }
251 mFastAltTabTrigger.stopDozing();
Winsone693aaf2016-03-01 12:05:59 -0800252 } else if (triggeredFromAltTab) {
253 // The fast alt-tab detector is not yet running, so start the trigger and wait for the
254 // hideRecents() call, or for the fast alt-tab duration to elapse
255 mFastAltTabTrigger.startDozing();
256 return;
Winson6b92c6e2015-11-06 13:11:16 -0800257 }
Winson190fe3bf2015-10-20 14:57:24 -0700258
259 try {
260 // Check if the top task is in the home stack, and start the recents activity
Winsone7f138c2015-10-22 16:15:21 -0700261 SystemServicesProxy ssp = Recents.getSystemServices();
262 ActivityManager.RunningTaskInfo topTask = ssp.getTopMostTask();
Winson190fe3bf2015-10-20 14:57:24 -0700263 MutableBoolean isTopTaskHome = new MutableBoolean(true);
Winsone7f138c2015-10-22 16:15:21 -0700264 if (topTask == null || !ssp.isRecentsTopMost(topTask, isTopTaskHome)) {
Jorim Jaggibb42a462015-11-20 16:27:16 -0800265 startRecentsActivity(topTask, isTopTaskHome.value, animate);
Winson190fe3bf2015-10-20 14:57:24 -0700266 }
267 } catch (ActivityNotFoundException e) {
Winson1b585612015-11-06 09:16:26 -0800268 Log.e(TAG, "Failed to launch RecentsActivity", e);
Winson190fe3bf2015-10-20 14:57:24 -0700269 }
270 }
271
Winson190fe3bf2015-10-20 14:57:24 -0700272 public void hideRecents(boolean triggeredFromAltTab, boolean triggeredFromHomeKey) {
Winson2799eca2016-02-25 12:10:42 -0800273 if (triggeredFromAltTab && mFastAltTabTrigger.isDozing()) {
274 // The user has released alt-tab before the trigger has run, so just show the next
275 // task immediately
276 showNextTask();
Winson6b92c6e2015-11-06 13:11:16 -0800277
Winson2799eca2016-02-25 12:10:42 -0800278 // Cancel the fast alt-tab trigger
279 mFastAltTabTrigger.stopDozing();
Winson2799eca2016-02-25 12:10:42 -0800280 return;
Winson190fe3bf2015-10-20 14:57:24 -0700281 }
Winson2799eca2016-02-25 12:10:42 -0800282
283 // Defer to the activity to handle hiding recents, if it handles it, then it must still
284 // be visible
285 EventBus.getDefault().post(new HideRecentsEvent(triggeredFromAltTab,
286 triggeredFromHomeKey));
Winson190fe3bf2015-10-20 14:57:24 -0700287 }
288
Winson190fe3bf2015-10-20 14:57:24 -0700289 public void toggleRecents() {
Winson6b92c6e2015-11-06 13:11:16 -0800290 // Skip this toggle if we are already waiting to trigger recents via alt-tab
291 if (mFastAltTabTrigger.isDozing()) {
292 return;
293 }
294
Jorim Jaggidd98d412015-11-18 15:57:38 -0800295 mDraggingInRecents = false;
Jorim Jaggie161f082016-02-05 14:26:16 -0800296 mLaunchedWhileDocking = false;
Winson190fe3bf2015-10-20 14:57:24 -0700297 mTriggeredFromAltTab = false;
298
299 try {
Winsone7f138c2015-10-22 16:15:21 -0700300 SystemServicesProxy ssp = Recents.getSystemServices();
301 ActivityManager.RunningTaskInfo topTask = ssp.getTopMostTask();
Winson190fe3bf2015-10-20 14:57:24 -0700302 MutableBoolean isTopTaskHome = new MutableBoolean(true);
Winsonb61e6542016-02-04 14:37:18 -0800303 long elapsedTime = SystemClock.elapsedRealtime() - mLastToggleTime;
304
Winsone7f138c2015-10-22 16:15:21 -0700305 if (topTask != null && ssp.isRecentsTopMost(topTask, isTopTaskHome)) {
Winsone693aaf2016-03-01 12:05:59 -0800306 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
Winson5da43472015-11-04 17:39:55 -0800307 RecentsConfiguration config = Recents.getConfiguration();
308 RecentsActivityLaunchState launchState = config.getLaunchState();
Winson Chungead5c0f2015-12-14 11:18:57 -0500309 if (!launchState.launchedWithAltTab) {
Winsonb61e6542016-02-04 14:37:18 -0800310 // If the user taps quickly
Winsone693aaf2016-03-01 12:05:59 -0800311 if (!debugFlags.isPagingEnabled() ||
312 (ViewConfiguration.getDoubleTapMinTime() < elapsedTime &&
313 elapsedTime < ViewConfiguration.getDoubleTapTimeout())) {
Winsonb61e6542016-02-04 14:37:18 -0800314 // Launch the next focused task
315 EventBus.getDefault().post(new LaunchNextTaskRequestEvent());
316 } else {
317 // Notify recents to move onto the next task
318 EventBus.getDefault().post(new IterateRecentsEvent());
319 }
Winson0d14d4d2015-10-26 17:05:04 -0700320 } else {
321 // If the user has toggled it too quickly, then just eat up the event here (it's
322 // better than showing a janky screenshot).
323 // NOTE: Ideally, the screenshot mechanism would take the window transform into
324 // account
Winsonb61e6542016-02-04 14:37:18 -0800325 if (elapsedTime < MIN_TOGGLE_DELAY_MS) {
Winson0d14d4d2015-10-26 17:05:04 -0700326 return;
327 }
328
329 EventBus.getDefault().post(new ToggleRecentsEvent());
330 mLastToggleTime = SystemClock.elapsedRealtime();
331 }
Winson190fe3bf2015-10-20 14:57:24 -0700332 return;
333 } else {
Winson0d14d4d2015-10-26 17:05:04 -0700334 // If the user has toggled it too quickly, then just eat up the event here (it's
335 // better than showing a janky screenshot).
336 // NOTE: Ideally, the screenshot mechanism would take the window transform into
337 // account
Winsonb61e6542016-02-04 14:37:18 -0800338 if (elapsedTime < MIN_TOGGLE_DELAY_MS) {
Winson0d14d4d2015-10-26 17:05:04 -0700339 return;
340 }
341
Winson190fe3bf2015-10-20 14:57:24 -0700342 // Otherwise, start the recents activity
Jorim Jaggibb42a462015-11-20 16:27:16 -0800343 startRecentsActivity(topTask, isTopTaskHome.value, true /* animate */);
Winsond8b1d632016-01-04 17:51:18 -0800344
345 // Only close the other system windows if we are actually showing recents
346 ssp.sendCloseSystemWindows(BaseStatusBar.SYSTEM_DIALOG_REASON_RECENT_APPS);
Winson0d14d4d2015-10-26 17:05:04 -0700347 mLastToggleTime = SystemClock.elapsedRealtime();
Winson190fe3bf2015-10-20 14:57:24 -0700348 }
349 } catch (ActivityNotFoundException e) {
Winson1b585612015-11-06 09:16:26 -0800350 Log.e(TAG, "Failed to launch RecentsActivity", e);
Winson190fe3bf2015-10-20 14:57:24 -0700351 }
352 }
353
Winson190fe3bf2015-10-20 14:57:24 -0700354 public void preloadRecents() {
355 // Preload only the raw task list into a new load plan (which will be consumed by the
356 // RecentsActivity) only if there is a task to animate to.
Winsone7f138c2015-10-22 16:15:21 -0700357 SystemServicesProxy ssp = Recents.getSystemServices();
358 ActivityManager.RunningTaskInfo topTask = ssp.getTopMostTask();
Winson190fe3bf2015-10-20 14:57:24 -0700359 MutableBoolean topTaskHome = new MutableBoolean(true);
Winsone7f138c2015-10-22 16:15:21 -0700360 if (topTask != null && !ssp.isRecentsTopMost(topTask, topTaskHome)) {
Winson5b4e0d22016-02-16 18:11:35 -0800361 RecentsTaskLoader loader = Recents.getTaskLoader();
362 sInstanceLoadPlan = loader.createLoadPlan(mContext);
Winson190fe3bf2015-10-20 14:57:24 -0700363 sInstanceLoadPlan.preloadRawTasks(topTaskHome.value);
Winson65c851e2016-01-20 12:43:35 -0800364 loader.preloadTasks(sInstanceLoadPlan, topTask.id, topTaskHome.value);
Winson190fe3bf2015-10-20 14:57:24 -0700365 TaskStack stack = sInstanceLoadPlan.getTaskStack();
Winson4b057c62016-01-12 15:01:52 -0800366 if (stack.getTaskCount() > 0) {
Winsonaeb298c2016-04-05 13:08:11 -0700367 // Only preload the icon (but not the thumbnail since it may not have been taken for
368 // the pausing activity)
369 preloadIcon(topTask);
370
371 // At this point, we don't know anything about the stack state. So only calculate
372 // the dimensions of the thumbnail that we need for the transition into Recents, but
373 // do not draw it until we construct the activity options when we start Recents
374 updateHeaderBarLayout(stack);
Winson190fe3bf2015-10-20 14:57:24 -0700375 }
376 }
377 }
378
Winson190fe3bf2015-10-20 14:57:24 -0700379 public void cancelPreloadingRecents() {
380 // Do nothing
381 }
382
Jorim Jaggidd98d412015-11-18 15:57:38 -0800383 public void onDraggingInRecents(float distanceFromTop) {
384 EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEvent(distanceFromTop));
385 }
386
Jorim Jaggidd98d412015-11-18 15:57:38 -0800387 public void onDraggingInRecentsEnded(float velocity) {
388 EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEndedEvent(velocity));
389 }
390
Winson6b92c6e2015-11-06 13:11:16 -0800391 /**
392 * Transitions to the next recent task in the stack.
393 */
394 public void showNextTask() {
Winsone7f138c2015-10-22 16:15:21 -0700395 SystemServicesProxy ssp = Recents.getSystemServices();
Winson6b92c6e2015-11-06 13:11:16 -0800396 RecentsTaskLoader loader = Recents.getTaskLoader();
397 RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
Winson65c851e2016-01-20 12:43:35 -0800398 loader.preloadTasks(plan, -1, true /* isTopTaskHome */);
Winson6b92c6e2015-11-06 13:11:16 -0800399 TaskStack focusedStack = plan.getTaskStack();
400
401 // Return early if there are no tasks in the focused stack
Winson4b057c62016-01-12 15:01:52 -0800402 if (focusedStack == null || focusedStack.getTaskCount() == 0) return;
Winson6b92c6e2015-11-06 13:11:16 -0800403
404 ActivityManager.RunningTaskInfo runningTask = ssp.getTopMostTask();
405 // Return early if there is no running task
406 if (runningTask == null) return;
Winson6b92c6e2015-11-06 13:11:16 -0800407
408 // Find the task in the recents list
Winsone86deb82015-11-12 09:32:10 -0800409 boolean isTopTaskHome = SystemServicesProxy.isHomeStack(runningTask.stackId);
Winson250608a2015-11-24 15:00:31 -0800410 ArrayList<Task> tasks = focusedStack.getStackTasks();
Winson6b92c6e2015-11-06 13:11:16 -0800411 Task toTask = null;
412 ActivityOptions launchOpts = null;
413 int taskCount = tasks.size();
414 for (int i = taskCount - 1; i >= 1; i--) {
415 Task task = tasks.get(i);
Winsone86deb82015-11-12 09:32:10 -0800416 if (isTopTaskHome) {
417 toTask = tasks.get(i - 1);
418 launchOpts = ActivityOptions.makeCustomAnimation(mContext,
419 R.anim.recents_launch_next_affiliated_task_target,
420 R.anim.recents_fast_toggle_app_home_exit);
421 break;
422 } else if (task.key.id == runningTask.id) {
Winson6b92c6e2015-11-06 13:11:16 -0800423 toTask = tasks.get(i - 1);
424 launchOpts = ActivityOptions.makeCustomAnimation(mContext,
425 R.anim.recents_launch_prev_affiliated_task_target,
426 R.anim.recents_launch_prev_affiliated_task_source);
427 break;
428 }
429 }
430
431 // Return early if there is no next task
432 if (toTask == null) {
433 ssp.startInPlaceAnimationOnFrontMostApplication(
434 ActivityOptions.makeCustomInPlaceAnimation(mContext,
435 R.anim.recents_launch_prev_affiliated_task_bounce));
436 return;
437 }
438
439 // Launch the task
Wale Ogunwale64ae08a2016-03-28 08:15:27 -0700440 ssp.startActivityFromRecents(mContext, toTask.key, toTask.title, launchOpts);
Winson6b92c6e2015-11-06 13:11:16 -0800441 }
442
443 /**
444 * Transitions to the next affiliated task.
445 */
446 public void showRelativeAffiliatedTask(boolean showNextTask) {
447 SystemServicesProxy ssp = Recents.getSystemServices();
Winsone7f138c2015-10-22 16:15:21 -0700448 RecentsTaskLoader loader = Recents.getTaskLoader();
Winson190fe3bf2015-10-20 14:57:24 -0700449 RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
Winson65c851e2016-01-20 12:43:35 -0800450 loader.preloadTasks(plan, -1, true /* isTopTaskHome */);
Winson190fe3bf2015-10-20 14:57:24 -0700451 TaskStack focusedStack = plan.getTaskStack();
452
453 // Return early if there are no tasks in the focused stack
Winson4b057c62016-01-12 15:01:52 -0800454 if (focusedStack == null || focusedStack.getTaskCount() == 0) return;
Winson190fe3bf2015-10-20 14:57:24 -0700455
Winsone7f138c2015-10-22 16:15:21 -0700456 ActivityManager.RunningTaskInfo runningTask = ssp.getTopMostTask();
Winson190fe3bf2015-10-20 14:57:24 -0700457 // Return early if there is no running task (can't determine affiliated tasks in this case)
458 if (runningTask == null) return;
459 // Return early if the running task is in the home stack (optimization)
Winson5510f6c2015-10-27 12:11:26 -0700460 if (SystemServicesProxy.isHomeStack(runningTask.stackId)) return;
Winson190fe3bf2015-10-20 14:57:24 -0700461
462 // Find the task in the recents list
Winson250608a2015-11-24 15:00:31 -0800463 ArrayList<Task> tasks = focusedStack.getStackTasks();
Winson190fe3bf2015-10-20 14:57:24 -0700464 Task toTask = null;
465 ActivityOptions launchOpts = null;
466 int taskCount = tasks.size();
467 int numAffiliatedTasks = 0;
468 for (int i = 0; i < taskCount; i++) {
469 Task task = tasks.get(i);
470 if (task.key.id == runningTask.id) {
471 TaskGrouping group = task.group;
472 Task.TaskKey toTaskKey;
473 if (showNextTask) {
474 toTaskKey = group.getNextTaskInGroup(task);
475 launchOpts = ActivityOptions.makeCustomAnimation(mContext,
476 R.anim.recents_launch_next_affiliated_task_target,
477 R.anim.recents_launch_next_affiliated_task_source);
478 } else {
479 toTaskKey = group.getPrevTaskInGroup(task);
480 launchOpts = ActivityOptions.makeCustomAnimation(mContext,
481 R.anim.recents_launch_prev_affiliated_task_target,
482 R.anim.recents_launch_prev_affiliated_task_source);
483 }
484 if (toTaskKey != null) {
485 toTask = focusedStack.findTaskWithId(toTaskKey.id);
486 }
487 numAffiliatedTasks = group.getTaskCount();
488 break;
489 }
490 }
491
492 // Return early if there is no next task
493 if (toTask == null) {
494 if (numAffiliatedTasks > 1) {
495 if (showNextTask) {
Winsone7f138c2015-10-22 16:15:21 -0700496 ssp.startInPlaceAnimationOnFrontMostApplication(
Winson190fe3bf2015-10-20 14:57:24 -0700497 ActivityOptions.makeCustomInPlaceAnimation(mContext,
498 R.anim.recents_launch_next_affiliated_task_bounce));
499 } else {
Winsone7f138c2015-10-22 16:15:21 -0700500 ssp.startInPlaceAnimationOnFrontMostApplication(
Winson190fe3bf2015-10-20 14:57:24 -0700501 ActivityOptions.makeCustomInPlaceAnimation(mContext,
502 R.anim.recents_launch_prev_affiliated_task_bounce));
503 }
504 }
505 return;
506 }
507
508 // Keep track of actually launched affiliated tasks
509 MetricsLogger.count(mContext, "overview_affiliated_task_launch", 1);
510
511 // Launch the task
Wale Ogunwale64ae08a2016-03-28 08:15:27 -0700512 ssp.startActivityFromRecents(mContext, toTask.key, toTask.title, launchOpts);
Winson190fe3bf2015-10-20 14:57:24 -0700513 }
514
515 public void showNextAffiliatedTask() {
516 // Keep track of when the affiliated task is triggered
517 MetricsLogger.count(mContext, "overview_affiliated_task_next", 1);
518 showRelativeAffiliatedTask(true);
519 }
520
521 public void showPrevAffiliatedTask() {
522 // Keep track of when the affiliated task is triggered
523 MetricsLogger.count(mContext, "overview_affiliated_task_prev", 1);
524 showRelativeAffiliatedTask(false);
525 }
526
Jorim Jaggicdb06ca2016-01-25 19:15:12 -0800527 public void dockTopTask(int topTaskId, int dragMode,
528 int stackCreateMode, Rect initialBounds) {
Jorim Jaggi75b25972015-10-21 14:51:10 +0200529 SystemServicesProxy ssp = Recents.getSystemServices();
Jorim Jaggi9511b0f2016-01-29 19:12:44 -0800530
531 // Make sure we inform DividerView before we actually start the activity so we can change
532 // the resize mode already.
Chong Zhange4fbd322016-03-01 14:44:03 -0800533 if (ssp.moveTaskToDockedStack(topTaskId, stackCreateMode, initialBounds)) {
Jorim Jaggi899327f2016-02-25 20:44:18 -0500534 EventBus.getDefault().send(new DockedTopTaskEvent(dragMode, initialBounds));
Chong Zhange4fbd322016-03-01 14:44:03 -0800535 showRecents(
536 false /* triggeredFromAltTab */,
537 dragMode == NavigationBarGestureHelper.DRAG_MODE_RECENTS,
538 false /* animate */,
Winsone693aaf2016-03-01 12:05:59 -0800539 true /* launchedWhileDockingTask*/);
Chong Zhange4fbd322016-03-01 14:44:03 -0800540 }
Jorim Jaggi75b25972015-10-21 14:51:10 +0200541 }
542
Winson190fe3bf2015-10-20 14:57:24 -0700543 /**
544 * Returns the preloaded load plan and invalidates it.
545 */
546 public static RecentsTaskLoadPlan consumeInstanceLoadPlan() {
547 RecentsTaskLoadPlan plan = sInstanceLoadPlan;
548 sInstanceLoadPlan = null;
549 return plan;
550 }
551
552 /**
Winsonb94443d2016-01-07 15:34:13 -0800553 * Reloads all the layouts for the header bar transition.
554 */
555 private void reloadHeaderBarLayout() {
556 Resources res = mContext.getResources();
557 LayoutInflater inflater = LayoutInflater.from(mContext);
558
559 mStatusBarHeight = res.getDimensionPixelSize(
560 com.android.internal.R.dimen.status_bar_height);
561 mNavBarHeight = res.getDimensionPixelSize(
562 com.android.internal.R.dimen.navigation_bar_height);
563 mNavBarWidth = res.getDimensionPixelSize(
564 com.android.internal.R.dimen.navigation_bar_width);
Winson21700932016-03-24 17:26:23 -0700565 mTaskBarHeight = TaskStackLayoutAlgorithm.getDimensionForDevice(res,
566 R.dimen.recents_task_view_header_height,
567 R.dimen.recents_task_view_header_height,
568 R.dimen.recents_task_view_header_height,
569 R.dimen.recents_task_view_header_height_tablet_land,
570 R.dimen.recents_task_view_header_height,
571 R.dimen.recents_task_view_header_height_tablet_land);
Winson88737542016-02-17 13:27:33 -0800572 mDummyStackView = new TaskStackView(mContext);
Winsonb94443d2016-01-07 15:34:13 -0800573 mHeaderBar = (TaskViewHeader) inflater.inflate(R.layout.recents_task_view_header,
574 null, false);
575 }
576
577 /**
Winson190fe3bf2015-10-20 14:57:24 -0700578 * Prepares the header bar layout for the next transition, if the task view bounds has changed
579 * since the last call, it will attempt to re-measure and layout the header bar to the new size.
580 *
Winsonf0d1c442015-12-01 11:04:45 -0800581 * @param stack the stack to initialize the stack layout with
Winson190fe3bf2015-10-20 14:57:24 -0700582 */
Winson008ee15f2016-03-18 17:17:25 -0700583 private void updateHeaderBarLayout(TaskStack stack) {
Winsone7f138c2015-10-22 16:15:21 -0700584 SystemServicesProxy ssp = Recents.getSystemServices();
Jorim Jaggic6c89a82016-01-28 17:48:21 -0800585 Rect systemInsets = new Rect();
586 ssp.getStableInsets(systemInsets);
Winsone7f138c2015-10-22 16:15:21 -0700587 Rect windowRect = ssp.getWindowRect();
Winsoncf9b8322016-03-31 15:36:07 -0700588 // When docked, the nav bar insets are consumed and the activity is measured without insets.
589 // However, the window bounds include the insets, so we need to subtract them here to make
590 // them identical.
591 if (ssp.hasDockedTask()) {
592 windowRect.bottom -= systemInsets.bottom;
593 systemInsets.bottom = 0;
594 }
Jorim Jaggic6c89a82016-01-28 17:48:21 -0800595 calculateWindowStableInsets(systemInsets, windowRect);
596 windowRect.offsetTo(0, 0);
Winson190fe3bf2015-10-20 14:57:24 -0700597
Winson59924fe2016-03-17 14:13:18 -0700598 TaskStackLayoutAlgorithm stackLayout = mDummyStackView.getStackAlgorithm();
Winson190fe3bf2015-10-20 14:57:24 -0700599
600 // Rebind the header bar and draw it for the transition
Winson88737542016-02-17 13:27:33 -0800601 stackLayout.setSystemInsets(systemInsets);
Winsonf0d1c442015-12-01 11:04:45 -0800602 if (stack != null) {
Winsoncf9b8322016-03-31 15:36:07 -0700603 stackLayout.getTaskStackBounds(windowRect, systemInsets.top, systemInsets.right,
604 mTaskStackBounds);
Winsonaeb298c2016-04-05 13:08:11 -0700605 stackLayout.reset();
Winsoncf9b8322016-03-31 15:36:07 -0700606 stackLayout.initialize(windowRect, mTaskStackBounds,
Winsonf0d1c442015-12-01 11:04:45 -0800607 TaskStackLayoutAlgorithm.StackState.getStackStateForStack(stack));
Winsona1ededd2016-03-25 12:23:12 -0700608 mDummyStackView.setTasks(stack, false /* allowNotifyStackChanges */);
Winson190fe3bf2015-10-20 14:57:24 -0700609
Winsonaeb298c2016-04-05 13:08:11 -0700610 Rect taskViewBounds = stackLayout.getUntransformedTaskViewBounds();
Winson94bc4f22016-04-07 14:22:12 -0700611 if (!taskViewBounds.isEmpty()) {
612 int taskViewWidth = taskViewBounds.width();
613 synchronized (mHeaderBarLock) {
614 if (mHeaderBar.getMeasuredWidth() != taskViewWidth ||
615 mHeaderBar.getMeasuredHeight() != mTaskBarHeight) {
616 mHeaderBar.measure(
617 MeasureSpec.makeMeasureSpec(taskViewWidth, MeasureSpec.EXACTLY),
618 MeasureSpec.makeMeasureSpec(mTaskBarHeight, MeasureSpec.EXACTLY));
619 }
620 mHeaderBar.layout(0, 0, taskViewWidth, mTaskBarHeight);
Winsonaeb298c2016-04-05 13:08:11 -0700621 }
Winsonaeb298c2016-04-05 13:08:11 -0700622
Winson94bc4f22016-04-07 14:22:12 -0700623 // Update the transition bitmap to match the new header bar height
624 if (mThumbTransitionBitmapCache == null ||
625 (mThumbTransitionBitmapCache.getWidth() != taskViewWidth) ||
626 (mThumbTransitionBitmapCache.getHeight() != mTaskBarHeight)) {
627 mThumbTransitionBitmapCache = Bitmap.createBitmap(taskViewWidth,
628 mTaskBarHeight, Bitmap.Config.ARGB_8888);
629 }
Winsonaeb298c2016-04-05 13:08:11 -0700630 }
Winson190fe3bf2015-10-20 14:57:24 -0700631 }
632 }
633
634 /**
Jorim Jaggic6c89a82016-01-28 17:48:21 -0800635 * Given the stable insets and the rect for our window, calculates the insets that affect our
636 * window.
637 */
638 private void calculateWindowStableInsets(Rect inOutInsets, Rect windowRect) {
639 Rect displayRect = Recents.getSystemServices().getDisplayRect();
640
641 // Display rect without insets - available app space
642 Rect appRect = new Rect(displayRect);
643 appRect.inset(inOutInsets);
644
645 // Our window intersected with available app space
646 Rect windowRectWithInsets = new Rect(windowRect);
647 windowRectWithInsets.intersect(appRect);
648 inOutInsets.left = windowRectWithInsets.left - windowRect.left;
649 inOutInsets.top = windowRectWithInsets.top - windowRect.top;
650 inOutInsets.right = windowRect.right - windowRectWithInsets.right;
651 inOutInsets.bottom = windowRect.bottom - windowRectWithInsets.bottom;
652 }
653
654 /**
Winson190fe3bf2015-10-20 14:57:24 -0700655 * Preloads the icon of a task.
656 */
657 private void preloadIcon(ActivityManager.RunningTaskInfo task) {
Winson190fe3bf2015-10-20 14:57:24 -0700658 // Ensure that we load the running task's icon
659 RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
660 launchOpts.runningTaskId = task.id;
661 launchOpts.loadThumbnails = false;
662 launchOpts.onlyLoadForCache = true;
Winsone7f138c2015-10-22 16:15:21 -0700663 Recents.getTaskLoader().loadTasks(mContext, sInstanceLoadPlan, launchOpts);
Winson190fe3bf2015-10-20 14:57:24 -0700664 }
665
666 /**
Winson190fe3bf2015-10-20 14:57:24 -0700667 * Creates the activity options for a unknown state->recents transition.
668 */
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700669 protected ActivityOptions getUnknownTransitionActivityOptions() {
Winson190fe3bf2015-10-20 14:57:24 -0700670 return ActivityOptions.makeCustomAnimation(mContext,
671 R.anim.recents_from_unknown_enter,
672 R.anim.recents_from_unknown_exit,
Winson3fb67562015-11-11 10:39:03 -0800673 mHandler, null);
Winson190fe3bf2015-10-20 14:57:24 -0700674 }
675
676 /**
677 * Creates the activity options for a home->recents transition.
678 */
Winson008ee15f2016-03-18 17:17:25 -0700679 protected ActivityOptions getHomeTransitionActivityOptions() {
Winson190fe3bf2015-10-20 14:57:24 -0700680 return ActivityOptions.makeCustomAnimation(mContext,
681 R.anim.recents_from_launcher_enter,
682 R.anim.recents_from_launcher_exit,
Winson3fb67562015-11-11 10:39:03 -0800683 mHandler, null);
Winson190fe3bf2015-10-20 14:57:24 -0700684 }
685
686 /**
687 * Creates the activity options for an app->recents transition.
688 */
689 private ActivityOptions getThumbnailTransitionActivityOptions(
Winsone693aaf2016-03-01 12:05:59 -0800690 ActivityManager.RunningTaskInfo topTask, TaskStackView stackView) {
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700691 if (topTask.stackId == FREEFORM_WORKSPACE_STACK_ID) {
692 ArrayList<AppTransitionAnimationSpec> specs = new ArrayList<>();
Winsone693aaf2016-03-01 12:05:59 -0800693 ArrayList<Task> tasks = stackView.getStack().getStackTasks();
694 TaskStackLayoutAlgorithm stackLayout = stackView.getStackAlgorithm();
695 TaskStackViewScroller stackScroller = stackView.getScroller();
696
Winson003eda62016-03-11 14:56:00 -0800697 stackView.updateLayoutAlgorithm(true /* boundScroll */);
Winson619e40c2016-03-25 16:12:35 -0700698 stackView.updateToInitialState(true /* scrollToInitialState */);
Winsone693aaf2016-03-01 12:05:59 -0800699
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700700 for (int i = tasks.size() - 1; i >= 0; i--) {
701 Task task = tasks.get(i);
Winson387aac62015-11-25 11:18:56 -0800702 if (task.isFreeformTask()) {
Winsone693aaf2016-03-01 12:05:59 -0800703 mTmpTransform = stackLayout.getStackTransformScreenCoordinates(task,
704 stackScroller.getStackScroll(), mTmpTransform, null);
Winsonaeb298c2016-04-05 13:08:11 -0700705 Bitmap thumbnail = drawThumbnailTransitionBitmap(task, mTmpTransform,
706 mThumbTransitionBitmapCache);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700707 Rect toTaskRect = new Rect();
708 mTmpTransform.rect.round(toTaskRect);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700709 specs.add(new AppTransitionAnimationSpec(task.key.id, thumbnail, toTaskRect));
710 }
711 }
712 AppTransitionAnimationSpec[] specsArray = new AppTransitionAnimationSpec[specs.size()];
713 specs.toArray(specsArray);
714 return ActivityOptions.makeThumbnailAspectScaleDownAnimation(mDummyStackView,
Winson3fb67562015-11-11 10:39:03 -0800715 specsArray, mHandler, null, this);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700716 } else {
717 // Update the destination rect
718 Task toTask = new Task();
Winsone693aaf2016-03-01 12:05:59 -0800719 TaskViewTransform toTransform = getThumbnailTransitionTransform(stackView, toTask);
Winsonaeb298c2016-04-05 13:08:11 -0700720 Bitmap thumbnail = drawThumbnailTransitionBitmap(toTask, toTransform,
721 mThumbTransitionBitmapCache);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700722 if (thumbnail != null) {
Winsonaeb298c2016-04-05 13:08:11 -0700723 RectF toTaskRect = toTransform.rect;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700724 return ActivityOptions.makeThumbnailAspectScaleDownAnimation(mDummyStackView,
725 thumbnail, (int) toTaskRect.left, (int) toTaskRect.top,
Winson3fb67562015-11-11 10:39:03 -0800726 (int) toTaskRect.width(), (int) toTaskRect.height(), mHandler, null);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700727 }
728 // If both the screenshot and thumbnail fails, then just fall back to the default transition
729 return getUnknownTransitionActivityOptions();
730 }
731 }
Winson190fe3bf2015-10-20 14:57:24 -0700732
Winson190fe3bf2015-10-20 14:57:24 -0700733 /**
734 * Returns the transition rect for the given task id.
735 */
Winsone693aaf2016-03-01 12:05:59 -0800736 private TaskViewTransform getThumbnailTransitionTransform(TaskStackView stackView,
737 Task runningTaskOut) {
Winson190fe3bf2015-10-20 14:57:24 -0700738 // Find the running task in the TaskStack
Winsone693aaf2016-03-01 12:05:59 -0800739 TaskStack stack = stackView.getStack();
Winson65c851e2016-01-20 12:43:35 -0800740 Task launchTask = stack.getLaunchTarget();
741 if (launchTask != null) {
742 runningTaskOut.copyFrom(launchTask);
743 } else {
Winson190fe3bf2015-10-20 14:57:24 -0700744 // If no task is specified or we can not find the task just use the front most one
Winson35a8b042016-01-22 09:41:09 -0800745 launchTask = stack.getStackFrontMostTask(true /* includeFreeform */);
Winson65c851e2016-01-20 12:43:35 -0800746 runningTaskOut.copyFrom(launchTask);
Winson190fe3bf2015-10-20 14:57:24 -0700747 }
748
749 // Get the transform for the running task
Winson003eda62016-03-11 14:56:00 -0800750 stackView.updateLayoutAlgorithm(true /* boundScroll */);
Winson619e40c2016-03-25 16:12:35 -0700751 stackView.updateToInitialState(true /* scrollToInitialState */);
Winson7845e8c2016-03-29 10:23:19 -0700752 stackView.getStackAlgorithm().getStackTransformScreenCoordinates(launchTask,
Winson190fe3bf2015-10-20 14:57:24 -0700753 stackView.getScroller().getStackScroll(), mTmpTransform, null);
754 return mTmpTransform;
755 }
756
757 /**
758 * Draws the header of a task used for the window animation into a bitmap.
759 */
Winsonaeb298c2016-04-05 13:08:11 -0700760 private Bitmap drawThumbnailTransitionBitmap(Task toTask, TaskViewTransform toTransform,
761 Bitmap thumbnail) {
Winson8be16342016-02-09 11:53:27 -0800762 SystemServicesProxy ssp = Recents.getSystemServices();
Winson190fe3bf2015-10-20 14:57:24 -0700763 if (toTransform != null && toTask.key != null) {
Winson190fe3bf2015-10-20 14:57:24 -0700764 synchronized (mHeaderBarLock) {
Winson8be16342016-02-09 11:53:27 -0800765 boolean disabledInSafeMode = !toTask.isSystemApp && ssp.isInSafeMode();
Winson Chung509d0d02015-12-16 15:43:12 -0500766 mHeaderBar.onTaskViewSizeChanged((int) toTransform.rect.width(),
767 (int) toTransform.rect.height());
Winsonc742f972015-11-12 11:32:21 -0800768 if (RecentsDebugFlags.Static.EnableTransitionThumbnailDebugMode) {
Winson190fe3bf2015-10-20 14:57:24 -0700769 thumbnail.eraseColor(0xFFff0000);
770 } else {
Winsonaeb298c2016-04-05 13:08:11 -0700771 thumbnail.eraseColor(0);
Winson190fe3bf2015-10-20 14:57:24 -0700772 Canvas c = new Canvas(thumbnail);
Winson22574af2016-03-23 19:00:28 -0700773 // Workaround for b/27815919, reset the callback so that we do not trigger an
774 // invalidate on the header bar as a result of updating the icon
775 Drawable icon = mHeaderBar.getIconView().getDrawable();
776 if (icon != null) {
777 icon.setCallback(null);
778 }
Winson8be16342016-02-09 11:53:27 -0800779 mHeaderBar.rebindToTask(toTask, false /* touchExplorationEnabled */,
780 disabledInSafeMode);
Winsone693aaf2016-03-01 12:05:59 -0800781 mHeaderBar.setDimAlpha(toTransform.dimAlpha);
Winson190fe3bf2015-10-20 14:57:24 -0700782 mHeaderBar.draw(c);
783 c.setBitmap(null);
784 }
785 }
786 return thumbnail.createAshmemBitmap();
787 }
788 return null;
789 }
790
791 /**
792 * Shows the recents activity
793 */
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700794 protected void startRecentsActivity(ActivityManager.RunningTaskInfo topTask,
Jorim Jaggibb42a462015-11-20 16:27:16 -0800795 boolean isTopTaskHome, boolean animate) {
Winsone7f138c2015-10-22 16:15:21 -0700796 RecentsTaskLoader loader = Recents.getTaskLoader();
Winsonc5b12dd2016-03-23 20:25:12 -0700797 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
Winson190fe3bf2015-10-20 14:57:24 -0700798
Winsone5f1faa2015-11-20 12:26:23 -0800799 // In the case where alt-tab is triggered, we never get a preloadRecents() call, so we
Jorim Jaggi435b2e42015-11-24 15:09:30 -0800800 // should always preload the tasks now. If we are dragging in recents, reload them as
801 // the stacks might have changed.
Jorim Jaggie161f082016-02-05 14:26:16 -0800802 if (mLaunchedWhileDocking || mTriggeredFromAltTab ||sInstanceLoadPlan == null) {
Winsone5f1faa2015-11-20 12:26:23 -0800803 // Create a new load plan if preloadRecents() was never triggered
Winson190fe3bf2015-10-20 14:57:24 -0700804 sInstanceLoadPlan = loader.createLoadPlan(mContext);
805 }
Jorim Jaggie161f082016-02-05 14:26:16 -0800806 if (mLaunchedWhileDocking || mTriggeredFromAltTab || !sInstanceLoadPlan.hasTasks()) {
Winson65c851e2016-01-20 12:43:35 -0800807 loader.preloadTasks(sInstanceLoadPlan, topTask.id, isTopTaskHome);
Winson190fe3bf2015-10-20 14:57:24 -0700808 }
Winsonc5b12dd2016-03-23 20:25:12 -0700809
Winson190fe3bf2015-10-20 14:57:24 -0700810 TaskStack stack = sInstanceLoadPlan.getTaskStack();
Winsonc5b12dd2016-03-23 20:25:12 -0700811 boolean hasRecentTasks = stack.getTaskCount() > 0;
812 boolean useThumbnailTransition = (topTask != null) && !isTopTaskHome && hasRecentTasks;
Winson190fe3bf2015-10-20 14:57:24 -0700813
Winsonaeb298c2016-04-05 13:08:11 -0700814 // Update the launch state that we need in updateHeaderBarLayout()
815 launchState.launchedFromHome = !useThumbnailTransition;
816 launchState.launchedFromApp = useThumbnailTransition || mLaunchedWhileDocking;
817 launchState.launchedViaDockGesture = mLaunchedWhileDocking;
818 launchState.launchedViaDragGesture = mDraggingInRecents;
819 launchState.launchedToTaskId = (topTask != null) ? topTask.id : -1;
820 launchState.launchedWithAltTab = mTriggeredFromAltTab;
821
822 // Preload the icon (this will be a null-op if we have preloaded the icon already in
823 // preloadRecents())
824 preloadIcon(topTask);
825
Winsonf0d1c442015-12-01 11:04:45 -0800826 // Update the header bar if necessary
Winson008ee15f2016-03-18 17:17:25 -0700827 updateHeaderBarLayout(stack);
Winsonf0d1c442015-12-01 11:04:45 -0800828
Winson190fe3bf2015-10-20 14:57:24 -0700829 // Prepare the dummy stack for the transition
Winson36a5a2c2015-10-29 18:04:39 -0700830 TaskStackLayoutAlgorithm.VisibilityReport stackVr =
Winson190fe3bf2015-10-20 14:57:24 -0700831 mDummyStackView.computeStackVisibilityReport();
Jorim Jaggibb42a462015-11-20 16:27:16 -0800832
Winsonaeb298c2016-04-05 13:08:11 -0700833 // Update the remaining launch state
Winsonc5b12dd2016-03-23 20:25:12 -0700834 launchState.launchedNumVisibleTasks = stackVr.numVisibleTasks;
835 launchState.launchedNumVisibleThumbnails = stackVr.numVisibleThumbnails;
Winsonc5b12dd2016-03-23 20:25:12 -0700836
Jorim Jaggibb42a462015-11-20 16:27:16 -0800837 if (!animate) {
Winsonc5b12dd2016-03-23 20:25:12 -0700838 startRecentsActivity(ActivityOptions.makeCustomAnimation(mContext, -1, -1));
Jorim Jaggibb42a462015-11-20 16:27:16 -0800839 return;
840 }
841
Winsonaeb298c2016-04-05 13:08:11 -0700842 ActivityOptions opts;
Winson190fe3bf2015-10-20 14:57:24 -0700843 if (useThumbnailTransition) {
844 // Try starting with a thumbnail transition
Winsonaeb298c2016-04-05 13:08:11 -0700845 opts = getThumbnailTransitionActivityOptions(topTask, mDummyStackView);
846 } else {
Winson190fe3bf2015-10-20 14:57:24 -0700847 // If there is no thumbnail transition, but is launching from home into recents, then
Winson008ee15f2016-03-18 17:17:25 -0700848 // use a quick home transition
Winsonaeb298c2016-04-05 13:08:11 -0700849 opts = hasRecentTasks
850 ? getHomeTransitionActivityOptions()
851 : getUnknownTransitionActivityOptions();
Winson190fe3bf2015-10-20 14:57:24 -0700852 }
Winsonaeb298c2016-04-05 13:08:11 -0700853 startRecentsActivity(opts);
Winson190fe3bf2015-10-20 14:57:24 -0700854 mLastToggleTime = SystemClock.elapsedRealtime();
855 }
856
857 /**
858 * Starts the recents activity.
859 */
Winsonc5b12dd2016-03-23 20:25:12 -0700860 private void startRecentsActivity(ActivityOptions opts) {
Winson190fe3bf2015-10-20 14:57:24 -0700861 Intent intent = new Intent();
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700862 intent.setClassName(RECENTS_PACKAGE, RECENTS_ACTIVITY);
Winson190fe3bf2015-10-20 14:57:24 -0700863 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
864 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
865 | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
Sid Soundararajanb58c46a2016-01-26 15:39:27 -0800866
Winson190fe3bf2015-10-20 14:57:24 -0700867 if (opts != null) {
868 mContext.startActivityAsUser(intent, opts.toBundle(), UserHandle.CURRENT);
869 } else {
870 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
871 }
Jorim Jaggicdb06ca2016-01-25 19:15:12 -0800872 EventBus.getDefault().send(new RecentsActivityStartingEvent());
Winson190fe3bf2015-10-20 14:57:24 -0700873 }
874
Winson3fb67562015-11-11 10:39:03 -0800875 /**** OnAnimationFinishedListener Implementation ****/
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -0700876
877 @Override
878 public void onAnimationFinished() {
879 EventBus.getDefault().post(new EnterRecentsWindowLastAnimationFrameEvent());
880 }
Winson190fe3bf2015-10-20 14:57:24 -0700881}