blob: d71bdbeb569f2249dc63a27f00fef360e34d7213 [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;
Winson3e874742016-01-07 10:08:17 -080022import android.content.res.Resources;
Winson Chung303e1ff2014-03-07 15:06:19 -080023import android.graphics.Canvas;
24import android.graphics.Rect;
Winson4165d3362015-10-10 14:40:35 -070025import android.graphics.drawable.Drawable;
Winson83c1b072015-11-09 10:48:04 -080026import android.os.Handler;
Winson882072b2015-10-12 11:26:33 -070027import android.util.ArraySet;
Winson Chung8e548f72014-06-24 14:40:53 -070028import android.util.AttributeSet;
Winson Chungecd9b302014-04-16 17:07:18 -070029import android.view.LayoutInflater;
Winsonbe7607a2015-10-01 17:24:51 -070030import android.view.MotionEvent;
Winsonc29ff002015-11-20 16:00:45 -080031import android.view.View;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -080032import android.view.ViewPropertyAnimator;
Winson Chung653f70c22014-05-19 14:49:42 -070033import android.view.WindowInsets;
Winson35f30502015-09-28 11:24:36 -070034import android.view.animation.AnimationUtils;
35import android.view.animation.Interpolator;
Winson Chung303e1ff2014-03-07 15:06:19 -080036import android.widget.FrameLayout;
Winson Chungde750de2015-12-11 10:26:06 -050037import android.widget.TextView;
Winsondc8de842016-01-06 15:21:41 -080038import com.android.internal.logging.MetricsLogger;
Winson Chungd16c5652015-01-26 16:11:07 -080039import com.android.systemui.R;
Winsone7f138c2015-10-22 16:15:21 -070040import com.android.systemui.recents.Recents;
Winsonbe7607a2015-10-01 17:24:51 -070041import com.android.systemui.recents.RecentsActivity;
Winsonb1e71d02015-11-23 12:40:23 -080042import com.android.systemui.recents.RecentsActivityLaunchState;
Winson Chungaee097c2015-04-02 18:16:02 -070043import com.android.systemui.recents.RecentsAppWidgetHostView;
Winson Chung303e1ff2014-03-07 15:06:19 -080044import com.android.systemui.recents.RecentsConfiguration;
Winsonc29ff002015-11-20 16:00:45 -080045import com.android.systemui.recents.RecentsDebugFlags;
Winson2536c7e2015-10-01 15:49:31 -070046import com.android.systemui.recents.events.EventBus;
Winson13d30662015-11-06 15:30:29 -080047import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
Winson412e1802015-10-20 16:57:57 -070048import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
Winsonc29ff002015-11-20 16:00:45 -080049import com.android.systemui.recents.events.activity.HideHistoryButtonEvent;
50import com.android.systemui.recents.events.activity.HideHistoryEvent;
Winson Chung48f2cda2015-12-11 13:20:12 -050051import com.android.systemui.recents.events.activity.LaunchTaskEvent;
Winsonc29ff002015-11-20 16:00:45 -080052import com.android.systemui.recents.events.activity.ShowHistoryButtonEvent;
53import com.android.systemui.recents.events.activity.ShowHistoryEvent;
Winson Chung06266772015-12-11 10:24:21 -050054import com.android.systemui.recents.events.activity.TaskStackUpdatedEvent;
Winsonb1e71d02015-11-23 12:40:23 -080055import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
Jorim Jaggidd98d412015-11-18 15:57:38 -080056import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
57import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
Winsoneca4ab62015-11-04 10:50:28 -080058import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
Winsonbe7607a2015-10-01 17:24:51 -070059import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
60import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
Winson Chung9a742902015-12-11 10:25:40 -050061import com.android.systemui.recents.misc.ReferenceCountedTrigger;
Winson Chung1f24c7e2014-07-11 17:06:48 -070062import com.android.systemui.recents.misc.SystemServicesProxy;
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;
Winsondc8de842016-01-06 15:21:41 -080070import java.util.Collections;
Winson Chung6ac8bd62015-01-07 16:38:35 -080071import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080072
Wale Ogunwale3797c222015-10-27 14:21:58 -070073import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070074
Winson Chung303e1ff2014-03-07 15:06:19 -080075/**
76 * This view is the the top level layout that contains TaskStacks (which are laid out according
77 * to their SpaceNode bounds.
78 */
Winson Chung48f2cda2015-12-11 13:20:12 -050079public class RecentsView extends FrameLayout {
Winson Chung47c4c692014-03-17 10:17:11 -070080
Winson3e874742016-01-07 10:08:17 -080081 private static final int DOCK_AREA_OVERLAY_TRANSITION_DURATION = 135;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070082
Winson Chung9a742902015-12-11 10:25:40 -050083 private final Handler mHandler;
Winson Chungd42a6cf2014-06-03 16:24:04 -070084
Winson Chung9a742902015-12-11 10:25:40 -050085 private TaskStack mStack;
86 private TaskStackView mTaskStackView;
87 private RecentsAppWidgetHostView mSearchBar;
Winson Chungde750de2015-12-11 10:26:06 -050088 private TextView mHistoryButton;
Winson Chung9a742902015-12-11 10:25:40 -050089 private View mEmptyView;
90 private boolean mAwaitingFirstLayout = true;
91 private boolean mLastTaskLaunchedWasFreeform;
92 private Rect mSystemInsets = new Rect();
Winson3e874742016-01-07 10:08:17 -080093 private int mDividerSize;
Winsonbe7607a2015-10-01 17:24:51 -070094
Winson Chung9a742902015-12-11 10:25:40 -050095 private RecentsTransitionHelper mTransitionHelper;
96 private RecentsViewTouchHandler mTouchHandler;
97 private TaskStack.DockState[] mVisibleDockStates = {
Winson882072b2015-10-12 11:26:33 -070098 TaskStack.DockState.LEFT,
99 TaskStack.DockState.TOP,
100 TaskStack.DockState.RIGHT,
101 TaskStack.DockState.BOTTOM,
102 };
Winsonbe7607a2015-10-01 17:24:51 -0700103
Winson Chung9a742902015-12-11 10:25:40 -0500104 private final Interpolator mFastOutSlowInInterpolator;
105 private final Interpolator mFastOutLinearInInterpolator;
106 private final FlingAnimationUtils mFlingAnimationUtils;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800107
Winson Chung303e1ff2014-03-07 15:06:19 -0800108 public RecentsView(Context context) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800109 this(context, null);
Winson Chung8e548f72014-06-24 14:40:53 -0700110 }
111
112 public RecentsView(Context context, AttributeSet attrs) {
113 this(context, attrs, 0);
114 }
115
116 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
117 this(context, attrs, defStyleAttr, 0);
118 }
119
120 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
121 super(context, attrs, defStyleAttr, defStyleRes);
Winsonbe7607a2015-10-01 17:24:51 -0700122 setWillNotDraw(false);
Winson3e874742016-01-07 10:08:17 -0800123
124 SystemServicesProxy ssp = Recents.getSystemServices();
Winson83c1b072015-11-09 10:48:04 -0800125 mHandler = new Handler();
126 mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
Winson35f30502015-09-28 11:24:36 -0700127 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
128 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonc29ff002015-11-20 16:00:45 -0800129 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
130 com.android.internal.R.interpolator.fast_out_linear_in);
Winson3e874742016-01-07 10:08:17 -0800131 mDividerSize = ssp.getDockedDividerSize(context);
Winsonbe7607a2015-10-01 17:24:51 -0700132 mTouchHandler = new RecentsViewTouchHandler(this);
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800133 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.3f);
Winsonc29ff002015-11-20 16:00:45 -0800134
135 LayoutInflater inflater = LayoutInflater.from(context);
Winson Chungde750de2015-12-11 10:26:06 -0500136 mHistoryButton = (TextView) inflater.inflate(R.layout.recents_history_button, this, false);
Winsonc29ff002015-11-20 16:00:45 -0800137 mHistoryButton.setOnClickListener(new View.OnClickListener() {
138 @Override
139 public void onClick(View v) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500140 EventBus.getDefault().send(new ShowHistoryEvent());
Winsonc29ff002015-11-20 16:00:45 -0800141 }
142 });
Winson Chung9a742902015-12-11 10:25:40 -0500143 addView(mHistoryButton);
144 mEmptyView = inflater.inflate(R.layout.recents_empty, this, false);
145 addView(mEmptyView);
Winson Chungd16c5652015-01-26 16:11:07 -0800146 }
147
Winson Chungdcfa7972014-07-22 12:27:13 -0700148 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700149 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700150 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung9a742902015-12-11 10:25:40 -0500151 RecentsActivityLaunchState launchState = config.getLaunchState();
Winsonc29ff002015-11-20 16:00:45 -0800152 mStack = stack;
Winson55e20d52016-01-06 15:21:29 -0800153 if (launchState.launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700154 if (mTaskStackView != null) {
155 // If onRecentsHidden is not triggered, we need to the stack view again here
156 mTaskStackView.reset();
157 mTaskStackView.setStack(stack);
158 } else {
159 mTaskStackView = new TaskStackView(getContext(), stack);
Winson147ecaf2015-09-16 16:49:55 -0700160 addView(mTaskStackView);
161 }
162 } else {
163 if (mTaskStackView != null) {
164 removeView(mTaskStackView);
165 }
166 mTaskStackView = new TaskStackView(getContext(), stack);
Winson147ecaf2015-09-16 16:49:55 -0700167 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800168 }
Winson Chung9a742902015-12-11 10:25:40 -0500169
170 // Update the top level view's visibilities
171 if (stack.getStackTaskCount() > 0) {
172 hideEmptyView();
Winsonc29ff002015-11-20 16:00:45 -0800173 } else {
Winson Chung9a742902015-12-11 10:25:40 -0500174 showEmptyView();
Winsonc29ff002015-11-20 16:00:45 -0800175 }
Winson Chung02d49272014-08-29 13:57:29 -0700176
Winson Chungb0a28ea2014-10-28 15:21:35 -0700177 // Trigger a new layout
178 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700179 }
180
Winsonb1e71d02015-11-23 12:40:23 -0800181 /**
182 * Returns whether the last task launched was in the freeform stack or not.
183 */
184 public boolean isLastTaskLaunchedFreeform() {
185 return mLastTaskLaunchedWasFreeform;
186 }
187
Winsonc29ff002015-11-20 16:00:45 -0800188 /**
189 * Returns the currently set task stack.
190 */
191 public TaskStack getTaskStack() {
192 return mStack;
193 }
194
Skuhne8aa7d162015-03-20 13:40:53 -0700195 /** Gets the next task in the stack - or if the last - the top task */
196 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700197 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700198 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700199 if (mTaskStackView != null) {
200 TaskStack stack = mTaskStackView.getStack();
Winson250608a2015-11-24 15:00:31 -0800201 ArrayList<Task> taskList = stack.getStackTasks();
Skuhne8aa7d162015-03-20 13:40:53 -0700202 // Iterate the stack views and try and find the focused task
203 for (int j = taskList.size() - 1; j >= 0; --j) {
204 Task task = taskList.get(j);
205 // Return the next task in the line.
206 if (found)
207 return task;
208 // Remember the first possible task as the top task.
209 if (returnTask == null)
210 returnTask = task;
211 if (task == taskToSearch)
212 found = true;
213 }
214 }
215 return returnTask;
216 }
217
Winson Chung1e8d71b2014-05-16 17:05:22 -0700218 /** Launches the focused task from the first stack if possible */
219 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700220 if (mTaskStackView != null) {
Winson142af422015-11-09 10:39:57 -0800221 Task task = mTaskStackView.getFocusedTask();
222 if (task != null) {
223 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson Chung48f2cda2015-12-11 13:20:12 -0500224 EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
225 INVALID_STACK_ID, false));
Winson142af422015-11-09 10:39:57 -0800226 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700227 }
228 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700229 return false;
230 }
231
Winsona0731a12015-12-02 15:10:14 -0800232 /** Launches the task that recents was launched from if possible */
233 public boolean launchPreviousTask() {
234 if (mTaskStackView != null) {
235 TaskStack stack = mTaskStackView.getStack();
236 Task task = stack.getLaunchTarget();
237 if (task != null) {
238 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson Chung48f2cda2015-12-11 13:20:12 -0500239 EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
240 INVALID_STACK_ID, false));
Winsona0731a12015-12-02 15:10:14 -0800241 return true;
242 }
243 }
244 return false;
245 }
246
Skuhne8aa7d162015-03-20 13:40:53 -0700247 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700248 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700249 if (mTaskStackView != null) {
Skuhne8aa7d162015-03-20 13:40:53 -0700250 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700251 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700252 int taskViewCount = taskViews.size();
253 for (int j = 0; j < taskViewCount; j++) {
254 TaskView tv = taskViews.get(j);
255 if (tv.getTask() == task) {
Winson Chung48f2cda2015-12-11 13:20:12 -0500256 EventBus.getDefault().send(new LaunchTaskEvent(tv, task, taskBounds,
257 destinationStack, false));
Skuhne8aa7d162015-03-20 13:40:53 -0700258 return true;
259 }
260 }
261 }
262 return false;
263 }
264
Winson Chungf7bca432014-04-30 17:11:13 -0700265 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700266 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700267 // Remove the previous search bar if one exists
268 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
269 removeView(mSearchBar);
270 }
271 // Add the new search bar
272 if (searchBar != null) {
273 mSearchBar = searchBar;
274 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700275 }
Winson Chungecd9b302014-04-16 17:07:18 -0700276 }
277
Winson Chung772b6b12014-07-03 15:54:02 -0700278 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700279 public boolean hasValidSearchBar() {
280 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700281 }
282
Winson Chung9a742902015-12-11 10:25:40 -0500283 /**
284 * Hides the task stack and shows the empty view.
285 */
286 public void showEmptyView() {
Winsond8b1d632016-01-04 17:51:18 -0800287 if (RecentsDebugFlags.Static.EnableSearchBar && (mSearchBar != null)) {
Winson Chung9a742902015-12-11 10:25:40 -0500288 mSearchBar.setVisibility(View.INVISIBLE);
289 }
290 mTaskStackView.setVisibility(View.INVISIBLE);
291 mEmptyView.setVisibility(View.VISIBLE);
292 mEmptyView.bringToFront();
293 mHistoryButton.bringToFront();
294 }
295
296 /**
297 * Shows the task stack and hides the empty view.
298 */
299 public void hideEmptyView() {
300 mEmptyView.setVisibility(View.INVISIBLE);
301 mTaskStackView.setVisibility(View.VISIBLE);
Winsond8b1d632016-01-04 17:51:18 -0800302 if (RecentsDebugFlags.Static.EnableSearchBar && (mSearchBar != null)) {
Winson Chung9a742902015-12-11 10:25:40 -0500303 mSearchBar.setVisibility(View.VISIBLE);
304 }
305 mTaskStackView.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700306 if (mSearchBar != null) {
Winson Chung012ef362014-07-31 18:36:25 -0700307 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700308 }
Winson Chung9a742902015-12-11 10:25:40 -0500309 mHistoryButton.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700310 }
311
Winsonc29ff002015-11-20 16:00:45 -0800312 /**
313 * Returns the last known system insets.
314 */
315 public Rect getSystemInsets() {
316 return mSystemInsets;
317 }
318
Winsonbe7607a2015-10-01 17:24:51 -0700319 @Override
320 protected void onAttachedToWindow() {
321 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
322 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
323 super.onAttachedToWindow();
324 }
325
326 @Override
327 protected void onDetachedFromWindow() {
328 super.onDetachedFromWindow();
329 EventBus.getDefault().unregister(this);
330 EventBus.getDefault().unregister(mTouchHandler);
331 }
332
Winson Chungf7bca432014-04-30 17:11:13 -0700333 /**
334 * This is called with the full size of the window since we are handling our own insets.
335 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800336 @Override
337 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700338 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800339 int width = MeasureSpec.getSize(widthMeasureSpec);
340 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700341
Winson Chungf7bca432014-04-30 17:11:13 -0700342 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700343 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700344 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700345 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700346 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700347 mSearchBar.measure(
348 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
349 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700350 }
351
Winson Chungf7bca432014-04-30 17:11:13 -0700352 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700353 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700354 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700355 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700356 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700357 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800358 }
359
Winson Chung9a742902015-12-11 10:25:40 -0500360 // Measure the empty view
361 measureChild(mEmptyView, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
362 MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
363
Winson250608a2015-11-24 15:00:31 -0800364 // Measure the history button with the full space above the stack, but width-constrained
365 // to the stack
Winson387aac62015-11-25 11:18:56 -0800366 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
Winsonc29ff002015-11-20 16:00:45 -0800367 measureChild(mHistoryButton,
Winson387aac62015-11-25 11:18:56 -0800368 MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
369 MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
Winson250608a2015-11-24 15:00:31 -0800370 MeasureSpec.EXACTLY));
Winson Chung9a742902015-12-11 10:25:40 -0500371
Winson Chung303e1ff2014-03-07 15:06:19 -0800372 setMeasuredDimension(width, height);
373 }
374
Winson Chungf7bca432014-04-30 17:11:13 -0700375 /**
376 * This is called with the full size of the window since we are handling our own insets.
377 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800378 @Override
379 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700380 RecentsConfiguration config = Recents.getConfiguration();
381
Winson Chungf7bca432014-04-30 17:11:13 -0700382 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700383 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
384 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700385 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700386 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700387 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700388 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
389 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700390 }
391
Winson147ecaf2015-09-16 16:49:55 -0700392 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
393 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800394 }
Winsonbe7607a2015-10-01 17:24:51 -0700395
Winson Chung9a742902015-12-11 10:25:40 -0500396 // Layout the empty view
397 mEmptyView.layout(left, top, right, bottom);
398
Winson250608a2015-11-24 15:00:31 -0800399 // Layout the history button left-aligned with the stack, but offset from the top of the
400 // view
Winson387aac62015-11-25 11:18:56 -0800401 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
402 mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
403 historyButtonRect.right, historyButtonRect.bottom);
Winsonc29ff002015-11-20 16:00:45 -0800404
Winsonb1e71d02015-11-23 12:40:23 -0800405 if (mAwaitingFirstLayout) {
406 mAwaitingFirstLayout = false;
407
408 // If launched via dragging from the nav bar, then we should translate the whole view
409 // down offscreen
410 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
411 if (launchState.launchedViaDragGesture) {
412 setTranslationY(getMeasuredHeight());
Jorim Jaggi0ca88262015-12-07 17:15:16 -0800413 } else {
414 setTranslationY(0f);
Winsonb1e71d02015-11-23 12:40:23 -0800415 }
416 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800417 }
418
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700419 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700420 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700421 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700422 requestLayout();
Winsonc29ff002015-11-20 16:00:45 -0800423 return insets;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700424 }
425
Winsonbe7607a2015-10-01 17:24:51 -0700426 @Override
427 public boolean onInterceptTouchEvent(MotionEvent ev) {
428 return mTouchHandler.onInterceptTouchEvent(ev);
429 }
430
431 @Override
432 public boolean onTouchEvent(MotionEvent ev) {
433 return mTouchHandler.onTouchEvent(ev);
434 }
435
436 @Override
437 protected void dispatchDraw(Canvas canvas) {
438 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700439 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
440 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
441 if (d.getAlpha() > 0) {
442 d.draw(canvas);
443 }
Winsonbe7607a2015-10-01 17:24:51 -0700444 }
445 }
446
Winson4165d3362015-10-10 14:40:35 -0700447 @Override
448 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700449 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
450 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
451 if (d == who) {
452 return true;
453 }
454 }
455 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700456 }
457
Winsonbe7607a2015-10-01 17:24:51 -0700458 /**** EventBus Events ****/
459
Winson Chung48f2cda2015-12-11 13:20:12 -0500460 public final void onBusEvent(LaunchTaskEvent event) {
461 mLastTaskLaunchedWasFreeform = event.task.isFreeformTask();
462 mTransitionHelper.launchTaskFromRecents(mStack, event.task, mTaskStackView, event.taskView,
463 event.screenPinningRequested, event.targetTaskBounds, event.targetTaskStack);
464 }
465
Winsonef064132016-01-05 12:11:31 -0800466 public final void onBusEvent(DismissRecentsToHomeAnimationStarted event) {
467 // Hide the history button
468 int taskViewExitToHomeDuration = getResources().getInteger(
469 R.integer.recents_task_exit_to_home_duration);
470 hideHistoryButton(taskViewExitToHomeDuration);
471
472 // If we are going home, cancel the previous task's window transition
473 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
474 }
475
Winsonbe7607a2015-10-01 17:24:51 -0700476 public final void onBusEvent(DragStartEvent event) {
Winson882072b2015-10-12 11:26:33 -0700477 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
Winson3e874742016-01-07 10:08:17 -0800478 true /* isDefaultDockState */, TaskStack.DockState.NONE.viewState.dockAreaAlpha,
479 true /* animateAlpha */, false /* animateBounds */);
Winsonbe7607a2015-10-01 17:24:51 -0700480 }
481
Winsoneca4ab62015-11-04 10:50:28 -0800482 public final void onBusEvent(DragDropTargetChangedEvent event) {
483 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700484 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
Winson3e874742016-01-07 10:08:17 -0800485 true /* isDefaultDockState */, TaskStack.DockState.NONE.viewState.dockAreaAlpha,
486 true /* animateAlpha */, true /* animateBounds */);
Winson882072b2015-10-12 11:26:33 -0700487 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800488 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
Winson3e874742016-01-07 10:08:17 -0800489 updateVisibleDockRegions(new TaskStack.DockState[] {dockState},
490 false /* isDefaultDockState */, -1, true /* animateAlpha */,
491 true /* animateBounds */);
Winson882072b2015-10-12 11:26:33 -0700492 }
Winsonbe7607a2015-10-01 17:24:51 -0700493 }
494
495 public final void onBusEvent(final DragEndEvent event) {
Winson479f7442015-11-25 15:16:27 -0800496 // Handle the case where we drop onto a dock region
497 if (event.dropTarget instanceof TaskStack.DockState) {
Winson3e874742016-01-07 10:08:17 -0800498 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
499
500 // Hide the dock region
501 updateVisibleDockRegions(null, false /* isDefaultDockState */, -1,
502 false /* animateAlpha */, false /* animateBounds */);
503
Winsonf24f2162016-01-05 12:11:55 -0800504 TaskStackLayoutAlgorithm stackLayout = mTaskStackView.getStackAlgorithm();
505 TaskStackViewScroller stackScroller = mTaskStackView.getScroller();
506 TaskViewTransform tmpTransform = new TaskViewTransform();
Winsoneca4ab62015-11-04 10:50:28 -0800507
Winson3e874742016-01-07 10:08:17 -0800508 // We translated the view but we need to animate it back from the current layout-space
509 // rect to its final layout-space rect
510 int x = (int) event.taskView.getTranslationX();
511 int y = (int) event.taskView.getTranslationY();
512 Rect taskViewRect = new Rect(event.taskView.getLeft(), event.taskView.getTop(),
513 event.taskView.getRight(), event.taskView.getBottom());
514 taskViewRect.offset(x, y);
515 event.taskView.setTranslationX(0);
516 event.taskView.setTranslationY(0);
517 event.taskView.setLeftTopRightBottom(taskViewRect.left, taskViewRect.top,
518 taskViewRect.right, taskViewRect.bottom);
519
Winsonf24f2162016-01-05 12:11:55 -0800520 // Remove the task view after it is docked
Winson3e874742016-01-07 10:08:17 -0800521 mTaskStackView.updateLayout(false /* boundScroll */);
Winsonf24f2162016-01-05 12:11:55 -0800522 stackLayout.getStackTransform(event.task, stackScroller.getStackScroll(), tmpTransform,
523 null);
Winson3e874742016-01-07 10:08:17 -0800524 tmpTransform.alpha = 0;
525 tmpTransform.scale = 1f;
526 tmpTransform.rect.set(taskViewRect);
Winsonf24f2162016-01-05 12:11:55 -0800527 mTaskStackView.updateTaskViewToTransform(event.taskView, tmpTransform,
Winson3e874742016-01-07 10:08:17 -0800528 new TaskViewAnimation(125, PhoneStatusBar.ALPHA_OUT,
Winsonf24f2162016-01-05 12:11:55 -0800529 new AnimatorListenerAdapter() {
530 @Override
531 public void onAnimationEnd(Animator animation) {
Winson3e874742016-01-07 10:08:17 -0800532 // Dock the task and launch it
533 SystemServicesProxy ssp = Recents.getSystemServices();
534 ssp.startTaskInDockedMode(getContext(), event.task.key.id,
535 dockState.createMode);
536 launchTask(event.task, null, INVALID_STACK_ID);
537
Winsonf24f2162016-01-05 12:11:55 -0800538 mTaskStackView.getStack().removeTask(event.task);
539 }
540 }));
Winsona0a8ae42015-12-01 10:43:02 -0800541
Jorim Jaggi92de6b62015-12-09 15:32:30 -0800542
543 MetricsLogger.action(mContext,
544 MetricsLogger.ACTION_WINDOW_DOCK_DRAG_DROP);
Winson3e874742016-01-07 10:08:17 -0800545 } else {
546 // Animate the overlay alpha back to 0
547 updateVisibleDockRegions(null, true /* isDefaultDockState */, -1,
548 true /* animateAlpha */, false /* animateBounds */);
Winsonbe7607a2015-10-01 17:24:51 -0700549 }
550 }
Winson4165d3362015-10-10 14:40:35 -0700551
Jorim Jaggidd98d412015-11-18 15:57:38 -0800552 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800553 if (mTaskStackView.getTaskViews().size() > 0) {
554 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
555 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800556 }
557
558 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800559 ViewPropertyAnimator animator = animate();
Jorim Jaggi8fccfe22015-12-03 16:58:08 -0800560 if (event.velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800561 animator.translationY(getHeight());
562 animator.withEndAction(new Runnable() {
563 @Override
564 public void run() {
565 WindowManagerProxy.getInstance().maximizeDockedStack();
566 }
567 });
568 mFlingAnimationUtils.apply(animator, getTranslationY(), getHeight(), event.velocity);
569 } else {
570 animator.translationY(0f);
571 animator.setListener(null);
572 mFlingAnimationUtils.apply(animator, getTranslationY(), 0, event.velocity);
573 }
574 animator.start();
Jorim Jaggidd98d412015-11-18 15:57:38 -0800575 }
576
Winsonc29ff002015-11-20 16:00:45 -0800577 public final void onBusEvent(ShowHistoryEvent event) {
578 // Hide the history button when the history view is shown
Winson Chung9a742902015-12-11 10:25:40 -0500579 hideHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500580 event.getAnimationTrigger());
581 event.addPostAnimationCallback(new Runnable() {
Winson Chung9a742902015-12-11 10:25:40 -0500582 @Override
583 public void run() {
584 setAlpha(0f);
585 }
586 });
Winsonc29ff002015-11-20 16:00:45 -0800587 }
588
589 public final void onBusEvent(HideHistoryEvent event) {
590 // Show the history button when the history view is hidden
Winson Chung9a742902015-12-11 10:25:40 -0500591 setAlpha(1f);
592 showHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500593 event.getAnimationTrigger());
Winsonc29ff002015-11-20 16:00:45 -0800594 }
595
596 public final void onBusEvent(ShowHistoryButtonEvent event) {
597 showHistoryButton(150);
598 }
599
600 public final void onBusEvent(HideHistoryButtonEvent event) {
601 hideHistoryButton(100);
602 }
603
Winson Chung06266772015-12-11 10:24:21 -0500604 public final void onBusEvent(TaskStackUpdatedEvent event) {
605 mStack.setTasks(event.stack.computeAllTasksList(), true /* notifyStackChanges */);
Winson Chung97567552015-12-16 17:07:19 -0500606 mStack.createAffiliatedGroupings(getContext());
Winson Chung06266772015-12-11 10:24:21 -0500607 }
608
Winsonc29ff002015-11-20 16:00:45 -0800609 /**
610 * Shows the history button.
611 */
Winson Chung9a742902015-12-11 10:25:40 -0500612 private void showHistoryButton(final int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500613 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500614 showHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500615 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500616 }
617
618 private void showHistoryButton(final int duration,
619 final ReferenceCountedTrigger postHideHistoryAnimationTrigger) {
Winson Chungde750de2015-12-11 10:26:06 -0500620 mHistoryButton.setText(getContext().getString(R.string.recents_history_label_format,
621 mStack.getHistoricalTasks().size()));
Winsonf24f2162016-01-05 12:11:55 -0800622 if (mHistoryButton.getVisibility() == View.INVISIBLE) {
623 mHistoryButton.setVisibility(View.VISIBLE);
624 mHistoryButton.setAlpha(0f);
625 postHideHistoryAnimationTrigger.addLastDecrementRunnable(new Runnable() {
626 @Override
627 public void run() {
628 mHistoryButton.animate()
629 .alpha(1f)
630 .setDuration(duration)
631 .setInterpolator(mFastOutSlowInInterpolator)
632 .withLayer()
633 .start();
634 }
635 });
636 }
Winsonc29ff002015-11-20 16:00:45 -0800637 }
638
639 /**
640 * Hides the history button.
641 */
642 private void hideHistoryButton(int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500643 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500644 hideHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500645 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500646 }
647
648 private void hideHistoryButton(int duration,
649 final ReferenceCountedTrigger postHideStackAnimationTrigger) {
Winsonf24f2162016-01-05 12:11:55 -0800650 if (mHistoryButton.getVisibility() == View.VISIBLE) {
651 mHistoryButton.animate()
652 .alpha(0f)
653 .setDuration(duration)
654 .setInterpolator(mFastOutLinearInInterpolator)
655 .withEndAction(new Runnable() {
656 @Override
657 public void run() {
658 mHistoryButton.setVisibility(View.INVISIBLE);
659 postHideStackAnimationTrigger.decrement();
660 }
661 })
662 .withLayer()
663 .start();
664 postHideStackAnimationTrigger.increment();
665 }
Winsonc29ff002015-11-20 16:00:45 -0800666 }
667
Winson4165d3362015-10-10 14:40:35 -0700668 /**
669 * Updates the dock region to match the specified dock state.
670 */
Winson3e874742016-01-07 10:08:17 -0800671 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates,
672 boolean isDefaultDockState, int overrideAlpha, boolean animateAlpha,
673 boolean animateBounds) {
Winson882072b2015-10-12 11:26:33 -0700674 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
675 if (newDockStates != null) {
Winsondc8de842016-01-06 15:21:41 -0800676 Collections.addAll(newDockStatesSet, newDockStates);
Winson4165d3362015-10-10 14:40:35 -0700677 }
Winson882072b2015-10-12 11:26:33 -0700678 for (TaskStack.DockState dockState : mVisibleDockStates) {
679 TaskStack.DockState.ViewState viewState = dockState.viewState;
680 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
681 // This is no longer visible, so hide it
Winson3e874742016-01-07 10:08:17 -0800682 viewState.startAnimation(null, 0, DOCK_AREA_OVERLAY_TRANSITION_DURATION,
683 PhoneStatusBar.ALPHA_OUT, animateAlpha, animateBounds);
Winson882072b2015-10-12 11:26:33 -0700684 } else {
685 // This state is now visible, update the bounds and show it
686 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
Winson3e874742016-01-07 10:08:17 -0800687 Rect bounds = isDefaultDockState
688 ? dockState.getPreDockedBounds(getMeasuredWidth(), getMeasuredHeight())
689 : dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight(),
690 mDividerSize, mSystemInsets, getResources());
691 if (viewState.dockAreaOverlay.getCallback() != this) {
692 viewState.dockAreaOverlay.setCallback(this);
693 viewState.dockAreaOverlay.setBounds(bounds);
694 }
695 viewState.startAnimation(bounds, alpha, DOCK_AREA_OVERLAY_TRANSITION_DURATION,
696 PhoneStatusBar.ALPHA_IN, animateAlpha, animateBounds);
Winson882072b2015-10-12 11:26:33 -0700697 }
Winson4165d3362015-10-10 14:40:35 -0700698 }
Winson4165d3362015-10-10 14:40:35 -0700699 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800700
Winsonb1e71d02015-11-23 12:40:23 -0800701 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
702 if (!event.visible) {
703 // Reset the view state
704 mAwaitingFirstLayout = true;
705 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800706 }
707 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800708}