blob: 406e03ff23ce2e844e68d56a29bbde4018367723 [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;
Winson Chung303e1ff2014-03-07 15:06:19 -0800127
128 TaskCallbacks mCb;
129
Winson Chung0d767552014-04-09 14:33:23 -0700130 public Task() {
131 // Only used by RecentsService for task rect calculations.
132 }
133
Winson Chunga4ccb862014-08-22 15:26:27 -0700134 public Task(TaskKey key, boolean isActive, int taskAffiliation, int taskAffiliationColor,
135 String activityTitle, Drawable activityIcon, int colorPrimary,
136 boolean lockToThisTask, boolean lockToTaskEnabled) {
137 boolean isInAffiliationGroup = (taskAffiliation != key.id);
Winson Chungec396d62014-08-06 17:08:00 -0700138 boolean hasAffiliationGroupColor = isInAffiliationGroup && (taskAffiliationColor != 0);
Winson Chunga4ccb862014-08-22 15:26:27 -0700139 this.key = key;
Winson Chung083baf92014-07-11 10:32:42 -0700140 this.taskAffiliation = taskAffiliation;
Winson Chungec396d62014-08-06 17:08:00 -0700141 this.taskAffiliationColor = taskAffiliationColor;
Winson Chung5e3e5d82014-04-02 15:44:55 -0700142 this.activityLabel = activityTitle;
143 this.activityIcon = activityIcon;
Winson Chungec396d62014-08-06 17:08:00 -0700144 this.colorPrimary = hasAffiliationGroupColor ? taskAffiliationColor : colorPrimary;
145 this.useLightOnPrimaryColor = Utilities.computeContrastBetweenColors(this.colorPrimary,
Winson Chung93748a12014-07-13 17:43:31 -0700146 Color.WHITE) > 3f;
Winson Chung4be04452014-03-24 17:22:30 -0700147 this.isActive = isActive;
Jason Monk56e09b42014-07-18 10:29:14 -0400148 this.lockToThisTask = lockToTaskEnabled && lockToThisTask;
149 this.lockToTaskEnabled = lockToTaskEnabled;
Winson Chunga4ccb862014-08-22 15:26:27 -0700150 }
151
152 /** Copies the other task. */
153 public void copyFrom(Task o) {
154 this.key = o.key;
155 this.taskAffiliation = o.taskAffiliation;
156 this.taskAffiliationColor = o.taskAffiliationColor;
157 this.activityLabel = o.activityLabel;
158 this.activityIcon = o.activityIcon;
159 this.colorPrimary = o.colorPrimary;
160 this.useLightOnPrimaryColor = o.useLightOnPrimaryColor;
161 this.isActive = o.isActive;
162 this.lockToThisTask = o.lockToThisTask;
163 this.lockToTaskEnabled = o.lockToTaskEnabled;
Winson Chung303e1ff2014-03-07 15:06:19 -0800164 }
165
166 /** Set the callbacks */
167 public void setCallbacks(TaskCallbacks cb) {
168 mCb = cb;
169 }
170
Winson Chungffa2ec62014-07-03 15:54:42 -0700171 /** Set the grouping */
172 public void setGroup(TaskGrouping group) {
173 if (group != null && this.group != null) {
174 throw new RuntimeException("This task is already assigned to a group.");
175 }
176 this.group = group;
177 }
178
Winson Chung4d7b0922014-03-13 17:14:17 -0700179 /** Notifies the callback listeners that this task has been loaded */
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700180 public void notifyTaskDataLoaded(Bitmap thumbnail, Drawable applicationIcon) {
Winson Chung5e3e5d82014-04-02 15:44:55 -0700181 this.applicationIcon = applicationIcon;
Winson Chung4d7b0922014-03-13 17:14:17 -0700182 this.thumbnail = thumbnail;
183 if (mCb != null) {
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700184 mCb.onTaskDataLoaded();
Winson Chung4d7b0922014-03-13 17:14:17 -0700185 }
186 }
187
188 /** Notifies the callback listeners that this task has been unloaded */
Winson Chung5e3e5d82014-04-02 15:44:55 -0700189 public void notifyTaskDataUnloaded(Bitmap defaultThumbnail, Drawable defaultApplicationIcon) {
190 applicationIcon = defaultApplicationIcon;
Winson Chung4d7b0922014-03-13 17:14:17 -0700191 thumbnail = defaultThumbnail;
192 if (mCb != null) {
Winson Chung04dfe0d2014-03-14 14:06:29 -0700193 mCb.onTaskDataUnloaded();
Winson Chung4d7b0922014-03-13 17:14:17 -0700194 }
195 }
196
Winson Chung303e1ff2014-03-07 15:06:19 -0800197 @Override
198 public boolean equals(Object o) {
Winson Chunga10370f2014-04-02 12:25:04 -0700199 // Check that the id matches
Winson Chung303e1ff2014-03-07 15:06:19 -0800200 Task t = (Task) o;
Winson Chung04dfe0d2014-03-14 14:06:29 -0700201 return key.equals(t.key);
Winson Chung303e1ff2014-03-07 15:06:19 -0800202 }
203
204 @Override
205 public String toString() {
Winson Chungffa2ec62014-07-03 15:54:42 -0700206 String groupAffiliation = "no group";
207 if (group != null) {
Winson Chung083baf92014-07-11 10:32:42 -0700208 groupAffiliation = Integer.toString(group.affiliation);
Winson Chungffa2ec62014-07-03 15:54:42 -0700209 }
210 return "Task (" + groupAffiliation + "): " + key.baseIntent.getComponent().getPackageName() +
211 " [" + super.toString() + "]";
Winson Chung303e1ff2014-03-07 15:06:19 -0800212 }
213}