Task.java revision ffa2ec664479bff6b4b61d4c349d9db2cb37ca16
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 Drawable applicationIcon; 83 public Drawable activityIcon; 84 public String activityLabel; 85 public int colorPrimary; 86 public int colorPrimaryGreyscale; 87 public Bitmap thumbnail; 88 public boolean isActive; 89 public int userId; 90 91 TaskCallbacks mCb; 92 93 public Task() { 94 // Only used by RecentsService for task rect calculations. 95 } 96 97 public Task(int id, boolean isActive, Intent intent, String activityTitle, 98 Drawable activityIcon, int colorPrimary, int userId, long firstActiveTime, 99 long lastActiveTime) { 100 this.key = new TaskKey(id, intent, userId, firstActiveTime, lastActiveTime); 101 this.activityLabel = activityTitle; 102 this.activityIcon = activityIcon; 103 this.colorPrimary = colorPrimary; 104 this.colorPrimaryGreyscale = Utilities.colorToGreyscale(colorPrimary); 105 this.isActive = isActive; 106 this.userId = userId; 107 } 108 109 /** Set the callbacks */ 110 public void setCallbacks(TaskCallbacks cb) { 111 mCb = cb; 112 } 113 114 /** Set the grouping */ 115 public void setGroup(TaskGrouping group) { 116 if (group != null && this.group != null) { 117 throw new RuntimeException("This task is already assigned to a group."); 118 } 119 this.group = group; 120 } 121 122 /** Notifies the callback listeners that this task has been loaded */ 123 public void notifyTaskDataLoaded(Bitmap thumbnail, Drawable applicationIcon) { 124 this.applicationIcon = applicationIcon; 125 this.thumbnail = thumbnail; 126 if (mCb != null) { 127 mCb.onTaskDataLoaded(); 128 } 129 } 130 131 /** Notifies the callback listeners that this task has been unloaded */ 132 public void notifyTaskDataUnloaded(Bitmap defaultThumbnail, Drawable defaultApplicationIcon) { 133 applicationIcon = defaultApplicationIcon; 134 thumbnail = defaultThumbnail; 135 if (mCb != null) { 136 mCb.onTaskDataUnloaded(); 137 } 138 } 139 140 @Override 141 public boolean equals(Object o) { 142 // Check that the id matches 143 Task t = (Task) o; 144 return key.equals(t.key); 145 } 146 147 @Override 148 public String toString() { 149 String groupAffiliation = "no group"; 150 if (group != null) { 151 groupAffiliation = group.affiliation; 152 } 153 return "Task (" + groupAffiliation + "): " + key.baseIntent.getComponent().getPackageName() + 154 " [" + super.toString() + "]"; 155 } 156} 157