blob: 7ae686e42602b219e2a0c43a9e6caa85e50849fd [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
Jorim Jaggi870ab5a2015-12-02 18:37:54 -080019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Winson Chung303e1ff2014-03-07 15:06:19 -080021import android.content.Context;
Winsonc29ff002015-11-20 16:00:45 -080022import android.content.res.Resources;
Winson Chung303e1ff2014-03-07 15:06:19 -080023import android.graphics.Canvas;
24import android.graphics.Rect;
Winson4165d3362015-10-10 14:40:35 -070025import android.graphics.drawable.Drawable;
Winson83c1b072015-11-09 10:48:04 -080026import android.os.Handler;
Winson882072b2015-10-12 11:26:33 -070027import android.util.ArraySet;
Winson Chung8e548f72014-06-24 14:40:53 -070028import android.util.AttributeSet;
Winson Chungecd9b302014-04-16 17:07:18 -070029import android.view.LayoutInflater;
Winsonbe7607a2015-10-01 17:24:51 -070030import android.view.MotionEvent;
Winsonc29ff002015-11-20 16:00:45 -080031import android.view.View;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -080032import android.view.ViewConfiguration;
33import android.view.ViewPropertyAnimator;
Winson Chung653f70c22014-05-19 14:49:42 -070034import android.view.WindowInsets;
Winson35f30502015-09-28 11:24:36 -070035import android.view.animation.AnimationUtils;
36import android.view.animation.Interpolator;
Winson Chung303e1ff2014-03-07 15:06:19 -080037import android.widget.FrameLayout;
Winson Chungd16c5652015-01-26 16:11:07 -080038import com.android.systemui.R;
Winsone7f138c2015-10-22 16:15:21 -070039import com.android.systemui.recents.Recents;
Winsonbe7607a2015-10-01 17:24:51 -070040import com.android.systemui.recents.RecentsActivity;
Winsonb1e71d02015-11-23 12:40:23 -080041import com.android.systemui.recents.RecentsActivityLaunchState;
Winson Chungaee097c2015-04-02 18:16:02 -070042import com.android.systemui.recents.RecentsAppWidgetHostView;
Winson Chung303e1ff2014-03-07 15:06:19 -080043import com.android.systemui.recents.RecentsConfiguration;
Winsonc29ff002015-11-20 16:00:45 -080044import com.android.systemui.recents.RecentsDebugFlags;
Winson2536c7e2015-10-01 15:49:31 -070045import com.android.systemui.recents.events.EventBus;
Winson13d30662015-11-06 15:30:29 -080046import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
Winsonc29ff002015-11-20 16:00:45 -080047import com.android.systemui.recents.events.activity.DebugFlagsChangedEvent;
Winson412e1802015-10-20 16:57:57 -070048import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
Winsonc29ff002015-11-20 16:00:45 -080049import com.android.systemui.recents.events.activity.HideHistoryButtonEvent;
50import com.android.systemui.recents.events.activity.HideHistoryEvent;
51import 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 Chung1f24c7e2014-07-11 17:06:48 -070059import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chung303e1ff2014-03-07 15:06:19 -080060import com.android.systemui.recents.model.Task;
61import com.android.systemui.recents.model.TaskStack;
Jorim Jaggi870ab5a2015-12-02 18:37:54 -080062import com.android.systemui.stackdivider.WindowManagerProxy;
63import com.android.systemui.statusbar.FlingAnimationUtils;
Winson Chung303e1ff2014-03-07 15:06:19 -080064
65import java.util.ArrayList;
Winson Chung6ac8bd62015-01-07 16:38:35 -080066import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080067
Wale Ogunwale3797c222015-10-27 14:21:58 -070068import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Winson412e1802015-10-20 16:57:57 -070069
Winson Chung303e1ff2014-03-07 15:06:19 -080070/**
71 * This view is the the top level layout that contains TaskStacks (which are laid out according
72 * to their SpaceNode bounds.
73 */
Winsone6c90732015-09-24 16:06:29 -070074public class RecentsView extends FrameLayout implements TaskStackView.TaskStackViewCallbacks {
Winson Chung47c4c692014-03-17 10:17:11 -070075
Jorim Jaggi6e18e002015-06-02 17:07:39 -070076 private static final String TAG = "RecentsView";
Winson0d14d4d2015-10-26 17:05:04 -070077 private static final boolean DEBUG = false;
Jorim Jaggi6e18e002015-06-02 17:07:39 -070078
Winson83c1b072015-11-09 10:48:04 -080079 Handler mHandler;
Winson Chungd42a6cf2014-06-03 16:24:04 -070080
Winsonc29ff002015-11-20 16:00:45 -080081 TaskStack mStack;
Winson147ecaf2015-09-16 16:49:55 -070082 TaskStackView mTaskStackView;
Winson Chungaee097c2015-04-02 18:16:02 -070083 RecentsAppWidgetHostView mSearchBar;
Winsonc29ff002015-11-20 16:00:45 -080084 View mHistoryButton;
Winsonb1e71d02015-11-23 12:40:23 -080085 boolean mAwaitingFirstLayout = true;
86 boolean mLastTaskLaunchedWasFreeform;
Winsonbe7607a2015-10-01 17:24:51 -070087
Winson83c1b072015-11-09 10:48:04 -080088 RecentsTransitionHelper mTransitionHelper;
Winsonbe7607a2015-10-01 17:24:51 -070089 RecentsViewTouchHandler mTouchHandler;
Winson882072b2015-10-12 11:26:33 -070090 TaskStack.DockState[] mVisibleDockStates = {
91 TaskStack.DockState.LEFT,
92 TaskStack.DockState.TOP,
93 TaskStack.DockState.RIGHT,
94 TaskStack.DockState.BOTTOM,
95 };
Winsonbe7607a2015-10-01 17:24:51 -070096
Winsonc29ff002015-11-20 16:00:45 -080097 private Interpolator mFastOutSlowInInterpolator;
98 private Interpolator mFastOutLinearInInterpolator;
99 private int mHistoryTransitionDuration;
Winson35f30502015-09-28 11:24:36 -0700100
101 Rect mSystemInsets = new Rect();
Winson Chung303e1ff2014-03-07 15:06:19 -0800102
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800103 final FlingAnimationUtils mFlingAnimationUtils;
104
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);
127 mHistoryTransitionDuration = res.getInteger(R.integer.recents_history_transition_duration);
Winsonbe7607a2015-10-01 17:24:51 -0700128 mTouchHandler = new RecentsViewTouchHandler(this);
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800129 mFlingAnimationUtils = new FlingAnimationUtils(context, 0.3f);
Winsonc29ff002015-11-20 16:00:45 -0800130
131 LayoutInflater inflater = LayoutInflater.from(context);
132 mHistoryButton = inflater.inflate(R.layout.recents_history_button, this, false);
133 mHistoryButton.setOnClickListener(new View.OnClickListener() {
134 @Override
135 public void onClick(View v) {
136 EventBus.getDefault().send(new ShowHistoryEvent());
137 }
138 });
Winson Chungd16c5652015-01-26 16:11:07 -0800139 }
140
Winson Chungdcfa7972014-07-22 12:27:13 -0700141 /** Set/get the bsp root node */
Winson147ecaf2015-09-16 16:49:55 -0700142 public void setTaskStack(TaskStack stack) {
Winson53ec42c2015-10-28 15:55:35 -0700143 RecentsConfiguration config = Recents.getConfiguration();
Winsonc29ff002015-11-20 16:00:45 -0800144 mStack = stack;
Winson53ec42c2015-10-28 15:55:35 -0700145 if (config.getLaunchState().launchedReuseTaskStackViews) {
Winson147ecaf2015-09-16 16:49:55 -0700146 if (mTaskStackView != null) {
147 // If onRecentsHidden is not triggered, we need to the stack view again here
148 mTaskStackView.reset();
149 mTaskStackView.setStack(stack);
Winson160f4c02015-12-02 19:06:44 -0800150 removeView(mTaskStackView);
151 addView(mTaskStackView);
Winson147ecaf2015-09-16 16:49:55 -0700152 } else {
153 mTaskStackView = new TaskStackView(getContext(), stack);
154 mTaskStackView.setCallbacks(this);
155 addView(mTaskStackView);
156 }
157 } else {
158 if (mTaskStackView != null) {
159 removeView(mTaskStackView);
160 }
161 mTaskStackView = new TaskStackView(getContext(), stack);
162 mTaskStackView.setCallbacks(this);
163 addView(mTaskStackView);
Winson Chung303e1ff2014-03-07 15:06:19 -0800164 }
Winsonc29ff002015-11-20 16:00:45 -0800165 if (indexOfChild(mHistoryButton) == -1) {
166 addView(mHistoryButton);
167 } else {
168 mHistoryButton.bringToFront();
169 }
Winson Chung02d49272014-08-29 13:57:29 -0700170
Winson Chungb0a28ea2014-10-28 15:21:35 -0700171 // Trigger a new layout
172 requestLayout();
Winson Chung19fc1172014-07-31 18:40:03 -0700173 }
174
Winsonb1e71d02015-11-23 12:40:23 -0800175 /**
176 * Returns whether the last task launched was in the freeform stack or not.
177 */
178 public boolean isLastTaskLaunchedFreeform() {
179 return mLastTaskLaunchedWasFreeform;
180 }
181
Winsonc29ff002015-11-20 16:00:45 -0800182 /**
183 * Returns the currently set task stack.
184 */
185 public TaskStack getTaskStack() {
186 return mStack;
187 }
188
Skuhne8aa7d162015-03-20 13:40:53 -0700189 /** Gets the next task in the stack - or if the last - the top task */
190 public Task getNextTaskOrTopTask(Task taskToSearch) {
Chong Zhang0fa656b2015-08-31 15:17:21 -0700191 Task returnTask = null;
Skuhne8aa7d162015-03-20 13:40:53 -0700192 boolean found = false;
Winson147ecaf2015-09-16 16:49:55 -0700193 if (mTaskStackView != null) {
194 TaskStack stack = mTaskStackView.getStack();
Winson250608a2015-11-24 15:00:31 -0800195 ArrayList<Task> taskList = stack.getStackTasks();
Skuhne8aa7d162015-03-20 13:40:53 -0700196 // Iterate the stack views and try and find the focused task
197 for (int j = taskList.size() - 1; j >= 0; --j) {
198 Task task = taskList.get(j);
199 // Return the next task in the line.
200 if (found)
201 return task;
202 // Remember the first possible task as the top task.
203 if (returnTask == null)
204 returnTask = task;
205 if (task == taskToSearch)
206 found = true;
207 }
208 }
209 return returnTask;
210 }
211
Winson Chung1e8d71b2014-05-16 17:05:22 -0700212 /** Launches the focused task from the first stack if possible */
213 public boolean launchFocusedTask() {
Winson147ecaf2015-09-16 16:49:55 -0700214 if (mTaskStackView != null) {
215 TaskStack stack = mTaskStackView.getStack();
Winson142af422015-11-09 10:39:57 -0800216 Task task = mTaskStackView.getFocusedTask();
217 if (task != null) {
218 TaskView taskView = mTaskStackView.getChildViewForTask(task);
Winson83c1b072015-11-09 10:48:04 -0800219 onTaskViewClicked(mTaskStackView, taskView, stack, task, false, null,
Winson142af422015-11-09 10:39:57 -0800220 INVALID_STACK_ID);
221 return true;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700222 }
223 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700224 return false;
225 }
226
Winsona0731a12015-12-02 15:10:14 -0800227 /** Launches the task that recents was launched from if possible */
228 public boolean launchPreviousTask() {
229 if (mTaskStackView != null) {
230 TaskStack stack = mTaskStackView.getStack();
231 Task task = stack.getLaunchTarget();
232 if (task != null) {
233 TaskView taskView = mTaskStackView.getChildViewForTask(task);
234 onTaskViewClicked(mTaskStackView, taskView, stack, task, false, null,
235 INVALID_STACK_ID);
236 return true;
237 }
238 }
239 return false;
240 }
241
Skuhne8aa7d162015-03-20 13:40:53 -0700242 /** Launches a given task. */
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700243 public boolean launchTask(Task task, Rect taskBounds, int destinationStack) {
Winson147ecaf2015-09-16 16:49:55 -0700244 if (mTaskStackView != null) {
245 TaskStack stack = mTaskStackView.getStack();
Skuhne8aa7d162015-03-20 13:40:53 -0700246 // Iterate the stack views and try and find the given task.
Winson147ecaf2015-09-16 16:49:55 -0700247 List<TaskView> taskViews = mTaskStackView.getTaskViews();
Skuhne8aa7d162015-03-20 13:40:53 -0700248 int taskViewCount = taskViews.size();
249 for (int j = 0; j < taskViewCount; j++) {
250 TaskView tv = taskViews.get(j);
251 if (tv.getTask() == task) {
Winson83c1b072015-11-09 10:48:04 -0800252 onTaskViewClicked(mTaskStackView, tv, stack, task, false,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700253 taskBounds, destinationStack);
Skuhne8aa7d162015-03-20 13:40:53 -0700254 return true;
255 }
256 }
257 }
258 return false;
259 }
260
Winson Chung24cf1522014-05-29 12:03:33 -0700261 /** Requests all task stacks to start their enter-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700262 public void startEnterRecentsAnimation(ViewAnimation.TaskViewEnterContext ctx) {
Winson Chung740c3ac2014-11-12 16:14:38 -0800263 // We have to increment/decrement the post animation trigger in case there are no children
264 // to ensure that it runs
265 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700266 if (mTaskStackView != null) {
267 mTaskStackView.startEnterRecentsAnimation(ctx);
Winson Chung24cf1522014-05-29 12:03:33 -0700268 }
Winson Chung740c3ac2014-11-12 16:14:38 -0800269 ctx.postAnimationTrigger.decrement();
Winson Chung24cf1522014-05-29 12:03:33 -0700270 }
271
Winson Chungd42a6cf2014-06-03 16:24:04 -0700272 /** Requests all task stacks to start their exit-recents animation */
Winson Chung969f5862014-06-16 17:08:24 -0700273 public void startExitToHomeAnimation(ViewAnimation.TaskViewExitContext ctx) {
Winson Chunga91c2932014-11-07 15:02:38 -0800274 // We have to increment/decrement the post animation trigger in case there are no children
275 // to ensure that it runs
276 ctx.postAnimationTrigger.increment();
Winson147ecaf2015-09-16 16:49:55 -0700277 if (mTaskStackView != null) {
278 mTaskStackView.startExitToHomeAnimation(ctx);
Winson Chungd42a6cf2014-06-03 16:24:04 -0700279 }
Winson Chunga91c2932014-11-07 15:02:38 -0800280 ctx.postAnimationTrigger.decrement();
Winson Chungd42a6cf2014-06-03 16:24:04 -0700281
Winsonc29ff002015-11-20 16:00:45 -0800282 // Hide the history button
283 int taskViewExitToHomeDuration = getResources().getInteger(
284 R.integer.recents_task_exit_to_home_duration);
285 hideHistoryButton(taskViewExitToHomeDuration);
286
Winson13d30662015-11-06 15:30:29 -0800287 // If we are going home, cancel the previous task's window transition
288 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
289
Winsonc29ff002015-11-20 16:00:45 -0800290 // Notify sof the exit animation
Winson412e1802015-10-20 16:57:57 -0700291 EventBus.getDefault().send(new DismissRecentsToHomeAnimationStarted());
Winson Chungd42a6cf2014-06-03 16:24:04 -0700292 }
293
Winson Chungf7bca432014-04-30 17:11:13 -0700294 /** Adds the search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700295 public void setSearchBar(RecentsAppWidgetHostView searchBar) {
Winson Chungaf3bb692015-06-03 17:31:39 -0700296 // Remove the previous search bar if one exists
297 if (mSearchBar != null && indexOfChild(mSearchBar) > -1) {
298 removeView(mSearchBar);
299 }
300 // Add the new search bar
301 if (searchBar != null) {
302 mSearchBar = searchBar;
303 addView(mSearchBar);
Winson Chungf7bca432014-04-30 17:11:13 -0700304 }
Winson Chungecd9b302014-04-16 17:07:18 -0700305 }
306
Winson Chung772b6b12014-07-03 15:54:02 -0700307 /** Returns whether there is currently a search bar */
Winson Chungaee097c2015-04-02 18:16:02 -0700308 public boolean hasValidSearchBar() {
309 return mSearchBar != null && !mSearchBar.isReinflateRequired();
Winson Chung772b6b12014-07-03 15:54:02 -0700310 }
311
312 /** Sets the visibility of the search bar */
313 public void setSearchBarVisibility(int visibility) {
314 if (mSearchBar != null) {
315 mSearchBar.setVisibility(visibility);
Winson Chung012ef362014-07-31 18:36:25 -0700316 // Always bring the search bar to the top
317 mSearchBar.bringToFront();
Winson Chung772b6b12014-07-03 15:54:02 -0700318 }
319 }
320
Winsonc29ff002015-11-20 16:00:45 -0800321 /**
322 * Returns the last known system insets.
323 */
324 public Rect getSystemInsets() {
325 return mSystemInsets;
326 }
327
Winsonbe7607a2015-10-01 17:24:51 -0700328 @Override
329 protected void onAttachedToWindow() {
330 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
331 EventBus.getDefault().register(mTouchHandler, RecentsActivity.EVENT_BUS_PRIORITY + 1);
332 super.onAttachedToWindow();
333 }
334
335 @Override
336 protected void onDetachedFromWindow() {
337 super.onDetachedFromWindow();
338 EventBus.getDefault().unregister(this);
339 EventBus.getDefault().unregister(mTouchHandler);
340 }
341
Winson Chungf7bca432014-04-30 17:11:13 -0700342 /**
343 * This is called with the full size of the window since we are handling our own insets.
344 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800345 @Override
346 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson53ec42c2015-10-28 15:55:35 -0700347 RecentsConfiguration config = Recents.getConfiguration();
Winson Chung303e1ff2014-03-07 15:06:19 -0800348 int width = MeasureSpec.getSize(widthMeasureSpec);
349 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungbd912972014-03-18 14:36:35 -0700350
Winson Chungf7bca432014-04-30 17:11:13 -0700351 // Get the search bar bounds and measure the search bar layout
Winson Chungaf3bb692015-06-03 17:31:39 -0700352 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700353 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700354 config.getSearchBarBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson147ecaf2015-09-16 16:49:55 -0700355 searchBarSpaceBounds);
Winson Chungf7bca432014-04-30 17:11:13 -0700356 mSearchBar.measure(
357 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.width(), MeasureSpec.EXACTLY),
358 MeasureSpec.makeMeasureSpec(searchBarSpaceBounds.height(), MeasureSpec.EXACTLY));
Winson Chungecd9b302014-04-16 17:07:18 -0700359 }
360
Winson Chungf7bca432014-04-30 17:11:13 -0700361 Rect taskStackBounds = new Rect();
Winson53ec42c2015-10-28 15:55:35 -0700362 config.getTaskStackBounds(new Rect(0, 0, width, height), mSystemInsets.top,
Winson35f30502015-09-28 11:24:36 -0700363 mSystemInsets.right, searchBarSpaceBounds, taskStackBounds);
Winson147ecaf2015-09-16 16:49:55 -0700364 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
Winson88f00ab2015-10-05 17:24:00 -0700365 mTaskStackView.setTaskStackBounds(taskStackBounds, mSystemInsets);
Winson147ecaf2015-09-16 16:49:55 -0700366 mTaskStackView.measure(widthMeasureSpec, heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -0800367 }
368
Winson250608a2015-11-24 15:00:31 -0800369 // Measure the history button with the full space above the stack, but width-constrained
370 // to the stack
Winson387aac62015-11-25 11:18:56 -0800371 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
Winsonc29ff002015-11-20 16:00:45 -0800372 measureChild(mHistoryButton,
Winson387aac62015-11-25 11:18:56 -0800373 MeasureSpec.makeMeasureSpec(historyButtonRect.width(), MeasureSpec.EXACTLY),
374 MeasureSpec.makeMeasureSpec(historyButtonRect.height(),
Winson250608a2015-11-24 15:00:31 -0800375 MeasureSpec.EXACTLY));
Winson Chung303e1ff2014-03-07 15:06:19 -0800376 setMeasuredDimension(width, height);
377 }
378
Winson Chungf7bca432014-04-30 17:11:13 -0700379 /**
380 * This is called with the full size of the window since we are handling our own insets.
381 */
Winson Chung303e1ff2014-03-07 15:06:19 -0800382 @Override
383 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson53ec42c2015-10-28 15:55:35 -0700384 RecentsConfiguration config = Recents.getConfiguration();
385
Winson Chungf7bca432014-04-30 17:11:13 -0700386 // Get the search bar bounds so that we lay it out
Winson147ecaf2015-09-16 16:49:55 -0700387 Rect measuredRect = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
388 Rect searchBarSpaceBounds = new Rect();
Winson Chungecd9b302014-04-16 17:07:18 -0700389 if (mSearchBar != null) {
Winson53ec42c2015-10-28 15:55:35 -0700390 config.getSearchBarBounds(measuredRect,
Winson35f30502015-09-28 11:24:36 -0700391 mSystemInsets.top, searchBarSpaceBounds);
Winson Chungdcfa7972014-07-22 12:27:13 -0700392 mSearchBar.layout(searchBarSpaceBounds.left, searchBarSpaceBounds.top,
393 searchBarSpaceBounds.right, searchBarSpaceBounds.bottom);
Winson Chungecd9b302014-04-16 17:07:18 -0700394 }
395
Winson147ecaf2015-09-16 16:49:55 -0700396 if (mTaskStackView != null && mTaskStackView.getVisibility() != GONE) {
397 mTaskStackView.layout(left, top, left + getMeasuredWidth(), top + getMeasuredHeight());
Winson Chung303e1ff2014-03-07 15:06:19 -0800398 }
Winsonbe7607a2015-10-01 17:24:51 -0700399
Winson250608a2015-11-24 15:00:31 -0800400 // Layout the history button left-aligned with the stack, but offset from the top of the
401 // view
Winson387aac62015-11-25 11:18:56 -0800402 Rect historyButtonRect = mTaskStackView.mLayoutAlgorithm.mHistoryButtonRect;
403 mHistoryButton.layout(historyButtonRect.left, historyButtonRect.top,
404 historyButtonRect.right, historyButtonRect.bottom);
Winsonc29ff002015-11-20 16:00:45 -0800405
Winsonb1e71d02015-11-23 12:40:23 -0800406 if (mAwaitingFirstLayout) {
407 mAwaitingFirstLayout = false;
408
409 // If launched via dragging from the nav bar, then we should translate the whole view
410 // down offscreen
411 RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
412 if (launchState.launchedViaDragGesture) {
413 setTranslationY(getMeasuredHeight());
Jorim Jaggi0ca88262015-12-07 17:15:16 -0800414 } else {
415 setTranslationY(0f);
Winsonb1e71d02015-11-23 12:40:23 -0800416 }
417 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800418 }
419
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700420 @Override
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700421 public WindowInsets onApplyWindowInsets(WindowInsets insets) {
Winson35f30502015-09-28 11:24:36 -0700422 mSystemInsets.set(insets.getSystemWindowInsets());
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700423 requestLayout();
Winsonc29ff002015-11-20 16:00:45 -0800424 return insets;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700425 }
426
Winsonbe7607a2015-10-01 17:24:51 -0700427 @Override
428 public boolean onInterceptTouchEvent(MotionEvent ev) {
429 return mTouchHandler.onInterceptTouchEvent(ev);
430 }
431
432 @Override
433 public boolean onTouchEvent(MotionEvent ev) {
434 return mTouchHandler.onTouchEvent(ev);
435 }
436
437 @Override
438 protected void dispatchDraw(Canvas canvas) {
439 super.dispatchDraw(canvas);
Winson882072b2015-10-12 11:26:33 -0700440 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
441 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
442 if (d.getAlpha() > 0) {
443 d.draw(canvas);
444 }
Winsonbe7607a2015-10-01 17:24:51 -0700445 }
446 }
447
Winson4165d3362015-10-10 14:40:35 -0700448 @Override
449 protected boolean verifyDrawable(Drawable who) {
Winson882072b2015-10-12 11:26:33 -0700450 for (int i = mVisibleDockStates.length - 1; i >= 0; i--) {
451 Drawable d = mVisibleDockStates[i].viewState.dockAreaOverlay;
452 if (d == who) {
453 return true;
454 }
455 }
456 return super.verifyDrawable(who);
Winson4165d3362015-10-10 14:40:35 -0700457 }
458
Winson Chung47c4c692014-03-17 10:17:11 -0700459 /**** TaskStackView.TaskStackCallbacks Implementation ****/
Winson Chung303e1ff2014-03-07 15:06:19 -0800460
461 @Override
Winson Chung7aceb9a2014-07-03 13:38:01 -0700462 public void onTaskViewClicked(final TaskStackView stackView, final TaskView tv,
Filip Gruszczynskif48b5852015-10-13 10:28:22 -0700463 final TaskStack stack, final Task task, final boolean lockToTask,
Winson83c1b072015-11-09 10:48:04 -0800464 final Rect bounds, int destinationStack) {
Winson387aac62015-11-25 11:18:56 -0800465 mLastTaskLaunchedWasFreeform = task.isFreeformTask();
Winson83c1b072015-11-09 10:48:04 -0800466 mTransitionHelper.launchTaskFromRecents(stack, task, stackView, tv, lockToTask, bounds,
467 destinationStack);
Winson Chung303e1ff2014-03-07 15:06:19 -0800468 }
Winson Chung9f9679d2014-04-11 16:49:09 -0700469
Winsonbe7607a2015-10-01 17:24:51 -0700470 /**** EventBus Events ****/
471
472 public final void onBusEvent(DragStartEvent event) {
Winson882072b2015-10-12 11:26:33 -0700473 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
474 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
Winsonbe7607a2015-10-01 17:24:51 -0700475 }
476
Winsoneca4ab62015-11-04 10:50:28 -0800477 public final void onBusEvent(DragDropTargetChangedEvent event) {
478 if (event.dropTarget == null || !(event.dropTarget instanceof TaskStack.DockState)) {
Winson882072b2015-10-12 11:26:33 -0700479 updateVisibleDockRegions(mTouchHandler.getDockStatesForCurrentOrientation(),
480 TaskStack.DockState.NONE.viewState.dockAreaAlpha);
481 } else {
Winsoneca4ab62015-11-04 10:50:28 -0800482 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
483 updateVisibleDockRegions(new TaskStack.DockState[] {dockState}, -1);
Winson882072b2015-10-12 11:26:33 -0700484 }
Winsonbe7607a2015-10-01 17:24:51 -0700485 }
486
487 public final void onBusEvent(final DragEndEvent event) {
Winsoneca4ab62015-11-04 10:50:28 -0800488 // Animate the overlay alpha back to 0
489 updateVisibleDockRegions(null, -1);
490
Winson479f7442015-11-25 15:16:27 -0800491 // Handle the case where we drop onto a dock region
492 if (event.dropTarget instanceof TaskStack.DockState) {
Winsoneca4ab62015-11-04 10:50:28 -0800493 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
494
Winsona0a8ae42015-12-01 10:43:02 -0800495 // Remove the task after it is docked
Winsona0a8ae42015-12-01 10:43:02 -0800496 event.taskView.animate()
497 .alpha(0f)
498 .setDuration(150)
499 .setInterpolator(mFastOutLinearInInterpolator)
500 .setUpdateListener(null)
501 .setListener(null)
502 .withEndAction(new Runnable() {
503 @Override
504 public void run() {
505 mTaskStackView.getStack().removeTask(event.task);
506 }
507 })
508 .withLayer()
509 .start();
510
Winsoneca4ab62015-11-04 10:50:28 -0800511 // Dock the task and launch it
512 SystemServicesProxy ssp = Recents.getSystemServices();
513 ssp.startTaskInDockedMode(event.task.key.id, dockState.createMode);
514 launchTask(event.task, null, INVALID_STACK_ID);
Winsonbe7607a2015-10-01 17:24:51 -0700515 }
516 }
Winson4165d3362015-10-10 14:40:35 -0700517
Jorim Jaggidd98d412015-11-18 15:57:38 -0800518 public final void onBusEvent(DraggingInRecentsEvent event) {
Jorim Jaggi54b1a6d2015-11-24 17:48:26 -0800519 if (mTaskStackView.getTaskViews().size() > 0) {
520 setTranslationY(event.distanceFromTop - mTaskStackView.getTaskViews().get(0).getY());
521 }
Jorim Jaggidd98d412015-11-18 15:57:38 -0800522 }
523
524 public final void onBusEvent(DraggingInRecentsEndedEvent event) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800525 ViewPropertyAnimator animator = animate();
Jorim Jaggi8fccfe22015-12-03 16:58:08 -0800526 if (event.velocity > mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
Jorim Jaggi870ab5a2015-12-02 18:37:54 -0800527 animator.translationY(getHeight());
528 animator.withEndAction(new Runnable() {
529 @Override
530 public void run() {
531 WindowManagerProxy.getInstance().maximizeDockedStack();
532 }
533 });
534 mFlingAnimationUtils.apply(animator, getTranslationY(), getHeight(), event.velocity);
535 } else {
536 animator.translationY(0f);
537 animator.setListener(null);
538 mFlingAnimationUtils.apply(animator, getTranslationY(), 0, event.velocity);
539 }
540 animator.start();
Jorim Jaggidd98d412015-11-18 15:57:38 -0800541 }
542
Winsonc29ff002015-11-20 16:00:45 -0800543 public final void onBusEvent(ShowHistoryEvent event) {
544 // Hide the history button when the history view is shown
545 hideHistoryButton(mHistoryTransitionDuration);
546 }
547
548 public final void onBusEvent(HideHistoryEvent event) {
549 // Show the history button when the history view is hidden
550 showHistoryButton(mHistoryTransitionDuration);
551 }
552
553 public final void onBusEvent(ShowHistoryButtonEvent event) {
554 showHistoryButton(150);
555 }
556
557 public final void onBusEvent(HideHistoryButtonEvent event) {
558 hideHistoryButton(100);
559 }
560
561 public final void onBusEvent(DebugFlagsChangedEvent event) {
562 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
563 if (!debugFlags.isHistoryEnabled()) {
564 hideHistoryButton(100);
565 }
566 }
567
568 /**
569 * Shows the history button.
570 */
571 private void showHistoryButton(int duration) {
572 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
573 if (!debugFlags.isHistoryEnabled()) {
574 return;
575 }
576
577 mHistoryButton.setVisibility(View.VISIBLE);
578 mHistoryButton.animate()
579 .alpha(1f)
580 .setDuration(duration)
581 .setInterpolator(mFastOutSlowInInterpolator)
582 .withLayer()
583 .start();
584 }
585
586 /**
587 * Hides the history button.
588 */
589 private void hideHistoryButton(int duration) {
590 mHistoryButton.animate()
591 .alpha(0f)
592 .setDuration(duration)
593 .setInterpolator(mFastOutLinearInInterpolator)
594 .withEndAction(new Runnable() {
595 @Override
596 public void run() {
597 mHistoryButton.setVisibility(View.INVISIBLE);
598 }
599 })
600 .withLayer()
601 .start();
602 }
603
Winson4165d3362015-10-10 14:40:35 -0700604 /**
605 * Updates the dock region to match the specified dock state.
606 */
Winson882072b2015-10-12 11:26:33 -0700607 private void updateVisibleDockRegions(TaskStack.DockState[] newDockStates, int overrideAlpha) {
608 ArraySet<TaskStack.DockState> newDockStatesSet = new ArraySet<>();
609 if (newDockStates != null) {
610 for (TaskStack.DockState dockState : newDockStates) {
611 newDockStatesSet.add(dockState);
612 }
Winson4165d3362015-10-10 14:40:35 -0700613 }
Winson882072b2015-10-12 11:26:33 -0700614 for (TaskStack.DockState dockState : mVisibleDockStates) {
615 TaskStack.DockState.ViewState viewState = dockState.viewState;
616 if (newDockStates == null || !newDockStatesSet.contains(dockState)) {
617 // This is no longer visible, so hide it
618 viewState.startAlphaAnimation(0, 150);
619 } else {
620 // This state is now visible, update the bounds and show it
621 int alpha = (overrideAlpha != -1 ? overrideAlpha : viewState.dockAreaAlpha);
622 viewState.dockAreaOverlay.setBounds(
623 dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight()));
624 viewState.dockAreaOverlay.setCallback(this);
625 viewState.startAlphaAnimation(alpha, 150);
626 }
Winson4165d3362015-10-10 14:40:35 -0700627 }
Winson4165d3362015-10-10 14:40:35 -0700628 }
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800629
Winsonb1e71d02015-11-23 12:40:23 -0800630 public final void onBusEvent(RecentsVisibilityChangedEvent event) {
631 if (!event.visible) {
632 // Reset the view state
633 mAwaitingFirstLayout = true;
634 mLastTaskLaunchedWasFreeform = false;
Filip Gruszczynski14b4e572015-11-03 15:53:55 -0800635 }
636 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800637}