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 | |
| 19 | import android.content.Intent; |
| 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.drawable.Drawable; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 22 | import com.android.systemui.recents.misc.Utilities; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 23 | |
| 24 | |
| 25 | /** |
| 26 | * A task represents the top most task in the system's task stack. |
| 27 | */ |
| 28 | public class Task { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 29 | /* Task callbacks */ |
| 30 | public interface TaskCallbacks { |
| 31 | /* Notifies when a task has been bound */ |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 32 | public void onTaskDataLoaded(); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 33 | /* 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 Chung | c6a1623 | 2014-04-01 14:04:48 -0700 | [diff] [blame] | 40 | public final Intent baseIntent; |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 41 | public final int userId; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 42 | public long firstActiveTime; |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 43 | public long lastActiveTime; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 44 | |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 45 | public TaskKey(int id, Intent intent, int userId, long firstActiveTime, long lastActiveTime) { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 46 | this.id = id; |
Winson Chung | c6a1623 | 2014-04-01 14:04:48 -0700 | [diff] [blame] | 47 | this.baseIntent = intent; |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 48 | this.userId = userId; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 49 | this.firstActiveTime = firstActiveTime; |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 50 | this.lastActiveTime = lastActiveTime; |
| 51 | } |
| 52 | |
| 53 | public void updateLastActiveTime(long lastActiveTime) { |
| 54 | this.lastActiveTime = lastActiveTime; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public boolean equals(Object o) { |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 59 | if (!(o instanceof TaskKey)) { |
| 60 | return false; |
| 61 | } |
| 62 | return id == ((TaskKey) o).id |
| 63 | && userId == ((TaskKey) o).userId; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public int hashCode() { |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 68 | return (id << 5) + userId; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public String toString() { |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 73 | return "Task.Key: " + id + ", " |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 74 | + "u: " + userId + ", " |
| 75 | + "lat: " + lastActiveTime + ", " |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 76 | + baseIntent.getComponent().getPackageName(); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | public TaskKey key; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 81 | public TaskGrouping group; |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame^] | 82 | public int taskAffiliation; |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 83 | public Drawable applicationIcon; |
Winson Chung | 11e41ba | 2014-04-21 12:39:20 -0700 | [diff] [blame] | 84 | public Drawable activityIcon; |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 85 | public String activityLabel; |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 86 | public int colorPrimary; |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 87 | public int colorPrimaryGreyscale; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 88 | public Bitmap thumbnail; |
Winson Chung | 4be0445 | 2014-03-24 17:22:30 -0700 | [diff] [blame] | 89 | public boolean isActive; |
Kenny Guy | 1f1cce1 | 2014-04-04 14:47:50 +0100 | [diff] [blame] | 90 | public int userId; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 91 | |
| 92 | TaskCallbacks mCb; |
| 93 | |
Winson Chung | 0d76755 | 2014-04-09 14:33:23 -0700 | [diff] [blame] | 94 | public Task() { |
| 95 | // Only used by RecentsService for task rect calculations. |
| 96 | } |
| 97 | |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame^] | 98 | 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 Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 101 | this.key = new TaskKey(id, intent, userId, firstActiveTime, lastActiveTime); |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame^] | 102 | this.taskAffiliation = taskAffiliation; |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 103 | this.activityLabel = activityTitle; |
| 104 | this.activityIcon = activityIcon; |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 105 | this.colorPrimary = colorPrimary; |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 106 | this.colorPrimaryGreyscale = Utilities.colorToGreyscale(colorPrimary); |
Winson Chung | 4be0445 | 2014-03-24 17:22:30 -0700 | [diff] [blame] | 107 | this.isActive = isActive; |
Kenny Guy | 1f1cce1 | 2014-04-04 14:47:50 +0100 | [diff] [blame] | 108 | this.userId = userId; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** Set the callbacks */ |
| 112 | public void setCallbacks(TaskCallbacks cb) { |
| 113 | mCb = cb; |
| 114 | } |
| 115 | |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 116 | /** 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 Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 124 | /** Notifies the callback listeners that this task has been loaded */ |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 125 | public void notifyTaskDataLoaded(Bitmap thumbnail, Drawable applicationIcon) { |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 126 | this.applicationIcon = applicationIcon; |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 127 | this.thumbnail = thumbnail; |
| 128 | if (mCb != null) { |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 129 | mCb.onTaskDataLoaded(); |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | /** Notifies the callback listeners that this task has been unloaded */ |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 134 | public void notifyTaskDataUnloaded(Bitmap defaultThumbnail, Drawable defaultApplicationIcon) { |
| 135 | applicationIcon = defaultApplicationIcon; |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 136 | thumbnail = defaultThumbnail; |
| 137 | if (mCb != null) { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 138 | mCb.onTaskDataUnloaded(); |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 142 | @Override |
| 143 | public boolean equals(Object o) { |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 144 | // Check that the id matches |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 145 | Task t = (Task) o; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 146 | return key.equals(t.key); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public String toString() { |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 151 | String groupAffiliation = "no group"; |
| 152 | if (group != null) { |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame^] | 153 | groupAffiliation = Integer.toString(group.affiliation); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 154 | } |
| 155 | return "Task (" + groupAffiliation + "): " + key.baseIntent.getComponent().getPackageName() + |
| 156 | " [" + super.toString() + "]"; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 157 | } |
| 158 | } |