Task.java revision 04dfe0d26b944324ee920001f40d74cff47281d6
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.Constants;
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 intent;
41
42        public TaskKey(int id, Intent intent) {
43            this.id = id;
44            this.intent = intent;
45        }
46
47        @Override
48        public boolean equals(Object o) {
49            return hashCode() == o.hashCode();
50        }
51
52        @Override
53        public int hashCode() {
54            return id;
55        }
56
57        @Override
58        public String toString() {
59            return "Task.Key: " + id + ", " + intent.getComponent().getPackageName();
60        }
61    }
62
63    public TaskKey key;
64    public String title;
65    public Drawable icon;
66    public Bitmap thumbnail;
67
68    TaskCallbacks mCb;
69
70    public Task(int id, Intent intent, String activityTitle) {
71        this(id, intent, activityTitle, null, null);
72    }
73
74    public Task(int id, Intent intent, String activityTitle, Drawable icon, Bitmap thumbnail) {
75        this.key = new TaskKey(id, intent);
76        this.title = activityTitle;
77        this.icon = icon;
78        this.thumbnail = thumbnail;
79    }
80
81    /** Set the callbacks */
82    public void setCallbacks(TaskCallbacks cb) {
83        mCb = cb;
84    }
85
86    /** Notifies the callback listeners that this task has been loaded */
87    public void notifyTaskDataLoaded(Bitmap thumbnail, Drawable icon) {
88        this.icon = icon;
89        this.thumbnail = thumbnail;
90        if (mCb != null) {
91            mCb.onTaskDataLoaded();
92        }
93    }
94
95    /** Notifies the callback listeners that this task has been unloaded */
96    public void notifyTaskDataUnloaded(Bitmap defaultThumbnail, Drawable defaultIcon) {
97        icon = defaultIcon;
98        thumbnail = defaultThumbnail;
99        if (mCb != null) {
100            mCb.onTaskDataUnloaded();
101        }
102    }
103
104    @Override
105    public boolean equals(Object o) {
106        // If we have multiple task entries for the same task, then we do the simple object
107        // equality check
108        if (Constants.Values.RecentsTaskLoader.TaskEntryMultiplier > 1) {
109            return super.equals(o);
110        }
111
112        // Otherwise, check that the id and intent match (the other fields can be asynchronously
113        // loaded and is unsuitable to testing the identity of this Task)
114        Task t = (Task) o;
115        return key.equals(t.key);
116    }
117
118    @Override
119    public String toString() {
120        return "Task: " + key.intent.getComponent().getPackageName() + " [" + super.toString() + "]";
121    }
122}
123