blob: 611f9f201f1edbbb2ec35f5118c6ec8ae73e23e6 [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;
Jorim Jaggidb21bbd2016-04-18 15:32:07 -070041import android.view.WindowManager;
Winsonc0d70582016-01-29 10:24:39 -080042
Winson190fe3bf2015-10-20 14:57:24 -070043import com.android.internal.logging.MetricsLogger;
Jorim Jaggidb21bbd2016-04-18 15:32:07 -070044import com.android.internal.policy.DockedDividerUtils;
Winson190fe3bf2015-10-20 14:57:24 -070045import com.android.systemui.R;
46import com.android.systemui.SystemUIApplication;
Winson412e1802015-10-20 16:57:57 -070047import com.android.systemui.recents.events.EventBus;
Jorim Jaggi899327f2016-02-25 20:44:18 -050048import com.android.systemui.recents.events.activity.DockedTopTaskEvent;
Winson1b585612015-11-06 09:16:26 -080049import com.android.systemui.recents.events.activity.EnterRecentsWindowLastAnimationFrameEvent;
Winson412e1802015-10-20 16:57:57 -070050import com.android.systemui.recents.events.activity.HideRecentsEvent;
Winson0d14d4d2015-10-26 17:05:04 -070051import com.android.systemui.recents.events.activity.IterateRecentsEvent;
Winsonb61e6542016-02-04 14:37:18 -080052import com.android.systemui.recents.events.activity.LaunchNextTaskRequestEvent;
Jorim Jaggicdb06ca2016-01-25 19:15:12 -080053import com.android.systemui.recents.events.activity.RecentsActivityStartingEvent;
Winson412e1802015-10-20 16:57:57 -070054import com.android.systemui.recents.events.activity.ToggleRecentsEvent;
Winson190fe3bf2015-10-20 14:57:24 -070055import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
56import com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
Jorim Jaggidd98d412015-11-18 15:57:38 -080057import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
58import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
Winson6b92c6e2015-11-06 13:11:16 -080059import com.android.systemui.recents.misc.DozeTrigger;
Winsonab84fc52015-10-23 11:52:07 -070060import com.android.systemui.recents.misc.ForegroundThread;
Winson190fe3bf2015-10-20 14:57:24 -070061import com.android.systemui.recents.misc.SystemServicesProxy;
Jaewan Kim938a50b2016-03-14 17:35:43 +090062import com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener;
Winson190fe3bf2015-10-20 14:57:24 -070063import com.android.systemui.recents.model.RecentsTaskLoadPlan;
64import com.android.systemui.recents.model.RecentsTaskLoader;
65import com.android.systemui.recents.model.Task;
66import com.android.systemui.recents.model.TaskGrouping;
67import com.android.systemui.recents.model.TaskStack;
Winson36a5a2c2015-10-29 18:04:39 -070068import com.android.systemui.recents.views.TaskStackLayoutAlgorithm;
Winson1b585612015-11-06 09:16:26 -080069import com.android.systemui.recents.views.TaskStackView;
Winsone693aaf2016-03-01 12:05:59 -080070import com.android.systemui.recents.views.TaskStackViewScroller;
Winson190fe3bf2015-10-20 14:57:24 -070071import com.android.systemui.recents.views.TaskViewHeader;
72import com.android.systemui.recents.views.TaskViewTransform;
Jorim Jaggidb21bbd2016-04-18 15:32:07 -070073import com.android.systemui.stackdivider.DividerView;
Winsond8b1d632016-01-04 17:51:18 -080074import com.android.systemui.statusbar.BaseStatusBar;
Jorim Jaggicdb06ca2016-01-25 19:15:12 -080075import com.android.systemui.statusbar.phone.NavigationBarGestureHelper;
Winson190fe3bf2015-10-20 14:57:24 -070076import com.android.systemui.statusbar.phone.PhoneStatusBar;
77
78import java.util.ArrayList;
79
80/**
81 * An implementation of the Recents component for the current user. For secondary users, this can
82 * be called remotely from the system user.
83 */
Jorim Jaggicdb06ca2016-01-25 19:15:12 -080084public class RecentsImpl implements ActivityOptions.OnAnimationFinishedListener {
Winson190fe3bf2015-10-20 14:57:24 -070085
86 private final static String TAG = "RecentsImpl";
Winsonb61e6542016-02-04 14:37:18 -080087
Winson6b92c6e2015-11-06 13:11:16 -080088 // The minimum amount of time between each recents button press that we will handle
89 private final static int MIN_TOGGLE_DELAY_MS = 350;
Winsonb61e6542016-02-04 14:37:18 -080090
Winson6b92c6e2015-11-06 13:11:16 -080091 // The duration within which the user releasing the alt tab (from when they pressed alt tab)
92 // that the fast alt-tab animation will run. If the user's alt-tab takes longer than this
93 // duration, then we will toggle recents after this duration.
94 private final static int FAST_ALT_TAB_DELAY_MS = 225;
Winson190fe3bf2015-10-20 14:57:24 -070095
96 public final static String RECENTS_PACKAGE = "com.android.systemui";
97 public final static String RECENTS_ACTIVITY = "com.android.systemui.recents.RecentsActivity";
Winsone693aaf2016-03-01 12:05:59 -080098
Winson190fe3bf2015-10-20 14:57:24 -070099 /**
Jaewan Kim938a50b2016-03-14 17:35:43 +0900100 * An implementation of TaskStackListener, that allows us to listen for changes to the system
Winson190fe3bf2015-10-20 14:57:24 -0700101 * task stacks and update recents accordingly.
102 */
Jaewan Kim938a50b2016-03-14 17:35:43 +0900103 class TaskStackListenerImpl extends TaskStackListener {
Winson190fe3bf2015-10-20 14:57:24 -0700104 @Override
105 public void onTaskStackChanged() {
Jaewan Kim938a50b2016-03-14 17:35:43 +0900106 // Preloads the next task
Jorim Jaggia0fdeec2016-01-07 14:42:28 +0100107 RecentsConfiguration config = Recents.getConfiguration();
Winson190fe3bf2015-10-20 14:57:24 -0700108 if (config.svelteLevel == RecentsConfiguration.SVELTE_NONE) {
Winsone7f138c2015-10-22 16:15:21 -0700109 RecentsTaskLoader loader = Recents.getTaskLoader();
110 SystemServicesProxy ssp = Recents.getSystemServices();
Winsond46b7272016-04-20 11:54:27 -0700111 ActivityManager.RunningTaskInfo runningTaskInfo = ssp.getRunningTask();
Winson190fe3bf2015-10-20 14:57:24 -0700112
113 // Load the next task only if we aren't svelte
114 RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
Winsondfd7be02016-05-10 13:30:37 -0700115 loader.preloadTasks(plan, -1, false /* includeFrontMostExcludedTask */);
Winson190fe3bf2015-10-20 14:57:24 -0700116 RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
117 // This callback is made when a new activity is launched and the old one is paused
118 // so ignore the current activity and try and preload the thumbnail for the
119 // previous one.
120 if (runningTaskInfo != null) {
121 launchOpts.runningTaskId = runningTaskInfo.id;
122 }
123 launchOpts.numVisibleTasks = 2;
124 launchOpts.numVisibleTaskThumbnails = 2;
125 launchOpts.onlyLoadForCache = true;
126 launchOpts.onlyLoadPausedActivities = true;
127 loader.loadTasks(mContext, plan, launchOpts);
128 }
Winson190fe3bf2015-10-20 14:57:24 -0700129 }
130 }
131
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700132 protected static RecentsTaskLoadPlan sInstanceLoadPlan;
Winson190fe3bf2015-10-20 14:57:24 -0700133
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700134 protected Context mContext;
135 protected Handler mHandler;
Winson190fe3bf2015-10-20 14:57:24 -0700136 TaskStackListenerImpl mTaskStackListener;
Jorim Jaggidd98d412015-11-18 15:57:38 -0800137 boolean mDraggingInRecents;
Jorim Jaggie161f082016-02-05 14:26:16 -0800138 boolean mLaunchedWhileDocking;
Winson190fe3bf2015-10-20 14:57:24 -0700139
140 // Task launching
Winson190fe3bf2015-10-20 14:57:24 -0700141 Rect mTaskStackBounds = new Rect();
Winson190fe3bf2015-10-20 14:57:24 -0700142 TaskViewTransform mTmpTransform = new TaskViewTransform();
143 int mStatusBarHeight;
144 int mNavBarHeight;
145 int mNavBarWidth;
146 int mTaskBarHeight;
147
148 // Header (for transition)
149 TaskViewHeader mHeaderBar;
150 final Object mHeaderBarLock = new Object();
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700151 protected TaskStackView mDummyStackView;
Winson190fe3bf2015-10-20 14:57:24 -0700152
153 // Variables to keep track of if we need to start recents after binding
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700154 protected boolean mTriggeredFromAltTab;
155 protected long mLastToggleTime;
Winson6b92c6e2015-11-06 13:11:16 -0800156 DozeTrigger mFastAltTabTrigger = new DozeTrigger(FAST_ALT_TAB_DELAY_MS, new Runnable() {
157 @Override
158 public void run() {
159 // When this fires, then the user has not released alt-tab for at least
160 // FAST_ALT_TAB_DELAY_MS milliseconds
Jorim Jaggi435b2e42015-11-24 15:09:30 -0800161 showRecents(mTriggeredFromAltTab, false /* draggingInRecents */, true /* animate */,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700162 false /* reloadTasks */, false /* fromHome */,
163 DividerView.INVALID_RECENTS_GROW_TARGET);
Winson6b92c6e2015-11-06 13:11:16 -0800164 }
165 });
Winson190fe3bf2015-10-20 14:57:24 -0700166
Winsonaeb298c2016-04-05 13:08:11 -0700167 protected Bitmap mThumbTransitionBitmapCache;
Winson190fe3bf2015-10-20 14:57:24 -0700168
Winson190fe3bf2015-10-20 14:57:24 -0700169 public RecentsImpl(Context context) {
170 mContext = context;
Winson190fe3bf2015-10-20 14:57:24 -0700171 mHandler = new Handler();
Winson190fe3bf2015-10-20 14:57:24 -0700172
Winsonab84fc52015-10-23 11:52:07 -0700173 // Initialize the static foreground thread
174 ForegroundThread.get();
175
Winson190fe3bf2015-10-20 14:57:24 -0700176 // Register the task stack listener
Jaewan Kim938a50b2016-03-14 17:35:43 +0900177 mTaskStackListener = new TaskStackListenerImpl();
Winsone7f138c2015-10-22 16:15:21 -0700178 SystemServicesProxy ssp = Recents.getSystemServices();
179 ssp.registerTaskStackListener(mTaskStackListener);
Winson190fe3bf2015-10-20 14:57:24 -0700180
181 // Initialize the static configuration resources
Winson670ea712016-04-12 17:02:26 -0700182 LayoutInflater inflater = LayoutInflater.from(mContext);
183 mDummyStackView = new TaskStackView(mContext);
184 mHeaderBar = (TaskViewHeader) inflater.inflate(R.layout.recents_task_view_header,
185 null, false);
186 reloadResources();
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);
Winsondfd7be02016-05-10 13:30:37 -0700192 loader.preloadTasks(plan, -1, false /* includeFrontMostExcludedTask */);
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() {
Winson670ea712016-04-12 17:02:26 -0700205 reloadResources();
206 mDummyStackView.reloadOnConfigurationChange();
207 mHeaderBar.onConfigurationChanged();
Winson190fe3bf2015-10-20 14:57:24 -0700208 }
209
210 /**
211 * This is only called from the system user's Recents. Secondary users will instead proxy their
212 * visibility change events through to the system user via
213 * {@link Recents#onBusEvent(RecentsVisibilityChangedEvent)}.
214 */
215 public void onVisibilityChanged(Context context, boolean visible) {
216 SystemUIApplication app = (SystemUIApplication) context;
217 PhoneStatusBar statusBar = app.getComponent(PhoneStatusBar.class);
218 if (statusBar != null) {
219 statusBar.updateRecentsVisibility(visible);
220 }
221 }
222
223 /**
224 * This is only called from the system user's Recents. Secondary users will instead proxy their
225 * visibility change events through to the system user via
226 * {@link Recents#onBusEvent(ScreenPinningRequestEvent)}.
227 */
Andrii Kulian0f051f52016-04-14 00:41:51 -0700228 public void onStartScreenPinning(Context context, int taskId) {
Winson190fe3bf2015-10-20 14:57:24 -0700229 SystemUIApplication app = (SystemUIApplication) context;
230 PhoneStatusBar statusBar = app.getComponent(PhoneStatusBar.class);
231 if (statusBar != null) {
Andrii Kulian0f051f52016-04-14 00:41:51 -0700232 statusBar.showScreenPinningRequest(taskId, false);
Winson190fe3bf2015-10-20 14:57:24 -0700233 }
234 }
235
Jorim Jaggibb42a462015-11-20 16:27:16 -0800236 public void showRecents(boolean triggeredFromAltTab, boolean draggingInRecents,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700237 boolean animate, boolean launchedWhileDockingTask, boolean fromHome,
238 int growTarget) {
Winson190fe3bf2015-10-20 14:57:24 -0700239 mTriggeredFromAltTab = triggeredFromAltTab;
Jorim Jaggidd98d412015-11-18 15:57:38 -0800240 mDraggingInRecents = draggingInRecents;
Jorim Jaggie161f082016-02-05 14:26:16 -0800241 mLaunchedWhileDocking = launchedWhileDockingTask;
Winsone693aaf2016-03-01 12:05:59 -0800242 if (mFastAltTabTrigger.isAsleep()) {
243 // Fast alt-tab duration has elapsed, fall through to showing Recents and reset
244 mFastAltTabTrigger.stopDozing();
Winson6b92c6e2015-11-06 13:11:16 -0800245 } else if (mFastAltTabTrigger.isDozing()) {
Winsone693aaf2016-03-01 12:05:59 -0800246 // Fast alt-tab duration has not elapsed. If this is triggered by a different
247 // showRecents() call, then ignore that call for now.
248 // TODO: We can not handle quick tabs that happen between the initial showRecents() call
249 // that started the activity and the activity starting up. The severity of this
250 // is inversely proportional to the FAST_ALT_TAB_DELAY_MS duration though.
Winson6b92c6e2015-11-06 13:11:16 -0800251 if (!triggeredFromAltTab) {
252 return;
253 }
254 mFastAltTabTrigger.stopDozing();
Winsone693aaf2016-03-01 12:05:59 -0800255 } else if (triggeredFromAltTab) {
256 // The fast alt-tab detector is not yet running, so start the trigger and wait for the
257 // hideRecents() call, or for the fast alt-tab duration to elapse
258 mFastAltTabTrigger.startDozing();
259 return;
Winson6b92c6e2015-11-06 13:11:16 -0800260 }
Winson190fe3bf2015-10-20 14:57:24 -0700261
262 try {
263 // Check if the top task is in the home stack, and start the recents activity
Winsone7f138c2015-10-22 16:15:21 -0700264 SystemServicesProxy ssp = Recents.getSystemServices();
Winsond7cb5172016-05-04 16:14:19 -0700265 boolean forceVisible = launchedWhileDockingTask || draggingInRecents;
266 MutableBoolean isHomeStackVisible = new MutableBoolean(forceVisible);
267 if (forceVisible || !ssp.isRecentsActivityVisible(isHomeStackVisible)) {
Winsond46b7272016-04-20 11:54:27 -0700268 ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
269 startRecentsActivity(runningTask, isHomeStackVisible.value || fromHome, animate,
270 growTarget);
Winson190fe3bf2015-10-20 14:57:24 -0700271 }
272 } catch (ActivityNotFoundException e) {
Winson1b585612015-11-06 09:16:26 -0800273 Log.e(TAG, "Failed to launch RecentsActivity", e);
Winson190fe3bf2015-10-20 14:57:24 -0700274 }
275 }
276
Winson190fe3bf2015-10-20 14:57:24 -0700277 public void hideRecents(boolean triggeredFromAltTab, boolean triggeredFromHomeKey) {
Winson2799eca2016-02-25 12:10:42 -0800278 if (triggeredFromAltTab && mFastAltTabTrigger.isDozing()) {
279 // The user has released alt-tab before the trigger has run, so just show the next
280 // task immediately
281 showNextTask();
Winson6b92c6e2015-11-06 13:11:16 -0800282
Winson2799eca2016-02-25 12:10:42 -0800283 // Cancel the fast alt-tab trigger
284 mFastAltTabTrigger.stopDozing();
Winson2799eca2016-02-25 12:10:42 -0800285 return;
Winson190fe3bf2015-10-20 14:57:24 -0700286 }
Winson2799eca2016-02-25 12:10:42 -0800287
288 // Defer to the activity to handle hiding recents, if it handles it, then it must still
289 // be visible
290 EventBus.getDefault().post(new HideRecentsEvent(triggeredFromAltTab,
291 triggeredFromHomeKey));
Winson190fe3bf2015-10-20 14:57:24 -0700292 }
293
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700294 public void toggleRecents(int growTarget) {
Winson6b92c6e2015-11-06 13:11:16 -0800295 // Skip this toggle if we are already waiting to trigger recents via alt-tab
296 if (mFastAltTabTrigger.isDozing()) {
297 return;
298 }
299
Jorim Jaggidd98d412015-11-18 15:57:38 -0800300 mDraggingInRecents = false;
Jorim Jaggie161f082016-02-05 14:26:16 -0800301 mLaunchedWhileDocking = false;
Winson190fe3bf2015-10-20 14:57:24 -0700302 mTriggeredFromAltTab = false;
303
304 try {
Winsone7f138c2015-10-22 16:15:21 -0700305 SystemServicesProxy ssp = Recents.getSystemServices();
Winsond46b7272016-04-20 11:54:27 -0700306 MutableBoolean isHomeStackVisible = new MutableBoolean(true);
Winsonb61e6542016-02-04 14:37:18 -0800307 long elapsedTime = SystemClock.elapsedRealtime() - mLastToggleTime;
308
Winsond46b7272016-04-20 11:54:27 -0700309 if (ssp.isRecentsActivityVisible(isHomeStackVisible)) {
Winsone693aaf2016-03-01 12:05:59 -0800310 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
Winson5da43472015-11-04 17:39:55 -0800311 RecentsConfiguration config = Recents.getConfiguration();
312 RecentsActivityLaunchState launchState = config.getLaunchState();
Winson Chungead5c0f2015-12-14 11:18:57 -0500313 if (!launchState.launchedWithAltTab) {
Winsonb61e6542016-02-04 14:37:18 -0800314 // If the user taps quickly
Winsone693aaf2016-03-01 12:05:59 -0800315 if (!debugFlags.isPagingEnabled() ||
316 (ViewConfiguration.getDoubleTapMinTime() < elapsedTime &&
317 elapsedTime < ViewConfiguration.getDoubleTapTimeout())) {
Winsonb61e6542016-02-04 14:37:18 -0800318 // Launch the next focused task
319 EventBus.getDefault().post(new LaunchNextTaskRequestEvent());
320 } else {
321 // Notify recents to move onto the next task
322 EventBus.getDefault().post(new IterateRecentsEvent());
323 }
Winson0d14d4d2015-10-26 17:05:04 -0700324 } else {
325 // If the user has toggled it too quickly, then just eat up the event here (it's
326 // better than showing a janky screenshot).
327 // NOTE: Ideally, the screenshot mechanism would take the window transform into
328 // account
Winsonb61e6542016-02-04 14:37:18 -0800329 if (elapsedTime < MIN_TOGGLE_DELAY_MS) {
Winson0d14d4d2015-10-26 17:05:04 -0700330 return;
331 }
332
333 EventBus.getDefault().post(new ToggleRecentsEvent());
334 mLastToggleTime = SystemClock.elapsedRealtime();
335 }
Winson190fe3bf2015-10-20 14:57:24 -0700336 return;
337 } else {
Winson0d14d4d2015-10-26 17:05:04 -0700338 // If the user has toggled it too quickly, then just eat up the event here (it's
339 // better than showing a janky screenshot).
340 // NOTE: Ideally, the screenshot mechanism would take the window transform into
341 // account
Winsonb61e6542016-02-04 14:37:18 -0800342 if (elapsedTime < MIN_TOGGLE_DELAY_MS) {
Winson0d14d4d2015-10-26 17:05:04 -0700343 return;
344 }
345
Winson190fe3bf2015-10-20 14:57:24 -0700346 // Otherwise, start the recents activity
Winsond46b7272016-04-20 11:54:27 -0700347 ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
348 startRecentsActivity(runningTask, isHomeStackVisible.value, true /* animate */,
349 growTarget);
Winsond8b1d632016-01-04 17:51:18 -0800350
351 // Only close the other system windows if we are actually showing recents
352 ssp.sendCloseSystemWindows(BaseStatusBar.SYSTEM_DIALOG_REASON_RECENT_APPS);
Winson0d14d4d2015-10-26 17:05:04 -0700353 mLastToggleTime = SystemClock.elapsedRealtime();
Winson190fe3bf2015-10-20 14:57:24 -0700354 }
355 } catch (ActivityNotFoundException e) {
Winson1b585612015-11-06 09:16:26 -0800356 Log.e(TAG, "Failed to launch RecentsActivity", e);
Winson190fe3bf2015-10-20 14:57:24 -0700357 }
358 }
359
Winson190fe3bf2015-10-20 14:57:24 -0700360 public void preloadRecents() {
361 // Preload only the raw task list into a new load plan (which will be consumed by the
362 // RecentsActivity) only if there is a task to animate to.
Winsone7f138c2015-10-22 16:15:21 -0700363 SystemServicesProxy ssp = Recents.getSystemServices();
Winsond46b7272016-04-20 11:54:27 -0700364 MutableBoolean isHomeStackVisible = new MutableBoolean(true);
365 if (!ssp.isRecentsActivityVisible(isHomeStackVisible)) {
366 ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
Winson5b4e0d22016-02-16 18:11:35 -0800367 RecentsTaskLoader loader = Recents.getTaskLoader();
368 sInstanceLoadPlan = loader.createLoadPlan(mContext);
Winsondfd7be02016-05-10 13:30:37 -0700369 sInstanceLoadPlan.preloadRawTasks(!isHomeStackVisible.value);
370 loader.preloadTasks(sInstanceLoadPlan, runningTask.id, !isHomeStackVisible.value);
Winson190fe3bf2015-10-20 14:57:24 -0700371 TaskStack stack = sInstanceLoadPlan.getTaskStack();
Winson4b057c62016-01-12 15:01:52 -0800372 if (stack.getTaskCount() > 0) {
Winsonaeb298c2016-04-05 13:08:11 -0700373 // Only preload the icon (but not the thumbnail since it may not have been taken for
374 // the pausing activity)
Winsond46b7272016-04-20 11:54:27 -0700375 preloadIcon(runningTask);
Winsonaeb298c2016-04-05 13:08:11 -0700376
377 // At this point, we don't know anything about the stack state. So only calculate
378 // the dimensions of the thumbnail that we need for the transition into Recents, but
379 // do not draw it until we construct the activity options when we start Recents
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700380 updateHeaderBarLayout(stack, null /* window rect override*/);
Winson190fe3bf2015-10-20 14:57:24 -0700381 }
382 }
383 }
384
Winson190fe3bf2015-10-20 14:57:24 -0700385 public void cancelPreloadingRecents() {
386 // Do nothing
387 }
388
Jorim Jaggidd98d412015-11-18 15:57:38 -0800389 public void onDraggingInRecents(float distanceFromTop) {
390 EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEvent(distanceFromTop));
391 }
392
Jorim Jaggidd98d412015-11-18 15:57:38 -0800393 public void onDraggingInRecentsEnded(float velocity) {
394 EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEndedEvent(velocity));
395 }
396
Winson6b92c6e2015-11-06 13:11:16 -0800397 /**
398 * Transitions to the next recent task in the stack.
399 */
400 public void showNextTask() {
Winsone7f138c2015-10-22 16:15:21 -0700401 SystemServicesProxy ssp = Recents.getSystemServices();
Winson6b92c6e2015-11-06 13:11:16 -0800402 RecentsTaskLoader loader = Recents.getTaskLoader();
403 RecentsTaskLoadPlan plan = loader.createLoadPlan(mContext);
Winsondfd7be02016-05-10 13:30:37 -0700404 loader.preloadTasks(plan, -1, false /* includeFrontMostExcludedTask */);
Winson6b92c6e2015-11-06 13:11:16 -0800405 TaskStack focusedStack = plan.getTaskStack();
406
407 // Return early if there are no tasks in the focused stack
Winson4b057c62016-01-12 15:01:52 -0800408 if (focusedStack == null || focusedStack.getTaskCount() == 0) return;
Winson6b92c6e2015-11-06 13:11:16 -0800409
Winson6b92c6e2015-11-06 13:11:16 -0800410 // Return early if there is no running task
Winsond46b7272016-04-20 11:54:27 -0700411 ActivityManager.RunningTaskInfo runningTask = ssp.getRunningTask();
Winson6b92c6e2015-11-06 13:11:16 -0800412 if (runningTask == null) return;
Winson6b92c6e2015-11-06 13:11:16 -0800413
414 // Find the task in the recents list
Winsond46b7272016-04-20 11:54:27 -0700415 boolean isRunningTaskInHomeStack = SystemServicesProxy.isHomeStack(runningTask.stackId);
Winson250608a2015-11-24 15:00:31 -0800416 ArrayList<Task> tasks = focusedStack.getStackTasks();
Winson6b92c6e2015-11-06 13:11:16 -0800417 Task toTask = null;
418 ActivityOptions launchOpts = null;
419 int taskCount = tasks.size();
420 for (int i = taskCount - 1; i >= 1; i--) {
421 Task task = tasks.get(i);
Winsond46b7272016-04-20 11:54:27 -0700422 if (isRunningTaskInHomeStack) {
Winsone86deb82015-11-12 09:32:10 -0800423 toTask = tasks.get(i - 1);
424 launchOpts = ActivityOptions.makeCustomAnimation(mContext,
425 R.anim.recents_launch_next_affiliated_task_target,
426 R.anim.recents_fast_toggle_app_home_exit);
427 break;
428 } else if (task.key.id == runningTask.id) {
Winson6b92c6e2015-11-06 13:11:16 -0800429 toTask = tasks.get(i - 1);
430 launchOpts = ActivityOptions.makeCustomAnimation(mContext,
431 R.anim.recents_launch_prev_affiliated_task_target,
432 R.anim.recents_launch_prev_affiliated_task_source);
433 break;
434 }
435 }
436
437 // Return early if there is no next task
438 if (toTask == null) {
439 ssp.startInPlaceAnimationOnFrontMostApplication(
440 ActivityOptions.makeCustomInPlaceAnimation(mContext,
441 R.anim.recents_launch_prev_affiliated_task_bounce));
442 return;
443 }
444
445 // Launch the task
Wale Ogunwale64ae08a2016-03-28 08:15:27 -0700446 ssp.startActivityFromRecents(mContext, toTask.key, toTask.title, launchOpts);
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
Wale Ogunwale64ae08a2016-03-28 08:15:27 -0700518 ssp.startActivityFromRecents(mContext, toTask.key, toTask.title, launchOpts);
Winson190fe3bf2015-10-20 14:57:24 -0700519 }
520
521 public void showNextAffiliatedTask() {
522 // Keep track of when the affiliated task is triggered
523 MetricsLogger.count(mContext, "overview_affiliated_task_next", 1);
524 showRelativeAffiliatedTask(true);
525 }
526
527 public void showPrevAffiliatedTask() {
528 // Keep track of when the affiliated task is triggered
529 MetricsLogger.count(mContext, "overview_affiliated_task_prev", 1);
530 showRelativeAffiliatedTask(false);
531 }
532
Jorim Jaggicdb06ca2016-01-25 19:15:12 -0800533 public void dockTopTask(int topTaskId, int dragMode,
534 int stackCreateMode, Rect initialBounds) {
Jorim Jaggi75b25972015-10-21 14:51:10 +0200535 SystemServicesProxy ssp = Recents.getSystemServices();
Jorim Jaggi9511b0f2016-01-29 19:12:44 -0800536
537 // Make sure we inform DividerView before we actually start the activity so we can change
538 // the resize mode already.
Chong Zhange4fbd322016-03-01 14:44:03 -0800539 if (ssp.moveTaskToDockedStack(topTaskId, stackCreateMode, initialBounds)) {
Jorim Jaggi899327f2016-02-25 20:44:18 -0500540 EventBus.getDefault().send(new DockedTopTaskEvent(dragMode, initialBounds));
Chong Zhange4fbd322016-03-01 14:44:03 -0800541 showRecents(
542 false /* triggeredFromAltTab */,
543 dragMode == NavigationBarGestureHelper.DRAG_MODE_RECENTS,
544 false /* animate */,
Jorim Jaggi681fc7b2016-04-14 22:02:39 -0700545 true /* launchedWhileDockingTask*/,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700546 false /* fromHome */,
547 DividerView.INVALID_RECENTS_GROW_TARGET);
Chong Zhange4fbd322016-03-01 14:44:03 -0800548 }
Jorim Jaggi75b25972015-10-21 14:51:10 +0200549 }
550
Winson190fe3bf2015-10-20 14:57:24 -0700551 /**
552 * Returns the preloaded load plan and invalidates it.
553 */
554 public static RecentsTaskLoadPlan consumeInstanceLoadPlan() {
555 RecentsTaskLoadPlan plan = sInstanceLoadPlan;
556 sInstanceLoadPlan = null;
557 return plan;
558 }
559
560 /**
Winson670ea712016-04-12 17:02:26 -0700561 * Reloads all the resources for the current configuration.
Winsonb94443d2016-01-07 15:34:13 -0800562 */
Winson670ea712016-04-12 17:02:26 -0700563 private void reloadResources() {
Winsonb94443d2016-01-07 15:34:13 -0800564 Resources res = mContext.getResources();
Winsonb94443d2016-01-07 15:34:13 -0800565
566 mStatusBarHeight = res.getDimensionPixelSize(
567 com.android.internal.R.dimen.status_bar_height);
568 mNavBarHeight = res.getDimensionPixelSize(
569 com.android.internal.R.dimen.navigation_bar_height);
570 mNavBarWidth = res.getDimensionPixelSize(
571 com.android.internal.R.dimen.navigation_bar_width);
Jorim Jaggi25160db2016-04-18 16:03:36 -0700572 mTaskBarHeight = TaskStackLayoutAlgorithm.getDimensionForDevice(mContext,
Winson21700932016-03-24 17:26:23 -0700573 R.dimen.recents_task_view_header_height,
574 R.dimen.recents_task_view_header_height,
575 R.dimen.recents_task_view_header_height,
576 R.dimen.recents_task_view_header_height_tablet_land,
577 R.dimen.recents_task_view_header_height,
578 R.dimen.recents_task_view_header_height_tablet_land);
Winsonb94443d2016-01-07 15:34:13 -0800579 }
580
581 /**
Winson190fe3bf2015-10-20 14:57:24 -0700582 * Prepares the header bar layout for the next transition, if the task view bounds has changed
583 * since the last call, it will attempt to re-measure and layout the header bar to the new size.
584 *
Winsonf0d1c442015-12-01 11:04:45 -0800585 * @param stack the stack to initialize the stack layout with
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700586 * @param windowRectOverride the rectangle to use when calculating the stack state which can
587 * be different from the current window rect if recents is resizing
588 * while being launched
Winson190fe3bf2015-10-20 14:57:24 -0700589 */
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700590 private void updateHeaderBarLayout(TaskStack stack, Rect windowRectOverride) {
Winsone7f138c2015-10-22 16:15:21 -0700591 SystemServicesProxy ssp = Recents.getSystemServices();
Winsonfc48b072016-04-21 11:20:11 -0700592 Rect displayRect = ssp.getDisplayRect();
Jorim Jaggic6c89a82016-01-28 17:48:21 -0800593 Rect systemInsets = new Rect();
594 ssp.getStableInsets(systemInsets);
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700595 Rect windowRect = windowRectOverride != null
596 ? new Rect(windowRectOverride)
597 : ssp.getWindowRect();
Winsoncf9b8322016-03-31 15:36:07 -0700598 // When docked, the nav bar insets are consumed and the activity is measured without insets.
599 // However, the window bounds include the insets, so we need to subtract them here to make
600 // them identical.
601 if (ssp.hasDockedTask()) {
602 windowRect.bottom -= systemInsets.bottom;
603 systemInsets.bottom = 0;
604 }
Jorim Jaggic6c89a82016-01-28 17:48:21 -0800605 calculateWindowStableInsets(systemInsets, windowRect);
606 windowRect.offsetTo(0, 0);
Winson190fe3bf2015-10-20 14:57:24 -0700607
Winson59924fe2016-03-17 14:13:18 -0700608 TaskStackLayoutAlgorithm stackLayout = mDummyStackView.getStackAlgorithm();
Winson190fe3bf2015-10-20 14:57:24 -0700609
610 // Rebind the header bar and draw it for the transition
Winson88737542016-02-17 13:27:33 -0800611 stackLayout.setSystemInsets(systemInsets);
Winsonf0d1c442015-12-01 11:04:45 -0800612 if (stack != null) {
Winsonfc48b072016-04-21 11:20:11 -0700613 stackLayout.getTaskStackBounds(displayRect, windowRect, systemInsets.top,
614 systemInsets.right, mTaskStackBounds);
Winsonaeb298c2016-04-05 13:08:11 -0700615 stackLayout.reset();
Winsonfc48b072016-04-21 11:20:11 -0700616 stackLayout.initialize(displayRect, windowRect, mTaskStackBounds,
Winsonf0d1c442015-12-01 11:04:45 -0800617 TaskStackLayoutAlgorithm.StackState.getStackStateForStack(stack));
Winsona1ededd2016-03-25 12:23:12 -0700618 mDummyStackView.setTasks(stack, false /* allowNotifyStackChanges */);
Winson190fe3bf2015-10-20 14:57:24 -0700619
Winsonaeb298c2016-04-05 13:08:11 -0700620 Rect taskViewBounds = stackLayout.getUntransformedTaskViewBounds();
Winson94bc4f22016-04-07 14:22:12 -0700621 if (!taskViewBounds.isEmpty()) {
622 int taskViewWidth = taskViewBounds.width();
623 synchronized (mHeaderBarLock) {
624 if (mHeaderBar.getMeasuredWidth() != taskViewWidth ||
625 mHeaderBar.getMeasuredHeight() != mTaskBarHeight) {
626 mHeaderBar.measure(
627 MeasureSpec.makeMeasureSpec(taskViewWidth, MeasureSpec.EXACTLY),
628 MeasureSpec.makeMeasureSpec(mTaskBarHeight, MeasureSpec.EXACTLY));
629 }
630 mHeaderBar.layout(0, 0, taskViewWidth, mTaskBarHeight);
Winsonaeb298c2016-04-05 13:08:11 -0700631 }
Winsonaeb298c2016-04-05 13:08:11 -0700632
Winson94bc4f22016-04-07 14:22:12 -0700633 // Update the transition bitmap to match the new header bar height
634 if (mThumbTransitionBitmapCache == null ||
635 (mThumbTransitionBitmapCache.getWidth() != taskViewWidth) ||
636 (mThumbTransitionBitmapCache.getHeight() != mTaskBarHeight)) {
637 mThumbTransitionBitmapCache = Bitmap.createBitmap(taskViewWidth,
638 mTaskBarHeight, Bitmap.Config.ARGB_8888);
639 }
Winsonaeb298c2016-04-05 13:08:11 -0700640 }
Winson190fe3bf2015-10-20 14:57:24 -0700641 }
642 }
643
644 /**
Jorim Jaggic6c89a82016-01-28 17:48:21 -0800645 * Given the stable insets and the rect for our window, calculates the insets that affect our
646 * window.
647 */
648 private void calculateWindowStableInsets(Rect inOutInsets, Rect windowRect) {
649 Rect displayRect = Recents.getSystemServices().getDisplayRect();
650
651 // Display rect without insets - available app space
652 Rect appRect = new Rect(displayRect);
653 appRect.inset(inOutInsets);
654
655 // Our window intersected with available app space
656 Rect windowRectWithInsets = new Rect(windowRect);
657 windowRectWithInsets.intersect(appRect);
658 inOutInsets.left = windowRectWithInsets.left - windowRect.left;
659 inOutInsets.top = windowRectWithInsets.top - windowRect.top;
660 inOutInsets.right = windowRect.right - windowRectWithInsets.right;
661 inOutInsets.bottom = windowRect.bottom - windowRectWithInsets.bottom;
662 }
663
664 /**
Winson190fe3bf2015-10-20 14:57:24 -0700665 * Preloads the icon of a task.
666 */
667 private void preloadIcon(ActivityManager.RunningTaskInfo task) {
Winson190fe3bf2015-10-20 14:57:24 -0700668 // Ensure that we load the running task's icon
669 RecentsTaskLoadPlan.Options launchOpts = new RecentsTaskLoadPlan.Options();
670 launchOpts.runningTaskId = task.id;
671 launchOpts.loadThumbnails = false;
672 launchOpts.onlyLoadForCache = true;
Winsone7f138c2015-10-22 16:15:21 -0700673 Recents.getTaskLoader().loadTasks(mContext, sInstanceLoadPlan, launchOpts);
Winson190fe3bf2015-10-20 14:57:24 -0700674 }
675
676 /**
Winson190fe3bf2015-10-20 14:57:24 -0700677 * Creates the activity options for a unknown state->recents transition.
678 */
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700679 protected ActivityOptions getUnknownTransitionActivityOptions() {
Winson190fe3bf2015-10-20 14:57:24 -0700680 return ActivityOptions.makeCustomAnimation(mContext,
681 R.anim.recents_from_unknown_enter,
682 R.anim.recents_from_unknown_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 a home->recents transition.
688 */
Winson008ee15f2016-03-18 17:17:25 -0700689 protected ActivityOptions getHomeTransitionActivityOptions() {
Winson190fe3bf2015-10-20 14:57:24 -0700690 return ActivityOptions.makeCustomAnimation(mContext,
691 R.anim.recents_from_launcher_enter,
692 R.anim.recents_from_launcher_exit,
Winson3fb67562015-11-11 10:39:03 -0800693 mHandler, null);
Winson190fe3bf2015-10-20 14:57:24 -0700694 }
695
696 /**
697 * Creates the activity options for an app->recents transition.
698 */
699 private ActivityOptions getThumbnailTransitionActivityOptions(
Winsond46b7272016-04-20 11:54:27 -0700700 ActivityManager.RunningTaskInfo runningTask, TaskStackView stackView,
701 Rect windowOverrideRect) {
702 if (runningTask.stackId == FREEFORM_WORKSPACE_STACK_ID) {
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700703 ArrayList<AppTransitionAnimationSpec> specs = new ArrayList<>();
Winsone693aaf2016-03-01 12:05:59 -0800704 ArrayList<Task> tasks = stackView.getStack().getStackTasks();
705 TaskStackLayoutAlgorithm stackLayout = stackView.getStackAlgorithm();
706 TaskStackViewScroller stackScroller = stackView.getScroller();
707
Winson003eda62016-03-11 14:56:00 -0800708 stackView.updateLayoutAlgorithm(true /* boundScroll */);
Winson67c79572016-04-13 14:02:18 -0700709 stackView.updateToInitialState();
Winsone693aaf2016-03-01 12:05:59 -0800710
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700711 for (int i = tasks.size() - 1; i >= 0; i--) {
712 Task task = tasks.get(i);
Winson387aac62015-11-25 11:18:56 -0800713 if (task.isFreeformTask()) {
Winsone693aaf2016-03-01 12:05:59 -0800714 mTmpTransform = stackLayout.getStackTransformScreenCoordinates(task,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700715 stackScroller.getStackScroll(), mTmpTransform, null,
716 windowOverrideRect);
Winsonaeb298c2016-04-05 13:08:11 -0700717 Bitmap thumbnail = drawThumbnailTransitionBitmap(task, mTmpTransform,
718 mThumbTransitionBitmapCache);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700719 Rect toTaskRect = new Rect();
720 mTmpTransform.rect.round(toTaskRect);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700721 specs.add(new AppTransitionAnimationSpec(task.key.id, thumbnail, toTaskRect));
722 }
723 }
724 AppTransitionAnimationSpec[] specsArray = new AppTransitionAnimationSpec[specs.size()];
725 specs.toArray(specsArray);
726 return ActivityOptions.makeThumbnailAspectScaleDownAnimation(mDummyStackView,
Winson3fb67562015-11-11 10:39:03 -0800727 specsArray, mHandler, null, this);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700728 } else {
729 // Update the destination rect
730 Task toTask = new Task();
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700731 TaskViewTransform toTransform = getThumbnailTransitionTransform(stackView, toTask,
732 windowOverrideRect);
Winsonaeb298c2016-04-05 13:08:11 -0700733 Bitmap thumbnail = drawThumbnailTransitionBitmap(toTask, toTransform,
734 mThumbTransitionBitmapCache);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700735 if (thumbnail != null) {
Winsonaeb298c2016-04-05 13:08:11 -0700736 RectF toTaskRect = toTransform.rect;
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700737 return ActivityOptions.makeThumbnailAspectScaleDownAnimation(mDummyStackView,
738 thumbnail, (int) toTaskRect.left, (int) toTaskRect.top,
Winson3fb67562015-11-11 10:39:03 -0800739 (int) toTaskRect.width(), (int) toTaskRect.height(), mHandler, null);
Filip Gruszczynskid64ef3e2015-10-27 17:58:02 -0700740 }
741 // If both the screenshot and thumbnail fails, then just fall back to the default transition
742 return getUnknownTransitionActivityOptions();
743 }
744 }
Winson190fe3bf2015-10-20 14:57:24 -0700745
Winson190fe3bf2015-10-20 14:57:24 -0700746 /**
747 * Returns the transition rect for the given task id.
748 */
Winsone693aaf2016-03-01 12:05:59 -0800749 private TaskViewTransform getThumbnailTransitionTransform(TaskStackView stackView,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700750 Task runningTaskOut, Rect windowOverrideRect) {
Winson190fe3bf2015-10-20 14:57:24 -0700751 // Find the running task in the TaskStack
Winsone693aaf2016-03-01 12:05:59 -0800752 TaskStack stack = stackView.getStack();
Winson65c851e2016-01-20 12:43:35 -0800753 Task launchTask = stack.getLaunchTarget();
754 if (launchTask != null) {
755 runningTaskOut.copyFrom(launchTask);
756 } else {
Winson190fe3bf2015-10-20 14:57:24 -0700757 // If no task is specified or we can not find the task just use the front most one
Winson35a8b042016-01-22 09:41:09 -0800758 launchTask = stack.getStackFrontMostTask(true /* includeFreeform */);
Winson65c851e2016-01-20 12:43:35 -0800759 runningTaskOut.copyFrom(launchTask);
Winson190fe3bf2015-10-20 14:57:24 -0700760 }
761
762 // Get the transform for the running task
Winson003eda62016-03-11 14:56:00 -0800763 stackView.updateLayoutAlgorithm(true /* boundScroll */);
Winson67c79572016-04-13 14:02:18 -0700764 stackView.updateToInitialState();
Winson7845e8c2016-03-29 10:23:19 -0700765 stackView.getStackAlgorithm().getStackTransformScreenCoordinates(launchTask,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700766 stackView.getScroller().getStackScroll(), mTmpTransform, null, windowOverrideRect);
Winson190fe3bf2015-10-20 14:57:24 -0700767 return mTmpTransform;
768 }
769
770 /**
771 * Draws the header of a task used for the window animation into a bitmap.
772 */
Winsonaeb298c2016-04-05 13:08:11 -0700773 private Bitmap drawThumbnailTransitionBitmap(Task toTask, TaskViewTransform toTransform,
774 Bitmap thumbnail) {
Winson8be16342016-02-09 11:53:27 -0800775 SystemServicesProxy ssp = Recents.getSystemServices();
Winson190fe3bf2015-10-20 14:57:24 -0700776 if (toTransform != null && toTask.key != null) {
Winson190fe3bf2015-10-20 14:57:24 -0700777 synchronized (mHeaderBarLock) {
Winson8be16342016-02-09 11:53:27 -0800778 boolean disabledInSafeMode = !toTask.isSystemApp && ssp.isInSafeMode();
Winson Chung509d0d02015-12-16 15:43:12 -0500779 mHeaderBar.onTaskViewSizeChanged((int) toTransform.rect.width(),
780 (int) toTransform.rect.height());
Winsonc742f972015-11-12 11:32:21 -0800781 if (RecentsDebugFlags.Static.EnableTransitionThumbnailDebugMode) {
Winson190fe3bf2015-10-20 14:57:24 -0700782 thumbnail.eraseColor(0xFFff0000);
783 } else {
Winsonaeb298c2016-04-05 13:08:11 -0700784 thumbnail.eraseColor(0);
Winson190fe3bf2015-10-20 14:57:24 -0700785 Canvas c = new Canvas(thumbnail);
Winson22574af2016-03-23 19:00:28 -0700786 // Workaround for b/27815919, reset the callback so that we do not trigger an
787 // invalidate on the header bar as a result of updating the icon
788 Drawable icon = mHeaderBar.getIconView().getDrawable();
789 if (icon != null) {
790 icon.setCallback(null);
791 }
Winsond2a03062016-04-15 11:19:07 -0700792 mHeaderBar.bindToTask(toTask, false /* touchExplorationEnabled */,
Winson8be16342016-02-09 11:53:27 -0800793 disabledInSafeMode);
Winsond2a03062016-04-15 11:19:07 -0700794 mHeaderBar.onTaskDataLoaded();
Winsone693aaf2016-03-01 12:05:59 -0800795 mHeaderBar.setDimAlpha(toTransform.dimAlpha);
Winson190fe3bf2015-10-20 14:57:24 -0700796 mHeaderBar.draw(c);
797 c.setBitmap(null);
798 }
799 }
800 return thumbnail.createAshmemBitmap();
801 }
802 return null;
803 }
804
805 /**
806 * Shows the recents activity
807 */
Winsond46b7272016-04-20 11:54:27 -0700808 protected void startRecentsActivity(ActivityManager.RunningTaskInfo runningTask,
809 boolean isHomeStackVisible, boolean animate, int growTarget) {
Winsone7f138c2015-10-22 16:15:21 -0700810 RecentsTaskLoader loader = Recents.getTaskLoader();
Winsonc5b12dd2016-03-23 20:25:12 -0700811 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
Winson190fe3bf2015-10-20 14:57:24 -0700812
Winsone5f1faa2015-11-20 12:26:23 -0800813 // In the case where alt-tab is triggered, we never get a preloadRecents() call, so we
Jorim Jaggi435b2e42015-11-24 15:09:30 -0800814 // should always preload the tasks now. If we are dragging in recents, reload them as
815 // the stacks might have changed.
Jorim Jaggie161f082016-02-05 14:26:16 -0800816 if (mLaunchedWhileDocking || mTriggeredFromAltTab ||sInstanceLoadPlan == null) {
Winsone5f1faa2015-11-20 12:26:23 -0800817 // Create a new load plan if preloadRecents() was never triggered
Winson190fe3bf2015-10-20 14:57:24 -0700818 sInstanceLoadPlan = loader.createLoadPlan(mContext);
819 }
Jorim Jaggie161f082016-02-05 14:26:16 -0800820 if (mLaunchedWhileDocking || mTriggeredFromAltTab || !sInstanceLoadPlan.hasTasks()) {
Winsondfd7be02016-05-10 13:30:37 -0700821 loader.preloadTasks(sInstanceLoadPlan, runningTask.id, !isHomeStackVisible);
Winson190fe3bf2015-10-20 14:57:24 -0700822 }
Winsonc5b12dd2016-03-23 20:25:12 -0700823
Winson190fe3bf2015-10-20 14:57:24 -0700824 TaskStack stack = sInstanceLoadPlan.getTaskStack();
Winsonc5b12dd2016-03-23 20:25:12 -0700825 boolean hasRecentTasks = stack.getTaskCount() > 0;
Winsond46b7272016-04-20 11:54:27 -0700826 boolean useThumbnailTransition = (runningTask != null) && !isHomeStackVisible && hasRecentTasks;
Winson190fe3bf2015-10-20 14:57:24 -0700827
Winsonaeb298c2016-04-05 13:08:11 -0700828 // Update the launch state that we need in updateHeaderBarLayout()
829 launchState.launchedFromHome = !useThumbnailTransition;
830 launchState.launchedFromApp = useThumbnailTransition || mLaunchedWhileDocking;
831 launchState.launchedViaDockGesture = mLaunchedWhileDocking;
832 launchState.launchedViaDragGesture = mDraggingInRecents;
Winsond46b7272016-04-20 11:54:27 -0700833 launchState.launchedToTaskId = (runningTask != null) ? runningTask.id : -1;
Winsonaeb298c2016-04-05 13:08:11 -0700834 launchState.launchedWithAltTab = mTriggeredFromAltTab;
835
836 // Preload the icon (this will be a null-op if we have preloaded the icon already in
837 // preloadRecents())
Winsond46b7272016-04-20 11:54:27 -0700838 preloadIcon(runningTask);
Winsonaeb298c2016-04-05 13:08:11 -0700839
Winsonf0d1c442015-12-01 11:04:45 -0800840 // Update the header bar if necessary
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700841 Rect windowOverrideRect = getWindowRectOverride(growTarget);
842 updateHeaderBarLayout(stack, windowOverrideRect);
Winsonf0d1c442015-12-01 11:04:45 -0800843
Winson190fe3bf2015-10-20 14:57:24 -0700844 // Prepare the dummy stack for the transition
Winson36a5a2c2015-10-29 18:04:39 -0700845 TaskStackLayoutAlgorithm.VisibilityReport stackVr =
Winson190fe3bf2015-10-20 14:57:24 -0700846 mDummyStackView.computeStackVisibilityReport();
Jorim Jaggibb42a462015-11-20 16:27:16 -0800847
Winsonaeb298c2016-04-05 13:08:11 -0700848 // Update the remaining launch state
Winsonc5b12dd2016-03-23 20:25:12 -0700849 launchState.launchedNumVisibleTasks = stackVr.numVisibleTasks;
850 launchState.launchedNumVisibleThumbnails = stackVr.numVisibleThumbnails;
Winsonc5b12dd2016-03-23 20:25:12 -0700851
Jorim Jaggibb42a462015-11-20 16:27:16 -0800852 if (!animate) {
Winsonc5b12dd2016-03-23 20:25:12 -0700853 startRecentsActivity(ActivityOptions.makeCustomAnimation(mContext, -1, -1));
Jorim Jaggibb42a462015-11-20 16:27:16 -0800854 return;
855 }
856
Winsonaeb298c2016-04-05 13:08:11 -0700857 ActivityOptions opts;
Winson190fe3bf2015-10-20 14:57:24 -0700858 if (useThumbnailTransition) {
859 // Try starting with a thumbnail transition
Winsond46b7272016-04-20 11:54:27 -0700860 opts = getThumbnailTransitionActivityOptions(runningTask, mDummyStackView,
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700861 windowOverrideRect);
Winsonaeb298c2016-04-05 13:08:11 -0700862 } else {
Winson190fe3bf2015-10-20 14:57:24 -0700863 // If there is no thumbnail transition, but is launching from home into recents, then
Winson008ee15f2016-03-18 17:17:25 -0700864 // use a quick home transition
Winsonaeb298c2016-04-05 13:08:11 -0700865 opts = hasRecentTasks
866 ? getHomeTransitionActivityOptions()
867 : getUnknownTransitionActivityOptions();
Winson190fe3bf2015-10-20 14:57:24 -0700868 }
Winsonaeb298c2016-04-05 13:08:11 -0700869 startRecentsActivity(opts);
Winson190fe3bf2015-10-20 14:57:24 -0700870 mLastToggleTime = SystemClock.elapsedRealtime();
871 }
872
Jorim Jaggidb21bbd2016-04-18 15:32:07 -0700873 private Rect getWindowRectOverride(int growTarget) {
874 if (growTarget == DividerView.INVALID_RECENTS_GROW_TARGET) {
875 return null;
876 }
877 Rect result = new Rect();
878 Rect displayRect = Recents.getSystemServices().getDisplayRect();
879 DockedDividerUtils.calculateBoundsForPosition(growTarget, WindowManager.DOCKED_BOTTOM,
880 result, displayRect.width(), displayRect.height(),
881 Recents.getSystemServices().getDockedDividerSize(mContext));
882 return result;
883 }
884
Winson190fe3bf2015-10-20 14:57:24 -0700885 /**
886 * Starts the recents activity.
887 */
Winsonc5b12dd2016-03-23 20:25:12 -0700888 private void startRecentsActivity(ActivityOptions opts) {
Winson190fe3bf2015-10-20 14:57:24 -0700889 Intent intent = new Intent();
Sid Soundararajan4bdb6872016-03-18 13:42:10 -0700890 intent.setClassName(RECENTS_PACKAGE, RECENTS_ACTIVITY);
Winson190fe3bf2015-10-20 14:57:24 -0700891 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
892 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
893 | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
Sid Soundararajanb58c46a2016-01-26 15:39:27 -0800894
Winson190fe3bf2015-10-20 14:57:24 -0700895 if (opts != null) {
896 mContext.startActivityAsUser(intent, opts.toBundle(), UserHandle.CURRENT);
897 } else {
898 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
899 }
Jorim Jaggicdb06ca2016-01-25 19:15:12 -0800900 EventBus.getDefault().send(new RecentsActivityStartingEvent());
Winson190fe3bf2015-10-20 14:57:24 -0700901 }
902
Winson3fb67562015-11-11 10:39:03 -0800903 /**** OnAnimationFinishedListener Implementation ****/
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -0700904
905 @Override
906 public void onAnimationFinished() {
907 EventBus.getDefault().post(new EnterRecentsWindowLastAnimationFrameEvent());
908 }
Winson190fe3bf2015-10-20 14:57:24 -0700909}