blob: e28e2b383aba400ef7c65133aa1c3b45672c5924 [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;
Winson Chung303e1ff2014-03-07 15:06:19 -080062import com.android.systemui.recents.model.Task;
63import com.android.systemui.recents.model.TaskStack;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -080064import com.android.systemui.stackdivider.WindowManagerProxy;
65import com.android.systemui.statusbar.FlingAnimationUtils;
Winson Chung303e1ff2014-03-07 15:06:19 -080066
67import java.util.ArrayList;
Winsondc8de842016-01-06 15:21:41 -080068import java.util.Collections;
Winson Chung6ac8bd62015-01-07 16:38:35 -080069import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080070
Wale Ogunwale3797c222015-10-27 14:21:58 -070071import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070072
Winson Chung303e1ff2014-03-07 15:06:19 -080073/**
74 * This view is the the top level layout that contains TaskStacks (which are laid out according
75 * to their SpaceNode bounds.
76 */
Winson Chung48f2cda2015-12-11 13:20:12 -050077public class RecentsView extends FrameLayout {
Winson Chung47c4c692014-03-17 10:17:11 -070078
Jorim Jaggi6e18e002015-06-02 17:07:39 -070079 private static final String TAG = "RecentsView";
Winson0d14d4d2015-10-26 17:05:04 -070080 private static final boolean DEBUG = false;
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();
Winsonbe7607a2015-10-01 17:24:51 -070092
Winson Chung9a742902015-12-11 10:25:40 -050093 private RecentsTransitionHelper mTransitionHelper;
94 private RecentsViewTouchHandler mTouchHandler;
95 private TaskStack.DockState[] mVisibleDockStates = {
Winson882072b2015-10-12 11:26:33 -070096 TaskStack.DockState.LEFT,
97 TaskStack.DockState.TOP,
98 TaskStack.DockState.RIGHT,
99 TaskStack.DockState.BOTTOM,
100 };
Winsonbe7607a2015-10-01 17:24:51 -0700101
Winson Chung9a742902015-12-11 10:25:40 -0500102 private final Interpolator mFastOutSlowInInterpolator;
103 private final Interpolator mFastOutLinearInInterpolator;
104 private final FlingAnimationUtils mFlingAnimationUtils;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800105
Winson Chung303e1ff2014-03-07 15:06:19 -0800106 public RecentsView(Context context) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800107 this(context, null);
Winson Chung8e548f72014-06-24 14:40:53 -0700108 }
109
110 public RecentsView(Context context, AttributeSet attrs) {
111 this(context, attrs, 0);
112 }
113
114 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
115 this(context, attrs, defStyleAttr, 0);
116 }
117
118 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
119 super(context, attrs, defStyleAttr, defStyleRes);
Winsonbe7607a2015-10-01 17:24:51 -0700120 setWillNotDraw(false);
Winson83c1b072015-11-09 10:48:04 -0800121 mHandler = new Handler();
122 mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
Winson35f30502015-09-28 11:24:36 -0700123 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
124 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonc29ff002015-11-20 16:00:45 -0800125 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
126 com.android.internal.R.interpolator.fast_out_linear_in);
Winsonbe7607a2015-10-01 17:24:51 -0700127 mTouchHandler = new RecentsViewTouchHandler(this);
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800128 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.3f);
Winsonc29ff002015-11-20 16:00:45 -0800129
130 LayoutInflater inflater = LayoutInflater.from(context);
Winson Chungde750de2015-12-11 10:26:06 -0500131 mHistoryButton = (TextView) inflater.inflate(R.layout.recents_history_button, this, false);
Winsonc29ff002015-11-20 16:00:45 -0800132 mHistoryButton.setOnClickListener(new View.OnClickListener() {
133 @Override
134 public void onClick(View v) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500135 EventBus.getDefault().send(new ShowHistoryEvent());
Winsonc29ff002015-11-20 16:00:45 -0800136 }
137 });
Winson Chung9a742902015-12-11 10:25:40 -0500138 addView(mHistoryButton);
139 mEmptyView = inflater.inflate(R.layout.recents_empty, this, false);
140 addView(mEmptyView);
Winson Chungd16c5652015-01-26 16:11:07 -0800141 }
142
Winson Chungdcfa7972014-07-22 12:27:13 -0700143 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700144 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700145 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung9a742902015-12-11 10:25:40 -0500146 RecentsActivityLaunchState launchState = config.getLaunchState();
Winsonc29ff002015-11-20 16:00:45 -0800147 mStack = stack;
Winson55e20d52016-01-06 15:21:29 -0800148 if (launchState.launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700149 if (mTaskStackView != null) {
150 // If onRecentsHidden is not triggered, we need to the stack view again here
151 mTaskStackView.reset();
152 mTaskStackView.setStack(stack);
153 } else {
154 mTaskStackView = new TaskStackView(getContext(), stack);
Winson147ecaf2015-09-16 16:49:55 -0700155 addView(mTaskStackView);
156 }
157 } else {
158 if (mTaskStackView != null) {
159 removeView(mTaskStackView);
160 }
161 mTaskStackView = new TaskStackView(getContext(), stack);
Winson147ecaf2015-09-16 16:49:55 -0700162 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800163 }
Winson Chung9a742902015-12-11 10:25:40 -0500164
165 // Update the top level view's visibilities
166 if (stack.getStackTaskCount() > 0) {
167 hideEmptyView();
Winsonc29ff002015-11-20 16:00:45 -0800168 } else {
Winson Chung9a742902015-12-11 10:25:40 -0500169 showEmptyView();
Winsonc29ff002015-11-20 16:00:45 -0800170 }
Winson Chung02d49272014-08-29 13:57:29 -0700171
Winson Chungb0a28ea2014-10-28 15:21:35 -0700172 // Trigger a new layout
173 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700174 }
175
Winsonb1e71d02015-11-23 12:40:23 -0800176 /**
177 * Returns whether the last task launched was in the freeform stack or not.
178 */
179 public boolean isLastTaskLaunchedFreeform() {
180 return mLastTaskLaunchedWasFreeform;
181 }
182
Winsonc29ff002015-11-20 16:00:45 -0800183 /**
184 * Returns the currently set task stack.
185 */
186 public TaskStack getTaskStack() {
187 return mStack;
188 }
189
Skuhne8aa7d162015-03-20 13:40:53 -0700190 /** Gets the next task in the stack - or if the last - the top task */
191 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700192 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700193 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700194 if (mTaskStackView != null) {
195 TaskStack stack = mTaskStackView.getStack();
Winson250608a2015-11-24 15:00:31 -0800196 ArrayList<Task> taskList = stack.getStackTasks();
Skuhne8aa7d162015-03-20 13:40:53 -0700197 // Iterate the stack views and try and find the focused task
198 for (int j = taskList.size() - 1; j >= 0; --j) {
199 Task task = taskList.get(j);
200 // Return the next task in the line.
201 if (found)
202 return task;
203 // Remember the first possible task as the top task.
204 if (returnTask == null)
205 returnTask = task;
206 if (task == taskToSearch)
207 found = true;
208 }
209 }
210 return returnTask;
211 }
212
Winson Chung1e8d71b2014-05-16 17:05:22 -0700213 /** Launches the focused task from the first stack if possible */
214 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700215 if (mTaskStackView != null) {
Winson142af422015-11-09 10:39:57 -0800216 Task task = mTaskStackView.getFocusedTask();
217 if (task != null) {
218 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson Chung48f2cda2015-12-11 13:20:12 -0500219 EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
220 INVALID_STACK_ID, false));
Winson142af422015-11-09 10:39:57 -0800221 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700222 }
223 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700224 return false;
225 }
226
Winsona0731a12015-12-02 15:10:14 -0800227 /** Launches the task that recents was launched from if possible */
228 public boolean launchPreviousTask() {
229 if (mTaskStackView != null) {
230 TaskStack stack = mTaskStackView.getStack();
231 Task task = stack.getLaunchTarget();
232 if (task != null) {
233 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson Chung48f2cda2015-12-11 13:20:12 -0500234 EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
235 INVALID_STACK_ID, false));
Winsona0731a12015-12-02 15:10:14 -0800236 return true;
237 }
238 }
239 return false;
240 }
241
Skuhne8aa7d162015-03-20 13:40:53 -0700242 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700243 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700244 if (mTaskStackView != null) {
Skuhne8aa7d162015-03-20 13:40:53 -0700245 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700246 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700247 int taskViewCount = taskViews.size();
248 for (int j = 0; j < taskViewCount; j++) {
249 TaskView tv = taskViews.get(j);
250 if (tv.getTask() == task) {
Winson Chung48f2cda2015-12-11 13:20:12 -0500251 EventBus.getDefault().send(new LaunchTaskEvent(tv, task, taskBounds,
252 destinationStack, false));
Skuhne8aa7d162015-03-20 13:40:53 -0700253 return true;
254 }
255 }
256 }
257 return false;
258 }
259
Winson Chungf7bca432014-04-30 17:11:13 -0700260 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700261 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700262 // Remove the previous search bar if one exists
263 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
264 removeView(mSearchBar);
265 }
266 // Add the new search bar
267 if (searchBar != null) {
268 mSearchBar = searchBar;
269 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700270 }
Winson Chungecd9b302014-04-16 17:07:18 -0700271 }
272
Winson Chung772b6b12014-07-03 15:54:02 -0700273 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700274 public boolean hasValidSearchBar() {
275 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700276 }
277
Winson Chung9a742902015-12-11 10:25:40 -0500278 /**
279 * Hides the task stack and shows the empty view.
280 */
281 public void showEmptyView() {
Winsond8b1d632016-01-04 17:51:18 -0800282 if (RecentsDebugFlags.Static.EnableSearchBar && (mSearchBar != null)) {
Winson Chung9a742902015-12-11 10:25:40 -0500283 mSearchBar.setVisibility(View.INVISIBLE);
284 }
285 mTaskStackView.setVisibility(View.INVISIBLE);
286 mEmptyView.setVisibility(View.VISIBLE);
287 mEmptyView.bringToFront();
288 mHistoryButton.bringToFront();
289 }
290
291 /**
292 * Shows the task stack and hides the empty view.
293 */
294 public void hideEmptyView() {
295 mEmptyView.setVisibility(View.INVISIBLE);
296 mTaskStackView.setVisibility(View.VISIBLE);
Winsond8b1d632016-01-04 17:51:18 -0800297 if (RecentsDebugFlags.Static.EnableSearchBar && (mSearchBar != null)) {
Winson Chung9a742902015-12-11 10:25:40 -0500298 mSearchBar.setVisibility(View.VISIBLE);
299 }
300 mTaskStackView.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700301 if (mSearchBar != null) {
Winson Chung012ef362014-07-31 18:36:25 -0700302 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700303 }
Winson Chung9a742902015-12-11 10:25:40 -0500304 mHistoryButton.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700305 }
306
Winsonc29ff002015-11-20 16:00:45 -0800307 /**
308 * Returns the last known system insets.
309 */
310 public Rect getSystemInsets() {
311 return mSystemInsets;
312 }
313
Winsonbe7607a2015-10-01 17:24:51 -0700314 @Override
315 protected void onAttachedToWindow() {
316 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
317 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
318 super.onAttachedToWindow();
319 }
320
321 @Override
322 protected void onDetachedFromWindow() {
323 super.onDetachedFromWindow();
324 EventBus.getDefault().unregister(this);
325 EventBus.getDefault().unregister(mTouchHandler);
326 }
327
Winson Chungf7bca432014-04-30 17:11:13 -0700328 /**
329 * This is called with the full size of the window since we are handling our own insets.
330 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800331 @Override
332 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700333 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800334 int width = MeasureSpec.getSize(widthMeasureSpec);
335 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700336
Winson Chungf7bca432014-04-30 17:11:13 -0700337 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700338 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700339 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700340 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700341 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700342 mSearchBar.measure(
343 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
344 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700345 }
346
Winson Chungf7bca432014-04-30 17:11:13 -0700347 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700348 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700349 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700350 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700351 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700352 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800353 }
354
Winson Chung9a742902015-12-11 10:25:40 -0500355 // Measure the empty view
356 measureChild(mEmptyView, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
357 MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
358
Winson250608a2015-11-24 15:00:31 -0800359 // Measure the history button with the full space above the stack, but width-constrained
360 // to the stack
Winson387aac62015-11-25 11:18:56 -0800361 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
Winsonc29ff002015-11-20 16:00:45 -0800362 measureChild(mHistoryButton,
Winson387aac62015-11-25 11:18:56 -0800363 MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
364 MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
Winson250608a2015-11-24 15:00:31 -0800365 MeasureSpec.EXACTLY));
Winson Chung9a742902015-12-11 10:25:40 -0500366
Winson Chung303e1ff2014-03-07 15:06:19 -0800367 setMeasuredDimension(width, height);
368 }
369
Winson Chungf7bca432014-04-30 17:11:13 -0700370 /**
371 * This is called with the full size of the window since we are handling our own insets.
372 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800373 @Override
374 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700375 RecentsConfiguration config = Recents.getConfiguration();
376
Winson Chungf7bca432014-04-30 17:11:13 -0700377 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700378 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
379 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700380 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700381 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700382 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700383 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
384 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700385 }
386
Winson147ecaf2015-09-16 16:49:55 -0700387 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
388 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800389 }
Winsonbe7607a2015-10-01 17:24:51 -0700390
Winson Chung9a742902015-12-11 10:25:40 -0500391 // Layout the empty view
392 mEmptyView.layout(left, top, right, bottom);
393
Winson250608a2015-11-24 15:00:31 -0800394 // Layout the history button left-aligned with the stack, but offset from the top of the
395 // view
Winson387aac62015-11-25 11:18:56 -0800396 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
397 mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
398 historyButtonRect.right, historyButtonRect.bottom);
Winsonc29ff002015-11-20 16:00:45 -0800399
Winsonb1e71d02015-11-23 12:40:23 -0800400 if (mAwaitingFirstLayout) {
401 mAwaitingFirstLayout = false;
402
403 // If launched via dragging from the nav bar, then we should translate the whole view
404 // down offscreen
405 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
406 if (launchState.launchedViaDragGesture) {
407 setTranslationY(getMeasuredHeight());
Jorim Jaggi0ca88262015-12-07 17:15:16 -0800408 } else {
409 setTranslationY(0f);
Winsonb1e71d02015-11-23 12:40:23 -0800410 }
411 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800412 }
413
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700414 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700415 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700416 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700417 requestLayout();
Winsonc29ff002015-11-20 16:00:45 -0800418 return insets;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700419 }
420
Winsonbe7607a2015-10-01 17:24:51 -0700421 @Override
422 public boolean onInterceptTouchEvent(MotionEvent ev) {
423 return mTouchHandler.onInterceptTouchEvent(ev);
424 }
425
426 @Override
427 public boolean onTouchEvent(MotionEvent ev) {
428 return mTouchHandler.onTouchEvent(ev);
429 }
430
431 @Override
432 protected void dispatchDraw(Canvas canvas) {
433 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700434 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
435 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
436 if (d.getAlpha() > 0) {
437 d.draw(canvas);
438 }
Winsonbe7607a2015-10-01 17:24:51 -0700439 }
440 }
441
Winson4165d3362015-10-10 14:40:35 -0700442 @Override
443 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700444 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
445 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
446 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(),
473 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700474 }
475
Winsoneca4ab62015-11-04 10:50:28 -0800476 public final void onBusEvent(DragDropTargetChangedEvent event) {
477 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700478 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
479 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
480 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800481 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
482 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700483 }
Winsonbe7607a2015-10-01 17:24:51 -0700484 }
485
486 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800487 // Animate the overlay alpha back to 0
488 updateVisibleDockRegions(null, -1);
489
Winson479f7442015-11-25 15:16:27 -0800490 // Handle the case where we drop onto a dock region
491 if (event.dropTarget instanceof TaskStack.DockState) {
Winsonf24f2162016-01-05 12:11:55 -0800492 TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
493 TaskStackLayoutAlgorithm stackLayout = mTaskStackView.getStackAlgorithm();
494 TaskStackViewScroller stackScroller = mTaskStackView.getScroller();
495 TaskViewTransform tmpTransform = new TaskViewTransform();
Winsoneca4ab62015-11-04 10:50:28 -0800496
Winsonf24f2162016-01-05 12:11:55 -0800497 // Remove the task view after it is docked
498 stackLayout.getStackTransform(event.task, stackScroller.getStackScroll(), tmpTransform,
499 null);
500 tmpTransform.scale = event.taskView.getScaleX();
501 tmpTransform.rect.offset(event.taskView.getTranslationX(),
502 event.taskView.getTranslationY());
503 mTaskStackView.updateTaskViewToTransform(event.taskView, tmpTransform,
504 new TaskViewAnimation(150, mFastOutLinearInInterpolator,
505 new AnimatorListenerAdapter() {
506 @Override
507 public void onAnimationEnd(Animator animation) {
508 mTaskStackView.getStack().removeTask(event.task);
509 }
510 }));
Winsona0a8ae42015-12-01 10:43:02 -0800511
Winsoneca4ab62015-11-04 10:50:28 -0800512 // Dock the task and launch it
513 SystemServicesProxy ssp = Recents.getSystemServices();
514 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
515 launchTask(event.task, null, INVALID_STACK_ID);
Jorim Jaggi92de6b62015-12-09 15:32:30 -0800516
517 MetricsLogger.action(mContext,
518 MetricsLogger.ACTION_WINDOW_DOCK_DRAG_DROP);
Winsonbe7607a2015-10-01 17:24:51 -0700519 }
520 }
Winson4165d3362015-10-10 14:40:35 -0700521
Jorim Jaggidd98d412015-11-18 15:57:38 -0800522 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800523 if (mTaskStackView.getTaskViews().size() > 0) {
524 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
525 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800526 }
527
528 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800529 ViewPropertyAnimator animator = animate();
Jorim Jaggi8fccfe22015-12-03 16:58:08 -0800530 if (event.velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800531 animator.translationY(getHeight());
532 animator.withEndAction(new Runnable() {
533 @Override
534 public void run() {
535 WindowManagerProxy.getInstance().maximizeDockedStack();
536 }
537 });
538 mFlingAnimationUtils.apply(animator, getTranslationY(), getHeight(), event.velocity);
539 } else {
540 animator.translationY(0f);
541 animator.setListener(null);
542 mFlingAnimationUtils.apply(animator, getTranslationY(), 0, event.velocity);
543 }
544 animator.start();
Jorim Jaggidd98d412015-11-18 15:57:38 -0800545 }
546
Winsonc29ff002015-11-20 16:00:45 -0800547 public final void onBusEvent(ShowHistoryEvent event) {
548 // Hide the history button when the history view is shown
Winson Chung9a742902015-12-11 10:25:40 -0500549 hideHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500550 event.getAnimationTrigger());
551 event.addPostAnimationCallback(new Runnable() {
Winson Chung9a742902015-12-11 10:25:40 -0500552 @Override
553 public void run() {
554 setAlpha(0f);
555 }
556 });
Winsonc29ff002015-11-20 16:00:45 -0800557 }
558
559 public final void onBusEvent(HideHistoryEvent event) {
560 // Show the history button when the history view is hidden
Winson Chung9a742902015-12-11 10:25:40 -0500561 setAlpha(1f);
562 showHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500563 event.getAnimationTrigger());
Winsonc29ff002015-11-20 16:00:45 -0800564 }
565
566 public final void onBusEvent(ShowHistoryButtonEvent event) {
567 showHistoryButton(150);
568 }
569
570 public final void onBusEvent(HideHistoryButtonEvent event) {
571 hideHistoryButton(100);
572 }
573
Winson Chung06266772015-12-11 10:24:21 -0500574 public final void onBusEvent(TaskStackUpdatedEvent event) {
575 mStack.setTasks(event.stack.computeAllTasksList(), true /* notifyStackChanges */);
Winson Chung97567552015-12-16 17:07:19 -0500576 mStack.createAffiliatedGroupings(getContext());
Winson Chung06266772015-12-11 10:24:21 -0500577 }
578
Winsonc29ff002015-11-20 16:00:45 -0800579 /**
580 * Shows the history button.
581 */
Winson Chung9a742902015-12-11 10:25:40 -0500582 private void showHistoryButton(final int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500583 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500584 showHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500585 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500586 }
587
588 private void showHistoryButton(final int duration,
589 final ReferenceCountedTrigger postHideHistoryAnimationTrigger) {
Winson Chungde750de2015-12-11 10:26:06 -0500590 mHistoryButton.setText(getContext().getString(R.string.recents_history_label_format,
591 mStack.getHistoricalTasks().size()));
Winsonf24f2162016-01-05 12:11:55 -0800592 if (mHistoryButton.getVisibility() == View.INVISIBLE) {
593 mHistoryButton.setVisibility(View.VISIBLE);
594 mHistoryButton.setAlpha(0f);
595 postHideHistoryAnimationTrigger.addLastDecrementRunnable(new Runnable() {
596 @Override
597 public void run() {
598 mHistoryButton.animate()
599 .alpha(1f)
600 .setDuration(duration)
601 .setInterpolator(mFastOutSlowInInterpolator)
602 .withLayer()
603 .start();
604 }
605 });
606 }
Winsonc29ff002015-11-20 16:00:45 -0800607 }
608
609 /**
610 * Hides the history button.
611 */
612 private void hideHistoryButton(int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500613 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500614 hideHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500615 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500616 }
617
618 private void hideHistoryButton(int duration,
619 final ReferenceCountedTrigger postHideStackAnimationTrigger) {
Winsonf24f2162016-01-05 12:11:55 -0800620 if (mHistoryButton.getVisibility() == View.VISIBLE) {
621 mHistoryButton.animate()
622 .alpha(0f)
623 .setDuration(duration)
624 .setInterpolator(mFastOutLinearInInterpolator)
625 .withEndAction(new Runnable() {
626 @Override
627 public void run() {
628 mHistoryButton.setVisibility(View.INVISIBLE);
629 postHideStackAnimationTrigger.decrement();
630 }
631 })
632 .withLayer()
633 .start();
634 postHideStackAnimationTrigger.increment();
635 }
Winsonc29ff002015-11-20 16:00:45 -0800636 }
637
Winson4165d3362015-10-10 14:40:35 -0700638 /**
639 * Updates the dock region to match the specified dock state.
640 */
Winson882072b2015-10-12 11:26:33 -0700641 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
642 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
643 if (newDockStates != null) {
Winsondc8de842016-01-06 15:21:41 -0800644 Collections.addAll(newDockStatesSet, newDockStates);
Winson4165d3362015-10-10 14:40:35 -0700645 }
Winson882072b2015-10-12 11:26:33 -0700646 for (TaskStack.DockState dockState : mVisibleDockStates) {
647 TaskStack.DockState.ViewState viewState = dockState.viewState;
648 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
649 // This is no longer visible, so hide it
650 viewState.startAlphaAnimation(0, 150);
651 } else {
652 // This state is now visible, update the bounds and show it
653 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
654 viewState.dockAreaOverlay.setBounds(
655 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
656 viewState.dockAreaOverlay.setCallback(this);
657 viewState.startAlphaAnimation(alpha, 150);
658 }
Winson4165d3362015-10-10 14:40:35 -0700659 }
Winson4165d3362015-10-10 14:40:35 -0700660 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800661
Winsonb1e71d02015-11-23 12:40:23 -0800662 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
663 if (!event.visible) {
664 // Reset the view state
665 mAwaitingFirstLayout = true;
666 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800667 }
668 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800669}