blob: 0911578e2c4f53081fdb8329189ced7565db2d99 [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;
Winson882072b2015-10-12 11:26:33 -070084 TaskStack.DockState[] mVisibleDockStates = {
85 TaskStack.DockState.LEFT,
86 TaskStack.DockState.TOP,
87 TaskStack.DockState.RIGHT,
88 TaskStack.DockState.BOTTOM,
89 };
Winsonbe7607a2015-10-01 17:24:51 -070090
Winsonc29ff002015-11-20 16:00:45 -080091 private Interpolator mFastOutSlowInInterpolator;
92 private Interpolator mFastOutLinearInInterpolator;
93 private int mHistoryTransitionDuration;
Winson35f30502015-09-28 11:24:36 -070094
95 Rect mSystemInsets = new Rect();
Winson Chung303e1ff2014-03-07 15:06:19 -080096
97 public RecentsView(Context context) {
98 super(context);
Winson Chung8e548f72014-06-24 14:40:53 -070099 }
100
101 public RecentsView(Context context, AttributeSet attrs) {
102 this(context, attrs, 0);
103 }
104
105 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
106 this(context, attrs, defStyleAttr, 0);
107 }
108
109 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
110 super(context, attrs, defStyleAttr, defStyleRes);
Winsonc29ff002015-11-20 16:00:45 -0800111 Resources res = context.getResources();
Winsonbe7607a2015-10-01 17:24:51 -0700112 setWillNotDraw(false);
Winson83c1b072015-11-09 10:48:04 -0800113 mHandler = new Handler();
114 mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
Winson35f30502015-09-28 11:24:36 -0700115 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
116 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonc29ff002015-11-20 16:00:45 -0800117 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
118 com.android.internal.R.interpolator.fast_out_linear_in);
119 mHistoryTransitionDuration = res.getInteger(R.integer.recents_history_transition_duration);
Winsonbe7607a2015-10-01 17:24:51 -0700120 mTouchHandler = new RecentsViewTouchHandler(this);
Winsonc29ff002015-11-20 16:00:45 -0800121
122 LayoutInflater inflater = LayoutInflater.from(context);
123 mHistoryButton = inflater.inflate(R.layout.recents_history_button, this, false);
124 mHistoryButton.setOnClickListener(new View.OnClickListener() {
125 @Override
126 public void onClick(View v) {
127 EventBus.getDefault().send(new ShowHistoryEvent());
128 }
129 });
Winson Chungd16c5652015-01-26 16:11:07 -0800130 }
131
Winson Chungdcfa7972014-07-22 12:27:13 -0700132 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700133 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700134 RecentsConfiguration config = Recents.getConfiguration();
Winsonc29ff002015-11-20 16:00:45 -0800135 mStack = stack;
Winson53ec42c2015-10-28 15:55:35 -0700136 if (config.getLaunchState().launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700137 if (mTaskStackView != null) {
138 // If onRecentsHidden is not triggered, we need to the stack view again here
139 mTaskStackView.reset();
140 mTaskStackView.setStack(stack);
141 } else {
142 mTaskStackView = new TaskStackView(getContext(), stack);
143 mTaskStackView.setCallbacks(this);
144 addView(mTaskStackView);
145 }
146 } else {
147 if (mTaskStackView != null) {
148 removeView(mTaskStackView);
149 }
150 mTaskStackView = new TaskStackView(getContext(), stack);
151 mTaskStackView.setCallbacks(this);
152 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800153 }
Winsonc29ff002015-11-20 16:00:45 -0800154 if (indexOfChild(mHistoryButton) == -1) {
155 addView(mHistoryButton);
156 } else {
157 mHistoryButton.bringToFront();
158 }
Winson Chung02d49272014-08-29 13:57:29 -0700159
Winson Chungb0a28ea2014-10-28 15:21:35 -0700160 // Trigger a new layout
161 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700162 }
163
Winsonb1e71d02015-11-23 12:40:23 -0800164 /**
165 * Returns whether the last task launched was in the freeform stack or not.
166 */
167 public boolean isLastTaskLaunchedFreeform() {
168 return mLastTaskLaunchedWasFreeform;
169 }
170
Winsonc29ff002015-11-20 16:00:45 -0800171 /**
172 * Returns the currently set task stack.
173 */
174 public TaskStack getTaskStack() {
175 return mStack;
176 }
177
Skuhne8aa7d162015-03-20 13:40:53 -0700178 /** Gets the next task in the stack - or if the last - the top task */
179 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700180 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700181 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700182 if (mTaskStackView != null) {
183 TaskStack stack = mTaskStackView.getStack();
Winson250608a2015-11-24 15:00:31 -0800184 ArrayList<Task> taskList = stack.getStackTasks();
Skuhne8aa7d162015-03-20 13:40:53 -0700185 // Iterate the stack views and try and find the focused task
186 for (int j = taskList.size() - 1; j >= 0; --j) {
187 Task task = taskList.get(j);
188 // Return the next task in the line.
189 if (found)
190 return task;
191 // Remember the first possible task as the top task.
192 if (returnTask == null)
193 returnTask = task;
194 if (task == taskToSearch)
195 found = true;
196 }
197 }
198 return returnTask;
199 }
200
Winson Chung1e8d71b2014-05-16 17:05:22 -0700201 /** Launches the focused task from the first stack if possible */
202 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700203 if (mTaskStackView != null) {
204 TaskStack stack = mTaskStackView.getStack();
Winson142af422015-11-09 10:39:57 -0800205 Task task = mTaskStackView.getFocusedTask();
206 if (task != null) {
207 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson83c1b072015-11-09 10:48:04 -0800208 onTaskViewClicked(mTaskStackView, taskView, stack, task, false, null,
Winson142af422015-11-09 10:39:57 -0800209 INVALID_STACK_ID);
210 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700211 }
212 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700213 return false;
214 }
215
Winsona0731a12015-12-02 15:10:14 -0800216 /** Launches the task that recents was launched from if possible */
217 public boolean launchPreviousTask() {
218 if (mTaskStackView != null) {
219 TaskStack stack = mTaskStackView.getStack();
220 Task task = stack.getLaunchTarget();
221 if (task != null) {
222 TaskView taskView = mTaskStackView.getChildViewForTask(task);
223 onTaskViewClicked(mTaskStackView, taskView, stack, task, false, null,
224 INVALID_STACK_ID);
225 return true;
226 }
227 }
228 return false;
229 }
230
Skuhne8aa7d162015-03-20 13:40:53 -0700231 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700232 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700233 if (mTaskStackView != null) {
234 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700235 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700236 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700237 int taskViewCount = taskViews.size();
238 for (int j = 0; j < taskViewCount; j++) {
239 TaskView tv = taskViews.get(j);
240 if (tv.getTask() == task) {
Winson83c1b072015-11-09 10:48:04 -0800241 onTaskViewClicked(mTaskStackView, tv, stack, task, false,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700242 taskBounds, destinationStack);
Skuhne8aa7d162015-03-20 13:40:53 -0700243 return true;
244 }
245 }
246 }
247 return false;
248 }
249
Winson Chung24cf1522014-05-29 12:03:33 -0700250 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700251 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800252 // We have to increment/decrement the post animation trigger in case there are no children
253 // to ensure that it runs
254 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700255 if (mTaskStackView != null) {
256 mTaskStackView.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700257 }
Winson Chung740c3ac2014-11-12 16:14:38 -0800258 ctx.postAnimationTrigger.decrement();
Winson Chung24cf1522014-05-29 12:03:33 -0700259 }
260
Winson Chungd42a6cf2014-06-03 16:24:04 -0700261 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700262 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chunga91c2932014-11-07 15:02:38 -0800263 // We have to increment/decrement the post animation trigger in case there are no children
264 // to ensure that it runs
265 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700266 if (mTaskStackView != null) {
267 mTaskStackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700268 }
Winson Chunga91c2932014-11-07 15:02:38 -0800269 ctx.postAnimationTrigger.decrement();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700270
Winsonc29ff002015-11-20 16:00:45 -0800271 // Hide the history button
272 int taskViewExitToHomeDuration = getResources().getInteger(
273 R.integer.recents_task_exit_to_home_duration);
274 hideHistoryButton(taskViewExitToHomeDuration);
275
Winson13d30662015-11-06 15:30:29 -0800276 // If we are going home, cancel the previous task's window transition
277 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
278
Winsonc29ff002015-11-20 16:00:45 -0800279 // Notify sof the exit animation
Winson412e1802015-10-20 16:57:57 -0700280 EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
Winson Chungd42a6cf2014-06-03 16:24:04 -0700281 }
282
Winson Chungf7bca432014-04-30 17:11:13 -0700283 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700284 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700285 // Remove the previous search bar if one exists
286 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
287 removeView(mSearchBar);
288 }
289 // Add the new search bar
290 if (searchBar != null) {
291 mSearchBar = searchBar;
292 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700293 }
Winson Chungecd9b302014-04-16 17:07:18 -0700294 }
295
Winson Chung772b6b12014-07-03 15:54:02 -0700296 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700297 public boolean hasValidSearchBar() {
298 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700299 }
300
301 /** Sets the visibility of the search bar */
302 public void setSearchBarVisibility(int visibility) {
303 if (mSearchBar != null) {
304 mSearchBar.setVisibility(visibility);
Winson Chung012ef362014-07-31 18:36:25 -0700305 // Always bring the search bar to the top
306 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700307 }
308 }
309
Winsonc29ff002015-11-20 16:00:45 -0800310 /**
311 * Returns the last known system insets.
312 */
313 public Rect getSystemInsets() {
314 return mSystemInsets;
315 }
316
Winsonbe7607a2015-10-01 17:24:51 -0700317 @Override
318 protected void onAttachedToWindow() {
319 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
320 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
321 super.onAttachedToWindow();
322 }
323
324 @Override
325 protected void onDetachedFromWindow() {
326 super.onDetachedFromWindow();
327 EventBus.getDefault().unregister(this);
328 EventBus.getDefault().unregister(mTouchHandler);
329 }
330
Winson Chungf7bca432014-04-30 17:11:13 -0700331 /**
332 * This is called with the full size of the window since we are handling our own insets.
333 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800334 @Override
335 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700336 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800337 int width = MeasureSpec.getSize(widthMeasureSpec);
338 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700339
Winson Chungf7bca432014-04-30 17:11:13 -0700340 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700341 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700342 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700343 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700344 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700345 mSearchBar.measure(
346 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
347 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700348 }
349
Winson Chungf7bca432014-04-30 17:11:13 -0700350 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700351 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700352 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700353 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700354 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700355 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800356 }
357
Winson250608a2015-11-24 15:00:31 -0800358 // Measure the history button with the full space above the stack, but width-constrained
359 // to the stack
Winson387aac62015-11-25 11:18:56 -0800360 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
Winsonc29ff002015-11-20 16:00:45 -0800361 measureChild(mHistoryButton,
Winson387aac62015-11-25 11:18:56 -0800362 MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
363 MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
Winson250608a2015-11-24 15:00:31 -0800364 MeasureSpec.EXACTLY));
Winson Chung303e1ff2014-03-07 15:06:19 -0800365 setMeasuredDimension(width, height);
366 }
367
Winson Chungf7bca432014-04-30 17:11:13 -0700368 /**
369 * This is called with the full size of the window since we are handling our own insets.
370 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800371 @Override
372 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700373 RecentsConfiguration config = Recents.getConfiguration();
374
Winson Chungf7bca432014-04-30 17:11:13 -0700375 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700376 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
377 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700378 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700379 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700380 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700381 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
382 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700383 }
384
Winson147ecaf2015-09-16 16:49:55 -0700385 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
386 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800387 }
Winsonbe7607a2015-10-01 17:24:51 -0700388
Winson250608a2015-11-24 15:00:31 -0800389 // Layout the history button left-aligned with the stack, but offset from the top of the
390 // view
Winson387aac62015-11-25 11:18:56 -0800391 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
392 mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
393 historyButtonRect.right, historyButtonRect.bottom);
Winsonc29ff002015-11-20 16:00:45 -0800394
Winsonb1e71d02015-11-23 12:40:23 -0800395 if (mAwaitingFirstLayout) {
396 mAwaitingFirstLayout = false;
397
398 // If launched via dragging from the nav bar, then we should translate the whole view
399 // down offscreen
400 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
401 if (launchState.launchedViaDragGesture) {
402 setTranslationY(getMeasuredHeight());
403 }
404 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800405 }
406
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700407 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700408 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700409 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700410 requestLayout();
Winsonc29ff002015-11-20 16:00:45 -0800411 return insets;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700412 }
413
Winsonbe7607a2015-10-01 17:24:51 -0700414 @Override
415 public boolean onInterceptTouchEvent(MotionEvent ev) {
416 return mTouchHandler.onInterceptTouchEvent(ev);
417 }
418
419 @Override
420 public boolean onTouchEvent(MotionEvent ev) {
421 return mTouchHandler.onTouchEvent(ev);
422 }
423
424 @Override
425 protected void dispatchDraw(Canvas canvas) {
426 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700427 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
428 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
429 if (d.getAlpha() > 0) {
430 d.draw(canvas);
431 }
Winsonbe7607a2015-10-01 17:24:51 -0700432 }
433 }
434
Winson4165d3362015-10-10 14:40:35 -0700435 @Override
436 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700437 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
438 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
439 if (d == who) {
440 return true;
441 }
442 }
443 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700444 }
445
Jorim Jaggi900fb482015-06-02 15:07:33 -0700446 public void disableLayersForOneFrame() {
Winson147ecaf2015-09-16 16:49:55 -0700447 if (mTaskStackView != null) {
448 mTaskStackView.disableLayersForOneFrame();
Jorim Jaggi900fb482015-06-02 15:07:33 -0700449 }
450 }
451
Winson Chung47c4c692014-03-17 10:17:11 -0700452 /**** TaskStackView.TaskStackCallbacks Implementation ****/
Winson Chung303e1ff2014-03-07 15:06:19 -0800453
454 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700455 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700456 final TaskStack stack, final Task task, final boolean lockToTask,
Winson83c1b072015-11-09 10:48:04 -0800457 final Rect bounds, int destinationStack) {
Winson387aac62015-11-25 11:18:56 -0800458 mLastTaskLaunchedWasFreeform = task.isFreeformTask();
Winson83c1b072015-11-09 10:48:04 -0800459 mTransitionHelper.launchTaskFromRecents(stack, task, stackView, tv, lockToTask, bounds,
460 destinationStack);
Winson Chung303e1ff2014-03-07 15:06:19 -0800461 }
Winson Chung9f9679d2014-04-11 16:49:09 -0700462
Winsonbe7607a2015-10-01 17:24:51 -0700463 /**** EventBus Events ****/
464
465 public final void onBusEvent(DragStartEvent event) {
Winson882072b2015-10-12 11:26:33 -0700466 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
467 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700468 }
469
Winsoneca4ab62015-11-04 10:50:28 -0800470 public final void onBusEvent(DragDropTargetChangedEvent event) {
471 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700472 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
473 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
474 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800475 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
476 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700477 }
Winsonbe7607a2015-10-01 17:24:51 -0700478 }
479
480 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800481 // Animate the overlay alpha back to 0
482 updateVisibleDockRegions(null, -1);
483
Winson479f7442015-11-25 15:16:27 -0800484 // Handle the case where we drop onto a dock region
485 if (event.dropTarget instanceof TaskStack.DockState) {
Winsoneca4ab62015-11-04 10:50:28 -0800486 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
487
Winsona0a8ae42015-12-01 10:43:02 -0800488 // Remove the task after it is docked
Winsona0a8ae42015-12-01 10:43:02 -0800489 event.taskView.animate()
490 .alpha(0f)
491 .setDuration(150)
492 .setInterpolator(mFastOutLinearInInterpolator)
493 .setUpdateListener(null)
494 .setListener(null)
495 .withEndAction(new Runnable() {
496 @Override
497 public void run() {
498 mTaskStackView.getStack().removeTask(event.task);
499 }
500 })
501 .withLayer()
502 .start();
503
Winsoneca4ab62015-11-04 10:50:28 -0800504 // Dock the task and launch it
505 SystemServicesProxy ssp = Recents.getSystemServices();
506 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
507 launchTask(event.task, null, INVALID_STACK_ID);
Winsonbe7607a2015-10-01 17:24:51 -0700508 }
509 }
Winson4165d3362015-10-10 14:40:35 -0700510
Jorim Jaggidd98d412015-11-18 15:57:38 -0800511 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800512 if (mTaskStackView.getTaskViews().size() > 0) {
513 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
514 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800515 }
516
517 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
518 animate().translationY(0f);
519 }
520
Winsonc29ff002015-11-20 16:00:45 -0800521 public final void onBusEvent(ShowHistoryEvent event) {
522 // Hide the history button when the history view is shown
523 hideHistoryButton(mHistoryTransitionDuration);
524 }
525
526 public final void onBusEvent(HideHistoryEvent event) {
527 // Show the history button when the history view is hidden
528 showHistoryButton(mHistoryTransitionDuration);
529 }
530
531 public final void onBusEvent(ShowHistoryButtonEvent event) {
532 showHistoryButton(150);
533 }
534
535 public final void onBusEvent(HideHistoryButtonEvent event) {
536 hideHistoryButton(100);
537 }
538
539 public final void onBusEvent(DebugFlagsChangedEvent event) {
540 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
541 if (!debugFlags.isHistoryEnabled()) {
542 hideHistoryButton(100);
543 }
544 }
545
546 /**
547 * Shows the history button.
548 */
549 private void showHistoryButton(int duration) {
550 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
551 if (!debugFlags.isHistoryEnabled()) {
552 return;
553 }
554
555 mHistoryButton.setVisibility(View.VISIBLE);
556 mHistoryButton.animate()
557 .alpha(1f)
558 .setDuration(duration)
559 .setInterpolator(mFastOutSlowInInterpolator)
560 .withLayer()
561 .start();
562 }
563
564 /**
565 * Hides the history button.
566 */
567 private void hideHistoryButton(int duration) {
568 mHistoryButton.animate()
569 .alpha(0f)
570 .setDuration(duration)
571 .setInterpolator(mFastOutLinearInInterpolator)
572 .withEndAction(new Runnable() {
573 @Override
574 public void run() {
575 mHistoryButton.setVisibility(View.INVISIBLE);
576 }
577 })
578 .withLayer()
579 .start();
580 }
581
Winson4165d3362015-10-10 14:40:35 -0700582 /**
583 * Updates the dock region to match the specified dock state.
584 */
Winson882072b2015-10-12 11:26:33 -0700585 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
586 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
587 if (newDockStates != null) {
588 for (TaskStack.DockState dockState : newDockStates) {
589 newDockStatesSet.add(dockState);
590 }
Winson4165d3362015-10-10 14:40:35 -0700591 }
Winson882072b2015-10-12 11:26:33 -0700592 for (TaskStack.DockState dockState : mVisibleDockStates) {
593 TaskStack.DockState.ViewState viewState = dockState.viewState;
594 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
595 // This is no longer visible, so hide it
596 viewState.startAlphaAnimation(0, 150);
597 } else {
598 // This state is now visible, update the bounds and show it
599 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
600 viewState.dockAreaOverlay.setBounds(
601 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
602 viewState.dockAreaOverlay.setCallback(this);
603 viewState.startAlphaAnimation(alpha, 150);
604 }
Winson4165d3362015-10-10 14:40:35 -0700605 }
Winson4165d3362015-10-10 14:40:35 -0700606 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800607
Winsonb1e71d02015-11-23 12:40:23 -0800608 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
609 if (!event.visible) {
610 // Reset the view state
611 mAwaitingFirstLayout = true;
612 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800613 }
614 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800615}