blob: a7e2b0b4f3a3dd8785ed7aa6cfe154caa16eb6aa [file] [log] [blame]
Winson Chung303e1ff2014-03-07 15:06:19 -08001/*
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
17package com.android.systemui.recents.model;
18
Kenny Guya734fc12014-08-28 21:06:27 +010019import android.content.ComponentName;
Winson Chung303e1ff2014-03-07 15:06:19 -080020import android.content.Intent;
21import android.graphics.Bitmap;
Winson Chung93748a12014-07-13 17:43:31 -070022import android.graphics.Color;
Winson Chung303e1ff2014-03-07 15:06:19 -080023import android.graphics.drawable.Drawable;
Winson Chungffa2ec62014-07-03 15:54:42 -070024import com.android.systemui.recents.misc.Utilities;
Winson Chung303e1ff2014-03-07 15:06:19 -080025
Kenny Guya734fc12014-08-28 21:06:27 +010026import java.util.Objects;
27
Winson Chung303e1ff2014-03-07 15:06:19 -080028
29/**
30 * A task represents the top most task in the system's task stack.
31 */
32public class Task {
Winson Chung04dfe0d2014-03-14 14:06:29 -070033 /* Task callbacks */
34 public interface TaskCallbacks {
35 /* Notifies when a task has been bound */
Winson Chung8eaeb7d2014-06-25 15:10:59 -070036 public void onTaskDataLoaded();
Winson Chung04dfe0d2014-03-14 14:06:29 -070037 /* Notifies when a task has been unbound */
38 public void onTaskDataUnloaded();
39 }
40
Kenny Guya734fc12014-08-28 21:06:27 +010041 /** 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 Chung04dfe0d2014-03-14 14:06:29 -070067 /* The Task Key represents the unique primary key for the task */
68 public static class TaskKey {
Kenny Guya734fc12014-08-28 21:06:27 +010069 final ComponentNameKey mComponentNameKey;
Winson Chung04dfe0d2014-03-14 14:06:29 -070070 public final int id;
Winson Chungc6a16232014-04-01 14:04:48 -070071 public final Intent baseIntent;
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070072 public final int userId;
Winson Chungffa2ec62014-07-03 15:54:42 -070073 public long firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -070074 public long lastActiveTime;
Winson Chung04dfe0d2014-03-14 14:06:29 -070075
Winson Chungffa2ec62014-07-03 15:54:42 -070076 public TaskKey(int id, Intent intent, int userId, long firstActiveTime, long lastActiveTime) {
Kenny Guya734fc12014-08-28 21:06:27 +010077 mComponentNameKey = new ComponentNameKey(intent.getComponent(), userId);
Winson Chung04dfe0d2014-03-14 14:06:29 -070078 this.id = id;
Winson Chungc6a16232014-04-01 14:04:48 -070079 this.baseIntent = intent;
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070080 this.userId = userId;
Winson Chungffa2ec62014-07-03 15:54:42 -070081 this.firstActiveTime = firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -070082 this.lastActiveTime = lastActiveTime;
83 }
84
Kenny Guya734fc12014-08-28 21:06:27 +010085 /** Returns the component name key for this task. */
86 public ComponentNameKey getComponentNameKey() {
87 return mComponentNameKey;
88 }
89
Winson Chung04dfe0d2014-03-14 14:06:29 -070090 @Override
91 public boolean equals(Object o) {
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070092 if (!(o instanceof TaskKey)) {
93 return false;
94 }
95 return id == ((TaskKey) o).id
96 && userId == ((TaskKey) o).userId;
Winson Chung04dfe0d2014-03-14 14:06:29 -070097 }
98
99 @Override
100 public int hashCode() {
Amith Yamasani4f0a49e2014-04-10 13:48:15 -0700101 return (id << 5) + userId;
Winson Chung04dfe0d2014-03-14 14:06:29 -0700102 }
103
104 @Override
105 public String toString() {
Amith Yamasani4f0a49e2014-04-10 13:48:15 -0700106 return "Task.Key: " + id + ", "
Winson Chungf1fbd772014-06-24 18:06:58 -0700107 + "u: " + userId + ", "
108 + "lat: " + lastActiveTime + ", "
Amith Yamasani4f0a49e2014-04-10 13:48:15 -0700109 + baseIntent.getComponent().getPackageName();
Winson Chung04dfe0d2014-03-14 14:06:29 -0700110 }
111 }
112
113 public TaskKey key;
Winson Chungffa2ec62014-07-03 15:54:42 -0700114 public TaskGrouping group;
Winson Chung083baf92014-07-11 10:32:42 -0700115 public int taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700116 public int taskAffiliationColor;
Winson Chungdcfa7972014-07-22 12:27:13 -0700117 public boolean isLaunchTarget;
Winson Chung5e3e5d82014-04-02 15:44:55 -0700118 public Drawable applicationIcon;
Winson Chung11e41ba2014-04-21 12:39:20 -0700119 public Drawable activityIcon;
Winson Chung5e3e5d82014-04-02 15:44:55 -0700120 public String activityLabel;
Winson Chungf5e22e72014-05-02 18:35:35 -0700121 public int colorPrimary;
Winson Chung93748a12014-07-13 17:43:31 -0700122 public boolean useLightOnPrimaryColor;
Winson Chung303e1ff2014-03-07 15:06:19 -0800123 public Bitmap thumbnail;
Winson Chung4be04452014-03-24 17:22:30 -0700124 public boolean isActive;
Jason Monk56e09b42014-07-18 10:29:14 -0400125 public boolean lockToThisTask;
126 public boolean lockToTaskEnabled;
Jorim Jaggibdd4b202014-09-26 18:29:07 +0200127 public Bitmap icon;
128 public String iconFilename;
Winson Chung303e1ff2014-03-07 15:06:19 -0800129 TaskCallbacks mCb;
130
Winson Chung0d767552014-04-09 14:33:23 -0700131 public Task() {
132 // Only used by RecentsService for task rect calculations.
133 }
134
Winson Chunga4ccb862014-08-22 15:26:27 -0700135 public Task(TaskKey key, boolean isActive, int taskAffiliation, int taskAffiliationColor,
136 String activityTitle, Drawable activityIcon, int colorPrimary,
Jorim Jaggibdd4b202014-09-26 18:29:07 +0200137 boolean lockToThisTask, boolean lockToTaskEnabled, Bitmap icon,
138 String iconFilename) {
Winson Chunga4ccb862014-08-22 15:26:27 -0700139 boolean isInAffiliationGroup = (taskAffiliation != key.id);
Winson Chungec396d62014-08-06 17:08:00 -0700140 boolean hasAffiliationGroupColor = isInAffiliationGroup && (taskAffiliationColor != 0);
Winson Chunga4ccb862014-08-22 15:26:27 -0700141 this.key = key;
Winson Chung083baf92014-07-11 10:32:42 -0700142 this.taskAffiliation = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700143 this.taskAffiliationColor = taskAffiliationColor;
Winson Chung5e3e5d82014-04-02 15:44:55 -0700144 this.activityLabel = activityTitle;
145 this.activityIcon = activityIcon;
Winson Chungec396d62014-08-06 17:08:00 -0700146 this.colorPrimary = hasAffiliationGroupColor ? taskAffiliationColor : colorPrimary;
147 this.useLightOnPrimaryColor = Utilities.computeContrastBetweenColors(this.colorPrimary,
Winson Chung93748a12014-07-13 17:43:31 -0700148 Color.WHITE) > 3f;
Winson Chung4be04452014-03-24 17:22:30 -0700149 this.isActive = isActive;
Jason Monk56e09b42014-07-18 10:29:14 -0400150 this.lockToThisTask = lockToTaskEnabled && lockToThisTask;
151 this.lockToTaskEnabled = lockToTaskEnabled;
Jorim Jaggibdd4b202014-09-26 18:29:07 +0200152 this.icon = icon;
153 this.iconFilename = iconFilename;
Winson Chunga4ccb862014-08-22 15:26:27 -0700154 }
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 Chung303e1ff2014-03-07 15:06:19 -0800168 }
169
170 /** Set the callbacks */
171 public void setCallbacks(TaskCallbacks cb) {
172 mCb = cb;
173 }
174
Winson Chungffa2ec62014-07-03 15:54:42 -0700175 /** 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 Chung4d7b0922014-03-13 17:14:17 -0700183 /** Notifies the callback listeners that this task has been loaded */
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700184 public void notifyTaskDataLoaded(Bitmap thumbnail, Drawable applicationIcon) {
Winson Chung5e3e5d82014-04-02 15:44:55 -0700185 this.applicationIcon = applicationIcon;
Winson Chung4d7b0922014-03-13 17:14:17 -0700186 this.thumbnail = thumbnail;
187 if (mCb != null) {
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700188 mCb.onTaskDataLoaded();
Winson Chung4d7b0922014-03-13 17:14:17 -0700189 }
190 }
191
192 /** Notifies the callback listeners that this task has been unloaded */
Winson Chung5e3e5d82014-04-02 15:44:55 -0700193 public void notifyTaskDataUnloaded(Bitmap defaultThumbnail, Drawable defaultApplicationIcon) {
194 applicationIcon = defaultApplicationIcon;
Winson Chung4d7b0922014-03-13 17:14:17 -0700195 thumbnail = defaultThumbnail;
196 if (mCb != null) {
Winson Chung04dfe0d2014-03-14 14:06:29 -0700197 mCb.onTaskDataUnloaded();
Winson Chung4d7b0922014-03-13 17:14:17 -0700198 }
199 }
200
Winson Chung303e1ff2014-03-07 15:06:19 -0800201 @Override
202 public boolean equals(Object o) {
Winson Chunga10370f2014-04-02 12:25:04 -0700203 // Check that the id matches
Winson Chung303e1ff2014-03-07 15:06:19 -0800204 Task t = (Task) o;
Winson Chung04dfe0d2014-03-14 14:06:29 -0700205 return key.equals(t.key);
Winson Chung303e1ff2014-03-07 15:06:19 -0800206 }
207
208 @Override
209 public String toString() {
Winson Chungffa2ec62014-07-03 15:54:42 -0700210 String groupAffiliation = "no group";
211 if (group != null) {
Winson Chung083baf92014-07-11 10:32:42 -0700212 groupAffiliation = Integer.toString(group.affiliation);
Winson Chungffa2ec62014-07-03 15:54:42 -0700213 }
214 return "Task (" + groupAffiliation + "): " + key.baseIntent.getComponent().getPackageName() +
215 " [" + super.toString() + "]";
Winson Chung303e1ff2014-03-07 15:06:19 -0800216 }
217}