blob: e727652f5582a01adfaca46aeaa767eafc249d11 [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
Winsonf24f2162016-01-05 12:11:55 -080019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Winson Chung303e1ff2014-03-07 15:06:19 -080021import android.content.Context;
Winson Chung303e1ff2014-03-07 15:06:19 -080022import android.graphics.Canvas;
23import android.graphics.Rect;
Winson4165d3362015-10-10 14:40:35 -070024import android.graphics.drawable.Drawable;
Winson83c1b072015-11-09 10:48:04 -080025import android.os.Handler;
Winson882072b2015-10-12 11:26:33 -070026import android.util.ArraySet;
Winson Chung8e548f72014-06-24 14:40:53 -070027import android.util.AttributeSet;
Winson Chungecd9b302014-04-16 17:07:18 -070028import android.view.LayoutInflater;
Winsonbe7607a2015-10-01 17:24:51 -070029import android.view.MotionEvent;
Winsonc29ff002015-11-20 16:00:45 -080030import android.view.View;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -080031import android.view.ViewPropertyAnimator;
Winson Chung653f70c22014-05-19 14:49:42 -070032import android.view.WindowInsets;
Winson35f30502015-09-28 11:24:36 -070033import android.view.animation.AnimationUtils;
34import android.view.animation.Interpolator;
Winson Chung303e1ff2014-03-07 15:06:19 -080035import android.widget.FrameLayout;
Winson Chungde750de2015-12-11 10:26:06 -050036import android.widget.TextView;
Winsondc8de842016-01-06 15:21:41 -080037import com.android.internal.logging.MetricsLogger;
Winson Chungd16c5652015-01-26 16:11:07 -080038import com.android.systemui.R;
Winsone7f138c2015-10-22 16:15:21 -070039import com.android.systemui.recents.Recents;
Winsonbe7607a2015-10-01 17:24:51 -070040import com.android.systemui.recents.RecentsActivity;
Winsonb1e71d02015-11-23 12:40:23 -080041import com.android.systemui.recents.RecentsActivityLaunchState;
Winson Chungaee097c2015-04-02 18:16:02 -070042import com.android.systemui.recents.RecentsAppWidgetHostView;
Winson Chung303e1ff2014-03-07 15:06:19 -080043import com.android.systemui.recents.RecentsConfiguration;
Winsonc29ff002015-11-20 16:00:45 -080044import com.android.systemui.recents.RecentsDebugFlags;
Winson2536c7e2015-10-01 15:49:31 -070045import com.android.systemui.recents.events.EventBus;
Winson13d30662015-11-06 15:30:29 -080046import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
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;
Winson Chung48f2cda2015-12-11 13:20:12 -050050import com.android.systemui.recents.events.activity.LaunchTaskEvent;
Winsonc29ff002015-11-20 16:00:45 -080051import com.android.systemui.recents.events.activity.ShowHistoryButtonEvent;
52import com.android.systemui.recents.events.activity.ShowHistoryEvent;
Winson Chung06266772015-12-11 10:24:21 -050053import com.android.systemui.recents.events.activity.TaskStackUpdatedEvent;
Winsonb1e71d02015-11-23 12:40:23 -080054import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
Jorim Jaggidd98d412015-11-18 15:57:38 -080055import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
56import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
Winsoneca4ab62015-11-04 10:50:28 -080057import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
Winsonbe7607a2015-10-01 17:24:51 -070058import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
59import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
Winson Chung9a742902015-12-11 10:25:40 -050060import com.android.systemui.recents.misc.ReferenceCountedTrigger;
Winson Chung1f24c7e2014-07-11 17:06:48 -070061import com.android.systemui.recents.misc.SystemServicesProxy;
Winsonc5fd3502016-01-18 15:18:37 -080062import com.android.systemui.recents.misc.Utilities;
Winson Chung303e1ff2014-03-07 15:06:19 -080063import com.android.systemui.recents.model.Task;
64import com.android.systemui.recents.model.TaskStack;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -080065import com.android.systemui.stackdivider.WindowManagerProxy;
66import com.android.systemui.statusbar.FlingAnimationUtils;
Winson3e874742016-01-07 10:08:17 -080067import com.android.systemui.statusbar.phone.PhoneStatusBar;
Winson Chung303e1ff2014-03-07 15:06:19 -080068
69import java.util.ArrayList;
Winson Chung6ac8bd62015-01-07 16:38:35 -080070import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080071
Wale Ogunwale3797c222015-10-27 14:21:58 -070072import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070073
Winson Chung303e1ff2014-03-07 15:06:19 -080074/**
75 * This view is the the top level layout that contains TaskStacks (which are laid out according
76 * to their SpaceNode bounds.
77 */
Winson Chung48f2cda2015-12-11 13:20:12 -050078public class RecentsView extends FrameLayout {
Winson Chung47c4c692014-03-17 10:17:11 -070079
Winson3e874742016-01-07 10:08:17 -080080 private static final int DOCK_AREA_OVERLAY_TRANSITION_DURATION = 135;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070081
Winson Chung9a742902015-12-11 10:25:40 -050082 private final Handler mHandler;
Winson Chungd42a6cf2014-06-03 16:24:04 -070083
Winson Chung9a742902015-12-11 10:25:40 -050084 private TaskStack mStack;
85 private TaskStackView mTaskStackView;
86 private RecentsAppWidgetHostView mSearchBar;
Winson Chungde750de2015-12-11 10:26:06 -050087 private TextView mHistoryButton;
Winson Chung9a742902015-12-11 10:25:40 -050088 private View mEmptyView;
89 private boolean mAwaitingFirstLayout = true;
90 private boolean mLastTaskLaunchedWasFreeform;
91 private Rect mSystemInsets = new Rect();
Winson3e874742016-01-07 10:08:17 -080092 private int mDividerSize;
Winsonbe7607a2015-10-01 17:24:51 -070093
Winson Chung9a742902015-12-11 10:25:40 -050094 private RecentsTransitionHelper mTransitionHelper;
95 private RecentsViewTouchHandler mTouchHandler;
Winsonbe7607a2015-10-01 17:24:51 -070096
Winson Chung9a742902015-12-11 10:25:40 -050097 private final Interpolator mFastOutSlowInInterpolator;
98 private final Interpolator mFastOutLinearInInterpolator;
99 private final FlingAnimationUtils mFlingAnimationUtils;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800100
Winson Chung303e1ff2014-03-07 15:06:19 -0800101 public RecentsView(Context context) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800102 this(context, null);
Winson Chung8e548f72014-06-24 14:40:53 -0700103 }
104
105 public RecentsView(Context context, AttributeSet attrs) {
106 this(context, attrs, 0);
107 }
108
109 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
110 this(context, attrs, defStyleAttr, 0);
111 }
112
113 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
114 super(context, attrs, defStyleAttr, defStyleRes);
Winsonbe7607a2015-10-01 17:24:51 -0700115 setWillNotDraw(false);
Winson3e874742016-01-07 10:08:17 -0800116
117 SystemServicesProxy ssp = Recents.getSystemServices();
Winson83c1b072015-11-09 10:48:04 -0800118 mHandler = new Handler();
119 mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
Winson35f30502015-09-28 11:24:36 -0700120 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
121 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonc29ff002015-11-20 16:00:45 -0800122 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
123 com.android.internal.R.interpolator.fast_out_linear_in);
Winson3e874742016-01-07 10:08:17 -0800124 mDividerSize = ssp.getDockedDividerSize(context);
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 Chungaaeaac12015-12-16 16:49:36 -0500133 EventBus.getDefault().send(new ShowHistoryEvent());
Winsonc29ff002015-11-20 16:00:45 -0800134 }
135 });
Winson Chung9a742902015-12-11 10:25:40 -0500136 addView(mHistoryButton);
137 mEmptyView = inflater.inflate(R.layout.recents_empty, this, false);
138 addView(mEmptyView);
Winson Chungd16c5652015-01-26 16:11:07 -0800139 }
140
Winson Chungdcfa7972014-07-22 12:27:13 -0700141 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700142 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700143 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung9a742902015-12-11 10:25:40 -0500144 RecentsActivityLaunchState launchState = config.getLaunchState();
Winsonc29ff002015-11-20 16:00:45 -0800145 mStack = stack;
Winson55e20d52016-01-06 15:21:29 -0800146 if (launchState.launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700147 if (mTaskStackView != null) {
148 // If onRecentsHidden is not triggered, we need to the stack view again here
149 mTaskStackView.reset();
150 mTaskStackView.setStack(stack);
151 } else {
152 mTaskStackView = new TaskStackView(getContext(), stack);
Winson147ecaf2015-09-16 16:49:55 -0700153 addView(mTaskStackView);
154 }
155 } else {
156 if (mTaskStackView != null) {
157 removeView(mTaskStackView);
158 }
159 mTaskStackView = new TaskStackView(getContext(), stack);
Winson147ecaf2015-09-16 16:49:55 -0700160 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800161 }
Winson Chung9a742902015-12-11 10:25:40 -0500162
163 // Update the top level view's visibilities
Winson4b057c62016-01-12 15:01:52 -0800164 if (stack.getTaskCount() > 0) {
Winson Chung9a742902015-12-11 10:25:40 -0500165 hideEmptyView();
Winsonc29ff002015-11-20 16:00:45 -0800166 } else {
Winson Chung9a742902015-12-11 10:25:40 -0500167 showEmptyView();
Winsonc29ff002015-11-20 16:00:45 -0800168 }
Winson Chung02d49272014-08-29 13:57:29 -0700169
Winson Chungb0a28ea2014-10-28 15:21:35 -0700170 // Trigger a new layout
171 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700172 }
173
Winsonb1e71d02015-11-23 12:40:23 -0800174 /**
175 * Returns whether the last task launched was in the freeform stack or not.
176 */
177 public boolean isLastTaskLaunchedFreeform() {
178 return mLastTaskLaunchedWasFreeform;
179 }
180
Winsonc29ff002015-11-20 16:00:45 -0800181 /**
182 * Returns the currently set task stack.
183 */
184 public TaskStack getTaskStack() {
185 return mStack;
186 }
187
Skuhne8aa7d162015-03-20 13:40:53 -0700188 /** Gets the next task in the stack - or if the last - the top task */
189 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700190 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700191 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700192 if (mTaskStackView != null) {
193 TaskStack stack = mTaskStackView.getStack();
Winson250608a2015-11-24 15:00:31 -0800194 ArrayList<Task> taskList = stack.getStackTasks();
Skuhne8aa7d162015-03-20 13:40:53 -0700195 // Iterate the stack views and try and find the focused task
196 for (int j = taskList.size() - 1; j >= 0; --j) {
197 Task task = taskList.get(j);
198 // Return the next task in the line.
199 if (found)
200 return task;
201 // Remember the first possible task as the top task.
202 if (returnTask == null)
203 returnTask = task;
204 if (task == taskToSearch)
205 found = true;
206 }
207 }
208 return returnTask;
209 }
210
Winson Chung1e8d71b2014-05-16 17:05:22 -0700211 /** Launches the focused task from the first stack if possible */
212 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700213 if (mTaskStackView != null) {
Winson142af422015-11-09 10:39:57 -0800214 Task task = mTaskStackView.getFocusedTask();
215 if (task != null) {
216 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson Chung48f2cda2015-12-11 13:20:12 -0500217 EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
218 INVALID_STACK_ID, false));
Winson142af422015-11-09 10:39:57 -0800219 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700220 }
221 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700222 return false;
223 }
224
Winsona0731a12015-12-02 15:10:14 -0800225 /** Launches the task that recents was launched from if possible */
226 public boolean launchPreviousTask() {
227 if (mTaskStackView != null) {
228 TaskStack stack = mTaskStackView.getStack();
229 Task task = stack.getLaunchTarget();
230 if (task != null) {
231 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson Chung48f2cda2015-12-11 13:20:12 -0500232 EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
233 INVALID_STACK_ID, false));
Winsona0731a12015-12-02 15:10:14 -0800234 return true;
235 }
236 }
237 return false;
238 }
239
Skuhne8aa7d162015-03-20 13:40:53 -0700240 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700241 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700242 if (mTaskStackView != null) {
Skuhne8aa7d162015-03-20 13:40:53 -0700243 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700244 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700245 int taskViewCount = taskViews.size();
246 for (int j = 0; j < taskViewCount; j++) {
247 TaskView tv = taskViews.get(j);
248 if (tv.getTask() == task) {
Winson Chung48f2cda2015-12-11 13:20:12 -0500249 EventBus.getDefault().send(new LaunchTaskEvent(tv, task, taskBounds,
250 destinationStack, false));
Skuhne8aa7d162015-03-20 13:40:53 -0700251 return true;
252 }
253 }
254 }
255 return false;
256 }
257
Winson Chungf7bca432014-04-30 17:11:13 -0700258 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700259 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700260 // Remove the previous search bar if one exists
261 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
262 removeView(mSearchBar);
263 }
264 // Add the new search bar
265 if (searchBar != null) {
266 mSearchBar = searchBar;
267 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700268 }
Winson Chungecd9b302014-04-16 17:07:18 -0700269 }
270
Winson Chung772b6b12014-07-03 15:54:02 -0700271 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700272 public boolean hasValidSearchBar() {
273 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700274 }
275
Winson Chung9a742902015-12-11 10:25:40 -0500276 /**
277 * Hides the task stack and shows the empty view.
278 */
279 public void showEmptyView() {
Winsond8b1d632016-01-04 17:51:18 -0800280 if (RecentsDebugFlags.Static.EnableSearchBar && (mSearchBar != null)) {
Winson Chung9a742902015-12-11 10:25:40 -0500281 mSearchBar.setVisibility(View.INVISIBLE);
282 }
283 mTaskStackView.setVisibility(View.INVISIBLE);
284 mEmptyView.setVisibility(View.VISIBLE);
285 mEmptyView.bringToFront();
286 mHistoryButton.bringToFront();
287 }
288
289 /**
290 * Shows the task stack and hides the empty view.
291 */
292 public void hideEmptyView() {
293 mEmptyView.setVisibility(View.INVISIBLE);
294 mTaskStackView.setVisibility(View.VISIBLE);
Winsond8b1d632016-01-04 17:51:18 -0800295 if (RecentsDebugFlags.Static.EnableSearchBar && (mSearchBar != null)) {
Winson Chung9a742902015-12-11 10:25:40 -0500296 mSearchBar.setVisibility(View.VISIBLE);
297 }
298 mTaskStackView.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700299 if (mSearchBar != null) {
Winson Chung012ef362014-07-31 18:36:25 -0700300 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700301 }
Winson Chung9a742902015-12-11 10:25:40 -0500302 mHistoryButton.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700303 }
304
Winsonc29ff002015-11-20 16:00:45 -0800305 /**
306 * Returns the last known system insets.
307 */
308 public Rect getSystemInsets() {
309 return mSystemInsets;
310 }
311
Winsonbe7607a2015-10-01 17:24:51 -0700312 @Override
313 protected void onAttachedToWindow() {
314 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
Winsonc8a4cdc2016-01-19 11:30:51 -0800315 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 2);
Winsonbe7607a2015-10-01 17:24:51 -0700316 super.onAttachedToWindow();
317 }
318
319 @Override
320 protected void onDetachedFromWindow() {
321 super.onDetachedFromWindow();
322 EventBus.getDefault().unregister(this);
323 EventBus.getDefault().unregister(mTouchHandler);
324 }
325
Winson Chungf7bca432014-04-30 17:11:13 -0700326 /**
327 * This is called with the full size of the window since we are handling our own insets.
328 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800329 @Override
330 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700331 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800332 int width = MeasureSpec.getSize(widthMeasureSpec);
333 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700334
Winson Chungf7bca432014-04-30 17:11:13 -0700335 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700336 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700337 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700338 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700339 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700340 mSearchBar.measure(
341 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
342 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700343 }
344
Winson Chungf7bca432014-04-30 17:11:13 -0700345 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700346 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700347 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700348 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700349 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700350 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800351 }
352
Winson Chung9a742902015-12-11 10:25:40 -0500353 // Measure the empty view
354 measureChild(mEmptyView, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
355 MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
356
Winson250608a2015-11-24 15:00:31 -0800357 // Measure the history button with the full space above the stack, but width-constrained
358 // to the stack
Winson387aac62015-11-25 11:18:56 -0800359 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
Winsonc29ff002015-11-20 16:00:45 -0800360 measureChild(mHistoryButton,
Winson387aac62015-11-25 11:18:56 -0800361 MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
362 MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
Winson250608a2015-11-24 15:00:31 -0800363 MeasureSpec.EXACTLY));
Winson Chung9a742902015-12-11 10:25:40 -0500364
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
Winson Chung9a742902015-12-11 10:25:40 -0500389 // Layout the empty view
390 mEmptyView.layout(left, top, right, bottom);
391
Winson250608a2015-11-24 15:00:31 -0800392 // Layout the history button left-aligned with the stack, but offset from the top of the
393 // view
Winson387aac62015-11-25 11:18:56 -0800394 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
395 mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
396 historyButtonRect.right, historyButtonRect.bottom);
Winsonc29ff002015-11-20 16:00:45 -0800397
Winsonb1e71d02015-11-23 12:40:23 -0800398 if (mAwaitingFirstLayout) {
399 mAwaitingFirstLayout = false;
400
401 // If launched via dragging from the nav bar, then we should translate the whole view
402 // down offscreen
403 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
404 if (launchState.launchedViaDragGesture) {
405 setTranslationY(getMeasuredHeight());
Jorim Jaggi0ca88262015-12-07 17:15:16 -0800406 } else {
407 setTranslationY(0f);
Winsonb1e71d02015-11-23 12:40:23 -0800408 }
409 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800410 }
411
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700412 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700413 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700414 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700415 requestLayout();
Winsonc29ff002015-11-20 16:00:45 -0800416 return insets;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700417 }
418
Winsonbe7607a2015-10-01 17:24:51 -0700419 @Override
420 public boolean onInterceptTouchEvent(MotionEvent ev) {
421 return mTouchHandler.onInterceptTouchEvent(ev);
422 }
423
424 @Override
425 public boolean onTouchEvent(MotionEvent ev) {
426 return mTouchHandler.onTouchEvent(ev);
427 }
428
429 @Override
430 protected void dispatchDraw(Canvas canvas) {
431 super.dispatchDraw(canvas);
Winson036693c2016-01-14 17:16:06 -0800432 ArrayList<TaskStack.DockState> visDockStates = mTouchHandler.getVisibleDockStates();
433 for (int i = visDockStates.size() - 1; i >= 0; i--) {
434 Drawable d = visDockStates.get(i).viewState.dockAreaOverlay;
Winson882072b2015-10-12 11:26:33 -0700435 if (d.getAlpha() > 0) {
436 d.draw(canvas);
437 }
Winsonbe7607a2015-10-01 17:24:51 -0700438 }
439 }
440
Winson4165d3362015-10-10 14:40:35 -0700441 @Override
442 protected boolean verifyDrawable(Drawable who) {
Winson036693c2016-01-14 17:16:06 -0800443 ArrayList<TaskStack.DockState> visDockStates = mTouchHandler.getVisibleDockStates();
444 for (int i = visDockStates.size() - 1; i >= 0; i--) {
445 Drawable d = visDockStates.get(i).viewState.dockAreaOverlay;
Winson882072b2015-10-12 11:26:33 -0700446 if (d == who) {
447 return true;
448 }
449 }
450 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700451 }
452
Winsonbe7607a2015-10-01 17:24:51 -0700453 /**** EventBus Events ****/
454
Winson Chung48f2cda2015-12-11 13:20:12 -0500455 public final void onBusEvent(LaunchTaskEvent event) {
456 mLastTaskLaunchedWasFreeform = event.task.isFreeformTask();
457 mTransitionHelper.launchTaskFromRecents(mStack, event.task, mTaskStackView, event.taskView,
458 event.screenPinningRequested, event.targetTaskBounds, event.targetTaskStack);
459 }
460
Winsonef064132016-01-05 12:11:31 -0800461 public final void onBusEvent(DismissRecentsToHomeAnimationStarted event) {
462 // Hide the history button
463 int taskViewExitToHomeDuration = getResources().getInteger(
464 R.integer.recents_task_exit_to_home_duration);
465 hideHistoryButton(taskViewExitToHomeDuration);
466
467 // If we are going home, cancel the previous task's window transition
468 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
469 }
470
Winsonbe7607a2015-10-01 17:24:51 -0700471 public final void onBusEvent(DragStartEvent event) {
Winson882072b2015-10-12 11:26:33 -0700472 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
Winson3e874742016-01-07 10:08:17 -0800473 true /* isDefaultDockState */, TaskStack.DockState.NONE.viewState.dockAreaAlpha,
474 true /* animateAlpha */, false /* animateBounds */);
Winsonbe7607a2015-10-01 17:24:51 -0700475 }
476
Winsoneca4ab62015-11-04 10:50:28 -0800477 public final void onBusEvent(DragDropTargetChangedEvent event) {
478 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700479 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
Winson3e874742016-01-07 10:08:17 -0800480 true /* isDefaultDockState */, TaskStack.DockState.NONE.viewState.dockAreaAlpha,
481 true /* animateAlpha */, true /* animateBounds */);
Winson882072b2015-10-12 11:26:33 -0700482 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800483 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
Winson3e874742016-01-07 10:08:17 -0800484 updateVisibleDockRegions(new TaskStack.DockState[] {dockState},
485 false /* isDefaultDockState */, -1, true /* animateAlpha */,
486 true /* animateBounds */);
Winson882072b2015-10-12 11:26:33 -0700487 }
Winsonbe7607a2015-10-01 17:24:51 -0700488 }
489
490 public final void onBusEvent(final DragEndEvent event) {
Winson479f7442015-11-25 15:16:27 -0800491 // Handle the case where we drop onto a dock region
492 if (event.dropTarget instanceof TaskStack.DockState) {
Winson3e874742016-01-07 10:08:17 -0800493 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
494
495 // Hide the dock region
496 updateVisibleDockRegions(null, false /* isDefaultDockState */, -1,
497 false /* animateAlpha */, false /* animateBounds */);
498
Winsonf24f2162016-01-05 12:11:55 -0800499 TaskStackLayoutAlgorithm stackLayout = mTaskStackView.getStackAlgorithm();
500 TaskStackViewScroller stackScroller = mTaskStackView.getScroller();
501 TaskViewTransform tmpTransform = new TaskViewTransform();
Winsoneca4ab62015-11-04 10:50:28 -0800502
Winson3e874742016-01-07 10:08:17 -0800503 // We translated the view but we need to animate it back from the current layout-space
504 // rect to its final layout-space rect
505 int x = (int) event.taskView.getTranslationX();
506 int y = (int) event.taskView.getTranslationY();
507 Rect taskViewRect = new Rect(event.taskView.getLeft(), event.taskView.getTop(),
508 event.taskView.getRight(), event.taskView.getBottom());
509 taskViewRect.offset(x, y);
510 event.taskView.setTranslationX(0);
511 event.taskView.setTranslationY(0);
512 event.taskView.setLeftTopRightBottom(taskViewRect.left, taskViewRect.top,
513 taskViewRect.right, taskViewRect.bottom);
514
Winsonf24f2162016-01-05 12:11:55 -0800515 // Remove the task view after it is docked
Winson3e874742016-01-07 10:08:17 -0800516 mTaskStackView.updateLayout(false /* boundScroll */);
Winsonf24f2162016-01-05 12:11:55 -0800517 stackLayout.getStackTransform(event.task, stackScroller.getStackScroll(), tmpTransform,
518 null);
Winson3e874742016-01-07 10:08:17 -0800519 tmpTransform.alpha = 0;
520 tmpTransform.scale = 1f;
521 tmpTransform.rect.set(taskViewRect);
Winsonf24f2162016-01-05 12:11:55 -0800522 mTaskStackView.updateTaskViewToTransform(event.taskView, tmpTransform,
Winson3e874742016-01-07 10:08:17 -0800523 new TaskViewAnimation(125, PhoneStatusBar.ALPHA_OUT,
Winsonf24f2162016-01-05 12:11:55 -0800524 new AnimatorListenerAdapter() {
525 @Override
526 public void onAnimationEnd(Animator animation) {
Winson3e874742016-01-07 10:08:17 -0800527 // Dock the task and launch it
528 SystemServicesProxy ssp = Recents.getSystemServices();
Filip Gruszczynskif298e902016-01-18 08:59:45 -0800529 ssp.startTaskInDockedMode(getContext(), event.taskView,
530 event.task.key.id, dockState.createMode);
Winson3e874742016-01-07 10:08:17 -0800531
Winsonf24f2162016-01-05 12:11:55 -0800532 mTaskStackView.getStack().removeTask(event.task);
533 }
534 }));
Winsona0a8ae42015-12-01 10:43:02 -0800535
Jorim Jaggi92de6b62015-12-09 15:32:30 -0800536
537 MetricsLogger.action(mContext,
538 MetricsLogger.ACTION_WINDOW_DOCK_DRAG_DROP);
Winson3e874742016-01-07 10:08:17 -0800539 } else {
540 // Animate the overlay alpha back to 0
541 updateVisibleDockRegions(null, true /* isDefaultDockState */, -1,
542 true /* animateAlpha */, false /* animateBounds */);
Winsonbe7607a2015-10-01 17:24:51 -0700543 }
544 }
Winson4165d3362015-10-10 14:40:35 -0700545
Jorim Jaggidd98d412015-11-18 15:57:38 -0800546 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800547 if (mTaskStackView.getTaskViews().size() > 0) {
548 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
549 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800550 }
551
552 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800553 ViewPropertyAnimator animator = animate();
Jorim Jaggi8fccfe22015-12-03 16:58:08 -0800554 if (event.velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800555 animator.translationY(getHeight());
556 animator.withEndAction(new Runnable() {
557 @Override
558 public void run() {
559 WindowManagerProxy.getInstance().maximizeDockedStack();
560 }
561 });
562 mFlingAnimationUtils.apply(animator, getTranslationY(), getHeight(), event.velocity);
563 } else {
564 animator.translationY(0f);
565 animator.setListener(null);
566 mFlingAnimationUtils.apply(animator, getTranslationY(), 0, event.velocity);
567 }
568 animator.start();
Jorim Jaggidd98d412015-11-18 15:57:38 -0800569 }
570
Winsonc29ff002015-11-20 16:00:45 -0800571 public final void onBusEvent(ShowHistoryEvent event) {
572 // Hide the history button when the history view is shown
Winson Chung9a742902015-12-11 10:25:40 -0500573 hideHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500574 event.getAnimationTrigger());
575 event.addPostAnimationCallback(new Runnable() {
Winson Chung9a742902015-12-11 10:25:40 -0500576 @Override
577 public void run() {
578 setAlpha(0f);
579 }
580 });
Winsonc29ff002015-11-20 16:00:45 -0800581 }
582
583 public final void onBusEvent(HideHistoryEvent event) {
584 // Show the history button when the history view is hidden
Winson Chung9a742902015-12-11 10:25:40 -0500585 setAlpha(1f);
586 showHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500587 event.getAnimationTrigger());
Winsonc29ff002015-11-20 16:00:45 -0800588 }
589
590 public final void onBusEvent(ShowHistoryButtonEvent event) {
591 showHistoryButton(150);
592 }
593
594 public final void onBusEvent(HideHistoryButtonEvent event) {
595 hideHistoryButton(100);
596 }
597
Winson Chung06266772015-12-11 10:24:21 -0500598 public final void onBusEvent(TaskStackUpdatedEvent event) {
599 mStack.setTasks(event.stack.computeAllTasksList(), true /* notifyStackChanges */);
Winson Chung97567552015-12-16 17:07:19 -0500600 mStack.createAffiliatedGroupings(getContext());
Winson Chung06266772015-12-11 10:24:21 -0500601 }
602
Winsonc29ff002015-11-20 16:00:45 -0800603 /**
604 * Shows the history button.
605 */
Winson Chung9a742902015-12-11 10:25:40 -0500606 private void showHistoryButton(final int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500607 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500608 showHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500609 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500610 }
611
612 private void showHistoryButton(final int duration,
613 final ReferenceCountedTrigger postHideHistoryAnimationTrigger) {
Winson Chungde750de2015-12-11 10:26:06 -0500614 mHistoryButton.setText(getContext().getString(R.string.recents_history_label_format,
615 mStack.getHistoricalTasks().size()));
Winsonf24f2162016-01-05 12:11:55 -0800616 if (mHistoryButton.getVisibility() == View.INVISIBLE) {
617 mHistoryButton.setVisibility(View.VISIBLE);
618 mHistoryButton.setAlpha(0f);
619 postHideHistoryAnimationTrigger.addLastDecrementRunnable(new Runnable() {
620 @Override
621 public void run() {
622 mHistoryButton.animate()
623 .alpha(1f)
624 .setDuration(duration)
625 .setInterpolator(mFastOutSlowInInterpolator)
626 .withLayer()
627 .start();
628 }
629 });
630 }
Winsonc29ff002015-11-20 16:00:45 -0800631 }
632
633 /**
634 * Hides the history button.
635 */
636 private void hideHistoryButton(int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500637 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500638 hideHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500639 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500640 }
641
642 private void hideHistoryButton(int duration,
643 final ReferenceCountedTrigger postHideStackAnimationTrigger) {
Winsonf24f2162016-01-05 12:11:55 -0800644 if (mHistoryButton.getVisibility() == View.VISIBLE) {
645 mHistoryButton.animate()
646 .alpha(0f)
647 .setDuration(duration)
648 .setInterpolator(mFastOutLinearInInterpolator)
649 .withEndAction(new Runnable() {
650 @Override
651 public void run() {
652 mHistoryButton.setVisibility(View.INVISIBLE);
653 postHideStackAnimationTrigger.decrement();
654 }
655 })
656 .withLayer()
657 .start();
658 postHideStackAnimationTrigger.increment();
659 }
Winsonc29ff002015-11-20 16:00:45 -0800660 }
661
Winson4165d3362015-10-10 14:40:35 -0700662 /**
663 * Updates the dock region to match the specified dock state.
664 */
Winson3e874742016-01-07 10:08:17 -0800665 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates,
666 boolean isDefaultDockState, int overrideAlpha, boolean animateAlpha,
667 boolean animateBounds) {
Winsonc5fd3502016-01-18 15:18:37 -0800668 ArraySet<TaskStack.DockState> newDockStatesSet = Utilities.arrayToSet(newDockStates,
669 new ArraySet<TaskStack.DockState>());
Winson036693c2016-01-14 17:16:06 -0800670 ArrayList<TaskStack.DockState> visDockStates = mTouchHandler.getVisibleDockStates();
671 for (int i = visDockStates.size() - 1; i >= 0; i--) {
672 TaskStack.DockState dockState = visDockStates.get(i);
Winson882072b2015-10-12 11:26:33 -0700673 TaskStack.DockState.ViewState viewState = dockState.viewState;
674 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
675 // This is no longer visible, so hide it
Winson3e874742016-01-07 10:08:17 -0800676 viewState.startAnimation(null, 0, DOCK_AREA_OVERLAY_TRANSITION_DURATION,
677 PhoneStatusBar.ALPHA_OUT, animateAlpha, animateBounds);
Winson882072b2015-10-12 11:26:33 -0700678 } else {
679 // This state is now visible, update the bounds and show it
680 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
Winson3e874742016-01-07 10:08:17 -0800681 Rect bounds = isDefaultDockState
682 ? dockState.getPreDockedBounds(getMeasuredWidth(), getMeasuredHeight())
683 : dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight(),
684 mDividerSize, mSystemInsets, getResources());
685 if (viewState.dockAreaOverlay.getCallback() != this) {
686 viewState.dockAreaOverlay.setCallback(this);
687 viewState.dockAreaOverlay.setBounds(bounds);
688 }
689 viewState.startAnimation(bounds, alpha, DOCK_AREA_OVERLAY_TRANSITION_DURATION,
690 PhoneStatusBar.ALPHA_IN, animateAlpha, animateBounds);
Winson882072b2015-10-12 11:26:33 -0700691 }
Winson4165d3362015-10-10 14:40:35 -0700692 }
Winson4165d3362015-10-10 14:40:35 -0700693 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800694
Winsonb1e71d02015-11-23 12:40:23 -0800695 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
696 if (!event.visible) {
697 // Reset the view state
698 mAwaitingFirstLayout = true;
699 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800700 }
701 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800702}