blob: 2601cd327982c414f78d3971e8d32ed7f77bc8d2 [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
Winson70f0bf72016-02-01 14:05:29 -080019import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
20import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
21import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
22
Winsona78a8f32015-12-03 10:55:01 -080023import android.animation.ObjectAnimator;
Winson Chungbf5dbf12014-09-16 00:58:25 +020024import android.animation.ValueAnimator;
Winson Chung9f49df92014-05-07 18:08:34 -070025import android.content.ComponentName;
Winson Chung303e1ff2014-03-07 15:06:19 -080026import android.content.Context;
Winsonbb410952015-12-04 14:34:11 -080027import android.content.res.Resources;
Jorim Jaggi900fb482015-06-02 15:07:33 -070028import android.graphics.Canvas;
Winson Chung303e1ff2014-03-07 15:06:19 -080029import android.graphics.Rect;
Winsona78a8f32015-12-03 10:55:01 -080030import android.graphics.drawable.Drawable;
Winsonde0591a2015-12-04 17:24:35 -080031import android.graphics.drawable.GradientDrawable;
Winson Chung83ea6f72015-06-17 13:00:23 -070032import android.os.Bundle;
Winson Chung4ab4d832015-12-11 10:25:46 -050033import android.os.Parcelable;
Winson Chung931c51f2015-12-17 17:08:55 -050034import android.provider.Settings;
Winson55003902016-01-12 12:00:37 -080035import android.util.ArrayMap;
36import android.util.ArraySet;
Winson8aa99592016-01-19 15:07:07 -080037import android.util.MutableBoolean;
Winson Chung37c8d8e2014-03-24 14:53:07 -070038import android.view.LayoutInflater;
Winson Chung303e1ff2014-03-07 15:06:19 -080039import android.view.MotionEvent;
Winson Chung303e1ff2014-03-07 15:06:19 -080040import android.view.View;
Winson231bc9c2016-02-09 12:31:00 -080041import android.view.ViewDebug;
Winson70f0bf72016-02-01 14:05:29 -080042import android.view.ViewGroup;
Winson Chungee445952014-09-09 16:12:59 +020043import android.view.accessibility.AccessibilityEvent;
Winson Chung83ea6f72015-06-17 13:00:23 -070044import android.view.accessibility.AccessibilityNodeInfo;
Winson Chung303e1ff2014-03-07 15:06:19 -080045import android.widget.FrameLayout;
Winsonc0d70582016-01-29 10:24:39 -080046
Winson42329522016-02-05 10:39:46 -080047import com.android.internal.logging.MetricsLogger;
48import com.android.internal.logging.MetricsProto.MetricsEvent;
Winsonc0d70582016-01-29 10:24:39 -080049import com.android.systemui.Interpolators;
Winson Chungc6a16232014-04-01 14:04:48 -070050import com.android.systemui.R;
Winsone7f138c2015-10-22 16:15:21 -070051import com.android.systemui.recents.Recents;
Winsone6c90732015-09-24 16:06:29 -070052import com.android.systemui.recents.RecentsActivity;
Winson2536c7e2015-10-01 15:49:31 -070053import com.android.systemui.recents.RecentsActivityLaunchState;
Winson Chung303e1ff2014-03-07 15:06:19 -080054import com.android.systemui.recents.RecentsConfiguration;
Winson4b9cded2016-01-26 16:26:47 -080055import com.android.systemui.recents.RecentsDebugFlags;
Winsone6c90732015-09-24 16:06:29 -070056import com.android.systemui.recents.events.EventBus;
Winsone5f1faa2015-11-20 12:26:23 -080057import com.android.systemui.recents.events.activity.CancelEnterRecentsWindowAnimationEvent;
Winsone693aaf2016-03-01 12:05:59 -080058import com.android.systemui.recents.events.activity.ConfigurationChangedEvent;
Winsonef064132016-01-05 12:11:31 -080059import com.android.systemui.recents.events.activity.DismissRecentsToHomeAnimationStarted;
Winson4b9cded2016-01-26 16:26:47 -080060import com.android.systemui.recents.events.activity.EnterRecentsTaskStackAnimationCompletedEvent;
Winsone5f1faa2015-11-20 12:26:23 -080061import com.android.systemui.recents.events.activity.EnterRecentsWindowAnimationCompletedEvent;
Winsonc29ff002015-11-20 16:00:45 -080062import com.android.systemui.recents.events.activity.HideHistoryButtonEvent;
63import com.android.systemui.recents.events.activity.HideHistoryEvent;
Winson8b1871d2015-11-20 09:56:20 -080064import com.android.systemui.recents.events.activity.IterateRecentsEvent;
Winsonb61e6542016-02-04 14:37:18 -080065import com.android.systemui.recents.events.activity.LaunchNextTaskRequestEvent;
Winson Chung48f2cda2015-12-11 13:20:12 -050066import com.android.systemui.recents.events.activity.LaunchTaskEvent;
Winsonef064132016-01-05 12:11:31 -080067import com.android.systemui.recents.events.activity.LaunchTaskStartedEvent;
Winson88737542016-02-17 13:27:33 -080068import com.android.systemui.recents.events.activity.MultiWindowStateChangedEvent;
Winsone6c90732015-09-24 16:06:29 -070069import com.android.systemui.recents.events.activity.PackagesChangedEvent;
Winsonc29ff002015-11-20 16:00:45 -080070import com.android.systemui.recents.events.activity.ShowHistoryButtonEvent;
71import com.android.systemui.recents.events.activity.ShowHistoryEvent;
Winson397ae742015-11-20 11:27:33 -080072import com.android.systemui.recents.events.ui.AllTaskViewsDismissedEvent;
Winsonef064132016-01-05 12:11:31 -080073import com.android.systemui.recents.events.ui.DeleteTaskDataEvent;
Winson0d14d4d2015-10-26 17:05:04 -070074import com.android.systemui.recents.events.ui.DismissTaskViewEvent;
Winsonef064132016-01-05 12:11:31 -080075import com.android.systemui.recents.events.ui.TaskViewDismissedEvent;
Winsonb1e71d02015-11-23 12:40:23 -080076import com.android.systemui.recents.events.ui.UpdateFreeformTaskViewVisibilityEvent;
Winsone7f138c2015-10-22 16:15:21 -070077import com.android.systemui.recents.events.ui.UserInteractionEvent;
Winsoneca4ab62015-11-04 10:50:28 -080078import com.android.systemui.recents.events.ui.dragndrop.DragDropTargetChangedEvent;
79import com.android.systemui.recents.events.ui.dragndrop.DragEndEvent;
80import com.android.systemui.recents.events.ui.dragndrop.DragStartEvent;
81import com.android.systemui.recents.events.ui.dragndrop.DragStartInitializeDropTargetsEvent;
Winson0d14d4d2015-10-26 17:05:04 -070082import com.android.systemui.recents.events.ui.focus.DismissFocusedTaskViewEvent;
83import com.android.systemui.recents.events.ui.focus.FocusNextTaskViewEvent;
84import com.android.systemui.recents.events.ui.focus.FocusPreviousTaskViewEvent;
Winson Chungffa2ec62014-07-03 15:54:42 -070085import com.android.systemui.recents.misc.DozeTrigger;
Winson49df4202016-01-25 17:33:34 -080086import com.android.systemui.recents.misc.ReferenceCountedTrigger;
Winson Chungee445952014-09-09 16:12:59 +020087import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chungbf5dbf12014-09-16 00:58:25 +020088import com.android.systemui.recents.misc.Utilities;
Winson Chung303e1ff2014-03-07 15:06:19 -080089import com.android.systemui.recents.model.Task;
90import com.android.systemui.recents.model.TaskStack;
Winson Chung303e1ff2014-03-07 15:06:19 -080091
92import java.util.ArrayList;
Winson Chung6ac8bd62015-01-07 16:38:35 -080093import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080094
Winson Chung303e1ff2014-03-07 15:06:19 -080095
96/* The visual representation of a task stack view */
Winson Chung04dfe0d2014-03-14 14:06:29 -070097public class TaskStackView extends FrameLayout implements TaskStack.TaskStackCallbacks,
Winson Chung012ef362014-07-31 18:36:25 -070098 TaskView.TaskViewCallbacks, TaskStackViewScroller.TaskStackViewScrollerCallbacks,
Winson1c846142016-01-22 11:34:38 -080099 TaskStackLayoutAlgorithm.TaskStackLayoutAlgorithmCallbacks,
Winsone6c90732015-09-24 16:06:29 -0700100 ViewPool.ViewPoolConsumer<TaskView, Task> {
Winson Chung04dfe0d2014-03-14 14:06:29 -0700101
Winson Chung4ab4d832015-12-11 10:25:46 -0500102 private final static String KEY_SAVED_STATE_SUPER = "saved_instance_state_super";
103 private final static String KEY_SAVED_STATE_LAYOUT_FOCUSED_STATE =
104 "saved_instance_state_layout_focused_state";
105 private final static String KEY_SAVED_STATE_LAYOUT_STACK_SCROLL =
106 "saved_instance_state_layout_stack_scroll";
107
Winsonc29ff002015-11-20 16:00:45 -0800108 // The thresholds at which to show/hide the history button.
109 private static final float SHOW_HISTORY_BUTTON_SCROLL_THRESHOLD = 0.3f;
110 private static final float HIDE_HISTORY_BUTTON_SCROLL_THRESHOLD = 0.3f;
111
Winson8aa99592016-01-19 15:07:07 -0800112 public static final int DEFAULT_SYNC_STACK_DURATION = 200;
Winsonf24f2162016-01-05 12:11:55 -0800113 private static final int DRAG_SCALE_DURATION = 175;
114 private static final float DRAG_SCALE_FACTOR = 1.05f;
Winson Chung06266772015-12-11 10:24:21 -0500115
Winson8aa99592016-01-19 15:07:07 -0800116 private static final ArraySet<Task.TaskKey> EMPTY_TASK_SET = new ArraySet<>();
117
Winson05e46ca2016-02-05 15:40:29 -0800118 LayoutInflater mInflater;
Winson88737542016-02-17 13:27:33 -0800119 TaskStack mStack = new TaskStack();
Winson231bc9c2016-02-09 12:31:00 -0800120 @ViewDebug.ExportedProperty(deepExport=true, prefix="layout_")
Winson36a5a2c2015-10-29 18:04:39 -0700121 TaskStackLayoutAlgorithm mLayoutAlgorithm;
Winson231bc9c2016-02-09 12:31:00 -0800122 @ViewDebug.ExportedProperty(deepExport=true, prefix="scroller_")
Winson Chung012ef362014-07-31 18:36:25 -0700123 TaskStackViewScroller mStackScroller;
Winson231bc9c2016-02-09 12:31:00 -0800124 @ViewDebug.ExportedProperty(deepExport=true, prefix="touch_")
Winson Chung303e1ff2014-03-07 15:06:19 -0800125 TaskStackViewTouchHandler mTouchHandler;
Winsonf24f2162016-01-05 12:11:55 -0800126 TaskStackAnimationHelper mAnimationHelper;
Winsonde0591a2015-12-04 17:24:35 -0800127 GradientDrawable mFreeformWorkspaceBackground;
Winsona78a8f32015-12-03 10:55:01 -0800128 ObjectAnimator mFreeformWorkspaceBackgroundAnimator;
Winson Chung303e1ff2014-03-07 15:06:19 -0800129 ViewPool<TaskView, Task> mViewPool;
Winsonf24f2162016-01-05 12:11:55 -0800130
131 ArrayList<TaskView> mTaskViews = new ArrayList<>();
Winsone6c90732015-09-24 16:06:29 -0700132 ArrayList<TaskViewTransform> mCurrentTaskTransforms = new ArrayList<>();
Winson8aa99592016-01-19 15:07:07 -0800133 ArraySet<Task.TaskKey> mIgnoreTasks = new ArraySet<>();
Winsonbe8e6962016-02-01 14:27:52 -0800134 AnimationProps mDeferredTaskViewLayoutAnimation = null;
Winsonf24f2162016-01-05 12:11:55 -0800135
Winson231bc9c2016-02-09 12:31:00 -0800136 @ViewDebug.ExportedProperty(deepExport=true, prefix="doze_")
Winson Chungd543c1b2014-06-23 15:06:45 -0700137 DozeTrigger mUIDozeTrigger;
Winson231bc9c2016-02-09 12:31:00 -0800138 @ViewDebug.ExportedProperty(deepExport=true, prefix="focused_task_")
Winsonaaf33bc2015-12-03 12:02:38 -0800139 Task mFocusedTask;
Winsonf24f2162016-01-05 12:11:55 -0800140
Winsonbb410952015-12-04 14:34:11 -0800141 int mTaskCornerRadiusPx;
Winson3e874742016-01-07 10:08:17 -0800142 private int mDividerSize;
Winson4b9cded2016-01-26 16:26:47 -0800143 private int mStartTimerIndicatorDuration;
Winsonf24f2162016-01-05 12:11:55 -0800144
Winson231bc9c2016-02-09 12:31:00 -0800145 @ViewDebug.ExportedProperty(category="recents")
Winsonf24f2162016-01-05 12:11:55 -0800146 boolean mTaskViewsClipDirty = true;
Winson231bc9c2016-02-09 12:31:00 -0800147 @ViewDebug.ExportedProperty(category="recents")
Winson Chung303e1ff2014-03-07 15:06:19 -0800148 boolean mAwaitingFirstLayout = true;
Winson231bc9c2016-02-09 12:31:00 -0800149 @ViewDebug.ExportedProperty(category="recents")
Winson70f0bf72016-02-01 14:05:29 -0800150 boolean mInMeasureLayout = false;
Winson231bc9c2016-02-09 12:31:00 -0800151 @ViewDebug.ExportedProperty(category="recents")
Winsone5f1faa2015-11-20 12:26:23 -0800152 boolean mEnterAnimationComplete = false;
Winson231bc9c2016-02-09 12:31:00 -0800153 @ViewDebug.ExportedProperty(category="recents")
Winsonf24f2162016-01-05 12:11:55 -0800154 boolean mTouchExplorationEnabled;
Winson231bc9c2016-02-09 12:31:00 -0800155 @ViewDebug.ExportedProperty(category="recents")
Winsonf24f2162016-01-05 12:11:55 -0800156 boolean mScreenPinningEnabled;
Winson3150e572015-10-23 15:07:24 -0700157
Winson3e874742016-01-07 10:08:17 -0800158 // The stable stack bounds are the full bounds that we were measured with from RecentsView
Winson231bc9c2016-02-09 12:31:00 -0800159 @ViewDebug.ExportedProperty(category="recents")
Winson05e46ca2016-02-05 15:40:29 -0800160 private Rect mStableStackBounds = new Rect();
Winson3e874742016-01-07 10:08:17 -0800161 // The current stack bounds are dynamic and may change as the user drags and drops
Winson231bc9c2016-02-09 12:31:00 -0800162 @ViewDebug.ExportedProperty(category="recents")
Winson05e46ca2016-02-05 15:40:29 -0800163 private Rect mStackBounds = new Rect();
Winsond9529612016-01-28 13:29:49 -0800164
Winson05e46ca2016-02-05 15:40:29 -0800165 private Rect mTmpRect = new Rect();
166 private ArrayMap<Task.TaskKey, TaskView> mTmpTaskViewMap = new ArrayMap<>();
167 private List<TaskView> mTmpTaskViews = new ArrayList<>();
168 private TaskViewTransform mTmpTransform = new TaskViewTransform();
Winsonc4387022016-02-24 12:05:26 -0800169 private int[] mTmpIntPair = new int[2];
Winson Chung931c51f2015-12-17 17:08:55 -0500170
Winson Chungbf5dbf12014-09-16 00:58:25 +0200171 // A convenience update listener to request updating clipping of tasks
Winsoneca4ab62015-11-04 10:50:28 -0800172 private ValueAnimator.AnimatorUpdateListener mRequestUpdateClippingListener =
Winson Chungbf5dbf12014-09-16 00:58:25 +0200173 new ValueAnimator.AnimatorUpdateListener() {
Winson5b7dd532015-12-01 16:02:12 -0800174 @Override
175 public void onAnimationUpdate(ValueAnimator animation) {
Winson55003902016-01-12 12:00:37 -0800176 if (!mTaskViewsClipDirty) {
177 mTaskViewsClipDirty = true;
178 invalidate();
179 }
Winson5b7dd532015-12-01 16:02:12 -0800180 }
181 };
Winson Chungbf5dbf12014-09-16 00:58:25 +0200182
Winsoneca4ab62015-11-04 10:50:28 -0800183 // The drop targets for a task drag
184 private DropTarget mFreeformWorkspaceDropTarget = new DropTarget() {
185 @Override
Winson3e874742016-01-07 10:08:17 -0800186 public boolean acceptsDrop(int x, int y, int width, int height, boolean isCurrentTarget) {
187 // This drop target has a fixed bounds and should be checked last, so just fall through
188 // if it is the current target
189 if (!isCurrentTarget) {
190 return mLayoutAlgorithm.mFreeformRect.contains(x, y);
191 }
192 return false;
Winsoneca4ab62015-11-04 10:50:28 -0800193 }
194 };
195
196 private DropTarget mStackDropTarget = new DropTarget() {
197 @Override
Winson3e874742016-01-07 10:08:17 -0800198 public boolean acceptsDrop(int x, int y, int width, int height, boolean isCurrentTarget) {
199 // This drop target has a fixed bounds and should be checked last, so just fall through
200 // if it is the current target
201 if (!isCurrentTarget) {
202 return mLayoutAlgorithm.mStackRect.contains(x, y);
203 }
204 return false;
Winsoneca4ab62015-11-04 10:50:28 -0800205 }
206 };
207
Winson88737542016-02-17 13:27:33 -0800208 public TaskStackView(Context context) {
Winson Chung303e1ff2014-03-07 15:06:19 -0800209 super(context);
Winsonde0591a2015-12-04 17:24:35 -0800210 SystemServicesProxy ssp = Recents.getSystemServices();
Winsonbb410952015-12-04 14:34:11 -0800211 Resources res = context.getResources();
212
Winson Chungb0a28ea2014-10-28 15:21:35 -0700213 // Set the stack first
Winson88737542016-02-17 13:27:33 -0800214 mStack.setCallbacks(this);
Winson35f30502015-09-28 11:24:36 -0700215 mViewPool = new ViewPool<>(context, this);
Winson Chung37c8d8e2014-03-24 14:53:07 -0700216 mInflater = LayoutInflater.from(context);
Winson1c846142016-01-22 11:34:38 -0800217 mLayoutAlgorithm = new TaskStackLayoutAlgorithm(context, this);
218 mStackScroller = new TaskStackViewScroller(context, this, mLayoutAlgorithm);
Winson35f30502015-09-28 11:24:36 -0700219 mTouchHandler = new TaskStackViewTouchHandler(context, this, mStackScroller);
Winsonf24f2162016-01-05 12:11:55 -0800220 mAnimationHelper = new TaskStackAnimationHelper(context, this);
Winsonbb410952015-12-04 14:34:11 -0800221 mTaskCornerRadiusPx = res.getDimensionPixelSize(
222 R.dimen.recents_task_view_rounded_corners_radius);
Winson3e874742016-01-07 10:08:17 -0800223 mDividerSize = ssp.getDockedDividerSize(context);
Winson35f30502015-09-28 11:24:36 -0700224
225 int taskBarDismissDozeDelaySeconds = getResources().getInteger(
226 R.integer.recents_task_bar_dismiss_delay_seconds);
227 mUIDozeTrigger = new DozeTrigger(taskBarDismissDozeDelaySeconds, new Runnable() {
Winson Chunga26fb782014-06-12 17:52:39 -0700228 @Override
229 public void run() {
230 // Show the task bar dismiss buttons
Winson Chung6ac8bd62015-01-07 16:38:35 -0800231 List<TaskView> taskViews = getTaskViews();
232 int taskViewCount = taskViews.size();
233 for (int i = 0; i < taskViewCount; i++) {
234 TaskView tv = taskViews.get(i);
Winson Chung969f5862014-06-16 17:08:24 -0700235 tv.startNoUserInteractionAnimation();
Winson Chunga26fb782014-06-12 17:52:39 -0700236 }
237 }
238 });
Winson Chung83ea6f72015-06-17 13:00:23 -0700239 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
Winson36a5a2c2015-10-29 18:04:39 -0700240
Winsonde0591a2015-12-04 17:24:35 -0800241 mFreeformWorkspaceBackground = (GradientDrawable) getContext().getDrawable(
242 R.drawable.recents_freeform_workspace_bg);
Winsona78a8f32015-12-03 10:55:01 -0800243 mFreeformWorkspaceBackground.setCallback(this);
Winsonde0591a2015-12-04 17:24:35 -0800244 if (ssp.hasFreeformWorkspaceSupport()) {
Winson Chungaa4f8002015-12-17 10:27:55 -0500245 mFreeformWorkspaceBackground.setColor(
246 getContext().getColor(R.color.recents_freeform_workspace_bg_color));
Winsonde0591a2015-12-04 17:24:35 -0800247 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800248 }
249
Winson88737542016-02-17 13:27:33 -0800250 /**
251 * Called only if we are resuming Recents.
252 */
253 void onResume(boolean isResumingFromVisible) {
254 if (!isResumingFromVisible) {
255 // Reset the focused task
256 resetFocusedTask(getFocusedTask());
257 }
258
259 // Reset the state of each of the task views
260 List<TaskView> taskViews = new ArrayList<>();
261 taskViews.addAll(getTaskViews());
262 taskViews.addAll(mViewPool.getViews());
263 for (int i = taskViews.size() - 1; i >= 0; i--) {
264 taskViews.get(i).onResume(isResumingFromVisible);
265 }
266
267 // Reset the stack state
268 readSystemFlags();
269 mTaskViewsClipDirty = true;
270 mEnterAnimationComplete = false;
271 mUIDozeTrigger.stopDozing();
272 if (isResumingFromVisible) {
273 // Animate in the freeform workspace
274 int ffBgAlpha = mLayoutAlgorithm.getStackState().freeformBackgroundAlpha;
275 animateFreeformWorkspaceBackgroundAlpha(ffBgAlpha, new AnimationProps(150,
276 Interpolators.FAST_OUT_SLOW_IN));
277 } else {
278 mStackScroller.reset();
279 mLayoutAlgorithm.reset();
280 mAwaitingFirstLayout = true;
281 requestLayout();
282 }
283 }
284
Winsone6c90732015-09-24 16:06:29 -0700285 @Override
286 protected void onAttachedToWindow() {
287 EventBus.getDefault().register(this, RecentsActivity.EVENT_BUS_PRIORITY + 1);
Winson Chung133ad442016-01-30 01:07:34 +0000288 super.onAttachedToWindow();
Winson Chungbbb3d3d2016-01-30 01:09:20 +0000289 readSystemFlags();
Winsone6c90732015-09-24 16:06:29 -0700290 }
291
292 @Override
293 protected void onDetachedFromWindow() {
294 super.onDetachedFromWindow();
295 EventBus.getDefault().unregister(this);
296 }
297
Winson88737542016-02-17 13:27:33 -0800298 /**
299 * Sets the stack tasks of this TaskStackView from the given TaskStack.
300 */
Winson4cfea692016-03-08 12:33:56 -0800301 public void setTasks(TaskStack stack, boolean notifyStackChanges, boolean relayoutTaskStack) {
Winson88737542016-02-17 13:27:33 -0800302 boolean isInitialized = mLayoutAlgorithm.isInitialized();
303 mStack.setTasks(getContext(), stack.computeAllTasksList(),
304 notifyStackChanges && isInitialized);
305 if (isInitialized) {
306 // Only update the layout if we are notifying, otherwise, we will update it in the next
307 // measure/layout pass
308 updateLayoutAlgorithm(false /* boundScroll */, EMPTY_TASK_SET);
309 updateToInitialState();
Winson43336942016-03-07 14:52:59 -0800310
Winson4cfea692016-03-08 12:33:56 -0800311 if (relayoutTaskStack) {
312 relayoutTaskViews(AnimationProps.IMMEDIATE);
313
314 // Rebind all the task views. This will not trigger new resources to be loaded
315 // unless they have actually changed
316 List<TaskView> taskViews = getTaskViews();
317 int taskViewCount = taskViews.size();
318 for (int i = 0; i < taskViewCount; i++) {
319 TaskView tv = taskViews.get(i);
320 bindTaskView(tv, tv.getTask());
321 }
Winson43336942016-03-07 14:52:59 -0800322 }
Winson Chungb0a28ea2014-10-28 15:21:35 -0700323 }
324 }
325
Winson Chungd16c5652015-01-26 16:11:07 -0800326 /** Returns the task stack. */
Winsone693aaf2016-03-01 12:05:59 -0800327 public TaskStack getStack() {
Winson Chungd16c5652015-01-26 16:11:07 -0800328 return mStack;
329 }
330
Winsone693aaf2016-03-01 12:05:59 -0800331 /**
332 * Updates this TaskStackView to the initial state.
333 */
334 public void updateToInitialState() {
335 mStackScroller.setStackScrollToInitialState();
336 mLayoutAlgorithm.updateToInitialState(mStack.getStackTasks());
337 }
338
Winson Chung6ac8bd62015-01-07 16:38:35 -0800339 /** Updates the list of task views */
340 void updateTaskViewsList() {
341 mTaskViews.clear();
342 int childCount = getChildCount();
343 for (int i = 0; i < childCount; i++) {
344 View v = getChildAt(i);
345 if (v instanceof TaskView) {
346 mTaskViews.add((TaskView) v);
347 }
348 }
Winson Chung6ac8bd62015-01-07 16:38:35 -0800349 }
350
351 /** Gets the list of task views */
352 List<TaskView> getTaskViews() {
Winsonf24f2162016-01-05 12:11:55 -0800353 return mTaskViews;
Winson Chung6ac8bd62015-01-07 16:38:35 -0800354 }
355
Winson1b585612015-11-06 09:16:26 -0800356 /**
357 * Returns the front most task view.
358 *
359 * @param stackTasksOnly if set, will return the front most task view in the stack (by default
360 * the front most task view will be freeform since they are placed above
361 * stack tasks)
362 */
363 private TaskView getFrontMostTaskView(boolean stackTasksOnly) {
364 List<TaskView> taskViews = getTaskViews();
365 int taskViewCount = taskViews.size();
366 for (int i = taskViewCount - 1; i >= 0; i--) {
367 TaskView tv = taskViews.get(i);
368 Task task = tv.getTask();
369 if (stackTasksOnly && task.isFreeformTask()) {
370 continue;
371 }
372 return tv;
373 }
374 return null;
375 }
376
377 /**
378 * Finds the child view given a specific {@param task}.
379 */
380 public TaskView getChildViewForTask(Task t) {
381 List<TaskView> taskViews = getTaskViews();
382 int taskViewCount = taskViews.size();
383 for (int i = 0; i < taskViewCount; i++) {
384 TaskView tv = taskViews.get(i);
385 if (tv.getTask() == t) {
386 return tv;
387 }
388 }
389 return null;
390 }
391
Winson Chungd7b2cb12014-06-26 15:08:50 -0700392 /** Returns the stack algorithm for this task stack. */
Winson36a5a2c2015-10-29 18:04:39 -0700393 public TaskStackLayoutAlgorithm getStackAlgorithm() {
Winson Chung012ef362014-07-31 18:36:25 -0700394 return mLayoutAlgorithm;
Winson Chung303e1ff2014-03-07 15:06:19 -0800395 }
396
Winson Chungc6a16232014-04-01 14:04:48 -0700397 /**
Winson8aa99592016-01-19 15:07:07 -0800398 * Adds a task to the ignored set.
Winson Chungc6a16232014-04-01 14:04:48 -0700399 */
Winson8aa99592016-01-19 15:07:07 -0800400 void addIgnoreTask(Task task) {
401 mIgnoreTasks.add(task.key);
402 }
403
404 /**
405 * Removes a task from the ignored set.
406 */
407 void removeIgnoreTask(Task task) {
408 mIgnoreTasks.remove(task.key);
409 }
410
411 /**
412 * Returns whether the specified {@param task} is ignored.
413 */
414 boolean isIgnoredTask(Task task) {
415 return mIgnoreTasks.contains(task.key);
416 }
417
418 /**
419 * Computes the task transforms at the current stack scroll for all visible tasks. If a valid
420 * target stack scroll is provided (ie. is different than {@param curStackScroll}), then the
421 * visible range includes all tasks at the target stack scroll. This is useful for ensure that
422 * all views necessary for a transition or animation will be visible at the start.
423 *
424 * This call ignores freeform tasks.
425 *
426 * @param taskTransforms The set of task view transforms to reuse, this list will be sized to
427 * match the size of {@param tasks}
428 * @param tasks The set of tasks for which to generate transforms
429 * @param curStackScroll The current stack scroll
430 * @param targetStackScroll The stack scroll that we anticipate we are going to be scrolling to.
431 * The range of the union of the visible views at the current and
432 * target stack scrolls will be returned.
433 * @param ignoreTasksSet The set of tasks to skip for purposes of calculaing the visible range.
434 * Transforms will still be calculated for the ignore tasks.
Winsonc4387022016-02-24 12:05:26 -0800435 * @return the front and back most visible task indices (there may be non visible tasks in
436 * between this range)
Winson8aa99592016-01-19 15:07:07 -0800437 */
Winsonc4387022016-02-24 12:05:26 -0800438 int[] computeVisibleTaskTransforms(ArrayList<TaskViewTransform> taskTransforms,
Winson8aa99592016-01-19 15:07:07 -0800439 ArrayList<Task> tasks, float curStackScroll, float targetStackScroll,
Winsone693aaf2016-03-01 12:05:59 -0800440 ArraySet<Task.TaskKey> ignoreTasksSet, boolean ignoreTaskOverrides) {
Winson Chungc6a16232014-04-01 14:04:48 -0700441 int taskCount = tasks.size();
Winsonc4387022016-02-24 12:05:26 -0800442 int[] visibleTaskRange = mTmpIntPair;
443 visibleTaskRange[0] = -1;
444 visibleTaskRange[1] = -1;
Winson8aa99592016-01-19 15:07:07 -0800445 boolean useTargetStackScroll = Float.compare(curStackScroll, targetStackScroll) != 0;
Winson Chungc9567c02014-06-16 20:25:51 -0700446
447 // We can reuse the task transforms where possible to reduce object allocation
Winsonc5fd3502016-01-18 15:18:37 -0800448 Utilities.matchTaskListSize(tasks, taskTransforms);
Winson Chungc9567c02014-06-16 20:25:51 -0700449
450 // Update the stack transforms
Winson4993c2f2015-11-19 10:06:06 -0800451 TaskViewTransform frontTransform = null;
Winson8aa99592016-01-19 15:07:07 -0800452 TaskViewTransform frontTransformAtTarget = null;
453 TaskViewTransform transform = null;
454 TaskViewTransform transformAtTarget = null;
Winson Chung7aceb9a2014-07-03 13:38:01 -0700455 for (int i = taskCount - 1; i >= 0; i--) {
Winsona5e6b362015-11-02 17:17:20 -0800456 Task task = tasks.get(i);
Winson8aa99592016-01-19 15:07:07 -0800457
458 // Calculate the current and (if necessary) the target transform for the task
459 transform = mLayoutAlgorithm.getStackTransform(task, curStackScroll,
Winsone693aaf2016-03-01 12:05:59 -0800460 taskTransforms.get(i), frontTransform, ignoreTaskOverrides);
Winson8aa99592016-01-19 15:07:07 -0800461 if (useTargetStackScroll && !transform.visible) {
462 // If we have a target stack scroll and the task is not currently visible, then we
463 // just update the transform at the new scroll
464 // TODO: Optimize this
465 transformAtTarget = mLayoutAlgorithm.getStackTransform(task,
466 targetStackScroll, new TaskViewTransform(), frontTransformAtTarget);
467 if (transformAtTarget.visible) {
468 transform.copyFrom(transformAtTarget);
469 }
Winson3e874742016-01-07 10:08:17 -0800470 }
471
Winson8aa99592016-01-19 15:07:07 -0800472 // For ignore tasks, only calculate the stack transform and skip the calculation of the
473 // visible stack indices
474 if (ignoreTasksSet.contains(task.key)) {
475 continue;
476 }
Winsonf24f2162016-01-05 12:11:55 -0800477
478 // For freeform tasks, only calculate the stack transform and skip the calculation of
479 // the visible stack indices
Winsona5e6b362015-11-02 17:17:20 -0800480 if (task.isFreeformTask()) {
481 continue;
482 }
483
Winson4993c2f2015-11-19 10:06:06 -0800484 frontTransform = transform;
Winson8aa99592016-01-19 15:07:07 -0800485 frontTransformAtTarget = transformAtTarget;
Winsonc4387022016-02-24 12:05:26 -0800486 if (transform.visible) {
487 if (visibleTaskRange[0] < 0) {
488 visibleTaskRange[0] = i;
489 }
490 visibleTaskRange[1] = i;
491 }
Winson Chungc6a16232014-04-01 14:04:48 -0700492 }
Winsonc4387022016-02-24 12:05:26 -0800493 return visibleTaskRange;
Winson Chungd42a6cf2014-06-03 16:24:04 -0700494 }
495
Winsonf24f2162016-01-05 12:11:55 -0800496 /**
Winson8aa99592016-01-19 15:07:07 -0800497 * Binds the visible {@link TaskView}s at the given target scroll.
Winsonf24f2162016-01-05 12:11:55 -0800498 */
Winson8aa99592016-01-19 15:07:07 -0800499 void bindVisibleTaskViews(float targetStackScroll) {
Winsone693aaf2016-03-01 12:05:59 -0800500 bindVisibleTaskViews(targetStackScroll, mIgnoreTasks, false /* ignoreTaskOverrides */);
501 }
502
503 void bindVisibleTaskViews(float targetStackScroll, boolean ignoreTaskOverrides) {
504 bindVisibleTaskViews(targetStackScroll, mIgnoreTasks, ignoreTaskOverrides);
Winson8aa99592016-01-19 15:07:07 -0800505 }
506
507 /**
508 * Synchronizes the set of children {@link TaskView}s to match the visible set of tasks in the
509 * current {@link TaskStack}. This call does not continue on to update their position to the
510 * computed {@link TaskViewTransform}s of the visible range, but only ensures that they will
511 * be added/removed from the view hierarchy and placed in the correct Z order and initial
512 * position (if not currently on screen).
513 *
514 * @param targetStackScroll If provided, will ensure that the set of visible {@link TaskView}s
515 * includes those visible at the current stack scroll, and all at the
516 * target stack scroll.
517 * @param ignoreTasksSet The set of tasks to ignore in this rebinding of the visible
518 * {@link TaskView}s
Winsone693aaf2016-03-01 12:05:59 -0800519 * @param ignoreTaskOverrides If set, the visible task computation will get the transforms for
520 * tasks at their non-overridden task progress
Winson8aa99592016-01-19 15:07:07 -0800521 */
Winsone693aaf2016-03-01 12:05:59 -0800522 void bindVisibleTaskViews(float targetStackScroll, ArraySet<Task.TaskKey> ignoreTasksSet,
523 boolean ignoreTaskOverrides) {
Winsonf24f2162016-01-05 12:11:55 -0800524 // Get all the task transforms
Winsonc4387022016-02-24 12:05:26 -0800525 ArrayList<Task> tasks = mStack.getStackTasks();
526 int[] visibleTaskRange = computeVisibleTaskTransforms(mCurrentTaskTransforms, tasks,
Winsone693aaf2016-03-01 12:05:59 -0800527 mStackScroller.getStackScroll(), targetStackScroll, ignoreTasksSet,
528 ignoreTaskOverrides);
Winsonf24f2162016-01-05 12:11:55 -0800529
530 // Return all the invisible children to the pool
Winson55003902016-01-12 12:00:37 -0800531 mTmpTaskViewMap.clear();
Winson8aa99592016-01-19 15:07:07 -0800532 List<TaskView> taskViews = getTaskViews();
533 int lastFocusedTaskIndex = -1;
534 int taskViewCount = taskViews.size();
Winsonf24f2162016-01-05 12:11:55 -0800535 for (int i = taskViewCount - 1; i >= 0; i--) {
Winson8aa99592016-01-19 15:07:07 -0800536 TaskView tv = taskViews.get(i);
537 Task task = tv.getTask();
Winsonf24f2162016-01-05 12:11:55 -0800538
Winson3e874742016-01-07 10:08:17 -0800539 // Skip ignored tasks
Winson8aa99592016-01-19 15:07:07 -0800540 if (ignoreTasksSet.contains(task.key)) {
Winson3e874742016-01-07 10:08:17 -0800541 continue;
542 }
543
Winson1d5ff7e2016-03-04 11:21:09 -0800544 // It is possible for the set of lingering TaskViews to differ from the stack if the
545 // stack was updated before the relayout. If the task view is no longer in the stack,
546 // then just return it back to the view pool.
547 int taskIndex = mStack.indexOfStackTask(task);
548 TaskViewTransform transform = null;
549 if (taskIndex != -1) {
550 transform = mCurrentTaskTransforms.get(taskIndex);
551 }
552
553 if (task.isFreeformTask() || (transform != null && transform.visible)) {
Winson55003902016-01-12 12:00:37 -0800554 mTmpTaskViewMap.put(task.key, tv);
Winsonf24f2162016-01-05 12:11:55 -0800555 } else {
556 if (mTouchExplorationEnabled) {
557 lastFocusedTaskIndex = taskIndex;
558 resetFocusedTask(task);
Winson Chung303e1ff2014-03-07 15:06:19 -0800559 }
Winsonf24f2162016-01-05 12:11:55 -0800560 mViewPool.returnViewToPool(tv);
Winson Chung303e1ff2014-03-07 15:06:19 -0800561 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800562 }
Winsonf24f2162016-01-05 12:11:55 -0800563
564 // Pick up all the newly visible children
Winsonc4387022016-02-24 12:05:26 -0800565 for (int i = tasks.size() - 1; i >= 0; i--) {
Winson8aa99592016-01-19 15:07:07 -0800566 Task task = tasks.get(i);
567 TaskViewTransform transform = mCurrentTaskTransforms.get(i);
Winsonf24f2162016-01-05 12:11:55 -0800568
Winson3e874742016-01-07 10:08:17 -0800569 // Skip ignored tasks
Winson8aa99592016-01-19 15:07:07 -0800570 if (ignoreTasksSet.contains(task.key)) {
Winson3e874742016-01-07 10:08:17 -0800571 continue;
572 }
573
Winsonf24f2162016-01-05 12:11:55 -0800574 // Skip the invisible non-freeform stack tasks
Winson05e46ca2016-02-05 15:40:29 -0800575 if (!task.isFreeformTask() && !transform.visible) {
Winsonf24f2162016-01-05 12:11:55 -0800576 continue;
577 }
578
Winson55003902016-01-12 12:00:37 -0800579 TaskView tv = mTmpTaskViewMap.get(task.key);
Winsonf24f2162016-01-05 12:11:55 -0800580 if (tv == null) {
581 tv = mViewPool.pickUpViewFromPool(task, task);
582 if (task.isFreeformTask()) {
Winsonbe8e6962016-02-01 14:27:52 -0800583 tv.updateViewPropertiesToTaskTransform(transform, AnimationProps.IMMEDIATE,
Winsonf24f2162016-01-05 12:11:55 -0800584 mRequestUpdateClippingListener);
585 } else {
Winson68088812016-02-12 16:06:04 -0800586 if (transform.rect.top <= mLayoutAlgorithm.mStackRect.top) {
Winsonf24f2162016-01-05 12:11:55 -0800587 tv.updateViewPropertiesToTaskTransform(
588 mLayoutAlgorithm.getBackOfStackTransform(),
Winsonbe8e6962016-02-01 14:27:52 -0800589 AnimationProps.IMMEDIATE, mRequestUpdateClippingListener);
Winsonf24f2162016-01-05 12:11:55 -0800590 } else {
591 tv.updateViewPropertiesToTaskTransform(
592 mLayoutAlgorithm.getFrontOfStackTransform(),
Winsonbe8e6962016-02-01 14:27:52 -0800593 AnimationProps.IMMEDIATE, mRequestUpdateClippingListener);
Winsonf24f2162016-01-05 12:11:55 -0800594 }
595 }
596 } else {
597 // Reattach it in the right z order
598 final int taskIndex = mStack.indexOfStackTask(task);
599 final int insertIndex = findTaskViewInsertIndex(task, taskIndex);
600 if (insertIndex != getTaskViews().indexOf(tv)){
601 detachViewFromParent(tv);
602 attachViewToParent(tv, insertIndex, tv.getLayoutParams());
603 updateTaskViewsList();
604 }
605 }
606 }
607
608 // Update the focus if the previous focused task was returned to the view pool
609 if (lastFocusedTaskIndex != -1) {
Winsonc4387022016-02-24 12:05:26 -0800610 if (lastFocusedTaskIndex < visibleTaskRange[1]) {
611 setFocusedTask(visibleTaskRange[1], false /* scrollToTask */,
Winsonf24f2162016-01-05 12:11:55 -0800612 true /* requestViewFocus */);
613 } else {
Winsonc4387022016-02-24 12:05:26 -0800614 setFocusedTask(visibleTaskRange[0], false /* scrollToTask */,
Winsonf24f2162016-01-05 12:11:55 -0800615 true /* requestViewFocus */);
616 }
617 }
618 }
619
620 /**
Winson8aa99592016-01-19 15:07:07 -0800621 * Relayout the the visible {@link TaskView}s to their current transforms as specified by the
622 * {@link TaskStackLayoutAlgorithm} with the given {@param animation}. This call cancels any
623 * animations that are current running on those task views, and will ensure that the children
624 * {@link TaskView}s will match the set of visible tasks in the stack.
Winson3e874742016-01-07 10:08:17 -0800625 *
Winsonbe8e6962016-02-01 14:27:52 -0800626 * @see #relayoutTaskViews(AnimationProps, ArraySet<Task.TaskKey>)
Winsonf24f2162016-01-05 12:11:55 -0800627 */
Winsonbe8e6962016-02-01 14:27:52 -0800628 void relayoutTaskViews(AnimationProps animation) {
Winson8aa99592016-01-19 15:07:07 -0800629 relayoutTaskViews(animation, mIgnoreTasks);
630 }
Winson3e874742016-01-07 10:08:17 -0800631
Winson8aa99592016-01-19 15:07:07 -0800632 /**
633 * Relayout the the visible {@link TaskView}s to their current transforms as specified by the
634 * {@link TaskStackLayoutAlgorithm} with the given {@param animation}. This call cancels any
635 * animations that are current running on those task views, and will ensure that the children
636 * {@link TaskView}s will match the set of visible tasks in the stack.
637 *
638 * @param ignoreTasksSet the set of tasks to ignore in the relayout
639 */
Winsonbe8e6962016-02-01 14:27:52 -0800640 void relayoutTaskViews(AnimationProps animation, ArraySet<Task.TaskKey> ignoreTasksSet) {
Winsonf24f2162016-01-05 12:11:55 -0800641 // If we had a deferred animation, cancel that
Winson8aa99592016-01-19 15:07:07 -0800642 mDeferredTaskViewLayoutAnimation = null;
Winsonf24f2162016-01-05 12:11:55 -0800643
644 // Cancel all task view animations
645 cancelAllTaskViewAnimations();
646
Winson8aa99592016-01-19 15:07:07 -0800647 // Synchronize the current set of TaskViews
Winsone693aaf2016-03-01 12:05:59 -0800648 bindVisibleTaskViews(mStackScroller.getStackScroll(), ignoreTasksSet,
649 false /* ignoreTaskOverrides */);
Winsonf24f2162016-01-05 12:11:55 -0800650
651 // Animate them to their final transforms with the given animation
652 List<TaskView> taskViews = getTaskViews();
653 int taskViewCount = taskViews.size();
654 for (int i = 0; i < taskViewCount; i++) {
Winson43336942016-03-07 14:52:59 -0800655 TaskView tv = taskViews.get(i);
656 int taskIndex = mStack.indexOfStackTask(tv.getTask());
657 TaskViewTransform transform = mCurrentTaskTransforms.get(taskIndex);
Winsonf24f2162016-01-05 12:11:55 -0800658
Winson8aa99592016-01-19 15:07:07 -0800659 if (ignoreTasksSet.contains(tv.getTask().key)) {
Winson3e874742016-01-07 10:08:17 -0800660 continue;
661 }
662
Winsonf24f2162016-01-05 12:11:55 -0800663 updateTaskViewToTransform(tv, transform, animation);
664 }
665 }
666
667 /**
668 * Posts an update to synchronize the {@link TaskView}s with the stack on the next frame.
669 */
Winsonbe8e6962016-02-01 14:27:52 -0800670 void relayoutTaskViewsOnNextFrame(AnimationProps animation) {
Winson8aa99592016-01-19 15:07:07 -0800671 mDeferredTaskViewLayoutAnimation = animation;
Winson1c846142016-01-22 11:34:38 -0800672 invalidate();
Winsonf24f2162016-01-05 12:11:55 -0800673 }
674
675 /**
676 * Called to update a specific {@link TaskView} to a given {@link TaskViewTransform} with a
Winsonbe8e6962016-02-01 14:27:52 -0800677 * given set of {@link AnimationProps} properties.
Winsonf24f2162016-01-05 12:11:55 -0800678 */
679 public void updateTaskViewToTransform(TaskView taskView, TaskViewTransform transform,
Winsonbe8e6962016-02-01 14:27:52 -0800680 AnimationProps animation) {
Winsonf24f2162016-01-05 12:11:55 -0800681 taskView.updateViewPropertiesToTaskTransform(transform, animation,
682 mRequestUpdateClippingListener);
683 }
684
685 /**
Winson8aa99592016-01-19 15:07:07 -0800686 * Returns the current task transforms of all tasks, falling back to the stack layout if there
687 * is no {@link TaskView} for the task.
Winsonf24f2162016-01-05 12:11:55 -0800688 */
Winson8aa99592016-01-19 15:07:07 -0800689 public void getCurrentTaskTransforms(ArrayList<Task> tasks,
690 ArrayList<TaskViewTransform> transformsOut) {
691 Utilities.matchTaskListSize(tasks, transformsOut);
Winson66474132016-02-23 18:45:47 -0800692 int focusState = mLayoutAlgorithm.getFocusState();
Winson8aa99592016-01-19 15:07:07 -0800693 for (int i = tasks.size() - 1; i >= 0; i--) {
694 Task task = tasks.get(i);
695 TaskViewTransform transform = transformsOut.get(i);
696 TaskView tv = getChildViewForTask(task);
697 if (tv != null) {
698 transform.fillIn(tv);
699 } else {
700 mLayoutAlgorithm.getStackTransform(task, mStackScroller.getStackScroll(),
Winsone693aaf2016-03-01 12:05:59 -0800701 focusState, transform, null, true /* forceUpdate */,
702 false /* ignoreTaskOverrides */);
Winson8aa99592016-01-19 15:07:07 -0800703 }
704 transform.visible = true;
705 }
706 }
707
708 /**
709 * Returns the task transforms for all the tasks in the stack if the stack was at the given
Winson14991502016-02-15 15:40:08 -0800710 * {@param stackScroll} and {@param focusState}.
Winson8aa99592016-01-19 15:07:07 -0800711 */
Winson66474132016-02-23 18:45:47 -0800712 public void getLayoutTaskTransforms(float stackScroll, int focusState, ArrayList<Task> tasks,
Winson8aa99592016-01-19 15:07:07 -0800713 ArrayList<TaskViewTransform> transformsOut) {
714 Utilities.matchTaskListSize(tasks, transformsOut);
715 for (int i = tasks.size() - 1; i >= 0; i--) {
716 Task task = tasks.get(i);
717 TaskViewTransform transform = transformsOut.get(i);
Winson14991502016-02-15 15:40:08 -0800718 mLayoutAlgorithm.getStackTransform(task, stackScroll, focusState, transform, null,
Winsone693aaf2016-03-01 12:05:59 -0800719 true /* forceUpdate */, true /* ignoreTaskOverrides */);
Winson8aa99592016-01-19 15:07:07 -0800720 transform.visible = true;
721 }
722 }
723
724 /**
Winson05e46ca2016-02-05 15:40:29 -0800725 * Cancels the next deferred task view layout.
726 */
727 void cancelDeferredTaskViewLayoutAnimation() {
728 mDeferredTaskViewLayoutAnimation = null;
729 }
730
731 /**
Winson8aa99592016-01-19 15:07:07 -0800732 * Cancels all {@link TaskView} animations.
733 *
734 * @see #cancelAllTaskViewAnimations(ArraySet<Task.TaskKey>)
735 */
736 void cancelAllTaskViewAnimations() {
737 cancelAllTaskViewAnimations(mIgnoreTasks);
738 }
739
740 /**
741 * Cancels all {@link TaskView} animations.
742 *
743 * @param ignoreTasksSet The set of tasks to continue running their animations.
744 */
745 void cancelAllTaskViewAnimations(ArraySet<Task.TaskKey> ignoreTasksSet) {
Winsonf24f2162016-01-05 12:11:55 -0800746 List<TaskView> taskViews = getTaskViews();
Winson3e874742016-01-07 10:08:17 -0800747 for (int i = taskViews.size() - 1; i >= 0; i--) {
Winsonf24f2162016-01-05 12:11:55 -0800748 final TaskView tv = taskViews.get(i);
Winson8aa99592016-01-19 15:07:07 -0800749 if (!ignoreTasksSet.contains(tv.getTask().key)) {
750 tv.cancelTransformAnimation();
751 }
Winsonf24f2162016-01-05 12:11:55 -0800752 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800753 }
754
Winson3150e572015-10-23 15:07:24 -0700755 /**
756 * Updates the clip for each of the task views from back to front.
757 */
Winsonf24f2162016-01-05 12:11:55 -0800758 private void clipTaskViews() {
Winson387aac62015-11-25 11:18:56 -0800759 RecentsConfiguration config = Recents.getConfiguration();
760
Winson Chung93748a12014-07-13 17:43:31 -0700761 // Update the clip on each task child
Winson Chung6ac8bd62015-01-07 16:38:35 -0800762 List<TaskView> taskViews = getTaskViews();
Winson3150e572015-10-23 15:07:24 -0700763 TaskView tmpTv = null;
Winson8aa99592016-01-19 15:07:07 -0800764 TaskView prevVisibleTv = null;
Winson Chung6ac8bd62015-01-07 16:38:35 -0800765 int taskViewCount = taskViews.size();
Winson3150e572015-10-23 15:07:24 -0700766 for (int i = 0; i < taskViewCount; i++) {
Winson Chung6ac8bd62015-01-07 16:38:35 -0800767 TaskView tv = taskViews.get(i);
Winson3150e572015-10-23 15:07:24 -0700768 TaskView frontTv = null;
Winson Chung6ac8bd62015-01-07 16:38:35 -0800769 int clipBottom = 0;
Winson8aa99592016-01-19 15:07:07 -0800770
Winson05e46ca2016-02-05 15:40:29 -0800771 if (isIgnoredTask(tv.getTask())) {
Winson8aa99592016-01-19 15:07:07 -0800772 // For each of the ignore tasks, update the translationZ of its TaskView to be
773 // between the translationZ of the tasks immediately underneath it
774 if (prevVisibleTv != null) {
775 tv.setTranslationZ(Math.max(tv.getTranslationZ(),
776 prevVisibleTv.getTranslationZ() + 0.1f));
777 }
778 }
779
Winson3150e572015-10-23 15:07:24 -0700780 if (i < (taskViewCount - 1) && tv.shouldClipViewInStack()) {
Winson Chung6ac8bd62015-01-07 16:38:35 -0800781 // Find the next view to clip against
Winson3150e572015-10-23 15:07:24 -0700782 for (int j = i + 1; j < taskViewCount; j++) {
783 tmpTv = taskViews.get(j);
Winsonef064132016-01-05 12:11:31 -0800784
Winson3150e572015-10-23 15:07:24 -0700785 if (tmpTv.shouldClipViewInStack()) {
786 frontTv = tmpTv;
Winson Chung6ac8bd62015-01-07 16:38:35 -0800787 break;
Winson Chung93748a12014-07-13 17:43:31 -0700788 }
789 }
Winson Chung6ac8bd62015-01-07 16:38:35 -0800790
791 // Clip against the next view, this is just an approximation since we are
792 // stacked and we can make assumptions about the visibility of the this
793 // task relative to the ones in front of it.
Winson3150e572015-10-23 15:07:24 -0700794 if (frontTv != null) {
Winsonbb410952015-12-04 14:34:11 -0800795 float taskBottom = tv.getBottom();
796 float frontTaskTop = frontTv.getTop();
Winson3150e572015-10-23 15:07:24 -0700797 if (frontTaskTop < taskBottom) {
798 // Map the stack view space coordinate (the rects) to view space
Winsonbb410952015-12-04 14:34:11 -0800799 clipBottom = (int) (taskBottom - frontTaskTop) - mTaskCornerRadiusPx;
Winson3150e572015-10-23 15:07:24 -0700800 }
Winson Chung6ac8bd62015-01-07 16:38:35 -0800801 }
Winson Chung93748a12014-07-13 17:43:31 -0700802 }
Winsonf24f2162016-01-05 12:11:55 -0800803 tv.getViewBounds().setClipBottom(clipBottom);
Winsone693aaf2016-03-01 12:05:59 -0800804 tv.mThumbnailView.updateThumbnailVisibility(clipBottom - tv.getPaddingBottom());
Winson8aa99592016-01-19 15:07:07 -0800805 prevVisibleTv = tv;
Winson Chung6ac8bd62015-01-07 16:38:35 -0800806 }
Winsonf24f2162016-01-05 12:11:55 -0800807 mTaskViewsClipDirty = false;
Winson Chung93748a12014-07-13 17:43:31 -0700808 }
809
Winson3e874742016-01-07 10:08:17 -0800810 /**
Winson8aa99592016-01-19 15:07:07 -0800811 * Updates the layout algorithm min and max virtual scroll bounds.
812 *
813 * @see #updateLayoutAlgorithm(boolean, ArraySet<Task.TaskKey>)
814 */
815 void updateLayoutAlgorithm(boolean boundScrollToNewMinMax) {
816 updateLayoutAlgorithm(boundScrollToNewMinMax, mIgnoreTasks);
817 }
818
819 /**
Winson3e874742016-01-07 10:08:17 -0800820 * Updates the min and max virtual scroll bounds.
821 *
Winson8aa99592016-01-19 15:07:07 -0800822 * @param ignoreTasksSet the set of tasks to ignore in the relayout
Winson3e874742016-01-07 10:08:17 -0800823 */
Winson8aa99592016-01-19 15:07:07 -0800824 void updateLayoutAlgorithm(boolean boundScrollToNewMinMax,
825 ArraySet<Task.TaskKey> ignoreTasksSet) {
Winson Chung303e1ff2014-03-07 15:06:19 -0800826 // Compute the min and max scroll values
Winson3e874742016-01-07 10:08:17 -0800827 mLayoutAlgorithm.update(mStack, ignoreTasksSet);
Winson Chung303e1ff2014-03-07 15:06:19 -0800828
Winson8aa99592016-01-19 15:07:07 -0800829 // Update the freeform workspace background
Winsona5e6b362015-11-02 17:17:20 -0800830 SystemServicesProxy ssp = Recents.getSystemServices();
831 if (ssp.hasFreeformWorkspaceSupport()) {
832 mTmpRect.set(mLayoutAlgorithm.mFreeformRect);
Winsona5e6b362015-11-02 17:17:20 -0800833 mFreeformWorkspaceBackground.setBounds(mTmpRect);
834 }
835
Winson Chung303e1ff2014-03-07 15:06:19 -0800836 if (boundScrollToNewMinMax) {
Winson Chung012ef362014-07-31 18:36:25 -0700837 mStackScroller.boundScroll();
Winson Chung303e1ff2014-03-07 15:06:19 -0800838 }
839 }
840
Winson Chung012ef362014-07-31 18:36:25 -0700841 /** Returns the scroller. */
842 public TaskStackViewScroller getScroller() {
843 return mStackScroller;
844 }
845
Winson0d14d4d2015-10-26 17:05:04 -0700846 /**
847 * Sets the focused task to the provided (bounded taskIndex).
Winsone5f1faa2015-11-20 12:26:23 -0800848 *
849 * @return whether or not the stack will scroll as a part of this focus change
Winson0d14d4d2015-10-26 17:05:04 -0700850 */
Winsonf24f2162016-01-05 12:11:55 -0800851 private boolean setFocusedTask(int taskIndex, boolean scrollToTask,
Winsonaaf33bc2015-12-03 12:02:38 -0800852 final boolean requestViewFocus) {
Winson4b9cded2016-01-26 16:26:47 -0800853 return setFocusedTask(taskIndex, scrollToTask, requestViewFocus, 0);
Peter Schillerb124d562015-12-11 21:31:17 -0800854 }
855
856 /**
Winson05e46ca2016-02-05 15:40:29 -0800857 * Sets the focused task to the provided (bounded focusTaskIndex).
Peter Schillerb124d562015-12-11 21:31:17 -0800858 *
859 * @return whether or not the stack will scroll as a part of this focus change
860 */
Winson05e46ca2016-02-05 15:40:29 -0800861 private boolean setFocusedTask(int focusTaskIndex, boolean scrollToTask,
862 boolean requestViewFocus, int timerIndicatorDuration) {
Winson0d14d4d2015-10-26 17:05:04 -0700863 // Find the next task to focus
Winson4b057c62016-01-12 15:01:52 -0800864 int newFocusedTaskIndex = mStack.getTaskCount() > 0 ?
Winson68088812016-02-12 16:06:04 -0800865 Utilities.clamp(focusTaskIndex, 0, mStack.getTaskCount() - 1) : -1;
Winson0d14d4d2015-10-26 17:05:04 -0700866 final Task newFocusedTask = (newFocusedTaskIndex != -1) ?
Winson250608a2015-11-24 15:00:31 -0800867 mStack.getStackTasks().get(newFocusedTaskIndex) : null;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700868
Winson0d14d4d2015-10-26 17:05:04 -0700869 // Reset the last focused task state if changed
Winsonaaf33bc2015-12-03 12:02:38 -0800870 if (mFocusedTask != null) {
Peter Schillerb124d562015-12-11 21:31:17 -0800871 // Cancel the timer indicator, if applicable
Winson4b9cded2016-01-26 16:26:47 -0800872 if (timerIndicatorDuration > 0) {
Peter Schillerb124d562015-12-11 21:31:17 -0800873 final TaskView tv = getChildViewForTask(mFocusedTask);
874 if (tv != null) {
875 tv.getHeaderView().cancelFocusTimerIndicator();
876 }
877 }
Winsonb433c5b2016-01-20 17:11:29 -0800878
879 resetFocusedTask(mFocusedTask);
Winson0d14d4d2015-10-26 17:05:04 -0700880 }
881
Winsone5f1faa2015-11-20 12:26:23 -0800882 boolean willScroll = false;
Winsonaaf33bc2015-12-03 12:02:38 -0800883 mFocusedTask = newFocusedTask;
Peter Schillerb124d562015-12-11 21:31:17 -0800884
Winsonaaf33bc2015-12-03 12:02:38 -0800885 if (newFocusedTask != null) {
Peter Schillerb124d562015-12-11 21:31:17 -0800886 // Start the timer indicator, if applicable
Winson4b9cded2016-01-26 16:26:47 -0800887 if (timerIndicatorDuration > 0) {
Peter Schillerb124d562015-12-11 21:31:17 -0800888 final TaskView tv = getChildViewForTask(mFocusedTask);
889 if (tv != null) {
Winson4b9cded2016-01-26 16:26:47 -0800890 tv.getHeaderView().startFocusTimerIndicator(timerIndicatorDuration);
Peter Schillerb124d562015-12-11 21:31:17 -0800891 } else {
892 // The view is null; set a flag for later
Winson4b9cded2016-01-26 16:26:47 -0800893 mStartTimerIndicatorDuration = timerIndicatorDuration;
Peter Schillerb124d562015-12-11 21:31:17 -0800894 }
895 }
896
Winson0d14d4d2015-10-26 17:05:04 -0700897 if (scrollToTask) {
Winson1c846142016-01-22 11:34:38 -0800898 // Cancel any running enter animations at this point when we scroll or change focus
899 if (!mEnterAnimationComplete) {
900 cancelAllTaskViewAnimations();
901 }
902
Winsone693aaf2016-03-01 12:05:59 -0800903 mLayoutAlgorithm.clearUnfocusedTaskOverrides();
Winson05e46ca2016-02-05 15:40:29 -0800904 willScroll = mAnimationHelper.startScrollToFocusedTaskAnimation(newFocusedTask,
905 requestViewFocus);
Winson Chung1e8d71b2014-05-16 17:05:22 -0700906 } else {
Winson05e46ca2016-02-05 15:40:29 -0800907 // Focus the task view
908 TaskView newFocusedTaskView = getChildViewForTask(newFocusedTask);
909 if (newFocusedTaskView != null) {
910 newFocusedTaskView.setFocusedState(true, requestViewFocus);
911 }
Winson Chung1e8d71b2014-05-16 17:05:22 -0700912 }
913 }
Winsone5f1faa2015-11-20 12:26:23 -0800914 return willScroll;
Winson Chung1e8d71b2014-05-16 17:05:22 -0700915 }
916
Winson Chungd213a1e2014-10-02 11:18:30 -0700917 /**
Winson0d14d4d2015-10-26 17:05:04 -0700918 * Sets the focused task relative to the currently focused task.
919 *
Winsone5f1faa2015-11-20 12:26:23 -0800920 * @param forward whether to go to the next task in the stack (along the curve) or the previous
Winson1b585612015-11-06 09:16:26 -0800921 * @param stackTasksOnly if set, will ensure that the traversal only goes along stack tasks, and
922 * if the currently focused task is not a stack task, will set the focus
923 * to the first visible stack task
Winson0d14d4d2015-10-26 17:05:04 -0700924 * @param animated determines whether to actually draw the highlight along with the change in
925 * focus.
Winson Chungd213a1e2014-10-02 11:18:30 -0700926 */
Winson1b585612015-11-06 09:16:26 -0800927 public void setRelativeFocusedTask(boolean forward, boolean stackTasksOnly, boolean animated) {
Winsone5f1faa2015-11-20 12:26:23 -0800928 setRelativeFocusedTask(forward, stackTasksOnly, animated, false);
929 }
930
931 /**
932 * Sets the focused task relative to the currently focused task.
933 *
934 * @param forward whether to go to the next task in the stack (along the curve) or the previous
935 * @param stackTasksOnly if set, will ensure that the traversal only goes along stack tasks, and
936 * if the currently focused task is not a stack task, will set the focus
937 * to the first visible stack task
938 * @param animated determines whether to actually draw the highlight along with the change in
939 * focus.
940 * @param cancelWindowAnimations if set, will attempt to cancel window animations if a scroll
Peter Schillerb124d562015-12-11 21:31:17 -0800941 * happens.
Winsone5f1faa2015-11-20 12:26:23 -0800942 */
943 public void setRelativeFocusedTask(boolean forward, boolean stackTasksOnly, boolean animated,
944 boolean cancelWindowAnimations) {
Winson4b9cded2016-01-26 16:26:47 -0800945 setRelativeFocusedTask(forward, stackTasksOnly, animated, cancelWindowAnimations, 0);
Peter Schillerb124d562015-12-11 21:31:17 -0800946 }
947
948 /**
949 * Sets the focused task relative to the currently focused task.
950 *
951 * @param forward whether to go to the next task in the stack (along the curve) or the previous
952 * @param stackTasksOnly if set, will ensure that the traversal only goes along stack tasks, and
953 * if the currently focused task is not a stack task, will set the focus
954 * to the first visible stack task
955 * @param animated determines whether to actually draw the highlight along with the change in
956 * focus.
957 * @param cancelWindowAnimations if set, will attempt to cancel window animations if a scroll
958 * happens.
Winson4b9cded2016-01-26 16:26:47 -0800959 * @param timerIndicatorDuration the duration to initialize the auto-advance timer indicator
Peter Schillerb124d562015-12-11 21:31:17 -0800960 */
961 public void setRelativeFocusedTask(boolean forward, boolean stackTasksOnly, boolean animated,
Winson4b9cded2016-01-26 16:26:47 -0800962 boolean cancelWindowAnimations,
963 int timerIndicatorDuration) {
Winsonaaf33bc2015-12-03 12:02:38 -0800964 int newIndex = mStack.indexOfStackTask(mFocusedTask);
965 if (mFocusedTask != null) {
Winson1b585612015-11-06 09:16:26 -0800966 if (stackTasksOnly) {
Winson250608a2015-11-24 15:00:31 -0800967 List<Task> tasks = mStack.getStackTasks();
Winsonaaf33bc2015-12-03 12:02:38 -0800968 if (mFocusedTask.isFreeformTask()) {
Winson1b585612015-11-06 09:16:26 -0800969 // Try and focus the front most stack task
970 TaskView tv = getFrontMostTaskView(stackTasksOnly);
971 if (tv != null) {
Winson250608a2015-11-24 15:00:31 -0800972 newIndex = mStack.indexOfStackTask(tv.getTask());
Winson1b585612015-11-06 09:16:26 -0800973 }
974 } else {
975 // Try the next task if it is a stack task
Winsonaaf33bc2015-12-03 12:02:38 -0800976 int tmpNewIndex = newIndex + (forward ? -1 : 1);
Winson1b585612015-11-06 09:16:26 -0800977 if (0 <= tmpNewIndex && tmpNewIndex < tasks.size()) {
978 Task t = tasks.get(tmpNewIndex);
979 if (!t.isFreeformTask()) {
980 newIndex = tmpNewIndex;
981 }
982 }
983 }
984 } else {
Winson8b1871d2015-11-20 09:56:20 -0800985 // No restrictions, lets just move to the new task (looping forward/backwards if
986 // necessary)
Winson4b057c62016-01-12 15:01:52 -0800987 int taskCount = mStack.getTaskCount();
Winsonaaf33bc2015-12-03 12:02:38 -0800988 newIndex = (newIndex + (forward ? -1 : 1) + taskCount) % taskCount;
Winson1b585612015-11-06 09:16:26 -0800989 }
990 } else {
Winson23b0d3f2016-02-15 17:43:01 -0800991 // We don't have a focused task
992 float stackScroll = mStackScroller.getStackScroll();
993 ArrayList<Task> tasks = mStack.getStackTasks();
994 int taskCount = tasks.size();
995 if (forward) {
996 // Walk backwards and focus the next task smaller than the current stack scroll
997 for (newIndex = taskCount - 1; newIndex >= 0; newIndex--) {
998 float taskP = mLayoutAlgorithm.getStackScrollForTask(tasks.get(newIndex));
999 if (Float.compare(taskP, stackScroll) <= 0) {
1000 break;
1001 }
1002 }
1003 } else {
1004 // Walk forwards and focus the next task larger than the current stack scroll
1005 for (newIndex = 0; newIndex < taskCount; newIndex++) {
1006 float taskP = mLayoutAlgorithm.getStackScrollForTask(tasks.get(newIndex));
1007 if (Float.compare(taskP, stackScroll) >= 0) {
1008 break;
1009 }
1010 }
Winson1b585612015-11-06 09:16:26 -08001011 }
1012 }
1013 if (newIndex != -1) {
Winsonf24f2162016-01-05 12:11:55 -08001014 boolean willScroll = setFocusedTask(newIndex, true /* scrollToTask */,
Winson4b9cded2016-01-26 16:26:47 -08001015 true /* requestViewFocus */, timerIndicatorDuration);
Winsone5f1faa2015-11-20 12:26:23 -08001016 if (willScroll && cancelWindowAnimations) {
1017 // As we iterate to the next/previous task, cancel any current/lagging window
1018 // transition animations
1019 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
1020 }
Winson1b585612015-11-06 09:16:26 -08001021 }
Winson Chunga0e88b52014-08-11 19:25:42 -07001022 }
1023
Winson0d14d4d2015-10-26 17:05:04 -07001024 /**
1025 * Resets the focused task.
1026 */
Winsona0731a12015-12-02 15:10:14 -08001027 void resetFocusedTask(Task task) {
1028 if (task != null) {
1029 TaskView tv = getChildViewForTask(task);
Winson Chungfc33cdf2014-12-03 13:16:48 -08001030 if (tv != null) {
Winsonf24f2162016-01-05 12:11:55 -08001031 tv.setFocusedState(false, false /* requestViewFocus */);
Winson Chungfc33cdf2014-12-03 13:16:48 -08001032 }
Winson Chungb0a28ea2014-10-28 15:21:35 -07001033 }
Winsonaaf33bc2015-12-03 12:02:38 -08001034 mFocusedTask = null;
Winson Chungb0a28ea2014-10-28 15:21:35 -07001035 }
1036
Winson142af422015-11-09 10:39:57 -08001037 /**
1038 * Returns the focused task.
1039 */
1040 Task getFocusedTask() {
Winsonaaf33bc2015-12-03 12:02:38 -08001041 return mFocusedTask;
Winson142af422015-11-09 10:39:57 -08001042 }
1043
Winson Chung303e1ff2014-03-07 15:06:19 -08001044 @Override
Winson Chungee445952014-09-09 16:12:59 +02001045 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1046 super.onInitializeAccessibilityEvent(event);
Winson Chung6ac8bd62015-01-07 16:38:35 -08001047 List<TaskView> taskViews = getTaskViews();
1048 int taskViewCount = taskViews.size();
1049 if (taskViewCount > 0) {
1050 TaskView backMostTask = taskViews.get(0);
1051 TaskView frontMostTask = taskViews.get(taskViewCount - 1);
Winson250608a2015-11-24 15:00:31 -08001052 event.setFromIndex(mStack.indexOfStackTask(backMostTask.getTask()));
1053 event.setToIndex(mStack.indexOfStackTask(frontMostTask.getTask()));
Winson Chung296278a2015-12-17 12:09:02 -05001054 event.setContentDescription(frontMostTask.getTask().title);
Winson Chungee445952014-09-09 16:12:59 +02001055 }
Winson4b057c62016-01-12 15:01:52 -08001056 event.setItemCount(mStack.getTaskCount());
Winson Chungee445952014-09-09 16:12:59 +02001057 event.setScrollY(mStackScroller.mScroller.getCurrY());
1058 event.setMaxScrollY(mStackScroller.progressToScrollRange(mLayoutAlgorithm.mMaxScrollP));
1059 }
1060
1061 @Override
Winson Chung83ea6f72015-06-17 13:00:23 -07001062 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1063 super.onInitializeAccessibilityNodeInfo(info);
1064 List<TaskView> taskViews = getTaskViews();
1065 int taskViewCount = taskViews.size();
Winsonaaf33bc2015-12-03 12:02:38 -08001066 if (taskViewCount > 1 && mFocusedTask != null) {
Winson Chung83ea6f72015-06-17 13:00:23 -07001067 info.setScrollable(true);
Winsonaaf33bc2015-12-03 12:02:38 -08001068 int focusedTaskIndex = mStack.indexOfStackTask(mFocusedTask);
1069 if (focusedTaskIndex > 0) {
Winson Chung83ea6f72015-06-17 13:00:23 -07001070 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
1071 }
Winson4b057c62016-01-12 15:01:52 -08001072 if (focusedTaskIndex < mStack.getTaskCount() - 1) {
Winson Chung83ea6f72015-06-17 13:00:23 -07001073 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
1074 }
1075 }
1076 }
1077
1078 @Override
Winson Chung4ab4d832015-12-11 10:25:46 -05001079 protected Parcelable onSaveInstanceState() {
1080 Bundle savedState = new Bundle();
1081 savedState.putParcelable(KEY_SAVED_STATE_SUPER, super.onSaveInstanceState());
Winson66474132016-02-23 18:45:47 -08001082 savedState.putInt(KEY_SAVED_STATE_LAYOUT_FOCUSED_STATE, mLayoutAlgorithm.getFocusState());
Winson Chung4ab4d832015-12-11 10:25:46 -05001083 savedState.putFloat(KEY_SAVED_STATE_LAYOUT_STACK_SCROLL, mStackScroller.getStackScroll());
1084 return super.onSaveInstanceState();
1085 }
1086
1087 @Override
1088 protected void onRestoreInstanceState(Parcelable state) {
1089 Bundle savedState = (Bundle) state;
1090 super.onRestoreInstanceState(savedState.getParcelable(KEY_SAVED_STATE_SUPER));
1091
Winson66474132016-02-23 18:45:47 -08001092 mLayoutAlgorithm.setFocusState(savedState.getInt(KEY_SAVED_STATE_LAYOUT_FOCUSED_STATE));
Winson Chung4ab4d832015-12-11 10:25:46 -05001093 mStackScroller.setStackScroll(savedState.getFloat(KEY_SAVED_STATE_LAYOUT_STACK_SCROLL));
1094 }
1095
1096 @Override
Winson Chung83ea6f72015-06-17 13:00:23 -07001097 public CharSequence getAccessibilityClassName() {
1098 return TaskStackView.class.getName();
1099 }
1100
1101 @Override
1102 public boolean performAccessibilityAction(int action, Bundle arguments) {
1103 if (super.performAccessibilityAction(action, arguments)) {
1104 return true;
1105 }
Winson0d14d4d2015-10-26 17:05:04 -07001106 switch (action) {
1107 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
Winson1b585612015-11-06 09:16:26 -08001108 setRelativeFocusedTask(true, false /* stackTasksOnly */, false /* animated */);
Winson0d14d4d2015-10-26 17:05:04 -07001109 return true;
1110 }
1111 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
Winson1b585612015-11-06 09:16:26 -08001112 setRelativeFocusedTask(false, false /* stackTasksOnly */, false /* animated */);
Winson0d14d4d2015-10-26 17:05:04 -07001113 return true;
Winson Chung83ea6f72015-06-17 13:00:23 -07001114 }
1115 }
1116 return false;
1117 }
1118
1119 @Override
Winson Chung303e1ff2014-03-07 15:06:19 -08001120 public boolean onInterceptTouchEvent(MotionEvent ev) {
1121 return mTouchHandler.onInterceptTouchEvent(ev);
1122 }
1123
1124 @Override
1125 public boolean onTouchEvent(MotionEvent ev) {
1126 return mTouchHandler.onTouchEvent(ev);
1127 }
1128
1129 @Override
Winson Chungd213a1e2014-10-02 11:18:30 -07001130 public boolean onGenericMotionEvent(MotionEvent ev) {
1131 return mTouchHandler.onGenericMotionEvent(ev);
1132 }
1133
1134 @Override
Winson Chungd7b2cb12014-06-26 15:08:50 -07001135 public void computeScroll() {
Winsonf24f2162016-01-05 12:11:55 -08001136 if (mStackScroller.computeScroll()) {
1137 // Notify accessibility
1138 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
1139 }
Winson8aa99592016-01-19 15:07:07 -08001140 if (mDeferredTaskViewLayoutAnimation != null) {
1141 relayoutTaskViews(mDeferredTaskViewLayoutAnimation);
Winsonf24f2162016-01-05 12:11:55 -08001142 mTaskViewsClipDirty = true;
Winson8aa99592016-01-19 15:07:07 -08001143 mDeferredTaskViewLayoutAnimation = null;
Winsonf24f2162016-01-05 12:11:55 -08001144 }
1145 if (mTaskViewsClipDirty) {
1146 clipTaskViews();
1147 }
Winson Chung303e1ff2014-03-07 15:06:19 -08001148 }
1149
Winson3e874742016-01-07 10:08:17 -08001150 /**
Winson8aa99592016-01-19 15:07:07 -08001151 * Computes the maximum number of visible tasks and thumbnails. Requires that
Winsoneca4ab62015-11-04 10:50:28 -08001152 * updateLayoutForStack() is called first.
Winson Chunga91c2932014-11-07 15:02:38 -08001153 */
Winson36a5a2c2015-10-29 18:04:39 -07001154 public TaskStackLayoutAlgorithm.VisibilityReport computeStackVisibilityReport() {
Winson250608a2015-11-24 15:00:31 -08001155 return mLayoutAlgorithm.computeStackVisibilityReport(mStack.getStackTasks());
Winson Chunga91c2932014-11-07 15:02:38 -08001156 }
1157
Winson3e874742016-01-07 10:08:17 -08001158 /**
1159 * Updates the expected task stack bounds for this stack view.
1160 */
Winson88f00ab2015-10-05 17:24:00 -07001161 public void setTaskStackBounds(Rect taskStackBounds, Rect systemInsets) {
Winson3e874742016-01-07 10:08:17 -08001162 // We can get spurious measure passes with the old bounds when docking, and since we are
1163 // using the current stack bounds during drag and drop, don't overwrite them until we
1164 // actually get new bounds
Winson49df4202016-01-25 17:33:34 -08001165 boolean requiresLayout = false;
Winson3e874742016-01-07 10:08:17 -08001166 if (!taskStackBounds.equals(mStableStackBounds)) {
1167 mStableStackBounds.set(taskStackBounds);
1168 mStackBounds.set(taskStackBounds);
Winson49df4202016-01-25 17:33:34 -08001169 requiresLayout = true;
Winson3e874742016-01-07 10:08:17 -08001170 }
Winson49df4202016-01-25 17:33:34 -08001171 if (!systemInsets.equals(mLayoutAlgorithm.mSystemInsets)) {
1172 mLayoutAlgorithm.setSystemInsets(systemInsets);
1173 requiresLayout = true;
1174 }
1175 if (requiresLayout) {
1176 requestLayout();
1177 }
Winson147ecaf2015-09-16 16:49:55 -07001178 }
1179
Winson Chunga91c2932014-11-07 15:02:38 -08001180 /**
Winson Chunga4ccb862014-08-22 15:26:27 -07001181 * This is called with the full window width and height to allow stack view children to
Winson Chungdcfa7972014-07-22 12:27:13 -07001182 * perform the full screen transition down.
Winson Chungf7bca432014-04-30 17:11:13 -07001183 */
Winson Chung303e1ff2014-03-07 15:06:19 -08001184 @Override
1185 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson70f0bf72016-02-01 14:05:29 -08001186 mInMeasureLayout = true;
Winson Chung303e1ff2014-03-07 15:06:19 -08001187 int width = MeasureSpec.getSize(widthMeasureSpec);
1188 int height = MeasureSpec.getSize(heightMeasureSpec);
Winson Chung303e1ff2014-03-07 15:06:19 -08001189
Winson8aa99592016-01-19 15:07:07 -08001190 // Compute the rects in the stack algorithm
1191 mLayoutAlgorithm.initialize(mStackBounds,
1192 TaskStackLayoutAlgorithm.StackState.getStackStateForStack(mStack));
1193 updateLayoutAlgorithm(false /* boundScroll */, EMPTY_TASK_SET);
Winson Chung303e1ff2014-03-07 15:06:19 -08001194
Winsonf24f2162016-01-05 12:11:55 -08001195 // If this is the first layout, then scroll to the front of the stack, then update the
1196 // TaskViews with the stack so that we can lay them out
Winsone693aaf2016-03-01 12:05:59 -08001197 // TODO: The second check is a workaround for wacky layouts that we get while docking via
1198 // long pressing the recents button
1199 if (mAwaitingFirstLayout ||
1200 (mStackScroller.getStackScroll() == mLayoutAlgorithm.mInitialScrollP)) {
1201 updateToInitialState();
Winson Chung303e1ff2014-03-07 15:06:19 -08001202 }
Winsone693aaf2016-03-01 12:05:59 -08001203
Winson8aa99592016-01-19 15:07:07 -08001204 // Rebind all the views, including the ignore ones
Winsone693aaf2016-03-01 12:05:59 -08001205 bindVisibleTaskViews(mStackScroller.getStackScroll(), EMPTY_TASK_SET,
1206 false /* ignoreTaskOverrides */);
Winson Chung303e1ff2014-03-07 15:06:19 -08001207
Winson Chungdcfa7972014-07-22 12:27:13 -07001208 // Measure each of the TaskViews
Winsona2236f12015-11-13 16:10:01 -08001209 mTmpTaskViews.clear();
1210 mTmpTaskViews.addAll(getTaskViews());
1211 mTmpTaskViews.addAll(mViewPool.getViews());
1212 int taskViewCount = mTmpTaskViews.size();
Winson Chung6ac8bd62015-01-07 16:38:35 -08001213 for (int i = 0; i < taskViewCount; i++) {
Winson70f0bf72016-02-01 14:05:29 -08001214 measureTaskView(mTmpTaskViews.get(i));
Winson Chung303e1ff2014-03-07 15:06:19 -08001215 }
1216
1217 setMeasuredDimension(width, height);
Winson70f0bf72016-02-01 14:05:29 -08001218 mInMeasureLayout = false;
1219 }
1220
1221 /**
1222 * Measures a TaskView.
1223 */
1224 private void measureTaskView(TaskView tv) {
1225 if (tv.getBackground() != null) {
1226 tv.getBackground().getPadding(mTmpRect);
1227 } else {
1228 mTmpRect.setEmpty();
1229 }
1230 tv.measure(
1231 MeasureSpec.makeMeasureSpec(
1232 mLayoutAlgorithm.mTaskRect.width() + mTmpRect.left + mTmpRect.right,
1233 MeasureSpec.EXACTLY),
1234 MeasureSpec.makeMeasureSpec(
1235 mLayoutAlgorithm.mTaskRect.height() + mTmpRect.top + mTmpRect.bottom,
1236 MeasureSpec.EXACTLY));
Winson Chung303e1ff2014-03-07 15:06:19 -08001237 }
1238
Winson Chungf7bca432014-04-30 17:11:13 -07001239 /**
1240 * This is called with the size of the space not including the top or right insets, or the
1241 * search bar height in portrait (but including the search bar width in landscape, since we want
1242 * to draw under it.
1243 */
Winson Chung303e1ff2014-03-07 15:06:19 -08001244 @Override
1245 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Winson36a5a2c2015-10-29 18:04:39 -07001246 // Layout each of the TaskViews
Winsona2236f12015-11-13 16:10:01 -08001247 mTmpTaskViews.clear();
1248 mTmpTaskViews.addAll(getTaskViews());
1249 mTmpTaskViews.addAll(mViewPool.getViews());
1250 int taskViewCount = mTmpTaskViews.size();
Winson Chung6ac8bd62015-01-07 16:38:35 -08001251 for (int i = 0; i < taskViewCount; i++) {
Winson70f0bf72016-02-01 14:05:29 -08001252 layoutTaskView(mTmpTaskViews.get(i));
Winson Chung303e1ff2014-03-07 15:06:19 -08001253 }
1254
Jorim Jaggi7af8ea82015-11-09 15:28:34 +01001255 if (changed) {
Winsona2236f12015-11-13 16:10:01 -08001256 if (mStackScroller.isScrollOutOfBounds()) {
1257 mStackScroller.boundScroll();
1258 }
Winsonf24f2162016-01-05 12:11:55 -08001259 }
Winson8aa99592016-01-19 15:07:07 -08001260 // Relayout all of the task views including the ignored ones
Winsonbe8e6962016-02-01 14:27:52 -08001261 relayoutTaskViews(AnimationProps.IMMEDIATE, EMPTY_TASK_SET);
Winsonf24f2162016-01-05 12:11:55 -08001262 clipTaskViews();
1263
1264 if (mAwaitingFirstLayout || !mEnterAnimationComplete) {
1265 mAwaitingFirstLayout = false;
1266 onFirstLayout();
Jorim Jaggi7af8ea82015-11-09 15:28:34 +01001267 }
Winson Chungdcfa7972014-07-22 12:27:13 -07001268 }
Winson Chung24cf1522014-05-29 12:03:33 -07001269
Winson70f0bf72016-02-01 14:05:29 -08001270 /**
1271 * Lays out a TaskView.
1272 */
1273 private void layoutTaskView(TaskView tv) {
1274 if (tv.getBackground() != null) {
1275 tv.getBackground().getPadding(mTmpRect);
1276 } else {
1277 mTmpRect.setEmpty();
1278 }
1279 Rect taskRect = mLayoutAlgorithm.mTaskRect;
1280 tv.layout(taskRect.left - mTmpRect.left, taskRect.top - mTmpRect.top,
1281 taskRect.right + mTmpRect.right, taskRect.bottom + mTmpRect.bottom);
1282 }
1283
Winson Chungdcfa7972014-07-22 12:27:13 -07001284 /** Handler for the first layout. */
1285 void onFirstLayout() {
Winsonf24f2162016-01-05 12:11:55 -08001286 // Setup the view for the enter animation
1287 mAnimationHelper.prepareForEnterAnimation();
Winson Chung083baf92014-07-11 10:32:42 -07001288
Winsona78a8f32015-12-03 10:55:01 -08001289 // Animate in the freeform workspace
Winsonbe8e6962016-02-01 14:27:52 -08001290 int ffBgAlpha = mLayoutAlgorithm.getStackState().freeformBackgroundAlpha;
1291 animateFreeformWorkspaceBackgroundAlpha(ffBgAlpha, new AnimationProps(150,
1292 Interpolators.FAST_OUT_SLOW_IN));
Winsona78a8f32015-12-03 10:55:01 -08001293
Winson0d14d4d2015-10-26 17:05:04 -07001294 // Set the task focused state without requesting view focus, and leave the focus animations
1295 // until after the enter-animation
Winson5da43472015-11-04 17:39:55 -08001296 RecentsConfiguration config = Recents.getConfiguration();
1297 RecentsActivityLaunchState launchState = config.getLaunchState();
Winson4b9cded2016-01-26 16:26:47 -08001298 int focusedTaskIndex = launchState.getInitialFocusTaskIndex(mStack.getTaskCount());
Winson5da43472015-11-04 17:39:55 -08001299 if (focusedTaskIndex != -1) {
Winsonf24f2162016-01-05 12:11:55 -08001300 setFocusedTask(focusedTaskIndex, false /* scrollToTask */,
Winson5da43472015-11-04 17:39:55 -08001301 false /* requestViewFocus */);
Winson0983e022015-10-13 17:21:42 -07001302 }
Winson Chung860e2d82014-12-04 11:43:02 -08001303
Winsonc29ff002015-11-20 16:00:45 -08001304 // Update the history button visibility
Winson Chungde750de2015-12-11 10:26:06 -05001305 if (shouldShowHistoryButton() &&
1306 mStackScroller.getStackScroll() < SHOW_HISTORY_BUTTON_SCROLL_THRESHOLD) {
Winson49df4202016-01-25 17:33:34 -08001307 EventBus.getDefault().send(new ShowHistoryButtonEvent(false /* translate */));
Winson Chungde750de2015-12-11 10:26:06 -05001308 } else {
1309 EventBus.getDefault().send(new HideHistoryButtonEvent());
Winsonc29ff002015-11-20 16:00:45 -08001310 }
Winson Chung24cf1522014-05-29 12:03:33 -07001311 }
1312
Winson671e8f92016-01-12 13:16:56 -08001313 public boolean isTouchPointInView(float x, float y, TaskView tv) {
Winson8aa99592016-01-19 15:07:07 -08001314 mTmpRect.set(tv.getLeft(), tv.getTop(), tv.getRight(), tv.getBottom());
1315 mTmpRect.offset((int) tv.getTranslationX(), (int) tv.getTranslationY());
1316 return mTmpRect.contains((int) x, (int) y);
1317 }
1318
1319 /**
1320 * Returns a non-ignored task in the {@param tasks} list that can be used as an achor when
1321 * calculating the scroll position before and after a layout change.
1322 */
1323 public Task findAnchorTask(List<Task> tasks, MutableBoolean isFrontMostTask) {
1324 for (int i = tasks.size() - 1; i >= 0; i--) {
1325 Task task = tasks.get(i);
1326
1327 // Ignore deleting tasks
Winson05e46ca2016-02-05 15:40:29 -08001328 if (isIgnoredTask(task)) {
Winson8aa99592016-01-19 15:07:07 -08001329 if (i == tasks.size() - 1) {
1330 isFrontMostTask.value = true;
1331 }
1332 continue;
1333 }
1334 return task;
1335 }
1336 return null;
Winson Chung303e1ff2014-03-07 15:06:19 -08001337 }
1338
Jorim Jaggi900fb482015-06-02 15:07:33 -07001339 @Override
Winsonbe8e6962016-02-01 14:27:52 -08001340 protected void onDraw(Canvas canvas) {
1341 super.onDraw(canvas);
1342
Winson36a5a2c2015-10-29 18:04:39 -07001343 // Draw the freeform workspace background
Winson805578d2015-11-23 14:47:37 -08001344 SystemServicesProxy ssp = Recents.getSystemServices();
1345 if (ssp.hasFreeformWorkspaceSupport()) {
1346 if (mFreeformWorkspaceBackground.getAlpha() > 0) {
1347 mFreeformWorkspaceBackground.draw(canvas);
1348 }
Winson36a5a2c2015-10-29 18:04:39 -07001349 }
Jorim Jaggi900fb482015-06-02 15:07:33 -07001350 }
1351
Winsona78a8f32015-12-03 10:55:01 -08001352 @Override
1353 protected boolean verifyDrawable(Drawable who) {
1354 if (who == mFreeformWorkspaceBackground) {
1355 return true;
1356 }
1357 return super.verifyDrawable(who);
1358 }
1359
Winsona5e6b362015-11-02 17:17:20 -08001360 /**
1361 * Launches the freeform tasks.
1362 */
1363 public boolean launchFreeformTasks() {
Winsonf24f2162016-01-05 12:11:55 -08001364 ArrayList<Task> tasks = mStack.getFreeformTasks();
1365 if (!tasks.isEmpty()) {
1366 Task frontTask = tasks.get(tasks.size() - 1);
1367 if (frontTask != null && frontTask.isFreeformTask()) {
1368 EventBus.getDefault().send(new LaunchTaskEvent(getChildViewForTask(frontTask),
1369 frontTask, null, INVALID_STACK_ID, false));
1370 return true;
1371 }
Winsona5e6b362015-11-02 17:17:20 -08001372 }
1373 return false;
1374 }
1375
Winson Chung303e1ff2014-03-07 15:06:19 -08001376 /**** TaskStackCallbacks Implementation ****/
1377
1378 @Override
Winson Chung06266772015-12-11 10:24:21 -05001379 public void onStackTaskAdded(TaskStack stack, Task newTask) {
1380 // Update the min/max scroll and animate other task views into their new positions
Winson8aa99592016-01-19 15:07:07 -08001381 updateLayoutAlgorithm(true /* boundScroll */);
Winson Chung06266772015-12-11 10:24:21 -05001382
1383 // Animate all the tasks into place
Winsonbe8e6962016-02-01 14:27:52 -08001384 relayoutTaskViews(new AnimationProps(DEFAULT_SYNC_STACK_DURATION,
Winsonc0d70582016-01-29 10:24:39 -08001385 Interpolators.FAST_OUT_SLOW_IN));
Winson Chung06266772015-12-11 10:24:21 -05001386 }
1387
Winson8aa99592016-01-19 15:07:07 -08001388 /**
1389 * We expect that the {@link TaskView} associated with the removed task is already hidden.
1390 */
Winson Chung06266772015-12-11 10:24:21 -05001391 @Override
Winsonaaf33bc2015-12-03 12:02:38 -08001392 public void onStackTaskRemoved(TaskStack stack, Task removedTask, boolean wasFrontMostTask,
Winsonbe8e6962016-02-01 14:27:52 -08001393 Task newFrontMostTask, AnimationProps animation) {
Winsonaaf33bc2015-12-03 12:02:38 -08001394 if (mFocusedTask == removedTask) {
Winsona0731a12015-12-02 15:10:14 -08001395 resetFocusedTask(removedTask);
1396 }
1397
Winson8aa99592016-01-19 15:07:07 -08001398 // Remove the view associated with this task, we can't rely on updateTransforms
1399 // to work here because the task is no longer in the list
1400 TaskView tv = getChildViewForTask(removedTask);
1401 if (tv != null) {
1402 mViewPool.returnViewToPool(tv);
Winson Chung303e1ff2014-03-07 15:06:19 -08001403 }
1404
Winson8aa99592016-01-19 15:07:07 -08001405 // Remove the task from the ignored set
1406 removeIgnoreTask(removedTask);
1407
1408 // If requested, relayout with the given animation
1409 if (animation != null) {
1410 updateLayoutAlgorithm(true /* boundScroll */);
1411 relayoutTaskViews(animation);
1412 }
Winsonf24f2162016-01-05 12:11:55 -08001413
Winson Chung931c51f2015-12-17 17:08:55 -05001414 // Update the new front most task's action button
1415 if (mScreenPinningEnabled && newFrontMostTask != null) {
Winson Chung1f24c7e2014-07-11 17:06:48 -07001416 TaskView frontTv = getChildViewForTask(newFrontMostTask);
1417 if (frontTv != null) {
Winson Chung931c51f2015-12-17 17:08:55 -05001418 frontTv.showActionButton(true /* fadeIn */, DEFAULT_SYNC_STACK_DURATION);
Winson Chung1f24c7e2014-07-11 17:06:48 -07001419 }
1420 }
1421
Winson397ae742015-11-20 11:27:33 -08001422 // If there are no remaining tasks, then just close recents
Winson4b057c62016-01-12 15:01:52 -08001423 if (mStack.getTaskCount() == 0) {
1424 EventBus.getDefault().send(new AllTaskViewsDismissedEvent());
Winson Chung9f49df92014-05-07 18:08:34 -07001425 }
Winson Chung303e1ff2014-03-07 15:06:19 -08001426 }
1427
Winsona0731a12015-12-02 15:10:14 -08001428 @Override
Winson8aa99592016-01-19 15:07:07 -08001429 public void onHistoryTaskRemoved(TaskStack stack, Task removedTask,
Winsonbe8e6962016-02-01 14:27:52 -08001430 AnimationProps animation) {
Winsona0731a12015-12-02 15:10:14 -08001431 // To be implemented
1432 }
1433
Winson Chung303e1ff2014-03-07 15:06:19 -08001434 /**** ViewPoolConsumer Implementation ****/
1435
1436 @Override
1437 public TaskView createView(Context context) {
Winson Chung37c8d8e2014-03-24 14:53:07 -07001438 return (TaskView) mInflater.inflate(R.layout.recents_task_view, this, false);
Winson Chung303e1ff2014-03-07 15:06:19 -08001439 }
1440
1441 @Override
Winson05e46ca2016-02-05 15:40:29 -08001442 public void onReturnViewToPool(TaskView tv) {
Winson Chung931c51f2015-12-17 17:08:55 -05001443 final Task task = tv.getTask();
Winson Chung303e1ff2014-03-07 15:06:19 -08001444
Winson43336942016-03-07 14:52:59 -08001445 // Unbind the task from the task view
1446 unbindTaskView(tv, task);
Winson Chung303e1ff2014-03-07 15:06:19 -08001447
Winson Chung931c51f2015-12-17 17:08:55 -05001448 // Reset the view properties and view state
Winson Chung8eaeb7d2014-06-25 15:10:59 -07001449 tv.resetViewProperties();
Winsonf24f2162016-01-05 12:11:55 -08001450 tv.setFocusedState(false, false /* requestViewFocus */);
Winson Chungb0a28ea2014-10-28 15:21:35 -07001451 tv.setClipViewInStack(false);
Winson Chung931c51f2015-12-17 17:08:55 -05001452 if (mScreenPinningEnabled) {
Winsonf24f2162016-01-05 12:11:55 -08001453 tv.hideActionButton(false /* fadeOut */, 0 /* duration */, false /* scaleDown */, null);
Winson Chung931c51f2015-12-17 17:08:55 -05001454 }
Winson3c107162016-01-22 15:53:00 -08001455
1456 // Detach the view from the hierarchy
1457 detachViewFromParent(tv);
1458 // Update the task views list after removing the task view
1459 updateTaskViewsList();
Winson Chung303e1ff2014-03-07 15:06:19 -08001460 }
1461
1462 @Override
Winson05e46ca2016-02-05 15:40:29 -08001463 public void onPickUpViewFromPool(TaskView tv, Task task, boolean isNewView) {
Winson Chung8eaeb7d2014-06-25 15:10:59 -07001464 // Find the index where this task should be placed in the stack
Winson250608a2015-11-24 15:00:31 -08001465 int taskIndex = mStack.indexOfStackTask(task);
Winsona78a8f32015-12-03 10:55:01 -08001466 int insertIndex = findTaskViewInsertIndex(task, taskIndex);
Winson Chung8eaeb7d2014-06-25 15:10:59 -07001467
Winson Chung303e1ff2014-03-07 15:06:19 -08001468 // Add/attach the view to the hierarchy
Winson Chung303e1ff2014-03-07 15:06:19 -08001469 if (isNewView) {
Winson70f0bf72016-02-01 14:05:29 -08001470 if (mInMeasureLayout) {
1471 // If we are measuring the layout, then just add the view normally as it will be
1472 // laid out during the layout pass
1473 addView(tv, insertIndex);
1474 } else {
1475 // Otherwise, this is from a bindVisibleTaskViews() call outside the measure/layout
1476 // pass, and we should layout the new child ourselves
1477 ViewGroup.LayoutParams params = tv.getLayoutParams();
1478 if (params == null) {
1479 params = generateDefaultLayoutParams();
1480 }
1481 addViewInLayout(tv, insertIndex, params, true /* preventRequestLayout */);
1482 measureTaskView(tv);
1483 layoutTaskView(tv);
1484 }
Winson Chung303e1ff2014-03-07 15:06:19 -08001485 } else {
1486 attachViewToParent(tv, insertIndex, tv.getLayoutParams());
1487 }
Winson Chung6ac8bd62015-01-07 16:38:35 -08001488 // Update the task views list after adding the new task view
1489 updateTaskViewsList();
Winson Chungb0a28ea2014-10-28 15:21:35 -07001490
Winson43336942016-03-07 14:52:59 -08001491 // Bind the task view to the new task
1492 bindTaskView(tv, task);
Winson3c107162016-01-22 15:53:00 -08001493
1494 // If the doze trigger has already fired, then update the state for this task view
Winsone693aaf2016-03-01 12:05:59 -08001495 if (mUIDozeTrigger.isAsleep()) {
Winson Chungbbb3d3d2016-01-30 01:09:20 +00001496 tv.setNoUserInteractionState();
1497 }
Winson3c107162016-01-22 15:53:00 -08001498
Winson Chungb0a28ea2014-10-28 15:21:35 -07001499 // Set the new state for this view, including the callbacks and view clipping
1500 tv.setCallbacks(this);
1501 tv.setTouchEnabled(true);
1502 tv.setClipViewInStack(true);
Winsonaaf33bc2015-12-03 12:02:38 -08001503 if (mFocusedTask == task) {
Winsonf24f2162016-01-05 12:11:55 -08001504 tv.setFocusedState(true, false /* requestViewFocus */);
Winson4b9cded2016-01-26 16:26:47 -08001505 if (mStartTimerIndicatorDuration > 0) {
Peter Schillerb124d562015-12-11 21:31:17 -08001506 // The timer indicator couldn't be started before, so start it now
Winson4b9cded2016-01-26 16:26:47 -08001507 tv.getHeaderView().startFocusTimerIndicator(mStartTimerIndicatorDuration);
1508 mStartTimerIndicatorDuration = 0;
Peter Schillerb124d562015-12-11 21:31:17 -08001509 }
Winsona0731a12015-12-02 15:10:14 -08001510 }
Winson Chung931c51f2015-12-17 17:08:55 -05001511
1512 // Restore the action button visibility if it is the front most task view
Winson35a8b042016-01-22 09:41:09 -08001513 if (mScreenPinningEnabled && tv.getTask() ==
1514 mStack.getStackFrontMostTask(false /* includeFreeform */)) {
Winson Chung931c51f2015-12-17 17:08:55 -05001515 tv.showActionButton(false /* fadeIn */, 0 /* fadeInDuration */);
1516 }
Winson Chung303e1ff2014-03-07 15:06:19 -08001517 }
1518
1519 @Override
1520 public boolean hasPreferredData(TaskView tv, Task preferredData) {
1521 return (tv.getTask() == preferredData);
1522 }
1523
Winson43336942016-03-07 14:52:59 -08001524 private void bindTaskView(TaskView tv, Task task) {
1525 // Rebind the task and request that this task's data be filled into the TaskView
1526 tv.onTaskBound(task);
1527
1528 // Load the task data
1529 Recents.getTaskLoader().loadTaskData(task, true /* fetchAndInvalidateThumbnails */);
1530 }
1531
1532 private void unbindTaskView(TaskView tv, Task task) {
1533 // Report that this task's data is no longer being used
1534 Recents.getTaskLoader().unloadTaskData(task);
1535 }
1536
Winson Chung303e1ff2014-03-07 15:06:19 -08001537 /**** TaskViewCallbacks Implementation ****/
1538
1539 @Override
Winson Chung93748a12014-07-13 17:43:31 -07001540 public void onTaskViewClipStateChanged(TaskView tv) {
Winson8aa99592016-01-19 15:07:07 -08001541 if (!mTaskViewsClipDirty) {
1542 mTaskViewsClipDirty = true;
1543 invalidate();
1544 }
Winson Chung93748a12014-07-13 17:43:31 -07001545 }
1546
Winson1c846142016-01-22 11:34:38 -08001547 /**** TaskStackLayoutAlgorithm.TaskStackLayoutAlgorithmCallbacks ****/
1548
1549 @Override
Winson66474132016-02-23 18:45:47 -08001550 public void onFocusStateChanged(int prevFocusState, int curFocusState) {
Winson1c846142016-01-22 11:34:38 -08001551 if (mDeferredTaskViewLayoutAnimation == null) {
1552 mUIDozeTrigger.poke();
Winsonbe8e6962016-02-01 14:27:52 -08001553 relayoutTaskViewsOnNextFrame(AnimationProps.IMMEDIATE);
Winson1c846142016-01-22 11:34:38 -08001554 }
1555 }
1556
Winson Chung012ef362014-07-31 18:36:25 -07001557 /**** TaskStackViewScroller.TaskStackViewScrollerCallbacks ****/
1558
1559 @Override
Winson14991502016-02-15 15:40:08 -08001560 public void onStackScrollChanged(float prevScroll, float curScroll, AnimationProps animation) {
Winson Chung012ef362014-07-31 18:36:25 -07001561 mUIDozeTrigger.poke();
Winson8aa99592016-01-19 15:07:07 -08001562 if (animation != null) {
1563 relayoutTaskViewsOnNextFrame(animation);
1564 }
Winson66474132016-02-23 18:45:47 -08001565 mLayoutAlgorithm.updateFocusStateOnScroll(curScroll, curScroll - prevScroll);
Winsonc29ff002015-11-20 16:00:45 -08001566
Winson49df4202016-01-25 17:33:34 -08001567 if (mEnterAnimationComplete) {
1568 if (shouldShowHistoryButton() &&
1569 prevScroll > SHOW_HISTORY_BUTTON_SCROLL_THRESHOLD &&
1570 curScroll <= SHOW_HISTORY_BUTTON_SCROLL_THRESHOLD) {
1571 EventBus.getDefault().send(new ShowHistoryButtonEvent(true /* translate */));
1572 } else if (prevScroll < HIDE_HISTORY_BUTTON_SCROLL_THRESHOLD &&
1573 curScroll >= HIDE_HISTORY_BUTTON_SCROLL_THRESHOLD) {
1574 EventBus.getDefault().send(new HideHistoryButtonEvent());
1575 }
Winsonc29ff002015-11-20 16:00:45 -08001576 }
Winson Chung012ef362014-07-31 18:36:25 -07001577 }
1578
Winsone6c90732015-09-24 16:06:29 -07001579 /**** EventBus Events ****/
Winson Chung9f49df92014-05-07 18:08:34 -07001580
Winsone6c90732015-09-24 16:06:29 -07001581 public final void onBusEvent(PackagesChangedEvent event) {
Winson Chung04400672014-10-17 14:53:30 -07001582 // Compute which components need to be removed
Winson55003902016-01-12 12:00:37 -08001583 ArraySet<ComponentName> removedComponents = mStack.computeComponentsRemoved(
Winsone7f138c2015-10-22 16:15:21 -07001584 event.packageName, event.userId);
Winson Chung04400672014-10-17 14:53:30 -07001585
Winson Chung9f49df92014-05-07 18:08:34 -07001586 // For other tasks, just remove them directly if they no longer exist
Winson250608a2015-11-24 15:00:31 -08001587 ArrayList<Task> tasks = mStack.getStackTasks();
Winson Chung9f49df92014-05-07 18:08:34 -07001588 for (int i = tasks.size() - 1; i >= 0; i--) {
1589 final Task t = tasks.get(i);
Winsone7f138c2015-10-22 16:15:21 -07001590 if (removedComponents.contains(t.key.getComponent())) {
Winson0d14d4d2015-10-26 17:05:04 -07001591 final TaskView tv = getChildViewForTask(t);
Winson Chung9f49df92014-05-07 18:08:34 -07001592 if (tv != null) {
1593 // For visible children, defer removing the task until after the animation
Winsonf24f2162016-01-05 12:11:55 -08001594 tv.dismissTask();
Winson Chung9f49df92014-05-07 18:08:34 -07001595 } else {
1596 // Otherwise, remove the task from the stack immediately
Winsonbe8e6962016-02-01 14:27:52 -08001597 mStack.removeTask(t, AnimationProps.IMMEDIATE);
Winson Chung9f49df92014-05-07 18:08:34 -07001598 }
1599 }
1600 }
1601 }
Winson2536c7e2015-10-01 15:49:31 -07001602
Winson Chung48f2cda2015-12-11 13:20:12 -05001603 public final void onBusEvent(LaunchTaskEvent event) {
1604 // Cancel any doze triggers once a task is launched
1605 mUIDozeTrigger.stopDozing();
1606 }
1607
Winsonb61e6542016-02-04 14:37:18 -08001608 public final void onBusEvent(LaunchNextTaskRequestEvent event) {
1609 int launchTaskIndex = mStack.indexOfStackTask(mStack.getLaunchTarget());
1610 if (launchTaskIndex != -1) {
1611 launchTaskIndex = Math.max(0, launchTaskIndex - 1);
1612 } else {
1613 launchTaskIndex = mStack.getTaskCount() - 1;
1614 }
1615 if (launchTaskIndex != -1) {
1616 // Stop all animations
1617 mUIDozeTrigger.stopDozing();
1618 cancelAllTaskViewAnimations();
1619
1620 Task launchTask = mStack.getStackTasks().get(launchTaskIndex);
1621 EventBus.getDefault().send(new LaunchTaskEvent(getChildViewForTask(launchTask),
1622 launchTask, null, INVALID_STACK_ID, false /* screenPinningRequested */));
Winsond9342902016-02-25 10:18:33 -08001623
1624 MetricsLogger.action(getContext(), MetricsEvent.OVERVIEW_LAUNCH_PREVIOUS_TASK,
1625 launchTask.key.getComponent().toString());
Winsonb61e6542016-02-04 14:37:18 -08001626 }
1627 }
1628
Winsonef064132016-01-05 12:11:31 -08001629 public final void onBusEvent(LaunchTaskStartedEvent event) {
Winsonf24f2162016-01-05 12:11:55 -08001630 mAnimationHelper.startLaunchTaskAnimation(event.taskView, event.screenPinningRequested,
1631 event.getAnimationTrigger());
Winson0d14d4d2015-10-26 17:05:04 -07001632 }
Winson2536c7e2015-10-01 15:49:31 -07001633
Winsonef064132016-01-05 12:11:31 -08001634 public final void onBusEvent(DismissRecentsToHomeAnimationStarted event) {
1635 // Stop any scrolling
1636 mStackScroller.stopScroller();
1637 mStackScroller.stopBoundScrollAnimation();
Winson2536c7e2015-10-01 15:49:31 -07001638
Winsonef064132016-01-05 12:11:31 -08001639 // Start the task animations
Winsonf24f2162016-01-05 12:11:55 -08001640 mAnimationHelper.startExitToHomeAnimation(event.animated, event.getAnimationTrigger());
Winsonef064132016-01-05 12:11:31 -08001641
1642 // Dismiss the freeform workspace background
Winson50448632016-02-01 18:04:59 -08001643 int taskViewExitToHomeDuration = TaskStackAnimationHelper.EXIT_TO_HOME_TRANSLATION_DURATION;
Winsonbe8e6962016-02-01 14:27:52 -08001644 animateFreeformWorkspaceBackgroundAlpha(0, new AnimationProps(taskViewExitToHomeDuration,
1645 Interpolators.FAST_OUT_SLOW_IN));
Winson0d14d4d2015-10-26 17:05:04 -07001646 }
Winson42be4312015-10-10 11:51:08 -07001647
Winson0d14d4d2015-10-26 17:05:04 -07001648 public final void onBusEvent(DismissFocusedTaskViewEvent event) {
Winsonaaf33bc2015-12-03 12:02:38 -08001649 if (mFocusedTask != null) {
1650 TaskView tv = getChildViewForTask(mFocusedTask);
1651 if (tv != null) {
1652 tv.dismissTask();
1653 }
1654 resetFocusedTask(mFocusedTask);
Winson2536c7e2015-10-01 15:49:31 -07001655 }
1656 }
Winsone7f138c2015-10-22 16:15:21 -07001657
Winsonef064132016-01-05 12:11:31 -08001658 public final void onBusEvent(final DismissTaskViewEvent event) {
1659 // For visible children, defer removing the task until after the animation
Winsonf24f2162016-01-05 12:11:55 -08001660 mAnimationHelper.startDeleteTaskAnimation(event.task, event.taskView,
1661 event.getAnimationTrigger());
Winsonef064132016-01-05 12:11:31 -08001662 }
1663
1664 public final void onBusEvent(TaskViewDismissedEvent event) {
Winson8aa99592016-01-19 15:07:07 -08001665 removeTaskViewFromStack(event.taskView, event.task);
Winsonef064132016-01-05 12:11:31 -08001666 EventBus.getDefault().send(new DeleteTaskDataEvent(event.task));
Winson42329522016-02-05 10:39:46 -08001667
1668 MetricsLogger.action(getContext(), MetricsEvent.OVERVIEW_DISMISS,
1669 event.task.key.getComponent().toString());
Winsonef064132016-01-05 12:11:31 -08001670 }
1671
1672 public final void onBusEvent(FocusNextTaskViewEvent event) {
Winson66474132016-02-23 18:45:47 -08001673 // Stop any scrolling
1674 mStackScroller.stopScroller();
1675 mStackScroller.stopBoundScrollAnimation();
1676
Peter Schillerb124d562015-12-11 21:31:17 -08001677 setRelativeFocusedTask(true, false /* stackTasksOnly */, true /* animated */, false,
Winson4b9cded2016-01-26 16:26:47 -08001678 event.timerIndicatorDuration);
Winsonef064132016-01-05 12:11:31 -08001679 }
1680
1681 public final void onBusEvent(FocusPreviousTaskViewEvent event) {
Winson66474132016-02-23 18:45:47 -08001682 // Stop any scrolling
1683 mStackScroller.stopScroller();
1684 mStackScroller.stopBoundScrollAnimation();
1685
Winsonef064132016-01-05 12:11:31 -08001686 setRelativeFocusedTask(false, false /* stackTasksOnly */, true /* animated */);
1687 }
1688
Winsone7f138c2015-10-22 16:15:21 -07001689 public final void onBusEvent(UserInteractionEvent event) {
1690 // Poke the doze trigger on user interaction
1691 mUIDozeTrigger.poke();
Winson4b9cded2016-01-26 16:26:47 -08001692
1693 RecentsDebugFlags debugFlags = Recents.getDebugFlags();
1694 if (debugFlags.isFastToggleRecentsEnabled() && mFocusedTask != null) {
Winsonc5ef63f2016-01-21 14:39:23 -08001695 TaskView tv = getChildViewForTask(mFocusedTask);
1696 if (tv != null) {
1697 tv.getHeaderView().cancelFocusTimerIndicator();
1698 }
Peter Schillerb124d562015-12-11 21:31:17 -08001699 }
Winsone7f138c2015-10-22 16:15:21 -07001700 }
1701
Winsoneca4ab62015-11-04 10:50:28 -08001702 public final void onBusEvent(DragStartEvent event) {
Winson70f0bf72016-02-01 14:05:29 -08001703 // Ensure that the drag task is not animated
1704 addIgnoreTask(event.task);
1705
Winsoneca4ab62015-11-04 10:50:28 -08001706 if (event.task.isFreeformTask()) {
1707 // Animate to the front of the stack
Winsond9529612016-01-28 13:29:49 -08001708 mStackScroller.animateScroll(mLayoutAlgorithm.mInitialScrollP, null);
Winsoneca4ab62015-11-04 10:50:28 -08001709 }
Winsonf24f2162016-01-05 12:11:55 -08001710
1711 // Enlarge the dragged view slightly
1712 float finalScale = event.taskView.getScaleX() * DRAG_SCALE_FACTOR;
1713 mLayoutAlgorithm.getStackTransform(event.task, getScroller().getStackScroll(),
1714 mTmpTransform, null);
1715 mTmpTransform.scale = finalScale;
Winson3e874742016-01-07 10:08:17 -08001716 mTmpTransform.translationZ = mLayoutAlgorithm.mMaxTranslationZ + 1;
Winsonf24f2162016-01-05 12:11:55 -08001717 updateTaskViewToTransform(event.taskView, mTmpTransform,
Winsonbe8e6962016-02-01 14:27:52 -08001718 new AnimationProps(DRAG_SCALE_DURATION, Interpolators.FAST_OUT_SLOW_IN));
Winsoneca4ab62015-11-04 10:50:28 -08001719 }
1720
1721 public final void onBusEvent(DragStartInitializeDropTargetsEvent event) {
1722 SystemServicesProxy ssp = Recents.getSystemServices();
1723 if (ssp.hasFreeformWorkspaceSupport()) {
1724 event.handler.registerDropTargetForCurrentDrag(mStackDropTarget);
1725 event.handler.registerDropTargetForCurrentDrag(mFreeformWorkspaceDropTarget);
1726 }
1727 }
1728
1729 public final void onBusEvent(DragDropTargetChangedEvent event) {
Winsonbe8e6962016-02-01 14:27:52 -08001730 AnimationProps animation = new AnimationProps(250, Interpolators.FAST_OUT_SLOW_IN);
Winson3e874742016-01-07 10:08:17 -08001731 if (event.dropTarget instanceof TaskStack.DockState) {
1732 // Calculate the new task stack bounds that matches the window size that Recents will
1733 // have after the drop
1734 final TaskStack.DockState dockState = (TaskStack.DockState) event.dropTarget;
1735 mStackBounds.set(dockState.getDockedTaskStackBounds(getMeasuredWidth(),
1736 getMeasuredHeight(), mDividerSize, mLayoutAlgorithm.mSystemInsets,
1737 getResources()));
Winson8aa99592016-01-19 15:07:07 -08001738 mLayoutAlgorithm.initialize(mStackBounds,
1739 TaskStackLayoutAlgorithm.StackState.getStackStateForStack(mStack));
1740 updateLayoutAlgorithm(true /* boundScroll */);
Winson3e874742016-01-07 10:08:17 -08001741 } else {
Winson8aa99592016-01-19 15:07:07 -08001742 // Restore the pre-drag task stack bounds, but ensure that we don't layout the dragging
1743 // task view, so add it back to the ignore set after updating the layout
Winson3e874742016-01-07 10:08:17 -08001744 mStackBounds.set(mStableStackBounds);
Winson8aa99592016-01-19 15:07:07 -08001745 removeIgnoreTask(event.task);
1746 mLayoutAlgorithm.initialize(mStackBounds,
1747 TaskStackLayoutAlgorithm.StackState.getStackStateForStack(mStack));
1748 updateLayoutAlgorithm(true /* boundScroll */);
1749 addIgnoreTask(event.task);
Winson3e874742016-01-07 10:08:17 -08001750 }
Winson8aa99592016-01-19 15:07:07 -08001751 relayoutTaskViews(animation);
Winsoneca4ab62015-11-04 10:50:28 -08001752 }
1753
1754 public final void onBusEvent(final DragEndEvent event) {
Winson479f7442015-11-25 15:16:27 -08001755 // We don't handle drops on the dock regions
1756 if (event.dropTarget instanceof TaskStack.DockState) {
Winsoneca4ab62015-11-04 10:50:28 -08001757 return;
1758 }
1759
Winson479f7442015-11-25 15:16:27 -08001760 boolean isFreeformTask = event.task.isFreeformTask();
1761 boolean hasChangedStacks =
1762 (!isFreeformTask && event.dropTarget == mFreeformWorkspaceDropTarget) ||
1763 (isFreeformTask && event.dropTarget == mStackDropTarget);
Winson479f7442015-11-25 15:16:27 -08001764
Winson5b7dd532015-12-01 16:02:12 -08001765 if (hasChangedStacks) {
Winson479f7442015-11-25 15:16:27 -08001766 // Move the task to the right position in the stack (ie. the front of the stack if
Winson8aa99592016-01-19 15:07:07 -08001767 // freeform or the front of the stack if fullscreen). Note, we MUST move the tasks
Winson479f7442015-11-25 15:16:27 -08001768 // before we update their stack ids, otherwise, the keys will have changed.
1769 if (event.dropTarget == mFreeformWorkspaceDropTarget) {
1770 mStack.moveTaskToStack(event.task, FREEFORM_WORKSPACE_STACK_ID);
Winson479f7442015-11-25 15:16:27 -08001771 } else if (event.dropTarget == mStackDropTarget) {
1772 mStack.moveTaskToStack(event.task, FULLSCREEN_WORKSPACE_STACK_ID);
Winson479f7442015-11-25 15:16:27 -08001773 }
Winson8aa99592016-01-19 15:07:07 -08001774 updateLayoutAlgorithm(true /* boundScroll */);
Winson479f7442015-11-25 15:16:27 -08001775
1776 // Move the task to the new stack in the system after the animation completes
Winson Chungaaeaac12015-12-16 16:49:36 -05001777 event.addPostAnimationCallback(new Runnable() {
Winson479f7442015-11-25 15:16:27 -08001778 @Override
1779 public void run() {
1780 SystemServicesProxy ssp = Recents.getSystemServices();
1781 ssp.moveTaskToStack(event.task.key.id, event.task.key.stackId);
1782 }
1783 });
Winsoneca4ab62015-11-04 10:50:28 -08001784 }
Winsoneca4ab62015-11-04 10:50:28 -08001785
Winsonbb410952015-12-04 14:34:11 -08001786 // We translated the view but we need to animate it back from the current layout-space rect
1787 // to its final layout-space rect
1788 int x = (int) event.taskView.getTranslationX();
1789 int y = (int) event.taskView.getTranslationY();
1790 Rect taskViewRect = new Rect(event.taskView.getLeft(), event.taskView.getTop(),
1791 event.taskView.getRight(), event.taskView.getBottom());
1792 taskViewRect.offset(x, y);
1793 event.taskView.setTranslationX(0);
1794 event.taskView.setTranslationY(0);
1795 event.taskView.setLeftTopRightBottom(taskViewRect.left, taskViewRect.top,
1796 taskViewRect.right, taskViewRect.bottom);
1797
Winson88737542016-02-17 13:27:33 -08001798 // Animate the non-drag TaskViews back into position
Winsonf24f2162016-01-05 12:11:55 -08001799 mLayoutAlgorithm.getStackTransform(event.task, getScroller().getStackScroll(),
1800 mTmpTransform, null);
1801 event.getAnimationTrigger().increment();
Winsonbe8e6962016-02-01 14:27:52 -08001802 relayoutTaskViews(new AnimationProps(DEFAULT_SYNC_STACK_DURATION,
Winsonc0d70582016-01-29 10:24:39 -08001803 Interpolators.FAST_OUT_SLOW_IN));
Winson88737542016-02-17 13:27:33 -08001804
1805 // Animate the drag TaskView back into position
Winsonf24f2162016-01-05 12:11:55 -08001806 updateTaskViewToTransform(event.taskView, mTmpTransform,
Winsonbe8e6962016-02-01 14:27:52 -08001807 new AnimationProps(DEFAULT_SYNC_STACK_DURATION, Interpolators.FAST_OUT_SLOW_IN,
Winsonf24f2162016-01-05 12:11:55 -08001808 event.getAnimationTrigger().decrementOnAnimationEnd()));
Winson8aa99592016-01-19 15:07:07 -08001809 removeIgnoreTask(event.task);
Winsoneca4ab62015-11-04 10:50:28 -08001810 }
1811
Winson8b1871d2015-11-20 09:56:20 -08001812 public final void onBusEvent(IterateRecentsEvent event) {
Winson Chungd6b78a32015-12-15 10:22:45 -05001813 if (!mEnterAnimationComplete) {
1814 // Cancel the previous task's window transition before animating the focused state
1815 EventBus.getDefault().send(new CancelEnterRecentsWindowAnimationEvent(null));
1816 }
Winson8b1871d2015-11-20 09:56:20 -08001817 }
1818
Winsone5f1faa2015-11-20 12:26:23 -08001819 public final void onBusEvent(EnterRecentsWindowAnimationCompletedEvent event) {
1820 mEnterAnimationComplete = true;
Winsonef064132016-01-05 12:11:31 -08001821
Winson4b057c62016-01-12 15:01:52 -08001822 if (mStack.getTaskCount() > 0) {
Winsonef064132016-01-05 12:11:31 -08001823 // Start the task enter animations
Winsonf24f2162016-01-05 12:11:55 -08001824 mAnimationHelper.startEnterAnimation(event.getAnimationTrigger());
Winsonef064132016-01-05 12:11:31 -08001825
1826 // Add a runnable to the post animation ref counter to clear all the views
1827 event.addPostAnimationCallback(new Runnable() {
1828 @Override
1829 public void run() {
1830 // Start the dozer to trigger to trigger any UI that shows after a timeout
1831 mUIDozeTrigger.startDozing();
1832
1833 // Update the focused state here -- since we only set the focused task without
1834 // requesting view focus in onFirstLayout(), actually request view focus and
1835 // animate the focused state if we are alt-tabbing now, after the window enter
1836 // animation is completed
1837 if (mFocusedTask != null) {
1838 RecentsConfiguration config = Recents.getConfiguration();
1839 RecentsActivityLaunchState launchState = config.getLaunchState();
1840 setFocusedTask(mStack.indexOfStackTask(mFocusedTask),
1841 false /* scrollToTask */, launchState.launchedWithAltTab);
1842 }
Winson4b9cded2016-01-26 16:26:47 -08001843
1844 EventBus.getDefault().send(new EnterRecentsTaskStackAnimationCompletedEvent());
Winsonef064132016-01-05 12:11:31 -08001845 }
1846 });
1847 }
Winsone5f1faa2015-11-20 12:26:23 -08001848 }
1849
Winsonb1e71d02015-11-23 12:40:23 -08001850 public final void onBusEvent(UpdateFreeformTaskViewVisibilityEvent event) {
1851 List<TaskView> taskViews = getTaskViews();
1852 int taskViewCount = taskViews.size();
1853 for (int i = 0; i < taskViewCount; i++) {
1854 TaskView tv = taskViews.get(i);
1855 Task task = tv.getTask();
Winson Chung296278a2015-12-17 12:09:02 -05001856 if (task.isFreeformTask()) {
Winsonb1e71d02015-11-23 12:40:23 -08001857 tv.setVisibility(event.visible ? View.VISIBLE : View.INVISIBLE);
1858 }
1859 }
1860 }
1861
Winsonc29ff002015-11-20 16:00:45 -08001862 public final void onBusEvent(ShowHistoryEvent event) {
Winson49df4202016-01-25 17:33:34 -08001863 ReferenceCountedTrigger postAnimTrigger = new ReferenceCountedTrigger();
1864 postAnimTrigger.addLastDecrementRunnable(new Runnable() {
1865 @Override
1866 public void run() {
1867 setVisibility(View.INVISIBLE);
1868 }
1869 });
1870 mAnimationHelper.startShowHistoryAnimation(postAnimTrigger);
Winsonc29ff002015-11-20 16:00:45 -08001871 }
1872
1873 public final void onBusEvent(HideHistoryEvent event) {
Winson49df4202016-01-25 17:33:34 -08001874 setVisibility(View.VISIBLE);
1875 mAnimationHelper.startHideHistoryAnimation();
Winsonc29ff002015-11-20 16:00:45 -08001876 }
1877
Winson88737542016-02-17 13:27:33 -08001878 public final void onBusEvent(MultiWindowStateChangedEvent event) {
Winson931845f2016-02-24 19:38:41 -08001879 if (!event.inMultiWindow) {
Winson88737542016-02-17 13:27:33 -08001880 // Scroll the stack to the front to see the undocked task
1881 mStackScroller.animateScroll(mLayoutAlgorithm.mMaxScrollP, new Runnable() {
Winson931845f2016-02-24 19:38:41 -08001882 @Override
1883 public void run() {
Winson88737542016-02-17 13:27:33 -08001884 List<TaskView> taskViews = getTaskViews();
1885 int taskViewCount = taskViews.size();
1886 for (int i = 0; i < taskViewCount; i++) {
1887 TaskView tv = taskViews.get(i);
1888 tv.getHeaderView().rebindToTask(tv.getTask(), tv.mTouchExplorationEnabled,
1889 tv.mIsDisabledInSafeMode);
1890 }
Winson931845f2016-02-24 19:38:41 -08001891 }
1892 });
1893 }
Winsond9529612016-01-28 13:29:49 -08001894 }
1895
Winsone693aaf2016-03-01 12:05:59 -08001896 public final void onBusEvent(ConfigurationChangedEvent event) {
1897 mLayoutAlgorithm.reloadOnConfigurationChange(getContext());
1898 mLayoutAlgorithm.initialize(mStackBounds,
1899 TaskStackLayoutAlgorithm.StackState.getStackStateForStack(mStack));
1900 }
1901
Winson0d14d4d2015-10-26 17:05:04 -07001902 /**
1903 * Removes the task from the stack, and updates the focus to the next task in the stack if the
1904 * removed TaskView was focused.
1905 */
Winson8aa99592016-01-19 15:07:07 -08001906 private void removeTaskViewFromStack(TaskView tv, Task task) {
Winson0d14d4d2015-10-26 17:05:04 -07001907 // Announce for accessibility
1908 tv.announceForAccessibility(getContext().getString(
Winson8aa99592016-01-19 15:07:07 -08001909 R.string.accessibility_recents_item_dismissed, task.title));
Winson0d14d4d2015-10-26 17:05:04 -07001910
1911 // Remove the task from the stack
Winsonbe8e6962016-02-01 14:27:52 -08001912 mStack.removeTask(task, new AnimationProps(DEFAULT_SYNC_STACK_DURATION,
Winsonc0d70582016-01-29 10:24:39 -08001913 Interpolators.FAST_OUT_SLOW_IN));
Winson0d14d4d2015-10-26 17:05:04 -07001914 }
Winsona78a8f32015-12-03 10:55:01 -08001915
1916 /**
1917 * Starts an alpha animation on the freeform workspace background.
1918 */
Winsonbe8e6962016-02-01 14:27:52 -08001919 private void animateFreeformWorkspaceBackgroundAlpha(int targetAlpha,
1920 AnimationProps animation) {
Winsona78a8f32015-12-03 10:55:01 -08001921 if (mFreeformWorkspaceBackground.getAlpha() == targetAlpha) {
1922 return;
1923 }
1924
1925 Utilities.cancelAnimationWithoutCallbacks(mFreeformWorkspaceBackgroundAnimator);
1926 mFreeformWorkspaceBackgroundAnimator = ObjectAnimator.ofInt(mFreeformWorkspaceBackground,
Winson3e874742016-01-07 10:08:17 -08001927 Utilities.DRAWABLE_ALPHA, mFreeformWorkspaceBackground.getAlpha(), targetAlpha);
Winsonbe8e6962016-02-01 14:27:52 -08001928 mFreeformWorkspaceBackgroundAnimator.setStartDelay(
1929 animation.getDuration(AnimationProps.ALPHA));
1930 mFreeformWorkspaceBackgroundAnimator.setDuration(
1931 animation.getDuration(AnimationProps.ALPHA));
1932 mFreeformWorkspaceBackgroundAnimator.setInterpolator(
1933 animation.getInterpolator(AnimationProps.ALPHA));
Winsona78a8f32015-12-03 10:55:01 -08001934 mFreeformWorkspaceBackgroundAnimator.start();
1935 }
1936
1937 /**
Winson8aa99592016-01-19 15:07:07 -08001938 * Returns the insert index for the task in the current set of task views. If the given task
Winsona78a8f32015-12-03 10:55:01 -08001939 * is already in the task view list, then this method returns the insert index assuming it
1940 * is first removed at the previous index.
1941 *
1942 * @param task the task we are finding the index for
1943 * @param taskIndex the index of the task in the stack
1944 */
1945 private int findTaskViewInsertIndex(Task task, int taskIndex) {
1946 if (taskIndex != -1) {
1947 List<TaskView> taskViews = getTaskViews();
1948 boolean foundTaskView = false;
1949 int taskViewCount = taskViews.size();
1950 for (int i = 0; i < taskViewCount; i++) {
1951 Task tvTask = taskViews.get(i).getTask();
1952 if (tvTask == task) {
1953 foundTaskView = true;
1954 } else if (taskIndex < mStack.indexOfStackTask(tvTask)) {
1955 if (foundTaskView) {
1956 return i - 1;
1957 } else {
1958 return i;
1959 }
1960 }
1961 }
1962 }
1963 return -1;
1964 }
Winson Chungde750de2015-12-11 10:26:06 -05001965
1966 /**
1967 * @return whether the history button should be visible
1968 */
1969 private boolean shouldShowHistoryButton() {
1970 return !mStack.getHistoricalTasks().isEmpty();
1971 }
Winson6ea25882016-01-13 10:59:04 -08001972
1973 /**
1974 * Reads current system flags related to accessibility and screen pinning.
1975 */
1976 private void readSystemFlags() {
1977 SystemServicesProxy ssp = Recents.getSystemServices();
1978 mTouchExplorationEnabled = ssp.isTouchExplorationEnabled();
1979 mScreenPinningEnabled = ssp.getSystemSetting(getContext(),
1980 Settings.System.LOCK_TO_APP_ENABLED) != 0;
1981 }
Winson Chunga4cc9662014-07-25 12:10:38 -07001982}