Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.recents.model; |
| 18 | |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 19 | import android.content.ComponentName; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 20 | import android.content.Intent; |
| 21 | import android.graphics.Bitmap; |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 22 | import android.graphics.Color; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 24 | import com.android.systemui.recents.misc.Utilities; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 25 | |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 26 | import java.util.Objects; |
| 27 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * A task represents the top most task in the system's task stack. |
| 31 | */ |
| 32 | public class Task { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 33 | /* Task callbacks */ |
| 34 | public interface TaskCallbacks { |
| 35 | /* Notifies when a task has been bound */ |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 36 | public void onTaskDataLoaded(); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 37 | /* Notifies when a task has been unbound */ |
| 38 | public void onTaskDataUnloaded(); |
| 39 | } |
| 40 | |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 41 | /** The ComponentNameKey represents the unique primary key for a component |
| 42 | * belonging to a specified user. */ |
| 43 | public static class ComponentNameKey { |
| 44 | final ComponentName component; |
| 45 | final int userId; |
| 46 | |
| 47 | public ComponentNameKey(ComponentName cn, int user) { |
| 48 | component = cn; |
| 49 | userId = user; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public int hashCode() { |
| 54 | return Objects.hash(component, userId); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public boolean equals(Object o) { |
| 59 | if (!(o instanceof ComponentNameKey)) { |
| 60 | return false; |
| 61 | } |
| 62 | return component.equals(((ComponentNameKey) o).component) && |
| 63 | userId == ((ComponentNameKey) o).userId; |
| 64 | } |
| 65 | } |
| 66 | |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 67 | /* The Task Key represents the unique primary key for the task */ |
| 68 | public static class TaskKey { |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 69 | final ComponentNameKey mComponentNameKey; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 70 | public final int id; |
Winson Chung | c6a1623 | 2014-04-01 14:04:48 -0700 | [diff] [blame] | 71 | public final Intent baseIntent; |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 72 | public final int userId; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 73 | public long firstActiveTime; |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 74 | public long lastActiveTime; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 75 | |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 76 | public TaskKey(int id, Intent intent, int userId, long firstActiveTime, long lastActiveTime) { |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 77 | mComponentNameKey = new ComponentNameKey(intent.getComponent(), userId); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 78 | this.id = id; |
Winson Chung | c6a1623 | 2014-04-01 14:04:48 -0700 | [diff] [blame] | 79 | this.baseIntent = intent; |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 80 | this.userId = userId; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 81 | this.firstActiveTime = firstActiveTime; |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 82 | this.lastActiveTime = lastActiveTime; |
| 83 | } |
| 84 | |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 85 | /** Returns the component name key for this task. */ |
| 86 | public ComponentNameKey getComponentNameKey() { |
| 87 | return mComponentNameKey; |
| 88 | } |
| 89 | |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 90 | @Override |
| 91 | public boolean equals(Object o) { |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 92 | if (!(o instanceof TaskKey)) { |
| 93 | return false; |
| 94 | } |
| 95 | return id == ((TaskKey) o).id |
| 96 | && userId == ((TaskKey) o).userId; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public int hashCode() { |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 101 | return (id << 5) + userId; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public String toString() { |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 106 | return "Task.Key: " + id + ", " |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 107 | + "u: " + userId + ", " |
| 108 | + "lat: " + lastActiveTime + ", " |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 109 | + baseIntent.getComponent().getPackageName(); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | public TaskKey key; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 114 | public TaskGrouping group; |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 115 | public int taskAffiliation; |
Winson Chung | ec396d6 | 2014-08-06 17:08:00 -0700 | [diff] [blame] | 116 | public int taskAffiliationColor; |
Winson Chung | dcfa797 | 2014-07-22 12:27:13 -0700 | [diff] [blame] | 117 | public boolean isLaunchTarget; |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 118 | public Drawable applicationIcon; |
Winson Chung | 11e41ba | 2014-04-21 12:39:20 -0700 | [diff] [blame] | 119 | public Drawable activityIcon; |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 120 | public String activityLabel; |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 121 | public int colorPrimary; |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 122 | public boolean useLightOnPrimaryColor; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 123 | public Bitmap thumbnail; |
Winson Chung | 4be0445 | 2014-03-24 17:22:30 -0700 | [diff] [blame] | 124 | public boolean isActive; |
Jason Monk | 56e09b4 | 2014-07-18 10:29:14 -0400 | [diff] [blame] | 125 | public boolean lockToThisTask; |
| 126 | public boolean lockToTaskEnabled; |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 127 | public Bitmap icon; |
| 128 | public String iconFilename; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 129 | TaskCallbacks mCb; |
| 130 | |
Winson Chung | 0d76755 | 2014-04-09 14:33:23 -0700 | [diff] [blame] | 131 | public Task() { |
| 132 | // Only used by RecentsService for task rect calculations. |
| 133 | } |
| 134 | |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 135 | public Task(TaskKey key, boolean isActive, int taskAffiliation, int taskAffiliationColor, |
| 136 | String activityTitle, Drawable activityIcon, int colorPrimary, |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 137 | boolean lockToThisTask, boolean lockToTaskEnabled, Bitmap icon, |
| 138 | String iconFilename) { |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 139 | boolean isInAffiliationGroup = (taskAffiliation != key.id); |
Winson Chung | ec396d6 | 2014-08-06 17:08:00 -0700 | [diff] [blame] | 140 | boolean hasAffiliationGroupColor = isInAffiliationGroup && (taskAffiliationColor != 0); |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 141 | this.key = key; |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 142 | this.taskAffiliation = taskAffiliation; |
Winson Chung | ec396d6 | 2014-08-06 17:08:00 -0700 | [diff] [blame] | 143 | this.taskAffiliationColor = taskAffiliationColor; |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 144 | this.activityLabel = activityTitle; |
| 145 | this.activityIcon = activityIcon; |
Winson Chung | ec396d6 | 2014-08-06 17:08:00 -0700 | [diff] [blame] | 146 | this.colorPrimary = hasAffiliationGroupColor ? taskAffiliationColor : colorPrimary; |
| 147 | this.useLightOnPrimaryColor = Utilities.computeContrastBetweenColors(this.colorPrimary, |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 148 | Color.WHITE) > 3f; |
Winson Chung | 4be0445 | 2014-03-24 17:22:30 -0700 | [diff] [blame] | 149 | this.isActive = isActive; |
Jason Monk | 56e09b4 | 2014-07-18 10:29:14 -0400 | [diff] [blame] | 150 | this.lockToThisTask = lockToTaskEnabled && lockToThisTask; |
| 151 | this.lockToTaskEnabled = lockToTaskEnabled; |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 152 | this.icon = icon; |
| 153 | this.iconFilename = iconFilename; |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** Copies the other task. */ |
| 157 | public void copyFrom(Task o) { |
| 158 | this.key = o.key; |
| 159 | this.taskAffiliation = o.taskAffiliation; |
| 160 | this.taskAffiliationColor = o.taskAffiliationColor; |
| 161 | this.activityLabel = o.activityLabel; |
| 162 | this.activityIcon = o.activityIcon; |
| 163 | this.colorPrimary = o.colorPrimary; |
| 164 | this.useLightOnPrimaryColor = o.useLightOnPrimaryColor; |
| 165 | this.isActive = o.isActive; |
| 166 | this.lockToThisTask = o.lockToThisTask; |
| 167 | this.lockToTaskEnabled = o.lockToTaskEnabled; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** Set the callbacks */ |
| 171 | public void setCallbacks(TaskCallbacks cb) { |
| 172 | mCb = cb; |
| 173 | } |
| 174 | |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 175 | /** Set the grouping */ |
| 176 | public void setGroup(TaskGrouping group) { |
| 177 | if (group != null && this.group != null) { |
| 178 | throw new RuntimeException("This task is already assigned to a group."); |
| 179 | } |
| 180 | this.group = group; |
| 181 | } |
| 182 | |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 183 | /** Notifies the callback listeners that this task has been loaded */ |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 184 | public void notifyTaskDataLoaded(Bitmap thumbnail, Drawable applicationIcon) { |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 185 | this.applicationIcon = applicationIcon; |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 186 | this.thumbnail = thumbnail; |
| 187 | if (mCb != null) { |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 188 | mCb.onTaskDataLoaded(); |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | /** Notifies the callback listeners that this task has been unloaded */ |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 193 | public void notifyTaskDataUnloaded(Bitmap defaultThumbnail, Drawable defaultApplicationIcon) { |
| 194 | applicationIcon = defaultApplicationIcon; |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 195 | thumbnail = defaultThumbnail; |
| 196 | if (mCb != null) { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 197 | mCb.onTaskDataUnloaded(); |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 201 | @Override |
| 202 | public boolean equals(Object o) { |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 203 | // Check that the id matches |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 204 | Task t = (Task) o; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 205 | return key.equals(t.key); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public String toString() { |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 210 | String groupAffiliation = "no group"; |
| 211 | if (group != null) { |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 212 | groupAffiliation = Integer.toString(group.affiliation); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 213 | } |
| 214 | return "Task (" + groupAffiliation + "): " + key.baseIntent.getComponent().getPackageName() + |
| 215 | " [" + super.toString() + "]"; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 216 | } |
| 217 | } |