blob: c4e5267c2aaf1c816f9bfbd456fda970f2a47f7f [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;
Winsonc29ff002015-11-20 16:00:45 -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;
Jorim Jaggi92de6b62015-12-09 15:32:30 -080037import com.android.internal.logging.MetricsLogger;
Winson Chungde750de2015-12-11 10:26:06 -050038import android.widget.TextView;
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;
Winsonc29ff002015-11-20 16:00:45 -080048import com.android.systemui.recents.events.activity.DebugFlagsChangedEvent;
Winson412e1802015-10-20 16:57:57 -070049import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
Winsonc29ff002015-11-20 16:00:45 -080050import com.android.systemui.recents.events.activity.HideHistoryButtonEvent;
51import com.android.systemui.recents.events.activity.HideHistoryEvent;
Winson Chung48f2cda2015-12-11 13:20:12 -050052import com.android.systemui.recents.events.activity.LaunchTaskEvent;
Winsonc29ff002015-11-20 16:00:45 -080053import com.android.systemui.recents.events.activity.ShowHistoryButtonEvent;
54import com.android.systemui.recents.events.activity.ShowHistoryEvent;
Winson Chung06266772015-12-11 10:24:21 -050055import com.android.systemui.recents.events.activity.TaskStackUpdatedEvent;
Winsonb1e71d02015-11-23 12:40:23 -080056import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
Jorim Jaggidd98d412015-11-18 15:57:38 -080057import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
58import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
Winsoneca4ab62015-11-04 10:50:28 -080059import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
Winsonbe7607a2015-10-01 17:24:51 -070060import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
61import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
Winson Chung9a742902015-12-11 10:25:40 -050062import com.android.systemui.recents.misc.ReferenceCountedTrigger;
Winson Chung1f24c7e2014-07-11 17:06:48 -070063import com.android.systemui.recents.misc.SystemServicesProxy;
Winsonf24f2162016-01-05 12:11:55 -080064import com.android.systemui.recents.misc.Utilities;
Winson Chung303e1ff2014-03-07 15:06:19 -080065import com.android.systemui.recents.model.Task;
66import com.android.systemui.recents.model.TaskStack;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -080067import com.android.systemui.stackdivider.WindowManagerProxy;
68import com.android.systemui.statusbar.FlingAnimationUtils;
Winson Chung303e1ff2014-03-07 15:06:19 -080069
70import java.util.ArrayList;
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
Jorim Jaggi6e18e002015-06-02 17:07:39 -070081 private static final String TAG = "RecentsView";
Winson0d14d4d2015-10-26 17:05:04 -070082 private static final boolean DEBUG = false;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070083
Winson Chung9a742902015-12-11 10:25:40 -050084 private final Handler mHandler;
Winson Chungd42a6cf2014-06-03 16:24:04 -070085
Winson Chung9a742902015-12-11 10:25:40 -050086 private TaskStack mStack;
87 private TaskStackView mTaskStackView;
88 private RecentsAppWidgetHostView mSearchBar;
Winson Chungde750de2015-12-11 10:26:06 -050089 private TextView mHistoryButton;
Winson Chung9a742902015-12-11 10:25:40 -050090 private View mEmptyView;
91 private boolean mAwaitingFirstLayout = true;
92 private boolean mLastTaskLaunchedWasFreeform;
93 private Rect mSystemInsets = new Rect();
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);
Winson83c1b072015-11-09 10:48:04 -0800123 mHandler = new Handler();
124 mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
Winson35f30502015-09-28 11:24:36 -0700125 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
126 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonc29ff002015-11-20 16:00:45 -0800127 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
128 com.android.internal.R.interpolator.fast_out_linear_in);
Winsonbe7607a2015-10-01 17:24:51 -0700129 mTouchHandler = new RecentsViewTouchHandler(this);
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800130 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.3f);
Winsonc29ff002015-11-20 16:00:45 -0800131
132 LayoutInflater inflater = LayoutInflater.from(context);
Winson Chungde750de2015-12-11 10:26:06 -0500133 mHistoryButton = (TextView) inflater.inflate(R.layout.recents_history_button, this, false);
Winsonc29ff002015-11-20 16:00:45 -0800134 mHistoryButton.setOnClickListener(new View.OnClickListener() {
135 @Override
136 public void onClick(View v) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500137 EventBus.getDefault().send(new ShowHistoryEvent());
Winsonc29ff002015-11-20 16:00:45 -0800138 }
139 });
Winson Chung9a742902015-12-11 10:25:40 -0500140 addView(mHistoryButton);
141 mEmptyView = inflater.inflate(R.layout.recents_empty, this, false);
142 addView(mEmptyView);
Winson Chungd16c5652015-01-26 16:11:07 -0800143 }
144
Winson Chungdcfa7972014-07-22 12:27:13 -0700145 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700146 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700147 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung9a742902015-12-11 10:25:40 -0500148 RecentsActivityLaunchState launchState = config.getLaunchState();
Winsonc29ff002015-11-20 16:00:45 -0800149 mStack = stack;
Winson Chungfca2a632015-12-11 15:51:18 -0500150 // Disable reusing task stack views until the visibility bug is fixed. b/25998134
Winson Chung9a742902015-12-11 10:25:40 -0500151 if (false && launchState.launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700152 if (mTaskStackView != null) {
153 // If onRecentsHidden is not triggered, we need to the stack view again here
154 mTaskStackView.reset();
155 mTaskStackView.setStack(stack);
Winson160f4c02015-12-02 19:06:44 -0800156 removeView(mTaskStackView);
157 addView(mTaskStackView);
Winson147ecaf2015-09-16 16:49:55 -0700158 } 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) {
221 TaskStack stack = mTaskStackView.getStack();
Winson142af422015-11-09 10:39:57 -0800222 Task task = mTaskStackView.getFocusedTask();
223 if (task != null) {
224 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson Chung48f2cda2015-12-11 13:20:12 -0500225 EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
226 INVALID_STACK_ID, false));
Winson142af422015-11-09 10:39:57 -0800227 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700228 }
229 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700230 return false;
231 }
232
Winsona0731a12015-12-02 15:10:14 -0800233 /** Launches the task that recents was launched from if possible */
234 public boolean launchPreviousTask() {
235 if (mTaskStackView != null) {
236 TaskStack stack = mTaskStackView.getStack();
237 Task task = stack.getLaunchTarget();
238 if (task != null) {
239 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson Chung48f2cda2015-12-11 13:20:12 -0500240 EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
241 INVALID_STACK_ID, false));
Winsona0731a12015-12-02 15:10:14 -0800242 return true;
243 }
244 }
245 return false;
246 }
247
Skuhne8aa7d162015-03-20 13:40:53 -0700248 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700249 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700250 if (mTaskStackView != null) {
251 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700252 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700253 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700254 int taskViewCount = taskViews.size();
255 for (int j = 0; j < taskViewCount; j++) {
256 TaskView tv = taskViews.get(j);
257 if (tv.getTask() == task) {
Winson Chung48f2cda2015-12-11 13:20:12 -0500258 EventBus.getDefault().send(new LaunchTaskEvent(tv, task, taskBounds,
259 destinationStack, false));
Skuhne8aa7d162015-03-20 13:40:53 -0700260 return true;
261 }
262 }
263 }
264 return false;
265 }
266
Winson Chungf7bca432014-04-30 17:11:13 -0700267 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700268 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700269 // Remove the previous search bar if one exists
270 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
271 removeView(mSearchBar);
272 }
273 // Add the new search bar
274 if (searchBar != null) {
275 mSearchBar = searchBar;
276 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700277 }
Winson Chungecd9b302014-04-16 17:07:18 -0700278 }
279
Winson Chung772b6b12014-07-03 15:54:02 -0700280 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700281 public boolean hasValidSearchBar() {
282 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700283 }
284
Winson Chung9a742902015-12-11 10:25:40 -0500285 /**
286 * Hides the task stack and shows the empty view.
287 */
288 public void showEmptyView() {
Winsond8b1d632016-01-04 17:51:18 -0800289 if (RecentsDebugFlags.Static.EnableSearchBar && (mSearchBar != null)) {
Winson Chung9a742902015-12-11 10:25:40 -0500290 mSearchBar.setVisibility(View.INVISIBLE);
291 }
292 mTaskStackView.setVisibility(View.INVISIBLE);
293 mEmptyView.setVisibility(View.VISIBLE);
294 mEmptyView.bringToFront();
295 mHistoryButton.bringToFront();
296 }
297
298 /**
299 * Shows the task stack and hides the empty view.
300 */
301 public void hideEmptyView() {
302 mEmptyView.setVisibility(View.INVISIBLE);
303 mTaskStackView.setVisibility(View.VISIBLE);
Winsond8b1d632016-01-04 17:51:18 -0800304 if (RecentsDebugFlags.Static.EnableSearchBar && (mSearchBar != null)) {
Winson Chung9a742902015-12-11 10:25:40 -0500305 mSearchBar.setVisibility(View.VISIBLE);
306 }
307 mTaskStackView.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700308 if (mSearchBar != null) {
Winson Chung012ef362014-07-31 18:36:25 -0700309 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700310 }
Winson Chung9a742902015-12-11 10:25:40 -0500311 mHistoryButton.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700312 }
313
Winsonc29ff002015-11-20 16:00:45 -0800314 /**
315 * Returns the last known system insets.
316 */
317 public Rect getSystemInsets() {
318 return mSystemInsets;
319 }
320
Winsonbe7607a2015-10-01 17:24:51 -0700321 @Override
322 protected void onAttachedToWindow() {
323 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
324 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
325 super.onAttachedToWindow();
326 }
327
328 @Override
329 protected void onDetachedFromWindow() {
330 super.onDetachedFromWindow();
331 EventBus.getDefault().unregister(this);
332 EventBus.getDefault().unregister(mTouchHandler);
333 }
334
Winson Chungf7bca432014-04-30 17:11:13 -0700335 /**
336 * This is called with the full size of the window since we are handling our own insets.
337 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800338 @Override
339 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700340 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800341 int width = MeasureSpec.getSize(widthMeasureSpec);
342 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700343
Winson Chungf7bca432014-04-30 17:11:13 -0700344 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700345 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700346 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700347 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700348 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700349 mSearchBar.measure(
350 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
351 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700352 }
353
Winson Chungf7bca432014-04-30 17:11:13 -0700354 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700355 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700356 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700357 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700358 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700359 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800360 }
361
Winson Chung9a742902015-12-11 10:25:40 -0500362 // Measure the empty view
363 measureChild(mEmptyView, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
364 MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
365
Winson250608a2015-11-24 15:00:31 -0800366 // Measure the history button with the full space above the stack, but width-constrained
367 // to the stack
Winson387aac62015-11-25 11:18:56 -0800368 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
Winsonc29ff002015-11-20 16:00:45 -0800369 measureChild(mHistoryButton,
Winson387aac62015-11-25 11:18:56 -0800370 MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
371 MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
Winson250608a2015-11-24 15:00:31 -0800372 MeasureSpec.EXACTLY));
Winson Chung9a742902015-12-11 10:25:40 -0500373
Winson Chung303e1ff2014-03-07 15:06:19 -0800374 setMeasuredDimension(width, height);
375 }
376
Winson Chungf7bca432014-04-30 17:11:13 -0700377 /**
378 * This is called with the full size of the window since we are handling our own insets.
379 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800380 @Override
381 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700382 RecentsConfiguration config = Recents.getConfiguration();
383
Winson Chungf7bca432014-04-30 17:11:13 -0700384 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700385 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
386 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700387 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700388 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700389 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700390 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
391 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700392 }
393
Winson147ecaf2015-09-16 16:49:55 -0700394 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
395 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800396 }
Winsonbe7607a2015-10-01 17:24:51 -0700397
Winson Chung9a742902015-12-11 10:25:40 -0500398 // Layout the empty view
399 mEmptyView.layout(left, top, right, bottom);
400
Winson250608a2015-11-24 15:00:31 -0800401 // Layout the history button left-aligned with the stack, but offset from the top of the
402 // view
Winson387aac62015-11-25 11:18:56 -0800403 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
404 mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
405 historyButtonRect.right, historyButtonRect.bottom);
Winsonc29ff002015-11-20 16:00:45 -0800406
Winsonb1e71d02015-11-23 12:40:23 -0800407 if (mAwaitingFirstLayout) {
408 mAwaitingFirstLayout = false;
409
410 // If launched via dragging from the nav bar, then we should translate the whole view
411 // down offscreen
412 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
413 if (launchState.launchedViaDragGesture) {
414 setTranslationY(getMeasuredHeight());
Jorim Jaggi0ca88262015-12-07 17:15:16 -0800415 } else {
416 setTranslationY(0f);
Winsonb1e71d02015-11-23 12:40:23 -0800417 }
418 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800419 }
420
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700421 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700422 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700423 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700424 requestLayout();
Winsonc29ff002015-11-20 16:00:45 -0800425 return insets;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700426 }
427
Winsonbe7607a2015-10-01 17:24:51 -0700428 @Override
429 public boolean onInterceptTouchEvent(MotionEvent ev) {
430 return mTouchHandler.onInterceptTouchEvent(ev);
431 }
432
433 @Override
434 public boolean onTouchEvent(MotionEvent ev) {
435 return mTouchHandler.onTouchEvent(ev);
436 }
437
438 @Override
439 protected void dispatchDraw(Canvas canvas) {
440 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700441 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
442 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
443 if (d.getAlpha() > 0) {
444 d.draw(canvas);
445 }
Winsonbe7607a2015-10-01 17:24:51 -0700446 }
447 }
448
Winson4165d3362015-10-10 14:40:35 -0700449 @Override
450 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700451 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
452 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
453 if (d == who) {
454 return true;
455 }
456 }
457 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700458 }
459
Winsonbe7607a2015-10-01 17:24:51 -0700460 /**** EventBus Events ****/
461
Winson Chung48f2cda2015-12-11 13:20:12 -0500462 public final void onBusEvent(LaunchTaskEvent event) {
463 mLastTaskLaunchedWasFreeform = event.task.isFreeformTask();
464 mTransitionHelper.launchTaskFromRecents(mStack, event.task, mTaskStackView, event.taskView,
465 event.screenPinningRequested, event.targetTaskBounds, event.targetTaskStack);
466 }
467
Winsonef064132016-01-05 12:11:31 -0800468 public final void onBusEvent(DismissRecentsToHomeAnimationStarted event) {
469 // Hide the history button
470 int taskViewExitToHomeDuration = getResources().getInteger(
471 R.integer.recents_task_exit_to_home_duration);
472 hideHistoryButton(taskViewExitToHomeDuration);
473
474 // If we are going home, cancel the previous task's window transition
475 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
476 }
477
Winsonbe7607a2015-10-01 17:24:51 -0700478 public final void onBusEvent(DragStartEvent event) {
Winson882072b2015-10-12 11:26:33 -0700479 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
480 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700481 }
482
Winsoneca4ab62015-11-04 10:50:28 -0800483 public final void onBusEvent(DragDropTargetChangedEvent event) {
484 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700485 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
486 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
487 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800488 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
489 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700490 }
Winsonbe7607a2015-10-01 17:24:51 -0700491 }
492
493 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800494 // Animate the overlay alpha back to 0
495 updateVisibleDockRegions(null, -1);
496
Winson479f7442015-11-25 15:16:27 -0800497 // Handle the case where we drop onto a dock region
498 if (event.dropTarget instanceof TaskStack.DockState) {
Winsonf24f2162016-01-05 12:11:55 -0800499 TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
500 TaskStackLayoutAlgorithm stackLayout = mTaskStackView.getStackAlgorithm();
501 TaskStackViewScroller stackScroller = mTaskStackView.getScroller();
502 TaskViewTransform tmpTransform = new TaskViewTransform();
Winsoneca4ab62015-11-04 10:50:28 -0800503
Winsonf24f2162016-01-05 12:11:55 -0800504 // Remove the task view after it is docked
505 stackLayout.getStackTransform(event.task, stackScroller.getStackScroll(), tmpTransform,
506 null);
507 tmpTransform.scale = event.taskView.getScaleX();
508 tmpTransform.rect.offset(event.taskView.getTranslationX(),
509 event.taskView.getTranslationY());
510 mTaskStackView.updateTaskViewToTransform(event.taskView, tmpTransform,
511 new TaskViewAnimation(150, mFastOutLinearInInterpolator,
512 new AnimatorListenerAdapter() {
513 @Override
514 public void onAnimationEnd(Animator animation) {
515 mTaskStackView.getStack().removeTask(event.task);
516 }
517 }));
Winsona0a8ae42015-12-01 10:43:02 -0800518
Winsoneca4ab62015-11-04 10:50:28 -0800519 // Dock the task and launch it
520 SystemServicesProxy ssp = Recents.getSystemServices();
521 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
522 launchTask(event.task, null, INVALID_STACK_ID);
Jorim Jaggi92de6b62015-12-09 15:32:30 -0800523
524 MetricsLogger.action(mContext,
525 MetricsLogger.ACTION_WINDOW_DOCK_DRAG_DROP);
Winsonbe7607a2015-10-01 17:24:51 -0700526 }
527 }
Winson4165d3362015-10-10 14:40:35 -0700528
Jorim Jaggidd98d412015-11-18 15:57:38 -0800529 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800530 if (mTaskStackView.getTaskViews().size() > 0) {
531 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
532 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800533 }
534
535 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800536 ViewPropertyAnimator animator = animate();
Jorim Jaggi8fccfe22015-12-03 16:58:08 -0800537 if (event.velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800538 animator.translationY(getHeight());
539 animator.withEndAction(new Runnable() {
540 @Override
541 public void run() {
542 WindowManagerProxy.getInstance().maximizeDockedStack();
543 }
544 });
545 mFlingAnimationUtils.apply(animator, getTranslationY(), getHeight(), event.velocity);
546 } else {
547 animator.translationY(0f);
548 animator.setListener(null);
549 mFlingAnimationUtils.apply(animator, getTranslationY(), 0, event.velocity);
550 }
551 animator.start();
Jorim Jaggidd98d412015-11-18 15:57:38 -0800552 }
553
Winsonc29ff002015-11-20 16:00:45 -0800554 public final void onBusEvent(ShowHistoryEvent event) {
555 // Hide the history button when the history view is shown
Winson Chung9a742902015-12-11 10:25:40 -0500556 hideHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500557 event.getAnimationTrigger());
558 event.addPostAnimationCallback(new Runnable() {
Winson Chung9a742902015-12-11 10:25:40 -0500559 @Override
560 public void run() {
561 setAlpha(0f);
562 }
563 });
Winsonc29ff002015-11-20 16:00:45 -0800564 }
565
566 public final void onBusEvent(HideHistoryEvent event) {
567 // Show the history button when the history view is hidden
Winson Chung9a742902015-12-11 10:25:40 -0500568 setAlpha(1f);
569 showHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500570 event.getAnimationTrigger());
Winsonc29ff002015-11-20 16:00:45 -0800571 }
572
573 public final void onBusEvent(ShowHistoryButtonEvent event) {
574 showHistoryButton(150);
575 }
576
577 public final void onBusEvent(HideHistoryButtonEvent event) {
578 hideHistoryButton(100);
579 }
580
Winson Chung06266772015-12-11 10:24:21 -0500581 public final void onBusEvent(TaskStackUpdatedEvent event) {
582 mStack.setTasks(event.stack.computeAllTasksList(), true /* notifyStackChanges */);
Winson Chung97567552015-12-16 17:07:19 -0500583 mStack.createAffiliatedGroupings(getContext());
Winson Chung06266772015-12-11 10:24:21 -0500584 }
585
Winsonc29ff002015-11-20 16:00:45 -0800586 /**
587 * Shows the history button.
588 */
Winson Chung9a742902015-12-11 10:25:40 -0500589 private void showHistoryButton(final int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500590 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500591 showHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500592 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500593 }
594
595 private void showHistoryButton(final int duration,
596 final ReferenceCountedTrigger postHideHistoryAnimationTrigger) {
Winson Chungde750de2015-12-11 10:26:06 -0500597 mHistoryButton.setText(getContext().getString(R.string.recents_history_label_format,
598 mStack.getHistoricalTasks().size()));
Winsonf24f2162016-01-05 12:11:55 -0800599 if (mHistoryButton.getVisibility() == View.INVISIBLE) {
600 mHistoryButton.setVisibility(View.VISIBLE);
601 mHistoryButton.setAlpha(0f);
602 postHideHistoryAnimationTrigger.addLastDecrementRunnable(new Runnable() {
603 @Override
604 public void run() {
605 mHistoryButton.animate()
606 .alpha(1f)
607 .setDuration(duration)
608 .setInterpolator(mFastOutSlowInInterpolator)
609 .withLayer()
610 .start();
611 }
612 });
613 }
Winsonc29ff002015-11-20 16:00:45 -0800614 }
615
616 /**
617 * Hides the history button.
618 */
619 private void hideHistoryButton(int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500620 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500621 hideHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500622 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500623 }
624
625 private void hideHistoryButton(int duration,
626 final ReferenceCountedTrigger postHideStackAnimationTrigger) {
Winsonf24f2162016-01-05 12:11:55 -0800627 if (mHistoryButton.getVisibility() == View.VISIBLE) {
628 mHistoryButton.animate()
629 .alpha(0f)
630 .setDuration(duration)
631 .setInterpolator(mFastOutLinearInInterpolator)
632 .withEndAction(new Runnable() {
633 @Override
634 public void run() {
635 mHistoryButton.setVisibility(View.INVISIBLE);
636 postHideStackAnimationTrigger.decrement();
637 }
638 })
639 .withLayer()
640 .start();
641 postHideStackAnimationTrigger.increment();
642 }
Winsonc29ff002015-11-20 16:00:45 -0800643 }
644
Winson4165d3362015-10-10 14:40:35 -0700645 /**
646 * Updates the dock region to match the specified dock state.
647 */
Winson882072b2015-10-12 11:26:33 -0700648 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
649 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
650 if (newDockStates != null) {
651 for (TaskStack.DockState dockState : newDockStates) {
652 newDockStatesSet.add(dockState);
653 }
Winson4165d3362015-10-10 14:40:35 -0700654 }
Winson882072b2015-10-12 11:26:33 -0700655 for (TaskStack.DockState dockState : mVisibleDockStates) {
656 TaskStack.DockState.ViewState viewState = dockState.viewState;
657 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
658 // This is no longer visible, so hide it
659 viewState.startAlphaAnimation(0, 150);
660 } else {
661 // This state is now visible, update the bounds and show it
662 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
663 viewState.dockAreaOverlay.setBounds(
664 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
665 viewState.dockAreaOverlay.setCallback(this);
666 viewState.startAlphaAnimation(alpha, 150);
667 }
Winson4165d3362015-10-10 14:40:35 -0700668 }
Winson4165d3362015-10-10 14:40:35 -0700669 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800670
Winsonb1e71d02015-11-23 12:40:23 -0800671 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
672 if (!event.visible) {
673 // Reset the view state
674 mAwaitingFirstLayout = true;
675 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800676 }
677 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800678}