blob: 501f052edab5d7ea78010d7d7899bd6e69b3d5cb [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;
Winson3e874742016-01-07 10:08:17 -080066import com.android.systemui.statusbar.phone.PhoneStatusBar;
Winson Chung303e1ff2014-03-07 15:06:19 -080067
68import java.util.ArrayList;
Winsondc8de842016-01-06 15:21:41 -080069import java.util.Collections;
Winson Chung6ac8bd62015-01-07 16:38:35 -080070import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080071
Wale Ogunwale3797c222015-10-27 14:21:58 -070072import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070073
Winson Chung303e1ff2014-03-07 15:06:19 -080074/**
75 * This view is the the top level layout that contains TaskStacks (which are laid out according
76 * to their SpaceNode bounds.
77 */
Winson Chung48f2cda2015-12-11 13:20:12 -050078public class RecentsView extends FrameLayout {
Winson Chung47c4c692014-03-17 10:17:11 -070079
Winson3e874742016-01-07 10:08:17 -080080 private static final int DOCK_AREA_OVERLAY_TRANSITION_DURATION = 135;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070081
Winson Chung9a742902015-12-11 10:25:40 -050082 private final Handler mHandler;
Winson Chungd42a6cf2014-06-03 16:24:04 -070083
Winson Chung9a742902015-12-11 10:25:40 -050084 private TaskStack mStack;
85 private TaskStackView mTaskStackView;
86 private RecentsAppWidgetHostView mSearchBar;
Winson Chungde750de2015-12-11 10:26:06 -050087 private TextView mHistoryButton;
Winson Chung9a742902015-12-11 10:25:40 -050088 private View mEmptyView;
89 private boolean mAwaitingFirstLayout = true;
90 private boolean mLastTaskLaunchedWasFreeform;
91 private Rect mSystemInsets = new Rect();
Winson3e874742016-01-07 10:08:17 -080092 private int mDividerSize;
Winsonbe7607a2015-10-01 17:24:51 -070093
Winson Chung9a742902015-12-11 10:25:40 -050094 private RecentsTransitionHelper mTransitionHelper;
95 private RecentsViewTouchHandler mTouchHandler;
96 private TaskStack.DockState[] mVisibleDockStates = {
Winson882072b2015-10-12 11:26:33 -070097 TaskStack.DockState.LEFT,
98 TaskStack.DockState.TOP,
99 TaskStack.DockState.RIGHT,
100 TaskStack.DockState.BOTTOM,
101 };
Winsonbe7607a2015-10-01 17:24:51 -0700102
Winson Chung9a742902015-12-11 10:25:40 -0500103 private final Interpolator mFastOutSlowInInterpolator;
104 private final Interpolator mFastOutLinearInInterpolator;
105 private final FlingAnimationUtils mFlingAnimationUtils;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800106
Winson Chung303e1ff2014-03-07 15:06:19 -0800107 public RecentsView(Context context) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800108 this(context, null);
Winson Chung8e548f72014-06-24 14:40:53 -0700109 }
110
111 public RecentsView(Context context, AttributeSet attrs) {
112 this(context, attrs, 0);
113 }
114
115 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
116 this(context, attrs, defStyleAttr, 0);
117 }
118
119 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
120 super(context, attrs, defStyleAttr, defStyleRes);
Winsonbe7607a2015-10-01 17:24:51 -0700121 setWillNotDraw(false);
Winson3e874742016-01-07 10:08:17 -0800122
123 SystemServicesProxy ssp = Recents.getSystemServices();
Winson83c1b072015-11-09 10:48:04 -0800124 mHandler = new Handler();
125 mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
Winson35f30502015-09-28 11:24:36 -0700126 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
127 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonc29ff002015-11-20 16:00:45 -0800128 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
129 com.android.internal.R.interpolator.fast_out_linear_in);
Winson3e874742016-01-07 10:08:17 -0800130 mDividerSize = ssp.getDockedDividerSize(context);
Winsonbe7607a2015-10-01 17:24:51 -0700131 mTouchHandler = new RecentsViewTouchHandler(this);
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800132 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.3f);
Winsonc29ff002015-11-20 16:00:45 -0800133
134 LayoutInflater inflater = LayoutInflater.from(context);
Winson Chungde750de2015-12-11 10:26:06 -0500135 mHistoryButton = (TextView) inflater.inflate(R.layout.recents_history_button, this, false);
Winsonc29ff002015-11-20 16:00:45 -0800136 mHistoryButton.setOnClickListener(new View.OnClickListener() {
137 @Override
138 public void onClick(View v) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500139 EventBus.getDefault().send(new ShowHistoryEvent());
Winsonc29ff002015-11-20 16:00:45 -0800140 }
141 });
Winson Chung9a742902015-12-11 10:25:40 -0500142 addView(mHistoryButton);
143 mEmptyView = inflater.inflate(R.layout.recents_empty, this, false);
144 addView(mEmptyView);
Winson Chungd16c5652015-01-26 16:11:07 -0800145 }
146
Winson Chungdcfa7972014-07-22 12:27:13 -0700147 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700148 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700149 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung9a742902015-12-11 10:25:40 -0500150 RecentsActivityLaunchState launchState = config.getLaunchState();
Winsonc29ff002015-11-20 16:00:45 -0800151 mStack = stack;
Winson55e20d52016-01-06 15:21:29 -0800152 if (launchState.launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700153 if (mTaskStackView != null) {
154 // If onRecentsHidden is not triggered, we need to the stack view again here
155 mTaskStackView.reset();
156 mTaskStackView.setStack(stack);
157 } else {
158 mTaskStackView = new TaskStackView(getContext(), stack);
Winson147ecaf2015-09-16 16:49:55 -0700159 addView(mTaskStackView);
160 }
161 } else {
162 if (mTaskStackView != null) {
163 removeView(mTaskStackView);
164 }
165 mTaskStackView = new TaskStackView(getContext(), stack);
Winson147ecaf2015-09-16 16:49:55 -0700166 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800167 }
Winson Chung9a742902015-12-11 10:25:40 -0500168
169 // Update the top level view's visibilities
Winson4b057c62016-01-12 15:01:52 -0800170 if (stack.getTaskCount() > 0) {
Winson Chung9a742902015-12-11 10:25:40 -0500171 hideEmptyView();
Winsonc29ff002015-11-20 16:00:45 -0800172 } else {
Winson Chung9a742902015-12-11 10:25:40 -0500173 showEmptyView();
Winsonc29ff002015-11-20 16:00:45 -0800174 }
Winson Chung02d49272014-08-29 13:57:29 -0700175
Winson Chungb0a28ea2014-10-28 15:21:35 -0700176 // Trigger a new layout
177 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700178 }
179
Winsonb1e71d02015-11-23 12:40:23 -0800180 /**
181 * Returns whether the last task launched was in the freeform stack or not.
182 */
183 public boolean isLastTaskLaunchedFreeform() {
184 return mLastTaskLaunchedWasFreeform;
185 }
186
Winsonc29ff002015-11-20 16:00:45 -0800187 /**
188 * Returns the currently set task stack.
189 */
190 public TaskStack getTaskStack() {
191 return mStack;
192 }
193
Skuhne8aa7d162015-03-20 13:40:53 -0700194 /** Gets the next task in the stack - or if the last - the top task */
195 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700196 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700197 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700198 if (mTaskStackView != null) {
199 TaskStack stack = mTaskStackView.getStack();
Winson250608a2015-11-24 15:00:31 -0800200 ArrayList<Task> taskList = stack.getStackTasks();
Skuhne8aa7d162015-03-20 13:40:53 -0700201 // Iterate the stack views and try and find the focused task
202 for (int j = taskList.size() - 1; j >= 0; --j) {
203 Task task = taskList.get(j);
204 // Return the next task in the line.
205 if (found)
206 return task;
207 // Remember the first possible task as the top task.
208 if (returnTask == null)
209 returnTask = task;
210 if (task == taskToSearch)
211 found = true;
212 }
213 }
214 return returnTask;
215 }
216
Winson Chung1e8d71b2014-05-16 17:05:22 -0700217 /** Launches the focused task from the first stack if possible */
218 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700219 if (mTaskStackView != null) {
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) {
Skuhne8aa7d162015-03-20 13:40:53 -0700249 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700250 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700251 int taskViewCount = taskViews.size();
252 for (int j = 0; j < taskViewCount; j++) {
253 TaskView tv = taskViews.get(j);
254 if (tv.getTask() == task) {
Winson Chung48f2cda2015-12-11 13:20:12 -0500255 EventBus.getDefault().send(new LaunchTaskEvent(tv, task, taskBounds,
256 destinationStack, false));
Skuhne8aa7d162015-03-20 13:40:53 -0700257 return true;
258 }
259 }
260 }
261 return false;
262 }
263
Winson Chungf7bca432014-04-30 17:11:13 -0700264 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700265 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700266 // Remove the previous search bar if one exists
267 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
268 removeView(mSearchBar);
269 }
270 // Add the new search bar
271 if (searchBar != null) {
272 mSearchBar = searchBar;
273 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700274 }
Winson Chungecd9b302014-04-16 17:07:18 -0700275 }
276
Winson Chung772b6b12014-07-03 15:54:02 -0700277 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700278 public boolean hasValidSearchBar() {
279 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700280 }
281
Winson Chung9a742902015-12-11 10:25:40 -0500282 /**
283 * Hides the task stack and shows the empty view.
284 */
285 public void showEmptyView() {
Winsond8b1d632016-01-04 17:51:18 -0800286 if (RecentsDebugFlags.Static.EnableSearchBar && (mSearchBar != null)) {
Winson Chung9a742902015-12-11 10:25:40 -0500287 mSearchBar.setVisibility(View.INVISIBLE);
288 }
289 mTaskStackView.setVisibility(View.INVISIBLE);
290 mEmptyView.setVisibility(View.VISIBLE);
291 mEmptyView.bringToFront();
292 mHistoryButton.bringToFront();
293 }
294
295 /**
296 * Shows the task stack and hides the empty view.
297 */
298 public void hideEmptyView() {
299 mEmptyView.setVisibility(View.INVISIBLE);
300 mTaskStackView.setVisibility(View.VISIBLE);
Winsond8b1d632016-01-04 17:51:18 -0800301 if (RecentsDebugFlags.Static.EnableSearchBar && (mSearchBar != null)) {
Winson Chung9a742902015-12-11 10:25:40 -0500302 mSearchBar.setVisibility(View.VISIBLE);
303 }
304 mTaskStackView.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700305 if (mSearchBar != null) {
Winson Chung012ef362014-07-31 18:36:25 -0700306 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700307 }
Winson Chung9a742902015-12-11 10:25:40 -0500308 mHistoryButton.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700309 }
310
Winsonc29ff002015-11-20 16:00:45 -0800311 /**
312 * Returns the last known system insets.
313 */
314 public Rect getSystemInsets() {
315 return mSystemInsets;
316 }
317
Winsonbe7607a2015-10-01 17:24:51 -0700318 @Override
319 protected void onAttachedToWindow() {
320 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
321 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
322 super.onAttachedToWindow();
323 }
324
325 @Override
326 protected void onDetachedFromWindow() {
327 super.onDetachedFromWindow();
328 EventBus.getDefault().unregister(this);
329 EventBus.getDefault().unregister(mTouchHandler);
330 }
331
Winson Chungf7bca432014-04-30 17:11:13 -0700332 /**
333 * This is called with the full size of the window since we are handling our own insets.
334 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800335 @Override
336 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700337 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800338 int width = MeasureSpec.getSize(widthMeasureSpec);
339 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700340
Winson Chungf7bca432014-04-30 17:11:13 -0700341 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700342 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700343 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700344 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700345 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700346 mSearchBar.measure(
347 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
348 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700349 }
350
Winson Chungf7bca432014-04-30 17:11:13 -0700351 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700352 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700353 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700354 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700355 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700356 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800357 }
358
Winson Chung9a742902015-12-11 10:25:40 -0500359 // Measure the empty view
360 measureChild(mEmptyView, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
361 MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
362
Winson250608a2015-11-24 15:00:31 -0800363 // Measure the history button with the full space above the stack, but width-constrained
364 // to the stack
Winson387aac62015-11-25 11:18:56 -0800365 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
Winsonc29ff002015-11-20 16:00:45 -0800366 measureChild(mHistoryButton,
Winson387aac62015-11-25 11:18:56 -0800367 MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
368 MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
Winson250608a2015-11-24 15:00:31 -0800369 MeasureSpec.EXACTLY));
Winson Chung9a742902015-12-11 10:25:40 -0500370
Winson Chung303e1ff2014-03-07 15:06:19 -0800371 setMeasuredDimension(width, height);
372 }
373
Winson Chungf7bca432014-04-30 17:11:13 -0700374 /**
375 * This is called with the full size of the window since we are handling our own insets.
376 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800377 @Override
378 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700379 RecentsConfiguration config = Recents.getConfiguration();
380
Winson Chungf7bca432014-04-30 17:11:13 -0700381 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700382 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
383 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700384 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700385 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700386 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700387 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
388 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700389 }
390
Winson147ecaf2015-09-16 16:49:55 -0700391 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
392 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800393 }
Winsonbe7607a2015-10-01 17:24:51 -0700394
Winson Chung9a742902015-12-11 10:25:40 -0500395 // Layout the empty view
396 mEmptyView.layout(left, top, right, bottom);
397
Winson250608a2015-11-24 15:00:31 -0800398 // Layout the history button left-aligned with the stack, but offset from the top of the
399 // view
Winson387aac62015-11-25 11:18:56 -0800400 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
401 mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
402 historyButtonRect.right, historyButtonRect.bottom);
Winsonc29ff002015-11-20 16:00:45 -0800403
Winsonb1e71d02015-11-23 12:40:23 -0800404 if (mAwaitingFirstLayout) {
405 mAwaitingFirstLayout = false;
406
407 // If launched via dragging from the nav bar, then we should translate the whole view
408 // down offscreen
409 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
410 if (launchState.launchedViaDragGesture) {
411 setTranslationY(getMeasuredHeight());
Jorim Jaggi0ca88262015-12-07 17:15:16 -0800412 } else {
413 setTranslationY(0f);
Winsonb1e71d02015-11-23 12:40:23 -0800414 }
415 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800416 }
417
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700418 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700419 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700420 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700421 requestLayout();
Winsonc29ff002015-11-20 16:00:45 -0800422 return insets;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700423 }
424
Winsonbe7607a2015-10-01 17:24:51 -0700425 @Override
426 public boolean onInterceptTouchEvent(MotionEvent ev) {
427 return mTouchHandler.onInterceptTouchEvent(ev);
428 }
429
430 @Override
431 public boolean onTouchEvent(MotionEvent ev) {
432 return mTouchHandler.onTouchEvent(ev);
433 }
434
435 @Override
436 protected void dispatchDraw(Canvas canvas) {
437 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700438 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
439 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
440 if (d.getAlpha() > 0) {
441 d.draw(canvas);
442 }
Winsonbe7607a2015-10-01 17:24:51 -0700443 }
444 }
445
Winson4165d3362015-10-10 14:40:35 -0700446 @Override
447 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700448 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
449 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
450 if (d == who) {
451 return true;
452 }
453 }
454 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700455 }
456
Winsonbe7607a2015-10-01 17:24:51 -0700457 /**** EventBus Events ****/
458
Winson Chung48f2cda2015-12-11 13:20:12 -0500459 public final void onBusEvent(LaunchTaskEvent event) {
460 mLastTaskLaunchedWasFreeform = event.task.isFreeformTask();
461 mTransitionHelper.launchTaskFromRecents(mStack, event.task, mTaskStackView, event.taskView,
462 event.screenPinningRequested, event.targetTaskBounds, event.targetTaskStack);
463 }
464
Winsonef064132016-01-05 12:11:31 -0800465 public final void onBusEvent(DismissRecentsToHomeAnimationStarted event) {
466 // Hide the history button
467 int taskViewExitToHomeDuration = getResources().getInteger(
468 R.integer.recents_task_exit_to_home_duration);
469 hideHistoryButton(taskViewExitToHomeDuration);
470
471 // If we are going home, cancel the previous task's window transition
472 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
473 }
474
Winsonbe7607a2015-10-01 17:24:51 -0700475 public final void onBusEvent(DragStartEvent event) {
Winson882072b2015-10-12 11:26:33 -0700476 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
Winson3e874742016-01-07 10:08:17 -0800477 true /* isDefaultDockState */, TaskStack.DockState.NONE.viewState.dockAreaAlpha,
478 true /* animateAlpha */, false /* animateBounds */);
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(),
Winson3e874742016-01-07 10:08:17 -0800484 true /* isDefaultDockState */, TaskStack.DockState.NONE.viewState.dockAreaAlpha,
485 true /* animateAlpha */, true /* animateBounds */);
Winson882072b2015-10-12 11:26:33 -0700486 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800487 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
Winson3e874742016-01-07 10:08:17 -0800488 updateVisibleDockRegions(new TaskStack.DockState[] {dockState},
489 false /* isDefaultDockState */, -1, true /* animateAlpha */,
490 true /* animateBounds */);
Winson882072b2015-10-12 11:26:33 -0700491 }
Winsonbe7607a2015-10-01 17:24:51 -0700492 }
493
494 public final void onBusEvent(final DragEndEvent event) {
Winson479f7442015-11-25 15:16:27 -0800495 // Handle the case where we drop onto a dock region
496 if (event.dropTarget instanceof TaskStack.DockState) {
Winson3e874742016-01-07 10:08:17 -0800497 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
498
499 // Hide the dock region
500 updateVisibleDockRegions(null, false /* isDefaultDockState */, -1,
501 false /* animateAlpha */, false /* animateBounds */);
502
Winsonf24f2162016-01-05 12:11:55 -0800503 TaskStackLayoutAlgorithm stackLayout = mTaskStackView.getStackAlgorithm();
504 TaskStackViewScroller stackScroller = mTaskStackView.getScroller();
505 TaskViewTransform tmpTransform = new TaskViewTransform();
Winsoneca4ab62015-11-04 10:50:28 -0800506
Winson3e874742016-01-07 10:08:17 -0800507 // We translated the view but we need to animate it back from the current layout-space
508 // rect to its final layout-space rect
509 int x = (int) event.taskView.getTranslationX();
510 int y = (int) event.taskView.getTranslationY();
511 Rect taskViewRect = new Rect(event.taskView.getLeft(), event.taskView.getTop(),
512 event.taskView.getRight(), event.taskView.getBottom());
513 taskViewRect.offset(x, y);
514 event.taskView.setTranslationX(0);
515 event.taskView.setTranslationY(0);
516 event.taskView.setLeftTopRightBottom(taskViewRect.left, taskViewRect.top,
517 taskViewRect.right, taskViewRect.bottom);
518
Winsonf24f2162016-01-05 12:11:55 -0800519 // Remove the task view after it is docked
Winson3e874742016-01-07 10:08:17 -0800520 mTaskStackView.updateLayout(false /* boundScroll */);
Winsonf24f2162016-01-05 12:11:55 -0800521 stackLayout.getStackTransform(event.task, stackScroller.getStackScroll(), tmpTransform,
522 null);
Winson3e874742016-01-07 10:08:17 -0800523 tmpTransform.alpha = 0;
524 tmpTransform.scale = 1f;
525 tmpTransform.rect.set(taskViewRect);
Winsonf24f2162016-01-05 12:11:55 -0800526 mTaskStackView.updateTaskViewToTransform(event.taskView, tmpTransform,
Winson3e874742016-01-07 10:08:17 -0800527 new TaskViewAnimation(125, PhoneStatusBar.ALPHA_OUT,
Winsonf24f2162016-01-05 12:11:55 -0800528 new AnimatorListenerAdapter() {
529 @Override
530 public void onAnimationEnd(Animator animation) {
Winson3e874742016-01-07 10:08:17 -0800531 // Dock the task and launch it
532 SystemServicesProxy ssp = Recents.getSystemServices();
533 ssp.startTaskInDockedMode(getContext(), event.task.key.id,
534 dockState.createMode);
535 launchTask(event.task, null, INVALID_STACK_ID);
536
Winsonf24f2162016-01-05 12:11:55 -0800537 mTaskStackView.getStack().removeTask(event.task);
538 }
539 }));
Winsona0a8ae42015-12-01 10:43:02 -0800540
Jorim Jaggi92de6b62015-12-09 15:32:30 -0800541
542 MetricsLogger.action(mContext,
543 MetricsLogger.ACTION_WINDOW_DOCK_DRAG_DROP);
Winson3e874742016-01-07 10:08:17 -0800544 } else {
545 // Animate the overlay alpha back to 0
546 updateVisibleDockRegions(null, true /* isDefaultDockState */, -1,
547 true /* animateAlpha */, false /* animateBounds */);
Winsonbe7607a2015-10-01 17:24:51 -0700548 }
549 }
Winson4165d3362015-10-10 14:40:35 -0700550
Jorim Jaggidd98d412015-11-18 15:57:38 -0800551 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800552 if (mTaskStackView.getTaskViews().size() > 0) {
553 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
554 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800555 }
556
557 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800558 ViewPropertyAnimator animator = animate();
Jorim Jaggi8fccfe22015-12-03 16:58:08 -0800559 if (event.velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800560 animator.translationY(getHeight());
561 animator.withEndAction(new Runnable() {
562 @Override
563 public void run() {
564 WindowManagerProxy.getInstance().maximizeDockedStack();
565 }
566 });
567 mFlingAnimationUtils.apply(animator, getTranslationY(), getHeight(), event.velocity);
568 } else {
569 animator.translationY(0f);
570 animator.setListener(null);
571 mFlingAnimationUtils.apply(animator, getTranslationY(), 0, event.velocity);
572 }
573 animator.start();
Jorim Jaggidd98d412015-11-18 15:57:38 -0800574 }
575
Winsonc29ff002015-11-20 16:00:45 -0800576 public final void onBusEvent(ShowHistoryEvent event) {
577 // Hide the history button when the history view is shown
Winson Chung9a742902015-12-11 10:25:40 -0500578 hideHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500579 event.getAnimationTrigger());
580 event.addPostAnimationCallback(new Runnable() {
Winson Chung9a742902015-12-11 10:25:40 -0500581 @Override
582 public void run() {
583 setAlpha(0f);
584 }
585 });
Winsonc29ff002015-11-20 16:00:45 -0800586 }
587
588 public final void onBusEvent(HideHistoryEvent event) {
589 // Show the history button when the history view is hidden
Winson Chung9a742902015-12-11 10:25:40 -0500590 setAlpha(1f);
591 showHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500592 event.getAnimationTrigger());
Winsonc29ff002015-11-20 16:00:45 -0800593 }
594
595 public final void onBusEvent(ShowHistoryButtonEvent event) {
596 showHistoryButton(150);
597 }
598
599 public final void onBusEvent(HideHistoryButtonEvent event) {
600 hideHistoryButton(100);
601 }
602
Winson Chung06266772015-12-11 10:24:21 -0500603 public final void onBusEvent(TaskStackUpdatedEvent event) {
604 mStack.setTasks(event.stack.computeAllTasksList(), true /* notifyStackChanges */);
Winson Chung97567552015-12-16 17:07:19 -0500605 mStack.createAffiliatedGroupings(getContext());
Winson Chung06266772015-12-11 10:24:21 -0500606 }
607
Winsonc29ff002015-11-20 16:00:45 -0800608 /**
609 * Shows the history button.
610 */
Winson Chung9a742902015-12-11 10:25:40 -0500611 private void showHistoryButton(final int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500612 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500613 showHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500614 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500615 }
616
617 private void showHistoryButton(final int duration,
618 final ReferenceCountedTrigger postHideHistoryAnimationTrigger) {
Winson Chungde750de2015-12-11 10:26:06 -0500619 mHistoryButton.setText(getContext().getString(R.string.recents_history_label_format,
620 mStack.getHistoricalTasks().size()));
Winsonf24f2162016-01-05 12:11:55 -0800621 if (mHistoryButton.getVisibility() == View.INVISIBLE) {
622 mHistoryButton.setVisibility(View.VISIBLE);
623 mHistoryButton.setAlpha(0f);
624 postHideHistoryAnimationTrigger.addLastDecrementRunnable(new Runnable() {
625 @Override
626 public void run() {
627 mHistoryButton.animate()
628 .alpha(1f)
629 .setDuration(duration)
630 .setInterpolator(mFastOutSlowInInterpolator)
631 .withLayer()
632 .start();
633 }
634 });
635 }
Winsonc29ff002015-11-20 16:00:45 -0800636 }
637
638 /**
639 * Hides the history button.
640 */
641 private void hideHistoryButton(int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500642 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500643 hideHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500644 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500645 }
646
647 private void hideHistoryButton(int duration,
648 final ReferenceCountedTrigger postHideStackAnimationTrigger) {
Winsonf24f2162016-01-05 12:11:55 -0800649 if (mHistoryButton.getVisibility() == View.VISIBLE) {
650 mHistoryButton.animate()
651 .alpha(0f)
652 .setDuration(duration)
653 .setInterpolator(mFastOutLinearInInterpolator)
654 .withEndAction(new Runnable() {
655 @Override
656 public void run() {
657 mHistoryButton.setVisibility(View.INVISIBLE);
658 postHideStackAnimationTrigger.decrement();
659 }
660 })
661 .withLayer()
662 .start();
663 postHideStackAnimationTrigger.increment();
664 }
Winsonc29ff002015-11-20 16:00:45 -0800665 }
666
Winson4165d3362015-10-10 14:40:35 -0700667 /**
668 * Updates the dock region to match the specified dock state.
669 */
Winson3e874742016-01-07 10:08:17 -0800670 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates,
671 boolean isDefaultDockState, int overrideAlpha, boolean animateAlpha,
672 boolean animateBounds) {
Winson882072b2015-10-12 11:26:33 -0700673 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
674 if (newDockStates != null) {
Winsondc8de842016-01-06 15:21:41 -0800675 Collections.addAll(newDockStatesSet, newDockStates);
Winson4165d3362015-10-10 14:40:35 -0700676 }
Winson882072b2015-10-12 11:26:33 -0700677 for (TaskStack.DockState dockState : mVisibleDockStates) {
678 TaskStack.DockState.ViewState viewState = dockState.viewState;
679 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
680 // This is no longer visible, so hide it
Winson3e874742016-01-07 10:08:17 -0800681 viewState.startAnimation(null, 0, DOCK_AREA_OVERLAY_TRANSITION_DURATION,
682 PhoneStatusBar.ALPHA_OUT, animateAlpha, animateBounds);
Winson882072b2015-10-12 11:26:33 -0700683 } else {
684 // This state is now visible, update the bounds and show it
685 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
Winson3e874742016-01-07 10:08:17 -0800686 Rect bounds = isDefaultDockState
687 ? dockState.getPreDockedBounds(getMeasuredWidth(), getMeasuredHeight())
688 : dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight(),
689 mDividerSize, mSystemInsets, getResources());
690 if (viewState.dockAreaOverlay.getCallback() != this) {
691 viewState.dockAreaOverlay.setCallback(this);
692 viewState.dockAreaOverlay.setBounds(bounds);
693 }
694 viewState.startAnimation(bounds, alpha, DOCK_AREA_OVERLAY_TRANSITION_DURATION,
695 PhoneStatusBar.ALPHA_IN, animateAlpha, animateBounds);
Winson882072b2015-10-12 11:26:33 -0700696 }
Winson4165d3362015-10-10 14:40:35 -0700697 }
Winson4165d3362015-10-10 14:40:35 -0700698 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800699
Winsonb1e71d02015-11-23 12:40:23 -0800700 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
701 if (!event.visible) {
702 // Reset the view state
703 mAwaitingFirstLayout = true;
704 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800705 }
706 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800707}