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