blob: 2109376d4ff33013c052ca02ca6e81a735d21ee3 [file] [log] [blame]
Winson Chungffa2ec62014-07-03 15:54:42 -07001package com.android.systemui.recents.model;
2
Winson55003902016-01-12 12:00:37 -08003import android.util.ArrayMap;
4
Winson Chungffa2ec62014-07-03 15:54:42 -07005import java.util.ArrayList;
6
7/** Represents a grouping of tasks witihin a stack. */
8public class TaskGrouping {
9
Winson Chung083baf92014-07-11 10:32:42 -070010 int affiliation;
Winson Chungffa2ec62014-07-03 15:54:42 -070011 long latestActiveTimeInGroup;
12
Winson Chung083baf92014-07-11 10:32:42 -070013 Task.TaskKey mFrontMostTaskKey;
14 ArrayList<Task.TaskKey> mTaskKeys = new ArrayList<Task.TaskKey>();
Winson55003902016-01-12 12:00:37 -080015 ArrayMap<Task.TaskKey, Integer> mTaskKeyIndices = new ArrayMap<>();
Winson Chungffa2ec62014-07-03 15:54:42 -070016
17 /** Creates a group with a specified affiliation. */
Winson Chung083baf92014-07-11 10:32:42 -070018 public TaskGrouping(int affiliation) {
Winson Chungffa2ec62014-07-03 15:54:42 -070019 this.affiliation = affiliation;
20 }
21
22 /** Adds a new task to this group. */
23 void addTask(Task t) {
Winson Chung083baf92014-07-11 10:32:42 -070024 mTaskKeys.add(t.key);
Winson Chungffa2ec62014-07-03 15:54:42 -070025 if (t.key.lastActiveTime > latestActiveTimeInGroup) {
26 latestActiveTimeInGroup = t.key.lastActiveTime;
27 }
28 t.setGroup(this);
Winson Chunga433fa92014-07-08 21:50:31 -070029 updateTaskIndices();
Winson Chungffa2ec62014-07-03 15:54:42 -070030 }
31
32 /** Removes a task from this group. */
33 void removeTask(Task t) {
Winson Chung083baf92014-07-11 10:32:42 -070034 mTaskKeys.remove(t.key);
Winson Chungffa2ec62014-07-03 15:54:42 -070035 latestActiveTimeInGroup = 0;
Winson Chung083baf92014-07-11 10:32:42 -070036 int taskCount = mTaskKeys.size();
Winson Chungffa2ec62014-07-03 15:54:42 -070037 for (int i = 0; i < taskCount; i++) {
Winson Chung083baf92014-07-11 10:32:42 -070038 long lastActiveTime = mTaskKeys.get(i).lastActiveTime;
Winson Chungffa2ec62014-07-03 15:54:42 -070039 if (lastActiveTime > latestActiveTimeInGroup) {
40 latestActiveTimeInGroup = lastActiveTime;
41 }
42 }
43 t.setGroup(null);
Winson Chunga433fa92014-07-08 21:50:31 -070044 updateTaskIndices();
Winson Chungffa2ec62014-07-03 15:54:42 -070045 }
46
Winson Chungb1f74992014-08-08 12:53:09 -070047 /** 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 Chungffa2ec62014-07-03 15:54:42 -070065 /** Gets the front task */
66 public boolean isFrontMostTask(Task t) {
Winson Chung083baf92014-07-11 10:32:42 -070067 return (t.key == mFrontMostTaskKey);
Winson Chungffa2ec62014-07-03 15:54:42 -070068 }
69
70 /** Finds the index of a given task in a group. */
71 public int indexOf(Task t) {
Winson Chung083baf92014-07-11 10:32:42 -070072 return mTaskKeyIndices.get(t.key);
Winson Chungffa2ec62014-07-03 15:54:42 -070073 }
74
Winson Chung1907cd42014-07-23 18:20:13 -070075 /** 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 Chungffa2ec62014-07-03 15:54:42 -070087 /** Returns the number of tasks in this group. */
Winson Chung083baf92014-07-11 10:32:42 -070088 public int getTaskCount() { return mTaskKeys.size(); }
Winson Chunga433fa92014-07-08 21:50:31 -070089
90 /** Updates the mapping of tasks to indices. */
91 private void updateTaskIndices() {
Winson Chung083baf92014-07-11 10:32:42 -070092 if (mTaskKeys.isEmpty()) {
93 mFrontMostTaskKey = null;
94 mTaskKeyIndices.clear();
95 return;
96 }
97
Winson55003902016-01-12 12:00:37 -080098 int taskCount = mTaskKeys.size();
Winson Chung083baf92014-07-11 10:32:42 -070099 mFrontMostTaskKey = mTaskKeys.get(mTaskKeys.size() - 1);
100 mTaskKeyIndices.clear();
Winson Chunga433fa92014-07-08 21:50:31 -0700101 for (int i = 0; i < taskCount; i++) {
Winson Chung083baf92014-07-11 10:32:42 -0700102 Task.TaskKey k = mTaskKeys.get(i);
103 mTaskKeyIndices.put(k, i);
Winson Chunga433fa92014-07-08 21:50:31 -0700104 }
105 }
Winson Chungffa2ec62014-07-03 15:54:42 -0700106}