blob: de8730dd8be7f594a2caa15bc345cc0b702c5ae9 [file] [log] [blame]
Winson Chung303e1ff2014-03-07 15:06:19 -08001/*
2 * Copyright (C) 2014 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.views;
18
Filip Gruszczynski170192a2015-08-16 17:46:34 -070019import android.app.ActivityManager;
Winson Chung303e1ff2014-03-07 15:06:19 -080020import android.app.ActivityOptions;
21import android.content.Context;
Winson Chung303e1ff2014-03-07 15:06:19 -080022import android.graphics.Bitmap;
23import android.graphics.Canvas;
24import android.graphics.Rect;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070025import android.os.Bundle;
26import android.os.IRemoteCallback;
27import android.os.RemoteException;
Winson Chung8e548f72014-06-24 14:40:53 -070028import android.util.AttributeSet;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070029import android.util.Log;
Filip Gruszczynski170192a2015-08-16 17:46:34 -070030import android.util.SparseArray;
31import android.view.AppTransitionAnimationSpec;
Winson Chungecd9b302014-04-16 17:07:18 -070032import android.view.LayoutInflater;
Winson Chung303e1ff2014-03-07 15:06:19 -080033import android.view.View;
Winson Chung653f70c22014-05-19 14:49:42 -070034import android.view.WindowInsets;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070035import android.view.WindowManagerGlobal;
Winson35f30502015-09-28 11:24:36 -070036import android.view.animation.AnimationUtils;
37import android.view.animation.Interpolator;
Winson Chung303e1ff2014-03-07 15:06:19 -080038import android.widget.FrameLayout;
Winson Chung5c9f4b92015-06-25 16:16:46 -070039import com.android.internal.logging.MetricsLogger;
Winson Chungd16c5652015-01-26 16:11:07 -080040import com.android.systemui.R;
Winson Chung303e1ff2014-03-07 15:06:19 -080041import com.android.systemui.recents.Constants;
Winson Chungaee097c2015-04-02 18:16:02 -070042import com.android.systemui.recents.RecentsAppWidgetHostView;
Winson Chung303e1ff2014-03-07 15:06:19 -080043import com.android.systemui.recents.RecentsConfiguration;
Winson2536c7e2015-10-01 15:49:31 -070044import com.android.systemui.recents.events.EventBus;
45import com.android.systemui.recents.events.ui.DismissTaskEvent;
Winson Chung1f24c7e2014-07-11 17:06:48 -070046import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chungf1fbd772014-06-24 18:06:58 -070047import com.android.systemui.recents.model.RecentsTaskLoader;
Winson Chung303e1ff2014-03-07 15:06:19 -080048import com.android.systemui.recents.model.Task;
49import com.android.systemui.recents.model.TaskStack;
50
51import java.util.ArrayList;
Winson Chung6ac8bd62015-01-07 16:38:35 -080052import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080053
Winson Chung303e1ff2014-03-07 15:06:19 -080054/**
55 * This view is the the top level layout that contains TaskStacks (which are laid out according
56 * to their SpaceNode bounds.
57 */
Winsone6c90732015-09-24 16:06:29 -070058public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks {
Winson Chung47c4c692014-03-17 10:17:11 -070059
Jorim Jaggi6e18e002015-06-02 17:07:39 -070060 private static final String TAG = "RecentsView";
61
Filip Gruszczynski170192a2015-08-16 17:46:34 -070062 private static final boolean ADD_HEADER_BITMAP = true;
63
Winson Chung47c4c692014-03-17 10:17:11 -070064 /** The RecentsView callbacks */
65 public interface RecentsViewCallbacks {
Winson Chung7aceb9a2014-07-03 13:38:01 -070066 public void onTaskViewClicked();
Winson Chung4e96eb72014-09-17 15:16:09 +020067 public void onTaskLaunchFailed();
Winson Chung7aceb9a2014-07-03 13:38:01 -070068 public void onAllTaskViewsDismissed();
Winson Chungcdbbb7e2014-06-24 12:11:49 -070069 public void onExitToHomeAnimationTriggered();
Jason Monk18f99d92014-09-11 13:36:42 -040070 public void onScreenPinningRequest();
Jorim Jaggi6e18e002015-06-02 17:07:39 -070071 public void runAfterPause(Runnable r);
Winson Chung47c4c692014-03-17 10:17:11 -070072 }
73
Winson Chungd42a6cf2014-06-03 16:24:04 -070074 RecentsConfiguration mConfig;
75 LayoutInflater mInflater;
76
Winson Chungdcfa7972014-07-22 12:27:13 -070077 ArrayList<TaskStack> mStacks;
Winson147ecaf2015-09-16 16:49:55 -070078 TaskStackView mTaskStackView;
Winson Chungaee097c2015-04-02 18:16:02 -070079 RecentsAppWidgetHostView mSearchBar;
Winson Chung47c4c692014-03-17 10:17:11 -070080 RecentsViewCallbacks mCb;
Winson35f30502015-09-28 11:24:36 -070081 Interpolator mFastOutSlowInInterpolator;
82
83 Rect mSystemInsets = new Rect();
Winson Chung303e1ff2014-03-07 15:06:19 -080084
85 public RecentsView(Context context) {
86 super(context);
Winson Chung8e548f72014-06-24 14:40:53 -070087 }
88
89 public RecentsView(Context context, AttributeSet attrs) {
90 this(context, attrs, 0);
91 }
92
93 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
94 this(context, attrs, defStyleAttr, 0);
95 }
96
97 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
98 super(context, attrs, defStyleAttr, defStyleRes);
Winson Chungd42a6cf2014-06-03 16:24:04 -070099 mConfig = RecentsConfiguration.getInstance();
Winson Chungecd9b302014-04-16 17:07:18 -0700100 mInflater = LayoutInflater.from(context);
Winson35f30502015-09-28 11:24:36 -0700101 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
102 com.android.internal.R.interpolator.fast_out_slow_in);
Winson Chungd16c5652015-01-26 16:11:07 -0800103 }
104
Winson Chung47c4c692014-03-17 10:17:11 -0700105 /** Sets the callbacks */
106 public void setCallbacks(RecentsViewCallbacks cb) {
107 mCb = cb;
108 }
109
Winson Chungdcfa7972014-07-22 12:27:13 -0700110 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700111 public void setTaskStack(TaskStack stack) {
Winson35f30502015-09-28 11:24:36 -0700112 if (mConfig.getLaunchState().launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700113 if (mTaskStackView != null) {
114 // If onRecentsHidden is not triggered, we need to the stack view again here
115 mTaskStackView.reset();
116 mTaskStackView.setStack(stack);
117 } else {
118 mTaskStackView = new TaskStackView(getContext(), stack);
119 mTaskStackView.setCallbacks(this);
120 addView(mTaskStackView);
121 }
122 } else {
123 if (mTaskStackView != null) {
124 removeView(mTaskStackView);
125 }
126 mTaskStackView = new TaskStackView(getContext(), stack);
127 mTaskStackView.setCallbacks(this);
128 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800129 }
Winson Chung02d49272014-08-29 13:57:29 -0700130
Winson Chungb0a28ea2014-10-28 15:21:35 -0700131 // Trigger a new layout
132 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700133 }
134
Skuhne8aa7d162015-03-20 13:40:53 -0700135 /** Gets the next task in the stack - or if the last - the top task */
136 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700137 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700138 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700139 if (mTaskStackView != null) {
140 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700141 ArrayList<Task> taskList = stack.getTasks();
142 // Iterate the stack views and try and find the focused task
143 for (int j = taskList.size() - 1; j >= 0; --j) {
144 Task task = taskList.get(j);
145 // Return the next task in the line.
146 if (found)
147 return task;
148 // Remember the first possible task as the top task.
149 if (returnTask == null)
150 returnTask = task;
151 if (task == taskToSearch)
152 found = true;
153 }
154 }
155 return returnTask;
156 }
157
Winson Chung1e8d71b2014-05-16 17:05:22 -0700158 /** Launches the focused task from the first stack if possible */
159 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700160 if (mTaskStackView != null) {
161 TaskStack stack = mTaskStackView.getStack();
Winson Chungd16c5652015-01-26 16:11:07 -0800162 // Iterate the stack views and try and find the focused task
Winson147ecaf2015-09-16 16:49:55 -0700163 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Winson Chungd16c5652015-01-26 16:11:07 -0800164 int taskViewCount = taskViews.size();
165 for (int j = 0; j < taskViewCount; j++) {
166 TaskView tv = taskViews.get(j);
167 Task task = tv.getTask();
168 if (tv.isFocusedTask()) {
Winson147ecaf2015-09-16 16:49:55 -0700169 onTaskViewClicked(mTaskStackView, tv, stack, task, false, false, null);
Winson Chungd16c5652015-01-26 16:11:07 -0800170 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700171 }
172 }
173 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700174 return false;
175 }
176
Skuhne8aa7d162015-03-20 13:40:53 -0700177 /** Launches a given task. */
Chong Zhang0fa656b2015-08-31 15:17:21 -0700178 public boolean launchTask(Task task, Rect taskBounds) {
Winson147ecaf2015-09-16 16:49:55 -0700179 if (mTaskStackView != null) {
180 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700181 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700182 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700183 int taskViewCount = taskViews.size();
184 for (int j = 0; j < taskViewCount; j++) {
185 TaskView tv = taskViews.get(j);
186 if (tv.getTask() == task) {
Winsonb1bbaed2015-09-23 15:45:11 -0700187 onTaskViewClicked(mTaskStackView, tv, stack, task, false, taskBounds != null,
188 taskBounds);
Skuhne8aa7d162015-03-20 13:40:53 -0700189 return true;
190 }
191 }
192 }
193 return false;
194 }
195
Winson Chungdcfa7972014-07-22 12:27:13 -0700196 /** Launches the task that Recents was launched from, if possible */
197 public boolean launchPreviousTask() {
Winson147ecaf2015-09-16 16:49:55 -0700198 if (mTaskStackView != null) {
199 TaskStack stack = mTaskStackView.getStack();
Winson Chungd16c5652015-01-26 16:11:07 -0800200 ArrayList<Task> tasks = stack.getTasks();
Winson Chung47c4c692014-03-17 10:17:11 -0700201
Winson Chungd16c5652015-01-26 16:11:07 -0800202 // Find the launch task in the stack
203 if (!tasks.isEmpty()) {
204 int taskCount = tasks.size();
205 for (int j = 0; j < taskCount; j++) {
206 if (tasks.get(j).isLaunchTarget) {
207 Task task = tasks.get(j);
Winson147ecaf2015-09-16 16:49:55 -0700208 TaskView tv = mTaskStackView.getChildViewForTask(task);
209 onTaskViewClicked(mTaskStackView, tv, stack, task, false, false, null);
Winson Chungd16c5652015-01-26 16:11:07 -0800210 return true;
Winson Chung303e1ff2014-03-07 15:06:19 -0800211 }
212 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800213 }
214 }
215 return false;
216 }
217
Winson Chung24cf1522014-05-29 12:03:33 -0700218 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700219 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800220 // We have to increment/decrement the post animation trigger in case there are no children
221 // to ensure that it runs
222 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700223 if (mTaskStackView != null) {
224 mTaskStackView.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700225 }
Winson Chung740c3ac2014-11-12 16:14:38 -0800226 ctx.postAnimationTrigger.decrement();
Winson Chung24cf1522014-05-29 12:03:33 -0700227 }
228
Winson Chungd42a6cf2014-06-03 16:24:04 -0700229 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700230 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chunga91c2932014-11-07 15:02:38 -0800231 // We have to increment/decrement the post animation trigger in case there are no children
232 // to ensure that it runs
233 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700234 if (mTaskStackView != null) {
235 mTaskStackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700236 }
Winson Chunga91c2932014-11-07 15:02:38 -0800237 ctx.postAnimationTrigger.decrement();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700238
Winson Chung969f5862014-06-16 17:08:24 -0700239 // Notify of the exit animation
Winson Chungcdbbb7e2014-06-24 12:11:49 -0700240 mCb.onExitToHomeAnimationTriggered();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700241 }
242
Winson Chungf7bca432014-04-30 17:11:13 -0700243 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700244 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700245 // Remove the previous search bar if one exists
246 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
247 removeView(mSearchBar);
248 }
249 // Add the new search bar
250 if (searchBar != null) {
251 mSearchBar = searchBar;
252 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700253 }
Winson Chungecd9b302014-04-16 17:07:18 -0700254 }
255
Winson Chung772b6b12014-07-03 15:54:02 -0700256 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700257 public boolean hasValidSearchBar() {
258 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700259 }
260
261 /** Sets the visibility of the search bar */
262 public void setSearchBarVisibility(int visibility) {
263 if (mSearchBar != null) {
264 mSearchBar.setVisibility(visibility);
Winson Chung012ef362014-07-31 18:36:25 -0700265 // Always bring the search bar to the top
266 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700267 }
268 }
269
Winson Chungf7bca432014-04-30 17:11:13 -0700270 /**
271 * This is called with the full size of the window since we are handling our own insets.
272 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800273 @Override
274 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
275 int width = MeasureSpec.getSize(widthMeasureSpec);
276 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700277
Winson Chungf7bca432014-04-30 17:11:13 -0700278 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700279 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700280 if (mSearchBar != null) {
Winson35f30502015-09-28 11:24:36 -0700281 mConfig.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700282 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700283 mSearchBar.measure(
284 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
285 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700286 }
287
Winson Chungf7bca432014-04-30 17:11:13 -0700288 Rect taskStackBounds = new Rect();
Winson35f30502015-09-28 11:24:36 -0700289 mConfig.getAvailableTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
290 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700291 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
292 mTaskStackView.setTaskStackBounds(taskStackBounds);
293 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800294 }
295
296 setMeasuredDimension(width, height);
297 }
298
Winson Chungf7bca432014-04-30 17:11:13 -0700299 /**
300 * This is called with the full size of the window since we are handling our own insets.
301 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800302 @Override
303 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf7bca432014-04-30 17:11:13 -0700304 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700305 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
306 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700307 if (mSearchBar != null) {
Winson147ecaf2015-09-16 16:49:55 -0700308 mConfig.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700309 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700310 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
311 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700312 }
313
Winson147ecaf2015-09-16 16:49:55 -0700314 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
315 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800316 }
317 }
318
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700319 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700320 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700321 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700322 requestLayout();
Winson Chungdcfa7972014-07-22 12:27:13 -0700323 return insets.consumeSystemWindowInsets();
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700324 }
325
Winson Chunga26fb782014-06-12 17:52:39 -0700326 /** Notifies each task view of the user interaction. */
327 public void onUserInteraction() {
328 // Get the first stack view
Winson147ecaf2015-09-16 16:49:55 -0700329 if (mTaskStackView != null) {
330 mTaskStackView.onUserInteraction();
Winson Chunga26fb782014-06-12 17:52:39 -0700331 }
332 }
333
Winson Chung1e8d71b2014-05-16 17:05:22 -0700334 /** Focuses the next task in the first stack view */
335 public void focusNextTask(boolean forward) {
336 // Get the first stack view
Winson147ecaf2015-09-16 16:49:55 -0700337 if (mTaskStackView != null) {
338 mTaskStackView.focusNextTask(forward, true);
Winson Chung1e8d71b2014-05-16 17:05:22 -0700339 }
Winson Chunga0e88b52014-08-11 19:25:42 -0700340 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700341
Winson Chunga0e88b52014-08-11 19:25:42 -0700342 /** Dismisses the focused task. */
343 public void dismissFocusedTask() {
344 // Get the first stack view
Winson147ecaf2015-09-16 16:49:55 -0700345 if (mTaskStackView != null) {
346 mTaskStackView.dismissFocusedTask();
Winson Chung1e8d71b2014-05-16 17:05:22 -0700347 }
348 }
349
Winson Chung303e1ff2014-03-07 15:06:19 -0800350 /** Unfilters any filtered stacks */
351 public boolean unfilterFilteredStacks() {
Winson Chungdcfa7972014-07-22 12:27:13 -0700352 if (mStacks != null) {
Winson Chung303e1ff2014-03-07 15:06:19 -0800353 // Check if there are any filtered stacks and unfilter them before we back out of Recents
354 boolean stacksUnfiltered = false;
Winson Chungdcfa7972014-07-22 12:27:13 -0700355 int numStacks = mStacks.size();
356 for (int i = 0; i < numStacks; i++) {
357 TaskStack stack = mStacks.get(i);
Winson Chung303e1ff2014-03-07 15:06:19 -0800358 if (stack.hasFilteredTasks()) {
359 stack.unfilterTasks();
360 stacksUnfiltered = true;
361 }
362 }
363 return stacksUnfiltered;
364 }
365 return false;
366 }
367
Jorim Jaggi900fb482015-06-02 15:07:33 -0700368 public void disableLayersForOneFrame() {
Winson147ecaf2015-09-16 16:49:55 -0700369 if (mTaskStackView != null) {
370 mTaskStackView.disableLayersForOneFrame();
Jorim Jaggi900fb482015-06-02 15:07:33 -0700371 }
372 }
373
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700374 private void postDrawHeaderThumbnailTransitionRunnable(final TaskStackView view,
375 final TaskView clickedView, final int offsetX, final int offsetY,
376 final float stackScroll,
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700377 final ActivityOptions.OnAnimationStartedListener animStartedListener) {
378 Runnable r = new Runnable() {
379 @Override
380 public void run() {
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700381 overrideDrawHeaderThumbnailTransition(view, clickedView, offsetX, offsetY,
382 stackScroll, animStartedListener);
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700383
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700384 }
385 };
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700386
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700387 mCb.runAfterPause(r);
388 }
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700389
390 private void overrideDrawHeaderThumbnailTransition(TaskStackView stackView,
391 TaskView clickedTask, int offsetX, int offsetY, float stackScroll,
392 final ActivityOptions.OnAnimationStartedListener animStartedListener) {
393 List<AppTransitionAnimationSpec> specs = getAppTransitionAnimationSpecs(stackView,
394 clickedTask, offsetX, offsetY, stackScroll);
395 if (specs == null) {
396 return;
397 }
398
399 IRemoteCallback.Stub callback = new IRemoteCallback.Stub() {
400 @Override
401 public void sendResult(Bundle data) throws RemoteException {
402 post(new Runnable() {
403 @Override
404 public void run() {
405 if (animStartedListener != null) {
406 animStartedListener.onAnimationStarted();
407 }
408 }
409 });
410 }
411 };
412
413 AppTransitionAnimationSpec[] specsArray =
414 new AppTransitionAnimationSpec[specs.size()];
415 try {
416 WindowManagerGlobal.getWindowManagerService().overridePendingAppTransitionMultiThumb(
417 specs.toArray(specsArray), callback, true /* scaleUp */);
418
419 } catch (RemoteException e) {
420 Log.w(TAG, "Error overriding app transition", e);
421 }
422 }
423
424 private List<AppTransitionAnimationSpec> getAppTransitionAnimationSpecs(TaskStackView stackView,
425 TaskView clickedTask, int offsetX, int offsetY, float stackScroll) {
426 final int targetStackId = clickedTask.getTask().key.stackId;
427 if (targetStackId != ActivityManager.FREEFORM_WORKSPACE_STACK_ID
428 && targetStackId != ActivityManager.FULLSCREEN_WORKSPACE_STACK_ID) {
429 return null;
430 }
431 // If this is a full screen stack, the transition will be towards the single, full screen
432 // task. We only need the transition spec for this task.
433 List<AppTransitionAnimationSpec> specs = new ArrayList<>();
434 if (targetStackId == ActivityManager.FULLSCREEN_WORKSPACE_STACK_ID) {
435 specs.add(createThumbnailHeaderAnimationSpec(
436 stackView, offsetX, offsetY, stackScroll, clickedTask,
437 clickedTask.getTask().key.id, ADD_HEADER_BITMAP));
438 return specs;
439 }
440 // This is a free form stack or full screen stack, so there will be multiple windows
441 // animating from thumbnails. We need transition animation specs for all of them.
442
443 // We will use top and bottom task views as a base for tasks, that aren't visible on the
444 // screen. This is necessary for cascade recents list, where some of the tasks might be
445 // hidden.
446 List<TaskView> taskViews = stackView.getTaskViews();
447 int childCount = taskViews.size();
448 TaskView topChild = taskViews.get(0);
449 TaskView bottomChild = taskViews.get(childCount - 1);
450 SparseArray<TaskView> taskViewsByTaskId = new SparseArray<>();
451 for (int i = 0; i < childCount; i++) {
452 TaskView taskView = taskViews.get(i);
453 taskViewsByTaskId.put(taskView.getTask().key.id, taskView);
454 }
455
456 TaskStack stack = stackView.getStack();
457 // We go through all tasks now and for each generate transition animation spec. If there is
458 // a view associated with a task, we use that view as a base for the animation. If there
459 // isn't, we use bottom or top view, depending on which one would be closer to the task
460 // view if it existed.
461 ArrayList<Task> tasks = stack.getTasks();
462 boolean passedClickedTask = false;
463 for (int i = 0, n = tasks.size(); i < n; i++) {
464 Task task = tasks.get(i);
465 TaskView taskView = taskViewsByTaskId.get(task.key.id);
466 if (taskView != null) {
467 specs.add(createThumbnailHeaderAnimationSpec(stackView, offsetX, offsetY,
468 stackScroll, taskView, taskView.getTask().key.id, ADD_HEADER_BITMAP));
469 if (taskView == clickedTask) {
470 passedClickedTask = true;
471 }
472 } else {
473 taskView = passedClickedTask ? bottomChild : topChild;
474 specs.add(createThumbnailHeaderAnimationSpec(stackView, offsetX, offsetY,
475 stackScroll, taskView, task.key.id, !ADD_HEADER_BITMAP));
476 }
477 }
478
479 return specs;
480 }
481
482 private AppTransitionAnimationSpec createThumbnailHeaderAnimationSpec(TaskStackView stackView,
483 int offsetX, int offsetY, float stackScroll, TaskView tv, int taskId,
484 boolean addHeaderBitmap) {
485 // Disable any focused state before we draw the header
486 // Upfront the processing of the thumbnail
487 if (tv.isFocusedTask()) {
488 tv.unsetFocusedTask();
489 }
490 TaskViewTransform transform = new TaskViewTransform();
491 transform = stackView.getStackAlgorithm().getStackTransform(tv.mTask, stackScroll,
492 transform, null);
493
494 float scale = tv.getScaleX();
495 int fromHeaderWidth = (int) (tv.mHeaderView.getMeasuredWidth() * scale);
496 int fromHeaderHeight = (int) (tv.mHeaderView.getMeasuredHeight() * scale);
497
498 Bitmap b = null;
499 if (addHeaderBitmap) {
500 b = Bitmap.createBitmap(fromHeaderWidth, fromHeaderHeight,
501 Bitmap.Config.ARGB_8888);
502
503 if (Constants.DebugFlags.App.EnableTransitionThumbnailDebugMode) {
504 b.eraseColor(0xFFff0000);
505 } else {
506 Canvas c = new Canvas(b);
507 c.scale(tv.getScaleX(), tv.getScaleY());
508 tv.mHeaderView.draw(c);
509 c.setBitmap(null);
510
511 }
512 b = b.createAshmemBitmap();
513 }
514
515 int[] pts = new int[2];
516 tv.getLocationOnScreen(pts);
517
518 final int left = pts[0] + offsetX;
519 final int top = pts[1] + offsetY;
520 final Rect rect = new Rect(left, top, left + transform.rect.width(),
521 top + transform.rect.height());
522
523 return new AppTransitionAnimationSpec(taskId, b, rect);
524 }
525
Winson Chung47c4c692014-03-17 10:17:11 -0700526 /**** TaskStackView.TaskStackCallbacks Implementation ****/
Winson Chung303e1ff2014-03-07 15:06:19 -0800527
528 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700529 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
Chong Zhang0fa656b2015-08-31 15:17:21 -0700530 final TaskStack stack, final Task task, final boolean lockToTask,
531 final boolean boundsValid, final Rect bounds) {
Jorim Jaggi88f3db92015-06-02 16:29:40 -0700532
Winson Chung47c4c692014-03-17 10:17:11 -0700533 // Notify any callbacks of the launching of a new task
534 if (mCb != null) {
Winson Chung7aceb9a2014-07-03 13:38:01 -0700535 mCb.onTaskViewClicked();
Winson Chung47c4c692014-03-17 10:17:11 -0700536 }
537
Winson Chunge0e45bc2014-06-17 17:56:17 -0700538 // Upfront the processing of the thumbnail
Winson Chungffa2ec62014-07-03 15:54:42 -0700539 TaskViewTransform transform = new TaskViewTransform();
Jorim Jaggicb557032014-09-16 23:09:24 +0200540 View sourceView;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700541 int offsetX = 0;
542 int offsetY = 0;
Winson Chung012ef362014-07-31 18:36:25 -0700543 float stackScroll = stackView.getScroller().getStackScroll();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700544 if (tv == null) {
545 // If there is no actual task view, then use the stack view as the source view
546 // and then offset to the expected transform rect, but bound this to just
547 // outside the display rect (to ensure we don't animate from too far away)
548 sourceView = stackView;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700549 offsetX = transform.rect.left;
Winson35f30502015-09-28 11:24:36 -0700550 offsetY = getMeasuredHeight();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700551 } else {
Jorim Jaggicb557032014-09-16 23:09:24 +0200552 sourceView = tv.mThumbnailView;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700553 }
554
555 // Compute the thumbnail to scale up from
Winson Chung1f24c7e2014-07-11 17:06:48 -0700556 final SystemServicesProxy ssp =
557 RecentsTaskLoader.getInstance().getSystemServicesProxy();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700558 ActivityOptions opts = null;
Chong Zhang0fa656b2015-08-31 15:17:21 -0700559 ActivityOptions.OnAnimationStartedListener animStartedListener = null;
Winson Chung2e7f3bd2014-09-05 13:17:22 +0200560 if (task.thumbnail != null && task.thumbnail.getWidth() > 0 &&
561 task.thumbnail.getHeight() > 0) {
Winson Chung1f24c7e2014-07-11 17:06:48 -0700562 if (lockToTask) {
563 animStartedListener = new ActivityOptions.OnAnimationStartedListener() {
564 boolean mTriggered = false;
565 @Override
566 public void onAnimationStarted() {
567 if (!mTriggered) {
568 postDelayed(new Runnable() {
569 @Override
570 public void run() {
Jason Monk18f99d92014-09-11 13:36:42 -0400571 mCb.onScreenPinningRequest();
Winson Chung1f24c7e2014-07-11 17:06:48 -0700572 }
573 }, 350);
574 mTriggered = true;
575 }
576 }
577 };
578 }
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700579 postDrawHeaderThumbnailTransitionRunnable(stackView, tv, offsetX, offsetY, stackScroll,
580 animStartedListener);
Winsonec417e42015-09-29 12:42:29 -0700581 opts = ActivityOptions.makeThumbnailAspectScaleUpAnimation(sourceView,
582 Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8).createAshmemBitmap(),
583 offsetX, offsetY, transform.rect.width(), transform.rect.height(),
584 sourceView.getHandler(), animStartedListener);
Chong Zhang0fa656b2015-08-31 15:17:21 -0700585 } else {
586 opts = ActivityOptions.makeBasic();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700587 }
Chong Zhang0fa656b2015-08-31 15:17:21 -0700588 if (boundsValid) {
Winsonb1bbaed2015-09-23 15:45:11 -0700589 opts.setBounds(bounds.isEmpty() ? null : bounds);
Chong Zhang0fa656b2015-08-31 15:17:21 -0700590 }
Winson Chunge0e45bc2014-06-17 17:56:17 -0700591 final ActivityOptions launchOpts = opts;
Chong Zhang0fa656b2015-08-31 15:17:21 -0700592 final boolean screenPinningRequested = (animStartedListener == null) && lockToTask;
Winson Chung303e1ff2014-03-07 15:06:19 -0800593 final Runnable launchRunnable = new Runnable() {
594 @Override
595 public void run() {
Winson Chung4be04452014-03-24 17:22:30 -0700596 if (task.isActive) {
597 // Bring an active task to the foreground
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700598 ssp.moveTaskToFront(task.key.id, launchOpts);
Winson Chung4be04452014-03-24 17:22:30 -0700599 } else {
Winson Chung4e96eb72014-09-17 15:16:09 +0200600 if (ssp.startActivityFromRecents(getContext(), task.key.id,
601 task.activityLabel, launchOpts)) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700602 if (screenPinningRequested) {
Jason Monk18f99d92014-09-11 13:36:42 -0400603 mCb.onScreenPinningRequest();
Winson Chung4be04452014-03-24 17:22:30 -0700604 }
Winson Chung4e96eb72014-09-17 15:16:09 +0200605 } else {
606 // Dismiss the task and return the user to home if we fail to
607 // launch the task
Winson2536c7e2015-10-01 15:49:31 -0700608 EventBus.getDefault().send(new DismissTaskEvent(task, tv));
Winson Chung4e96eb72014-09-17 15:16:09 +0200609 if (mCb != null) {
610 mCb.onTaskLaunchFailed();
611 }
Winson Chung5c9f4b92015-06-25 16:16:46 -0700612
613 // Keep track of failed launches
614 MetricsLogger.count(getContext(), "overview_task_launch_failed", 1);
Winson Chung4be04452014-03-24 17:22:30 -0700615 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800616 }
617 }
618 };
619
Winson Chung5c9f4b92015-06-25 16:16:46 -0700620 // Keep track of the index of the task launch
621 int taskIndexFromFront = 0;
622 int taskIndex = stack.indexOfTask(task);
623 if (taskIndex > -1) {
624 taskIndexFromFront = stack.getTaskCount() - taskIndex - 1;
625 }
626 MetricsLogger.histogram(getContext(), "overview_task_launch_index", taskIndexFromFront);
627
Winson Chung303e1ff2014-03-07 15:06:19 -0800628 // Launch the app right away if there is no task view, otherwise, animate the icon out first
Winson Chung814086d2014-05-07 15:01:14 -0700629 if (tv == null) {
Winson Chunga91c2932014-11-07 15:02:38 -0800630 launchRunnable.run();
Winson Chung303e1ff2014-03-07 15:06:19 -0800631 } else {
Winson Chunge1e20e12015-06-02 14:11:49 -0700632 if (task.group != null && !task.group.isFrontMostTask(task)) {
Winson Chunga4ccb862014-08-22 15:26:27 -0700633 // For affiliated tasks that are behind other tasks, we must animate the front cards
634 // out of view before starting the task transition
Winson Chungebfc6982014-08-26 12:25:34 -0700635 stackView.startLaunchTaskAnimation(tv, launchRunnable, lockToTask);
Winson Chunga4ccb862014-08-22 15:26:27 -0700636 } else {
637 // Otherwise, we can start the task transition immediately
Winson Chungebfc6982014-08-26 12:25:34 -0700638 stackView.startLaunchTaskAnimation(tv, null, lockToTask);
Winson Chunga91c2932014-11-07 15:02:38 -0800639 launchRunnable.run();
Winson Chunga4ccb862014-08-22 15:26:27 -0700640 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800641 }
642 }
Winson Chung9f9679d2014-04-11 16:49:09 -0700643
644 @Override
Winson Chung6ac8bd62015-01-07 16:38:35 -0800645 public void onAllTaskViewsDismissed(ArrayList<Task> removedTasks) {
Winson2536c7e2015-10-01 15:49:31 -0700646 /* TODO: Not currently enabled
Winson Chung6ac8bd62015-01-07 16:38:35 -0800647 if (removedTasks != null) {
648 int taskCount = removedTasks.size();
649 for (int i = 0; i < taskCount; i++) {
650 onTaskViewDismissed(removedTasks.get(i));
651 }
652 }
Winson2536c7e2015-10-01 15:49:31 -0700653 */
Winson Chung6ac8bd62015-01-07 16:38:35 -0800654
Winson Chung7aceb9a2014-07-03 13:38:01 -0700655 mCb.onAllTaskViewsDismissed();
Winson Chung5c9f4b92015-06-25 16:16:46 -0700656
657 // Keep track of all-deletions
658 MetricsLogger.count(getContext(), "overview_task_all_dismissed", 1);
Winson Chungd7b2cb12014-06-26 15:08:50 -0700659 }
660
Winson Chungb0a28ea2014-10-28 15:21:35 -0700661 /** Final callback after Recents is finally hidden. */
662 public void onRecentsHidden() {
663 // Notify each task stack view
Winson147ecaf2015-09-16 16:49:55 -0700664 if (mTaskStackView != null) {
665 mTaskStackView.onRecentsHidden();
Winson Chungb0a28ea2014-10-28 15:21:35 -0700666 }
667 }
668
Winson Chungd7b2cb12014-06-26 15:08:50 -0700669 @Override
Winson Chung8e548f72014-06-24 14:40:53 -0700670 public void onTaskStackFilterTriggered() {
671 // Hide the search bar
672 if (mSearchBar != null) {
Winson35f30502015-09-28 11:24:36 -0700673 int filterDuration = getResources().getInteger(
674 R.integer.recents_filter_animate_current_views_duration);
Winson Chung8e548f72014-06-24 14:40:53 -0700675 mSearchBar.animate()
676 .alpha(0f)
677 .setStartDelay(0)
Winson35f30502015-09-28 11:24:36 -0700678 .setInterpolator(mFastOutSlowInInterpolator)
679 .setDuration(filterDuration)
Winson Chung8e548f72014-06-24 14:40:53 -0700680 .withLayer()
681 .start();
682 }
683 }
684
685 @Override
686 public void onTaskStackUnfilterTriggered() {
687 // Show the search bar
688 if (mSearchBar != null) {
Winson35f30502015-09-28 11:24:36 -0700689 int filterDuration = getResources().getInteger(
690 R.integer.recents_filter_animate_new_views_duration);
Winson Chung8e548f72014-06-24 14:40:53 -0700691 mSearchBar.animate()
692 .alpha(1f)
693 .setStartDelay(0)
Winson35f30502015-09-28 11:24:36 -0700694 .setInterpolator(mFastOutSlowInInterpolator)
695 .setDuration(filterDuration)
Winson Chung8e548f72014-06-24 14:40:53 -0700696 .withLayer()
697 .start();
698 }
699 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800700}