1 package com.android.systemui.recents.model; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 6 /** Represents a grouping of tasks witihin a stack. */ 7 public 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. */ TaskGrouping(int affiliation)17 public TaskGrouping(int affiliation) { 18 this.affiliation = affiliation; 19 } 20 21 /** Adds a new task to this group. */ addTask(Task t)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. */ removeTask(Task t)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 /** Returns the key of the next task in the group. */ getNextTaskInGroup(Task t)47 public Task.TaskKey getNextTaskInGroup(Task t) { 48 int i = indexOf(t); 49 if ((i + 1) < getTaskCount()) { 50 return mTaskKeys.get(i + 1); 51 } 52 return null; 53 } 54 55 /** Returns the key of the previous task in the group. */ getPrevTaskInGroup(Task t)56 public Task.TaskKey getPrevTaskInGroup(Task t) { 57 int i = indexOf(t); 58 if ((i - 1) >= 0) { 59 return mTaskKeys.get(i - 1); 60 } 61 return null; 62 } 63 64 /** Gets the front task */ isFrontMostTask(Task t)65 public boolean isFrontMostTask(Task t) { 66 return (t.key == mFrontMostTaskKey); 67 } 68 69 /** Finds the index of a given task in a group. */ indexOf(Task t)70 public int indexOf(Task t) { 71 return mTaskKeyIndices.get(t.key); 72 } 73 74 /** Returns whether a task is in this grouping. */ containsTask(Task t)75 public boolean containsTask(Task t) { 76 return mTaskKeyIndices.containsKey(t.key); 77 } 78 79 /** Returns whether one task is above another in the group. If they are not in the same group, 80 * this returns false. */ isTaskAboveTask(Task t, Task below)81 public boolean isTaskAboveTask(Task t, Task below) { 82 return mTaskKeyIndices.containsKey(t.key) && mTaskKeyIndices.containsKey(below.key) && 83 mTaskKeyIndices.get(t.key) > mTaskKeyIndices.get(below.key); 84 } 85 86 /** Returns the number of tasks in this group. */ getTaskCount()87 public int getTaskCount() { return mTaskKeys.size(); } 88 89 /** Updates the mapping of tasks to indices. */ updateTaskIndices()90 private void updateTaskIndices() { 91 if (mTaskKeys.isEmpty()) { 92 mFrontMostTaskKey = null; 93 mTaskKeyIndices.clear(); 94 return; 95 } 96 97 mFrontMostTaskKey = mTaskKeys.get(mTaskKeys.size() - 1); 98 mTaskKeyIndices.clear(); 99 int taskCount = mTaskKeys.size(); 100 for (int i = 0; i < taskCount; i++) { 101 Task.TaskKey k = mTaskKeys.get(i); 102 mTaskKeyIndices.put(k, i); 103 } 104 } 105 } 106