1package com.android.systemui.recents.model;
2
3import android.util.ArrayMap;
4
5import java.util.ArrayList;
6
7/** Represents a grouping of tasks witihin a stack. */
8public class TaskGrouping {
9
10    int affiliation;
11    long latestActiveTimeInGroup;
12
13    Task.TaskKey mFrontMostTaskKey;
14    ArrayList<Task.TaskKey> mTaskKeys = new ArrayList<Task.TaskKey>();
15    ArrayMap<Task.TaskKey, Integer> mTaskKeyIndices = new ArrayMap<>();
16
17    /** Creates a group with a specified affiliation. */
18    public TaskGrouping(int affiliation) {
19        this.affiliation = affiliation;
20    }
21
22    /** Adds a new task to this group. */
23    void addTask(Task t) {
24        mTaskKeys.add(t.key);
25        if (t.key.lastActiveTime > latestActiveTimeInGroup) {
26            latestActiveTimeInGroup = t.key.lastActiveTime;
27        }
28        t.setGroup(this);
29        updateTaskIndices();
30    }
31
32    /** Removes a task from this group. */
33    void removeTask(Task t) {
34        mTaskKeys.remove(t.key);
35        latestActiveTimeInGroup = 0;
36        int taskCount = mTaskKeys.size();
37        for (int i = 0; i < taskCount; i++) {
38            long lastActiveTime = mTaskKeys.get(i).lastActiveTime;
39            if (lastActiveTime > latestActiveTimeInGroup) {
40                latestActiveTimeInGroup = lastActiveTime;
41            }
42        }
43        t.setGroup(null);
44        updateTaskIndices();
45    }
46
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
65    /** Gets the front task */
66    public boolean isFrontMostTask(Task t) {
67        return (t.key == mFrontMostTaskKey);
68    }
69
70    /** Finds the index of a given task in a group. */
71    public int indexOf(Task t) {
72        return mTaskKeyIndices.get(t.key);
73    }
74
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
87    /** Returns the number of tasks in this group. */
88    public int getTaskCount() { return mTaskKeys.size(); }
89
90    /** Updates the mapping of tasks to indices. */
91    private void updateTaskIndices() {
92        if (mTaskKeys.isEmpty()) {
93            mFrontMostTaskKey = null;
94            mTaskKeyIndices.clear();
95            return;
96        }
97
98        int taskCount = mTaskKeys.size();
99        mFrontMostTaskKey = mTaskKeys.get(mTaskKeys.size() - 1);
100        mTaskKeyIndices.clear();
101        for (int i = 0; i < taskCount; i++) {
102            Task.TaskKey k = mTaskKeys.get(i);
103            mTaskKeyIndices.put(k, i);
104        }
105    }
106}
107