blob: 1670735873048c99ddb574528ae4cbb6ee107618 [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;
Winson Chung93748a12014-07-13 17:43:31 -070021import android.graphics.Color;
Winson Chung303e1ff2014-03-07 15:06:19 -080022import android.graphics.drawable.Drawable;
Winson Chungffa2ec62014-07-03 15:54:42 -070023import com.android.systemui.recents.misc.Utilities;
Winson Chung303e1ff2014-03-07 15:06:19 -080024
25
26/**
27 * A task represents the top most task in the system's task stack.
28 */
29public class Task {
Winson Chung04dfe0d2014-03-14 14:06:29 -070030 /* Task callbacks */
31 public interface TaskCallbacks {
32 /* Notifies when a task has been bound */
Winson Chung8eaeb7d2014-06-25 15:10:59 -070033 public void onTaskDataLoaded();
Winson Chung04dfe0d2014-03-14 14:06:29 -070034 /* Notifies when a task has been unbound */
35 public void onTaskDataUnloaded();
36 }
37
38 /* The Task Key represents the unique primary key for the task */
39 public static class TaskKey {
40 public final int id;
Winson Chungc6a16232014-04-01 14:04:48 -070041 public final Intent baseIntent;
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070042 public final int userId;
Winson Chungffa2ec62014-07-03 15:54:42 -070043 public long firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -070044 public long lastActiveTime;
Winson Chung04dfe0d2014-03-14 14:06:29 -070045
Winson Chungffa2ec62014-07-03 15:54:42 -070046 public TaskKey(int id, Intent intent, int userId, long firstActiveTime, long lastActiveTime) {
Winson Chung04dfe0d2014-03-14 14:06:29 -070047 this.id = id;
Winson Chungc6a16232014-04-01 14:04:48 -070048 this.baseIntent = intent;
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070049 this.userId = userId;
Winson Chungffa2ec62014-07-03 15:54:42 -070050 this.firstActiveTime = firstActiveTime;
Winson Chungf1fbd772014-06-24 18:06:58 -070051 this.lastActiveTime = lastActiveTime;
52 }
53
54 public void updateLastActiveTime(long lastActiveTime) {
55 this.lastActiveTime = lastActiveTime;
Winson Chung04dfe0d2014-03-14 14:06:29 -070056 }
57
58 @Override
59 public boolean equals(Object o) {
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070060 if (!(o instanceof TaskKey)) {
61 return false;
62 }
63 return id == ((TaskKey) o).id
64 && userId == ((TaskKey) o).userId;
Winson Chung04dfe0d2014-03-14 14:06:29 -070065 }
66
67 @Override
68 public int hashCode() {
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070069 return (id << 5) + userId;
Winson Chung04dfe0d2014-03-14 14:06:29 -070070 }
71
72 @Override
73 public String toString() {
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070074 return "Task.Key: " + id + ", "
Winson Chungf1fbd772014-06-24 18:06:58 -070075 + "u: " + userId + ", "
76 + "lat: " + lastActiveTime + ", "
Amith Yamasani4f0a49e2014-04-10 13:48:15 -070077 + baseIntent.getComponent().getPackageName();
Winson Chung04dfe0d2014-03-14 14:06:29 -070078 }
79 }
80
81 public TaskKey key;
Winson Chungffa2ec62014-07-03 15:54:42 -070082 public TaskGrouping group;
Winson Chung083baf92014-07-11 10:32:42 -070083 public int taskAffiliation;
Winson Chung5e3e5d82014-04-02 15:44:55 -070084 public Drawable applicationIcon;
Winson Chung11e41ba2014-04-21 12:39:20 -070085 public Drawable activityIcon;
Winson Chung5e3e5d82014-04-02 15:44:55 -070086 public String activityLabel;
Winson Chungf5e22e72014-05-02 18:35:35 -070087 public int colorPrimary;
Winson Chung93748a12014-07-13 17:43:31 -070088 public boolean useLightOnPrimaryColor;
Winson Chung303e1ff2014-03-07 15:06:19 -080089 public Bitmap thumbnail;
Winson Chung4be04452014-03-24 17:22:30 -070090 public boolean isActive;
Winson Chung1f24c7e2014-07-11 17:06:48 -070091 public boolean canLockToTask;
Kenny Guy1f1cce12014-04-04 14:47:50 +010092 public int userId;
Winson Chung303e1ff2014-03-07 15:06:19 -080093
94 TaskCallbacks mCb;
95
Winson Chung0d767552014-04-09 14:33:23 -070096 public Task() {
97 // Only used by RecentsService for task rect calculations.
98 }
99
Winson Chung083baf92014-07-11 10:32:42 -0700100 public Task(int id, boolean isActive, Intent intent, int taskAffiliation, String activityTitle,
101 Drawable activityIcon, int colorPrimary, int userId,
Winson Chung1f24c7e2014-07-11 17:06:48 -0700102 long firstActiveTime, long lastActiveTime, boolean canLockToTask) {
Winson Chungffa2ec62014-07-03 15:54:42 -0700103 this.key = new TaskKey(id, intent, userId, firstActiveTime, lastActiveTime);
Winson Chung083baf92014-07-11 10:32:42 -0700104 this.taskAffiliation = taskAffiliation;
Winson Chung5e3e5d82014-04-02 15:44:55 -0700105 this.activityLabel = activityTitle;
106 this.activityIcon = activityIcon;
Winson Chungf5e22e72014-05-02 18:35:35 -0700107 this.colorPrimary = colorPrimary;
Winson Chung93748a12014-07-13 17:43:31 -0700108 this.useLightOnPrimaryColor = Utilities.computeContrastBetweenColors(colorPrimary,
109 Color.WHITE) > 3f;
Winson Chung4be04452014-03-24 17:22:30 -0700110 this.isActive = isActive;
Winson Chung1f24c7e2014-07-11 17:06:48 -0700111 this.canLockToTask = canLockToTask;
Kenny Guy1f1cce12014-04-04 14:47:50 +0100112 this.userId = userId;
Winson Chung303e1ff2014-03-07 15:06:19 -0800113 }
114
115 /** Set the callbacks */
116 public void setCallbacks(TaskCallbacks cb) {
117 mCb = cb;
118 }
119
Winson Chungffa2ec62014-07-03 15:54:42 -0700120 /** Set the grouping */
121 public void setGroup(TaskGrouping group) {
122 if (group != null && this.group != null) {
123 throw new RuntimeException("This task is already assigned to a group.");
124 }
125 this.group = group;
126 }
127
Winson Chung4d7b0922014-03-13 17:14:17 -0700128 /** Notifies the callback listeners that this task has been loaded */
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700129 public void notifyTaskDataLoaded(Bitmap thumbnail, Drawable applicationIcon) {
Winson Chung5e3e5d82014-04-02 15:44:55 -0700130 this.applicationIcon = applicationIcon;
Winson Chung4d7b0922014-03-13 17:14:17 -0700131 this.thumbnail = thumbnail;
132 if (mCb != null) {
Winson Chung8eaeb7d2014-06-25 15:10:59 -0700133 mCb.onTaskDataLoaded();
Winson Chung4d7b0922014-03-13 17:14:17 -0700134 }
135 }
136
137 /** Notifies the callback listeners that this task has been unloaded */
Winson Chung5e3e5d82014-04-02 15:44:55 -0700138 public void notifyTaskDataUnloaded(Bitmap defaultThumbnail, Drawable defaultApplicationIcon) {
139 applicationIcon = defaultApplicationIcon;
Winson Chung4d7b0922014-03-13 17:14:17 -0700140 thumbnail = defaultThumbnail;
141 if (mCb != null) {
Winson Chung04dfe0d2014-03-14 14:06:29 -0700142 mCb.onTaskDataUnloaded();
Winson Chung4d7b0922014-03-13 17:14:17 -0700143 }
144 }
145
Winson Chung303e1ff2014-03-07 15:06:19 -0800146 @Override
147 public boolean equals(Object o) {
Winson Chunga10370f2014-04-02 12:25:04 -0700148 // Check that the id matches
Winson Chung303e1ff2014-03-07 15:06:19 -0800149 Task t = (Task) o;
Winson Chung04dfe0d2014-03-14 14:06:29 -0700150 return key.equals(t.key);
Winson Chung303e1ff2014-03-07 15:06:19 -0800151 }
152
153 @Override
154 public String toString() {
Winson Chungffa2ec62014-07-03 15:54:42 -0700155 String groupAffiliation = "no group";
156 if (group != null) {
Winson Chung083baf92014-07-11 10:32:42 -0700157 groupAffiliation = Integer.toString(group.affiliation);
Winson Chungffa2ec62014-07-03 15:54:42 -0700158 }
159 return "Task (" + groupAffiliation + "): " + key.baseIntent.getComponent().getPackageName() +
160 " [" + super.toString() + "]";
Winson Chung303e1ff2014-03-07 15:06:19 -0800161 }
162}