blob: 9b1315a2220ff7bfb3fe42e0f807215e35916d98 [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 Chung24cf1522014-05-29 12:03:33 -0700265 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700266 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800267 // We have to increment/decrement the post animation trigger in case there are no children
268 // to ensure that it runs
269 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700270 if (mTaskStackView != null) {
271 mTaskStackView.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700272 }
Winson Chung740c3ac2014-11-12 16:14:38 -0800273 ctx.postAnimationTrigger.decrement();
Winson Chung24cf1522014-05-29 12:03:33 -0700274 }
275
Winson Chungd42a6cf2014-06-03 16:24:04 -0700276 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700277 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chunga91c2932014-11-07 15:02:38 -0800278 // We have to increment/decrement the post animation trigger in case there are no children
279 // to ensure that it runs
280 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700281 if (mTaskStackView != null) {
282 mTaskStackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700283 }
Winson Chunga91c2932014-11-07 15:02:38 -0800284 ctx.postAnimationTrigger.decrement();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700285
Winsonc29ff002015-11-20 16:00:45 -0800286 // Hide the history button
287 int taskViewExitToHomeDuration = getResources().getInteger(
288 R.integer.recents_task_exit_to_home_duration);
289 hideHistoryButton(taskViewExitToHomeDuration);
290
Winson13d30662015-11-06 15:30:29 -0800291 // If we are going home, cancel the previous task's window transition
292 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
293
Winsonc29ff002015-11-20 16:00:45 -0800294 // Notify sof the exit animation
Winson412e1802015-10-20 16:57:57 -0700295 EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
Winson Chungd42a6cf2014-06-03 16:24:04 -0700296 }
297
Winson Chungf7bca432014-04-30 17:11:13 -0700298 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700299 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700300 // Remove the previous search bar if one exists
301 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
302 removeView(mSearchBar);
303 }
304 // Add the new search bar
305 if (searchBar != null) {
306 mSearchBar = searchBar;
307 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700308 }
Winson Chungecd9b302014-04-16 17:07:18 -0700309 }
310
Winson Chung772b6b12014-07-03 15:54:02 -0700311 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700312 public boolean hasValidSearchBar() {
313 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700314 }
315
Winson Chung9a742902015-12-11 10:25:40 -0500316 /**
317 * Hides the task stack and shows the empty view.
318 */
319 public void showEmptyView() {
320 if (!RecentsDebugFlags.Static.DisableSearchBar && (mSearchBar != null)) {
321 mSearchBar.setVisibility(View.INVISIBLE);
322 }
323 mTaskStackView.setVisibility(View.INVISIBLE);
324 mEmptyView.setVisibility(View.VISIBLE);
325 mEmptyView.bringToFront();
326 mHistoryButton.bringToFront();
327 }
328
329 /**
330 * Shows the task stack and hides the empty view.
331 */
332 public void hideEmptyView() {
333 mEmptyView.setVisibility(View.INVISIBLE);
334 mTaskStackView.setVisibility(View.VISIBLE);
335 if (!RecentsDebugFlags.Static.DisableSearchBar && (mSearchBar != null)) {
336 mSearchBar.setVisibility(View.VISIBLE);
337 }
338 mTaskStackView.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700339 if (mSearchBar != null) {
Winson Chung012ef362014-07-31 18:36:25 -0700340 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700341 }
Winson Chung9a742902015-12-11 10:25:40 -0500342 mHistoryButton.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700343 }
344
Winsonc29ff002015-11-20 16:00:45 -0800345 /**
346 * Returns the last known system insets.
347 */
348 public Rect getSystemInsets() {
349 return mSystemInsets;
350 }
351
Winsonbe7607a2015-10-01 17:24:51 -0700352 @Override
353 protected void onAttachedToWindow() {
354 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
355 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
356 super.onAttachedToWindow();
357 }
358
359 @Override
360 protected void onDetachedFromWindow() {
361 super.onDetachedFromWindow();
362 EventBus.getDefault().unregister(this);
363 EventBus.getDefault().unregister(mTouchHandler);
364 }
365
Winson Chungf7bca432014-04-30 17:11:13 -0700366 /**
367 * This is called with the full size of the window since we are handling our own insets.
368 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800369 @Override
370 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700371 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800372 int width = MeasureSpec.getSize(widthMeasureSpec);
373 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700374
Winson Chungf7bca432014-04-30 17:11:13 -0700375 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700376 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700377 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700378 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700379 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700380 mSearchBar.measure(
381 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
382 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700383 }
384
Winson Chungf7bca432014-04-30 17:11:13 -0700385 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700386 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700387 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700388 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700389 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700390 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800391 }
392
Winson Chung9a742902015-12-11 10:25:40 -0500393 // Measure the empty view
394 measureChild(mEmptyView, MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
395 MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
396
Winson250608a2015-11-24 15:00:31 -0800397 // Measure the history button with the full space above the stack, but width-constrained
398 // to the stack
Winson387aac62015-11-25 11:18:56 -0800399 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
Winsonc29ff002015-11-20 16:00:45 -0800400 measureChild(mHistoryButton,
Winson387aac62015-11-25 11:18:56 -0800401 MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
402 MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
Winson250608a2015-11-24 15:00:31 -0800403 MeasureSpec.EXACTLY));
Winson Chung9a742902015-12-11 10:25:40 -0500404
Winson Chung303e1ff2014-03-07 15:06:19 -0800405 setMeasuredDimension(width, height);
406 }
407
Winson Chungf7bca432014-04-30 17:11:13 -0700408 /**
409 * This is called with the full size of the window since we are handling our own insets.
410 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800411 @Override
412 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700413 RecentsConfiguration config = Recents.getConfiguration();
414
Winson Chungf7bca432014-04-30 17:11:13 -0700415 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700416 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
417 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700418 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700419 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700420 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700421 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
422 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700423 }
424
Winson147ecaf2015-09-16 16:49:55 -0700425 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
426 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800427 }
Winsonbe7607a2015-10-01 17:24:51 -0700428
Winson Chung9a742902015-12-11 10:25:40 -0500429 // Layout the empty view
430 mEmptyView.layout(left, top, right, bottom);
431
Winson250608a2015-11-24 15:00:31 -0800432 // Layout the history button left-aligned with the stack, but offset from the top of the
433 // view
Winson387aac62015-11-25 11:18:56 -0800434 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
435 mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
436 historyButtonRect.right, historyButtonRect.bottom);
Winsonc29ff002015-11-20 16:00:45 -0800437
Winsonb1e71d02015-11-23 12:40:23 -0800438 if (mAwaitingFirstLayout) {
439 mAwaitingFirstLayout = false;
440
441 // If launched via dragging from the nav bar, then we should translate the whole view
442 // down offscreen
443 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
444 if (launchState.launchedViaDragGesture) {
445 setTranslationY(getMeasuredHeight());
Jorim Jaggi0ca88262015-12-07 17:15:16 -0800446 } else {
447 setTranslationY(0f);
Winsonb1e71d02015-11-23 12:40:23 -0800448 }
449 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800450 }
451
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700452 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700453 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700454 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700455 requestLayout();
Winsonc29ff002015-11-20 16:00:45 -0800456 return insets;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700457 }
458
Winsonbe7607a2015-10-01 17:24:51 -0700459 @Override
460 public boolean onInterceptTouchEvent(MotionEvent ev) {
461 return mTouchHandler.onInterceptTouchEvent(ev);
462 }
463
464 @Override
465 public boolean onTouchEvent(MotionEvent ev) {
466 return mTouchHandler.onTouchEvent(ev);
467 }
468
469 @Override
470 protected void dispatchDraw(Canvas canvas) {
471 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700472 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
473 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
474 if (d.getAlpha() > 0) {
475 d.draw(canvas);
476 }
Winsonbe7607a2015-10-01 17:24:51 -0700477 }
478 }
479
Winson4165d3362015-10-10 14:40:35 -0700480 @Override
481 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700482 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
483 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
484 if (d == who) {
485 return true;
486 }
487 }
488 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700489 }
490
Winsonbe7607a2015-10-01 17:24:51 -0700491 /**** EventBus Events ****/
492
Winson Chung48f2cda2015-12-11 13:20:12 -0500493 public final void onBusEvent(LaunchTaskEvent event) {
494 mLastTaskLaunchedWasFreeform = event.task.isFreeformTask();
495 mTransitionHelper.launchTaskFromRecents(mStack, event.task, mTaskStackView, event.taskView,
496 event.screenPinningRequested, event.targetTaskBounds, event.targetTaskStack);
497 }
498
Winsonbe7607a2015-10-01 17:24:51 -0700499 public final void onBusEvent(DragStartEvent event) {
Winson882072b2015-10-12 11:26:33 -0700500 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
501 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700502 }
503
Winsoneca4ab62015-11-04 10:50:28 -0800504 public final void onBusEvent(DragDropTargetChangedEvent event) {
505 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700506 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
507 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
508 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800509 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
510 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700511 }
Winsonbe7607a2015-10-01 17:24:51 -0700512 }
513
514 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800515 // Animate the overlay alpha back to 0
516 updateVisibleDockRegions(null, -1);
517
Winson479f7442015-11-25 15:16:27 -0800518 // Handle the case where we drop onto a dock region
519 if (event.dropTarget instanceof TaskStack.DockState) {
Winsoneca4ab62015-11-04 10:50:28 -0800520 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
521
Winsona0a8ae42015-12-01 10:43:02 -0800522 // Remove the task after it is docked
Winsona0a8ae42015-12-01 10:43:02 -0800523 event.taskView.animate()
524 .alpha(0f)
525 .setDuration(150)
526 .setInterpolator(mFastOutLinearInInterpolator)
527 .setUpdateListener(null)
528 .setListener(null)
529 .withEndAction(new Runnable() {
530 @Override
531 public void run() {
532 mTaskStackView.getStack().removeTask(event.task);
533 }
534 })
535 .withLayer()
536 .start();
537
Winsoneca4ab62015-11-04 10:50:28 -0800538 // Dock the task and launch it
539 SystemServicesProxy ssp = Recents.getSystemServices();
540 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
541 launchTask(event.task, null, INVALID_STACK_ID);
Jorim Jaggi92de6b62015-12-09 15:32:30 -0800542
543 MetricsLogger.action(mContext,
544 MetricsLogger.ACTION_WINDOW_DOCK_DRAG_DROP);
Winsonbe7607a2015-10-01 17:24:51 -0700545 }
546 }
Winson4165d3362015-10-10 14:40:35 -0700547
Jorim Jaggidd98d412015-11-18 15:57:38 -0800548 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800549 if (mTaskStackView.getTaskViews().size() > 0) {
550 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
551 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800552 }
553
554 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800555 ViewPropertyAnimator animator = animate();
Jorim Jaggi8fccfe22015-12-03 16:58:08 -0800556 if (event.velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800557 animator.translationY(getHeight());
558 animator.withEndAction(new Runnable() {
559 @Override
560 public void run() {
561 WindowManagerProxy.getInstance().maximizeDockedStack();
562 }
563 });
564 mFlingAnimationUtils.apply(animator, getTranslationY(), getHeight(), event.velocity);
565 } else {
566 animator.translationY(0f);
567 animator.setListener(null);
568 mFlingAnimationUtils.apply(animator, getTranslationY(), 0, event.velocity);
569 }
570 animator.start();
Jorim Jaggidd98d412015-11-18 15:57:38 -0800571 }
572
Winsonc29ff002015-11-20 16:00:45 -0800573 public final void onBusEvent(ShowHistoryEvent event) {
574 // Hide the history button when the history view is shown
Winson Chung9a742902015-12-11 10:25:40 -0500575 hideHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500576 event.getAnimationTrigger());
577 event.addPostAnimationCallback(new Runnable() {
Winson Chung9a742902015-12-11 10:25:40 -0500578 @Override
579 public void run() {
580 setAlpha(0f);
581 }
582 });
Winsonc29ff002015-11-20 16:00:45 -0800583 }
584
585 public final void onBusEvent(HideHistoryEvent event) {
586 // Show the history button when the history view is hidden
Winson Chung9a742902015-12-11 10:25:40 -0500587 setAlpha(1f);
588 showHistoryButton(getResources().getInteger(R.integer.recents_history_transition_duration),
Winson Chungaaeaac12015-12-16 16:49:36 -0500589 event.getAnimationTrigger());
Winsonc29ff002015-11-20 16:00:45 -0800590 }
591
592 public final void onBusEvent(ShowHistoryButtonEvent event) {
593 showHistoryButton(150);
594 }
595
596 public final void onBusEvent(HideHistoryButtonEvent event) {
597 hideHistoryButton(100);
598 }
599
Winson Chung06266772015-12-11 10:24:21 -0500600 public final void onBusEvent(TaskStackUpdatedEvent event) {
601 mStack.setTasks(event.stack.computeAllTasksList(), true /* notifyStackChanges */);
Winson Chung97567552015-12-16 17:07:19 -0500602 mStack.createAffiliatedGroupings(getContext());
Winson Chung06266772015-12-11 10:24:21 -0500603 }
604
Winsonc29ff002015-11-20 16:00:45 -0800605 /**
606 * Shows the history button.
607 */
Winson Chung9a742902015-12-11 10:25:40 -0500608 private void showHistoryButton(final int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500609 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500610 showHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500611 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500612 }
613
614 private void showHistoryButton(final int duration,
615 final ReferenceCountedTrigger postHideHistoryAnimationTrigger) {
Winsonc29ff002015-11-20 16:00:45 -0800616 mHistoryButton.setVisibility(View.VISIBLE);
Winson Chung9a742902015-12-11 10:25:40 -0500617 mHistoryButton.setAlpha(0f);
Winson Chungde750de2015-12-11 10:26:06 -0500618 mHistoryButton.setText(getContext().getString(R.string.recents_history_label_format,
619 mStack.getHistoricalTasks().size()));
Winson Chung9a742902015-12-11 10:25:40 -0500620 postHideHistoryAnimationTrigger.addLastDecrementRunnable(new Runnable() {
621 @Override
622 public void run() {
623 mHistoryButton.animate()
624 .alpha(1f)
625 .setDuration(duration)
626 .setInterpolator(mFastOutSlowInInterpolator)
627 .withLayer()
628 .start();
629 }
630 });
Winsonc29ff002015-11-20 16:00:45 -0800631 }
632
633 /**
634 * Hides the history button.
635 */
636 private void hideHistoryButton(int duration) {
Winson Chungaaeaac12015-12-16 16:49:36 -0500637 ReferenceCountedTrigger postAnimationTrigger = new ReferenceCountedTrigger();
Winson Chung9a742902015-12-11 10:25:40 -0500638 hideHistoryButton(duration, postAnimationTrigger);
Winson Chungaaeaac12015-12-16 16:49:36 -0500639 postAnimationTrigger.flushLastDecrementRunnables();
Winson Chung9a742902015-12-11 10:25:40 -0500640 }
641
642 private void hideHistoryButton(int duration,
643 final ReferenceCountedTrigger postHideStackAnimationTrigger) {
Winsonc29ff002015-11-20 16:00:45 -0800644 mHistoryButton.animate()
645 .alpha(0f)
646 .setDuration(duration)
647 .setInterpolator(mFastOutLinearInInterpolator)
648 .withEndAction(new Runnable() {
649 @Override
650 public void run() {
651 mHistoryButton.setVisibility(View.INVISIBLE);
Winson Chung9a742902015-12-11 10:25:40 -0500652 postHideStackAnimationTrigger.decrement();
Winsonc29ff002015-11-20 16:00:45 -0800653 }
654 })
655 .withLayer()
656 .start();
Winson Chung9a742902015-12-11 10:25:40 -0500657 postHideStackAnimationTrigger.increment();
Winsonc29ff002015-11-20 16:00:45 -0800658 }
659
Winson4165d3362015-10-10 14:40:35 -0700660 /**
661 * Updates the dock region to match the specified dock state.
662 */
Winson882072b2015-10-12 11:26:33 -0700663 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
664 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
665 if (newDockStates != null) {
666 for (TaskStack.DockState dockState : newDockStates) {
667 newDockStatesSet.add(dockState);
668 }
Winson4165d3362015-10-10 14:40:35 -0700669 }
Winson882072b2015-10-12 11:26:33 -0700670 for (TaskStack.DockState dockState : mVisibleDockStates) {
671 TaskStack.DockState.ViewState viewState = dockState.viewState;
672 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
673 // This is no longer visible, so hide it
674 viewState.startAlphaAnimation(0, 150);
675 } else {
676 // This state is now visible, update the bounds and show it
677 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
678 viewState.dockAreaOverlay.setBounds(
679 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
680 viewState.dockAreaOverlay.setCallback(this);
681 viewState.startAlphaAnimation(alpha, 150);
682 }
Winson4165d3362015-10-10 14:40:35 -0700683 }
Winson4165d3362015-10-10 14:40:35 -0700684 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800685
Winsonb1e71d02015-11-23 12:40:23 -0800686 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
687 if (!event.visible) {
688 // Reset the view state
689 mAwaitingFirstLayout = true;
690 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800691 }
692 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800693}