Task.java revision ec396d6399c5c31d697d81e94aff459e9771b0c6
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        @Override
55        public boolean equals(Object o) {
56            if (!(o instanceof TaskKey)) {
57                return false;
58            }
59            return id == ((TaskKey) o).id
60                    && userId == ((TaskKey) o).userId;
61        }
62
63        @Override
64        public int hashCode() {
65            return (id << 5) + userId;
66        }
67
68        @Override
69        public String toString() {
70            return "Task.Key: " + id + ", "
71                    + "u: " + userId + ", "
72                    + "lat: " + lastActiveTime + ", "
73                    + baseIntent.getComponent().getPackageName();
74        }
75    }
76
77    public TaskKey key;
78    public TaskGrouping group;
79    public int taskAffiliation;
80    public int taskAffiliationColor;
81    public boolean isLaunchTarget;
82    public Drawable applicationIcon;
83    public Drawable activityIcon;
84    public String activityLabel;
85    public int colorPrimary;
86    public boolean useLightOnPrimaryColor;
87    public Bitmap thumbnail;
88    public boolean isActive;
89    public boolean lockToThisTask;
90    public boolean lockToTaskEnabled;
91    public int userId;
92
93    TaskCallbacks mCb;
94
95    public Task() {
96        // Only used by RecentsService for task rect calculations.
97    }
98
99    public Task(int id, boolean isActive, Intent intent, int taskAffiliation, int taskAffiliationColor,
100                String activityTitle, Drawable activityIcon, int colorPrimary, int userId,
101                long firstActiveTime, long lastActiveTime, boolean lockToThisTask,
102                boolean lockToTaskEnabled) {
103        boolean isInAffiliationGroup = (taskAffiliation != id);
104        boolean hasAffiliationGroupColor = isInAffiliationGroup && (taskAffiliationColor != 0);
105        this.key = new TaskKey(id, intent, userId, firstActiveTime, lastActiveTime);
106        this.taskAffiliation = taskAffiliation;
107        this.taskAffiliationColor = taskAffiliationColor;
108        this.activityLabel = activityTitle;
109        this.activityIcon = activityIcon;
110        this.colorPrimary = hasAffiliationGroupColor ? taskAffiliationColor : colorPrimary;
111        this.useLightOnPrimaryColor = Utilities.computeContrastBetweenColors(this.colorPrimary,
112                Color.WHITE) > 3f;
113        this.isActive = isActive;
114        this.lockToThisTask = lockToTaskEnabled && lockToThisTask;
115        this.lockToTaskEnabled = lockToTaskEnabled;
116        this.userId = userId;
117    }
118
119    /** Set the callbacks */
120    public void setCallbacks(TaskCallbacks cb) {
121        mCb = cb;
122    }
123
124    /** Set the grouping */
125    public void setGroup(TaskGrouping group) {
126        if (group != null && this.group != null) {
127            throw new RuntimeException("This task is already assigned to a group.");
128        }
129        this.group = group;
130    }
131
132    /** Notifies the callback listeners that this task has been loaded */
133    public void notifyTaskDataLoaded(Bitmap thumbnail, Drawable applicationIcon) {
134        this.applicationIcon = applicationIcon;
135        this.thumbnail = thumbnail;
136        if (mCb != null) {
137            mCb.onTaskDataLoaded();
138        }
139    }
140
141    /** Notifies the callback listeners that this task has been unloaded */
142    public void notifyTaskDataUnloaded(Bitmap defaultThumbnail, Drawable defaultApplicationIcon) {
143        applicationIcon = defaultApplicationIcon;
144        thumbnail = defaultThumbnail;
145        if (mCb != null) {
146            mCb.onTaskDataUnloaded();
147        }
148    }
149
150    @Override
151    public boolean equals(Object o) {
152        // Check that the id matches
153        Task t = (Task) o;
154        return key.equals(t.key);
155    }
156
157    @Override
158    public String toString() {
159        String groupAffiliation = "no group";
160        if (group != null) {
161            groupAffiliation = Integer.toString(group.affiliation);
162        }
163        return "Task (" + groupAffiliation + "): " + key.baseIntent.getComponent().getPackageName() +
164                " [" + super.toString() + "]";
165    }
166}
167