blob: af302689272541bdf759fb962b466543c2b2e929 [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;
Jorim Jaggi2f7d2922015-10-29 13:08:29 +010020import android.app.ActivityOptions.OnAnimationStartedListener;
Winson Chung303e1ff2014-03-07 15:06:19 -080021import android.content.Context;
Winson Chung303e1ff2014-03-07 15:06:19 -080022import android.graphics.Bitmap;
23import android.graphics.Canvas;
24import android.graphics.Rect;
Winson4165d3362015-10-10 14:40:35 -070025import android.graphics.drawable.Drawable;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070026import android.os.Bundle;
27import android.os.IRemoteCallback;
28import android.os.RemoteException;
Winson882072b2015-10-12 11:26:33 -070029import android.util.ArraySet;
Winson Chung8e548f72014-06-24 14:40:53 -070030import android.util.AttributeSet;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070031import android.util.Log;
Filip Gruszczynski170192a2015-08-16 17:46:34 -070032import android.util.SparseArray;
33import android.view.AppTransitionAnimationSpec;
Jorim Jaggi2f7d2922015-10-29 13:08:29 +010034import android.view.IAppTransitionAnimationSpecsFuture;
Winson Chungecd9b302014-04-16 17:07:18 -070035import android.view.LayoutInflater;
Winsonbe7607a2015-10-01 17:24:51 -070036import android.view.MotionEvent;
Winson Chung303e1ff2014-03-07 15:06:19 -080037import android.view.View;
Winson Chung653f70c22014-05-19 14:49:42 -070038import android.view.WindowInsets;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070039import android.view.WindowManagerGlobal;
Winson35f30502015-09-28 11:24:36 -070040import android.view.animation.AnimationUtils;
41import android.view.animation.Interpolator;
Winson Chung303e1ff2014-03-07 15:06:19 -080042import android.widget.FrameLayout;
Jorim Jaggi2f7d2922015-10-29 13:08:29 +010043
44import com.android.internal.annotations.GuardedBy;
Winson Chung5c9f4b92015-06-25 16:16:46 -070045import com.android.internal.logging.MetricsLogger;
Winson Chungd16c5652015-01-26 16:11:07 -080046import com.android.systemui.R;
Winson Chung303e1ff2014-03-07 15:06:19 -080047import com.android.systemui.recents.Constants;
Winsone7f138c2015-10-22 16:15:21 -070048import com.android.systemui.recents.Recents;
Winsonbe7607a2015-10-01 17:24:51 -070049import com.android.systemui.recents.RecentsActivity;
Winsonc28098f2015-10-30 14:50:19 -070050import com.android.systemui.recents.RecentsActivityLaunchState;
Winson Chungaee097c2015-04-02 18:16:02 -070051import com.android.systemui.recents.RecentsAppWidgetHostView;
Winson Chung303e1ff2014-03-07 15:06:19 -080052import com.android.systemui.recents.RecentsConfiguration;
Winson2536c7e2015-10-01 15:49:31 -070053import com.android.systemui.recents.events.EventBus;
Winson13d30662015-11-06 15:30:29 -080054import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
Winson412e1802015-10-20 16:57:57 -070055import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
56import com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
Winson0d14d4d2015-10-26 17:05:04 -070057import com.android.systemui.recents.events.ui.DismissTaskViewEvent;
Winsoneca4ab62015-11-04 10:50:28 -080058import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
Winsonbe7607a2015-10-01 17:24:51 -070059import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
60import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
Winson Chung1f24c7e2014-07-11 17:06:48 -070061import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chung303e1ff2014-03-07 15:06:19 -080062import com.android.systemui.recents.model.Task;
63import com.android.systemui.recents.model.TaskStack;
64
65import java.util.ArrayList;
Winson Chung6ac8bd62015-01-07 16:38:35 -080066import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080067
Wale Ogunwale3797c222015-10-27 14:21:58 -070068import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Winson1b585612015-11-06 09:16:26 -080069import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070070import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070071
Winson Chung303e1ff2014-03-07 15:06:19 -080072/**
73 * This view is the the top level layout that contains TaskStacks (which are laid out according
74 * to their SpaceNode bounds.
75 */
Winsone6c90732015-09-24 16:06:29 -070076public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks {
Winson Chung47c4c692014-03-17 10:17:11 -070077
Jorim Jaggi6e18e002015-06-02 17:07:39 -070078 private static final String TAG = "RecentsView";
Winson0d14d4d2015-10-26 17:05:04 -070079 private static final boolean DEBUG = false;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070080
Filip Gruszczynski170192a2015-08-16 17:46:34 -070081 private static final boolean ADD_HEADER_BITMAP = true;
Jorim Jaggi29deb332015-11-09 14:33:20 +010082
83 /**
84 * Special value for {@link #mAppTransitionAnimationSpecs}: Indicate that we are currently
85 * waiting for the specs to be retrieved.
86 */
87 private static final List<AppTransitionAnimationSpec> SPECS_WAITING = new ArrayList<>();
88
Filip Gruszczynski14b4e572015-11-03 15:53:55 -080089 private int mStackViewVisibility = View.VISIBLE;
Filip Gruszczynski170192a2015-08-16 17:46:34 -070090
Winson Chung47c4c692014-03-17 10:17:11 -070091 /** The RecentsView callbacks */
92 public interface RecentsViewCallbacks {
Winson Chung4e96eb72014-09-17 15:16:09 +020093 public void onTaskLaunchFailed();
Winson Chung7aceb9a2014-07-03 13:38:01 -070094 public void onAllTaskViewsDismissed();
Winson Chung47c4c692014-03-17 10:17:11 -070095 }
96
Winson Chungd42a6cf2014-06-03 16:24:04 -070097 LayoutInflater mInflater;
98
Winson Chungdcfa7972014-07-22 12:27:13 -070099 ArrayList<TaskStack> mStacks;
Winson147ecaf2015-09-16 16:49:55 -0700100 TaskStackView mTaskStackView;
Winson Chungaee097c2015-04-02 18:16:02 -0700101 RecentsAppWidgetHostView mSearchBar;
Winson Chung47c4c692014-03-17 10:17:11 -0700102 RecentsViewCallbacks mCb;
Winsonbe7607a2015-10-01 17:24:51 -0700103
104 RecentsViewTouchHandler mTouchHandler;
105 DragView mDragView;
Winson882072b2015-10-12 11:26:33 -0700106 TaskStack.DockState[] mVisibleDockStates = {
107 TaskStack.DockState.LEFT,
108 TaskStack.DockState.TOP,
109 TaskStack.DockState.RIGHT,
110 TaskStack.DockState.BOTTOM,
111 };
Winsonbe7607a2015-10-01 17:24:51 -0700112
Winson35f30502015-09-28 11:24:36 -0700113 Interpolator mFastOutSlowInInterpolator;
114
115 Rect mSystemInsets = new Rect();
Winson Chung303e1ff2014-03-07 15:06:19 -0800116
Winson13d30662015-11-06 15:30:29 -0800117
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100118 @GuardedBy("this")
Jorim Jaggi29deb332015-11-09 14:33:20 +0100119 List<AppTransitionAnimationSpec> mAppTransitionAnimationSpecs = SPECS_WAITING;
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100120
Winson Chung303e1ff2014-03-07 15:06:19 -0800121 public RecentsView(Context context) {
122 super(context);
Winson Chung8e548f72014-06-24 14:40:53 -0700123 }
124
125 public RecentsView(Context context, AttributeSet attrs) {
126 this(context, attrs, 0);
127 }
128
129 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
130 this(context, attrs, defStyleAttr, 0);
131 }
132
133 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
134 super(context, attrs, defStyleAttr, defStyleRes);
Winsonbe7607a2015-10-01 17:24:51 -0700135 setWillNotDraw(false);
Winson Chungecd9b302014-04-16 17:07:18 -0700136 mInflater = LayoutInflater.from(context);
Winson35f30502015-09-28 11:24:36 -0700137 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
138 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonbe7607a2015-10-01 17:24:51 -0700139 mTouchHandler = new RecentsViewTouchHandler(this);
Winson Chungd16c5652015-01-26 16:11:07 -0800140 }
141
Winson Chung47c4c692014-03-17 10:17:11 -0700142 /** Sets the callbacks */
143 public void setCallbacks(RecentsViewCallbacks cb) {
144 mCb = cb;
145 }
146
Winson Chungdcfa7972014-07-22 12:27:13 -0700147 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700148 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700149 RecentsConfiguration config = Recents.getConfiguration();
150 if (config.getLaunchState().launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700151 if (mTaskStackView != null) {
152 // If onRecentsHidden is not triggered, we need to the stack view again here
153 mTaskStackView.reset();
154 mTaskStackView.setStack(stack);
155 } else {
156 mTaskStackView = new TaskStackView(getContext(), stack);
157 mTaskStackView.setCallbacks(this);
158 addView(mTaskStackView);
159 }
160 } else {
161 if (mTaskStackView != null) {
162 removeView(mTaskStackView);
163 }
164 mTaskStackView = new TaskStackView(getContext(), stack);
165 mTaskStackView.setCallbacks(this);
166 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800167 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800168 mTaskStackView.setVisibility(mStackViewVisibility);
Winson Chung02d49272014-08-29 13:57:29 -0700169
Winson Chungb0a28ea2014-10-28 15:21:35 -0700170 // Trigger a new layout
171 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700172 }
173
Skuhne8aa7d162015-03-20 13:40:53 -0700174 /** Gets the next task in the stack - or if the last - the top task */
175 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700176 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700177 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700178 if (mTaskStackView != null) {
179 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700180 ArrayList<Task> taskList = stack.getTasks();
181 // Iterate the stack views and try and find the focused task
182 for (int j = taskList.size() - 1; j >= 0; --j) {
183 Task task = taskList.get(j);
184 // Return the next task in the line.
185 if (found)
186 return task;
187 // Remember the first possible task as the top task.
188 if (returnTask == null)
189 returnTask = task;
190 if (task == taskToSearch)
191 found = true;
192 }
193 }
194 return returnTask;
195 }
196
Winson Chung1e8d71b2014-05-16 17:05:22 -0700197 /** Launches the focused task from the first stack if possible */
198 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700199 if (mTaskStackView != null) {
200 TaskStack stack = mTaskStackView.getStack();
Winson142af422015-11-09 10:39:57 -0800201 Task task = mTaskStackView.getFocusedTask();
202 if (task != null) {
203 TaskView taskView = mTaskStackView.getChildViewForTask(task);
204 onTaskViewClicked(mTaskStackView, taskView, stack, task, false, false, null,
205 INVALID_STACK_ID);
206 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700207 }
208 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700209 return false;
210 }
211
Skuhne8aa7d162015-03-20 13:40:53 -0700212 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700213 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700214 if (mTaskStackView != null) {
215 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700216 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700217 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700218 int taskViewCount = taskViews.size();
219 for (int j = 0; j < taskViewCount; j++) {
220 TaskView tv = taskViews.get(j);
221 if (tv.getTask() == task) {
Winsonb1bbaed2015-09-23 15:45:11 -0700222 onTaskViewClicked(mTaskStackView, tv, stack, task, false, taskBounds != null,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700223 taskBounds, destinationStack);
Skuhne8aa7d162015-03-20 13:40:53 -0700224 return true;
225 }
226 }
227 }
228 return false;
229 }
230
Winson Chung24cf1522014-05-29 12:03:33 -0700231 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700232 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800233 // We have to increment/decrement the post animation trigger in case there are no children
234 // to ensure that it runs
235 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700236 if (mTaskStackView != null) {
237 mTaskStackView.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700238 }
Winson Chung740c3ac2014-11-12 16:14:38 -0800239 ctx.postAnimationTrigger.decrement();
Winson Chung24cf1522014-05-29 12:03:33 -0700240 }
241
Winson Chungd42a6cf2014-06-03 16:24:04 -0700242 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700243 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chunga91c2932014-11-07 15:02:38 -0800244 // We have to increment/decrement the post animation trigger in case there are no children
245 // to ensure that it runs
246 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700247 if (mTaskStackView != null) {
248 mTaskStackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700249 }
Winson Chunga91c2932014-11-07 15:02:38 -0800250 ctx.postAnimationTrigger.decrement();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700251
Winson13d30662015-11-06 15:30:29 -0800252 // If we are going home, cancel the previous task's window transition
253 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
254
Winson Chung969f5862014-06-16 17:08:24 -0700255 // Notify of the exit animation
Winson412e1802015-10-20 16:57:57 -0700256 EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
Winson Chungd42a6cf2014-06-03 16:24:04 -0700257 }
258
Winson Chungf7bca432014-04-30 17:11:13 -0700259 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700260 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700261 // Remove the previous search bar if one exists
262 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
263 removeView(mSearchBar);
264 }
265 // Add the new search bar
266 if (searchBar != null) {
267 mSearchBar = searchBar;
268 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700269 }
Winson Chungecd9b302014-04-16 17:07:18 -0700270 }
271
Winson Chung772b6b12014-07-03 15:54:02 -0700272 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700273 public boolean hasValidSearchBar() {
274 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700275 }
276
277 /** Sets the visibility of the search bar */
278 public void setSearchBarVisibility(int visibility) {
279 if (mSearchBar != null) {
280 mSearchBar.setVisibility(visibility);
Winson Chung012ef362014-07-31 18:36:25 -0700281 // Always bring the search bar to the top
282 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700283 }
284 }
285
Winsonbe7607a2015-10-01 17:24:51 -0700286 @Override
287 protected void onAttachedToWindow() {
288 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
289 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
290 super.onAttachedToWindow();
291 }
292
293 @Override
294 protected void onDetachedFromWindow() {
295 super.onDetachedFromWindow();
296 EventBus.getDefault().unregister(this);
297 EventBus.getDefault().unregister(mTouchHandler);
298 }
299
Winson Chungf7bca432014-04-30 17:11:13 -0700300 /**
301 * This is called with the full size of the window since we are handling our own insets.
302 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800303 @Override
304 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700305 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800306 int width = MeasureSpec.getSize(widthMeasureSpec);
307 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700308
Winson Chungf7bca432014-04-30 17:11:13 -0700309 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700310 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700311 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700312 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700313 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700314 mSearchBar.measure(
315 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
316 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700317 }
318
Winson Chungf7bca432014-04-30 17:11:13 -0700319 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700320 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700321 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700322 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700323 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700324 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800325 }
326
Winsonbe7607a2015-10-01 17:24:51 -0700327 if (mDragView != null) {
Winsoneca4ab62015-11-04 10:50:28 -0800328 Rect taskRect = mTaskStackView.mLayoutAlgorithm.mTaskRect;
Winsonbe7607a2015-10-01 17:24:51 -0700329 mDragView.measure(
Winsoneca4ab62015-11-04 10:50:28 -0800330 MeasureSpec.makeMeasureSpec(taskRect.width(), MeasureSpec.AT_MOST),
331 MeasureSpec.makeMeasureSpec(taskRect.height(), MeasureSpec.AT_MOST));
Winsonbe7607a2015-10-01 17:24:51 -0700332 }
333
Winson Chung303e1ff2014-03-07 15:06:19 -0800334 setMeasuredDimension(width, height);
335 }
336
Winson Chungf7bca432014-04-30 17:11:13 -0700337 /**
338 * This is called with the full size of the window since we are handling our own insets.
339 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800340 @Override
341 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700342 RecentsConfiguration config = Recents.getConfiguration();
343
Winson Chungf7bca432014-04-30 17:11:13 -0700344 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700345 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
346 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700347 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700348 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700349 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700350 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
351 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700352 }
353
Winson147ecaf2015-09-16 16:49:55 -0700354 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
355 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800356 }
Winsonbe7607a2015-10-01 17:24:51 -0700357
358 if (mDragView != null) {
359 mDragView.layout(left, top, left + mDragView.getMeasuredWidth(),
360 top + mDragView.getMeasuredHeight());
361 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800362 }
363
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700364 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700365 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700366 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700367 requestLayout();
Winson Chungdcfa7972014-07-22 12:27:13 -0700368 return insets.consumeSystemWindowInsets();
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700369 }
370
Winsonbe7607a2015-10-01 17:24:51 -0700371 @Override
372 public boolean onInterceptTouchEvent(MotionEvent ev) {
373 return mTouchHandler.onInterceptTouchEvent(ev);
374 }
375
376 @Override
377 public boolean onTouchEvent(MotionEvent ev) {
378 return mTouchHandler.onTouchEvent(ev);
379 }
380
381 @Override
382 protected void dispatchDraw(Canvas canvas) {
383 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700384 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
385 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
386 if (d.getAlpha() > 0) {
387 d.draw(canvas);
388 }
Winsonbe7607a2015-10-01 17:24:51 -0700389 }
390 }
391
Winson4165d3362015-10-10 14:40:35 -0700392 @Override
393 protected boolean verifyDrawable(Drawable who) {
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 == who) {
397 return true;
398 }
399 }
400 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700401 }
402
Winson Chung303e1ff2014-03-07 15:06:19 -0800403 /** Unfilters any filtered stacks */
404 public boolean unfilterFilteredStacks() {
Winson Chungdcfa7972014-07-22 12:27:13 -0700405 if (mStacks != null) {
Winson Chung303e1ff2014-03-07 15:06:19 -0800406 // Check if there are any filtered stacks and unfilter them before we back out of Recents
407 boolean stacksUnfiltered = false;
Winson Chungdcfa7972014-07-22 12:27:13 -0700408 int numStacks = mStacks.size();
409 for (int i = 0; i < numStacks; i++) {
410 TaskStack stack = mStacks.get(i);
Winson Chung303e1ff2014-03-07 15:06:19 -0800411 if (stack.hasFilteredTasks()) {
412 stack.unfilterTasks();
413 stacksUnfiltered = true;
414 }
415 }
416 return stacksUnfiltered;
417 }
418 return false;
419 }
420
Jorim Jaggi900fb482015-06-02 15:07:33 -0700421 public void disableLayersForOneFrame() {
Winson147ecaf2015-09-16 16:49:55 -0700422 if (mTaskStackView != null) {
423 mTaskStackView.disableLayersForOneFrame();
Jorim Jaggi900fb482015-06-02 15:07:33 -0700424 }
425 }
426
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100427 private IAppTransitionAnimationSpecsFuture getAppTransitionFuture(final TaskStackView stackView,
428 final TaskView clickedTask, final int offsetX, final int offsetY,
429 final float stackScroll, final int destinationStack) {
430 return new IAppTransitionAnimationSpecsFuture.Stub() {
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700431 @Override
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100432 public AppTransitionAnimationSpec[] get() throws RemoteException {
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700433 post(new Runnable() {
434 @Override
435 public void run() {
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100436 synchronized (RecentsView.this) {
437 mAppTransitionAnimationSpecs = getAppTransitionAnimationSpecs(stackView,
438 clickedTask, offsetX, offsetY, stackScroll, destinationStack);
439 RecentsView.this.notifyAll();
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700440 }
441 }
442 });
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100443 synchronized (RecentsView.this) {
Jorim Jaggi29deb332015-11-09 14:33:20 +0100444 while (mAppTransitionAnimationSpecs == SPECS_WAITING) {
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100445 try {
446 RecentsView.this.wait();
447 } catch (InterruptedException e) {}
448 }
449 if (mAppTransitionAnimationSpecs == null) {
450 return null;
451 }
452 AppTransitionAnimationSpec[] specs
453 = new AppTransitionAnimationSpec[mAppTransitionAnimationSpecs.size()];
Jorim Jaggi29deb332015-11-09 14:33:20 +0100454 mAppTransitionAnimationSpecs.toArray(specs);
455 mAppTransitionAnimationSpecs = SPECS_WAITING;
456 return specs;
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100457 }
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700458 }
459 };
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700460 }
461
462 private List<AppTransitionAnimationSpec> getAppTransitionAnimationSpecs(TaskStackView stackView,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700463 TaskView clickedTask, int offsetX, int offsetY, float stackScroll,
464 int destinationStack) {
465 final int targetStackId = destinationStack != INVALID_STACK_ID ?
466 destinationStack : clickedTask.getTask().key.stackId;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700467 if (targetStackId != FREEFORM_WORKSPACE_STACK_ID
468 && targetStackId != FULLSCREEN_WORKSPACE_STACK_ID) {
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700469 return null;
470 }
471 // If this is a full screen stack, the transition will be towards the single, full screen
472 // task. We only need the transition spec for this task.
473 List<AppTransitionAnimationSpec> specs = new ArrayList<>();
Wale Ogunwale3797c222015-10-27 14:21:58 -0700474 if (targetStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700475 specs.add(createThumbnailHeaderAnimationSpec(
476 stackView, offsetX, offsetY, stackScroll, clickedTask,
477 clickedTask.getTask().key.id, ADD_HEADER_BITMAP));
478 return specs;
479 }
480 // This is a free form stack or full screen stack, so there will be multiple windows
481 // animating from thumbnails. We need transition animation specs for all of them.
482
483 // We will use top and bottom task views as a base for tasks, that aren't visible on the
484 // screen. This is necessary for cascade recents list, where some of the tasks might be
485 // hidden.
486 List<TaskView> taskViews = stackView.getTaskViews();
487 int childCount = taskViews.size();
488 TaskView topChild = taskViews.get(0);
489 TaskView bottomChild = taskViews.get(childCount - 1);
490 SparseArray<TaskView> taskViewsByTaskId = new SparseArray<>();
491 for (int i = 0; i < childCount; i++) {
492 TaskView taskView = taskViews.get(i);
493 taskViewsByTaskId.put(taskView.getTask().key.id, taskView);
494 }
495
496 TaskStack stack = stackView.getStack();
497 // We go through all tasks now and for each generate transition animation spec. If there is
498 // a view associated with a task, we use that view as a base for the animation. If there
499 // isn't, we use bottom or top view, depending on which one would be closer to the task
500 // view if it existed.
501 ArrayList<Task> tasks = stack.getTasks();
502 boolean passedClickedTask = false;
503 for (int i = 0, n = tasks.size(); i < n; i++) {
504 Task task = tasks.get(i);
505 TaskView taskView = taskViewsByTaskId.get(task.key.id);
506 if (taskView != null) {
507 specs.add(createThumbnailHeaderAnimationSpec(stackView, offsetX, offsetY,
508 stackScroll, taskView, taskView.getTask().key.id, ADD_HEADER_BITMAP));
509 if (taskView == clickedTask) {
510 passedClickedTask = true;
511 }
512 } else {
513 taskView = passedClickedTask ? bottomChild : topChild;
514 specs.add(createThumbnailHeaderAnimationSpec(stackView, offsetX, offsetY,
515 stackScroll, taskView, task.key.id, !ADD_HEADER_BITMAP));
516 }
517 }
518
519 return specs;
520 }
521
522 private AppTransitionAnimationSpec createThumbnailHeaderAnimationSpec(TaskStackView stackView,
523 int offsetX, int offsetY, float stackScroll, TaskView tv, int taskId,
524 boolean addHeaderBitmap) {
525 // Disable any focused state before we draw the header
526 // Upfront the processing of the thumbnail
527 if (tv.isFocusedTask()) {
Winson0d14d4d2015-10-26 17:05:04 -0700528 tv.setFocusedState(false, false /* animated */, false /* requestViewFocus */);
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700529 }
530 TaskViewTransform transform = new TaskViewTransform();
531 transform = stackView.getStackAlgorithm().getStackTransform(tv.mTask, stackScroll,
532 transform, null);
533
534 float scale = tv.getScaleX();
535 int fromHeaderWidth = (int) (tv.mHeaderView.getMeasuredWidth() * scale);
536 int fromHeaderHeight = (int) (tv.mHeaderView.getMeasuredHeight() * scale);
537
538 Bitmap b = null;
539 if (addHeaderBitmap) {
540 b = Bitmap.createBitmap(fromHeaderWidth, fromHeaderHeight,
541 Bitmap.Config.ARGB_8888);
542
543 if (Constants.DebugFlags.App.EnableTransitionThumbnailDebugMode) {
544 b.eraseColor(0xFFff0000);
545 } else {
546 Canvas c = new Canvas(b);
547 c.scale(tv.getScaleX(), tv.getScaleY());
548 tv.mHeaderView.draw(c);
549 c.setBitmap(null);
550
551 }
552 b = b.createAshmemBitmap();
553 }
554
555 int[] pts = new int[2];
556 tv.getLocationOnScreen(pts);
557
558 final int left = pts[0] + offsetX;
559 final int top = pts[1] + offsetY;
Winson3150e572015-10-23 15:07:24 -0700560 final Rect rect = new Rect(left, top, left + (int) transform.rect.width(),
561 top + (int) transform.rect.height());
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700562
563 return new AppTransitionAnimationSpec(taskId, b, rect);
564 }
565
Winson Chung47c4c692014-03-17 10:17:11 -0700566 /**** TaskStackView.TaskStackCallbacks Implementation ****/
Winson Chung303e1ff2014-03-07 15:06:19 -0800567
568 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700569 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700570 final TaskStack stack, final Task task, final boolean lockToTask,
571 final boolean boundsValid, final Rect bounds, int destinationStack) {
Winson Chung47c4c692014-03-17 10:17:11 -0700572
Winson Chunge0e45bc2014-06-17 17:56:17 -0700573 // Upfront the processing of the thumbnail
Winson Chungffa2ec62014-07-03 15:54:42 -0700574 TaskViewTransform transform = new TaskViewTransform();
Jorim Jaggicb557032014-09-16 23:09:24 +0200575 View sourceView;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700576 int offsetX = 0;
577 int offsetY = 0;
Winson Chung012ef362014-07-31 18:36:25 -0700578 float stackScroll = stackView.getScroller().getStackScroll();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700579 if (tv == null) {
580 // If there is no actual task view, then use the stack view as the source view
581 // and then offset to the expected transform rect, but bound this to just
582 // outside the display rect (to ensure we don't animate from too far away)
583 sourceView = stackView;
Winson3150e572015-10-23 15:07:24 -0700584 offsetX = (int) transform.rect.left;
Winson35f30502015-09-28 11:24:36 -0700585 offsetY = getMeasuredHeight();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700586 } else {
Jorim Jaggicb557032014-09-16 23:09:24 +0200587 sourceView = tv.mThumbnailView;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700588 }
589
590 // Compute the thumbnail to scale up from
Winsone7f138c2015-10-22 16:15:21 -0700591 final SystemServicesProxy ssp = Recents.getSystemServices();
Winsonc28098f2015-10-30 14:50:19 -0700592 boolean screenPinningRequested = false;
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100593 ActivityOptions opts = ActivityOptions.makeBasic();
Chong Zhang0fa656b2015-08-31 15:17:21 -0700594 ActivityOptions.OnAnimationStartedListener animStartedListener = null;
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100595 final IAppTransitionAnimationSpecsFuture transitionFuture;
Winson Chung2e7f3bd2014-09-05 13:17:22 +0200596 if (task.thumbnail != null && task.thumbnail.getWidth() > 0 &&
597 task.thumbnail.getHeight() > 0) {
Winsonc28098f2015-10-30 14:50:19 -0700598 animStartedListener = new ActivityOptions.OnAnimationStartedListener() {
599 @Override
600 public void onAnimationStarted() {
601 // If we are launching into another task, cancel the previous task's
602 // window transition
Winson13d30662015-11-06 15:30:29 -0800603 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(task));
Winsonc28098f2015-10-30 14:50:19 -0700604
605 if (lockToTask) {
606 // Request screen pinning after the animation runs
607 postDelayed(new Runnable() {
608 @Override
609 public void run() {
610 EventBus.getDefault().send(new ScreenPinningRequestEvent(
611 getContext(), ssp));
612 }
613 }, 350);
Winson Chung1f24c7e2014-07-11 17:06:48 -0700614 }
Winsonc28098f2015-10-30 14:50:19 -0700615 }
616 };
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100617 transitionFuture = getAppTransitionFuture(stackView, tv, offsetX, offsetY, stackScroll,
618 destinationStack);
Winsonc28098f2015-10-30 14:50:19 -0700619 screenPinningRequested = true;
Chong Zhang0fa656b2015-08-31 15:17:21 -0700620 } else {
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100621 transitionFuture = null;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700622 }
Chong Zhang0fa656b2015-08-31 15:17:21 -0700623 if (boundsValid) {
Winsonb1bbaed2015-09-23 15:45:11 -0700624 opts.setBounds(bounds.isEmpty() ? null : bounds);
Chong Zhang0fa656b2015-08-31 15:17:21 -0700625 }
Winson Chunge0e45bc2014-06-17 17:56:17 -0700626 final ActivityOptions launchOpts = opts;
Winsonc28098f2015-10-30 14:50:19 -0700627 final boolean finalScreenPinningRequested = screenPinningRequested;
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100628 final OnAnimationStartedListener finalAnimStartedListener = animStartedListener;
Winson Chung303e1ff2014-03-07 15:06:19 -0800629 final Runnable launchRunnable = new Runnable() {
630 @Override
631 public void run() {
Winson Chung4be04452014-03-24 17:22:30 -0700632 if (task.isActive) {
633 // Bring an active task to the foreground
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700634 ssp.moveTaskToFront(task.key.id, launchOpts);
Winson Chung4be04452014-03-24 17:22:30 -0700635 } else {
Winsonc28098f2015-10-30 14:50:19 -0700636 if (ssp.startActivityFromRecents(getContext(), task.key.id, task.activityLabel,
637 launchOpts)) {
638 if (!finalScreenPinningRequested) {
639 // If we have not requested this already to be run after the window
640 // transition, then just run it now
Winson412e1802015-10-20 16:57:57 -0700641 EventBus.getDefault().send(new ScreenPinningRequestEvent(
642 getContext(), ssp));
Winson Chung4be04452014-03-24 17:22:30 -0700643 }
Winson Chung4e96eb72014-09-17 15:16:09 +0200644 } else {
645 // Dismiss the task and return the user to home if we fail to
646 // launch the task
Winson0d14d4d2015-10-26 17:05:04 -0700647 EventBus.getDefault().send(new DismissTaskViewEvent(task, tv));
Winson Chung4e96eb72014-09-17 15:16:09 +0200648 if (mCb != null) {
649 mCb.onTaskLaunchFailed();
650 }
Winson Chung5c9f4b92015-06-25 16:16:46 -0700651
652 // Keep track of failed launches
653 MetricsLogger.count(getContext(), "overview_task_launch_failed", 1);
Winson Chung4be04452014-03-24 17:22:30 -0700654 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800655 }
Jorim Jaggi2f7d2922015-10-29 13:08:29 +0100656 if (transitionFuture != null) {
657 IRemoteCallback.Stub callback = new IRemoteCallback.Stub() {
658 @Override
659 public void sendResult(Bundle data) throws RemoteException {
660 post(new Runnable() {
661 @Override
662 public void run() {
663 if (finalAnimStartedListener != null) {
664 finalAnimStartedListener.onAnimationStarted();
665 }
666 }
667 });
668 }
669 };
670 try {
671 WindowManagerGlobal.getWindowManagerService()
672 .overridePendingAppTransitionMultiThumbFuture(transitionFuture,
673 callback, true /* scaleUp */);
674 } catch (RemoteException e) {
675 Log.w(TAG, "Failed to override transition: " + e);
676 }
677 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800678 }
679 };
680
Winson Chung5c9f4b92015-06-25 16:16:46 -0700681 // Keep track of the index of the task launch
682 int taskIndexFromFront = 0;
683 int taskIndex = stack.indexOfTask(task);
684 if (taskIndex > -1) {
685 taskIndexFromFront = stack.getTaskCount() - taskIndex - 1;
686 }
687 MetricsLogger.histogram(getContext(), "overview_task_launch_index", taskIndexFromFront);
688
Winson Chung303e1ff2014-03-07 15:06:19 -0800689 // Launch the app right away if there is no task view, otherwise, animate the icon out first
Winson Chung814086d2014-05-07 15:01:14 -0700690 if (tv == null) {
Winson Chunga91c2932014-11-07 15:02:38 -0800691 launchRunnable.run();
Winson Chung303e1ff2014-03-07 15:06:19 -0800692 } else {
Winson Chunge1e20e12015-06-02 14:11:49 -0700693 if (task.group != null && !task.group.isFrontMostTask(task)) {
Winson Chunga4ccb862014-08-22 15:26:27 -0700694 // For affiliated tasks that are behind other tasks, we must animate the front cards
695 // out of view before starting the task transition
Winson Chungebfc6982014-08-26 12:25:34 -0700696 stackView.startLaunchTaskAnimation(tv, launchRunnable, lockToTask);
Winson Chunga4ccb862014-08-22 15:26:27 -0700697 } else {
698 // Otherwise, we can start the task transition immediately
Winson Chungebfc6982014-08-26 12:25:34 -0700699 stackView.startLaunchTaskAnimation(tv, null, lockToTask);
Winson Chunga91c2932014-11-07 15:02:38 -0800700 launchRunnable.run();
Winson Chunga4ccb862014-08-22 15:26:27 -0700701 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800702 }
703 }
Winson Chung9f9679d2014-04-11 16:49:09 -0700704
705 @Override
Winson Chung6ac8bd62015-01-07 16:38:35 -0800706 public void onAllTaskViewsDismissed(ArrayList<Task> removedTasks) {
Winson2536c7e2015-10-01 15:49:31 -0700707 /* TODO: Not currently enabled
Winson Chung6ac8bd62015-01-07 16:38:35 -0800708 if (removedTasks != null) {
709 int taskCount = removedTasks.size();
710 for (int i = 0; i < taskCount; i++) {
711 onTaskViewDismissed(removedTasks.get(i));
712 }
713 }
Winson2536c7e2015-10-01 15:49:31 -0700714 */
Winson Chung6ac8bd62015-01-07 16:38:35 -0800715
Winson Chung7aceb9a2014-07-03 13:38:01 -0700716 mCb.onAllTaskViewsDismissed();
Winson Chung5c9f4b92015-06-25 16:16:46 -0700717
718 // Keep track of all-deletions
719 MetricsLogger.count(getContext(), "overview_task_all_dismissed", 1);
Winson Chungd7b2cb12014-06-26 15:08:50 -0700720 }
721
722 @Override
Winson Chung8e548f72014-06-24 14:40:53 -0700723 public void onTaskStackFilterTriggered() {
724 // Hide the search bar
725 if (mSearchBar != null) {
Winson35f30502015-09-28 11:24:36 -0700726 int filterDuration = getResources().getInteger(
727 R.integer.recents_filter_animate_current_views_duration);
Winson Chung8e548f72014-06-24 14:40:53 -0700728 mSearchBar.animate()
729 .alpha(0f)
730 .setStartDelay(0)
Winson35f30502015-09-28 11:24:36 -0700731 .setInterpolator(mFastOutSlowInInterpolator)
732 .setDuration(filterDuration)
Winson Chung8e548f72014-06-24 14:40:53 -0700733 .withLayer()
734 .start();
735 }
736 }
737
738 @Override
739 public void onTaskStackUnfilterTriggered() {
740 // Show the search bar
741 if (mSearchBar != null) {
Winson35f30502015-09-28 11:24:36 -0700742 int filterDuration = getResources().getInteger(
743 R.integer.recents_filter_animate_new_views_duration);
Winson Chung8e548f72014-06-24 14:40:53 -0700744 mSearchBar.animate()
745 .alpha(1f)
746 .setStartDelay(0)
Winson35f30502015-09-28 11:24:36 -0700747 .setInterpolator(mFastOutSlowInInterpolator)
748 .setDuration(filterDuration)
Winson Chung8e548f72014-06-24 14:40:53 -0700749 .withLayer()
750 .start();
751 }
752 }
Winsonbe7607a2015-10-01 17:24:51 -0700753
754 /**** EventBus Events ****/
755
756 public final void onBusEvent(DragStartEvent event) {
757 // Add the drag view
758 mDragView = event.dragView;
759 addView(mDragView);
Winson4165d3362015-10-10 14:40:35 -0700760
Winson882072b2015-10-12 11:26:33 -0700761 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
762 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700763 }
764
Winsoneca4ab62015-11-04 10:50:28 -0800765 public final void onBusEvent(DragDropTargetChangedEvent event) {
766 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700767 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
768 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
769 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800770 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
771 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700772 }
Winsonbe7607a2015-10-01 17:24:51 -0700773 }
774
775 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800776 final Runnable cleanUpRunnable = new Runnable() {
Winsonbe7607a2015-10-01 17:24:51 -0700777 @Override
778 public void run() {
779 // Remove the drag view
780 removeView(mDragView);
781 mDragView = null;
Winsonbe7607a2015-10-01 17:24:51 -0700782 }
Winsoneca4ab62015-11-04 10:50:28 -0800783 };
784
785 // Animate the overlay alpha back to 0
786 updateVisibleDockRegions(null, -1);
787
788 if (event.dropTarget == null) {
789 // No drop targets for hit, so just animate the task back to its place
790 event.postAnimationTrigger.increment();
791 event.postAnimationTrigger.addLastDecrementRunnable(new Runnable() {
792 @Override
793 public void run() {
794 cleanUpRunnable.run();
795 }
796 });
797 // Animate the task back to where it was before then clean up afterwards
798 TaskViewTransform taskTransform = new TaskViewTransform();
799 TaskStackLayoutAlgorithm layoutAlgorithm = mTaskStackView.getStackAlgorithm();
800 layoutAlgorithm.getStackTransform(event.task,
801 mTaskStackView.getScroller().getStackScroll(), taskTransform, null);
Winsonbe7607a2015-10-01 17:24:51 -0700802 event.dragView.animate()
Winsoneca4ab62015-11-04 10:50:28 -0800803 .scaleX(taskTransform.scale)
804 .scaleY(taskTransform.scale)
805 .translationX((layoutAlgorithm.mTaskRect.left - event.dragView.getLeft())
806 + taskTransform.translationX)
807 .translationY((layoutAlgorithm.mTaskRect.top - event.dragView.getTop())
808 + taskTransform.translationY)
Winsonbe7607a2015-10-01 17:24:51 -0700809 .setDuration(175)
Winson4165d3362015-10-10 14:40:35 -0700810 .setInterpolator(mFastOutSlowInInterpolator)
Winsonbe7607a2015-10-01 17:24:51 -0700811 .withEndAction(event.postAnimationTrigger.decrementAsRunnable())
Winsonbe7607a2015-10-01 17:24:51 -0700812 .start();
Winson4165d3362015-10-10 14:40:35 -0700813
Winsoneca4ab62015-11-04 10:50:28 -0800814 } else if (event.dropTarget instanceof TaskStack.DockState) {
815 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
816
817 // For now, just remove the drag view and the original task
818 // TODO: Animate the task to the drop target rect before launching it above
819 cleanUpRunnable.run();
820
821 // Dock the task and launch it
822 SystemServicesProxy ssp = Recents.getSystemServices();
823 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
824 launchTask(event.task, null, INVALID_STACK_ID);
825
Winsonbe7607a2015-10-01 17:24:51 -0700826 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800827 // We dropped on another drop target, so just add the cleanup to the post animation
828 // trigger
829 event.postAnimationTrigger.addLastDecrementRunnable(new Runnable() {
830 @Override
831 public void run() {
832 cleanUpRunnable.run();
833 }
834 });
Winsonbe7607a2015-10-01 17:24:51 -0700835 }
836 }
Winson4165d3362015-10-10 14:40:35 -0700837
838 /**
839 * Updates the dock region to match the specified dock state.
840 */
Winson882072b2015-10-12 11:26:33 -0700841 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
842 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
843 if (newDockStates != null) {
844 for (TaskStack.DockState dockState : newDockStates) {
845 newDockStatesSet.add(dockState);
846 }
Winson4165d3362015-10-10 14:40:35 -0700847 }
Winson882072b2015-10-12 11:26:33 -0700848 for (TaskStack.DockState dockState : mVisibleDockStates) {
849 TaskStack.DockState.ViewState viewState = dockState.viewState;
850 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
851 // This is no longer visible, so hide it
852 viewState.startAlphaAnimation(0, 150);
853 } else {
854 // This state is now visible, update the bounds and show it
855 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
856 viewState.dockAreaOverlay.setBounds(
857 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
858 viewState.dockAreaOverlay.setCallback(this);
859 viewState.startAlphaAnimation(alpha, 150);
860 }
Winson4165d3362015-10-10 14:40:35 -0700861 }
Winson4165d3362015-10-10 14:40:35 -0700862 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800863
864 public void setStackViewVisibility(int stackViewVisibility) {
865 mStackViewVisibility = stackViewVisibility;
866 if (mTaskStackView != null) {
867 mTaskStackView.setVisibility(stackViewVisibility);
868 invalidate();
869 }
870 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800871}