blob: 9e9956e0a3b9560f983d120508ab706e9652f3ed [file] [log] [blame]
Winson Chung303e1ff2014-03-07 15:06:19 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.recents.views;
18
Winson Chung303e1ff2014-03-07 15:06:19 -080019import android.content.Context;
Winsonc29ff002015-11-20 16:00:45 -080020import android.content.res.Resources;
Winson Chung303e1ff2014-03-07 15:06:19 -080021import android.graphics.Canvas;
22import android.graphics.Rect;
Winson4165d3362015-10-10 14:40:35 -070023import android.graphics.drawable.Drawable;
Winson83c1b072015-11-09 10:48:04 -080024import android.os.Handler;
Winson882072b2015-10-12 11:26:33 -070025import android.util.ArraySet;
Winson Chung8e548f72014-06-24 14:40:53 -070026import android.util.AttributeSet;
Winson Chungecd9b302014-04-16 17:07:18 -070027import android.view.LayoutInflater;
Winsonbe7607a2015-10-01 17:24:51 -070028import android.view.MotionEvent;
Winsonc29ff002015-11-20 16:00:45 -080029import android.view.View;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -080030import android.view.ViewPropertyAnimator;
Winson Chung653f70c22014-05-19 14:49:42 -070031import android.view.WindowInsets;
Winson35f30502015-09-28 11:24:36 -070032import android.view.animation.AnimationUtils;
33import android.view.animation.Interpolator;
Winson Chung303e1ff2014-03-07 15:06:19 -080034import android.widget.FrameLayout;
Jorim Jaggi92de6b62015-12-09 15:32:30 -080035import com.android.internal.logging.MetricsLogger;
Winson Chungde750de2015-12-11 10:26:06 -050036import android.widget.TextView;
Winson Chungd16c5652015-01-26 16:11:07 -080037import com.android.systemui.R;
Winsone7f138c2015-10-22 16:15:21 -070038import com.android.systemui.recents.Recents;
Winsonbe7607a2015-10-01 17:24:51 -070039import com.android.systemui.recents.RecentsActivity;
Winsonb1e71d02015-11-23 12:40:23 -080040import com.android.systemui.recents.RecentsActivityLaunchState;
Winson Chungaee097c2015-04-02 18:16:02 -070041import com.android.systemui.recents.RecentsAppWidgetHostView;
Winson Chung303e1ff2014-03-07 15:06:19 -080042import com.android.systemui.recents.RecentsConfiguration;
Winsonc29ff002015-11-20 16:00:45 -080043import com.android.systemui.recents.RecentsDebugFlags;
Winson2536c7e2015-10-01 15:49:31 -070044import com.android.systemui.recents.events.EventBus;
Winson13d30662015-11-06 15:30:29 -080045import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
Winsonc29ff002015-11-20 16:00:45 -080046import com.android.systemui.recents.events.activity.DebugFlagsChangedEvent;
Winson412e1802015-10-20 16:57:57 -070047import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
Winsonc29ff002015-11-20 16:00:45 -080048import com.android.systemui.recents.events.activity.HideHistoryButtonEvent;
49import com.android.systemui.recents.events.activity.HideHistoryEvent;
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;
Winson Chung6ac8bd62015-01-07 16:38:35 -080068import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080069
Wale Ogunwale3797c222015-10-27 14:21:58 -070070import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070071
Winson Chung303e1ff2014-03-07 15:06:19 -080072/**
73 * This view is the the top level layout that contains TaskStacks (which are laid out according
74 * to their SpaceNode bounds.
75 */
Winson Chung48f2cda2015-12-11 13:20:12 -050076public class RecentsView extends FrameLayout {
Winson Chung47c4c692014-03-17 10:17:11 -070077
Jorim Jaggi6e18e002015-06-02 17:07:39 -070078 private static final String TAG = "RecentsView";
Winson0d14d4d2015-10-26 17:05:04 -070079 private static final boolean DEBUG = false;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070080
Winson Chung9a742902015-12-11 10:25:40 -050081 private final Handler mHandler;
Winson Chungd42a6cf2014-06-03 16:24:04 -070082
Winson Chung9a742902015-12-11 10:25:40 -050083 private TaskStack mStack;
84 private TaskStackView mTaskStackView;
85 private RecentsAppWidgetHostView mSearchBar;
Winson Chungde750de2015-12-11 10:26:06 -050086 private TextView mHistoryButton;
Winson Chung9a742902015-12-11 10:25:40 -050087 private View mEmptyView;
88 private boolean mAwaitingFirstLayout = true;
89 private boolean mLastTaskLaunchedWasFreeform;
90 private Rect mSystemInsets = new Rect();
Winsonbe7607a2015-10-01 17:24:51 -070091
Winson Chung9a742902015-12-11 10:25:40 -050092 private RecentsTransitionHelper mTransitionHelper;
93 private RecentsViewTouchHandler mTouchHandler;
94 private TaskStack.DockState[] mVisibleDockStates = {
Winson882072b2015-10-12 11:26:33 -070095 TaskStack.DockState.LEFT,
96 TaskStack.DockState.TOP,
97 TaskStack.DockState.RIGHT,
98 TaskStack.DockState.BOTTOM,
99 };
Winsonbe7607a2015-10-01 17:24:51 -0700100
Winson Chung9a742902015-12-11 10:25:40 -0500101 private final Interpolator mFastOutSlowInInterpolator;
102 private final Interpolator mFastOutLinearInInterpolator;
103 private final FlingAnimationUtils mFlingAnimationUtils;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800104
Winson Chung303e1ff2014-03-07 15:06:19 -0800105 public RecentsView(Context context) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800106 this(context, null);
Winson Chung8e548f72014-06-24 14:40:53 -0700107 }
108
109 public RecentsView(Context context, AttributeSet attrs) {
110 this(context, attrs, 0);
111 }
112
113 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
114 this(context, attrs, defStyleAttr, 0);
115 }
116
117 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
118 super(context, attrs, defStyleAttr, defStyleRes);
Winsonc29ff002015-11-20 16:00:45 -0800119 Resources res = context.getResources();
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;
Winson Chungfca2a632015-12-11 15:51:18 -0500148 // Disable reusing task stack views until the visibility bug is fixed. b/25998134
Winson Chung9a742902015-12-11 10:25:40 -0500149 if (false && launchState.launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700150 if (mTaskStackView != null) {
151 // If onRecentsHidden is not triggered, we need to the stack view again here
152 mTaskStackView.reset();
153 mTaskStackView.setStack(stack);
Winson160f4c02015-12-02 19:06:44 -0800154 removeView(mTaskStackView);
155 addView(mTaskStackView);
Winson147ecaf2015-09-16 16:49:55 -0700156 } else {
157 mTaskStackView = new TaskStackView(getContext(), stack);
Winson147ecaf2015-09-16 16:49:55 -0700158 addView(mTaskStackView);
159 }
160 } else {
161 if (mTaskStackView != null) {
162 removeView(mTaskStackView);
163 }
164 mTaskStackView = new TaskStackView(getContext(), stack);
Winson147ecaf2015-09-16 16:49:55 -0700165 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800166 }
Winson Chung9a742902015-12-11 10:25:40 -0500167
168 // Update the top level view's visibilities
169 if (stack.getStackTaskCount() > 0) {
170 hideEmptyView();
Winsonc29ff002015-11-20 16:00:45 -0800171 } else {
Winson Chung9a742902015-12-11 10:25:40 -0500172 showEmptyView();
Winsonc29ff002015-11-20 16:00:45 -0800173 }
Winson Chung02d49272014-08-29 13:57:29 -0700174
Winson Chungb0a28ea2014-10-28 15:21:35 -0700175 // Trigger a new layout
176 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700177 }
178
Winsonb1e71d02015-11-23 12:40:23 -0800179 /**
180 * Returns whether the last task launched was in the freeform stack or not.
181 */
182 public boolean isLastTaskLaunchedFreeform() {
183 return mLastTaskLaunchedWasFreeform;
184 }
185
Winsonc29ff002015-11-20 16:00:45 -0800186 /**
187 * Returns the currently set task stack.
188 */
189 public TaskStack getTaskStack() {
190 return mStack;
191 }
192
Skuhne8aa7d162015-03-20 13:40:53 -0700193 /** Gets the next task in the stack - or if the last - the top task */
194 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700195 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700196 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700197 if (mTaskStackView != null) {
198 TaskStack stack = mTaskStackView.getStack();
Winson250608a2015-11-24 15:00:31 -0800199 ArrayList<Task> taskList = stack.getStackTasks();
Skuhne8aa7d162015-03-20 13:40:53 -0700200 // Iterate the stack views and try and find the focused task
201 for (int j = taskList.size() - 1; j >= 0; --j) {
202 Task task = taskList.get(j);
203 // Return the next task in the line.
204 if (found)
205 return task;
206 // Remember the first possible task as the top task.
207 if (returnTask == null)
208 returnTask = task;
209 if (task == taskToSearch)
210 found = true;
211 }
212 }
213 return returnTask;
214 }
215
Winson Chung1e8d71b2014-05-16 17:05:22 -0700216 /** Launches the focused task from the first stack if possible */
217 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700218 if (mTaskStackView != null) {
219 TaskStack stack = mTaskStackView.getStack();
Winson142af422015-11-09 10:39:57 -0800220 Task task = mTaskStackView.getFocusedTask();
221 if (task != null) {
222 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson Chung48f2cda2015-12-11 13:20:12 -0500223 EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
224 INVALID_STACK_ID, false));
Winson142af422015-11-09 10:39:57 -0800225 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700226 }
227 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700228 return false;
229 }
230
Winsona0731a12015-12-02 15:10:14 -0800231 /** Launches the task that recents was launched from if possible */
232 public boolean launchPreviousTask() {
233 if (mTaskStackView != null) {
234 TaskStack stack = mTaskStackView.getStack();
235 Task task = stack.getLaunchTarget();
236 if (task != null) {
237 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson Chung48f2cda2015-12-11 13:20:12 -0500238 EventBus.getDefault().send(new LaunchTaskEvent(taskView, task, null,
239 INVALID_STACK_ID, false));
Winsona0731a12015-12-02 15:10:14 -0800240 return true;
241 }
242 }
243 return false;
244 }
245
Skuhne8aa7d162015-03-20 13:40:53 -0700246 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700247 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700248 if (mTaskStackView != null) {
249 TaskStack stack = mTaskStackView.getStack();
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(),
478 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700479 }
480
Winsoneca4ab62015-11-04 10:50:28 -0800481 public final void onBusEvent(DragDropTargetChangedEvent event) {
482 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700483 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
484 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
485 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800486 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
487 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700488 }
Winsonbe7607a2015-10-01 17:24:51 -0700489 }
490
491 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800492 // Animate the overlay alpha back to 0
493 updateVisibleDockRegions(null, -1);
494
Winson479f7442015-11-25 15:16:27 -0800495 // Handle the case where we drop onto a dock region
496 if (event.dropTarget instanceof TaskStack.DockState) {
Winsoneca4ab62015-11-04 10:50:28 -0800497 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
498
Winsona0a8ae42015-12-01 10:43:02 -0800499 // Remove the task after it is docked
Winsona0a8ae42015-12-01 10:43:02 -0800500 event.taskView.animate()
501 .alpha(0f)
502 .setDuration(150)
503 .setInterpolator(mFastOutLinearInInterpolator)
504 .setUpdateListener(null)
505 .setListener(null)
506 .withEndAction(new Runnable() {
507 @Override
508 public void run() {
509 mTaskStackView.getStack().removeTask(event.task);
510 }
511 })
512 .withLayer()
513 .start();
514
Winsoneca4ab62015-11-04 10:50:28 -0800515 // Dock the task and launch it
516 SystemServicesProxy ssp = Recents.getSystemServices();
517 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
518 launchTask(event.task, null, INVALID_STACK_ID);
Jorim Jaggi92de6b62015-12-09 15:32:30 -0800519
520 MetricsLogger.action(mContext,
521 MetricsLogger.ACTION_WINDOW_DOCK_DRAG_DROP);
Winsonbe7607a2015-10-01 17:24:51 -0700522 }
523 }
Winson4165d3362015-10-10 14:40:35 -0700524
Jorim Jaggidd98d412015-11-18 15:57:38 -0800525 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800526 if (mTaskStackView.getTaskViews().size() > 0) {
527 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
528 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800529 }
530
531 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800532 ViewPropertyAnimator animator = animate();
Jorim Jaggi8fccfe22015-12-03 16:58:08 -0800533 if (event.velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800534 animator.translationY(getHeight());
535 animator.withEndAction(new Runnable() {
536 @Override
537 public void run() {
538 WindowManagerProxy.getInstance().maximizeDockedStack();
539 }
540 });
541 mFlingAnimationUtils.apply(animator, getTranslationY(), getHeight(), event.velocity);
542 } else {
543 animator.translationY(0f);
544 animator.setListener(null);
545 mFlingAnimationUtils.apply(animator, getTranslationY(), 0, event.velocity);
546 }
547 animator.start();
Jorim Jaggidd98d412015-11-18 15:57:38 -0800548 }
549
Winsonc29ff002015-11-20 16:00:45 -0800550 public final void onBusEvent(ShowHistoryEvent event) {
551 // Hide the history button when the history view is shown
Winson Chung9a742902015-12-11 10:25:40 -0500552 hideHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500553 event.getAnimationTrigger());
554 event.addPostAnimationCallback(new Runnable() {
Winson Chung9a742902015-12-11 10:25:40 -0500555 @Override
556 public void run() {
557 setAlpha(0f);
558 }
559 });
Winsonc29ff002015-11-20 16:00:45 -0800560 }
561
562 public final void onBusEvent(HideHistoryEvent event) {
563 // Show the history button when the history view is hidden
Winson Chung9a742902015-12-11 10:25:40 -0500564 setAlpha(1f);
565 showHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500566 event.getAnimationTrigger());
Winsonc29ff002015-11-20 16:00:45 -0800567 }
568
569 public final void onBusEvent(ShowHistoryButtonEvent event) {
570 showHistoryButton(150);
571 }
572
573 public final void onBusEvent(HideHistoryButtonEvent event) {
574 hideHistoryButton(100);
575 }
576
Winson Chung06266772015-12-11 10:24:21 -0500577 public final void onBusEvent(TaskStackUpdatedEvent event) {
578 mStack.setTasks(event.stack.computeAllTasksList(), true /* notifyStackChanges */);
Winson Chung97567552015-12-16 17:07:19 -0500579 mStack.createAffiliatedGroupings(getContext());
Winson Chung06266772015-12-11 10:24:21 -0500580 }
581
Winsonc29ff002015-11-20 16:00:45 -0800582 /**
583 * Shows the history button.
584 */
Winson Chung9a742902015-12-11 10:25:40 -0500585 private void showHistoryButton(final int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500586 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500587 showHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500588 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500589 }
590
591 private void showHistoryButton(final int duration,
592 final ReferenceCountedTrigger postHideHistoryAnimationTrigger) {
Winsonc29ff002015-11-20 16:00:45 -0800593 mHistoryButton.setVisibility(View.VISIBLE);
Winson Chung9a742902015-12-11 10:25:40 -0500594 mHistoryButton.setAlpha(0f);
Winson Chungde750de2015-12-11 10:26:06 -0500595 mHistoryButton.setText(getContext().getString(R.string.recents_history_label_format,
596 mStack.getHistoricalTasks().size()));
Winson Chung9a742902015-12-11 10:25:40 -0500597 postHideHistoryAnimationTrigger.addLastDecrementRunnable(new Runnable() {
598 @Override
599 public void run() {
600 mHistoryButton.animate()
601 .alpha(1f)
602 .setDuration(duration)
603 .setInterpolator(mFastOutSlowInInterpolator)
604 .withLayer()
605 .start();
606 }
607 });
Winsonc29ff002015-11-20 16:00:45 -0800608 }
609
610 /**
611 * Hides the history button.
612 */
613 private void hideHistoryButton(int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500614 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500615 hideHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500616 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500617 }
618
619 private void hideHistoryButton(int duration,
620 final ReferenceCountedTrigger postHideStackAnimationTrigger) {
Winsonc29ff002015-11-20 16:00:45 -0800621 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);
Winson Chung9a742902015-12-11 10:25:40 -0500629 postHideStackAnimationTrigger.decrement();
Winsonc29ff002015-11-20 16:00:45 -0800630 }
631 })
632 .withLayer()
633 .start();
Winson Chung9a742902015-12-11 10:25:40 -0500634 postHideStackAnimationTrigger.increment();
Winsonc29ff002015-11-20 16:00:45 -0800635 }
636
Winson4165d3362015-10-10 14:40:35 -0700637 /**
638 * Updates the dock region to match the specified dock state.
639 */
Winson882072b2015-10-12 11:26:33 -0700640 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
641 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
642 if (newDockStates != null) {
643 for (TaskStack.DockState dockState : newDockStates) {
644 newDockStatesSet.add(dockState);
645 }
Winson4165d3362015-10-10 14:40:35 -0700646 }
Winson882072b2015-10-12 11:26:33 -0700647 for (TaskStack.DockState dockState : mVisibleDockStates) {
648 TaskStack.DockState.ViewState viewState = dockState.viewState;
649 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
650 // This is no longer visible, so hide it
651 viewState.startAlphaAnimation(0, 150);
652 } else {
653 // This state is now visible, update the bounds and show it
654 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
655 viewState.dockAreaOverlay.setBounds(
656 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
657 viewState.dockAreaOverlay.setCallback(this);
658 viewState.startAlphaAnimation(alpha, 150);
659 }
Winson4165d3362015-10-10 14:40:35 -0700660 }
Winson4165d3362015-10-10 14:40:35 -0700661 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800662
Winsonb1e71d02015-11-23 12:40:23 -0800663 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
664 if (!event.visible) {
665 // Reset the view state
666 mAwaitingFirstLayout = true;
667 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800668 }
669 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800670}