blob: 501cbc088f871626e4f27f24f952e86b1f03bbd6 [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;
Winsonb1e71d02015-11-23 12:40:23 -080053import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
Jorim Jaggidd98d412015-11-18 15:57:38 -080054import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
55import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
Winsoneca4ab62015-11-04 10:50:28 -080056import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
Winsonbe7607a2015-10-01 17:24:51 -070057import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
58import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
Winson Chung9a742902015-12-11 10:25:40 -050059import com.android.systemui.recents.misc.ReferenceCountedTrigger;
Winson Chung1f24c7e2014-07-11 17:06:48 -070060import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chung303e1ff2014-03-07 15:06:19 -080061import com.android.systemui.recents.model.Task;
62import com.android.systemui.recents.model.TaskStack;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -080063import com.android.systemui.stackdivider.WindowManagerProxy;
64import com.android.systemui.statusbar.FlingAnimationUtils;
Winson Chung303e1ff2014-03-07 15:06:19 -080065
66import java.util.ArrayList;
Winson Chung6ac8bd62015-01-07 16:38:35 -080067import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080068
Wale Ogunwale3797c222015-10-27 14:21:58 -070069import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070070
Winson Chung303e1ff2014-03-07 15:06:19 -080071/**
72 * This view is the the top level layout that contains TaskStacks (which are laid out according
73 * to their SpaceNode bounds.
74 */
Winson Chung48f2cda2015-12-11 13:20:12 -050075public class RecentsView extends FrameLayout {
Winson Chung47c4c692014-03-17 10:17:11 -070076
Jorim Jaggi6e18e002015-06-02 17:07:39 -070077 private static final String TAG = "RecentsView";
Winson0d14d4d2015-10-26 17:05:04 -070078 private static final boolean DEBUG = false;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070079
Winson Chung9a742902015-12-11 10:25:40 -050080 private final Handler mHandler;
Winson Chungd42a6cf2014-06-03 16:24:04 -070081
Winson Chung9a742902015-12-11 10:25:40 -050082 private TaskStack mStack;
83 private TaskStackView mTaskStackView;
84 private RecentsAppWidgetHostView mSearchBar;
Winson Chungde750de2015-12-11 10:26:06 -050085 private TextView mHistoryButton;
Winson Chung9a742902015-12-11 10:25:40 -050086 private View mEmptyView;
87 private boolean mAwaitingFirstLayout = true;
88 private boolean mLastTaskLaunchedWasFreeform;
89 private Rect mSystemInsets = new Rect();
Winsonbe7607a2015-10-01 17:24:51 -070090
Winson Chung9a742902015-12-11 10:25:40 -050091 private RecentsTransitionHelper mTransitionHelper;
92 private RecentsViewTouchHandler mTouchHandler;
93 private TaskStack.DockState[] mVisibleDockStates = {
Winson882072b2015-10-12 11:26:33 -070094 TaskStack.DockState.LEFT,
95 TaskStack.DockState.TOP,
96 TaskStack.DockState.RIGHT,
97 TaskStack.DockState.BOTTOM,
98 };
Winsonbe7607a2015-10-01 17:24:51 -070099
Winson Chung9a742902015-12-11 10:25:40 -0500100 private final Interpolator mFastOutSlowInInterpolator;
101 private final Interpolator mFastOutLinearInInterpolator;
102 private final FlingAnimationUtils mFlingAnimationUtils;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800103
Winson Chung303e1ff2014-03-07 15:06:19 -0800104 public RecentsView(Context context) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800105 this(context, null);
Winson Chung8e548f72014-06-24 14:40:53 -0700106 }
107
108 public RecentsView(Context context, AttributeSet attrs) {
109 this(context, attrs, 0);
110 }
111
112 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
113 this(context, attrs, defStyleAttr, 0);
114 }
115
116 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
117 super(context, attrs, defStyleAttr, defStyleRes);
Winsonc29ff002015-11-20 16:00:45 -0800118 Resources res = context.getResources();
Winsonbe7607a2015-10-01 17:24:51 -0700119 setWillNotDraw(false);
Winson83c1b072015-11-09 10:48:04 -0800120 mHandler = new Handler();
121 mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
Winson35f30502015-09-28 11:24:36 -0700122 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
123 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonc29ff002015-11-20 16:00:45 -0800124 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
125 com.android.internal.R.interpolator.fast_out_linear_in);
Winsonbe7607a2015-10-01 17:24:51 -0700126 mTouchHandler = new RecentsViewTouchHandler(this);
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800127 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.3f);
Winsonc29ff002015-11-20 16:00:45 -0800128
129 LayoutInflater inflater = LayoutInflater.from(context);
Winson Chungde750de2015-12-11 10:26:06 -0500130 mHistoryButton = (TextView) inflater.inflate(R.layout.recents_history_button, this, false);
Winsonc29ff002015-11-20 16:00:45 -0800131 mHistoryButton.setOnClickListener(new View.OnClickListener() {
132 @Override
133 public void onClick(View v) {
Winson Chung9a742902015-12-11 10:25:40 -0500134 ReferenceCountedTrigger postHideStackAnimationTrigger = new ReferenceCountedTrigger(v.getContext());
135 postHideStackAnimationTrigger.increment();
136 EventBus.getDefault().send(new ShowHistoryEvent(postHideStackAnimationTrigger));
137 postHideStackAnimationTrigger.decrement();
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 Chung24cf1522014-05-29 12:03:33 -0700267 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700268 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800269 // We have to increment/decrement the post animation trigger in case there are no children
270 // to ensure that it runs
271 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700272 if (mTaskStackView != null) {
273 mTaskStackView.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700274 }
Winson Chung740c3ac2014-11-12 16:14:38 -0800275 ctx.postAnimationTrigger.decrement();
Winson Chung24cf1522014-05-29 12:03:33 -0700276 }
277
Winson Chungd42a6cf2014-06-03 16:24:04 -0700278 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700279 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chunga91c2932014-11-07 15:02:38 -0800280 // We have to increment/decrement the post animation trigger in case there are no children
281 // to ensure that it runs
282 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700283 if (mTaskStackView != null) {
284 mTaskStackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700285 }
Winson Chunga91c2932014-11-07 15:02:38 -0800286 ctx.postAnimationTrigger.decrement();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700287
Winsonc29ff002015-11-20 16:00:45 -0800288 // Hide the history button
289 int taskViewExitToHomeDuration = getResources().getInteger(
290 R.integer.recents_task_exit_to_home_duration);
291 hideHistoryButton(taskViewExitToHomeDuration);
292
Winson13d30662015-11-06 15:30:29 -0800293 // If we are going home, cancel the previous task's window transition
294 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
295
Winsonc29ff002015-11-20 16:00:45 -0800296 // Notify sof the exit animation
Winson412e1802015-10-20 16:57:57 -0700297 EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
Winson Chungd42a6cf2014-06-03 16:24:04 -0700298 }
299
Winson Chungf7bca432014-04-30 17:11:13 -0700300 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700301 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700302 // Remove the previous search bar if one exists
303 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
304 removeView(mSearchBar);
305 }
306 // Add the new search bar
307 if (searchBar != null) {
308 mSearchBar = searchBar;
309 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700310 }
Winson Chungecd9b302014-04-16 17:07:18 -0700311 }
312
Winson Chung772b6b12014-07-03 15:54:02 -0700313 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700314 public boolean hasValidSearchBar() {
315 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700316 }
317
Winson Chung9a742902015-12-11 10:25:40 -0500318 /**
319 * Hides the task stack and shows the empty view.
320 */
321 public void showEmptyView() {
322 if (!RecentsDebugFlags.Static.DisableSearchBar && (mSearchBar != null)) {
323 mSearchBar.setVisibility(View.INVISIBLE);
324 }
325 mTaskStackView.setVisibility(View.INVISIBLE);
326 mEmptyView.setVisibility(View.VISIBLE);
327 mEmptyView.bringToFront();
328 mHistoryButton.bringToFront();
329 }
330
331 /**
332 * Shows the task stack and hides the empty view.
333 */
334 public void hideEmptyView() {
335 mEmptyView.setVisibility(View.INVISIBLE);
336 mTaskStackView.setVisibility(View.VISIBLE);
337 if (!RecentsDebugFlags.Static.DisableSearchBar && (mSearchBar != null)) {
338 mSearchBar.setVisibility(View.VISIBLE);
339 }
340 mTaskStackView.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700341 if (mSearchBar != null) {
Winson Chung012ef362014-07-31 18:36:25 -0700342 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700343 }
Winson Chung9a742902015-12-11 10:25:40 -0500344 mHistoryButton.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700345 }
346
Winsonc29ff002015-11-20 16:00:45 -0800347 /**
348 * Returns the last known system insets.
349 */
350 public Rect getSystemInsets() {
351 return mSystemInsets;
352 }
353
Winsonbe7607a2015-10-01 17:24:51 -0700354 @Override
355 protected void onAttachedToWindow() {
356 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
357 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
358 super.onAttachedToWindow();
359 }
360
361 @Override
362 protected void onDetachedFromWindow() {
363 super.onDetachedFromWindow();
364 EventBus.getDefault().unregister(this);
365 EventBus.getDefault().unregister(mTouchHandler);
366 }
367
Winson Chungf7bca432014-04-30 17:11:13 -0700368 /**
369 * This is called with the full size of the window since we are handling our own insets.
370 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800371 @Override
372 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700373 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800374 int width = MeasureSpec.getSize(widthMeasureSpec);
375 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700376
Winson Chungf7bca432014-04-30 17:11:13 -0700377 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700378 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700379 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700380 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700381 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700382 mSearchBar.measure(
383 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
384 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700385 }
386
Winson Chungf7bca432014-04-30 17:11:13 -0700387 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700388 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700389 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700390 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700391 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700392 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800393 }
394
Winson Chung9a742902015-12-11 10:25:40 -0500395 // Measure the empty view
396 measureChild(mEmptyView, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
397 MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
398
Winson250608a2015-11-24 15:00:31 -0800399 // Measure the history button with the full space above the stack, but width-constrained
400 // to the stack
Winson387aac62015-11-25 11:18:56 -0800401 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
Winsonc29ff002015-11-20 16:00:45 -0800402 measureChild(mHistoryButton,
Winson387aac62015-11-25 11:18:56 -0800403 MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
404 MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
Winson250608a2015-11-24 15:00:31 -0800405 MeasureSpec.EXACTLY));
Winson Chung9a742902015-12-11 10:25:40 -0500406
Winson Chung303e1ff2014-03-07 15:06:19 -0800407 setMeasuredDimension(width, height);
408 }
409
Winson Chungf7bca432014-04-30 17:11:13 -0700410 /**
411 * This is called with the full size of the window since we are handling our own insets.
412 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800413 @Override
414 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700415 RecentsConfiguration config = Recents.getConfiguration();
416
Winson Chungf7bca432014-04-30 17:11:13 -0700417 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700418 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
419 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700420 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700421 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700422 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700423 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
424 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700425 }
426
Winson147ecaf2015-09-16 16:49:55 -0700427 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
428 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800429 }
Winsonbe7607a2015-10-01 17:24:51 -0700430
Winson Chung9a742902015-12-11 10:25:40 -0500431 // Layout the empty view
432 mEmptyView.layout(left, top, right, bottom);
433
Winson250608a2015-11-24 15:00:31 -0800434 // Layout the history button left-aligned with the stack, but offset from the top of the
435 // view
Winson387aac62015-11-25 11:18:56 -0800436 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
437 mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
438 historyButtonRect.right, historyButtonRect.bottom);
Winsonc29ff002015-11-20 16:00:45 -0800439
Winsonb1e71d02015-11-23 12:40:23 -0800440 if (mAwaitingFirstLayout) {
441 mAwaitingFirstLayout = false;
442
443 // If launched via dragging from the nav bar, then we should translate the whole view
444 // down offscreen
445 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
446 if (launchState.launchedViaDragGesture) {
447 setTranslationY(getMeasuredHeight());
Jorim Jaggi0ca88262015-12-07 17:15:16 -0800448 } else {
449 setTranslationY(0f);
Winsonb1e71d02015-11-23 12:40:23 -0800450 }
451 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800452 }
453
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700454 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700455 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700456 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700457 requestLayout();
Winsonc29ff002015-11-20 16:00:45 -0800458 return insets;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700459 }
460
Winsonbe7607a2015-10-01 17:24:51 -0700461 @Override
462 public boolean onInterceptTouchEvent(MotionEvent ev) {
463 return mTouchHandler.onInterceptTouchEvent(ev);
464 }
465
466 @Override
467 public boolean onTouchEvent(MotionEvent ev) {
468 return mTouchHandler.onTouchEvent(ev);
469 }
470
471 @Override
472 protected void dispatchDraw(Canvas canvas) {
473 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700474 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
475 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
476 if (d.getAlpha() > 0) {
477 d.draw(canvas);
478 }
Winsonbe7607a2015-10-01 17:24:51 -0700479 }
480 }
481
Winson4165d3362015-10-10 14:40:35 -0700482 @Override
483 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700484 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
485 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
486 if (d == who) {
487 return true;
488 }
489 }
490 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700491 }
492
Winsonbe7607a2015-10-01 17:24:51 -0700493 /**** EventBus Events ****/
494
Winson Chung48f2cda2015-12-11 13:20:12 -0500495 public final void onBusEvent(LaunchTaskEvent event) {
496 mLastTaskLaunchedWasFreeform = event.task.isFreeformTask();
497 mTransitionHelper.launchTaskFromRecents(mStack, event.task, mTaskStackView, event.taskView,
498 event.screenPinningRequested, event.targetTaskBounds, event.targetTaskStack);
499 }
500
Winsonbe7607a2015-10-01 17:24:51 -0700501 public final void onBusEvent(DragStartEvent event) {
Winson882072b2015-10-12 11:26:33 -0700502 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
503 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700504 }
505
Winsoneca4ab62015-11-04 10:50:28 -0800506 public final void onBusEvent(DragDropTargetChangedEvent event) {
507 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700508 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
509 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
510 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800511 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
512 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700513 }
Winsonbe7607a2015-10-01 17:24:51 -0700514 }
515
516 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800517 // Animate the overlay alpha back to 0
518 updateVisibleDockRegions(null, -1);
519
Winson479f7442015-11-25 15:16:27 -0800520 // Handle the case where we drop onto a dock region
521 if (event.dropTarget instanceof TaskStack.DockState) {
Winsoneca4ab62015-11-04 10:50:28 -0800522 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
523
Winsona0a8ae42015-12-01 10:43:02 -0800524 // Remove the task after it is docked
Winsona0a8ae42015-12-01 10:43:02 -0800525 event.taskView.animate()
526 .alpha(0f)
527 .setDuration(150)
528 .setInterpolator(mFastOutLinearInInterpolator)
529 .setUpdateListener(null)
530 .setListener(null)
531 .withEndAction(new Runnable() {
532 @Override
533 public void run() {
534 mTaskStackView.getStack().removeTask(event.task);
535 }
536 })
537 .withLayer()
538 .start();
539
Winsoneca4ab62015-11-04 10:50:28 -0800540 // Dock the task and launch it
541 SystemServicesProxy ssp = Recents.getSystemServices();
542 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
543 launchTask(event.task, null, INVALID_STACK_ID);
Jorim Jaggi92de6b62015-12-09 15:32:30 -0800544
545 MetricsLogger.action(mContext,
546 MetricsLogger.ACTION_WINDOW_DOCK_DRAG_DROP);
Winsonbe7607a2015-10-01 17:24:51 -0700547 }
548 }
Winson4165d3362015-10-10 14:40:35 -0700549
Jorim Jaggidd98d412015-11-18 15:57:38 -0800550 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800551 if (mTaskStackView.getTaskViews().size() > 0) {
552 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
553 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800554 }
555
556 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800557 ViewPropertyAnimator animator = animate();
Jorim Jaggi8fccfe22015-12-03 16:58:08 -0800558 if (event.velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800559 animator.translationY(getHeight());
560 animator.withEndAction(new Runnable() {
561 @Override
562 public void run() {
563 WindowManagerProxy.getInstance().maximizeDockedStack();
564 }
565 });
566 mFlingAnimationUtils.apply(animator, getTranslationY(), getHeight(), event.velocity);
567 } else {
568 animator.translationY(0f);
569 animator.setListener(null);
570 mFlingAnimationUtils.apply(animator, getTranslationY(), 0, event.velocity);
571 }
572 animator.start();
Jorim Jaggidd98d412015-11-18 15:57:38 -0800573 }
574
Winsonc29ff002015-11-20 16:00:45 -0800575 public final void onBusEvent(ShowHistoryEvent event) {
576 // Hide the history button when the history view is shown
Winson Chung9a742902015-12-11 10:25:40 -0500577 hideHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
578 event.postHideStackAnimationTrigger);
579 event.postHideStackAnimationTrigger.addLastDecrementRunnable(new Runnable() {
580 @Override
581 public void run() {
582 setAlpha(0f);
583 }
584 });
Winsonc29ff002015-11-20 16:00:45 -0800585 }
586
587 public final void onBusEvent(HideHistoryEvent event) {
588 // Show the history button when the history view is hidden
Winson Chung9a742902015-12-11 10:25:40 -0500589 setAlpha(1f);
590 showHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
591 event.postHideHistoryAnimationTrigger);
Winsonc29ff002015-11-20 16:00:45 -0800592 }
593
594 public final void onBusEvent(ShowHistoryButtonEvent event) {
595 showHistoryButton(150);
596 }
597
598 public final void onBusEvent(HideHistoryButtonEvent event) {
599 hideHistoryButton(100);
600 }
601
602 public final void onBusEvent(DebugFlagsChangedEvent event) {
603 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
604 if (!debugFlags.isHistoryEnabled()) {
605 hideHistoryButton(100);
Winson Chungde750de2015-12-11 10:26:06 -0500606 } else {
607 showHistoryButton(100);
Winsonc29ff002015-11-20 16:00:45 -0800608 }
609 }
610
611 /**
612 * Shows the history button.
613 */
Winson Chung9a742902015-12-11 10:25:40 -0500614 private void showHistoryButton(final int duration) {
615 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger(getContext());
616 postAnimationTrigger.increment();
617 showHistoryButton(duration, postAnimationTrigger);
618 postAnimationTrigger.decrement();
619 }
620
621 private void showHistoryButton(final int duration,
622 final ReferenceCountedTrigger postHideHistoryAnimationTrigger) {
Winsonc29ff002015-11-20 16:00:45 -0800623 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
624 if (!debugFlags.isHistoryEnabled()) {
625 return;
626 }
627
628 mHistoryButton.setVisibility(View.VISIBLE);
Winson Chung9a742902015-12-11 10:25:40 -0500629 mHistoryButton.setAlpha(0f);
Winson Chungde750de2015-12-11 10:26:06 -0500630 mHistoryButton.setText(getContext().getString(R.string.recents_history_label_format,
631 mStack.getHistoricalTasks().size()));
Winson Chung9a742902015-12-11 10:25:40 -0500632 postHideHistoryAnimationTrigger.addLastDecrementRunnable(new Runnable() {
633 @Override
634 public void run() {
635 mHistoryButton.animate()
636 .alpha(1f)
637 .setDuration(duration)
638 .setInterpolator(mFastOutSlowInInterpolator)
639 .withLayer()
640 .start();
641 }
642 });
Winsonc29ff002015-11-20 16:00:45 -0800643 }
644
645 /**
646 * Hides the history button.
647 */
648 private void hideHistoryButton(int duration) {
Winson Chung9a742902015-12-11 10:25:40 -0500649 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger(getContext());
650 postAnimationTrigger.increment();
651 hideHistoryButton(duration, postAnimationTrigger);
652 postAnimationTrigger.decrement();
653 }
654
655 private void hideHistoryButton(int duration,
656 final ReferenceCountedTrigger postHideStackAnimationTrigger) {
Winsonc29ff002015-11-20 16:00:45 -0800657 mHistoryButton.animate()
658 .alpha(0f)
659 .setDuration(duration)
660 .setInterpolator(mFastOutLinearInInterpolator)
661 .withEndAction(new Runnable() {
662 @Override
663 public void run() {
664 mHistoryButton.setVisibility(View.INVISIBLE);
Winson Chung9a742902015-12-11 10:25:40 -0500665 postHideStackAnimationTrigger.decrement();
Winsonc29ff002015-11-20 16:00:45 -0800666 }
667 })
668 .withLayer()
669 .start();
Winson Chung9a742902015-12-11 10:25:40 -0500670 postHideStackAnimationTrigger.increment();
Winsonc29ff002015-11-20 16:00:45 -0800671 }
672
Winson4165d3362015-10-10 14:40:35 -0700673 /**
674 * Updates the dock region to match the specified dock state.
675 */
Winson882072b2015-10-12 11:26:33 -0700676 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
677 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
678 if (newDockStates != null) {
679 for (TaskStack.DockState dockState : newDockStates) {
680 newDockStatesSet.add(dockState);
681 }
Winson4165d3362015-10-10 14:40:35 -0700682 }
Winson882072b2015-10-12 11:26:33 -0700683 for (TaskStack.DockState dockState : mVisibleDockStates) {
684 TaskStack.DockState.ViewState viewState = dockState.viewState;
685 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
686 // This is no longer visible, so hide it
687 viewState.startAlphaAnimation(0, 150);
688 } else {
689 // This state is now visible, update the bounds and show it
690 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
691 viewState.dockAreaOverlay.setBounds(
692 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
693 viewState.dockAreaOverlay.setCallback(this);
694 viewState.startAlphaAnimation(alpha, 150);
695 }
Winson4165d3362015-10-10 14:40:35 -0700696 }
Winson4165d3362015-10-10 14:40:35 -0700697 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800698
Winsonb1e71d02015-11-23 12:40:23 -0800699 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
700 if (!event.visible) {
701 // Reset the view state
702 mAwaitingFirstLayout = true;
703 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800704 }
705 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800706}