TaskGrouping.java revision 1907cd478209f01a4215d9a3a76294c2c77c9a63
1package com.android.systemui.recents.model;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5
6/** Represents a grouping of tasks witihin a stack. */
7public class TaskGrouping {
8
9    int affiliation;
10    long latestActiveTimeInGroup;
11
12    Task.TaskKey mFrontMostTaskKey;
13    ArrayList<Task.TaskKey> mTaskKeys = new ArrayList<Task.TaskKey>();
14    HashMap<Task.TaskKey, Integer> mTaskKeyIndices = new HashMap<Task.TaskKey, Integer>();
15
16    /** Creates a group with a specified affiliation. */
17    public TaskGrouping(int affiliation) {
18        this.affiliation = affiliation;
19    }
20
21    /** Adds a new task to this group. */
22    void addTask(Task t) {
23        mTaskKeys.add(t.key);
24        if (t.key.lastActiveTime > latestActiveTimeInGroup) {
25            latestActiveTimeInGroup = t.key.lastActiveTime;
26        }
27        t.setGroup(this);
28        updateTaskIndices();
29    }
30
31    /** Removes a task from this group. */
32    void removeTask(Task t) {
33        mTaskKeys.remove(t.key);
34        latestActiveTimeInGroup = 0;
35        int taskCount = mTaskKeys.size();
36        for (int i = 0; i < taskCount; i++) {
37            long lastActiveTime = mTaskKeys.get(i).lastActiveTime;
38            if (lastActiveTime > latestActiveTimeInGroup) {
39                latestActiveTimeInGroup = lastActiveTime;
40            }
41        }
42        t.setGroup(null);
43        updateTaskIndices();
44    }
45
46    /** Gets the front task */
47    public boolean isFrontMostTask(Task t) {
48        return (t.key == mFrontMostTaskKey);
49    }
50
51    /** Finds the index of a given task in a group. */
52    public int indexOf(Task t) {
53        return mTaskKeyIndices.get(t.key);
54    }
55
56    /** Returns whether a task is in this grouping. */
57    public boolean containsTask(Task t) {
58        return mTaskKeyIndices.containsKey(t.key);
59    }
60
61    /** Returns whether one task is above another in the group.  If they are not in the same group,
62     * this returns false. */
63    public boolean isTaskAboveTask(Task t, Task below) {
64        return mTaskKeyIndices.containsKey(t.key) && mTaskKeyIndices.containsKey(below.key) &&
65                mTaskKeyIndices.get(t.key) > mTaskKeyIndices.get(below.key);
66    }
67
68    /** Returns the number of tasks in this group. */
69    public int getTaskCount() { return mTaskKeys.size(); }
70
71    /** Updates the mapping of tasks to indices. */
72    private void updateTaskIndices() {
73        if (mTaskKeys.isEmpty()) {
74            mFrontMostTaskKey = null;
75            mTaskKeyIndices.clear();
76            return;
77        }
78
79        mFrontMostTaskKey = mTaskKeys.get(mTaskKeys.size() - 1);
80        mTaskKeyIndices.clear();
81        int taskCount = mTaskKeys.size();
82        for (int i = 0; i < taskCount; i++) {
83            Task.TaskKey k = mTaskKeys.get(i);
84            mTaskKeyIndices.put(k, i);
85        }
86    }
87}
88