blob: 90456c0838d736477823af24173bca89eb2ccf6f [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
Winson Chung303e1ff2014-03-07 15:06:19 -080019import android.content.Context;
Winson Chung303e1ff2014-03-07 15:06:19 -080020import android.graphics.Canvas;
21import android.graphics.Rect;
Winson4165d3362015-10-10 14:40:35 -070022import android.graphics.drawable.Drawable;
Winson83c1b072015-11-09 10:48:04 -080023import android.os.Handler;
Winson882072b2015-10-12 11:26:33 -070024import android.util.ArraySet;
Winson Chung8e548f72014-06-24 14:40:53 -070025import android.util.AttributeSet;
Winson Chungecd9b302014-04-16 17:07:18 -070026import android.view.LayoutInflater;
Winsonbe7607a2015-10-01 17:24:51 -070027import android.view.MotionEvent;
Winson Chung653f70c22014-05-19 14:49:42 -070028import android.view.WindowInsets;
Winson35f30502015-09-28 11:24:36 -070029import android.view.animation.AnimationUtils;
30import android.view.animation.Interpolator;
Winson Chung303e1ff2014-03-07 15:06:19 -080031import android.widget.FrameLayout;
Winson Chung5c9f4b92015-06-25 16:16:46 -070032import com.android.internal.logging.MetricsLogger;
Winson Chungd16c5652015-01-26 16:11:07 -080033import com.android.systemui.R;
Winsone7f138c2015-10-22 16:15:21 -070034import com.android.systemui.recents.Recents;
Winsonbe7607a2015-10-01 17:24:51 -070035import com.android.systemui.recents.RecentsActivity;
Winsonb1e71d02015-11-23 12:40:23 -080036import com.android.systemui.recents.RecentsActivityLaunchState;
Winson Chungaee097c2015-04-02 18:16:02 -070037import com.android.systemui.recents.RecentsAppWidgetHostView;
Winson Chung303e1ff2014-03-07 15:06:19 -080038import com.android.systemui.recents.RecentsConfiguration;
Winson2536c7e2015-10-01 15:49:31 -070039import com.android.systemui.recents.events.EventBus;
Winson13d30662015-11-06 15:30:29 -080040import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
Winson412e1802015-10-20 16:57:57 -070041import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
Winsonb1e71d02015-11-23 12:40:23 -080042import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
Jorim Jaggidd98d412015-11-18 15:57:38 -080043import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
44import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
Winsoneca4ab62015-11-04 10:50:28 -080045import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
Winsonbe7607a2015-10-01 17:24:51 -070046import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
47import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
Winson Chung1f24c7e2014-07-11 17:06:48 -070048import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chung303e1ff2014-03-07 15:06:19 -080049import com.android.systemui.recents.model.Task;
50import com.android.systemui.recents.model.TaskStack;
51
52import java.util.ArrayList;
Winson Chung6ac8bd62015-01-07 16:38:35 -080053import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080054
Wale Ogunwale3797c222015-10-27 14:21:58 -070055import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070056
Winson Chung303e1ff2014-03-07 15:06:19 -080057/**
58 * This view is the the top level layout that contains TaskStacks (which are laid out according
59 * to their SpaceNode bounds.
60 */
Winsone6c90732015-09-24 16:06:29 -070061public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks {
Winson Chung47c4c692014-03-17 10:17:11 -070062
Jorim Jaggi6e18e002015-06-02 17:07:39 -070063 private static final String TAG = "RecentsView";
Winson0d14d4d2015-10-26 17:05:04 -070064 private static final boolean DEBUG = false;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070065
Winson Chungd42a6cf2014-06-03 16:24:04 -070066 LayoutInflater mInflater;
Winson83c1b072015-11-09 10:48:04 -080067 Handler mHandler;
Winson Chungd42a6cf2014-06-03 16:24:04 -070068
Winson147ecaf2015-09-16 16:49:55 -070069 TaskStackView mTaskStackView;
Winson Chungaee097c2015-04-02 18:16:02 -070070 RecentsAppWidgetHostView mSearchBar;
Winsonb1e71d02015-11-23 12:40:23 -080071 boolean mAwaitingFirstLayout = true;
72 boolean mLastTaskLaunchedWasFreeform;
Winsonbe7607a2015-10-01 17:24:51 -070073
Winson83c1b072015-11-09 10:48:04 -080074 RecentsTransitionHelper mTransitionHelper;
Winsonbe7607a2015-10-01 17:24:51 -070075 RecentsViewTouchHandler mTouchHandler;
76 DragView mDragView;
Winson882072b2015-10-12 11:26:33 -070077 TaskStack.DockState[] mVisibleDockStates = {
78 TaskStack.DockState.LEFT,
79 TaskStack.DockState.TOP,
80 TaskStack.DockState.RIGHT,
81 TaskStack.DockState.BOTTOM,
82 };
Winsonbe7607a2015-10-01 17:24:51 -070083
Winson35f30502015-09-28 11:24:36 -070084 Interpolator mFastOutSlowInInterpolator;
85
86 Rect mSystemInsets = new Rect();
Winson Chung303e1ff2014-03-07 15:06:19 -080087
88 public RecentsView(Context context) {
89 super(context);
Winson Chung8e548f72014-06-24 14:40:53 -070090 }
91
92 public RecentsView(Context context, AttributeSet attrs) {
93 this(context, attrs, 0);
94 }
95
96 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
97 this(context, attrs, defStyleAttr, 0);
98 }
99
100 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
101 super(context, attrs, defStyleAttr, defStyleRes);
Winsonbe7607a2015-10-01 17:24:51 -0700102 setWillNotDraw(false);
Winson Chungecd9b302014-04-16 17:07:18 -0700103 mInflater = LayoutInflater.from(context);
Winson83c1b072015-11-09 10:48:04 -0800104 mHandler = new Handler();
105 mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
Winson35f30502015-09-28 11:24:36 -0700106 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
107 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonbe7607a2015-10-01 17:24:51 -0700108 mTouchHandler = new RecentsViewTouchHandler(this);
Winson Chungd16c5652015-01-26 16:11:07 -0800109 }
110
Winson Chungdcfa7972014-07-22 12:27:13 -0700111 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700112 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700113 RecentsConfiguration config = Recents.getConfiguration();
114 if (config.getLaunchState().launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700115 if (mTaskStackView != null) {
116 // If onRecentsHidden is not triggered, we need to the stack view again here
117 mTaskStackView.reset();
118 mTaskStackView.setStack(stack);
119 } else {
120 mTaskStackView = new TaskStackView(getContext(), stack);
121 mTaskStackView.setCallbacks(this);
122 addView(mTaskStackView);
123 }
124 } else {
125 if (mTaskStackView != null) {
126 removeView(mTaskStackView);
127 }
128 mTaskStackView = new TaskStackView(getContext(), stack);
129 mTaskStackView.setCallbacks(this);
130 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800131 }
Winson Chung02d49272014-08-29 13:57:29 -0700132
Winson Chungb0a28ea2014-10-28 15:21:35 -0700133 // Trigger a new layout
134 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700135 }
136
Winsonb1e71d02015-11-23 12:40:23 -0800137 /**
138 * Returns whether the last task launched was in the freeform stack or not.
139 */
140 public boolean isLastTaskLaunchedFreeform() {
141 return mLastTaskLaunchedWasFreeform;
142 }
143
Skuhne8aa7d162015-03-20 13:40:53 -0700144 /** Gets the next task in the stack - or if the last - the top task */
145 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700146 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700147 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700148 if (mTaskStackView != null) {
149 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700150 ArrayList<Task> taskList = stack.getTasks();
151 // Iterate the stack views and try and find the focused task
152 for (int j = taskList.size() - 1; j >= 0; --j) {
153 Task task = taskList.get(j);
154 // Return the next task in the line.
155 if (found)
156 return task;
157 // Remember the first possible task as the top task.
158 if (returnTask == null)
159 returnTask = task;
160 if (task == taskToSearch)
161 found = true;
162 }
163 }
164 return returnTask;
165 }
166
Winson Chung1e8d71b2014-05-16 17:05:22 -0700167 /** Launches the focused task from the first stack if possible */
168 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700169 if (mTaskStackView != null) {
170 TaskStack stack = mTaskStackView.getStack();
Winson142af422015-11-09 10:39:57 -0800171 Task task = mTaskStackView.getFocusedTask();
172 if (task != null) {
173 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson83c1b072015-11-09 10:48:04 -0800174 onTaskViewClicked(mTaskStackView, taskView, stack, task, false, null,
Winson142af422015-11-09 10:39:57 -0800175 INVALID_STACK_ID);
176 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700177 }
178 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700179 return false;
180 }
181
Skuhne8aa7d162015-03-20 13:40:53 -0700182 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700183 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700184 if (mTaskStackView != null) {
185 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700186 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700187 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700188 int taskViewCount = taskViews.size();
189 for (int j = 0; j < taskViewCount; j++) {
190 TaskView tv = taskViews.get(j);
191 if (tv.getTask() == task) {
Winson83c1b072015-11-09 10:48:04 -0800192 onTaskViewClicked(mTaskStackView, tv, stack, task, false,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700193 taskBounds, destinationStack);
Skuhne8aa7d162015-03-20 13:40:53 -0700194 return true;
195 }
196 }
197 }
198 return false;
199 }
200
Winson Chung24cf1522014-05-29 12:03:33 -0700201 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700202 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800203 // We have to increment/decrement the post animation trigger in case there are no children
204 // to ensure that it runs
205 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700206 if (mTaskStackView != null) {
207 mTaskStackView.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700208 }
Winson Chung740c3ac2014-11-12 16:14:38 -0800209 ctx.postAnimationTrigger.decrement();
Winson Chung24cf1522014-05-29 12:03:33 -0700210 }
211
Winson Chungd42a6cf2014-06-03 16:24:04 -0700212 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700213 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chunga91c2932014-11-07 15:02:38 -0800214 // We have to increment/decrement the post animation trigger in case there are no children
215 // to ensure that it runs
216 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700217 if (mTaskStackView != null) {
218 mTaskStackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700219 }
Winson Chunga91c2932014-11-07 15:02:38 -0800220 ctx.postAnimationTrigger.decrement();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700221
Winson13d30662015-11-06 15:30:29 -0800222 // If we are going home, cancel the previous task's window transition
223 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
224
Winson Chung969f5862014-06-16 17:08:24 -0700225 // Notify of the exit animation
Winson412e1802015-10-20 16:57:57 -0700226 EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
Winson Chungd42a6cf2014-06-03 16:24:04 -0700227 }
228
Winson Chungf7bca432014-04-30 17:11:13 -0700229 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700230 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700231 // Remove the previous search bar if one exists
232 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
233 removeView(mSearchBar);
234 }
235 // Add the new search bar
236 if (searchBar != null) {
237 mSearchBar = searchBar;
238 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700239 }
Winson Chungecd9b302014-04-16 17:07:18 -0700240 }
241
Winson Chung772b6b12014-07-03 15:54:02 -0700242 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700243 public boolean hasValidSearchBar() {
244 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700245 }
246
247 /** Sets the visibility of the search bar */
248 public void setSearchBarVisibility(int visibility) {
249 if (mSearchBar != null) {
250 mSearchBar.setVisibility(visibility);
Winson Chung012ef362014-07-31 18:36:25 -0700251 // Always bring the search bar to the top
252 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700253 }
254 }
255
Winsonbe7607a2015-10-01 17:24:51 -0700256 @Override
257 protected void onAttachedToWindow() {
258 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
259 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
260 super.onAttachedToWindow();
261 }
262
263 @Override
264 protected void onDetachedFromWindow() {
265 super.onDetachedFromWindow();
266 EventBus.getDefault().unregister(this);
267 EventBus.getDefault().unregister(mTouchHandler);
268 }
269
Winson Chungf7bca432014-04-30 17:11:13 -0700270 /**
271 * This is called with the full size of the window since we are handling our own insets.
272 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800273 @Override
274 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700275 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800276 int width = MeasureSpec.getSize(widthMeasureSpec);
277 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700278
Winson Chungf7bca432014-04-30 17:11:13 -0700279 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700280 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700281 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700282 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700283 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700284 mSearchBar.measure(
285 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
286 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700287 }
288
Winson Chungf7bca432014-04-30 17:11:13 -0700289 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700290 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700291 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700292 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700293 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700294 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800295 }
296
Winsonbe7607a2015-10-01 17:24:51 -0700297 if (mDragView != null) {
Winsoneca4ab62015-11-04 10:50:28 -0800298 Rect taskRect = mTaskStackView.mLayoutAlgorithm.mTaskRect;
Winsonbe7607a2015-10-01 17:24:51 -0700299 mDragView.measure(
Winsoneca4ab62015-11-04 10:50:28 -0800300 MeasureSpec.makeMeasureSpec(taskRect.width(), MeasureSpec.AT_MOST),
301 MeasureSpec.makeMeasureSpec(taskRect.height(), MeasureSpec.AT_MOST));
Winsonbe7607a2015-10-01 17:24:51 -0700302 }
303
Winson Chung303e1ff2014-03-07 15:06:19 -0800304 setMeasuredDimension(width, height);
305 }
306
Winson Chungf7bca432014-04-30 17:11:13 -0700307 /**
308 * This is called with the full size of the window since we are handling our own insets.
309 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800310 @Override
311 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700312 RecentsConfiguration config = Recents.getConfiguration();
313
Winson Chungf7bca432014-04-30 17:11:13 -0700314 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700315 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
316 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700317 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700318 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700319 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700320 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
321 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700322 }
323
Winson147ecaf2015-09-16 16:49:55 -0700324 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
325 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800326 }
Winsonbe7607a2015-10-01 17:24:51 -0700327
328 if (mDragView != null) {
329 mDragView.layout(left, top, left + mDragView.getMeasuredWidth(),
330 top + mDragView.getMeasuredHeight());
331 }
Winsonb1e71d02015-11-23 12:40:23 -0800332
333 if (mAwaitingFirstLayout) {
334 mAwaitingFirstLayout = false;
335
336 // If launched via dragging from the nav bar, then we should translate the whole view
337 // down offscreen
338 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
339 if (launchState.launchedViaDragGesture) {
340 setTranslationY(getMeasuredHeight());
341 }
342 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800343 }
344
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700345 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700346 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700347 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700348 requestLayout();
Winson Chungdcfa7972014-07-22 12:27:13 -0700349 return insets.consumeSystemWindowInsets();
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700350 }
351
Winsonbe7607a2015-10-01 17:24:51 -0700352 @Override
353 public boolean onInterceptTouchEvent(MotionEvent ev) {
354 return mTouchHandler.onInterceptTouchEvent(ev);
355 }
356
357 @Override
358 public boolean onTouchEvent(MotionEvent ev) {
359 return mTouchHandler.onTouchEvent(ev);
360 }
361
362 @Override
363 protected void dispatchDraw(Canvas canvas) {
364 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700365 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
366 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
367 if (d.getAlpha() > 0) {
368 d.draw(canvas);
369 }
Winsonbe7607a2015-10-01 17:24:51 -0700370 }
371 }
372
Winson4165d3362015-10-10 14:40:35 -0700373 @Override
374 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700375 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
376 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
377 if (d == who) {
378 return true;
379 }
380 }
381 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700382 }
383
Jorim Jaggi900fb482015-06-02 15:07:33 -0700384 public void disableLayersForOneFrame() {
Winson147ecaf2015-09-16 16:49:55 -0700385 if (mTaskStackView != null) {
386 mTaskStackView.disableLayersForOneFrame();
Jorim Jaggi900fb482015-06-02 15:07:33 -0700387 }
388 }
389
Winson Chung47c4c692014-03-17 10:17:11 -0700390 /**** TaskStackView.TaskStackCallbacks Implementation ****/
Winson Chung303e1ff2014-03-07 15:06:19 -0800391
392 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700393 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700394 final TaskStack stack, final Task task, final boolean lockToTask,
Winson83c1b072015-11-09 10:48:04 -0800395 final Rect bounds, int destinationStack) {
Winsonb1e71d02015-11-23 12:40:23 -0800396 mLastTaskLaunchedWasFreeform = SystemServicesProxy.isFreeformStack(task.key.stackId);
Winson83c1b072015-11-09 10:48:04 -0800397 mTransitionHelper.launchTaskFromRecents(stack, task, stackView, tv, lockToTask, bounds,
398 destinationStack);
Winson Chung303e1ff2014-03-07 15:06:19 -0800399 }
Winson Chung9f9679d2014-04-11 16:49:09 -0700400
Winsonbe7607a2015-10-01 17:24:51 -0700401 /**** EventBus Events ****/
402
403 public final void onBusEvent(DragStartEvent event) {
404 // Add the drag view
405 mDragView = event.dragView;
406 addView(mDragView);
Winson4165d3362015-10-10 14:40:35 -0700407
Winson882072b2015-10-12 11:26:33 -0700408 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
409 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700410 }
411
Winsoneca4ab62015-11-04 10:50:28 -0800412 public final void onBusEvent(DragDropTargetChangedEvent event) {
413 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700414 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
415 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
416 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800417 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
418 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700419 }
Winsonbe7607a2015-10-01 17:24:51 -0700420 }
421
422 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800423 final Runnable cleanUpRunnable = new Runnable() {
Winsonbe7607a2015-10-01 17:24:51 -0700424 @Override
425 public void run() {
426 // Remove the drag view
427 removeView(mDragView);
428 mDragView = null;
Winsonbe7607a2015-10-01 17:24:51 -0700429 }
Winsoneca4ab62015-11-04 10:50:28 -0800430 };
431
432 // Animate the overlay alpha back to 0
433 updateVisibleDockRegions(null, -1);
434
435 if (event.dropTarget == null) {
436 // No drop targets for hit, so just animate the task back to its place
437 event.postAnimationTrigger.increment();
438 event.postAnimationTrigger.addLastDecrementRunnable(new Runnable() {
439 @Override
440 public void run() {
441 cleanUpRunnable.run();
442 }
443 });
444 // Animate the task back to where it was before then clean up afterwards
445 TaskViewTransform taskTransform = new TaskViewTransform();
446 TaskStackLayoutAlgorithm layoutAlgorithm = mTaskStackView.getStackAlgorithm();
447 layoutAlgorithm.getStackTransform(event.task,
448 mTaskStackView.getScroller().getStackScroll(), taskTransform, null);
Winsonbe7607a2015-10-01 17:24:51 -0700449 event.dragView.animate()
Winsoneca4ab62015-11-04 10:50:28 -0800450 .scaleX(taskTransform.scale)
451 .scaleY(taskTransform.scale)
452 .translationX((layoutAlgorithm.mTaskRect.left - event.dragView.getLeft())
453 + taskTransform.translationX)
454 .translationY((layoutAlgorithm.mTaskRect.top - event.dragView.getTop())
455 + taskTransform.translationY)
Winsonbe7607a2015-10-01 17:24:51 -0700456 .setDuration(175)
Winson4165d3362015-10-10 14:40:35 -0700457 .setInterpolator(mFastOutSlowInInterpolator)
Winsonbe7607a2015-10-01 17:24:51 -0700458 .withEndAction(event.postAnimationTrigger.decrementAsRunnable())
Winsonbe7607a2015-10-01 17:24:51 -0700459 .start();
Winson4165d3362015-10-10 14:40:35 -0700460
Winsoneca4ab62015-11-04 10:50:28 -0800461 } else if (event.dropTarget instanceof TaskStack.DockState) {
462 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
463
464 // For now, just remove the drag view and the original task
465 // TODO: Animate the task to the drop target rect before launching it above
466 cleanUpRunnable.run();
467
468 // Dock the task and launch it
469 SystemServicesProxy ssp = Recents.getSystemServices();
470 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
471 launchTask(event.task, null, INVALID_STACK_ID);
472
Winsonbe7607a2015-10-01 17:24:51 -0700473 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800474 // We dropped on another drop target, so just add the cleanup to the post animation
475 // trigger
476 event.postAnimationTrigger.addLastDecrementRunnable(new Runnable() {
477 @Override
478 public void run() {
479 cleanUpRunnable.run();
480 }
481 });
Winsonbe7607a2015-10-01 17:24:51 -0700482 }
483 }
Winson4165d3362015-10-10 14:40:35 -0700484
Jorim Jaggidd98d412015-11-18 15:57:38 -0800485 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800486 if (mTaskStackView.getTaskViews().size() > 0) {
487 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
488 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800489 }
490
491 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
492 animate().translationY(0f);
493 }
494
Winson4165d3362015-10-10 14:40:35 -0700495 /**
496 * Updates the dock region to match the specified dock state.
497 */
Winson882072b2015-10-12 11:26:33 -0700498 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
499 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
500 if (newDockStates != null) {
501 for (TaskStack.DockState dockState : newDockStates) {
502 newDockStatesSet.add(dockState);
503 }
Winson4165d3362015-10-10 14:40:35 -0700504 }
Winson882072b2015-10-12 11:26:33 -0700505 for (TaskStack.DockState dockState : mVisibleDockStates) {
506 TaskStack.DockState.ViewState viewState = dockState.viewState;
507 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
508 // This is no longer visible, so hide it
509 viewState.startAlphaAnimation(0, 150);
510 } else {
511 // This state is now visible, update the bounds and show it
512 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
513 viewState.dockAreaOverlay.setBounds(
514 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
515 viewState.dockAreaOverlay.setCallback(this);
516 viewState.startAlphaAnimation(alpha, 150);
517 }
Winson4165d3362015-10-10 14:40:35 -0700518 }
Winson4165d3362015-10-10 14:40:35 -0700519 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800520
Winsonb1e71d02015-11-23 12:40:23 -0800521 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
522 if (!event.visible) {
523 // Reset the view state
524 mAwaitingFirstLayout = true;
525 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800526 }
527 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800528}