blob: 0667e4cb25bdfaefce984855d0e2e4e620ccb894 [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
19import android.content.Intent;
20import android.graphics.Bitmap;
21import android.graphics.drawable.Drawable;
Winson Chungffa2ec62014-07-03 15:54:42 -070022import com.android.systemui.recents.misc.Utilities;
Winson Chung303e1ff2014-03-07 15:06:19 -080023
24
25/**
26 * A task represents the top most task in the system's task stack.
27 */
28public class Task {
Winson Chung04dfe0d2014-03-14 14:06:29 -070029 /* Task callbacks */
30 public interface TaskCallbacks {
31 /* Notifies when a task has been bound */
Winson Chung8eaeb7d2014-06-25 15:10:59 -070032 public void onTaskDataLoaded();
Winson Chung04dfe0d2014-03-14 14:06:29 -070033 /* Notifies when a task has been unbound */
34 public void onTaskDataUnloaded();
35 }
36
37 /* The Task Key represents the unique primary key for the task */
38 public static class TaskKey {
39 public final int id;
Winson Chungc6a16232014-04-01 14:04:48 -070040 public final Intent baseIntent;
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070041 public final int userId;
Winson Chungffa2ec62014-07-03 15:54:42 -070042 public long firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -070043 public long lastActiveTime;
Winson Chung04dfe0d2014-03-14 14:06:29 -070044
Winson Chungffa2ec62014-07-03 15:54:42 -070045 public TaskKey(int id, Intent intent, int userId, long firstActiveTime, long lastActiveTime) {
Winson Chung04dfe0d2014-03-14 14:06:29 -070046 this.id = id;
Winson Chungc6a16232014-04-01 14:04:48 -070047 this.baseIntent = intent;
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070048 this.userId = userId;
Winson Chungffa2ec62014-07-03 15:54:42 -070049 this.firstActiveTime = firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -070050 this.lastActiveTime = lastActiveTime;
51 }
52
53 public void updateLastActiveTime(long lastActiveTime) {
54 this.lastActiveTime = lastActiveTime;
Winson Chung04dfe0d2014-03-14 14:06:29 -070055 }
56
57 @Override
58 public boolean equals(Object o) {
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070059 if (!(o instanceof TaskKey)) {
60 return false;
61 }
62 return id == ((TaskKey) o).id
63 && userId == ((TaskKey) o).userId;
Winson Chung04dfe0d2014-03-14 14:06:29 -070064 }
65
66 @Override
67 public int hashCode() {
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070068 return (id << 5) + userId;
Winson Chung04dfe0d2014-03-14 14:06:29 -070069 }
70
71 @Override
72 public String toString() {
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070073 return "Task.Key: " + id + ", "
Winson Chungf1fbd772014-06-24 18:06:58 -070074 + "u: " + userId + ", "
75 + "lat: " + lastActiveTime + ", "
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070076 + baseIntent.getComponent().getPackageName();
Winson Chung04dfe0d2014-03-14 14:06:29 -070077 }
78 }
79
80 public TaskKey key;
Winson Chungffa2ec62014-07-03 15:54:42 -070081 public TaskGrouping group;
Winson Chung083baf92014-07-11 10:32:42 -070082 public int taskAffiliation;
Winson Chung5e3e5d82014-04-02 15:44:55 -070083 public Drawable applicationIcon;
Winson Chung11e41ba2014-04-21 12:39:20 -070084 public Drawable activityIcon;
Winson Chung5e3e5d82014-04-02 15:44:55 -070085 public String activityLabel;
Winson Chungf5e22e72014-05-02 18:35:35 -070086 public int colorPrimary;
Winson Chung8eaeb7d2014-06-25 15:10:59 -070087 public int colorPrimaryGreyscale;
Winson Chung303e1ff2014-03-07 15:06:19 -080088 public Bitmap thumbnail;
Winson Chung4be04452014-03-24 17:22:30 -070089 public boolean isActive;
Kenny Guy1f1cce12014-04-04 14:47:50 +010090 public int userId;
Winson Chung303e1ff2014-03-07 15:06:19 -080091
92 TaskCallbacks mCb;
93
Winson Chung0d767552014-04-09 14:33:23 -070094 public Task() {
95 // Only used by RecentsService for task rect calculations.
96 }
97
Winson Chung083baf92014-07-11 10:32:42 -070098 public Task(int id, boolean isActive, Intent intent, int taskAffiliation, String activityTitle,
99 Drawable activityIcon, int colorPrimary, int userId,
100 long firstActiveTime, long lastActiveTime) {
Winson Chungffa2ec62014-07-03 15:54:42 -0700101 this.key = new TaskKey(id, intent, userId, firstActiveTime, lastActiveTime);
Winson Chung083baf92014-07-11 10:32:42 -0700102 this.taskAffiliation = taskAffiliation;
Winson Chung5e3e5d82014-04-02 15:44:55 -0700103 this.activityLabel = activityTitle;
104 this.activityIcon = activityIcon;
Winson Chungf5e22e72014-05-02 18:35:35 -0700105 this.colorPrimary = colorPrimary;
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700106 this.colorPrimaryGreyscale = Utilities.colorToGreyscale(colorPrimary);
Winson Chung4be04452014-03-24 17:22:30 -0700107 this.isActive = isActive;
Kenny Guy1f1cce12014-04-04 14:47:50 +0100108 this.userId = userId;
Winson Chung303e1ff2014-03-07 15:06:19 -0800109 }
110
111 /** Set the callbacks */
112 public void setCallbacks(TaskCallbacks cb) {
113 mCb = cb;
114 }
115
Winson Chungffa2ec62014-07-03 15:54:42 -0700116 /** Set the grouping */
117 public void setGroup(TaskGrouping group) {
118 if (group != null && this.group != null) {
119 throw new RuntimeException("This task is already assigned to a group.");
120 }
121 this.group = group;
122 }
123
Winson Chung4d7b0922014-03-13 17:14:17 -0700124 /** Notifies the callback listeners that this task has been loaded */
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700125 public void notifyTaskDataLoaded(Bitmap thumbnail, Drawable applicationIcon) {
Winson Chung5e3e5d82014-04-02 15:44:55 -0700126 this.applicationIcon = applicationIcon;
Winson Chung4d7b0922014-03-13 17:14:17 -0700127 this.thumbnail = thumbnail;
128 if (mCb != null) {
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700129 mCb.onTaskDataLoaded();
Winson Chung4d7b0922014-03-13 17:14:17 -0700130 }
131 }
132
133 /** Notifies the callback listeners that this task has been unloaded */
Winson Chung5e3e5d82014-04-02 15:44:55 -0700134 public void notifyTaskDataUnloaded(Bitmap defaultThumbnail, Drawable defaultApplicationIcon) {
135 applicationIcon = defaultApplicationIcon;
Winson Chung4d7b0922014-03-13 17:14:17 -0700136 thumbnail = defaultThumbnail;
137 if (mCb != null) {
Winson Chung04dfe0d2014-03-14 14:06:29 -0700138 mCb.onTaskDataUnloaded();
Winson Chung4d7b0922014-03-13 17:14:17 -0700139 }
140 }
141
Winson Chung303e1ff2014-03-07 15:06:19 -0800142 @Override
143 public boolean equals(Object o) {
Winson Chunga10370f2014-04-02 12:25:04 -0700144 // Check that the id matches
Winson Chung303e1ff2014-03-07 15:06:19 -0800145 Task t = (Task) o;
Winson Chung04dfe0d2014-03-14 14:06:29 -0700146 return key.equals(t.key);
Winson Chung303e1ff2014-03-07 15:06:19 -0800147 }
148
149 @Override
150 public String toString() {
Winson Chungffa2ec62014-07-03 15:54:42 -0700151 String groupAffiliation = "no group";
152 if (group != null) {
Winson Chung083baf92014-07-11 10:32:42 -0700153 groupAffiliation = Integer.toString(group.affiliation);
Winson Chungffa2ec62014-07-03 15:54:42 -0700154 }
155 return "Task (" + groupAffiliation + "): " + key.baseIntent.getComponent().getPackageName() +
156 " [" + super.toString() + "]";
Winson Chung303e1ff2014-03-07 15:06:19 -0800157 }
158}