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