Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.recents.model; |
| 18 | |
Winson | 59924fe | 2016-03-17 14:13:18 -0700 | [diff] [blame] | 19 | import static android.app.ActivityManager.DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT; |
| 20 | import static android.app.ActivityManager.DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT; |
| 21 | import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID; |
| 22 | import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID; |
| 23 | import static android.view.WindowManager.DOCKED_BOTTOM; |
| 24 | import static android.view.WindowManager.DOCKED_INVALID; |
| 25 | import static android.view.WindowManager.DOCKED_LEFT; |
| 26 | import static android.view.WindowManager.DOCKED_RIGHT; |
| 27 | import static android.view.WindowManager.DOCKED_TOP; |
| 28 | |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 29 | import android.animation.Animator; |
| 30 | import android.animation.AnimatorSet; |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 31 | import android.animation.ObjectAnimator; |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 32 | import android.animation.PropertyValuesHolder; |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 33 | import android.annotation.IntDef; |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 34 | import android.content.ComponentName; |
Winson | 35f3050 | 2015-09-28 11:24:36 -0700 | [diff] [blame] | 35 | import android.content.Context; |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 36 | import android.content.res.Configuration; |
| 37 | import android.content.res.Resources; |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 38 | import android.graphics.Canvas; |
Winson Chung | ec396d6 | 2014-08-06 17:08:00 -0700 | [diff] [blame] | 39 | import android.graphics.Color; |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 40 | import android.graphics.Paint; |
| 41 | import android.graphics.Point; |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 42 | import android.graphics.Rect; |
| 43 | import android.graphics.RectF; |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 44 | import android.graphics.drawable.ColorDrawable; |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 45 | import android.util.ArrayMap; |
| 46 | import android.util.ArraySet; |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 47 | import android.util.IntProperty; |
Winson Chung | 2b9ef65 | 2015-12-11 10:23:59 -0500 | [diff] [blame] | 48 | import android.util.SparseArray; |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 49 | import android.view.animation.Interpolator; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 50 | |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 51 | import com.android.internal.policy.DockedDividerUtils; |
Winson | 3f32e7e | 2016-04-20 17:18:08 -0700 | [diff] [blame] | 52 | import com.android.systemui.Interpolators; |
Winson | 2536c7e | 2015-10-01 15:49:31 -0700 | [diff] [blame] | 53 | import com.android.systemui.R; |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 54 | import com.android.systemui.recents.Recents; |
Winson | c742f97 | 2015-11-12 11:32:21 -0800 | [diff] [blame] | 55 | import com.android.systemui.recents.RecentsDebugFlags; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 56 | import com.android.systemui.recents.misc.NamedCounter; |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 57 | import com.android.systemui.recents.misc.SystemServicesProxy; |
Winson Chung | a0e88b5 | 2014-08-11 19:25:42 -0700 | [diff] [blame] | 58 | import com.android.systemui.recents.misc.Utilities; |
Winson | be8e696 | 2016-02-01 14:27:52 -0800 | [diff] [blame] | 59 | import com.android.systemui.recents.views.AnimationProps; |
Winson | 59924fe | 2016-03-17 14:13:18 -0700 | [diff] [blame] | 60 | import com.android.systemui.recents.views.DropTarget; |
| 61 | import com.android.systemui.recents.views.TaskStackLayoutAlgorithm; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 62 | |
Winson | d72c315 | 2016-04-05 15:33:35 -0700 | [diff] [blame] | 63 | import java.io.PrintWriter; |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 64 | import java.lang.annotation.Retention; |
| 65 | import java.lang.annotation.RetentionPolicy; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 66 | import java.util.ArrayList; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 67 | import java.util.Collections; |
| 68 | import java.util.Comparator; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 69 | import java.util.List; |
Winson Chung | a433fa9 | 2014-07-08 21:50:31 -0700 | [diff] [blame] | 70 | import java.util.Random; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 71 | |
| 72 | |
| 73 | /** |
| 74 | * An interface for a task filter to query whether a particular task should show in a stack. |
| 75 | */ |
| 76 | interface TaskFilter { |
| 77 | /** Returns whether the filter accepts the specified task */ |
Winson Chung | 2b9ef65 | 2015-12-11 10:23:59 -0500 | [diff] [blame] | 78 | public boolean acceptTask(SparseArray<Task> taskIdMap, Task t, int index); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * A list of filtered tasks. |
| 83 | */ |
| 84 | class FilteredTaskList { |
Winson | eca4ab6 | 2015-11-04 10:50:28 -0800 | [diff] [blame] | 85 | |
Winson | eca4ab6 | 2015-11-04 10:50:28 -0800 | [diff] [blame] | 86 | ArrayList<Task> mTasks = new ArrayList<>(); |
| 87 | ArrayList<Task> mFilteredTasks = new ArrayList<>(); |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 88 | ArrayMap<Task.TaskKey, Integer> mTaskIndices = new ArrayMap<>(); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 89 | TaskFilter mFilter; |
| 90 | |
| 91 | /** Sets the task filter, saving the current touch state */ |
Winson Chung | c6a1623 | 2014-04-01 14:04:48 -0700 | [diff] [blame] | 92 | boolean setFilter(TaskFilter filter) { |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 93 | ArrayList<Task> prevFilteredTasks = new ArrayList<>(mFilteredTasks); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 94 | mFilter = filter; |
| 95 | updateFilteredTasks(); |
Winson Chung | b44c24f | 2014-04-09 15:17:43 -0700 | [diff] [blame] | 96 | if (!prevFilteredTasks.equals(mFilteredTasks)) { |
| 97 | return true; |
| 98 | } else { |
Winson Chung | b44c24f | 2014-04-09 15:17:43 -0700 | [diff] [blame] | 99 | return false; |
| 100 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** Removes the task filter and returns the previous touch state */ |
| 104 | void removeFilter() { |
| 105 | mFilter = null; |
| 106 | updateFilteredTasks(); |
| 107 | } |
| 108 | |
| 109 | /** Adds a new task to the task list */ |
| 110 | void add(Task t) { |
| 111 | mTasks.add(t); |
| 112 | updateFilteredTasks(); |
| 113 | } |
| 114 | |
Winson | eca4ab6 | 2015-11-04 10:50:28 -0800 | [diff] [blame] | 115 | /** |
| 116 | * Moves the given task. |
| 117 | */ |
| 118 | public void moveTaskToStack(Task task, int insertIndex, int newStackId) { |
| 119 | int taskIndex = indexOf(task); |
| 120 | if (taskIndex != insertIndex) { |
| 121 | mTasks.remove(taskIndex); |
| 122 | if (taskIndex < insertIndex) { |
| 123 | insertIndex--; |
| 124 | } |
| 125 | mTasks.add(insertIndex, task); |
| 126 | } |
| 127 | |
| 128 | // Update the stack id now, after we've moved the task, and before we update the |
| 129 | // filtered tasks |
| 130 | task.setStackId(newStackId); |
| 131 | updateFilteredTasks(); |
| 132 | } |
| 133 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 134 | /** Sets the list of tasks */ |
| 135 | void set(List<Task> tasks) { |
| 136 | mTasks.clear(); |
| 137 | mTasks.addAll(tasks); |
| 138 | updateFilteredTasks(); |
| 139 | } |
| 140 | |
| 141 | /** Removes a task from the base list only if it is in the filtered list */ |
| 142 | boolean remove(Task t) { |
| 143 | if (mFilteredTasks.contains(t)) { |
| 144 | boolean removed = mTasks.remove(t); |
| 145 | updateFilteredTasks(); |
| 146 | return removed; |
| 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | /** Returns the index of this task in the list of filtered tasks */ |
| 152 | int indexOf(Task t) { |
Winson | 23746d5 | 2015-12-03 16:13:07 -0800 | [diff] [blame] | 153 | if (t != null && mTaskIndices.containsKey(t.key)) { |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 154 | return mTaskIndices.get(t.key); |
| 155 | } |
| 156 | return -1; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** Returns the size of the list of filtered tasks */ |
| 160 | int size() { |
| 161 | return mFilteredTasks.size(); |
| 162 | } |
| 163 | |
| 164 | /** Returns whether the filtered list contains this task */ |
| 165 | boolean contains(Task t) { |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 166 | return mTaskIndices.containsKey(t.key); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** Updates the list of filtered tasks whenever the base task list changes */ |
| 170 | private void updateFilteredTasks() { |
| 171 | mFilteredTasks.clear(); |
| 172 | if (mFilter != null) { |
Winson Chung | 2b9ef65 | 2015-12-11 10:23:59 -0500 | [diff] [blame] | 173 | // Create a sparse array from task id to Task |
| 174 | SparseArray<Task> taskIdMap = new SparseArray<>(); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 175 | int taskCount = mTasks.size(); |
| 176 | for (int i = 0; i < taskCount; i++) { |
| 177 | Task t = mTasks.get(i); |
Winson Chung | 2b9ef65 | 2015-12-11 10:23:59 -0500 | [diff] [blame] | 178 | taskIdMap.put(t.key.id, t); |
| 179 | } |
| 180 | |
| 181 | for (int i = 0; i < taskCount; i++) { |
| 182 | Task t = mTasks.get(i); |
| 183 | if (mFilter.acceptTask(taskIdMap, t, i)) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 184 | mFilteredTasks.add(t); |
| 185 | } |
| 186 | } |
| 187 | } else { |
| 188 | mFilteredTasks.addAll(mTasks); |
| 189 | } |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 190 | updateFilteredTaskIndices(); |
| 191 | } |
| 192 | |
| 193 | /** Updates the mapping of tasks to indices. */ |
| 194 | private void updateFilteredTaskIndices() { |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 195 | int taskCount = mFilteredTasks.size(); |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 196 | mTaskIndices.clear(); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 197 | for (int i = 0; i < taskCount; i++) { |
| 198 | Task t = mFilteredTasks.get(i); |
| 199 | mTaskIndices.put(t.key, i); |
| 200 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** Returns whether this task list is filtered */ |
| 204 | boolean hasFilter() { |
| 205 | return (mFilter != null); |
| 206 | } |
| 207 | |
| 208 | /** Returns the list of filtered tasks */ |
| 209 | ArrayList<Task> getTasks() { |
| 210 | return mFilteredTasks; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * The task stack contains a list of multiple tasks. |
| 216 | */ |
| 217 | public class TaskStack { |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 218 | |
Winson | d72c315 | 2016-04-05 15:33:35 -0700 | [diff] [blame] | 219 | private static final String TAG = "TaskStack"; |
| 220 | |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 221 | /** Task stack callbacks */ |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 222 | public interface TaskStackCallbacks { |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 223 | /** |
| 224 | * Notifies when a new task has been added to the stack. |
| 225 | */ |
| 226 | void onStackTaskAdded(TaskStack stack, Task newTask); |
| 227 | |
| 228 | /** |
| 229 | * Notifies when a task has been removed from the stack. |
| 230 | */ |
Winson | 6c8217a | 2016-05-25 10:53:53 -0700 | [diff] [blame] | 231 | void onStackTaskRemoved(TaskStack stack, Task removedTask, Task newFrontMostTask, |
| 232 | AnimationProps animation, boolean fromDockGesture); |
Winson | a1ededd | 2016-03-25 12:23:12 -0700 | [diff] [blame] | 233 | |
| 234 | /** |
Winson | 3b6ba1a | 2016-03-22 15:37:54 -0700 | [diff] [blame] | 235 | * Notifies when all tasks have been removed from the stack. |
| 236 | */ |
| 237 | void onStackTasksRemoved(TaskStack stack); |
| 238 | |
| 239 | /** |
Winson | a1ededd | 2016-03-25 12:23:12 -0700 | [diff] [blame] | 240 | * Notifies when tasks in the stack have been updated. |
| 241 | */ |
| 242 | void onStackTasksUpdated(TaskStack stack); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 245 | /** |
| 246 | * The various possible dock states when dragging and dropping a task. |
| 247 | */ |
Winson | f0d1c44 | 2015-12-01 11:04:45 -0800 | [diff] [blame] | 248 | public static class DockState implements DropTarget { |
| 249 | |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 250 | // The rotation to apply to the hint text |
| 251 | @Retention(RetentionPolicy.SOURCE) |
| 252 | @IntDef({HORIZONTAL, VERTICAL}) |
| 253 | public @interface TextOrientation {} |
| 254 | private static final int HORIZONTAL = 0; |
| 255 | private static final int VERTICAL = 1; |
| 256 | |
Winson | 3f32e7e | 2016-04-20 17:18:08 -0700 | [diff] [blame] | 257 | private static final int DOCK_AREA_ALPHA = 80; |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 258 | public static final DockState NONE = new DockState(DOCKED_INVALID, -1, 80, 255, HORIZONTAL, |
| 259 | null, null, null); |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 260 | public static final DockState LEFT = new DockState(DOCKED_LEFT, |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 261 | DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT, DOCK_AREA_ALPHA, 0, VERTICAL, |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 262 | new RectF(0, 0, 0.125f, 1), new RectF(0, 0, 0.125f, 1), |
| 263 | new RectF(0, 0, 0.5f, 1)); |
| 264 | public static final DockState TOP = new DockState(DOCKED_TOP, |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 265 | DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT, DOCK_AREA_ALPHA, 0, HORIZONTAL, |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 266 | new RectF(0, 0, 1, 0.125f), new RectF(0, 0, 1, 0.125f), |
| 267 | new RectF(0, 0, 1, 0.5f)); |
| 268 | public static final DockState RIGHT = new DockState(DOCKED_RIGHT, |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 269 | DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT, DOCK_AREA_ALPHA, 0, VERTICAL, |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 270 | new RectF(0.875f, 0, 1, 1), new RectF(0.875f, 0, 1, 1), |
| 271 | new RectF(0.5f, 0, 1, 1)); |
| 272 | public static final DockState BOTTOM = new DockState(DOCKED_BOTTOM, |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 273 | DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT, DOCK_AREA_ALPHA, 0, HORIZONTAL, |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 274 | new RectF(0, 0.875f, 1, 1), new RectF(0, 0.875f, 1, 1), |
| 275 | new RectF(0, 0.5f, 1, 1)); |
Winson | eca4ab6 | 2015-11-04 10:50:28 -0800 | [diff] [blame] | 276 | |
| 277 | @Override |
Winson | 08deff0 | 2016-08-05 13:58:31 -0700 | [diff] [blame] | 278 | public boolean acceptsDrop(int x, int y, int width, int height, Rect insets, |
| 279 | boolean isCurrentTarget) { |
| 280 | if (isCurrentTarget) { |
| 281 | getMappedRect(expandedTouchDockArea, width, height, mTmpRect); |
| 282 | return mTmpRect.contains(x, y); |
| 283 | } else { |
| 284 | getMappedRect(touchArea, width, height, mTmpRect); |
| 285 | updateBoundsWithSystemInsets(mTmpRect, insets); |
| 286 | return mTmpRect.contains(x, y); |
| 287 | } |
Winson | eca4ab6 | 2015-11-04 10:50:28 -0800 | [diff] [blame] | 288 | } |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 289 | |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 290 | // Represents the view state of this dock state |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 291 | public static class ViewState { |
| 292 | private static final IntProperty<ViewState> HINT_ALPHA = |
| 293 | new IntProperty<ViewState>("drawableAlpha") { |
| 294 | @Override |
| 295 | public void setValue(ViewState object, int alpha) { |
| 296 | object.mHintTextAlpha = alpha; |
| 297 | object.dockAreaOverlay.invalidateSelf(); |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public Integer get(ViewState object) { |
| 302 | return object.mHintTextAlpha; |
| 303 | } |
| 304 | }; |
| 305 | |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 306 | public final int dockAreaAlpha; |
| 307 | public final ColorDrawable dockAreaOverlay; |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 308 | public final int hintTextAlpha; |
| 309 | public final int hintTextOrientation; |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 310 | |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 311 | private final int mHintTextResId; |
| 312 | private String mHintText; |
| 313 | private Paint mHintTextPaint; |
| 314 | private Point mHintTextBounds = new Point(); |
| 315 | private int mHintTextAlpha = 255; |
| 316 | private AnimatorSet mDockAreaOverlayAnimator; |
| 317 | private Rect mTmpRect = new Rect(); |
| 318 | |
| 319 | private ViewState(int areaAlpha, int hintAlpha, @TextOrientation int hintOrientation, |
| 320 | int hintTextResId) { |
| 321 | dockAreaAlpha = areaAlpha; |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 322 | dockAreaOverlay = new ColorDrawable(0xFFffffff); |
| 323 | dockAreaOverlay.setAlpha(0); |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 324 | hintTextAlpha = hintAlpha; |
| 325 | hintTextOrientation = hintOrientation; |
| 326 | mHintTextResId = hintTextResId; |
| 327 | mHintTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 328 | mHintTextPaint.setColor(Color.WHITE); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Updates the view state with the given context. |
| 333 | */ |
| 334 | public void update(Context context) { |
| 335 | Resources res = context.getResources(); |
| 336 | mHintText = context.getString(mHintTextResId); |
| 337 | mHintTextPaint.setTextSize(res.getDimensionPixelSize( |
| 338 | R.dimen.recents_drag_hint_text_size)); |
| 339 | mHintTextPaint.getTextBounds(mHintText, 0, mHintText.length(), mTmpRect); |
| 340 | mHintTextBounds.set((int) mHintTextPaint.measureText(mHintText), mTmpRect.height()); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Draws the current view state. |
| 345 | */ |
| 346 | public void draw(Canvas canvas) { |
| 347 | // Draw the overlay background |
| 348 | if (dockAreaOverlay.getAlpha() > 0) { |
| 349 | dockAreaOverlay.draw(canvas); |
| 350 | } |
| 351 | |
| 352 | // Draw the hint text |
| 353 | if (mHintTextAlpha > 0) { |
| 354 | Rect bounds = dockAreaOverlay.getBounds(); |
| 355 | int x = bounds.left + (bounds.width() - mHintTextBounds.x) / 2; |
| 356 | int y = bounds.top + (bounds.height() + mHintTextBounds.y) / 2; |
| 357 | mHintTextPaint.setAlpha(mHintTextAlpha); |
| 358 | if (hintTextOrientation == VERTICAL) { |
| 359 | canvas.save(); |
| 360 | canvas.rotate(-90f, bounds.centerX(), bounds.centerY()); |
| 361 | } |
| 362 | canvas.drawText(mHintText, x, y, mHintTextPaint); |
| 363 | if (hintTextOrientation == VERTICAL) { |
| 364 | canvas.restore(); |
| 365 | } |
| 366 | } |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /** |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 370 | * Creates a new bounds and alpha animation. |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 371 | */ |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 372 | public void startAnimation(Rect bounds, int areaAlpha, int hintAlpha, int duration, |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 373 | Interpolator interpolator, boolean animateAlpha, boolean animateBounds) { |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 374 | if (mDockAreaOverlayAnimator != null) { |
| 375 | mDockAreaOverlayAnimator.cancel(); |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Winson | 3f32e7e | 2016-04-20 17:18:08 -0700 | [diff] [blame] | 378 | ObjectAnimator anim; |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 379 | ArrayList<Animator> animators = new ArrayList<>(); |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 380 | if (dockAreaOverlay.getAlpha() != areaAlpha) { |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 381 | if (animateAlpha) { |
Winson | 3f32e7e | 2016-04-20 17:18:08 -0700 | [diff] [blame] | 382 | anim = ObjectAnimator.ofInt(dockAreaOverlay, |
| 383 | Utilities.DRAWABLE_ALPHA, dockAreaOverlay.getAlpha(), areaAlpha); |
| 384 | anim.setDuration(duration); |
| 385 | anim.setInterpolator(interpolator); |
| 386 | animators.add(anim); |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 387 | } else { |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 388 | dockAreaOverlay.setAlpha(areaAlpha); |
| 389 | } |
| 390 | } |
| 391 | if (mHintTextAlpha != hintAlpha) { |
| 392 | if (animateAlpha) { |
Winson | 3f32e7e | 2016-04-20 17:18:08 -0700 | [diff] [blame] | 393 | anim = ObjectAnimator.ofInt(this, HINT_ALPHA, mHintTextAlpha, |
| 394 | hintAlpha); |
| 395 | anim.setDuration(150); |
| 396 | anim.setInterpolator(hintAlpha > mHintTextAlpha |
| 397 | ? Interpolators.ALPHA_IN |
| 398 | : Interpolators.ALPHA_OUT); |
| 399 | animators.add(anim); |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 400 | } else { |
| 401 | mHintTextAlpha = hintAlpha; |
| 402 | dockAreaOverlay.invalidateSelf(); |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 403 | } |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 404 | } |
| 405 | if (bounds != null && !dockAreaOverlay.getBounds().equals(bounds)) { |
| 406 | if (animateBounds) { |
| 407 | PropertyValuesHolder prop = PropertyValuesHolder.ofObject( |
Winson | 67c7957 | 2016-04-13 14:02:18 -0700 | [diff] [blame] | 408 | Utilities.DRAWABLE_RECT, Utilities.RECT_EVALUATOR, |
Winson | 3f32e7e | 2016-04-20 17:18:08 -0700 | [diff] [blame] | 409 | new Rect(dockAreaOverlay.getBounds()), bounds); |
| 410 | anim = ObjectAnimator.ofPropertyValuesHolder(dockAreaOverlay, prop); |
| 411 | anim.setDuration(duration); |
| 412 | anim.setInterpolator(interpolator); |
| 413 | animators.add(anim); |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 414 | } else { |
| 415 | dockAreaOverlay.setBounds(bounds); |
| 416 | } |
| 417 | } |
| 418 | if (!animators.isEmpty()) { |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 419 | mDockAreaOverlayAnimator = new AnimatorSet(); |
| 420 | mDockAreaOverlayAnimator.playTogether(animators); |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 421 | mDockAreaOverlayAnimator.start(); |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 426 | public final int dockSide; |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 427 | public final int createMode; |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 428 | public final ViewState viewState; |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 429 | private final RectF touchArea; |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 430 | private final RectF dockArea; |
| 431 | private final RectF expandedTouchDockArea; |
Winson | 08deff0 | 2016-08-05 13:58:31 -0700 | [diff] [blame] | 432 | private static final Rect mTmpRect = new Rect(); |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 433 | |
| 434 | /** |
| 435 | * @param createMode used to pass to ActivityManager to dock the task |
| 436 | * @param touchArea the area in which touch will initiate this dock state |
Winson | eca4ab6 | 2015-11-04 10:50:28 -0800 | [diff] [blame] | 437 | * @param dockArea the visible dock area |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 438 | * @param expandedTouchDockArea the areain which touch will continue to dock after entering |
| 439 | * the initial touch area. This is also the new dock area to |
| 440 | * draw. |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 441 | */ |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 442 | DockState(int dockSide, int createMode, int dockAreaAlpha, int hintTextAlpha, |
| 443 | @TextOrientation int hintTextOrientation, RectF touchArea, RectF dockArea, |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 444 | RectF expandedTouchDockArea) { |
| 445 | this.dockSide = dockSide; |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 446 | this.createMode = createMode; |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 447 | this.viewState = new ViewState(dockAreaAlpha, hintTextAlpha, hintTextOrientation, |
| 448 | R.string.recents_drag_hint_message); |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 449 | this.dockArea = dockArea; |
Winson | 882072b | 2015-10-12 11:26:33 -0700 | [diff] [blame] | 450 | this.touchArea = touchArea; |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 451 | this.expandedTouchDockArea = expandedTouchDockArea; |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /** |
Winson | 99ef458 | 2016-04-18 16:57:27 -0700 | [diff] [blame] | 455 | * Updates the dock state with the given context. |
| 456 | */ |
| 457 | public void update(Context context) { |
| 458 | viewState.update(context); |
| 459 | } |
| 460 | |
| 461 | /** |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 462 | * Returns the docked task bounds with the given {@param width} and {@param height}. |
| 463 | */ |
Winson | 08deff0 | 2016-08-05 13:58:31 -0700 | [diff] [blame] | 464 | public Rect getPreDockedBounds(int width, int height, Rect insets) { |
| 465 | getMappedRect(dockArea, width, height, mTmpRect); |
| 466 | return updateBoundsWithSystemInsets(mTmpRect, insets); |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 467 | } |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 468 | |
| 469 | /** |
| 470 | * Returns the expanded docked task bounds with the given {@param width} and |
| 471 | * {@param height}. |
| 472 | */ |
| 473 | public Rect getDockedBounds(int width, int height, int dividerSize, Rect insets, |
| 474 | Resources res) { |
| 475 | // Calculate the docked task bounds |
| 476 | boolean isHorizontalDivision = |
| 477 | res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT; |
| 478 | int position = DockedDividerUtils.calculateMiddlePosition(isHorizontalDivision, |
| 479 | insets, width, height, dividerSize); |
| 480 | Rect newWindowBounds = new Rect(); |
| 481 | DockedDividerUtils.calculateBoundsForPosition(position, dockSide, newWindowBounds, |
| 482 | width, height, dividerSize); |
| 483 | return newWindowBounds; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Returns the task stack bounds with the given {@param width} and |
| 488 | * {@param height}. |
| 489 | */ |
Winson | fc48b07 | 2016-04-21 11:20:11 -0700 | [diff] [blame] | 490 | public Rect getDockedTaskStackBounds(Rect displayRect, int width, int height, |
| 491 | int dividerSize, Rect insets, TaskStackLayoutAlgorithm layoutAlgorithm, |
| 492 | Resources res, Rect windowRectOut) { |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 493 | // Calculate the inverse docked task bounds |
| 494 | boolean isHorizontalDivision = |
| 495 | res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT; |
| 496 | int position = DockedDividerUtils.calculateMiddlePosition(isHorizontalDivision, |
| 497 | insets, width, height, dividerSize); |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 498 | DockedDividerUtils.calculateBoundsForPosition(position, |
Winson | 59924fe | 2016-03-17 14:13:18 -0700 | [diff] [blame] | 499 | DockedDividerUtils.invertDockSide(dockSide), windowRectOut, width, height, |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 500 | dividerSize); |
| 501 | |
| 502 | // Calculate the task stack bounds from the new window bounds |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 503 | Rect taskStackBounds = new Rect(); |
Winson | d952961 | 2016-01-28 13:29:49 -0800 | [diff] [blame] | 504 | // If the task stack bounds is specifically under the dock area, then ignore the top |
| 505 | // inset |
| 506 | int top = dockArea.bottom < 1f |
| 507 | ? 0 |
| 508 | : insets.top; |
Winson | 08deff0 | 2016-08-05 13:58:31 -0700 | [diff] [blame] | 509 | // For now, ignore the left insets since we always dock on the left and show Recents |
| 510 | // on the right |
| 511 | layoutAlgorithm.getTaskStackBounds(displayRect, windowRectOut, top, 0, insets.right, |
| 512 | taskStackBounds); |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 513 | return taskStackBounds; |
| 514 | } |
Winson | 08deff0 | 2016-08-05 13:58:31 -0700 | [diff] [blame] | 515 | |
| 516 | /** |
| 517 | * Returns the expanded bounds in certain dock sides such that the bounds account for the |
| 518 | * system insets (namely the vertical nav bar). This call modifies and returns the given |
| 519 | * {@param bounds}. |
| 520 | */ |
| 521 | private Rect updateBoundsWithSystemInsets(Rect bounds, Rect insets) { |
| 522 | if (dockSide == DOCKED_LEFT) { |
| 523 | bounds.right += insets.left; |
| 524 | } else if (dockSide == DOCKED_RIGHT) { |
| 525 | bounds.left -= insets.right; |
| 526 | } |
| 527 | return bounds; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Returns the mapped rect to the given dimensions. |
| 532 | */ |
| 533 | private void getMappedRect(RectF bounds, int width, int height, Rect out) { |
| 534 | out.set((int) (bounds.left * width), (int) (bounds.top * height), |
| 535 | (int) (bounds.right * width), (int) (bounds.bottom * height)); |
| 536 | } |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Winson | 6976f7b | 2016-05-03 14:58:12 -0700 | [diff] [blame] | 539 | // A comparator that sorts tasks by their freeform state |
| 540 | private Comparator<Task> FREEFORM_COMPARATOR = new Comparator<Task>() { |
Winson Chung | 509d0d0 | 2015-12-16 15:43:12 -0500 | [diff] [blame] | 541 | @Override |
| 542 | public int compare(Task o1, Task o2) { |
| 543 | if (o1.isFreeformTask() && !o2.isFreeformTask()) { |
| 544 | return 1; |
| 545 | } else if (o2.isFreeformTask() && !o1.isFreeformTask()) { |
| 546 | return -1; |
| 547 | } |
Winson | 6976f7b | 2016-05-03 14:58:12 -0700 | [diff] [blame] | 548 | return Long.compare(o1.temporarySortIndexInStack, o2.temporarySortIndexInStack); |
Winson Chung | 509d0d0 | 2015-12-16 15:43:12 -0500 | [diff] [blame] | 549 | } |
| 550 | }; |
| 551 | |
| 552 | |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 553 | // The task offset to apply to a task id as a group affiliation |
| 554 | static final int IndividualTaskIdOffset = 1 << 16; |
| 555 | |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 556 | ArrayList<Task> mRawTaskList = new ArrayList<>(); |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 557 | FilteredTaskList mStackTaskList = new FilteredTaskList(); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 558 | TaskStackCallbacks mCb; |
| 559 | |
Winson | 8f0e3a6 | 2015-11-23 09:15:08 -0800 | [diff] [blame] | 560 | ArrayList<TaskGrouping> mGroups = new ArrayList<>(); |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 561 | ArrayMap<Integer, TaskGrouping> mAffinitiesGroups = new ArrayMap<>(); |
Winson | 8f0e3a6 | 2015-11-23 09:15:08 -0800 | [diff] [blame] | 562 | |
| 563 | public TaskStack() { |
| 564 | // Ensure that we only show non-docked tasks |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 565 | mStackTaskList.setFilter(new TaskFilter() { |
Winson | 8f0e3a6 | 2015-11-23 09:15:08 -0800 | [diff] [blame] | 566 | @Override |
Winson Chung | 2b9ef65 | 2015-12-11 10:23:59 -0500 | [diff] [blame] | 567 | public boolean acceptTask(SparseArray<Task> taskIdMap, Task t, int index) { |
Winson | d8f7431 | 2016-03-23 20:39:24 -0700 | [diff] [blame] | 568 | if (RecentsDebugFlags.Static.EnableAffiliatedTaskGroups) { |
| 569 | if (t.isAffiliatedTask()) { |
| 570 | // If this task is affiliated with another parent in the stack, then the |
| 571 | // historical state of this task depends on the state of the parent task |
| 572 | Task parentTask = taskIdMap.get(t.affiliationTaskId); |
| 573 | if (parentTask != null) { |
| 574 | t = parentTask; |
| 575 | } |
Winson Chung | 2b9ef65 | 2015-12-11 10:23:59 -0500 | [diff] [blame] | 576 | } |
| 577 | } |
Winson | 8f6ee48 | 2016-03-18 17:51:48 -0700 | [diff] [blame] | 578 | return t.isStackTask; |
Winson | 8f0e3a6 | 2015-11-23 09:15:08 -0800 | [diff] [blame] | 579 | } |
| 580 | }); |
| 581 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 582 | |
Winson Chung | d16c565 | 2015-01-26 16:11:07 -0800 | [diff] [blame] | 583 | /** Sets the callbacks for this task stack. */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 584 | public void setCallbacks(TaskStackCallbacks cb) { |
| 585 | mCb = cb; |
| 586 | } |
| 587 | |
Winson | eca4ab6 | 2015-11-04 10:50:28 -0800 | [diff] [blame] | 588 | /** |
| 589 | * Moves the given task to either the front of the freeform workspace or the stack. |
| 590 | */ |
| 591 | public void moveTaskToStack(Task task, int newStackId) { |
| 592 | // Find the index to insert into |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 593 | ArrayList<Task> taskList = mStackTaskList.getTasks(); |
Winson | eca4ab6 | 2015-11-04 10:50:28 -0800 | [diff] [blame] | 594 | int taskCount = taskList.size(); |
| 595 | if (!task.isFreeformTask() && (newStackId == FREEFORM_WORKSPACE_STACK_ID)) { |
| 596 | // Insert freeform tasks at the front |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 597 | mStackTaskList.moveTaskToStack(task, taskCount, newStackId); |
Winson | eca4ab6 | 2015-11-04 10:50:28 -0800 | [diff] [blame] | 598 | } else if (task.isFreeformTask() && (newStackId == FULLSCREEN_WORKSPACE_STACK_ID)) { |
| 599 | // Insert after the first stacked task |
| 600 | int insertIndex = 0; |
| 601 | for (int i = taskCount - 1; i >= 0; i--) { |
| 602 | if (!taskList.get(i).isFreeformTask()) { |
| 603 | insertIndex = i + 1; |
| 604 | break; |
| 605 | } |
| 606 | } |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 607 | mStackTaskList.moveTaskToStack(task, insertIndex, newStackId); |
Winson | eca4ab6 | 2015-11-04 10:50:28 -0800 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Winson Chung | 6ac8bd6 | 2015-01-07 16:38:35 -0800 | [diff] [blame] | 611 | /** Does the actual work associated with removing the task. */ |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 612 | void removeTaskImpl(FilteredTaskList taskList, Task t) { |
Winson Chung | 6ac8bd6 | 2015-01-07 16:38:35 -0800 | [diff] [blame] | 613 | // Remove the task from the list |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 614 | taskList.remove(t); |
Winson Chung | 6ac8bd6 | 2015-01-07 16:38:35 -0800 | [diff] [blame] | 615 | // Remove it from the group as well, and if it is empty, remove the group |
| 616 | TaskGrouping group = t.group; |
Winson Chung | 4e5fb2f | 2015-12-15 14:46:23 -0500 | [diff] [blame] | 617 | if (group != null) { |
| 618 | group.removeTask(t); |
| 619 | if (group.getTaskCount() == 0) { |
| 620 | removeGroup(group); |
| 621 | } |
Winson Chung | 6ac8bd6 | 2015-01-07 16:38:35 -0800 | [diff] [blame] | 622 | } |
Winson Chung | 6ac8bd6 | 2015-01-07 16:38:35 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Winson | 8aa9959 | 2016-01-19 15:07:07 -0800 | [diff] [blame] | 625 | /** |
| 626 | * Removes a task from the stack, with an additional {@param animation} hint to the callbacks on |
| 627 | * how they should update themselves. |
| 628 | */ |
Winson | 2068408 | 2016-03-16 17:13:34 -0700 | [diff] [blame] | 629 | public void removeTask(Task t, AnimationProps animation, boolean fromDockGesture) { |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 630 | if (mStackTaskList.contains(t)) { |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 631 | removeTaskImpl(mStackTaskList, t); |
Winson | 35a8b04 | 2016-01-22 09:41:09 -0800 | [diff] [blame] | 632 | Task newFrontMostTask = getStackFrontMostTask(false /* includeFreeform */); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 633 | if (mCb != null) { |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 634 | // Notify that a task has been removed |
Winson | 6c8217a | 2016-05-25 10:53:53 -0700 | [diff] [blame] | 635 | mCb.onStackTaskRemoved(this, t, newFrontMostTask, animation, |
Winson | 2068408 | 2016-03-16 17:13:34 -0700 | [diff] [blame] | 636 | fromDockGesture); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 637 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 638 | } |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 639 | mRawTaskList.remove(t); |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /** |
Winson | 3b6ba1a | 2016-03-22 15:37:54 -0700 | [diff] [blame] | 643 | * Removes all tasks from the stack. |
| 644 | */ |
| 645 | public void removeAllTasks() { |
| 646 | ArrayList<Task> tasks = mStackTaskList.getTasks(); |
| 647 | for (int i = tasks.size() - 1; i >= 0; i--) { |
| 648 | Task t = tasks.get(i); |
| 649 | removeTaskImpl(mStackTaskList, t); |
| 650 | mRawTaskList.remove(t); |
| 651 | } |
| 652 | if (mCb != null) { |
| 653 | // Notify that all tasks have been removed |
| 654 | mCb.onStackTasksRemoved(this); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | /** |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 659 | * Sets a few tasks in one go, without calling any callbacks. |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 660 | * |
| 661 | * @param tasks the new set of tasks to replace the current set. |
| 662 | * @param notifyStackChanges whether or not to callback on specific changes to the list of tasks. |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 663 | */ |
Winson | 8873754 | 2016-02-17 13:27:33 -0800 | [diff] [blame] | 664 | public void setTasks(Context context, List<Task> tasks, boolean notifyStackChanges) { |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 665 | // Compute a has set for each of the tasks |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 666 | ArrayMap<Task.TaskKey, Task> currentTasksMap = createTaskKeyMapFromList(mRawTaskList); |
| 667 | ArrayMap<Task.TaskKey, Task> newTasksMap = createTaskKeyMapFromList(tasks); |
Winson | 8873754 | 2016-02-17 13:27:33 -0800 | [diff] [blame] | 668 | ArrayList<Task> addedTasks = new ArrayList<>(); |
Winson | 6c8217a | 2016-05-25 10:53:53 -0700 | [diff] [blame] | 669 | ArrayList<Task> removedTasks = new ArrayList<>(); |
Winson | 8873754 | 2016-02-17 13:27:33 -0800 | [diff] [blame] | 670 | ArrayList<Task> allTasks = new ArrayList<>(); |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 671 | |
| 672 | // Disable notifications if there are no callbacks |
| 673 | if (mCb == null) { |
| 674 | notifyStackChanges = false; |
| 675 | } |
| 676 | |
| 677 | // Remove any tasks that no longer exist |
| 678 | int taskCount = mRawTaskList.size(); |
Winson | 8873754 | 2016-02-17 13:27:33 -0800 | [diff] [blame] | 679 | for (int i = taskCount - 1; i >= 0; i--) { |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 680 | Task task = mRawTaskList.get(i); |
| 681 | if (!newTasksMap.containsKey(task.key)) { |
| 682 | if (notifyStackChanges) { |
Winson | 6c8217a | 2016-05-25 10:53:53 -0700 | [diff] [blame] | 683 | removedTasks.add(task); |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 684 | } |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 685 | } |
Winson Chung | 9756755 | 2015-12-16 17:07:19 -0500 | [diff] [blame] | 686 | task.setGroup(null); |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | // Add any new tasks |
| 690 | taskCount = tasks.size(); |
| 691 | for (int i = 0; i < taskCount; i++) { |
Winson | 8873754 | 2016-02-17 13:27:33 -0800 | [diff] [blame] | 692 | Task newTask = tasks.get(i); |
| 693 | Task currentTask = currentTasksMap.get(newTask.key); |
| 694 | if (currentTask == null && notifyStackChanges) { |
| 695 | addedTasks.add(newTask); |
| 696 | } else if (currentTask != null) { |
| 697 | // The current task has bound callbacks, so just copy the data from the new task |
| 698 | // state and add it back into the list |
| 699 | currentTask.copyFrom(newTask); |
| 700 | newTask = currentTask; |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 701 | } |
Winson | 8873754 | 2016-02-17 13:27:33 -0800 | [diff] [blame] | 702 | allTasks.add(newTask); |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | // Sort all the tasks to ensure they are ordered correctly |
Winson | 6976f7b | 2016-05-03 14:58:12 -0700 | [diff] [blame] | 706 | for (int i = allTasks.size() - 1; i >= 0; i--) { |
| 707 | allTasks.get(i).temporarySortIndexInStack = i; |
| 708 | } |
| 709 | Collections.sort(allTasks, FREEFORM_COMPARATOR); |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 710 | |
Winson | 8f6ee48 | 2016-03-18 17:51:48 -0700 | [diff] [blame] | 711 | mStackTaskList.set(allTasks); |
Winson | 8873754 | 2016-02-17 13:27:33 -0800 | [diff] [blame] | 712 | mRawTaskList = allTasks; |
| 713 | |
Winson | a1ededd | 2016-03-25 12:23:12 -0700 | [diff] [blame] | 714 | // Update the affiliated groupings |
| 715 | createAffiliatedGroupings(context); |
| 716 | |
Winson | 6c8217a | 2016-05-25 10:53:53 -0700 | [diff] [blame] | 717 | // Only callback for the removed tasks after the stack has updated |
| 718 | int removedTaskCount = removedTasks.size(); |
| 719 | Task newFrontMostTask = getStackFrontMostTask(false); |
| 720 | for (int i = 0; i < removedTaskCount; i++) { |
| 721 | mCb.onStackTaskRemoved(this, removedTasks.get(i), newFrontMostTask, |
| 722 | AnimationProps.IMMEDIATE, false /* fromDockGesture */); |
| 723 | } |
| 724 | |
Winson | 8873754 | 2016-02-17 13:27:33 -0800 | [diff] [blame] | 725 | // Only callback for the newly added tasks after this stack has been updated |
| 726 | int addedTaskCount = addedTasks.size(); |
| 727 | for (int i = 0; i < addedTaskCount; i++) { |
| 728 | mCb.onStackTaskAdded(this, addedTasks.get(i)); |
| 729 | } |
| 730 | |
Winson | a1ededd | 2016-03-25 12:23:12 -0700 | [diff] [blame] | 731 | // Notify that the task stack has been updated |
| 732 | if (notifyStackChanges) { |
| 733 | mCb.onStackTasksUpdated(this); |
| 734 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 735 | } |
| 736 | |
Winson Chung | 931c51f | 2015-12-17 17:08:55 -0500 | [diff] [blame] | 737 | /** |
| 738 | * Gets the front-most task in the stack. |
| 739 | */ |
Winson | 35a8b04 | 2016-01-22 09:41:09 -0800 | [diff] [blame] | 740 | public Task getStackFrontMostTask(boolean includeFreeformTasks) { |
Winson Chung | 931c51f | 2015-12-17 17:08:55 -0500 | [diff] [blame] | 741 | ArrayList<Task> stackTasks = mStackTaskList.getTasks(); |
| 742 | if (stackTasks.isEmpty()) { |
| 743 | return null; |
| 744 | } |
| 745 | for (int i = stackTasks.size() - 1; i >= 0; i--) { |
| 746 | Task task = stackTasks.get(i); |
Winson | 35a8b04 | 2016-01-22 09:41:09 -0800 | [diff] [blame] | 747 | if (!task.isFreeformTask() || includeFreeformTasks) { |
Winson Chung | 931c51f | 2015-12-17 17:08:55 -0500 | [diff] [blame] | 748 | return task; |
| 749 | } |
| 750 | } |
| 751 | return null; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Winson Chung | 0440067 | 2014-10-17 14:53:30 -0700 | [diff] [blame] | 754 | /** Gets the task keys */ |
| 755 | public ArrayList<Task.TaskKey> getTaskKeys() { |
Winson | 8f0e3a6 | 2015-11-23 09:15:08 -0800 | [diff] [blame] | 756 | ArrayList<Task.TaskKey> taskKeys = new ArrayList<>(); |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 757 | ArrayList<Task> tasks = computeAllTasksList(); |
Winson Chung | 0440067 | 2014-10-17 14:53:30 -0700 | [diff] [blame] | 758 | int taskCount = tasks.size(); |
| 759 | for (int i = 0; i < taskCount; i++) { |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 760 | Task task = tasks.get(i); |
| 761 | taskKeys.add(task.key); |
Winson Chung | 0440067 | 2014-10-17 14:53:30 -0700 | [diff] [blame] | 762 | } |
| 763 | return taskKeys; |
| 764 | } |
| 765 | |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 766 | /** |
| 767 | * Returns the set of "active" (non-historical) tasks in the stack that have been used recently. |
| 768 | */ |
| 769 | public ArrayList<Task> getStackTasks() { |
| 770 | return mStackTaskList.getTasks(); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 771 | } |
| 772 | |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 773 | /** |
Winson | f24f216 | 2016-01-05 12:11:55 -0800 | [diff] [blame] | 774 | * Returns the set of "freeform" tasks in the stack. |
| 775 | */ |
| 776 | public ArrayList<Task> getFreeformTasks() { |
| 777 | ArrayList<Task> freeformTasks = new ArrayList<>(); |
| 778 | ArrayList<Task> tasks = mStackTaskList.getTasks(); |
| 779 | int taskCount = tasks.size(); |
| 780 | for (int i = 0; i < taskCount; i++) { |
| 781 | Task task = tasks.get(i); |
| 782 | if (task.isFreeformTask()) { |
| 783 | freeformTasks.add(task); |
| 784 | } |
| 785 | } |
| 786 | return freeformTasks; |
| 787 | } |
| 788 | |
| 789 | /** |
Winson | 6976f7b | 2016-05-03 14:58:12 -0700 | [diff] [blame] | 790 | * Computes a set of all the active and historical tasks. |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 791 | */ |
| 792 | public ArrayList<Task> computeAllTasksList() { |
| 793 | ArrayList<Task> tasks = new ArrayList<>(); |
| 794 | tasks.addAll(mStackTaskList.getTasks()); |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 795 | return tasks; |
| 796 | } |
| 797 | |
| 798 | /** |
Winson | 4b057c6 | 2016-01-12 15:01:52 -0800 | [diff] [blame] | 799 | * Returns the number of stack and freeform tasks. |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 800 | */ |
Winson | 4b057c6 | 2016-01-12 15:01:52 -0800 | [diff] [blame] | 801 | public int getTaskCount() { |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 802 | return mStackTaskList.size(); |
| 803 | } |
| 804 | |
| 805 | /** |
Winson | 4b057c6 | 2016-01-12 15:01:52 -0800 | [diff] [blame] | 806 | * Returns the number of stack tasks. |
Winson | f0d1c44 | 2015-12-01 11:04:45 -0800 | [diff] [blame] | 807 | */ |
Winson | 4b057c6 | 2016-01-12 15:01:52 -0800 | [diff] [blame] | 808 | public int getStackTaskCount() { |
| 809 | ArrayList<Task> tasks = mStackTaskList.getTasks(); |
| 810 | int stackCount = 0; |
| 811 | int taskCount = tasks.size(); |
| 812 | for (int i = 0; i < taskCount; i++) { |
| 813 | Task task = tasks.get(i); |
| 814 | if (!task.isFreeformTask()) { |
| 815 | stackCount++; |
| 816 | } |
| 817 | } |
| 818 | return stackCount; |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * Returns the number of freeform tasks. |
| 823 | */ |
| 824 | public int getFreeformTaskCount() { |
Winson | f0d1c44 | 2015-12-01 11:04:45 -0800 | [diff] [blame] | 825 | ArrayList<Task> tasks = mStackTaskList.getTasks(); |
| 826 | int freeformCount = 0; |
| 827 | int taskCount = tasks.size(); |
| 828 | for (int i = 0; i < taskCount; i++) { |
| 829 | Task task = tasks.get(i); |
| 830 | if (task.isFreeformTask()) { |
| 831 | freeformCount++; |
| 832 | } |
| 833 | } |
| 834 | return freeformCount; |
| 835 | } |
| 836 | |
| 837 | /** |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 838 | * Returns the task in stack tasks which is the launch target. |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 839 | */ |
| 840 | public Task getLaunchTarget() { |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 841 | ArrayList<Task> tasks = mStackTaskList.getTasks(); |
Winson | 36a5a2c | 2015-10-29 18:04:39 -0700 | [diff] [blame] | 842 | int taskCount = tasks.size(); |
| 843 | for (int i = 0; i < taskCount; i++) { |
| 844 | Task task = tasks.get(i); |
| 845 | if (task.isLaunchTarget) { |
| 846 | return task; |
| 847 | } |
| 848 | } |
| 849 | return null; |
| 850 | } |
| 851 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 852 | /** Returns the index of this task in this current task stack */ |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 853 | public int indexOfStackTask(Task t) { |
| 854 | return mStackTaskList.indexOf(t); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 855 | } |
| 856 | |
Winson Chung | b1f7499 | 2014-08-08 12:53:09 -0700 | [diff] [blame] | 857 | /** Finds the task with the specified task id. */ |
| 858 | public Task findTaskWithId(int taskId) { |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 859 | ArrayList<Task> tasks = computeAllTasksList(); |
Winson | d72c315 | 2016-04-05 15:33:35 -0700 | [diff] [blame] | 860 | int taskCount = tasks.size(); |
| 861 | for (int i = 0; i < taskCount; i++) { |
| 862 | Task task = tasks.get(i); |
Winson Chung | b1f7499 | 2014-08-08 12:53:09 -0700 | [diff] [blame] | 863 | if (task.key.id == taskId) { |
| 864 | return task; |
| 865 | } |
| 866 | } |
| 867 | return null; |
| 868 | } |
| 869 | |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 870 | /******** Grouping ********/ |
| 871 | |
| 872 | /** Adds a group to the set */ |
| 873 | public void addGroup(TaskGrouping group) { |
| 874 | mGroups.add(group); |
| 875 | mAffinitiesGroups.put(group.affiliation, group); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | public void removeGroup(TaskGrouping group) { |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 879 | mGroups.remove(group); |
| 880 | mAffinitiesGroups.remove(group.affiliation); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /** Returns the group with the specified affiliation. */ |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 884 | public TaskGrouping getGroupWithAffiliation(int affiliation) { |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 885 | return mAffinitiesGroups.get(affiliation); |
| 886 | } |
| 887 | |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 888 | /** |
Winson | 8873754 | 2016-02-17 13:27:33 -0800 | [diff] [blame] | 889 | * Temporary: This method will simulate affiliation groups |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 890 | */ |
Winson | 8873754 | 2016-02-17 13:27:33 -0800 | [diff] [blame] | 891 | void createAffiliatedGroupings(Context context) { |
| 892 | mGroups.clear(); |
| 893 | mAffinitiesGroups.clear(); |
| 894 | |
Winson | 6e6bd877 | 2016-01-25 10:41:40 -0800 | [diff] [blame] | 895 | if (RecentsDebugFlags.Static.EnableMockTaskGroups) { |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 896 | ArrayMap<Task.TaskKey, Task> taskMap = new ArrayMap<>(); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 897 | // Sort all tasks by increasing firstActiveTime of the task |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 898 | ArrayList<Task> tasks = mStackTaskList.getTasks(); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 899 | Collections.sort(tasks, new Comparator<Task>() { |
| 900 | @Override |
| 901 | public int compare(Task task, Task task2) { |
Winson Chung | 16bc2a7 | 2015-12-15 10:27:04 -0500 | [diff] [blame] | 902 | return Long.compare(task.key.firstActiveTime, task2.key.firstActiveTime); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 903 | } |
| 904 | }); |
| 905 | // Create groups when sequential packages are the same |
| 906 | NamedCounter counter = new NamedCounter("task-group", ""); |
| 907 | int taskCount = tasks.size(); |
| 908 | String prevPackage = ""; |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 909 | int prevAffiliation = -1; |
Winson Chung | a433fa9 | 2014-07-08 21:50:31 -0700 | [diff] [blame] | 910 | Random r = new Random(); |
Winson | 6e6bd877 | 2016-01-25 10:41:40 -0800 | [diff] [blame] | 911 | int groupCountDown = RecentsDebugFlags.Static.MockTaskGroupsTaskCount; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 912 | for (int i = 0; i < taskCount; i++) { |
| 913 | Task t = tasks.get(i); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 914 | String packageName = t.key.getComponent().getPackageName(); |
Winson Chung | a433fa9 | 2014-07-08 21:50:31 -0700 | [diff] [blame] | 915 | packageName = "pkg"; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 916 | TaskGrouping group; |
Winson Chung | a433fa9 | 2014-07-08 21:50:31 -0700 | [diff] [blame] | 917 | if (packageName.equals(prevPackage) && groupCountDown > 0) { |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 918 | group = getGroupWithAffiliation(prevAffiliation); |
Winson Chung | a433fa9 | 2014-07-08 21:50:31 -0700 | [diff] [blame] | 919 | groupCountDown--; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 920 | } else { |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 921 | int affiliation = IndividualTaskIdOffset + t.key.id; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 922 | group = new TaskGrouping(affiliation); |
| 923 | addGroup(group); |
| 924 | prevAffiliation = affiliation; |
| 925 | prevPackage = packageName; |
Winson | 6e6bd877 | 2016-01-25 10:41:40 -0800 | [diff] [blame] | 926 | groupCountDown = RecentsDebugFlags.Static.MockTaskGroupsTaskCount; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 927 | } |
| 928 | group.addTask(t); |
| 929 | taskMap.put(t.key, t); |
| 930 | } |
| 931 | // Sort groups by increasing latestActiveTime of the group |
| 932 | Collections.sort(mGroups, new Comparator<TaskGrouping>() { |
| 933 | @Override |
| 934 | public int compare(TaskGrouping taskGrouping, TaskGrouping taskGrouping2) { |
Winson Chung | 509d0d0 | 2015-12-16 15:43:12 -0500 | [diff] [blame] | 935 | return Long.compare(taskGrouping.latestActiveTimeInGroup, |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 936 | taskGrouping2.latestActiveTimeInGroup); |
| 937 | } |
| 938 | }); |
Winson Chung | 2b9ef65 | 2015-12-11 10:23:59 -0500 | [diff] [blame] | 939 | // Sort group tasks by increasing firstActiveTime of the task, and also build a new list |
| 940 | // of tasks |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 941 | int taskIndex = 0; |
| 942 | int groupCount = mGroups.size(); |
| 943 | for (int i = 0; i < groupCount; i++) { |
| 944 | TaskGrouping group = mGroups.get(i); |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 945 | Collections.sort(group.mTaskKeys, new Comparator<Task.TaskKey>() { |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 946 | @Override |
| 947 | public int compare(Task.TaskKey taskKey, Task.TaskKey taskKey2) { |
Winson Chung | 509d0d0 | 2015-12-16 15:43:12 -0500 | [diff] [blame] | 948 | return Long.compare(taskKey.firstActiveTime, taskKey2.firstActiveTime); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 949 | } |
| 950 | }); |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 951 | ArrayList<Task.TaskKey> groupTasks = group.mTaskKeys; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 952 | int groupTaskCount = groupTasks.size(); |
| 953 | for (int j = 0; j < groupTaskCount; j++) { |
| 954 | tasks.set(taskIndex, taskMap.get(groupTasks.get(j))); |
| 955 | taskIndex++; |
| 956 | } |
| 957 | } |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 958 | mStackTaskList.set(tasks); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 959 | } else { |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 960 | // Create the task groups |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 961 | ArrayMap<Task.TaskKey, Task> tasksMap = new ArrayMap<>(); |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 962 | ArrayList<Task> tasks = mStackTaskList.getTasks(); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 963 | int taskCount = tasks.size(); |
| 964 | for (int i = 0; i < taskCount; i++) { |
| 965 | Task t = tasks.get(i); |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 966 | TaskGrouping group; |
Winson | 65c851e | 2016-01-20 12:43:35 -0800 | [diff] [blame] | 967 | if (RecentsDebugFlags.Static.EnableAffiliatedTaskGroups) { |
| 968 | int affiliation = t.affiliationTaskId > 0 ? t.affiliationTaskId : |
| 969 | IndividualTaskIdOffset + t.key.id; |
| 970 | if (mAffinitiesGroups.containsKey(affiliation)) { |
| 971 | group = getGroupWithAffiliation(affiliation); |
| 972 | } else { |
| 973 | group = new TaskGrouping(affiliation); |
| 974 | addGroup(group); |
| 975 | } |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 976 | } else { |
Winson | 65c851e | 2016-01-20 12:43:35 -0800 | [diff] [blame] | 977 | group = new TaskGrouping(t.key.id); |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 978 | addGroup(group); |
| 979 | } |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 980 | group.addTask(t); |
Winson Chung | ec396d6 | 2014-08-06 17:08:00 -0700 | [diff] [blame] | 981 | tasksMap.put(t.key, t); |
| 982 | } |
| 983 | // Update the task colors for each of the groups |
Winson | 35f3050 | 2015-09-28 11:24:36 -0700 | [diff] [blame] | 984 | float minAlpha = context.getResources().getFloat( |
| 985 | R.dimen.recents_task_affiliation_color_min_alpha_percentage); |
Winson Chung | ec396d6 | 2014-08-06 17:08:00 -0700 | [diff] [blame] | 986 | int taskGroupCount = mGroups.size(); |
| 987 | for (int i = 0; i < taskGroupCount; i++) { |
| 988 | TaskGrouping group = mGroups.get(i); |
| 989 | taskCount = group.getTaskCount(); |
| 990 | // Ignore the groups that only have one task |
| 991 | if (taskCount <= 1) continue; |
| 992 | // Calculate the group color distribution |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 993 | int affiliationColor = tasksMap.get(group.mTaskKeys.get(0)).affiliationColor; |
Winson Chung | ec396d6 | 2014-08-06 17:08:00 -0700 | [diff] [blame] | 994 | float alphaStep = (1f - minAlpha) / taskCount; |
| 995 | float alpha = 1f; |
| 996 | for (int j = 0; j < taskCount; j++) { |
| 997 | Task t = tasksMap.get(group.mTaskKeys.get(j)); |
Winson Chung | a0e88b5 | 2014-08-11 19:25:42 -0700 | [diff] [blame] | 998 | t.colorPrimary = Utilities.getColorWithOverlay(affiliationColor, Color.WHITE, |
| 999 | alpha); |
Winson Chung | ec396d6 | 2014-08-06 17:08:00 -0700 | [diff] [blame] | 1000 | alpha -= alphaStep; |
| 1001 | } |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 1006 | /** |
| 1007 | * Computes the components of tasks in this stack that have been removed as a result of a change |
| 1008 | * in the specified package. |
| 1009 | */ |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 1010 | public ArraySet<ComponentName> computeComponentsRemoved(String packageName, int userId) { |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 1011 | // Identify all the tasks that should be removed as a result of the package being removed. |
| 1012 | // Using a set to ensure that we callback once per unique component. |
| 1013 | SystemServicesProxy ssp = Recents.getSystemServices(); |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 1014 | ArraySet<ComponentName> existingComponents = new ArraySet<>(); |
| 1015 | ArraySet<ComponentName> removedComponents = new ArraySet<>(); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 1016 | ArrayList<Task.TaskKey> taskKeys = getTaskKeys(); |
Winson | d72c315 | 2016-04-05 15:33:35 -0700 | [diff] [blame] | 1017 | int taskKeyCount = taskKeys.size(); |
| 1018 | for (int i = 0; i < taskKeyCount; i++) { |
| 1019 | Task.TaskKey t = taskKeys.get(i); |
| 1020 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 1021 | // Skip if this doesn't apply to the current user |
| 1022 | if (t.userId != userId) continue; |
| 1023 | |
| 1024 | ComponentName cn = t.getComponent(); |
| 1025 | if (cn.getPackageName().equals(packageName)) { |
| 1026 | if (existingComponents.contains(cn)) { |
| 1027 | // If we know that the component still exists in the package, then skip |
| 1028 | continue; |
| 1029 | } |
| 1030 | if (ssp.getActivityInfo(cn, userId) != null) { |
| 1031 | existingComponents.add(cn); |
| 1032 | } else { |
| 1033 | removedComponents.add(cn); |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | return removedComponents; |
| 1038 | } |
| 1039 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 1040 | @Override |
| 1041 | public String toString() { |
Winson | 8873754 | 2016-02-17 13:27:33 -0800 | [diff] [blame] | 1042 | String str = "Stack Tasks (" + mStackTaskList.size() + "):\n"; |
Winson | d72c315 | 2016-04-05 15:33:35 -0700 | [diff] [blame] | 1043 | ArrayList<Task> tasks = mStackTaskList.getTasks(); |
| 1044 | int taskCount = tasks.size(); |
| 1045 | for (int i = 0; i < taskCount; i++) { |
| 1046 | str += " " + tasks.get(i).toString() + "\n"; |
Winson | 250608a | 2015-11-24 15:00:31 -0800 | [diff] [blame] | 1047 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 1048 | return str; |
| 1049 | } |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 1050 | |
| 1051 | /** |
| 1052 | * Given a list of tasks, returns a map of each task's key to the task. |
| 1053 | */ |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 1054 | private ArrayMap<Task.TaskKey, Task> createTaskKeyMapFromList(List<Task> tasks) { |
| 1055 | ArrayMap<Task.TaskKey, Task> map = new ArrayMap<>(tasks.size()); |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 1056 | int taskCount = tasks.size(); |
| 1057 | for (int i = 0; i < taskCount; i++) { |
| 1058 | Task task = tasks.get(i); |
| 1059 | map.put(task.key, task); |
| 1060 | } |
| 1061 | return map; |
| 1062 | } |
Winson | d72c315 | 2016-04-05 15:33:35 -0700 | [diff] [blame] | 1063 | |
| 1064 | public void dump(String prefix, PrintWriter writer) { |
| 1065 | String innerPrefix = prefix + " "; |
| 1066 | |
| 1067 | writer.print(prefix); writer.print(TAG); |
| 1068 | writer.print(" numStackTasks="); writer.print(mStackTaskList.size()); |
| 1069 | writer.println(); |
| 1070 | ArrayList<Task> tasks = mStackTaskList.getTasks(); |
| 1071 | int taskCount = tasks.size(); |
| 1072 | for (int i = 0; i < taskCount; i++) { |
| 1073 | tasks.get(i).dump(innerPrefix, writer); |
| 1074 | } |
| 1075 | } |
Winson Chung | 0626677 | 2015-12-11 10:24:21 -0500 | [diff] [blame] | 1076 | } |