blob: 56160187fed4fadf8e1555d6f4eeeea3c96e84db [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;
Winson Chung653f70c22014-05-19 14:49:42 -070030import android.view.WindowInsets;
Winson35f30502015-09-28 11:24:36 -070031import android.view.animation.AnimationUtils;
32import android.view.animation.Interpolator;
Winson Chung303e1ff2014-03-07 15:06:19 -080033import android.widget.FrameLayout;
Winson Chungd16c5652015-01-26 16:11:07 -080034import com.android.systemui.R;
Winsone7f138c2015-10-22 16:15:21 -070035import com.android.systemui.recents.Recents;
Winsonbe7607a2015-10-01 17:24:51 -070036import com.android.systemui.recents.RecentsActivity;
Winsonb1e71d02015-11-23 12:40:23 -080037import com.android.systemui.recents.RecentsActivityLaunchState;
Winson Chungaee097c2015-04-02 18:16:02 -070038import com.android.systemui.recents.RecentsAppWidgetHostView;
Winson Chung303e1ff2014-03-07 15:06:19 -080039import com.android.systemui.recents.RecentsConfiguration;
Winsonc29ff002015-11-20 16:00:45 -080040import com.android.systemui.recents.RecentsDebugFlags;
Winson2536c7e2015-10-01 15:49:31 -070041import com.android.systemui.recents.events.EventBus;
Winson13d30662015-11-06 15:30:29 -080042import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
Winsonc29ff002015-11-20 16:00:45 -080043import com.android.systemui.recents.events.activity.DebugFlagsChangedEvent;
Winson412e1802015-10-20 16:57:57 -070044import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
Winsonc29ff002015-11-20 16:00:45 -080045import com.android.systemui.recents.events.activity.HideHistoryButtonEvent;
46import com.android.systemui.recents.events.activity.HideHistoryEvent;
47import com.android.systemui.recents.events.activity.ShowHistoryButtonEvent;
48import com.android.systemui.recents.events.activity.ShowHistoryEvent;
Winsonb1e71d02015-11-23 12:40:23 -080049import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
Jorim Jaggidd98d412015-11-18 15:57:38 -080050import com.android.systemui.recents.events.ui.DraggingInRecentsEndedEvent;
51import com.android.systemui.recents.events.ui.DraggingInRecentsEvent;
Winsoneca4ab62015-11-04 10:50:28 -080052import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
Winsonbe7607a2015-10-01 17:24:51 -070053import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
54import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
Winson Chung1f24c7e2014-07-11 17:06:48 -070055import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chung303e1ff2014-03-07 15:06:19 -080056import com.android.systemui.recents.model.Task;
57import com.android.systemui.recents.model.TaskStack;
58
59import java.util.ArrayList;
Winson Chung6ac8bd62015-01-07 16:38:35 -080060import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080061
Wale Ogunwale3797c222015-10-27 14:21:58 -070062import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070063
Winson Chung303e1ff2014-03-07 15:06:19 -080064/**
65 * This view is the the top level layout that contains TaskStacks (which are laid out according
66 * to their SpaceNode bounds.
67 */
Winsone6c90732015-09-24 16:06:29 -070068public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks {
Winson Chung47c4c692014-03-17 10:17:11 -070069
Jorim Jaggi6e18e002015-06-02 17:07:39 -070070 private static final String TAG = "RecentsView";
Winson0d14d4d2015-10-26 17:05:04 -070071 private static final boolean DEBUG = false;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070072
Winson83c1b072015-11-09 10:48:04 -080073 Handler mHandler;
Winson Chungd42a6cf2014-06-03 16:24:04 -070074
Winsonc29ff002015-11-20 16:00:45 -080075 TaskStack mStack;
Winson147ecaf2015-09-16 16:49:55 -070076 TaskStackView mTaskStackView;
Winson Chungaee097c2015-04-02 18:16:02 -070077 RecentsAppWidgetHostView mSearchBar;
Winsonc29ff002015-11-20 16:00:45 -080078 View mHistoryButton;
Winsonb1e71d02015-11-23 12:40:23 -080079 boolean mAwaitingFirstLayout = true;
80 boolean mLastTaskLaunchedWasFreeform;
Winsonbe7607a2015-10-01 17:24:51 -070081
Winson83c1b072015-11-09 10:48:04 -080082 RecentsTransitionHelper mTransitionHelper;
Winsonbe7607a2015-10-01 17:24:51 -070083 RecentsViewTouchHandler mTouchHandler;
Winson882072b2015-10-12 11:26:33 -070084 TaskStack.DockState[] mVisibleDockStates = {
85 TaskStack.DockState.LEFT,
86 TaskStack.DockState.TOP,
87 TaskStack.DockState.RIGHT,
88 TaskStack.DockState.BOTTOM,
89 };
Winsonbe7607a2015-10-01 17:24:51 -070090
Winsonc29ff002015-11-20 16:00:45 -080091 private Interpolator mFastOutSlowInInterpolator;
92 private Interpolator mFastOutLinearInInterpolator;
93 private int mHistoryTransitionDuration;
Winson35f30502015-09-28 11:24:36 -070094
95 Rect mSystemInsets = new Rect();
Winson Chung303e1ff2014-03-07 15:06:19 -080096
97 public RecentsView(Context context) {
98 super(context);
Winson Chung8e548f72014-06-24 14:40:53 -070099 }
100
101 public RecentsView(Context context, AttributeSet attrs) {
102 this(context, attrs, 0);
103 }
104
105 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr) {
106 this(context, attrs, defStyleAttr, 0);
107 }
108
109 public RecentsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
110 super(context, attrs, defStyleAttr, defStyleRes);
Winsonc29ff002015-11-20 16:00:45 -0800111 Resources res = context.getResources();
Winsonbe7607a2015-10-01 17:24:51 -0700112 setWillNotDraw(false);
Winson83c1b072015-11-09 10:48:04 -0800113 mHandler = new Handler();
114 mTransitionHelper = new RecentsTransitionHelper(getContext(), mHandler);
Winson35f30502015-09-28 11:24:36 -0700115 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
116 com.android.internal.R.interpolator.fast_out_slow_in);
Winsonc29ff002015-11-20 16:00:45 -0800117 mFastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
118 com.android.internal.R.interpolator.fast_out_linear_in);
119 mHistoryTransitionDuration = res.getInteger(R.integer.recents_history_transition_duration);
Winsonbe7607a2015-10-01 17:24:51 -0700120 mTouchHandler = new RecentsViewTouchHandler(this);
Winsonc29ff002015-11-20 16:00:45 -0800121
122 LayoutInflater inflater = LayoutInflater.from(context);
123 mHistoryButton = inflater.inflate(R.layout.recents_history_button, this, false);
124 mHistoryButton.setOnClickListener(new View.OnClickListener() {
125 @Override
126 public void onClick(View v) {
127 EventBus.getDefault().send(new ShowHistoryEvent());
128 }
129 });
Winson Chungd16c5652015-01-26 16:11:07 -0800130 }
131
Winson Chungdcfa7972014-07-22 12:27:13 -0700132 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700133 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700134 RecentsConfiguration config = Recents.getConfiguration();
Winsonc29ff002015-11-20 16:00:45 -0800135 mStack = stack;
Winson53ec42c2015-10-28 15:55:35 -0700136 if (config.getLaunchState().launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700137 if (mTaskStackView != null) {
138 // If onRecentsHidden is not triggered, we need to the stack view again here
139 mTaskStackView.reset();
140 mTaskStackView.setStack(stack);
141 } else {
142 mTaskStackView = new TaskStackView(getContext(), stack);
143 mTaskStackView.setCallbacks(this);
144 addView(mTaskStackView);
145 }
146 } else {
147 if (mTaskStackView != null) {
148 removeView(mTaskStackView);
149 }
150 mTaskStackView = new TaskStackView(getContext(), stack);
151 mTaskStackView.setCallbacks(this);
152 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800153 }
Winsonc29ff002015-11-20 16:00:45 -0800154 if (indexOfChild(mHistoryButton) == -1) {
155 addView(mHistoryButton);
156 } else {
157 mHistoryButton.bringToFront();
158 }
Winson Chung02d49272014-08-29 13:57:29 -0700159
Winson Chungb0a28ea2014-10-28 15:21:35 -0700160 // Trigger a new layout
161 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700162 }
163
Winsonb1e71d02015-11-23 12:40:23 -0800164 /**
165 * Returns whether the last task launched was in the freeform stack or not.
166 */
167 public boolean isLastTaskLaunchedFreeform() {
168 return mLastTaskLaunchedWasFreeform;
169 }
170
Winsonc29ff002015-11-20 16:00:45 -0800171 /**
172 * Returns the currently set task stack.
173 */
174 public TaskStack getTaskStack() {
175 return mStack;
176 }
177
Skuhne8aa7d162015-03-20 13:40:53 -0700178 /** Gets the next task in the stack - or if the last - the top task */
179 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700180 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700181 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700182 if (mTaskStackView != null) {
183 TaskStack stack = mTaskStackView.getStack();
Winson250608a2015-11-24 15:00:31 -0800184 ArrayList<Task> taskList = stack.getStackTasks();
Skuhne8aa7d162015-03-20 13:40:53 -0700185 // Iterate the stack views and try and find the focused task
186 for (int j = taskList.size() - 1; j >= 0; --j) {
187 Task task = taskList.get(j);
188 // Return the next task in the line.
189 if (found)
190 return task;
191 // Remember the first possible task as the top task.
192 if (returnTask == null)
193 returnTask = task;
194 if (task == taskToSearch)
195 found = true;
196 }
197 }
198 return returnTask;
199 }
200
Winson Chung1e8d71b2014-05-16 17:05:22 -0700201 /** Launches the focused task from the first stack if possible */
202 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700203 if (mTaskStackView != null) {
204 TaskStack stack = mTaskStackView.getStack();
Winson142af422015-11-09 10:39:57 -0800205 Task task = mTaskStackView.getFocusedTask();
206 if (task != null) {
207 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson83c1b072015-11-09 10:48:04 -0800208 onTaskViewClicked(mTaskStackView, taskView, stack, task, false, null,
Winson142af422015-11-09 10:39:57 -0800209 INVALID_STACK_ID);
210 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700211 }
212 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700213 return false;
214 }
215
Skuhne8aa7d162015-03-20 13:40:53 -0700216 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700217 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700218 if (mTaskStackView != null) {
219 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700220 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700221 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700222 int taskViewCount = taskViews.size();
223 for (int j = 0; j < taskViewCount; j++) {
224 TaskView tv = taskViews.get(j);
225 if (tv.getTask() == task) {
Winson83c1b072015-11-09 10:48:04 -0800226 onTaskViewClicked(mTaskStackView, tv, stack, task, false,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700227 taskBounds, destinationStack);
Skuhne8aa7d162015-03-20 13:40:53 -0700228 return true;
229 }
230 }
231 }
232 return false;
233 }
234
Winson Chung24cf1522014-05-29 12:03:33 -0700235 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700236 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800237 // We have to increment/decrement the post animation trigger in case there are no children
238 // to ensure that it runs
239 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700240 if (mTaskStackView != null) {
241 mTaskStackView.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700242 }
Winson Chung740c3ac2014-11-12 16:14:38 -0800243 ctx.postAnimationTrigger.decrement();
Winson Chung24cf1522014-05-29 12:03:33 -0700244 }
245
Winson Chungd42a6cf2014-06-03 16:24:04 -0700246 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700247 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chunga91c2932014-11-07 15:02:38 -0800248 // We have to increment/decrement the post animation trigger in case there are no children
249 // to ensure that it runs
250 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700251 if (mTaskStackView != null) {
252 mTaskStackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700253 }
Winson Chunga91c2932014-11-07 15:02:38 -0800254 ctx.postAnimationTrigger.decrement();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700255
Winsonc29ff002015-11-20 16:00:45 -0800256 // Hide the history button
257 int taskViewExitToHomeDuration = getResources().getInteger(
258 R.integer.recents_task_exit_to_home_duration);
259 hideHistoryButton(taskViewExitToHomeDuration);
260
Winson13d30662015-11-06 15:30:29 -0800261 // If we are going home, cancel the previous task's window transition
262 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
263
Winsonc29ff002015-11-20 16:00:45 -0800264 // Notify sof the exit animation
Winson412e1802015-10-20 16:57:57 -0700265 EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
Winson Chungd42a6cf2014-06-03 16:24:04 -0700266 }
267
Winson Chungf7bca432014-04-30 17:11:13 -0700268 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700269 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700270 // Remove the previous search bar if one exists
271 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
272 removeView(mSearchBar);
273 }
274 // Add the new search bar
275 if (searchBar != null) {
276 mSearchBar = searchBar;
277 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700278 }
Winson Chungecd9b302014-04-16 17:07:18 -0700279 }
280
Winson Chung772b6b12014-07-03 15:54:02 -0700281 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700282 public boolean hasValidSearchBar() {
283 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700284 }
285
286 /** Sets the visibility of the search bar */
287 public void setSearchBarVisibility(int visibility) {
288 if (mSearchBar != null) {
289 mSearchBar.setVisibility(visibility);
Winson Chung012ef362014-07-31 18:36:25 -0700290 // Always bring the search bar to the top
291 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700292 }
293 }
294
Winsonc29ff002015-11-20 16:00:45 -0800295 /**
296 * Returns the last known system insets.
297 */
298 public Rect getSystemInsets() {
299 return mSystemInsets;
300 }
301
Winsonbe7607a2015-10-01 17:24:51 -0700302 @Override
303 protected void onAttachedToWindow() {
304 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
305 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
306 super.onAttachedToWindow();
307 }
308
309 @Override
310 protected void onDetachedFromWindow() {
311 super.onDetachedFromWindow();
312 EventBus.getDefault().unregister(this);
313 EventBus.getDefault().unregister(mTouchHandler);
314 }
315
Winson Chungf7bca432014-04-30 17:11:13 -0700316 /**
317 * This is called with the full size of the window since we are handling our own insets.
318 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800319 @Override
320 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700321 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800322 int width = MeasureSpec.getSize(widthMeasureSpec);
323 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700324
Winson Chungf7bca432014-04-30 17:11:13 -0700325 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700326 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700327 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700328 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700329 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700330 mSearchBar.measure(
331 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
332 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700333 }
334
Winson Chungf7bca432014-04-30 17:11:13 -0700335 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700336 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700337 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700338 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700339 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700340 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800341 }
342
Winson250608a2015-11-24 15:00:31 -0800343 // Measure the history button with the full space above the stack, but width-constrained
344 // to the stack
Winson387aac62015-11-25 11:18:56 -0800345 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
Winsonc29ff002015-11-20 16:00:45 -0800346 measureChild(mHistoryButton,
Winson387aac62015-11-25 11:18:56 -0800347 MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
348 MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
Winson250608a2015-11-24 15:00:31 -0800349 MeasureSpec.EXACTLY));
Winson Chung303e1ff2014-03-07 15:06:19 -0800350 setMeasuredDimension(width, height);
351 }
352
Winson Chungf7bca432014-04-30 17:11:13 -0700353 /**
354 * This is called with the full size of the window since we are handling our own insets.
355 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800356 @Override
357 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700358 RecentsConfiguration config = Recents.getConfiguration();
359
Winson Chungf7bca432014-04-30 17:11:13 -0700360 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700361 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
362 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700363 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700364 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700365 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700366 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
367 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700368 }
369
Winson147ecaf2015-09-16 16:49:55 -0700370 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
371 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800372 }
Winsonbe7607a2015-10-01 17:24:51 -0700373
Winson250608a2015-11-24 15:00:31 -0800374 // Layout the history button left-aligned with the stack, but offset from the top of the
375 // view
Winson387aac62015-11-25 11:18:56 -0800376 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
377 mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
378 historyButtonRect.right, historyButtonRect.bottom);
Winsonc29ff002015-11-20 16:00:45 -0800379
Winsonb1e71d02015-11-23 12:40:23 -0800380 if (mAwaitingFirstLayout) {
381 mAwaitingFirstLayout = false;
382
383 // If launched via dragging from the nav bar, then we should translate the whole view
384 // down offscreen
385 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
386 if (launchState.launchedViaDragGesture) {
387 setTranslationY(getMeasuredHeight());
388 }
389 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800390 }
391
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700392 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700393 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700394 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700395 requestLayout();
Winsonc29ff002015-11-20 16:00:45 -0800396 return insets;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700397 }
398
Winsonbe7607a2015-10-01 17:24:51 -0700399 @Override
400 public boolean onInterceptTouchEvent(MotionEvent ev) {
401 return mTouchHandler.onInterceptTouchEvent(ev);
402 }
403
404 @Override
405 public boolean onTouchEvent(MotionEvent ev) {
406 return mTouchHandler.onTouchEvent(ev);
407 }
408
409 @Override
410 protected void dispatchDraw(Canvas canvas) {
411 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700412 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
413 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
414 if (d.getAlpha() > 0) {
415 d.draw(canvas);
416 }
Winsonbe7607a2015-10-01 17:24:51 -0700417 }
418 }
419
Winson4165d3362015-10-10 14:40:35 -0700420 @Override
421 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700422 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
423 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
424 if (d == who) {
425 return true;
426 }
427 }
428 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700429 }
430
Jorim Jaggi900fb482015-06-02 15:07:33 -0700431 public void disableLayersForOneFrame() {
Winson147ecaf2015-09-16 16:49:55 -0700432 if (mTaskStackView != null) {
433 mTaskStackView.disableLayersForOneFrame();
Jorim Jaggi900fb482015-06-02 15:07:33 -0700434 }
435 }
436
Winson Chung47c4c692014-03-17 10:17:11 -0700437 /**** TaskStackView.TaskStackCallbacks Implementation ****/
Winson Chung303e1ff2014-03-07 15:06:19 -0800438
439 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700440 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700441 final TaskStack stack, final Task task, final boolean lockToTask,
Winson83c1b072015-11-09 10:48:04 -0800442 final Rect bounds, int destinationStack) {
Winson387aac62015-11-25 11:18:56 -0800443 mLastTaskLaunchedWasFreeform = task.isFreeformTask();
Winson83c1b072015-11-09 10:48:04 -0800444 mTransitionHelper.launchTaskFromRecents(stack, task, stackView, tv, lockToTask, bounds,
445 destinationStack);
Winson Chung303e1ff2014-03-07 15:06:19 -0800446 }
Winson Chung9f9679d2014-04-11 16:49:09 -0700447
Winsonbe7607a2015-10-01 17:24:51 -0700448 /**** EventBus Events ****/
449
450 public final void onBusEvent(DragStartEvent event) {
Winson882072b2015-10-12 11:26:33 -0700451 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
452 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700453 }
454
Winsoneca4ab62015-11-04 10:50:28 -0800455 public final void onBusEvent(DragDropTargetChangedEvent event) {
456 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700457 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
458 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
459 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800460 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
461 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700462 }
Winsonbe7607a2015-10-01 17:24:51 -0700463 }
464
465 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800466 // Animate the overlay alpha back to 0
467 updateVisibleDockRegions(null, -1);
468
Winson479f7442015-11-25 15:16:27 -0800469 // Handle the case where we drop onto a dock region
470 if (event.dropTarget instanceof TaskStack.DockState) {
Winsoneca4ab62015-11-04 10:50:28 -0800471 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
472
Winsoneca4ab62015-11-04 10:50:28 -0800473 // Dock the task and launch it
474 SystemServicesProxy ssp = Recents.getSystemServices();
475 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
476 launchTask(event.task, null, INVALID_STACK_ID);
Winsonbe7607a2015-10-01 17:24:51 -0700477 }
478 }
Winson4165d3362015-10-10 14:40:35 -0700479
Jorim Jaggidd98d412015-11-18 15:57:38 -0800480 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800481 if (mTaskStackView.getTaskViews().size() > 0) {
482 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
483 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800484 }
485
486 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
487 animate().translationY(0f);
488 }
489
Winsonc29ff002015-11-20 16:00:45 -0800490 public final void onBusEvent(ShowHistoryEvent event) {
491 // Hide the history button when the history view is shown
492 hideHistoryButton(mHistoryTransitionDuration);
493 }
494
495 public final void onBusEvent(HideHistoryEvent event) {
496 // Show the history button when the history view is hidden
497 showHistoryButton(mHistoryTransitionDuration);
498 }
499
500 public final void onBusEvent(ShowHistoryButtonEvent event) {
501 showHistoryButton(150);
502 }
503
504 public final void onBusEvent(HideHistoryButtonEvent event) {
505 hideHistoryButton(100);
506 }
507
508 public final void onBusEvent(DebugFlagsChangedEvent event) {
509 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
510 if (!debugFlags.isHistoryEnabled()) {
511 hideHistoryButton(100);
512 }
513 }
514
515 /**
516 * Shows the history button.
517 */
518 private void showHistoryButton(int duration) {
519 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
520 if (!debugFlags.isHistoryEnabled()) {
521 return;
522 }
523
524 mHistoryButton.setVisibility(View.VISIBLE);
525 mHistoryButton.animate()
526 .alpha(1f)
527 .setDuration(duration)
528 .setInterpolator(mFastOutSlowInInterpolator)
529 .withLayer()
530 .start();
531 }
532
533 /**
534 * Hides the history button.
535 */
536 private void hideHistoryButton(int duration) {
537 mHistoryButton.animate()
538 .alpha(0f)
539 .setDuration(duration)
540 .setInterpolator(mFastOutLinearInInterpolator)
541 .withEndAction(new Runnable() {
542 @Override
543 public void run() {
544 mHistoryButton.setVisibility(View.INVISIBLE);
545 }
546 })
547 .withLayer()
548 .start();
549 }
550
Winson4165d3362015-10-10 14:40:35 -0700551 /**
552 * Updates the dock region to match the specified dock state.
553 */
Winson882072b2015-10-12 11:26:33 -0700554 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
555 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
556 if (newDockStates != null) {
557 for (TaskStack.DockState dockState : newDockStates) {
558 newDockStatesSet.add(dockState);
559 }
Winson4165d3362015-10-10 14:40:35 -0700560 }
Winson882072b2015-10-12 11:26:33 -0700561 for (TaskStack.DockState dockState : mVisibleDockStates) {
562 TaskStack.DockState.ViewState viewState = dockState.viewState;
563 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
564 // This is no longer visible, so hide it
565 viewState.startAlphaAnimation(0, 150);
566 } else {
567 // This state is now visible, update the bounds and show it
568 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
569 viewState.dockAreaOverlay.setBounds(
570 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
571 viewState.dockAreaOverlay.setCallback(this);
572 viewState.startAlphaAnimation(alpha, 150);
573 }
Winson4165d3362015-10-10 14:40:35 -0700574 }
Winson4165d3362015-10-10 14:40:35 -0700575 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800576
Winsonb1e71d02015-11-23 12:40:23 -0800577 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
578 if (!event.visible) {
579 // Reset the view state
580 mAwaitingFirstLayout = true;
581 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800582 }
583 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800584}