blob: 0557f651acc6ee620b017882fc6fa8227df0a7f8 [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;
Winsonc29ff002015-11-20 16:00:45 -080020import android.content.res.Resources;
Winson Chung303e1ff2014-03-07 15:06:19 -080021import android.graphics.Canvas;
22import android.graphics.Rect;
Winson4165d3362015-10-10 14:40:35 -070023import android.graphics.drawable.Drawable;
Winson83c1b072015-11-09 10:48:04 -080024import android.os.Handler;
Winson882072b2015-10-12 11:26:33 -070025import android.util.ArraySet;
Winson Chung8e548f72014-06-24 14:40:53 -070026import android.util.AttributeSet;
Winson Chungecd9b302014-04-16 17:07:18 -070027import android.view.LayoutInflater;
Winsonbe7607a2015-10-01 17:24:51 -070028import android.view.MotionEvent;
Winsonc29ff002015-11-20 16:00:45 -080029import android.view.View;
Winson Chung653f70c22014-05-19 14:49:42 -070030import android.view.WindowInsets;
Winson35f30502015-09-28 11:24:36 -070031import android.view.animation.AnimationUtils;
32import android.view.animation.Interpolator;
Winson Chung303e1ff2014-03-07 15:06:19 -080033import android.widget.FrameLayout;
Winson Chungd16c5652015-01-26 16:11:07 -080034import com.android.systemui.R;
Winsone7f138c2015-10-22 16:15:21 -070035import com.android.systemui.recents.Recents;
Winsonbe7607a2015-10-01 17:24:51 -070036import com.android.systemui.recents.RecentsActivity;
Winsonb1e71d02015-11-23 12:40:23 -080037import com.android.systemui.recents.RecentsActivityLaunchState;
Winson Chungaee097c2015-04-02 18:16:02 -070038import com.android.systemui.recents.RecentsAppWidgetHostView;
Winson Chung303e1ff2014-03-07 15:06:19 -080039import com.android.systemui.recents.RecentsConfiguration;
Winsonc29ff002015-11-20 16:00:45 -080040import com.android.systemui.recents.RecentsDebugFlags;
Winson2536c7e2015-10-01 15:49:31 -070041import com.android.systemui.recents.events.EventBus;
Winson13d30662015-11-06 15:30:29 -080042import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
Winsonc29ff002015-11-20 16:00:45 -080043import com.android.systemui.recents.events.activity.DebugFlagsChangedEvent;
Winson412e1802015-10-20 16:57:57 -070044import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
Winsonc29ff002015-11-20 16:00:45 -080045import com.android.systemui.recents.events.activity.HideHistoryButtonEvent;
46import com.android.systemui.recents.events.activity.HideHistoryEvent;
47import com.android.systemui.recents.events.activity.ShowHistoryButtonEvent;
48import com.android.systemui.recents.events.activity.ShowHistoryEvent;
Winsonb1e71d02015-11-23 12:40:23 -080049import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
Jorim Jaggidd98d412015-11-18 15:57:38 -080050import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
51import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
Winsoneca4ab62015-11-04 10:50:28 -080052import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
Winsonbe7607a2015-10-01 17:24:51 -070053import 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 Chung303e1ff2014-03-07 15:06:19 -080056import com.android.systemui.recents.model.Task;
57import com.android.systemui.recents.model.TaskStack;
58
59import java.util.ArrayList;
Winson Chung6ac8bd62015-01-07 16:38:35 -080060import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080061
Wale Ogunwale3797c222015-10-27 14:21:58 -070062import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070063
Winson Chung303e1ff2014-03-07 15:06:19 -080064/**
65 * This view is the the top level layout that contains TaskStacks (which are laid out according
66 * to their SpaceNode bounds.
67 */
Winsone6c90732015-09-24 16:06:29 -070068public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks {
Winson Chung47c4c692014-03-17 10:17:11 -070069
Jorim Jaggi6e18e002015-06-02 17:07:39 -070070 private static final String TAG = "RecentsView";
Winson0d14d4d2015-10-26 17:05:04 -070071 private static final boolean DEBUG = false;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070072
Winson83c1b072015-11-09 10:48:04 -080073 Handler mHandler;
Winson Chungd42a6cf2014-06-03 16:24:04 -070074
Winsonc29ff002015-11-20 16:00:45 -080075 TaskStack mStack;
Winson147ecaf2015-09-16 16:49:55 -070076 TaskStackView mTaskStackView;
Winson Chungaee097c2015-04-02 18:16:02 -070077 RecentsAppWidgetHostView mSearchBar;
Winsonc29ff002015-11-20 16:00:45 -080078 View mHistoryButton;
Winsonb1e71d02015-11-23 12:40:23 -080079 boolean mAwaitingFirstLayout = true;
80 boolean mLastTaskLaunchedWasFreeform;
Winsonbe7607a2015-10-01 17:24:51 -070081
Winson83c1b072015-11-09 10:48:04 -080082 RecentsTransitionHelper mTransitionHelper;
Winsonbe7607a2015-10-01 17:24:51 -070083 RecentsViewTouchHandler mTouchHandler;
84 DragView mDragView;
Winson882072b2015-10-12 11:26:33 -070085 TaskStack.DockState[] mVisibleDockStates = {
86 TaskStack.DockState.LEFT,
87 TaskStack.DockState.TOP,
88 TaskStack.DockState.RIGHT,
89 TaskStack.DockState.BOTTOM,
90 };
Winsonbe7607a2015-10-01 17:24:51 -070091
Winsonc29ff002015-11-20 16:00:45 -080092 private Interpolator mFastOutSlowInInterpolator;
93 private Interpolator mFastOutLinearInInterpolator;
94 private int mHistoryTransitionDuration;
Winson35f30502015-09-28 11:24:36 -070095
96 Rect mSystemInsets = new Rect();
Winson Chung303e1ff2014-03-07 15:06:19 -080097
98 public RecentsView(Context context) {
99 super(context);
Winson Chung8e548f72014-06-24 14:40:53 -0700100 }
101
102 public RecentsView(Context context, AttributeSet attrs) {
103 this(context, attrs, 0);
104 }
105
106 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
107 this(context, attrs, defStyleAttr, 0);
108 }
109
110 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
111 super(context, attrs, defStyleAttr, defStyleRes);
Winsonc29ff002015-11-20 16:00:45 -0800112 Resources res = context.getResources();
Winsonbe7607a2015-10-01 17:24:51 -0700113 setWillNotDraw(false);
Winson83c1b072015-11-09 10:48:04 -0800114 mHandler = new Handler();
115 mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
Winson35f30502015-09-28 11:24:36 -0700116 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
117 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonc29ff002015-11-20 16:00:45 -0800118 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
119 com.android.internal.R.interpolator.fast_out_linear_in);
120 mHistoryTransitionDuration = res.getInteger(R.integer.recents_history_transition_duration);
Winsonbe7607a2015-10-01 17:24:51 -0700121 mTouchHandler = new RecentsViewTouchHandler(this);
Winsonc29ff002015-11-20 16:00:45 -0800122
123 LayoutInflater inflater = LayoutInflater.from(context);
124 mHistoryButton = inflater.inflate(R.layout.recents_history_button, this, false);
125 mHistoryButton.setOnClickListener(new View.OnClickListener() {
126 @Override
127 public void onClick(View v) {
128 EventBus.getDefault().send(new ShowHistoryEvent());
129 }
130 });
Winson Chungd16c5652015-01-26 16:11:07 -0800131 }
132
Winson Chungdcfa7972014-07-22 12:27:13 -0700133 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700134 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700135 RecentsConfiguration config = Recents.getConfiguration();
Winsonc29ff002015-11-20 16:00:45 -0800136 mStack = stack;
Winson53ec42c2015-10-28 15:55:35 -0700137 if (config.getLaunchState().launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700138 if (mTaskStackView != null) {
139 // If onRecentsHidden is not triggered, we need to the stack view again here
140 mTaskStackView.reset();
141 mTaskStackView.setStack(stack);
142 } else {
143 mTaskStackView = new TaskStackView(getContext(), stack);
144 mTaskStackView.setCallbacks(this);
145 addView(mTaskStackView);
146 }
147 } else {
148 if (mTaskStackView != null) {
149 removeView(mTaskStackView);
150 }
151 mTaskStackView = new TaskStackView(getContext(), stack);
152 mTaskStackView.setCallbacks(this);
153 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800154 }
Winsonc29ff002015-11-20 16:00:45 -0800155 if (indexOfChild(mHistoryButton) == -1) {
156 addView(mHistoryButton);
157 } else {
158 mHistoryButton.bringToFront();
159 }
Winson Chung02d49272014-08-29 13:57:29 -0700160
Winson Chungb0a28ea2014-10-28 15:21:35 -0700161 // Trigger a new layout
162 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700163 }
164
Winsonb1e71d02015-11-23 12:40:23 -0800165 /**
166 * Returns whether the last task launched was in the freeform stack or not.
167 */
168 public boolean isLastTaskLaunchedFreeform() {
169 return mLastTaskLaunchedWasFreeform;
170 }
171
Winsonc29ff002015-11-20 16:00:45 -0800172 /**
173 * Returns the currently set task stack.
174 */
175 public TaskStack getTaskStack() {
176 return mStack;
177 }
178
Skuhne8aa7d162015-03-20 13:40:53 -0700179 /** Gets the next task in the stack - or if the last - the top task */
180 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700181 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700182 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700183 if (mTaskStackView != null) {
184 TaskStack stack = mTaskStackView.getStack();
Winson250608a2015-11-24 15:00:31 -0800185 ArrayList<Task> taskList = stack.getStackTasks();
Skuhne8aa7d162015-03-20 13:40:53 -0700186 // Iterate the stack views and try and find the focused task
187 for (int j = taskList.size() - 1; j >= 0; --j) {
188 Task task = taskList.get(j);
189 // Return the next task in the line.
190 if (found)
191 return task;
192 // Remember the first possible task as the top task.
193 if (returnTask == null)
194 returnTask = task;
195 if (task == taskToSearch)
196 found = true;
197 }
198 }
199 return returnTask;
200 }
201
Winson Chung1e8d71b2014-05-16 17:05:22 -0700202 /** Launches the focused task from the first stack if possible */
203 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700204 if (mTaskStackView != null) {
205 TaskStack stack = mTaskStackView.getStack();
Winson142af422015-11-09 10:39:57 -0800206 Task task = mTaskStackView.getFocusedTask();
207 if (task != null) {
208 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson83c1b072015-11-09 10:48:04 -0800209 onTaskViewClicked(mTaskStackView, taskView, stack, task, false, null,
Winson142af422015-11-09 10:39:57 -0800210 INVALID_STACK_ID);
211 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700212 }
213 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700214 return false;
215 }
216
Skuhne8aa7d162015-03-20 13:40:53 -0700217 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700218 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700219 if (mTaskStackView != null) {
220 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700221 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700222 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700223 int taskViewCount = taskViews.size();
224 for (int j = 0; j < taskViewCount; j++) {
225 TaskView tv = taskViews.get(j);
226 if (tv.getTask() == task) {
Winson83c1b072015-11-09 10:48:04 -0800227 onTaskViewClicked(mTaskStackView, tv, stack, task, false,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700228 taskBounds, destinationStack);
Skuhne8aa7d162015-03-20 13:40:53 -0700229 return true;
230 }
231 }
232 }
233 return false;
234 }
235
Winson Chung24cf1522014-05-29 12:03:33 -0700236 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700237 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800238 // We have to increment/decrement the post animation trigger in case there are no children
239 // to ensure that it runs
240 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700241 if (mTaskStackView != null) {
242 mTaskStackView.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700243 }
Winson Chung740c3ac2014-11-12 16:14:38 -0800244 ctx.postAnimationTrigger.decrement();
Winson Chung24cf1522014-05-29 12:03:33 -0700245 }
246
Winson Chungd42a6cf2014-06-03 16:24:04 -0700247 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700248 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chunga91c2932014-11-07 15:02:38 -0800249 // We have to increment/decrement the post animation trigger in case there are no children
250 // to ensure that it runs
251 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700252 if (mTaskStackView != null) {
253 mTaskStackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700254 }
Winson Chunga91c2932014-11-07 15:02:38 -0800255 ctx.postAnimationTrigger.decrement();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700256
Winsonc29ff002015-11-20 16:00:45 -0800257 // Hide the history button
258 int taskViewExitToHomeDuration = getResources().getInteger(
259 R.integer.recents_task_exit_to_home_duration);
260 hideHistoryButton(taskViewExitToHomeDuration);
261
Winson13d30662015-11-06 15:30:29 -0800262 // If we are going home, cancel the previous task's window transition
263 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
264
Winsonc29ff002015-11-20 16:00:45 -0800265 // Notify sof the exit animation
Winson412e1802015-10-20 16:57:57 -0700266 EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
Winson Chungd42a6cf2014-06-03 16:24:04 -0700267 }
268
Winson Chungf7bca432014-04-30 17:11:13 -0700269 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700270 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700271 // Remove the previous search bar if one exists
272 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
273 removeView(mSearchBar);
274 }
275 // Add the new search bar
276 if (searchBar != null) {
277 mSearchBar = searchBar;
278 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700279 }
Winson Chungecd9b302014-04-16 17:07:18 -0700280 }
281
Winson Chung772b6b12014-07-03 15:54:02 -0700282 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700283 public boolean hasValidSearchBar() {
284 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700285 }
286
287 /** Sets the visibility of the search bar */
288 public void setSearchBarVisibility(int visibility) {
289 if (mSearchBar != null) {
290 mSearchBar.setVisibility(visibility);
Winson Chung012ef362014-07-31 18:36:25 -0700291 // Always bring the search bar to the top
292 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700293 }
294 }
295
Winsonc29ff002015-11-20 16:00:45 -0800296 /**
297 * Returns the last known system insets.
298 */
299 public Rect getSystemInsets() {
300 return mSystemInsets;
301 }
302
Winsonbe7607a2015-10-01 17:24:51 -0700303 @Override
304 protected void onAttachedToWindow() {
305 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
306 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
307 super.onAttachedToWindow();
308 }
309
310 @Override
311 protected void onDetachedFromWindow() {
312 super.onDetachedFromWindow();
313 EventBus.getDefault().unregister(this);
314 EventBus.getDefault().unregister(mTouchHandler);
315 }
316
Winson Chungf7bca432014-04-30 17:11:13 -0700317 /**
318 * This is called with the full size of the window since we are handling our own insets.
319 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800320 @Override
321 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700322 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800323 int width = MeasureSpec.getSize(widthMeasureSpec);
324 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700325
Winson Chungf7bca432014-04-30 17:11:13 -0700326 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700327 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700328 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700329 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700330 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700331 mSearchBar.measure(
332 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
333 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700334 }
335
Winson Chungf7bca432014-04-30 17:11:13 -0700336 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700337 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700338 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700339 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700340 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700341 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800342 }
343
Winsonbe7607a2015-10-01 17:24:51 -0700344 if (mDragView != null) {
Winsoneca4ab62015-11-04 10:50:28 -0800345 Rect taskRect = mTaskStackView.mLayoutAlgorithm.mTaskRect;
Winsonbe7607a2015-10-01 17:24:51 -0700346 mDragView.measure(
Winsoneca4ab62015-11-04 10:50:28 -0800347 MeasureSpec.makeMeasureSpec(taskRect.width(), MeasureSpec.AT_MOST),
348 MeasureSpec.makeMeasureSpec(taskRect.height(), MeasureSpec.AT_MOST));
Winsonbe7607a2015-10-01 17:24:51 -0700349 }
350
Winson250608a2015-11-24 15:00:31 -0800351 // Measure the history button with the full space above the stack, but width-constrained
352 // to the stack
Winson387aac62015-11-25 11:18:56 -0800353 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
Winsonc29ff002015-11-20 16:00:45 -0800354 measureChild(mHistoryButton,
Winson387aac62015-11-25 11:18:56 -0800355 MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
356 MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
Winson250608a2015-11-24 15:00:31 -0800357 MeasureSpec.EXACTLY));
Winson Chung303e1ff2014-03-07 15:06:19 -0800358 setMeasuredDimension(width, height);
359 }
360
Winson Chungf7bca432014-04-30 17:11:13 -0700361 /**
362 * This is called with the full size of the window since we are handling our own insets.
363 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800364 @Override
365 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700366 RecentsConfiguration config = Recents.getConfiguration();
367
Winson Chungf7bca432014-04-30 17:11:13 -0700368 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700369 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
370 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700371 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700372 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700373 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700374 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
375 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700376 }
377
Winson147ecaf2015-09-16 16:49:55 -0700378 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
379 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800380 }
Winsonbe7607a2015-10-01 17:24:51 -0700381
382 if (mDragView != null) {
383 mDragView.layout(left, top, left + mDragView.getMeasuredWidth(),
384 top + mDragView.getMeasuredHeight());
385 }
Winsonb1e71d02015-11-23 12:40:23 -0800386
Winson250608a2015-11-24 15:00:31 -0800387 // Layout the history button left-aligned with the stack, but offset from the top of the
388 // view
Winson387aac62015-11-25 11:18:56 -0800389 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
390 mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
391 historyButtonRect.right, historyButtonRect.bottom);
Winsonc29ff002015-11-20 16:00:45 -0800392
Winsonb1e71d02015-11-23 12:40:23 -0800393 if (mAwaitingFirstLayout) {
394 mAwaitingFirstLayout = false;
395
396 // If launched via dragging from the nav bar, then we should translate the whole view
397 // down offscreen
398 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
399 if (launchState.launchedViaDragGesture) {
400 setTranslationY(getMeasuredHeight());
401 }
402 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800403 }
404
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700405 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700406 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700407 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700408 requestLayout();
Winsonc29ff002015-11-20 16:00:45 -0800409 return insets;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700410 }
411
Winsonbe7607a2015-10-01 17:24:51 -0700412 @Override
413 public boolean onInterceptTouchEvent(MotionEvent ev) {
414 return mTouchHandler.onInterceptTouchEvent(ev);
415 }
416
417 @Override
418 public boolean onTouchEvent(MotionEvent ev) {
419 return mTouchHandler.onTouchEvent(ev);
420 }
421
422 @Override
423 protected void dispatchDraw(Canvas canvas) {
424 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700425 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
426 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
427 if (d.getAlpha() > 0) {
428 d.draw(canvas);
429 }
Winsonbe7607a2015-10-01 17:24:51 -0700430 }
431 }
432
Winson4165d3362015-10-10 14:40:35 -0700433 @Override
434 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700435 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
436 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
437 if (d == who) {
438 return true;
439 }
440 }
441 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700442 }
443
Jorim Jaggi900fb482015-06-02 15:07:33 -0700444 public void disableLayersForOneFrame() {
Winson147ecaf2015-09-16 16:49:55 -0700445 if (mTaskStackView != null) {
446 mTaskStackView.disableLayersForOneFrame();
Jorim Jaggi900fb482015-06-02 15:07:33 -0700447 }
448 }
449
Winson Chung47c4c692014-03-17 10:17:11 -0700450 /**** TaskStackView.TaskStackCallbacks Implementation ****/
Winson Chung303e1ff2014-03-07 15:06:19 -0800451
452 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700453 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700454 final TaskStack stack, final Task task, final boolean lockToTask,
Winson83c1b072015-11-09 10:48:04 -0800455 final Rect bounds, int destinationStack) {
Winson387aac62015-11-25 11:18:56 -0800456 mLastTaskLaunchedWasFreeform = task.isFreeformTask();
Winson83c1b072015-11-09 10:48:04 -0800457 mTransitionHelper.launchTaskFromRecents(stack, task, stackView, tv, lockToTask, bounds,
458 destinationStack);
Winson Chung303e1ff2014-03-07 15:06:19 -0800459 }
Winson Chung9f9679d2014-04-11 16:49:09 -0700460
Winsonbe7607a2015-10-01 17:24:51 -0700461 /**** EventBus Events ****/
462
463 public final void onBusEvent(DragStartEvent event) {
464 // Add the drag view
465 mDragView = event.dragView;
466 addView(mDragView);
Winson4165d3362015-10-10 14:40:35 -0700467
Winson882072b2015-10-12 11:26:33 -0700468 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
469 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700470 }
471
Winsoneca4ab62015-11-04 10:50:28 -0800472 public final void onBusEvent(DragDropTargetChangedEvent event) {
473 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700474 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
475 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
476 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800477 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
478 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700479 }
Winsonbe7607a2015-10-01 17:24:51 -0700480 }
481
482 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800483 final Runnable cleanUpRunnable = new Runnable() {
Winsonbe7607a2015-10-01 17:24:51 -0700484 @Override
485 public void run() {
486 // Remove the drag view
487 removeView(mDragView);
488 mDragView = null;
Winsonbe7607a2015-10-01 17:24:51 -0700489 }
Winsoneca4ab62015-11-04 10:50:28 -0800490 };
491
492 // Animate the overlay alpha back to 0
493 updateVisibleDockRegions(null, -1);
494
495 if (event.dropTarget == null) {
496 // No drop targets for hit, so just animate the task back to its place
497 event.postAnimationTrigger.increment();
498 event.postAnimationTrigger.addLastDecrementRunnable(new Runnable() {
499 @Override
500 public void run() {
501 cleanUpRunnable.run();
502 }
503 });
504 // Animate the task back to where it was before then clean up afterwards
505 TaskViewTransform taskTransform = new TaskViewTransform();
506 TaskStackLayoutAlgorithm layoutAlgorithm = mTaskStackView.getStackAlgorithm();
507 layoutAlgorithm.getStackTransform(event.task,
508 mTaskStackView.getScroller().getStackScroll(), taskTransform, null);
Winsonbe7607a2015-10-01 17:24:51 -0700509 event.dragView.animate()
Winsoneca4ab62015-11-04 10:50:28 -0800510 .scaleX(taskTransform.scale)
511 .scaleY(taskTransform.scale)
512 .translationX((layoutAlgorithm.mTaskRect.left - event.dragView.getLeft())
513 + taskTransform.translationX)
514 .translationY((layoutAlgorithm.mTaskRect.top - event.dragView.getTop())
515 + taskTransform.translationY)
Winsonbe7607a2015-10-01 17:24:51 -0700516 .setDuration(175)
Winson4165d3362015-10-10 14:40:35 -0700517 .setInterpolator(mFastOutSlowInInterpolator)
Winsonbe7607a2015-10-01 17:24:51 -0700518 .withEndAction(event.postAnimationTrigger.decrementAsRunnable())
Winsonbe7607a2015-10-01 17:24:51 -0700519 .start();
Winson4165d3362015-10-10 14:40:35 -0700520
Winsoneca4ab62015-11-04 10:50:28 -0800521 } else if (event.dropTarget instanceof TaskStack.DockState) {
522 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
523
524 // For now, just remove the drag view and the original task
525 // TODO: Animate the task to the drop target rect before launching it above
526 cleanUpRunnable.run();
527
528 // Dock the task and launch it
529 SystemServicesProxy ssp = Recents.getSystemServices();
530 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
531 launchTask(event.task, null, INVALID_STACK_ID);
532
Winsonbe7607a2015-10-01 17:24:51 -0700533 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800534 // We dropped on another drop target, so just add the cleanup to the post animation
535 // trigger
536 event.postAnimationTrigger.addLastDecrementRunnable(new Runnable() {
537 @Override
538 public void run() {
539 cleanUpRunnable.run();
540 }
541 });
Winsonbe7607a2015-10-01 17:24:51 -0700542 }
543 }
Winson4165d3362015-10-10 14:40:35 -0700544
Jorim Jaggidd98d412015-11-18 15:57:38 -0800545 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800546 if (mTaskStackView.getTaskViews().size() > 0) {
547 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
548 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800549 }
550
551 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
552 animate().translationY(0f);
553 }
554
Winsonc29ff002015-11-20 16:00:45 -0800555 public final void onBusEvent(ShowHistoryEvent event) {
556 // Hide the history button when the history view is shown
557 hideHistoryButton(mHistoryTransitionDuration);
558 }
559
560 public final void onBusEvent(HideHistoryEvent event) {
561 // Show the history button when the history view is hidden
562 showHistoryButton(mHistoryTransitionDuration);
563 }
564
565 public final void onBusEvent(ShowHistoryButtonEvent event) {
566 showHistoryButton(150);
567 }
568
569 public final void onBusEvent(HideHistoryButtonEvent event) {
570 hideHistoryButton(100);
571 }
572
573 public final void onBusEvent(DebugFlagsChangedEvent event) {
574 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
575 if (!debugFlags.isHistoryEnabled()) {
576 hideHistoryButton(100);
577 }
578 }
579
580 /**
581 * Shows the history button.
582 */
583 private void showHistoryButton(int duration) {
584 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
585 if (!debugFlags.isHistoryEnabled()) {
586 return;
587 }
588
589 mHistoryButton.setVisibility(View.VISIBLE);
590 mHistoryButton.animate()
591 .alpha(1f)
592 .setDuration(duration)
593 .setInterpolator(mFastOutSlowInInterpolator)
594 .withLayer()
595 .start();
596 }
597
598 /**
599 * Hides the history button.
600 */
601 private void hideHistoryButton(int duration) {
602 mHistoryButton.animate()
603 .alpha(0f)
604 .setDuration(duration)
605 .setInterpolator(mFastOutLinearInInterpolator)
606 .withEndAction(new Runnable() {
607 @Override
608 public void run() {
609 mHistoryButton.setVisibility(View.INVISIBLE);
610 }
611 })
612 .withLayer()
613 .start();
614 }
615
Winson4165d3362015-10-10 14:40:35 -0700616 /**
617 * Updates the dock region to match the specified dock state.
618 */
Winson882072b2015-10-12 11:26:33 -0700619 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
620 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
621 if (newDockStates != null) {
622 for (TaskStack.DockState dockState : newDockStates) {
623 newDockStatesSet.add(dockState);
624 }
Winson4165d3362015-10-10 14:40:35 -0700625 }
Winson882072b2015-10-12 11:26:33 -0700626 for (TaskStack.DockState dockState : mVisibleDockStates) {
627 TaskStack.DockState.ViewState viewState = dockState.viewState;
628 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
629 // This is no longer visible, so hide it
630 viewState.startAlphaAnimation(0, 150);
631 } else {
632 // This state is now visible, update the bounds and show it
633 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
634 viewState.dockAreaOverlay.setBounds(
635 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
636 viewState.dockAreaOverlay.setCallback(this);
637 viewState.startAlphaAnimation(alpha, 150);
638 }
Winson4165d3362015-10-10 14:40:35 -0700639 }
Winson4165d3362015-10-10 14:40:35 -0700640 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800641
Winsonb1e71d02015-11-23 12:40:23 -0800642 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
643 if (!event.visible) {
644 // Reset the view state
645 mAwaitingFirstLayout = true;
646 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800647 }
648 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800649}