blob: c2400dfbf7cb897e7d8da75974a78507781488ec [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;
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;
Winson Chungecd9b302014-04-16 17:07:18 -070034import android.view.LayoutInflater;
Winsonbe7607a2015-10-01 17:24:51 -070035import android.view.MotionEvent;
Winson Chung303e1ff2014-03-07 15:06:19 -080036import android.view.View;
Winson Chung653f70c22014-05-19 14:49:42 -070037import android.view.WindowInsets;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070038import android.view.WindowManagerGlobal;
Winson35f30502015-09-28 11:24:36 -070039import android.view.animation.AnimationUtils;
40import android.view.animation.Interpolator;
Winson Chung303e1ff2014-03-07 15:06:19 -080041import android.widget.FrameLayout;
Winson Chung5c9f4b92015-06-25 16:16:46 -070042import com.android.internal.logging.MetricsLogger;
Winson Chungd16c5652015-01-26 16:11:07 -080043import com.android.systemui.R;
Winson Chung303e1ff2014-03-07 15:06:19 -080044import com.android.systemui.recents.Constants;
Winsonbe7607a2015-10-01 17:24:51 -070045import com.android.systemui.recents.RecentsActivity;
Winson Chungaee097c2015-04-02 18:16:02 -070046import com.android.systemui.recents.RecentsAppWidgetHostView;
Winson Chung303e1ff2014-03-07 15:06:19 -080047import com.android.systemui.recents.RecentsConfiguration;
Winson2536c7e2015-10-01 15:49:31 -070048import com.android.systemui.recents.events.EventBus;
Winson412e1802015-10-20 16:57:57 -070049import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
50import com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
Winson2536c7e2015-10-01 15:49:31 -070051import com.android.systemui.recents.events.ui.DismissTaskEvent;
Winsonbe7607a2015-10-01 17:24:51 -070052import com.android.systemui.recents.events.ui.dragndrop.DragDockStateChangedEvent;
53import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
54import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
Winson Chung1f24c7e2014-07-11 17:06:48 -070055import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chungf1fbd772014-06-24 18:06:58 -070056import com.android.systemui.recents.model.RecentsTaskLoader;
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
Winson412e1802015-10-20 16:57:57 -070063import static android.app.ActivityManager.INVALID_STACK_ID;
64
Winson Chung303e1ff2014-03-07 15:06:19 -080065/**
66 * This view is the the top level layout that contains TaskStacks (which are laid out according
67 * to their SpaceNode bounds.
68 */
Winsone6c90732015-09-24 16:06:29 -070069public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks {
Winson Chung47c4c692014-03-17 10:17:11 -070070
Jorim Jaggi6e18e002015-06-02 17:07:39 -070071 private static final String TAG = "RecentsView";
72
Filip Gruszczynski170192a2015-08-16 17:46:34 -070073 private static final boolean ADD_HEADER_BITMAP = true;
74
Winson Chung47c4c692014-03-17 10:17:11 -070075 /** The RecentsView callbacks */
76 public interface RecentsViewCallbacks {
Winson Chung7aceb9a2014-07-03 13:38:01 -070077 public void onTaskViewClicked();
Winson Chung4e96eb72014-09-17 15:16:09 +020078 public void onTaskLaunchFailed();
Winson Chung7aceb9a2014-07-03 13:38:01 -070079 public void onAllTaskViewsDismissed();
Jorim Jaggi6e18e002015-06-02 17:07:39 -070080 public void runAfterPause(Runnable r);
Winson Chung47c4c692014-03-17 10:17:11 -070081 }
82
Winson Chungd42a6cf2014-06-03 16:24:04 -070083 RecentsConfiguration mConfig;
84 LayoutInflater mInflater;
85
Winson Chungdcfa7972014-07-22 12:27:13 -070086 ArrayList<TaskStack> mStacks;
Winson147ecaf2015-09-16 16:49:55 -070087 TaskStackView mTaskStackView;
Winson Chungaee097c2015-04-02 18:16:02 -070088 RecentsAppWidgetHostView mSearchBar;
Winson Chung47c4c692014-03-17 10:17:11 -070089 RecentsViewCallbacks mCb;
Winsonbe7607a2015-10-01 17:24:51 -070090
91 RecentsViewTouchHandler mTouchHandler;
92 DragView mDragView;
Winson882072b2015-10-12 11:26:33 -070093 TaskStack.DockState[] mVisibleDockStates = {
94 TaskStack.DockState.LEFT,
95 TaskStack.DockState.TOP,
96 TaskStack.DockState.RIGHT,
97 TaskStack.DockState.BOTTOM,
98 };
Winsonbe7607a2015-10-01 17:24:51 -070099
Winson35f30502015-09-28 11:24:36 -0700100 Interpolator mFastOutSlowInInterpolator;
101
102 Rect mSystemInsets = new Rect();
Winson Chung303e1ff2014-03-07 15:06:19 -0800103
104 public RecentsView(Context context) {
105 super(context);
Winson Chung8e548f72014-06-24 14:40:53 -0700106 }
107
108 public RecentsView(Context context, AttributeSet attrs) {
109 this(context, attrs, 0);
110 }
111
112 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
113 this(context, attrs, defStyleAttr, 0);
114 }
115
116 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
117 super(context, attrs, defStyleAttr, defStyleRes);
Winsonbe7607a2015-10-01 17:24:51 -0700118 setWillNotDraw(false);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700119 mConfig = RecentsConfiguration.getInstance();
Winson Chungecd9b302014-04-16 17:07:18 -0700120 mInflater = LayoutInflater.from(context);
Winson35f30502015-09-28 11:24:36 -0700121 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
122 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonbe7607a2015-10-01 17:24:51 -0700123 mTouchHandler = new RecentsViewTouchHandler(this);
Winson Chungd16c5652015-01-26 16:11:07 -0800124 }
125
Winson Chung47c4c692014-03-17 10:17:11 -0700126 /** Sets the callbacks */
127 public void setCallbacks(RecentsViewCallbacks cb) {
128 mCb = cb;
129 }
130
Winson Chungdcfa7972014-07-22 12:27:13 -0700131 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700132 public void setTaskStack(TaskStack stack) {
Winson35f30502015-09-28 11:24:36 -0700133 if (mConfig.getLaunchState().launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700134 if (mTaskStackView != null) {
135 // If onRecentsHidden is not triggered, we need to the stack view again here
136 mTaskStackView.reset();
137 mTaskStackView.setStack(stack);
138 } else {
139 mTaskStackView = new TaskStackView(getContext(), stack);
140 mTaskStackView.setCallbacks(this);
141 addView(mTaskStackView);
142 }
143 } else {
144 if (mTaskStackView != null) {
145 removeView(mTaskStackView);
146 }
147 mTaskStackView = new TaskStackView(getContext(), stack);
148 mTaskStackView.setCallbacks(this);
149 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800150 }
Winson Chung02d49272014-08-29 13:57:29 -0700151
Winson Chungb0a28ea2014-10-28 15:21:35 -0700152 // Trigger a new layout
153 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700154 }
155
Skuhne8aa7d162015-03-20 13:40:53 -0700156 /** Gets the next task in the stack - or if the last - the top task */
157 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700158 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700159 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700160 if (mTaskStackView != null) {
161 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700162 ArrayList<Task> taskList = stack.getTasks();
163 // Iterate the stack views and try and find the focused task
164 for (int j = taskList.size() - 1; j >= 0; --j) {
165 Task task = taskList.get(j);
166 // Return the next task in the line.
167 if (found)
168 return task;
169 // Remember the first possible task as the top task.
170 if (returnTask == null)
171 returnTask = task;
172 if (task == taskToSearch)
173 found = true;
174 }
175 }
176 return returnTask;
177 }
178
Winson Chung1e8d71b2014-05-16 17:05:22 -0700179 /** Launches the focused task from the first stack if possible */
180 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700181 if (mTaskStackView != null) {
182 TaskStack stack = mTaskStackView.getStack();
Winson Chungd16c5652015-01-26 16:11:07 -0800183 // Iterate the stack views and try and find the focused task
Winson147ecaf2015-09-16 16:49:55 -0700184 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Winson Chungd16c5652015-01-26 16:11:07 -0800185 int taskViewCount = taskViews.size();
186 for (int j = 0; j < taskViewCount; j++) {
187 TaskView tv = taskViews.get(j);
188 Task task = tv.getTask();
189 if (tv.isFocusedTask()) {
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700190 onTaskViewClicked(mTaskStackView, tv, stack, task, false, false, null,
191 INVALID_STACK_ID);
Winson Chungd16c5652015-01-26 16:11:07 -0800192 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700193 }
194 }
195 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700196 return false;
197 }
198
Skuhne8aa7d162015-03-20 13:40:53 -0700199 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700200 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700201 if (mTaskStackView != null) {
202 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700203 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700204 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700205 int taskViewCount = taskViews.size();
206 for (int j = 0; j < taskViewCount; j++) {
207 TaskView tv = taskViews.get(j);
208 if (tv.getTask() == task) {
Winsonb1bbaed2015-09-23 15:45:11 -0700209 onTaskViewClicked(mTaskStackView, tv, stack, task, false, taskBounds != null,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700210 taskBounds, destinationStack);
Skuhne8aa7d162015-03-20 13:40:53 -0700211 return true;
212 }
213 }
214 }
215 return false;
216 }
217
Winson Chungdcfa7972014-07-22 12:27:13 -0700218 /** Launches the task that Recents was launched from, if possible */
219 public boolean launchPreviousTask() {
Winson147ecaf2015-09-16 16:49:55 -0700220 if (mTaskStackView != null) {
221 TaskStack stack = mTaskStackView.getStack();
Winson Chungd16c5652015-01-26 16:11:07 -0800222 ArrayList<Task> tasks = stack.getTasks();
Winson Chung47c4c692014-03-17 10:17:11 -0700223
Winson Chungd16c5652015-01-26 16:11:07 -0800224 // Find the launch task in the stack
Winsonbe7607a2015-10-01 17:24:51 -0700225 // TODO: replace this with an event from RecentsActivity
Winson Chungd16c5652015-01-26 16:11:07 -0800226 if (!tasks.isEmpty()) {
227 int taskCount = tasks.size();
228 for (int j = 0; j < taskCount; j++) {
229 if (tasks.get(j).isLaunchTarget) {
230 Task task = tasks.get(j);
Winson147ecaf2015-09-16 16:49:55 -0700231 TaskView tv = mTaskStackView.getChildViewForTask(task);
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700232 onTaskViewClicked(mTaskStackView, tv, stack, task, false, false, null,
233 INVALID_STACK_ID);
Winson Chungd16c5652015-01-26 16:11:07 -0800234 return true;
Winson Chung303e1ff2014-03-07 15:06:19 -0800235 }
236 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800237 }
238 }
239 return false;
240 }
241
Winson Chung24cf1522014-05-29 12:03:33 -0700242 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700243 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung740c3ac2014-11-12 16:14: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.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700249 }
Winson Chung740c3ac2014-11-12 16:14:38 -0800250 ctx.postAnimationTrigger.decrement();
Winson Chung24cf1522014-05-29 12:03:33 -0700251 }
252
Winson Chungd42a6cf2014-06-03 16:24:04 -0700253 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700254 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chunga91c2932014-11-07 15:02:38 -0800255 // We have to increment/decrement the post animation trigger in case there are no children
256 // to ensure that it runs
257 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700258 if (mTaskStackView != null) {
259 mTaskStackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700260 }
Winson Chunga91c2932014-11-07 15:02:38 -0800261 ctx.postAnimationTrigger.decrement();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700262
Winson Chung969f5862014-06-16 17:08:24 -0700263 // Notify of the exit animation
Winson412e1802015-10-20 16:57:57 -0700264 EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
Winson Chungd42a6cf2014-06-03 16:24:04 -0700265 }
266
Winson Chungf7bca432014-04-30 17:11:13 -0700267 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700268 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700269 // Remove the previous search bar if one exists
270 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
271 removeView(mSearchBar);
272 }
273 // Add the new search bar
274 if (searchBar != null) {
275 mSearchBar = searchBar;
276 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700277 }
Winson Chungecd9b302014-04-16 17:07:18 -0700278 }
279
Winson Chung772b6b12014-07-03 15:54:02 -0700280 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700281 public boolean hasValidSearchBar() {
282 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700283 }
284
285 /** Sets the visibility of the search bar */
286 public void setSearchBarVisibility(int visibility) {
287 if (mSearchBar != null) {
288 mSearchBar.setVisibility(visibility);
Winson Chung012ef362014-07-31 18:36:25 -0700289 // Always bring the search bar to the top
290 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700291 }
292 }
293
Winsonbe7607a2015-10-01 17:24:51 -0700294 @Override
295 protected void onAttachedToWindow() {
296 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
297 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
298 super.onAttachedToWindow();
299 }
300
301 @Override
302 protected void onDetachedFromWindow() {
303 super.onDetachedFromWindow();
304 EventBus.getDefault().unregister(this);
305 EventBus.getDefault().unregister(mTouchHandler);
306 }
307
Winson Chungf7bca432014-04-30 17:11:13 -0700308 /**
309 * This is called with the full size of the window since we are handling our own insets.
310 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800311 @Override
312 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
313 int width = MeasureSpec.getSize(widthMeasureSpec);
314 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700315
Winson Chungf7bca432014-04-30 17:11:13 -0700316 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700317 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700318 if (mSearchBar != null) {
Winson35f30502015-09-28 11:24:36 -0700319 mConfig.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700320 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700321 mSearchBar.measure(
322 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
323 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700324 }
325
Winson Chungf7bca432014-04-30 17:11:13 -0700326 Rect taskStackBounds = new Rect();
Winson88f00ab2015-10-05 17:24:00 -0700327 mConfig.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700328 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700329 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700330 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700331 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800332 }
333
Winsonbe7607a2015-10-01 17:24:51 -0700334 if (mDragView != null) {
335 mDragView.measure(
336 MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST),
337 MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));
338 }
339
Winson Chung303e1ff2014-03-07 15:06:19 -0800340 setMeasuredDimension(width, height);
341 }
342
Winson Chungf7bca432014-04-30 17:11:13 -0700343 /**
344 * This is called with the full size of the window since we are handling our own insets.
345 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800346 @Override
347 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson Chungf7bca432014-04-30 17:11:13 -0700348 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700349 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
350 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700351 if (mSearchBar != null) {
Winson147ecaf2015-09-16 16:49:55 -0700352 mConfig.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700353 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700354 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
355 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700356 }
357
Winson147ecaf2015-09-16 16:49:55 -0700358 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
359 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800360 }
Winsonbe7607a2015-10-01 17:24:51 -0700361
362 if (mDragView != null) {
363 mDragView.layout(left, top, left + mDragView.getMeasuredWidth(),
364 top + mDragView.getMeasuredHeight());
365 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800366 }
367
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700368 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700369 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700370 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700371 requestLayout();
Winson Chungdcfa7972014-07-22 12:27:13 -0700372 return insets.consumeSystemWindowInsets();
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700373 }
374
Winsonbe7607a2015-10-01 17:24:51 -0700375 @Override
376 public boolean onInterceptTouchEvent(MotionEvent ev) {
377 return mTouchHandler.onInterceptTouchEvent(ev);
378 }
379
380 @Override
381 public boolean onTouchEvent(MotionEvent ev) {
382 return mTouchHandler.onTouchEvent(ev);
383 }
384
385 @Override
386 protected void dispatchDraw(Canvas canvas) {
387 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700388 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
389 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
390 if (d.getAlpha() > 0) {
391 d.draw(canvas);
392 }
Winsonbe7607a2015-10-01 17:24:51 -0700393 }
394 }
395
Winson4165d3362015-10-10 14:40:35 -0700396 @Override
397 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700398 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
399 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
400 if (d == who) {
401 return true;
402 }
403 }
404 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700405 }
406
Winson Chunga26fb782014-06-12 17:52:39 -0700407 /** Notifies each task view of the user interaction. */
408 public void onUserInteraction() {
409 // Get the first stack view
Winson147ecaf2015-09-16 16:49:55 -0700410 if (mTaskStackView != null) {
411 mTaskStackView.onUserInteraction();
Winson Chunga26fb782014-06-12 17:52:39 -0700412 }
413 }
414
Winson Chung1e8d71b2014-05-16 17:05:22 -0700415 /** Focuses the next task in the first stack view */
416 public void focusNextTask(boolean forward) {
417 // Get the first stack view
Winson147ecaf2015-09-16 16:49:55 -0700418 if (mTaskStackView != null) {
419 mTaskStackView.focusNextTask(forward, true);
Winson Chung1e8d71b2014-05-16 17:05:22 -0700420 }
Winson Chunga0e88b52014-08-11 19:25:42 -0700421 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700422
Winson Chunga0e88b52014-08-11 19:25:42 -0700423 /** Dismisses the focused task. */
424 public void dismissFocusedTask() {
425 // Get the first stack view
Winson147ecaf2015-09-16 16:49:55 -0700426 if (mTaskStackView != null) {
427 mTaskStackView.dismissFocusedTask();
Winson Chung1e8d71b2014-05-16 17:05:22 -0700428 }
429 }
430
Winson Chung303e1ff2014-03-07 15:06:19 -0800431 /** Unfilters any filtered stacks */
432 public boolean unfilterFilteredStacks() {
Winson Chungdcfa7972014-07-22 12:27:13 -0700433 if (mStacks != null) {
Winson Chung303e1ff2014-03-07 15:06:19 -0800434 // Check if there are any filtered stacks and unfilter them before we back out of Recents
435 boolean stacksUnfiltered = false;
Winson Chungdcfa7972014-07-22 12:27:13 -0700436 int numStacks = mStacks.size();
437 for (int i = 0; i < numStacks; i++) {
438 TaskStack stack = mStacks.get(i);
Winson Chung303e1ff2014-03-07 15:06:19 -0800439 if (stack.hasFilteredTasks()) {
440 stack.unfilterTasks();
441 stacksUnfiltered = true;
442 }
443 }
444 return stacksUnfiltered;
445 }
446 return false;
447 }
448
Jorim Jaggi900fb482015-06-02 15:07:33 -0700449 public void disableLayersForOneFrame() {
Winson147ecaf2015-09-16 16:49:55 -0700450 if (mTaskStackView != null) {
451 mTaskStackView.disableLayersForOneFrame();
Jorim Jaggi900fb482015-06-02 15:07:33 -0700452 }
453 }
454
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700455 private void postDrawHeaderThumbnailTransitionRunnable(final TaskStackView view,
456 final TaskView clickedView, final int offsetX, final int offsetY,
457 final float stackScroll,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700458 final ActivityOptions.OnAnimationStartedListener animStartedListener,
459 final int destinationStack) {
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700460 Runnable r = new Runnable() {
461 @Override
462 public void run() {
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700463 overrideDrawHeaderThumbnailTransition(view, clickedView, offsetX, offsetY,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700464 stackScroll, animStartedListener, destinationStack);
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700465
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700466 }
467 };
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700468
Jorim Jaggi6e18e002015-06-02 17:07:39 -0700469 mCb.runAfterPause(r);
470 }
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700471
472 private void overrideDrawHeaderThumbnailTransition(TaskStackView stackView,
473 TaskView clickedTask, int offsetX, int offsetY, float stackScroll,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700474 final ActivityOptions.OnAnimationStartedListener animStartedListener,
475 int destinationStack) {
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700476 List<AppTransitionAnimationSpec> specs = getAppTransitionAnimationSpecs(stackView,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700477 clickedTask, offsetX, offsetY, stackScroll, destinationStack);
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700478 if (specs == null) {
479 return;
480 }
481
482 IRemoteCallback.Stub callback = new IRemoteCallback.Stub() {
483 @Override
484 public void sendResult(Bundle data) throws RemoteException {
485 post(new Runnable() {
486 @Override
487 public void run() {
488 if (animStartedListener != null) {
489 animStartedListener.onAnimationStarted();
490 }
491 }
492 });
493 }
494 };
495
496 AppTransitionAnimationSpec[] specsArray =
497 new AppTransitionAnimationSpec[specs.size()];
498 try {
499 WindowManagerGlobal.getWindowManagerService().overridePendingAppTransitionMultiThumb(
500 specs.toArray(specsArray), callback, true /* scaleUp */);
501
502 } catch (RemoteException e) {
503 Log.w(TAG, "Error overriding app transition", e);
504 }
505 }
506
507 private List<AppTransitionAnimationSpec> getAppTransitionAnimationSpecs(TaskStackView stackView,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700508 TaskView clickedTask, int offsetX, int offsetY, float stackScroll,
509 int destinationStack) {
510 final int targetStackId = destinationStack != INVALID_STACK_ID ?
511 destinationStack : clickedTask.getTask().key.stackId;
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700512 if (targetStackId != ActivityManager.FREEFORM_WORKSPACE_STACK_ID
513 && targetStackId != ActivityManager.FULLSCREEN_WORKSPACE_STACK_ID) {
514 return null;
515 }
516 // If this is a full screen stack, the transition will be towards the single, full screen
517 // task. We only need the transition spec for this task.
518 List<AppTransitionAnimationSpec> specs = new ArrayList<>();
519 if (targetStackId == ActivityManager.FULLSCREEN_WORKSPACE_STACK_ID) {
520 specs.add(createThumbnailHeaderAnimationSpec(
521 stackView, offsetX, offsetY, stackScroll, clickedTask,
522 clickedTask.getTask().key.id, ADD_HEADER_BITMAP));
523 return specs;
524 }
525 // This is a free form stack or full screen stack, so there will be multiple windows
526 // animating from thumbnails. We need transition animation specs for all of them.
527
528 // We will use top and bottom task views as a base for tasks, that aren't visible on the
529 // screen. This is necessary for cascade recents list, where some of the tasks might be
530 // hidden.
531 List<TaskView> taskViews = stackView.getTaskViews();
532 int childCount = taskViews.size();
533 TaskView topChild = taskViews.get(0);
534 TaskView bottomChild = taskViews.get(childCount - 1);
535 SparseArray<TaskView> taskViewsByTaskId = new SparseArray<>();
536 for (int i = 0; i < childCount; i++) {
537 TaskView taskView = taskViews.get(i);
538 taskViewsByTaskId.put(taskView.getTask().key.id, taskView);
539 }
540
541 TaskStack stack = stackView.getStack();
542 // We go through all tasks now and for each generate transition animation spec. If there is
543 // a view associated with a task, we use that view as a base for the animation. If there
544 // isn't, we use bottom or top view, depending on which one would be closer to the task
545 // view if it existed.
546 ArrayList<Task> tasks = stack.getTasks();
547 boolean passedClickedTask = false;
548 for (int i = 0, n = tasks.size(); i < n; i++) {
549 Task task = tasks.get(i);
550 TaskView taskView = taskViewsByTaskId.get(task.key.id);
551 if (taskView != null) {
552 specs.add(createThumbnailHeaderAnimationSpec(stackView, offsetX, offsetY,
553 stackScroll, taskView, taskView.getTask().key.id, ADD_HEADER_BITMAP));
554 if (taskView == clickedTask) {
555 passedClickedTask = true;
556 }
557 } else {
558 taskView = passedClickedTask ? bottomChild : topChild;
559 specs.add(createThumbnailHeaderAnimationSpec(stackView, offsetX, offsetY,
560 stackScroll, taskView, task.key.id, !ADD_HEADER_BITMAP));
561 }
562 }
563
564 return specs;
565 }
566
567 private AppTransitionAnimationSpec createThumbnailHeaderAnimationSpec(TaskStackView stackView,
568 int offsetX, int offsetY, float stackScroll, TaskView tv, int taskId,
569 boolean addHeaderBitmap) {
570 // Disable any focused state before we draw the header
571 // Upfront the processing of the thumbnail
572 if (tv.isFocusedTask()) {
573 tv.unsetFocusedTask();
574 }
575 TaskViewTransform transform = new TaskViewTransform();
576 transform = stackView.getStackAlgorithm().getStackTransform(tv.mTask, stackScroll,
577 transform, null);
578
579 float scale = tv.getScaleX();
580 int fromHeaderWidth = (int) (tv.mHeaderView.getMeasuredWidth() * scale);
581 int fromHeaderHeight = (int) (tv.mHeaderView.getMeasuredHeight() * scale);
582
583 Bitmap b = null;
584 if (addHeaderBitmap) {
585 b = Bitmap.createBitmap(fromHeaderWidth, fromHeaderHeight,
586 Bitmap.Config.ARGB_8888);
587
588 if (Constants.DebugFlags.App.EnableTransitionThumbnailDebugMode) {
589 b.eraseColor(0xFFff0000);
590 } else {
591 Canvas c = new Canvas(b);
592 c.scale(tv.getScaleX(), tv.getScaleY());
593 tv.mHeaderView.draw(c);
594 c.setBitmap(null);
595
596 }
597 b = b.createAshmemBitmap();
598 }
599
600 int[] pts = new int[2];
601 tv.getLocationOnScreen(pts);
602
603 final int left = pts[0] + offsetX;
604 final int top = pts[1] + offsetY;
605 final Rect rect = new Rect(left, top, left + transform.rect.width(),
606 top + transform.rect.height());
607
608 return new AppTransitionAnimationSpec(taskId, b, rect);
609 }
610
Winson Chung47c4c692014-03-17 10:17:11 -0700611 /**** TaskStackView.TaskStackCallbacks Implementation ****/
Winson Chung303e1ff2014-03-07 15:06:19 -0800612
613 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700614 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700615 final TaskStack stack, final Task task, final boolean lockToTask,
616 final boolean boundsValid, final Rect bounds, int destinationStack) {
Winson Chung47c4c692014-03-17 10:17:11 -0700617 // Notify any callbacks of the launching of a new task
618 if (mCb != null) {
Winson Chung7aceb9a2014-07-03 13:38:01 -0700619 mCb.onTaskViewClicked();
Winson Chung47c4c692014-03-17 10:17:11 -0700620 }
621
Winson Chunge0e45bc2014-06-17 17:56:17 -0700622 // Upfront the processing of the thumbnail
Winson Chungffa2ec62014-07-03 15:54:42 -0700623 TaskViewTransform transform = new TaskViewTransform();
Jorim Jaggicb557032014-09-16 23:09:24 +0200624 View sourceView;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700625 int offsetX = 0;
626 int offsetY = 0;
Winson Chung012ef362014-07-31 18:36:25 -0700627 float stackScroll = stackView.getScroller().getStackScroll();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700628 if (tv == null) {
629 // If there is no actual task view, then use the stack view as the source view
630 // and then offset to the expected transform rect, but bound this to just
631 // outside the display rect (to ensure we don't animate from too far away)
632 sourceView = stackView;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700633 offsetX = transform.rect.left;
Winson35f30502015-09-28 11:24:36 -0700634 offsetY = getMeasuredHeight();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700635 } else {
Jorim Jaggicb557032014-09-16 23:09:24 +0200636 sourceView = tv.mThumbnailView;
Winson Chunge0e45bc2014-06-17 17:56:17 -0700637 }
638
639 // Compute the thumbnail to scale up from
Winson Chung1f24c7e2014-07-11 17:06:48 -0700640 final SystemServicesProxy ssp =
641 RecentsTaskLoader.getInstance().getSystemServicesProxy();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700642 ActivityOptions opts = null;
Chong Zhang0fa656b2015-08-31 15:17:21 -0700643 ActivityOptions.OnAnimationStartedListener animStartedListener = null;
Winson Chung2e7f3bd2014-09-05 13:17:22 +0200644 if (task.thumbnail != null && task.thumbnail.getWidth() > 0 &&
645 task.thumbnail.getHeight() > 0) {
Winson Chung1f24c7e2014-07-11 17:06:48 -0700646 if (lockToTask) {
647 animStartedListener = new ActivityOptions.OnAnimationStartedListener() {
648 boolean mTriggered = false;
649 @Override
650 public void onAnimationStarted() {
651 if (!mTriggered) {
652 postDelayed(new Runnable() {
653 @Override
654 public void run() {
Winson412e1802015-10-20 16:57:57 -0700655 RecentsTaskLoader loader = RecentsTaskLoader.getInstance();
656 SystemServicesProxy ssp = loader.getSystemServicesProxy();
657 EventBus.getDefault().send(new ScreenPinningRequestEvent(
658 getContext(), ssp));
Winson Chung1f24c7e2014-07-11 17:06:48 -0700659 }
660 }, 350);
661 mTriggered = true;
662 }
663 }
664 };
665 }
Filip Gruszczynski170192a2015-08-16 17:46:34 -0700666 postDrawHeaderThumbnailTransitionRunnable(stackView, tv, offsetX, offsetY, stackScroll,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700667 animStartedListener, destinationStack);
Winsonec417e42015-09-29 12:42:29 -0700668 opts = ActivityOptions.makeThumbnailAspectScaleUpAnimation(sourceView,
669 Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8).createAshmemBitmap(),
670 offsetX, offsetY, transform.rect.width(), transform.rect.height(),
671 sourceView.getHandler(), animStartedListener);
Chong Zhang0fa656b2015-08-31 15:17:21 -0700672 } else {
673 opts = ActivityOptions.makeBasic();
Winson Chunge0e45bc2014-06-17 17:56:17 -0700674 }
Chong Zhang0fa656b2015-08-31 15:17:21 -0700675 if (boundsValid) {
Winsonb1bbaed2015-09-23 15:45:11 -0700676 opts.setBounds(bounds.isEmpty() ? null : bounds);
Chong Zhang0fa656b2015-08-31 15:17:21 -0700677 }
Winson Chunge0e45bc2014-06-17 17:56:17 -0700678 final ActivityOptions launchOpts = opts;
Chong Zhang0fa656b2015-08-31 15:17:21 -0700679 final boolean screenPinningRequested = (animStartedListener == null) && lockToTask;
Winson Chung303e1ff2014-03-07 15:06:19 -0800680 final Runnable launchRunnable = new Runnable() {
681 @Override
682 public void run() {
Winson Chung4be04452014-03-24 17:22:30 -0700683 if (task.isActive) {
684 // Bring an active task to the foreground
Craig Mautnerdc00cbe2014-07-20 17:48:47 -0700685 ssp.moveTaskToFront(task.key.id, launchOpts);
Winson Chung4be04452014-03-24 17:22:30 -0700686 } else {
Winson Chung4e96eb72014-09-17 15:16:09 +0200687 if (ssp.startActivityFromRecents(getContext(), task.key.id,
688 task.activityLabel, launchOpts)) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700689 if (screenPinningRequested) {
Winson412e1802015-10-20 16:57:57 -0700690 RecentsTaskLoader loader = RecentsTaskLoader.getInstance();
691 SystemServicesProxy ssp = loader.getSystemServicesProxy();
692 EventBus.getDefault().send(new ScreenPinningRequestEvent(
693 getContext(), ssp));
Winson Chung4be04452014-03-24 17:22:30 -0700694 }
Winson Chung4e96eb72014-09-17 15:16:09 +0200695 } else {
696 // Dismiss the task and return the user to home if we fail to
697 // launch the task
Winson2536c7e2015-10-01 15:49:31 -0700698 EventBus.getDefault().send(new DismissTaskEvent(task, tv));
Winson Chung4e96eb72014-09-17 15:16:09 +0200699 if (mCb != null) {
700 mCb.onTaskLaunchFailed();
701 }
Winson Chung5c9f4b92015-06-25 16:16:46 -0700702
703 // Keep track of failed launches
704 MetricsLogger.count(getContext(), "overview_task_launch_failed", 1);
Winson Chung4be04452014-03-24 17:22:30 -0700705 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800706 }
707 }
708 };
709
Winson Chung5c9f4b92015-06-25 16:16:46 -0700710 // Keep track of the index of the task launch
711 int taskIndexFromFront = 0;
712 int taskIndex = stack.indexOfTask(task);
713 if (taskIndex > -1) {
714 taskIndexFromFront = stack.getTaskCount() - taskIndex - 1;
715 }
716 MetricsLogger.histogram(getContext(), "overview_task_launch_index", taskIndexFromFront);
717
Winson Chung303e1ff2014-03-07 15:06:19 -0800718 // Launch the app right away if there is no task view, otherwise, animate the icon out first
Winson Chung814086d2014-05-07 15:01:14 -0700719 if (tv == null) {
Winson Chunga91c2932014-11-07 15:02:38 -0800720 launchRunnable.run();
Winson Chung303e1ff2014-03-07 15:06:19 -0800721 } else {
Winson Chunge1e20e12015-06-02 14:11:49 -0700722 if (task.group != null && !task.group.isFrontMostTask(task)) {
Winson Chunga4ccb862014-08-22 15:26:27 -0700723 // For affiliated tasks that are behind other tasks, we must animate the front cards
724 // out of view before starting the task transition
Winson Chungebfc6982014-08-26 12:25:34 -0700725 stackView.startLaunchTaskAnimation(tv, launchRunnable, lockToTask);
Winson Chunga4ccb862014-08-22 15:26:27 -0700726 } else {
727 // Otherwise, we can start the task transition immediately
Winson Chungebfc6982014-08-26 12:25:34 -0700728 stackView.startLaunchTaskAnimation(tv, null, lockToTask);
Winson Chunga91c2932014-11-07 15:02:38 -0800729 launchRunnable.run();
Winson Chunga4ccb862014-08-22 15:26:27 -0700730 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800731 }
732 }
Winson Chung9f9679d2014-04-11 16:49:09 -0700733
734 @Override
Winson Chung6ac8bd62015-01-07 16:38:35 -0800735 public void onAllTaskViewsDismissed(ArrayList<Task> removedTasks) {
Winson2536c7e2015-10-01 15:49:31 -0700736 /* TODO: Not currently enabled
Winson Chung6ac8bd62015-01-07 16:38:35 -0800737 if (removedTasks != null) {
738 int taskCount = removedTasks.size();
739 for (int i = 0; i < taskCount; i++) {
740 onTaskViewDismissed(removedTasks.get(i));
741 }
742 }
Winson2536c7e2015-10-01 15:49:31 -0700743 */
Winson Chung6ac8bd62015-01-07 16:38:35 -0800744
Winson Chung7aceb9a2014-07-03 13:38:01 -0700745 mCb.onAllTaskViewsDismissed();
Winson Chung5c9f4b92015-06-25 16:16:46 -0700746
747 // Keep track of all-deletions
748 MetricsLogger.count(getContext(), "overview_task_all_dismissed", 1);
Winson Chungd7b2cb12014-06-26 15:08:50 -0700749 }
750
Winson Chungb0a28ea2014-10-28 15:21:35 -0700751 /** Final callback after Recents is finally hidden. */
752 public void onRecentsHidden() {
753 // Notify each task stack view
Winson147ecaf2015-09-16 16:49:55 -0700754 if (mTaskStackView != null) {
755 mTaskStackView.onRecentsHidden();
Winson Chungb0a28ea2014-10-28 15:21:35 -0700756 }
757 }
758
Winson Chungd7b2cb12014-06-26 15:08:50 -0700759 @Override
Winson Chung8e548f72014-06-24 14:40:53 -0700760 public void onTaskStackFilterTriggered() {
761 // Hide the search bar
762 if (mSearchBar != null) {
Winson35f30502015-09-28 11:24:36 -0700763 int filterDuration = getResources().getInteger(
764 R.integer.recents_filter_animate_current_views_duration);
Winson Chung8e548f72014-06-24 14:40:53 -0700765 mSearchBar.animate()
766 .alpha(0f)
767 .setStartDelay(0)
Winson35f30502015-09-28 11:24:36 -0700768 .setInterpolator(mFastOutSlowInInterpolator)
769 .setDuration(filterDuration)
Winson Chung8e548f72014-06-24 14:40:53 -0700770 .withLayer()
771 .start();
772 }
773 }
774
775 @Override
776 public void onTaskStackUnfilterTriggered() {
777 // Show the search bar
778 if (mSearchBar != null) {
Winson35f30502015-09-28 11:24:36 -0700779 int filterDuration = getResources().getInteger(
780 R.integer.recents_filter_animate_new_views_duration);
Winson Chung8e548f72014-06-24 14:40:53 -0700781 mSearchBar.animate()
782 .alpha(1f)
783 .setStartDelay(0)
Winson35f30502015-09-28 11:24:36 -0700784 .setInterpolator(mFastOutSlowInInterpolator)
785 .setDuration(filterDuration)
Winson Chung8e548f72014-06-24 14:40:53 -0700786 .withLayer()
787 .start();
788 }
789 }
Winsonbe7607a2015-10-01 17:24:51 -0700790
791 /**** EventBus Events ****/
792
793 public final void onBusEvent(DragStartEvent event) {
794 // Add the drag view
795 mDragView = event.dragView;
796 addView(mDragView);
Winson4165d3362015-10-10 14:40:35 -0700797
Winson882072b2015-10-12 11:26:33 -0700798 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
799 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700800 }
801
802 public final void onBusEvent(DragDockStateChangedEvent event) {
Winson882072b2015-10-12 11:26:33 -0700803 if (event.dockState == TaskStack.DockState.NONE) {
804 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
805 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
806 } else {
807 updateVisibleDockRegions(new TaskStack.DockState[] {event.dockState}, -1);
808 }
Winsonbe7607a2015-10-01 17:24:51 -0700809 }
810
811 public final void onBusEvent(final DragEndEvent event) {
812 event.postAnimationTrigger.increment();
813 event.postAnimationTrigger.addLastDecrementRunnable(new Runnable() {
814 @Override
815 public void run() {
816 // Remove the drag view
817 removeView(mDragView);
818 mDragView = null;
Winson882072b2015-10-12 11:26:33 -0700819 updateVisibleDockRegions(null, -1);
Winsonbe7607a2015-10-01 17:24:51 -0700820
821 // Dock the new task if we are hovering over a valid dock state
Winson4165d3362015-10-10 14:40:35 -0700822 if (event.dockState != TaskStack.DockState.NONE) {
Winsonbe7607a2015-10-01 17:24:51 -0700823 SystemServicesProxy ssp = RecentsTaskLoader.getInstance().getSystemServicesProxy();
824 ssp.setTaskResizeable(event.task.key.id);
825 ssp.dockTask(event.task.key.id, event.dockState.createMode);
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700826 launchTask(event.task, null, INVALID_STACK_ID);
Winsonbe7607a2015-10-01 17:24:51 -0700827 }
828 }
829 });
Winson4165d3362015-10-10 14:40:35 -0700830 if (event.dockState == TaskStack.DockState.NONE) {
Winsonbe7607a2015-10-01 17:24:51 -0700831 // Animate the alpha back to what it was before
832 Rect taskBounds = mTaskStackView.getStackAlgorithm().getUntransformedTaskViewBounds();
833 int left = taskBounds.left + (int) ((1f - event.taskView.getScaleX()) * taskBounds.width()) / 2;
834 int top = taskBounds.top + (int) ((1f - event.taskView.getScaleY()) * taskBounds.height()) / 2;
835 event.dragView.animate()
Winson4165d3362015-10-10 14:40:35 -0700836 .scaleX(1f)
837 .scaleY(1f)
Winsonbe7607a2015-10-01 17:24:51 -0700838 .translationX(left + event.taskView.getTranslationX())
839 .translationY(top + event.taskView.getTranslationY())
840 .setDuration(175)
Winson4165d3362015-10-10 14:40:35 -0700841 .setInterpolator(mFastOutSlowInInterpolator)
Winsonbe7607a2015-10-01 17:24:51 -0700842 .withEndAction(event.postAnimationTrigger.decrementAsRunnable())
Winsonbe7607a2015-10-01 17:24:51 -0700843 .start();
Winson4165d3362015-10-10 14:40:35 -0700844
845 // Animate the overlay alpha back to 0
Winson882072b2015-10-12 11:26:33 -0700846 updateVisibleDockRegions(null, -1);
Winsonbe7607a2015-10-01 17:24:51 -0700847 } else {
848 event.postAnimationTrigger.decrement();
849 }
850 }
Winson4165d3362015-10-10 14:40:35 -0700851
852 /**
853 * Updates the dock region to match the specified dock state.
854 */
Winson882072b2015-10-12 11:26:33 -0700855 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
856 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
857 if (newDockStates != null) {
858 for (TaskStack.DockState dockState : newDockStates) {
859 newDockStatesSet.add(dockState);
860 }
Winson4165d3362015-10-10 14:40:35 -0700861 }
Winson882072b2015-10-12 11:26:33 -0700862 for (TaskStack.DockState dockState : mVisibleDockStates) {
863 TaskStack.DockState.ViewState viewState = dockState.viewState;
864 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
865 // This is no longer visible, so hide it
866 viewState.startAlphaAnimation(0, 150);
867 } else {
868 // This state is now visible, update the bounds and show it
869 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
870 viewState.dockAreaOverlay.setBounds(
871 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
872 viewState.dockAreaOverlay.setCallback(this);
873 viewState.startAlphaAnimation(alpha, 150);
874 }
Winson4165d3362015-10-10 14:40:35 -0700875 }
Winson4165d3362015-10-10 14:40:35 -0700876 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800877}