Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 1 | package com.android.systemui.recents.model; |
| 2 | |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 3 | import android.util.ArrayMap; |
| 4 | |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 5 | import java.util.ArrayList; |
| 6 | |
| 7 | /** Represents a grouping of tasks witihin a stack. */ |
| 8 | public class TaskGrouping { |
| 9 | |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 10 | int affiliation; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 11 | long latestActiveTimeInGroup; |
| 12 | |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 13 | Task.TaskKey mFrontMostTaskKey; |
| 14 | ArrayList<Task.TaskKey> mTaskKeys = new ArrayList<Task.TaskKey>(); |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 15 | ArrayMap<Task.TaskKey, Integer> mTaskKeyIndices = new ArrayMap<>(); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 16 | |
| 17 | /** Creates a group with a specified affiliation. */ |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 18 | public TaskGrouping(int affiliation) { |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 19 | this.affiliation = affiliation; |
| 20 | } |
| 21 | |
| 22 | /** Adds a new task to this group. */ |
| 23 | void addTask(Task t) { |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 24 | mTaskKeys.add(t.key); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 25 | if (t.key.lastActiveTime > latestActiveTimeInGroup) { |
| 26 | latestActiveTimeInGroup = t.key.lastActiveTime; |
| 27 | } |
| 28 | t.setGroup(this); |
Winson Chung | a433fa9 | 2014-07-08 21:50:31 -0700 | [diff] [blame] | 29 | updateTaskIndices(); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /** Removes a task from this group. */ |
| 33 | void removeTask(Task t) { |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 34 | mTaskKeys.remove(t.key); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 35 | latestActiveTimeInGroup = 0; |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 36 | int taskCount = mTaskKeys.size(); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 37 | for (int i = 0; i < taskCount; i++) { |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 38 | long lastActiveTime = mTaskKeys.get(i).lastActiveTime; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 39 | if (lastActiveTime > latestActiveTimeInGroup) { |
| 40 | latestActiveTimeInGroup = lastActiveTime; |
| 41 | } |
| 42 | } |
| 43 | t.setGroup(null); |
Winson Chung | a433fa9 | 2014-07-08 21:50:31 -0700 | [diff] [blame] | 44 | updateTaskIndices(); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Winson Chung | b1f7499 | 2014-08-08 12:53:09 -0700 | [diff] [blame] | 47 | /** Returns the key of the next task in the group. */ |
| 48 | public Task.TaskKey getNextTaskInGroup(Task t) { |
| 49 | int i = indexOf(t); |
| 50 | if ((i + 1) < getTaskCount()) { |
| 51 | return mTaskKeys.get(i + 1); |
| 52 | } |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | /** Returns the key of the previous task in the group. */ |
| 57 | public Task.TaskKey getPrevTaskInGroup(Task t) { |
| 58 | int i = indexOf(t); |
| 59 | if ((i - 1) >= 0) { |
| 60 | return mTaskKeys.get(i - 1); |
| 61 | } |
| 62 | return null; |
| 63 | } |
| 64 | |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 65 | /** Gets the front task */ |
| 66 | public boolean isFrontMostTask(Task t) { |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 67 | return (t.key == mFrontMostTaskKey); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** Finds the index of a given task in a group. */ |
| 71 | public int indexOf(Task t) { |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 72 | return mTaskKeyIndices.get(t.key); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Winson Chung | 1907cd4 | 2014-07-23 18:20:13 -0700 | [diff] [blame] | 75 | /** Returns whether a task is in this grouping. */ |
| 76 | public boolean containsTask(Task t) { |
| 77 | return mTaskKeyIndices.containsKey(t.key); |
| 78 | } |
| 79 | |
| 80 | /** Returns whether one task is above another in the group. If they are not in the same group, |
| 81 | * this returns false. */ |
| 82 | public boolean isTaskAboveTask(Task t, Task below) { |
| 83 | return mTaskKeyIndices.containsKey(t.key) && mTaskKeyIndices.containsKey(below.key) && |
| 84 | mTaskKeyIndices.get(t.key) > mTaskKeyIndices.get(below.key); |
| 85 | } |
| 86 | |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 87 | /** Returns the number of tasks in this group. */ |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 88 | public int getTaskCount() { return mTaskKeys.size(); } |
Winson Chung | a433fa9 | 2014-07-08 21:50:31 -0700 | [diff] [blame] | 89 | |
| 90 | /** Updates the mapping of tasks to indices. */ |
| 91 | private void updateTaskIndices() { |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 92 | if (mTaskKeys.isEmpty()) { |
| 93 | mFrontMostTaskKey = null; |
| 94 | mTaskKeyIndices.clear(); |
| 95 | return; |
| 96 | } |
| 97 | |
Winson | 5500390 | 2016-01-12 12:00:37 -0800 | [diff] [blame] | 98 | int taskCount = mTaskKeys.size(); |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 99 | mFrontMostTaskKey = mTaskKeys.get(mTaskKeys.size() - 1); |
| 100 | mTaskKeyIndices.clear(); |
Winson Chung | a433fa9 | 2014-07-08 21:50:31 -0700 | [diff] [blame] | 101 | for (int i = 0; i < taskCount; i++) { |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 102 | Task.TaskKey k = mTaskKeys.get(i); |
| 103 | mTaskKeyIndices.put(k, i); |
Winson Chung | a433fa9 | 2014-07-08 21:50:31 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 106 | } |