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