blob: 24871d47dfe2caf45b710a61ae67f76878206804 [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
19import android.app.ActivityOptions;
20import android.content.Context;
Winson Chung303e1ff2014-03-07 15:06:19 -080021import android.graphics.Bitmap;
22import android.graphics.Canvas;
23import android.graphics.Rect;
Winson4165d3362015-10-10 14:40:35 -070024import android.graphics.drawable.Drawable;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070025import android.os.Bundle;
26import android.os.IRemoteCallback;
27import android.os.RemoteException;
Winson882072b2015-10-12 11:26:33 -070028import android.util.ArraySet;
Winson Chung8e548f72014-06-24 14:40:53 -070029import android.util.AttributeSet;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070030import android.util.Log;
Filip Gruszczynski170192a2015-08-16 17:46:34 -070031import android.util.SparseArray;
32import android.view.AppTransitionAnimationSpec;
Winson Chungecd9b302014-04-16 17:07:18 -070033import android.view.LayoutInflater;
Winsonbe7607a2015-10-01 17:24:51 -070034import android.view.MotionEvent;
Winson Chung303e1ff2014-03-07 15:06:19 -080035import android.view.View;
Winson Chung653f70c22014-05-19 14:49:42 -070036import android.view.WindowInsets;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070037import android.view.WindowManagerGlobal;
Winson35f30502015-09-28 11:24:36 -070038import android.view.animation.AnimationUtils;
39import android.view.animation.Interpolator;
Winson Chung303e1ff2014-03-07 15:06:19 -080040import android.widget.FrameLayout;
Winson Chung5c9f4b92015-06-25 16:16:46 -070041import com.android.internal.logging.MetricsLogger;
Winson Chungd16c5652015-01-26 16:11:07 -080042import com.android.systemui.R;
Winson Chung303e1ff2014-03-07 15:06:19 -080043import com.android.systemui.recents.Constants;
Winsone7f138c2015-10-22 16:15:21 -070044import com.android.systemui.recents.Recents;
Winsonbe7607a2015-10-01 17:24:51 -070045import com.android.systemui.recents.RecentsActivity;
Winsonc28098f2015-10-30 14:50:19 -070046import com.android.systemui.recents.RecentsActivityLaunchState;
Winson Chungaee097c2015-04-02 18:16:02 -070047import com.android.systemui.recents.RecentsAppWidgetHostView;
Winson Chung303e1ff2014-03-07 15:06:19 -080048import com.android.systemui.recents.RecentsConfiguration;
Winson2536c7e2015-10-01 15:49:31 -070049import com.android.systemui.recents.events.EventBus;
Winson412e1802015-10-20 16:57:57 -070050import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
51import com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
Winson0d14d4d2015-10-26 17:05:04 -070052import com.android.systemui.recents.events.ui.DismissTaskViewEvent;
Winsoneca4ab62015-11-04 10:50:28 -080053import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
Winsonbe7607a2015-10-01 17:24:51 -070054import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
55import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
Winson Chung1f24c7e2014-07-11 17:06:48 -070056import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chung303e1ff2014-03-07 15:06:19 -080057import com.android.systemui.recents.model.Task;
58import com.android.systemui.recents.model.TaskStack;
59
60import java.util.ArrayList;
Winson Chung6ac8bd62015-01-07 16:38:35 -080061import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080062
Wale Ogunwale3797c222015-10-27 14:21:58 -070063import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
64import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
65import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070066
Winson Chung303e1ff2014-03-07 15:06:19 -080067/**
68 * This view is the the top level layout that contains TaskStacks (which are laid out according
69 * to their SpaceNode bounds.
70 */
Winsone6c90732015-09-24 16:06:29 -070071public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks {
Winson Chung47c4c692014-03-17 10:17:11 -070072
Jorim Jaggi6e18e002015-06-02 17:07:39 -070073 private static final String TAG = "RecentsView";
Winson0d14d4d2015-10-26 17:05:04 -070074 private static final boolean DEBUG = false;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070075
Filip Gruszczynski170192a2015-08-16 17:46:34 -070076 private static final boolean ADD_HEADER_BITMAP = true;
77
Winson Chung47c4c692014-03-17 10:17:11 -070078 /** The RecentsView callbacks */
79 public interface RecentsViewCallbacks {
Winson Chung7aceb9a2014-07-03 13:38:01 -070080 public void onTaskViewClicked();
Winson Chung4e96eb72014-09-17 15:16:09 +020081 public void onTaskLaunchFailed();
Winson Chung7aceb9a2014-07-03 13:38:01 -070082 public void onAllTaskViewsDismissed();
Jorim Jaggi6e18e002015-06-02 17:07:39 -070083 public void runAfterPause(Runnable r);
Winson Chung47c4c692014-03-17 10:17:11 -070084 }
85
Winson Chungd42a6cf2014-06-03 16:24:04 -070086 LayoutInflater mInflater;
87
Winson Chungdcfa7972014-07-22 12:27:13 -070088 ArrayList<TaskStack> mStacks;
Winson147ecaf2015-09-16 16:49:55 -070089 TaskStackView mTaskStackView;
Winson Chungaee097c2015-04-02 18:16:02 -070090 RecentsAppWidgetHostView mSearchBar;
Winson Chung47c4c692014-03-17 10:17:11 -070091 RecentsViewCallbacks mCb;
Winsonbe7607a2015-10-01 17:24:51 -070092
93 RecentsViewTouchHandler mTouchHandler;
94 DragView mDragView;
Winson882072b2015-10-12 11:26:33 -070095 TaskStack.DockState[] mVisibleDockStates = {
96 TaskStack.DockState.LEFT,
97 TaskStack.DockState.TOP,
98 TaskStack.DockState.RIGHT,
99 TaskStack.DockState.BOTTOM,
100 };
Winsonbe7607a2015-10-01 17:24:51 -0700101
Winson35f30502015-09-28 11:24:36 -0700102 Interpolator mFastOutSlowInInterpolator;
103
104 Rect mSystemInsets = new Rect();
Winson Chung303e1ff2014-03-07 15:06:19 -0800105
106 public RecentsView(Context context) {
107 super(context);
Winson Chung8e548f72014-06-24 14:40:53 -0700108 }
109
110 public RecentsView(Context context, AttributeSet attrs) {
111 this(context, attrs, 0);
112 }
113
114 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
115 this(context, attrs, defStyleAttr, 0);
116 }
117
118 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
119 super(context, attrs, defStyleAttr, defStyleRes);
Winsonbe7607a2015-10-01 17:24:51 -0700120 setWillNotDraw(false);
Winson Chungecd9b302014-04-16 17:07:18 -0700121 mInflater = LayoutInflater.from(context);
Winson35f30502015-09-28 11:24:36 -0700122 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
123 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonbe7607a2015-10-01 17:24:51 -0700124 mTouchHandler = new RecentsViewTouchHandler(this);
Winson Chungd16c5652015-01-26 16:11:07 -0800125 }
126
Winson Chung47c4c692014-03-17 10:17:11 -0700127 /** Sets the callbacks */
128 public void setCallbacks(RecentsViewCallbacks cb) {
129 mCb = cb;
130 }
131
Winson Chungdcfa7972014-07-22 12:27:13 -0700132 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700133 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700134 RecentsConfiguration config = Recents.getConfiguration();
135 if (config.getLaunchState().launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700136 if (mTaskStackView != null) {
137 // If onRecentsHidden is not triggered, we need to the stack view again here
138 mTaskStackView.reset();
139 mTaskStackView.setStack(stack);
140 } else {
141 mTaskStackView = new TaskStackView(getContext(), stack);
142 mTaskStackView.setCallbacks(this);
143 addView(mTaskStackView);
144 }
145 } else {
146 if (mTaskStackView != null) {
147 removeView(mTaskStackView);
148 }
149 mTaskStackView = new TaskStackView(getContext(), stack);
150 mTaskStackView.setCallbacks(this);
151 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800152 }
Winson Chung02d49272014-08-29 13:57:29 -0700153
Winson Chungb0a28ea2014-10-28 15:21:35 -0700154 // Trigger a new layout
155 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700156 }
157
Skuhne8aa7d162015-03-20 13:40:53 -0700158 /** Gets the next task in the stack - or if the last - the top task */
159 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700160 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700161 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700162 if (mTaskStackView != null) {
163 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700164 ArrayList<Task> taskList = stack.getTasks();
165 // Iterate the stack views and try and find the focused task
166 for (int j = taskList.size() - 1; j >= 0; --j) {
167 Task task = taskList.get(j);
168 // Return the next task in the line.
169 if (found)
170 return task;
171 // Remember the first possible task as the top task.
172 if (returnTask == null)
173 returnTask = task;
174 if (task == taskToSearch)
175 found = true;
176 }
177 }
178 return returnTask;
179 }
180
Winson Chung1e8d71b2014-05-16 17:05:22 -0700181 /** Launches the focused task from the first stack if possible */
182 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700183 if (mTaskStackView != null) {
184 TaskStack stack = mTaskStackView.getStack();
Winson Chungd16c5652015-01-26 16:11:07 -0800185 // Iterate the stack views and try and find the focused task
Winson147ecaf2015-09-16 16:49:55 -0700186 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Winson Chungd16c5652015-01-26 16:11:07 -0800187 int taskViewCount = taskViews.size();
188 for (int j = 0; j < taskViewCount; j++) {
189 TaskView tv = taskViews.get(j);
190 Task task = tv.getTask();
191 if (tv.isFocusedTask()) {
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700192 onTaskViewClicked(mTaskStackView, tv, stack, task, false, false, null,
193 INVALID_STACK_ID);
Winson Chungd16c5652015-01-26 16:11:07 -0800194 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700195 }
196 }
197 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700198 return false;
199 }
200
Skuhne8aa7d162015-03-20 13:40:53 -0700201 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700202 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700203 if (mTaskStackView != null) {
204 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700205 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700206 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700207 int taskViewCount = taskViews.size();
208 for (int j = 0; j < taskViewCount; j++) {
209 TaskView tv = taskViews.get(j);
210 if (tv.getTask() == task) {
Winsonb1bbaed2015-09-23 15:45:11 -0700211 onTaskViewClicked(mTaskStackView, tv, stack, task, false, taskBounds != null,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700212 taskBounds, destinationStack);
Skuhne8aa7d162015-03-20 13:40:53 -0700213 return true;
214 }
215 }
216 }
217 return false;
218 }
219
Winson Chungdcfa7972014-07-22 12:27:13 -0700220 /** Launches the task that Recents was launched from, if possible */
221 public boolean launchPreviousTask() {
Winson147ecaf2015-09-16 16:49:55 -0700222 if (mTaskStackView != null) {
223 TaskStack stack = mTaskStackView.getStack();
Winson Chungd16c5652015-01-26 16:11:07 -0800224 ArrayList<Task> tasks = stack.getTasks();
Winson Chung47c4c692014-03-17 10:17:11 -0700225
Winson Chungd16c5652015-01-26 16:11:07 -0800226 // Find the launch task in the stack
Winsonbe7607a2015-10-01 17:24:51 -0700227 // TODO: replace this with an event from RecentsActivity
Winson Chungd16c5652015-01-26 16:11:07 -0800228 if (!tasks.isEmpty()) {
229 int taskCount = tasks.size();
230 for (int j = 0; j < taskCount; j++) {
231 if (tasks.get(j).isLaunchTarget) {
232 Task task = tasks.get(j);
Winson147ecaf2015-09-16 16:49:55 -0700233 TaskView tv = mTaskStackView.getChildViewForTask(task);
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700234 onTaskViewClicked(mTaskStackView, tv, stack, task, false, false, null,
235 INVALID_STACK_ID);
Winson Chungd16c5652015-01-26 16:11:07 -0800236 return true;
Winson Chung303e1ff2014-03-07 15:06:19 -0800237 }
238 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800239 }
240 }
241 return false;
242 }
243
Winson Chung24cf1522014-05-29 12:03:33 -0700244 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700245 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800246 // We have to increment/decrement the post animation trigger in case there are no children
247 // to ensure that it runs
248 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700249 if (mTaskStackView != null) {
250 mTaskStackView.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700251 }
Winson Chung740c3ac2014-11-12 16:14:38 -0800252 ctx.postAnimationTrigger.decrement();
Winson Chung24cf1522014-05-29 12:03:33 -0700253 }
254
Winson Chungd42a6cf2014-06-03 16:24:04 -0700255 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700256 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chunga91c2932014-11-07 15:02:38 -0800257 // We have to increment/decrement the post animation trigger in case there are no children
258 // to ensure that it runs
259 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700260 if (mTaskStackView != null) {
261 mTaskStackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700262 }
Winson Chunga91c2932014-11-07 15:02:38 -0800263 ctx.postAnimationTrigger.decrement();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700264
Winson Chung969f5862014-06-16 17:08:24 -0700265 // Notify of the exit animation
Winson412e1802015-10-20 16:57:57 -0700266 EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
Winson Chungd42a6cf2014-06-03 16:24:04 -0700267 }
268
Winson Chungf7bca432014-04-30 17:11:13 -0700269 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700270 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700271 // Remove the previous search bar if one exists
272 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
273 removeView(mSearchBar);
274 }
275 // Add the new search bar
276 if (searchBar != null) {
277 mSearchBar = searchBar;
278 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700279 }
Winson Chungecd9b302014-04-16 17:07:18 -0700280 }
281
Winson Chung772b6b12014-07-03 15:54:02 -0700282 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700283 public boolean hasValidSearchBar() {
284 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700285 }
286
287 /** Sets the visibility of the search bar */
288 public void setSearchBarVisibility(int visibility) {
289 if (mSearchBar != null) {
290 mSearchBar.setVisibility(visibility);
Winson Chung012ef362014-07-31 18:36:25 -0700291 // Always bring the search bar to the top
292 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700293 }
294 }
295
Winsonbe7607a2015-10-01 17:24:51 -0700296 @Override
297 protected void onAttachedToWindow() {
298 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
299 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
300 super.onAttachedToWindow();
301 }
302
303 @Override
304 protected void onDetachedFromWindow() {
305 super.onDetachedFromWindow();
306 EventBus.getDefault().unregister(this);
307 EventBus.getDefault().unregister(mTouchHandler);
308 }
309
Winson Chungf7bca432014-04-30 17:11:13 -0700310 /**
311 * This is called with the full size of the window since we are handling our own insets.
312 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800313 @Override
314 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700315 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800316 int width = MeasureSpec.getSize(widthMeasureSpec);
317 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700318
Winson Chungf7bca432014-04-30 17:11:13 -0700319 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700320 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700321 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700322 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700323 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700324 mSearchBar.measure(
325 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
326 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700327 }
328
Winson Chungf7bca432014-04-30 17:11:13 -0700329 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700330 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700331 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700332 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700333 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700334 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800335 }
336
Winsonbe7607a2015-10-01 17:24:51 -0700337 if (mDragView != null) {
Winsoneca4ab62015-11-04 10:50:28 -0800338 Rect taskRect = mTaskStackView.mLayoutAlgorithm.mTaskRect;
Winsonbe7607a2015-10-01 17:24:51 -0700339 mDragView.measure(
Winsoneca4ab62015-11-04 10:50:28 -0800340 MeasureSpec.makeMeasureSpec(taskRect.width(), MeasureSpec.AT_MOST),
341 MeasureSpec.makeMeasureSpec(taskRect.height(), MeasureSpec.AT_MOST));
Winsonbe7607a2015-10-01 17:24:51 -0700342 }
343
Winson Chung303e1ff2014-03-07 15:06:19 -0800344 setMeasuredDimension(width, height);
345 }
346
Winson Chungf7bca432014-04-30 17:11:13 -0700347 /**
348 * This is called with the full size of the window since we are handling our own insets.
349 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800350 @Override
351 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700352 RecentsConfiguration config = Recents.getConfiguration();
353
Winson Chungf7bca432014-04-30 17:11:13 -0700354 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700355 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
356 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700357 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700358 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700359 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700360 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
361 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700362 }
363
Winson147ecaf2015-09-16 16:49:55 -0700364 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
365 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800366 }
Winsonbe7607a2015-10-01 17:24:51 -0700367
368 if (mDragView != null) {
369 mDragView.layout(left, top, left + mDragView.getMeasuredWidth(),
370 top + mDragView.getMeasuredHeight());
371 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800372 }
373
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700374 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700375 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700376 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700377 requestLayout();
Winson Chungdcfa7972014-07-22 12:27:13 -0700378 return insets.consumeSystemWindowInsets();
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700379 }
380
Winsonbe7607a2015-10-01 17:24:51 -0700381 @Override
382 public boolean onInterceptTouchEvent(MotionEvent ev) {
383 return mTouchHandler.onInterceptTouchEvent(ev);
384 }
385
386 @Override
387 public boolean onTouchEvent(MotionEvent ev) {
388 return mTouchHandler.onTouchEvent(ev);
389 }
390
391 @Override
392 protected void dispatchDraw(Canvas canvas) {
393 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700394 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
395 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
396 if (d.getAlpha() > 0) {
397 d.draw(canvas);
398 }
Winsonbe7607a2015-10-01 17:24:51 -0700399 }
400 }
401
Winson4165d3362015-10-10 14:40:35 -0700402 @Override
403 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700404 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
405 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
406 if (d == who) {
407 return true;
408 }
409 }
410 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700411 }
412
Winson Chung303e1ff2014-03-07 15:06:19 -0800413 /** Unfilters any filtered stacks */
414 public boolean unfilterFilteredStacks() {
Winson Chungdcfa7972014-07-22 12:27:13 -0700415 if (mStacks != null) {
Winson Chung303e1ff2014-03-07 15:06:19 -0800416 // Check if there are any filtered stacks and unfilter them before we back out of Recents
417 boolean stacksUnfiltered = false;
Winson Chungdcfa7972014-07-22 12:27:13 -0700418 int numStacks = mStacks.size();
419 for (int i = 0; i < numStacks; i++) {
420 TaskStack stack = mStacks.get(i);
Winson Chung303e1ff2014-03-07 15:06:19 -0800421 if (stack.hasFilteredTasks()) {
422 stack.unfilterTasks();
423 stacksUnfiltered = true;
424 }
425 }
426 return stacksUnfiltered;
427 }
428 return false;
429 }
430
Jorim Jaggi900fb482015-06-02 15:07:33 -0700431 public void disableLayersForOneFrame() {
Winson147ecaf2015-09-16 16:49:55 -0700432 if (mTaskStackView != null) {
433 mTaskStackView.disableLayersForOneFrame();
Jorim Jaggi900fb482015-06-02 15:07:33 -0700434 }
435 }
436
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700437 private void postDrawHeaderThumbnailTransitionRunnable(final TaskStackView view,
438 final TaskView clickedView, final int offsetX, final int offsetY,
439 final float stackScroll,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700440 final ActivityOptions.OnAnimationStartedListener animStartedListener,
441 final int destinationStack) {
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700442 Runnable r = new Runnable() {
443 @Override
444 public void run() {
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700445 overrideDrawHeaderThumbnailTransition(view, clickedView, offsetX, offsetY,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700446 stackScroll, animStartedListener, destinationStack);
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700447
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700448 }
449 };
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700450
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700451 mCb.runAfterPause(r);
452 }
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700453
454 private void overrideDrawHeaderThumbnailTransition(TaskStackView stackView,
455 TaskView clickedTask, int offsetX, int offsetY, float stackScroll,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700456 final ActivityOptions.OnAnimationStartedListener animStartedListener,
457 int destinationStack) {
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700458 List<AppTransitionAnimationSpec> specs = getAppTransitionAnimationSpecs(stackView,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700459 clickedTask, offsetX, offsetY, stackScroll, destinationStack);
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700460 if (specs == null) {
461 return;
462 }
463
464 IRemoteCallback.Stub callback = new IRemoteCallback.Stub() {
465 @Override
466 public void sendResult(Bundle data) throws RemoteException {
467 post(new Runnable() {
468 @Override
469 public void run() {
470 if (animStartedListener != null) {
471 animStartedListener.onAnimationStarted();
472 }
473 }
474 });
475 }
476 };
477
478 AppTransitionAnimationSpec[] specsArray =
479 new AppTransitionAnimationSpec[specs.size()];
480 try {
481 WindowManagerGlobal.getWindowManagerService().overridePendingAppTransitionMultiThumb(
Filip Gruszczynski1a5203d2015-10-29 17:43:49 -0700482 specs.toArray(specsArray), callback, null, true /* scaleUp */);
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700483
484 } catch (RemoteException e) {
485 Log.w(TAG, "Error overriding app transition", e);
486 }
487 }
488
489 private List<AppTransitionAnimationSpec> getAppTransitionAnimationSpecs(TaskStackView stackView,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700490 TaskView clickedTask, int offsetX, int offsetY, float stackScroll,
491 int destinationStack) {
492 final int targetStackId = destinationStack != INVALID_STACK_ID ?
493 destinationStack : clickedTask.getTask().key.stackId;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700494 if (targetStackId != FREEFORM_WORKSPACE_STACK_ID
495 && targetStackId != FULLSCREEN_WORKSPACE_STACK_ID) {
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700496 return null;
497 }
498 // If this is a full screen stack, the transition will be towards the single, full screen
499 // task. We only need the transition spec for this task.
500 List<AppTransitionAnimationSpec> specs = new ArrayList<>();
Wale Ogunwale3797c222015-10-27 14:21:58 -0700501 if (targetStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700502 specs.add(createThumbnailHeaderAnimationSpec(
503 stackView, offsetX, offsetY, stackScroll, clickedTask,
504 clickedTask.getTask().key.id, ADD_HEADER_BITMAP));
505 return specs;
506 }
507 // This is a free form stack or full screen stack, so there will be multiple windows
508 // animating from thumbnails. We need transition animation specs for all of them.
509
510 // We will use top and bottom task views as a base for tasks, that aren't visible on the
511 // screen. This is necessary for cascade recents list, where some of the tasks might be
512 // hidden.
513 List<TaskView> taskViews = stackView.getTaskViews();
514 int childCount = taskViews.size();
515 TaskView topChild = taskViews.get(0);
516 TaskView bottomChild = taskViews.get(childCount - 1);
517 SparseArray<TaskView> taskViewsByTaskId = new SparseArray<>();
518 for (int i = 0; i < childCount; i++) {
519 TaskView taskView = taskViews.get(i);
520 taskViewsByTaskId.put(taskView.getTask().key.id, taskView);
521 }
522
523 TaskStack stack = stackView.getStack();
524 // We go through all tasks now and for each generate transition animation spec. If there is
525 // a view associated with a task, we use that view as a base for the animation. If there
526 // isn't, we use bottom or top view, depending on which one would be closer to the task
527 // view if it existed.
528 ArrayList<Task> tasks = stack.getTasks();
529 boolean passedClickedTask = false;
530 for (int i = 0, n = tasks.size(); i < n; i++) {
531 Task task = tasks.get(i);
532 TaskView taskView = taskViewsByTaskId.get(task.key.id);
533 if (taskView != null) {
534 specs.add(createThumbnailHeaderAnimationSpec(stackView, offsetX, offsetY,
535 stackScroll, taskView, taskView.getTask().key.id, ADD_HEADER_BITMAP));
536 if (taskView == clickedTask) {
537 passedClickedTask = true;
538 }
539 } else {
540 taskView = passedClickedTask ? bottomChild : topChild;
541 specs.add(createThumbnailHeaderAnimationSpec(stackView, offsetX, offsetY,
542 stackScroll, taskView, task.key.id, !ADD_HEADER_BITMAP));
543 }
544 }
545
546 return specs;
547 }
548
549 private AppTransitionAnimationSpec createThumbnailHeaderAnimationSpec(TaskStackView stackView,
550 int offsetX, int offsetY, float stackScroll, TaskView tv, int taskId,
551 boolean addHeaderBitmap) {
552 // Disable any focused state before we draw the header
553 // Upfront the processing of the thumbnail
554 if (tv.isFocusedTask()) {
Winson0d14d4d2015-10-26 17:05:04 -0700555 tv.setFocusedState(false, false /* animated */, false /* requestViewFocus */);
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700556 }
557 TaskViewTransform transform = new TaskViewTransform();
558 transform = stackView.getStackAlgorithm().getStackTransform(tv.mTask, stackScroll,
559 transform, null);
560
561 float scale = tv.getScaleX();
562 int fromHeaderWidth = (int) (tv.mHeaderView.getMeasuredWidth() * scale);
563 int fromHeaderHeight = (int) (tv.mHeaderView.getMeasuredHeight() * scale);
564
565 Bitmap b = null;
566 if (addHeaderBitmap) {
567 b = Bitmap.createBitmap(fromHeaderWidth, fromHeaderHeight,
568 Bitmap.Config.ARGB_8888);
569
570 if (Constants.DebugFlags.App.EnableTransitionThumbnailDebugMode) {
571 b.eraseColor(0xFFff0000);
572 } else {
573 Canvas c = new Canvas(b);
574 c.scale(tv.getScaleX(), tv.getScaleY());
575 tv.mHeaderView.draw(c);
576 c.setBitmap(null);
577
578 }
579 b = b.createAshmemBitmap();
580 }
581
582 int[] pts = new int[2];
583 tv.getLocationOnScreen(pts);
584
585 final int left = pts[0] + offsetX;
586 final int top = pts[1] + offsetY;
Winson3150e572015-10-23 15:07:24 -0700587 final Rect rect = new Rect(left, top, left + (int) transform.rect.width(),
588 top + (int) transform.rect.height());
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700589
590 return new AppTransitionAnimationSpec(taskId, b, rect);
591 }
592
Winsonc28098f2015-10-30 14:50:19 -0700593 /**
594 * Cancels any running window transitions for the launched task (the task animating into
595 * Recents).
596 */
597 private void cancelLaunchedTaskWindowTransition(final Task task) {
598 SystemServicesProxy ssp = Recents.getSystemServices();
599 RecentsConfiguration config = Recents.getConfiguration();
600 RecentsActivityLaunchState launchState = config.getLaunchState();
601 if (launchState.launchedToTaskId != -1 &&
602 launchState.launchedToTaskId != task.key.id) {
603 ssp.cancelWindowTransition(launchState.launchedToTaskId);
604 }
605 }
606
Winson Chung47c4c692014-03-17 10:17:11 -0700607 /**** TaskStackView.TaskStackCallbacks Implementation ****/
Winson Chung303e1ff2014-03-07 15:06:19 -0800608
609 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700610 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700611 final TaskStack stack, final Task task, final boolean lockToTask,
612 final boolean boundsValid, final Rect bounds, int destinationStack) {
Winson Chung47c4c692014-03-17 10:17:11 -0700613 // Notify any callbacks of the launching of a new task
614 if (mCb != null) {
Winson Chung7aceb9a2014-07-03 13:38:01 -0700615 mCb.onTaskViewClicked();
Winson Chung47c4c692014-03-17 10:17:11 -0700616 }
617
Winson Chunge0e45bc2014-06-17 17:56:17 -0700618 // Upfront the processing of the thumbnail
Winson Chungffa2ec62014-07-03 15:54:42 -0700619 TaskViewTransform transform = new TaskViewTransform();
Jorim Jaggicb557032014-09-16 23:09:24 +0200620 View sourceView;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700621 int offsetX = 0;
622 int offsetY = 0;
Winson Chung012ef362014-07-31 18:36:25 -0700623 float stackScroll = stackView.getScroller().getStackScroll();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700624 if (tv == null) {
625 // If there is no actual task view, then use the stack view as the source view
626 // and then offset to the expected transform rect, but bound this to just
627 // outside the display rect (to ensure we don't animate from too far away)
628 sourceView = stackView;
Winson3150e572015-10-23 15:07:24 -0700629 offsetX = (int) transform.rect.left;
Winson35f30502015-09-28 11:24:36 -0700630 offsetY = getMeasuredHeight();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700631 } else {
Jorim Jaggicb557032014-09-16 23:09:24 +0200632 sourceView = tv.mThumbnailView;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700633 }
634
635 // Compute the thumbnail to scale up from
Winsone7f138c2015-10-22 16:15:21 -0700636 final SystemServicesProxy ssp = Recents.getSystemServices();
Winsonc28098f2015-10-30 14:50:19 -0700637 boolean screenPinningRequested = false;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700638 ActivityOptions opts = null;
Chong Zhang0fa656b2015-08-31 15:17:21 -0700639 ActivityOptions.OnAnimationStartedListener animStartedListener = null;
Winson Chung2e7f3bd2014-09-05 13:17:22 +0200640 if (task.thumbnail != null && task.thumbnail.getWidth() > 0 &&
641 task.thumbnail.getHeight() > 0) {
Winsonc28098f2015-10-30 14:50:19 -0700642 animStartedListener = new ActivityOptions.OnAnimationStartedListener() {
643 @Override
644 public void onAnimationStarted() {
645 // If we are launching into another task, cancel the previous task's
646 // window transition
647 cancelLaunchedTaskWindowTransition(task);
648
649 if (lockToTask) {
650 // Request screen pinning after the animation runs
651 postDelayed(new Runnable() {
652 @Override
653 public void run() {
654 EventBus.getDefault().send(new ScreenPinningRequestEvent(
655 getContext(), ssp));
656 }
657 }, 350);
Winson Chung1f24c7e2014-07-11 17:06:48 -0700658 }
Winsonc28098f2015-10-30 14:50:19 -0700659 }
660 };
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700661 postDrawHeaderThumbnailTransitionRunnable(stackView, tv, offsetX, offsetY, stackScroll,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700662 animStartedListener, destinationStack);
Winsonec417e42015-09-29 12:42:29 -0700663 opts = ActivityOptions.makeThumbnailAspectScaleUpAnimation(sourceView,
664 Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8).createAshmemBitmap(),
Winson3150e572015-10-23 15:07:24 -0700665 offsetX, offsetY, (int) transform.rect.width(), (int) transform.rect.height(),
Winsonec417e42015-09-29 12:42:29 -0700666 sourceView.getHandler(), animStartedListener);
Winsonc28098f2015-10-30 14:50:19 -0700667 screenPinningRequested = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -0700668 } else {
669 opts = ActivityOptions.makeBasic();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700670 }
Chong Zhang0fa656b2015-08-31 15:17:21 -0700671 if (boundsValid) {
Winsonb1bbaed2015-09-23 15:45:11 -0700672 opts.setBounds(bounds.isEmpty() ? null : bounds);
Chong Zhang0fa656b2015-08-31 15:17:21 -0700673 }
Winson Chunge0e45bc2014-06-17 17:56:17 -0700674 final ActivityOptions launchOpts = opts;
Winsonc28098f2015-10-30 14:50:19 -0700675 final boolean finalScreenPinningRequested = screenPinningRequested;
Winson Chung303e1ff2014-03-07 15:06:19 -0800676 final Runnable launchRunnable = new Runnable() {
677 @Override
678 public void run() {
Winson Chung4be04452014-03-24 17:22:30 -0700679 if (task.isActive) {
680 // Bring an active task to the foreground
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700681 ssp.moveTaskToFront(task.key.id, launchOpts);
Winson Chung4be04452014-03-24 17:22:30 -0700682 } else {
Winsonc28098f2015-10-30 14:50:19 -0700683 if (ssp.startActivityFromRecents(getContext(), task.key.id, task.activityLabel,
684 launchOpts)) {
685 if (!finalScreenPinningRequested) {
686 // If we have not requested this already to be run after the window
687 // transition, then just run it now
Winson412e1802015-10-20 16:57:57 -0700688 EventBus.getDefault().send(new ScreenPinningRequestEvent(
689 getContext(), ssp));
Winson Chung4be04452014-03-24 17:22:30 -0700690 }
Winson Chung4e96eb72014-09-17 15:16:09 +0200691 } else {
692 // Dismiss the task and return the user to home if we fail to
693 // launch the task
Winson0d14d4d2015-10-26 17:05:04 -0700694 EventBus.getDefault().send(new DismissTaskViewEvent(task, tv));
Winson Chung4e96eb72014-09-17 15:16:09 +0200695 if (mCb != null) {
696 mCb.onTaskLaunchFailed();
697 }
Winson Chung5c9f4b92015-06-25 16:16:46 -0700698
699 // Keep track of failed launches
700 MetricsLogger.count(getContext(), "overview_task_launch_failed", 1);
Winson Chung4be04452014-03-24 17:22:30 -0700701 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800702 }
703 }
704 };
705
Winson Chung5c9f4b92015-06-25 16:16:46 -0700706 // Keep track of the index of the task launch
707 int taskIndexFromFront = 0;
708 int taskIndex = stack.indexOfTask(task);
709 if (taskIndex > -1) {
710 taskIndexFromFront = stack.getTaskCount() - taskIndex - 1;
711 }
712 MetricsLogger.histogram(getContext(), "overview_task_launch_index", taskIndexFromFront);
713
Winson Chung303e1ff2014-03-07 15:06:19 -0800714 // Launch the app right away if there is no task view, otherwise, animate the icon out first
Winson Chung814086d2014-05-07 15:01:14 -0700715 if (tv == null) {
Winson Chunga91c2932014-11-07 15:02:38 -0800716 launchRunnable.run();
Winson Chung303e1ff2014-03-07 15:06:19 -0800717 } else {
Winson Chunge1e20e12015-06-02 14:11:49 -0700718 if (task.group != null && !task.group.isFrontMostTask(task)) {
Winson Chunga4ccb862014-08-22 15:26:27 -0700719 // For affiliated tasks that are behind other tasks, we must animate the front cards
720 // out of view before starting the task transition
Winson Chungebfc6982014-08-26 12:25:34 -0700721 stackView.startLaunchTaskAnimation(tv, launchRunnable, lockToTask);
Winson Chunga4ccb862014-08-22 15:26:27 -0700722 } else {
723 // Otherwise, we can start the task transition immediately
Winson Chungebfc6982014-08-26 12:25:34 -0700724 stackView.startLaunchTaskAnimation(tv, null, lockToTask);
Winson Chunga91c2932014-11-07 15:02:38 -0800725 launchRunnable.run();
Winson Chunga4ccb862014-08-22 15:26:27 -0700726 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800727 }
728 }
Winson Chung9f9679d2014-04-11 16:49:09 -0700729
730 @Override
Winson Chung6ac8bd62015-01-07 16:38:35 -0800731 public void onAllTaskViewsDismissed(ArrayList<Task> removedTasks) {
Winson2536c7e2015-10-01 15:49:31 -0700732 /* TODO: Not currently enabled
Winson Chung6ac8bd62015-01-07 16:38:35 -0800733 if (removedTasks != null) {
734 int taskCount = removedTasks.size();
735 for (int i = 0; i < taskCount; i++) {
736 onTaskViewDismissed(removedTasks.get(i));
737 }
738 }
Winson2536c7e2015-10-01 15:49:31 -0700739 */
Winson Chung6ac8bd62015-01-07 16:38:35 -0800740
Winson Chung7aceb9a2014-07-03 13:38:01 -0700741 mCb.onAllTaskViewsDismissed();
Winson Chung5c9f4b92015-06-25 16:16:46 -0700742
743 // Keep track of all-deletions
744 MetricsLogger.count(getContext(), "overview_task_all_dismissed", 1);
Winson Chungd7b2cb12014-06-26 15:08:50 -0700745 }
746
747 @Override
Winson Chung8e548f72014-06-24 14:40:53 -0700748 public void onTaskStackFilterTriggered() {
749 // Hide the search bar
750 if (mSearchBar != null) {
Winson35f30502015-09-28 11:24:36 -0700751 int filterDuration = getResources().getInteger(
752 R.integer.recents_filter_animate_current_views_duration);
Winson Chung8e548f72014-06-24 14:40:53 -0700753 mSearchBar.animate()
754 .alpha(0f)
755 .setStartDelay(0)
Winson35f30502015-09-28 11:24:36 -0700756 .setInterpolator(mFastOutSlowInInterpolator)
757 .setDuration(filterDuration)
Winson Chung8e548f72014-06-24 14:40:53 -0700758 .withLayer()
759 .start();
760 }
761 }
762
763 @Override
764 public void onTaskStackUnfilterTriggered() {
765 // Show the search bar
766 if (mSearchBar != null) {
Winson35f30502015-09-28 11:24:36 -0700767 int filterDuration = getResources().getInteger(
768 R.integer.recents_filter_animate_new_views_duration);
Winson Chung8e548f72014-06-24 14:40:53 -0700769 mSearchBar.animate()
770 .alpha(1f)
771 .setStartDelay(0)
Winson35f30502015-09-28 11:24:36 -0700772 .setInterpolator(mFastOutSlowInInterpolator)
773 .setDuration(filterDuration)
Winson Chung8e548f72014-06-24 14:40:53 -0700774 .withLayer()
775 .start();
776 }
777 }
Winsonbe7607a2015-10-01 17:24:51 -0700778
779 /**** EventBus Events ****/
780
781 public final void onBusEvent(DragStartEvent event) {
782 // Add the drag view
783 mDragView = event.dragView;
784 addView(mDragView);
Winson4165d3362015-10-10 14:40:35 -0700785
Winson882072b2015-10-12 11:26:33 -0700786 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
787 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700788 }
789
Winsoneca4ab62015-11-04 10:50:28 -0800790 public final void onBusEvent(DragDropTargetChangedEvent event) {
791 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700792 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
793 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
794 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800795 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
796 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700797 }
Winsonbe7607a2015-10-01 17:24:51 -0700798 }
799
800 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800801 final Runnable cleanUpRunnable = new Runnable() {
Winsonbe7607a2015-10-01 17:24:51 -0700802 @Override
803 public void run() {
804 // Remove the drag view
805 removeView(mDragView);
806 mDragView = null;
Winsonbe7607a2015-10-01 17:24:51 -0700807 }
Winsoneca4ab62015-11-04 10:50:28 -0800808 };
809
810 // Animate the overlay alpha back to 0
811 updateVisibleDockRegions(null, -1);
812
813 if (event.dropTarget == null) {
814 // No drop targets for hit, so just animate the task back to its place
815 event.postAnimationTrigger.increment();
816 event.postAnimationTrigger.addLastDecrementRunnable(new Runnable() {
817 @Override
818 public void run() {
819 cleanUpRunnable.run();
820 }
821 });
822 // Animate the task back to where it was before then clean up afterwards
823 TaskViewTransform taskTransform = new TaskViewTransform();
824 TaskStackLayoutAlgorithm layoutAlgorithm = mTaskStackView.getStackAlgorithm();
825 layoutAlgorithm.getStackTransform(event.task,
826 mTaskStackView.getScroller().getStackScroll(), taskTransform, null);
Winsonbe7607a2015-10-01 17:24:51 -0700827 event.dragView.animate()
Winsoneca4ab62015-11-04 10:50:28 -0800828 .scaleX(taskTransform.scale)
829 .scaleY(taskTransform.scale)
830 .translationX((layoutAlgorithm.mTaskRect.left - event.dragView.getLeft())
831 + taskTransform.translationX)
832 .translationY((layoutAlgorithm.mTaskRect.top - event.dragView.getTop())
833 + taskTransform.translationY)
Winsonbe7607a2015-10-01 17:24:51 -0700834 .setDuration(175)
Winson4165d3362015-10-10 14:40:35 -0700835 .setInterpolator(mFastOutSlowInInterpolator)
Winsonbe7607a2015-10-01 17:24:51 -0700836 .withEndAction(event.postAnimationTrigger.decrementAsRunnable())
Winsonbe7607a2015-10-01 17:24:51 -0700837 .start();
Winson4165d3362015-10-10 14:40:35 -0700838
Winsoneca4ab62015-11-04 10:50:28 -0800839 } else if (event.dropTarget instanceof TaskStack.DockState) {
840 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
841
842 // For now, just remove the drag view and the original task
843 // TODO: Animate the task to the drop target rect before launching it above
844 cleanUpRunnable.run();
845
846 // Dock the task and launch it
847 SystemServicesProxy ssp = Recents.getSystemServices();
848 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
849 launchTask(event.task, null, INVALID_STACK_ID);
850
Winsonbe7607a2015-10-01 17:24:51 -0700851 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800852 // We dropped on another drop target, so just add the cleanup to the post animation
853 // trigger
854 event.postAnimationTrigger.addLastDecrementRunnable(new Runnable() {
855 @Override
856 public void run() {
857 cleanUpRunnable.run();
858 }
859 });
Winsonbe7607a2015-10-01 17:24:51 -0700860 }
861 }
Winson4165d3362015-10-10 14:40:35 -0700862
863 /**
864 * Updates the dock region to match the specified dock state.
865 */
Winson882072b2015-10-12 11:26:33 -0700866 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
867 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
868 if (newDockStates != null) {
869 for (TaskStack.DockState dockState : newDockStates) {
870 newDockStatesSet.add(dockState);
871 }
Winson4165d3362015-10-10 14:40:35 -0700872 }
Winson882072b2015-10-12 11:26:33 -0700873 for (TaskStack.DockState dockState : mVisibleDockStates) {
874 TaskStack.DockState.ViewState viewState = dockState.viewState;
875 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
876 // This is no longer visible, so hide it
877 viewState.startAlphaAnimation(0, 150);
878 } else {
879 // This state is now visible, update the bounds and show it
880 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
881 viewState.dockAreaOverlay.setBounds(
882 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
883 viewState.dockAreaOverlay.setCallback(this);
884 viewState.startAlphaAnimation(alpha, 150);
885 }
Winson4165d3362015-10-10 14:40:35 -0700886 }
Winson4165d3362015-10-10 14:40:35 -0700887 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800888}