blob: addcc85d7f154eebd37b5329997f59e5cedecaa2 [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;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -080030import android.view.ViewPropertyAnimator;
Winson Chung653f70c22014-05-19 14:49:42 -070031import android.view.WindowInsets;
Winson35f30502015-09-28 11:24:36 -070032import android.view.animation.AnimationUtils;
33import android.view.animation.Interpolator;
Winson Chung303e1ff2014-03-07 15:06:19 -080034import android.widget.FrameLayout;
Jorim Jaggi92de6b62015-12-09 15:32:30 -080035import com.android.internal.logging.MetricsLogger;
Winson Chungde750de2015-12-11 10:26:06 -050036import android.widget.TextView;
Winson Chungd16c5652015-01-26 16:11:07 -080037import com.android.systemui.R;
Winsone7f138c2015-10-22 16:15:21 -070038import com.android.systemui.recents.Recents;
Winsonbe7607a2015-10-01 17:24:51 -070039import com.android.systemui.recents.RecentsActivity;
Winsonb1e71d02015-11-23 12:40:23 -080040import com.android.systemui.recents.RecentsActivityLaunchState;
Winson Chungaee097c2015-04-02 18:16:02 -070041import com.android.systemui.recents.RecentsAppWidgetHostView;
Winson Chung303e1ff2014-03-07 15:06:19 -080042import com.android.systemui.recents.RecentsConfiguration;
Winsonc29ff002015-11-20 16:00:45 -080043import com.android.systemui.recents.RecentsDebugFlags;
Winson2536c7e2015-10-01 15:49:31 -070044import com.android.systemui.recents.events.EventBus;
Winson13d30662015-11-06 15:30:29 -080045import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
Winsonc29ff002015-11-20 16:00:45 -080046import com.android.systemui.recents.events.activity.DebugFlagsChangedEvent;
Winson412e1802015-10-20 16:57:57 -070047import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
Winsonc29ff002015-11-20 16:00:45 -080048import com.android.systemui.recents.events.activity.HideHistoryButtonEvent;
49import com.android.systemui.recents.events.activity.HideHistoryEvent;
50import com.android.systemui.recents.events.activity.ShowHistoryButtonEvent;
51import com.android.systemui.recents.events.activity.ShowHistoryEvent;
Winsonb1e71d02015-11-23 12:40:23 -080052import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
Jorim Jaggidd98d412015-11-18 15:57:38 -080053import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
54import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
Winsoneca4ab62015-11-04 10:50:28 -080055import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
Winsonbe7607a2015-10-01 17:24:51 -070056import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
57import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
Winson Chung9a742902015-12-11 10:25:40 -050058import com.android.systemui.recents.misc.ReferenceCountedTrigger;
Winson Chung1f24c7e2014-07-11 17:06:48 -070059import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chung303e1ff2014-03-07 15:06:19 -080060import com.android.systemui.recents.model.Task;
61import com.android.systemui.recents.model.TaskStack;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -080062import com.android.systemui.stackdivider.WindowManagerProxy;
63import com.android.systemui.statusbar.FlingAnimationUtils;
Winson Chung303e1ff2014-03-07 15:06:19 -080064
65import java.util.ArrayList;
Winson Chung6ac8bd62015-01-07 16:38:35 -080066import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080067
Wale Ogunwale3797c222015-10-27 14:21:58 -070068import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070069
Winson Chung303e1ff2014-03-07 15:06:19 -080070/**
71 * This view is the the top level layout that contains TaskStacks (which are laid out according
72 * to their SpaceNode bounds.
73 */
Winsone6c90732015-09-24 16:06:29 -070074public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks {
Winson Chung47c4c692014-03-17 10:17:11 -070075
Jorim Jaggi6e18e002015-06-02 17:07:39 -070076 private static final String TAG = "RecentsView";
Winson0d14d4d2015-10-26 17:05:04 -070077 private static final boolean DEBUG = false;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070078
Winson Chung9a742902015-12-11 10:25:40 -050079 private final Handler mHandler;
Winson Chungd42a6cf2014-06-03 16:24:04 -070080
Winson Chung9a742902015-12-11 10:25:40 -050081 private TaskStack mStack;
82 private TaskStackView mTaskStackView;
83 private RecentsAppWidgetHostView mSearchBar;
Winson Chungde750de2015-12-11 10:26:06 -050084 private TextView mHistoryButton;
Winson Chung9a742902015-12-11 10:25:40 -050085 private View mEmptyView;
86 private boolean mAwaitingFirstLayout = true;
87 private boolean mLastTaskLaunchedWasFreeform;
88 private Rect mSystemInsets = new Rect();
Winsonbe7607a2015-10-01 17:24:51 -070089
Winson Chung9a742902015-12-11 10:25:40 -050090 private RecentsTransitionHelper mTransitionHelper;
91 private RecentsViewTouchHandler mTouchHandler;
92 private TaskStack.DockState[] mVisibleDockStates = {
Winson882072b2015-10-12 11:26:33 -070093 TaskStack.DockState.LEFT,
94 TaskStack.DockState.TOP,
95 TaskStack.DockState.RIGHT,
96 TaskStack.DockState.BOTTOM,
97 };
Winsonbe7607a2015-10-01 17:24:51 -070098
Winson Chung9a742902015-12-11 10:25:40 -050099 private final Interpolator mFastOutSlowInInterpolator;
100 private final Interpolator mFastOutLinearInInterpolator;
101 private final FlingAnimationUtils mFlingAnimationUtils;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800102
Winson Chung303e1ff2014-03-07 15:06:19 -0800103 public RecentsView(Context context) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800104 this(context, null);
Winson Chung8e548f72014-06-24 14:40:53 -0700105 }
106
107 public RecentsView(Context context, AttributeSet attrs) {
108 this(context, attrs, 0);
109 }
110
111 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
112 this(context, attrs, defStyleAttr, 0);
113 }
114
115 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
116 super(context, attrs, defStyleAttr, defStyleRes);
Winsonc29ff002015-11-20 16:00:45 -0800117 Resources res = context.getResources();
Winsonbe7607a2015-10-01 17:24:51 -0700118 setWillNotDraw(false);
Winson83c1b072015-11-09 10:48:04 -0800119 mHandler = new Handler();
120 mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
Winson35f30502015-09-28 11:24:36 -0700121 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
122 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonc29ff002015-11-20 16:00:45 -0800123 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
124 com.android.internal.R.interpolator.fast_out_linear_in);
Winsonbe7607a2015-10-01 17:24:51 -0700125 mTouchHandler = new RecentsViewTouchHandler(this);
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800126 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.3f);
Winsonc29ff002015-11-20 16:00:45 -0800127
128 LayoutInflater inflater = LayoutInflater.from(context);
Winson Chungde750de2015-12-11 10:26:06 -0500129 mHistoryButton = (TextView) inflater.inflate(R.layout.recents_history_button, this, false);
Winsonc29ff002015-11-20 16:00:45 -0800130 mHistoryButton.setOnClickListener(new View.OnClickListener() {
131 @Override
132 public void onClick(View v) {
Winson Chung9a742902015-12-11 10:25:40 -0500133 ReferenceCountedTrigger postHideStackAnimationTrigger = new ReferenceCountedTrigger(v.getContext());
134 postHideStackAnimationTrigger.increment();
135 EventBus.getDefault().send(new ShowHistoryEvent(postHideStackAnimationTrigger));
136 postHideStackAnimationTrigger.decrement();
Winsonc29ff002015-11-20 16:00:45 -0800137 }
138 });
Winson Chung9a742902015-12-11 10:25:40 -0500139 addView(mHistoryButton);
140 mEmptyView = inflater.inflate(R.layout.recents_empty, this, false);
141 addView(mEmptyView);
Winson Chungd16c5652015-01-26 16:11:07 -0800142 }
143
Winson Chungdcfa7972014-07-22 12:27:13 -0700144 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700145 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700146 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung9a742902015-12-11 10:25:40 -0500147 RecentsActivityLaunchState launchState = config.getLaunchState();
Winsonc29ff002015-11-20 16:00:45 -0800148 mStack = stack;
Winson Chungfca2a632015-12-11 15:51:18 -0500149 // Disable reusing task stack views until the visibility bug is fixed. b/25998134
Winson Chung9a742902015-12-11 10:25:40 -0500150 if (false && launchState.launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700151 if (mTaskStackView != null) {
152 // If onRecentsHidden is not triggered, we need to the stack view again here
153 mTaskStackView.reset();
154 mTaskStackView.setStack(stack);
Winson160f4c02015-12-02 19:06:44 -0800155 removeView(mTaskStackView);
156 addView(mTaskStackView);
Winson147ecaf2015-09-16 16:49:55 -0700157 } else {
158 mTaskStackView = new TaskStackView(getContext(), stack);
159 mTaskStackView.setCallbacks(this);
160 addView(mTaskStackView);
161 }
162 } else {
163 if (mTaskStackView != null) {
164 removeView(mTaskStackView);
165 }
166 mTaskStackView = new TaskStackView(getContext(), stack);
167 mTaskStackView.setCallbacks(this);
168 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800169 }
Winson Chung9a742902015-12-11 10:25:40 -0500170
171 // Update the top level view's visibilities
172 if (stack.getStackTaskCount() > 0) {
173 hideEmptyView();
Winsonc29ff002015-11-20 16:00:45 -0800174 } else {
Winson Chung9a742902015-12-11 10:25:40 -0500175 showEmptyView();
Winsonc29ff002015-11-20 16:00:45 -0800176 }
Winson Chung02d49272014-08-29 13:57:29 -0700177
Winson Chungb0a28ea2014-10-28 15:21:35 -0700178 // Trigger a new layout
179 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700180 }
181
Winsonb1e71d02015-11-23 12:40:23 -0800182 /**
183 * Returns whether the last task launched was in the freeform stack or not.
184 */
185 public boolean isLastTaskLaunchedFreeform() {
186 return mLastTaskLaunchedWasFreeform;
187 }
188
Winsonc29ff002015-11-20 16:00:45 -0800189 /**
190 * Returns the currently set task stack.
191 */
192 public TaskStack getTaskStack() {
193 return mStack;
194 }
195
Skuhne8aa7d162015-03-20 13:40:53 -0700196 /** Gets the next task in the stack - or if the last - the top task */
197 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700198 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700199 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700200 if (mTaskStackView != null) {
201 TaskStack stack = mTaskStackView.getStack();
Winson250608a2015-11-24 15:00:31 -0800202 ArrayList<Task> taskList = stack.getStackTasks();
Skuhne8aa7d162015-03-20 13:40:53 -0700203 // Iterate the stack views and try and find the focused task
204 for (int j = taskList.size() - 1; j >= 0; --j) {
205 Task task = taskList.get(j);
206 // Return the next task in the line.
207 if (found)
208 return task;
209 // Remember the first possible task as the top task.
210 if (returnTask == null)
211 returnTask = task;
212 if (task == taskToSearch)
213 found = true;
214 }
215 }
216 return returnTask;
217 }
218
Winson Chung1e8d71b2014-05-16 17:05:22 -0700219 /** Launches the focused task from the first stack if possible */
220 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700221 if (mTaskStackView != null) {
222 TaskStack stack = mTaskStackView.getStack();
Winson142af422015-11-09 10:39:57 -0800223 Task task = mTaskStackView.getFocusedTask();
224 if (task != null) {
225 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson83c1b072015-11-09 10:48:04 -0800226 onTaskViewClicked(mTaskStackView, taskView, stack, task, false, null,
Winson142af422015-11-09 10:39:57 -0800227 INVALID_STACK_ID);
228 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700229 }
230 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700231 return false;
232 }
233
Winsona0731a12015-12-02 15:10:14 -0800234 /** Launches the task that recents was launched from if possible */
235 public boolean launchPreviousTask() {
236 if (mTaskStackView != null) {
237 TaskStack stack = mTaskStackView.getStack();
238 Task task = stack.getLaunchTarget();
239 if (task != null) {
240 TaskView taskView = mTaskStackView.getChildViewForTask(task);
241 onTaskViewClicked(mTaskStackView, taskView, stack, task, false, null,
242 INVALID_STACK_ID);
243 return true;
244 }
245 }
246 return false;
247 }
248
Skuhne8aa7d162015-03-20 13:40:53 -0700249 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700250 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700251 if (mTaskStackView != null) {
252 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700253 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700254 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700255 int taskViewCount = taskViews.size();
256 for (int j = 0; j < taskViewCount; j++) {
257 TaskView tv = taskViews.get(j);
258 if (tv.getTask() == task) {
Winson83c1b072015-11-09 10:48:04 -0800259 onTaskViewClicked(mTaskStackView, tv, stack, task, false,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700260 taskBounds, destinationStack);
Skuhne8aa7d162015-03-20 13:40:53 -0700261 return true;
262 }
263 }
264 }
265 return false;
266 }
267
Winson Chung24cf1522014-05-29 12:03:33 -0700268 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700269 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800270 // We have to increment/decrement the post animation trigger in case there are no children
271 // to ensure that it runs
272 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700273 if (mTaskStackView != null) {
274 mTaskStackView.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700275 }
Winson Chung740c3ac2014-11-12 16:14:38 -0800276 ctx.postAnimationTrigger.decrement();
Winson Chung24cf1522014-05-29 12:03:33 -0700277 }
278
Winson Chungd42a6cf2014-06-03 16:24:04 -0700279 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700280 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chunga91c2932014-11-07 15:02:38 -0800281 // We have to increment/decrement the post animation trigger in case there are no children
282 // to ensure that it runs
283 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700284 if (mTaskStackView != null) {
285 mTaskStackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700286 }
Winson Chunga91c2932014-11-07 15:02:38 -0800287 ctx.postAnimationTrigger.decrement();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700288
Winsonc29ff002015-11-20 16:00:45 -0800289 // Hide the history button
290 int taskViewExitToHomeDuration = getResources().getInteger(
291 R.integer.recents_task_exit_to_home_duration);
292 hideHistoryButton(taskViewExitToHomeDuration);
293
Winson13d30662015-11-06 15:30:29 -0800294 // If we are going home, cancel the previous task's window transition
295 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
296
Winsonc29ff002015-11-20 16:00:45 -0800297 // Notify sof the exit animation
Winson412e1802015-10-20 16:57:57 -0700298 EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
Winson Chungd42a6cf2014-06-03 16:24:04 -0700299 }
300
Winson Chungf7bca432014-04-30 17:11:13 -0700301 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700302 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700303 // Remove the previous search bar if one exists
304 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
305 removeView(mSearchBar);
306 }
307 // Add the new search bar
308 if (searchBar != null) {
309 mSearchBar = searchBar;
310 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700311 }
Winson Chungecd9b302014-04-16 17:07:18 -0700312 }
313
Winson Chung772b6b12014-07-03 15:54:02 -0700314 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700315 public boolean hasValidSearchBar() {
316 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700317 }
318
Winson Chung9a742902015-12-11 10:25:40 -0500319 /**
320 * Hides the task stack and shows the empty view.
321 */
322 public void showEmptyView() {
323 if (!RecentsDebugFlags.Static.DisableSearchBar && (mSearchBar != null)) {
324 mSearchBar.setVisibility(View.INVISIBLE);
325 }
326 mTaskStackView.setVisibility(View.INVISIBLE);
327 mEmptyView.setVisibility(View.VISIBLE);
328 mEmptyView.bringToFront();
329 mHistoryButton.bringToFront();
330 }
331
332 /**
333 * Shows the task stack and hides the empty view.
334 */
335 public void hideEmptyView() {
336 mEmptyView.setVisibility(View.INVISIBLE);
337 mTaskStackView.setVisibility(View.VISIBLE);
338 if (!RecentsDebugFlags.Static.DisableSearchBar && (mSearchBar != null)) {
339 mSearchBar.setVisibility(View.VISIBLE);
340 }
341 mTaskStackView.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700342 if (mSearchBar != null) {
Winson Chung012ef362014-07-31 18:36:25 -0700343 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700344 }
Winson Chung9a742902015-12-11 10:25:40 -0500345 mHistoryButton.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700346 }
347
Winsonc29ff002015-11-20 16:00:45 -0800348 /**
349 * Returns the last known system insets.
350 */
351 public Rect getSystemInsets() {
352 return mSystemInsets;
353 }
354
Winsonbe7607a2015-10-01 17:24:51 -0700355 @Override
356 protected void onAttachedToWindow() {
357 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
358 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
359 super.onAttachedToWindow();
360 }
361
362 @Override
363 protected void onDetachedFromWindow() {
364 super.onDetachedFromWindow();
365 EventBus.getDefault().unregister(this);
366 EventBus.getDefault().unregister(mTouchHandler);
367 }
368
Winson Chungf7bca432014-04-30 17:11:13 -0700369 /**
370 * This is called with the full size of the window since we are handling our own insets.
371 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800372 @Override
373 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700374 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800375 int width = MeasureSpec.getSize(widthMeasureSpec);
376 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700377
Winson Chungf7bca432014-04-30 17:11:13 -0700378 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700379 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700380 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700381 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700382 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700383 mSearchBar.measure(
384 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
385 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700386 }
387
Winson Chungf7bca432014-04-30 17:11:13 -0700388 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700389 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700390 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700391 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700392 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700393 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800394 }
395
Winson Chung9a742902015-12-11 10:25:40 -0500396 // Measure the empty view
397 measureChild(mEmptyView, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
398 MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
399
Winson250608a2015-11-24 15:00:31 -0800400 // Measure the history button with the full space above the stack, but width-constrained
401 // to the stack
Winson387aac62015-11-25 11:18:56 -0800402 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
Winsonc29ff002015-11-20 16:00:45 -0800403 measureChild(mHistoryButton,
Winson387aac62015-11-25 11:18:56 -0800404 MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
405 MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
Winson250608a2015-11-24 15:00:31 -0800406 MeasureSpec.EXACTLY));
Winson Chung9a742902015-12-11 10:25:40 -0500407
Winson Chung303e1ff2014-03-07 15:06:19 -0800408 setMeasuredDimension(width, height);
409 }
410
Winson Chungf7bca432014-04-30 17:11:13 -0700411 /**
412 * This is called with the full size of the window since we are handling our own insets.
413 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800414 @Override
415 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700416 RecentsConfiguration config = Recents.getConfiguration();
417
Winson Chungf7bca432014-04-30 17:11:13 -0700418 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700419 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
420 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700421 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700422 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700423 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700424 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
425 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700426 }
427
Winson147ecaf2015-09-16 16:49:55 -0700428 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
429 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800430 }
Winsonbe7607a2015-10-01 17:24:51 -0700431
Winson Chung9a742902015-12-11 10:25:40 -0500432 // Layout the empty view
433 mEmptyView.layout(left, top, right, bottom);
434
Winson250608a2015-11-24 15:00:31 -0800435 // Layout the history button left-aligned with the stack, but offset from the top of the
436 // view
Winson387aac62015-11-25 11:18:56 -0800437 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
438 mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
439 historyButtonRect.right, historyButtonRect.bottom);
Winsonc29ff002015-11-20 16:00:45 -0800440
Winsonb1e71d02015-11-23 12:40:23 -0800441 if (mAwaitingFirstLayout) {
442 mAwaitingFirstLayout = false;
443
444 // If launched via dragging from the nav bar, then we should translate the whole view
445 // down offscreen
446 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
447 if (launchState.launchedViaDragGesture) {
448 setTranslationY(getMeasuredHeight());
Jorim Jaggi0ca88262015-12-07 17:15:16 -0800449 } else {
450 setTranslationY(0f);
Winsonb1e71d02015-11-23 12:40:23 -0800451 }
452 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800453 }
454
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700455 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700456 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700457 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700458 requestLayout();
Winsonc29ff002015-11-20 16:00:45 -0800459 return insets;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700460 }
461
Winsonbe7607a2015-10-01 17:24:51 -0700462 @Override
463 public boolean onInterceptTouchEvent(MotionEvent ev) {
464 return mTouchHandler.onInterceptTouchEvent(ev);
465 }
466
467 @Override
468 public boolean onTouchEvent(MotionEvent ev) {
469 return mTouchHandler.onTouchEvent(ev);
470 }
471
472 @Override
473 protected void dispatchDraw(Canvas canvas) {
474 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700475 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
476 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
477 if (d.getAlpha() > 0) {
478 d.draw(canvas);
479 }
Winsonbe7607a2015-10-01 17:24:51 -0700480 }
481 }
482
Winson4165d3362015-10-10 14:40:35 -0700483 @Override
484 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700485 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
486 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
487 if (d == who) {
488 return true;
489 }
490 }
491 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700492 }
493
Winson Chung47c4c692014-03-17 10:17:11 -0700494 /**** TaskStackView.TaskStackCallbacks Implementation ****/
Winson Chung303e1ff2014-03-07 15:06:19 -0800495
496 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700497 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700498 final TaskStack stack, final Task task, final boolean lockToTask,
Winson83c1b072015-11-09 10:48:04 -0800499 final Rect bounds, int destinationStack) {
Winson387aac62015-11-25 11:18:56 -0800500 mLastTaskLaunchedWasFreeform = task.isFreeformTask();
Winson83c1b072015-11-09 10:48:04 -0800501 mTransitionHelper.launchTaskFromRecents(stack, task, stackView, tv, lockToTask, bounds,
502 destinationStack);
Winson Chung303e1ff2014-03-07 15:06:19 -0800503 }
Winson Chung9f9679d2014-04-11 16:49:09 -0700504
Winsonbe7607a2015-10-01 17:24:51 -0700505 /**** EventBus Events ****/
506
507 public final void onBusEvent(DragStartEvent event) {
Winson882072b2015-10-12 11:26:33 -0700508 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
509 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700510 }
511
Winsoneca4ab62015-11-04 10:50:28 -0800512 public final void onBusEvent(DragDropTargetChangedEvent event) {
513 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700514 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
515 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
516 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800517 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
518 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700519 }
Winsonbe7607a2015-10-01 17:24:51 -0700520 }
521
522 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800523 // Animate the overlay alpha back to 0
524 updateVisibleDockRegions(null, -1);
525
Winson479f7442015-11-25 15:16:27 -0800526 // Handle the case where we drop onto a dock region
527 if (event.dropTarget instanceof TaskStack.DockState) {
Winsoneca4ab62015-11-04 10:50:28 -0800528 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
529
Winsona0a8ae42015-12-01 10:43:02 -0800530 // Remove the task after it is docked
Winsona0a8ae42015-12-01 10:43:02 -0800531 event.taskView.animate()
532 .alpha(0f)
533 .setDuration(150)
534 .setInterpolator(mFastOutLinearInInterpolator)
535 .setUpdateListener(null)
536 .setListener(null)
537 .withEndAction(new Runnable() {
538 @Override
539 public void run() {
540 mTaskStackView.getStack().removeTask(event.task);
541 }
542 })
543 .withLayer()
544 .start();
545
Winsoneca4ab62015-11-04 10:50:28 -0800546 // Dock the task and launch it
547 SystemServicesProxy ssp = Recents.getSystemServices();
548 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
549 launchTask(event.task, null, INVALID_STACK_ID);
Jorim Jaggi92de6b62015-12-09 15:32:30 -0800550
551 MetricsLogger.action(mContext,
552 MetricsLogger.ACTION_WINDOW_DOCK_DRAG_DROP);
Winsonbe7607a2015-10-01 17:24:51 -0700553 }
554 }
Winson4165d3362015-10-10 14:40:35 -0700555
Jorim Jaggidd98d412015-11-18 15:57:38 -0800556 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800557 if (mTaskStackView.getTaskViews().size() > 0) {
558 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
559 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800560 }
561
562 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800563 ViewPropertyAnimator animator = animate();
Jorim Jaggi8fccfe22015-12-03 16:58:08 -0800564 if (event.velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800565 animator.translationY(getHeight());
566 animator.withEndAction(new Runnable() {
567 @Override
568 public void run() {
569 WindowManagerProxy.getInstance().maximizeDockedStack();
570 }
571 });
572 mFlingAnimationUtils.apply(animator, getTranslationY(), getHeight(), event.velocity);
573 } else {
574 animator.translationY(0f);
575 animator.setListener(null);
576 mFlingAnimationUtils.apply(animator, getTranslationY(), 0, event.velocity);
577 }
578 animator.start();
Jorim Jaggidd98d412015-11-18 15:57:38 -0800579 }
580
Winsonc29ff002015-11-20 16:00:45 -0800581 public final void onBusEvent(ShowHistoryEvent event) {
582 // Hide the history button when the history view is shown
Winson Chung9a742902015-12-11 10:25:40 -0500583 hideHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
584 event.postHideStackAnimationTrigger);
585 event.postHideStackAnimationTrigger.addLastDecrementRunnable(new Runnable() {
586 @Override
587 public void run() {
588 setAlpha(0f);
589 }
590 });
Winsonc29ff002015-11-20 16:00:45 -0800591 }
592
593 public final void onBusEvent(HideHistoryEvent event) {
594 // Show the history button when the history view is hidden
Winson Chung9a742902015-12-11 10:25:40 -0500595 setAlpha(1f);
596 showHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
597 event.postHideHistoryAnimationTrigger);
Winsonc29ff002015-11-20 16:00:45 -0800598 }
599
600 public final void onBusEvent(ShowHistoryButtonEvent event) {
601 showHistoryButton(150);
602 }
603
604 public final void onBusEvent(HideHistoryButtonEvent event) {
605 hideHistoryButton(100);
606 }
607
608 public final void onBusEvent(DebugFlagsChangedEvent event) {
609 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
610 if (!debugFlags.isHistoryEnabled()) {
611 hideHistoryButton(100);
Winson Chungde750de2015-12-11 10:26:06 -0500612 } else {
613 showHistoryButton(100);
Winsonc29ff002015-11-20 16:00:45 -0800614 }
615 }
616
617 /**
618 * Shows the history button.
619 */
Winson Chung9a742902015-12-11 10:25:40 -0500620 private void showHistoryButton(final int duration) {
621 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger(getContext());
622 postAnimationTrigger.increment();
623 showHistoryButton(duration, postAnimationTrigger);
624 postAnimationTrigger.decrement();
625 }
626
627 private void showHistoryButton(final int duration,
628 final ReferenceCountedTrigger postHideHistoryAnimationTrigger) {
Winsonc29ff002015-11-20 16:00:45 -0800629 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
630 if (!debugFlags.isHistoryEnabled()) {
631 return;
632 }
633
634 mHistoryButton.setVisibility(View.VISIBLE);
Winson Chung9a742902015-12-11 10:25:40 -0500635 mHistoryButton.setAlpha(0f);
Winson Chungde750de2015-12-11 10:26:06 -0500636 mHistoryButton.setText(getContext().getString(R.string.recents_history_label_format,
637 mStack.getHistoricalTasks().size()));
Winson Chung9a742902015-12-11 10:25:40 -0500638 postHideHistoryAnimationTrigger.addLastDecrementRunnable(new Runnable() {
639 @Override
640 public void run() {
641 mHistoryButton.animate()
642 .alpha(1f)
643 .setDuration(duration)
644 .setInterpolator(mFastOutSlowInInterpolator)
645 .withLayer()
646 .start();
647 }
648 });
Winsonc29ff002015-11-20 16:00:45 -0800649 }
650
651 /**
652 * Hides the history button.
653 */
654 private void hideHistoryButton(int duration) {
Winson Chung9a742902015-12-11 10:25:40 -0500655 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger(getContext());
656 postAnimationTrigger.increment();
657 hideHistoryButton(duration, postAnimationTrigger);
658 postAnimationTrigger.decrement();
659 }
660
661 private void hideHistoryButton(int duration,
662 final ReferenceCountedTrigger postHideStackAnimationTrigger) {
Winsonc29ff002015-11-20 16:00:45 -0800663 mHistoryButton.animate()
664 .alpha(0f)
665 .setDuration(duration)
666 .setInterpolator(mFastOutLinearInInterpolator)
667 .withEndAction(new Runnable() {
668 @Override
669 public void run() {
670 mHistoryButton.setVisibility(View.INVISIBLE);
Winson Chung9a742902015-12-11 10:25:40 -0500671 postHideStackAnimationTrigger.decrement();
Winsonc29ff002015-11-20 16:00:45 -0800672 }
673 })
674 .withLayer()
675 .start();
Winson Chung9a742902015-12-11 10:25:40 -0500676 postHideStackAnimationTrigger.increment();
Winsonc29ff002015-11-20 16:00:45 -0800677 }
678
Winson4165d3362015-10-10 14:40:35 -0700679 /**
680 * Updates the dock region to match the specified dock state.
681 */
Winson882072b2015-10-12 11:26:33 -0700682 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
683 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
684 if (newDockStates != null) {
685 for (TaskStack.DockState dockState : newDockStates) {
686 newDockStatesSet.add(dockState);
687 }
Winson4165d3362015-10-10 14:40:35 -0700688 }
Winson882072b2015-10-12 11:26:33 -0700689 for (TaskStack.DockState dockState : mVisibleDockStates) {
690 TaskStack.DockState.ViewState viewState = dockState.viewState;
691 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
692 // This is no longer visible, so hide it
693 viewState.startAlphaAnimation(0, 150);
694 } else {
695 // This state is now visible, update the bounds and show it
696 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
697 viewState.dockAreaOverlay.setBounds(
698 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
699 viewState.dockAreaOverlay.setCallback(this);
700 viewState.startAlphaAnimation(alpha, 150);
701 }
Winson4165d3362015-10-10 14:40:35 -0700702 }
Winson4165d3362015-10-10 14:40:35 -0700703 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800704
Winsonb1e71d02015-11-23 12:40:23 -0800705 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
706 if (!event.visible) {
707 // Reset the view state
708 mAwaitingFirstLayout = true;
709 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800710 }
711 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800712}