Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 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 | |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 17 | package com.android.systemui.recents.model; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 18 | |
| 19 | import android.app.ActivityManager; |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 20 | import android.content.ComponentCallbacks2; |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.pm.ActivityInfo; |
Winson Chung | 37c8d8e | 2014-03-24 14:53:07 -0700 | [diff] [blame] | 24 | import android.content.res.Resources; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 25 | import android.graphics.Bitmap; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 26 | import android.graphics.drawable.BitmapDrawable; |
| 27 | import android.graphics.drawable.Drawable; |
| 28 | import android.os.Handler; |
| 29 | import android.os.HandlerThread; |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 30 | import android.util.Log; |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 31 | import android.util.LruCache; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 32 | |
Jorim Jaggi | 81e0c84 | 2014-09-12 23:28:58 +0200 | [diff] [blame] | 33 | import com.android.systemui.R; |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 34 | import com.android.systemui.recents.Recents; |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 35 | import com.android.systemui.recents.RecentsConfiguration; |
Winson | c742f97 | 2015-11-12 11:32:21 -0800 | [diff] [blame] | 36 | import com.android.systemui.recents.RecentsDebugFlags; |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 37 | import com.android.systemui.recents.events.activity.PackagesChangedEvent; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 38 | import com.android.systemui.recents.misc.SystemServicesProxy; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 39 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 40 | import java.util.Map; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 41 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 42 | |
| 43 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 44 | /** |
| 45 | * A Task load queue |
| 46 | */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 47 | class TaskResourceLoadQueue { |
| 48 | ConcurrentLinkedQueue<Task> mQueue = new ConcurrentLinkedQueue<Task>(); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 49 | |
Winson Chung | 4c71aef | 2014-03-21 15:15:11 -0700 | [diff] [blame] | 50 | /** Adds a new task to the load queue */ |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 51 | void addTask(Task t) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 52 | if (!mQueue.contains(t)) { |
| 53 | mQueue.add(t); |
| 54 | } |
| 55 | synchronized(this) { |
| 56 | notifyAll(); |
| 57 | } |
| 58 | } |
| 59 | |
Winson Chung | 4c71aef | 2014-03-21 15:15:11 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Retrieves the next task from the load queue, as well as whether we want that task to be |
| 62 | * force reloaded. |
| 63 | */ |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 64 | Task nextTask() { |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 65 | return mQueue.poll(); |
Winson Chung | 4c71aef | 2014-03-21 15:15:11 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** Removes a task from the load queue */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 69 | void removeTask(Task t) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 70 | mQueue.remove(t); |
| 71 | } |
| 72 | |
Winson Chung | 4c71aef | 2014-03-21 15:15:11 -0700 | [diff] [blame] | 73 | /** Clears all the tasks from the load queue */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 74 | void clearTasks() { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 75 | mQueue.clear(); |
| 76 | } |
| 77 | |
Winson Chung | 4c71aef | 2014-03-21 15:15:11 -0700 | [diff] [blame] | 78 | /** Returns whether the load queue is empty */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 79 | boolean isEmpty() { |
| 80 | return mQueue.isEmpty(); |
| 81 | } |
| 82 | } |
| 83 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Task resource loader |
| 86 | */ |
| 87 | class BackgroundTaskLoader implements Runnable { |
Winson Chung | 96d7041 | 2014-11-12 14:17:17 -0800 | [diff] [blame] | 88 | static String TAG = "TaskResourceLoader"; |
| 89 | static boolean DEBUG = false; |
| 90 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 91 | Context mContext; |
| 92 | HandlerThread mLoadThread; |
| 93 | Handler mLoadThreadHandler; |
| 94 | Handler mMainThreadHandler; |
| 95 | |
| 96 | TaskResourceLoadQueue mLoadQueue; |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 97 | TaskKeyLruCache<Drawable> mIconCache; |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 98 | TaskKeyLruCache<ThumbnailData> mThumbnailCache; |
Winson Chung | c9567c0 | 2014-06-16 20:25:51 -0700 | [diff] [blame] | 99 | Bitmap mDefaultThumbnail; |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 100 | BitmapDrawable mDefaultIcon; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 101 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 102 | boolean mCancelled; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 103 | boolean mWaitingOnLoadQueue; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 104 | |
| 105 | /** Constructor, creates a new loading thread that loads task resources in the background */ |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 106 | public BackgroundTaskLoader(TaskResourceLoadQueue loadQueue, |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 107 | TaskKeyLruCache<Drawable> iconCache, TaskKeyLruCache<ThumbnailData> thumbnailCache, |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 108 | Bitmap defaultThumbnail, BitmapDrawable defaultIcon) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 109 | mLoadQueue = loadQueue; |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 110 | mIconCache = iconCache; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 111 | mThumbnailCache = thumbnailCache; |
Winson Chung | c9567c0 | 2014-06-16 20:25:51 -0700 | [diff] [blame] | 112 | mDefaultThumbnail = defaultThumbnail; |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 113 | mDefaultIcon = defaultIcon; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 114 | mMainThreadHandler = new Handler(); |
Jorim Jaggi | 81e0c84 | 2014-09-12 23:28:58 +0200 | [diff] [blame] | 115 | mLoadThread = new HandlerThread("Recents-TaskResourceLoader", |
Jorim Jaggi | 50949ea | 2014-09-15 16:07:29 +0200 | [diff] [blame] | 116 | android.os.Process.THREAD_PRIORITY_BACKGROUND); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 117 | mLoadThread.start(); |
| 118 | mLoadThreadHandler = new Handler(mLoadThread.getLooper()); |
| 119 | mLoadThreadHandler.post(this); |
| 120 | } |
| 121 | |
| 122 | /** Restarts the loader thread */ |
| 123 | void start(Context context) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 124 | mContext = context; |
| 125 | mCancelled = false; |
| 126 | // Notify the load thread to start loading |
| 127 | synchronized(mLoadThread) { |
| 128 | mLoadThread.notifyAll(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** Requests the loader thread to stop after the current iteration */ |
| 133 | void stop() { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 134 | // Mark as cancelled for the thread to pick up |
| 135 | mCancelled = true; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 136 | // If we are waiting for the load queue for more tasks, then we can just reset the |
| 137 | // Context now, since nothing is using it |
| 138 | if (mWaitingOnLoadQueue) { |
| 139 | mContext = null; |
| 140 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void run() { |
| 145 | while (true) { |
Winson Chung | 10f8139 | 2014-05-20 16:21:31 -0700 | [diff] [blame] | 146 | if (mCancelled) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 147 | // We have to unset the context here, since the background thread may be using it |
| 148 | // when we call stop() |
| 149 | mContext = null; |
| 150 | // If we are cancelled, then wait until we are started again |
| 151 | synchronized(mLoadThread) { |
| 152 | try { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 153 | mLoadThread.wait(); |
| 154 | } catch (InterruptedException ie) { |
| 155 | ie.printStackTrace(); |
| 156 | } |
| 157 | } |
| 158 | } else { |
Winson | 53ec42c | 2015-10-28 15:55:35 -0700 | [diff] [blame] | 159 | RecentsConfiguration config = Recents.getConfiguration(); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 160 | SystemServicesProxy ssp = Recents.getSystemServices(); |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 161 | // If we've stopped the loader, then fall through to the above logic to wait on |
winsonchung | f14fdda | 2014-10-27 12:01:14 -0700 | [diff] [blame] | 162 | // the load thread |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 163 | if (ssp != null) { |
| 164 | // Load the next item from the queue |
| 165 | final Task t = mLoadQueue.nextTask(); |
| 166 | if (t != null) { |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 167 | Drawable cachedIcon = mIconCache.get(t.key); |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 168 | ThumbnailData cachedThumbnailData = mThumbnailCache.get(t.key); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 169 | |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 170 | // Load the icon if it is stale or we haven't cached one yet |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 171 | if (cachedIcon == null) { |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 172 | cachedIcon = ssp.getBadgedTaskDescriptionIcon(t.taskDescription, |
| 173 | t.key.userId, mContext.getResources()); |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 174 | |
| 175 | if (cachedIcon == null) { |
| 176 | ActivityInfo info = ssp.getActivityInfo( |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 177 | t.key.getComponent(), t.key.userId); |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 178 | if (info != null) { |
| 179 | if (DEBUG) Log.d(TAG, "Loading icon: " + t.key); |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 180 | cachedIcon = ssp.getBadgedActivityIcon(info, t.key.userId); |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 181 | } |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 182 | } |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 183 | |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 184 | if (cachedIcon == null) { |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 185 | cachedIcon = mDefaultIcon; |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 186 | } |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 187 | |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 188 | // At this point, even if we can't load the icon, we will set the |
| 189 | // default icon. |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 190 | mIconCache.put(t.key, cachedIcon); |
Winson Chung | 96d7041 | 2014-11-12 14:17:17 -0800 | [diff] [blame] | 191 | } |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 192 | // Load the thumbnail if it is stale or we haven't cached one yet |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 193 | if (cachedThumbnailData == null) { |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 194 | if (config.svelteLevel < RecentsConfiguration.SVELTE_DISABLE_LOADING) { |
| 195 | if (DEBUG) Log.d(TAG, "Loading thumbnail: " + t.key); |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 196 | cachedThumbnailData = ssp.getTaskThumbnail(t.key.id); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 197 | } |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 198 | |
| 199 | if (cachedThumbnailData.thumbnail == null) { |
| 200 | cachedThumbnailData.thumbnail = mDefaultThumbnail; |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 201 | } |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 202 | |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 203 | // When svelte, we trim the memory to just the visible thumbnails when |
| 204 | // leaving, so don't thrash the cache as the user scrolls (just load |
| 205 | // them from scratch each time) |
| 206 | if (config.svelteLevel < RecentsConfiguration.SVELTE_LIMIT_CACHE) { |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 207 | mThumbnailCache.put(t.key, cachedThumbnailData); |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | if (!mCancelled) { |
| 211 | // Notify that the task data has changed |
| 212 | final Drawable newIcon = cachedIcon; |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 213 | final ThumbnailData newThumbnailData = cachedThumbnailData; |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 214 | mMainThreadHandler.post(new Runnable() { |
| 215 | @Override |
| 216 | public void run() { |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 217 | t.notifyTaskDataLoaded(newThumbnailData.thumbnail, newIcon, |
| 218 | newThumbnailData.thumbnailInfo); |
Winson Chung | 66e86f9 | 2014-11-18 10:35:50 -0800 | [diff] [blame] | 219 | } |
| 220 | }); |
| 221 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | // If there are no other items in the list, then just wait until something is added |
| 226 | if (!mCancelled && mLoadQueue.isEmpty()) { |
| 227 | synchronized(mLoadQueue) { |
| 228 | try { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 229 | mWaitingOnLoadQueue = true; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 230 | mLoadQueue.wait(); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 231 | mWaitingOnLoadQueue = false; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 232 | } catch (InterruptedException ie) { |
| 233 | ie.printStackTrace(); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 242 | /** |
| 243 | * Recents task loader |
| 244 | */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 245 | public class RecentsTaskLoader { |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 246 | |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 247 | private static final String TAG = "RecentsTaskLoader"; |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 248 | private static final boolean DEBUG = false; |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 249 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 250 | // This activity info LruCache is useful because it can be expensive to retrieve ActivityInfos |
| 251 | // for many tasks, which we use to get the activity labels and icons. Unlike the other caches |
| 252 | // below, this is per-package so we can't invalidate the items in the cache based on the last |
| 253 | // active time. Instead, we rely on the RecentsPackageMonitor to keep us informed whenever a |
| 254 | // package in the cache has been updated, so that we may remove it. |
| 255 | private final LruCache<ComponentName, ActivityInfo> mActivityInfoCache; |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 256 | private final TaskKeyLruCache<Drawable> mIconCache; |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 257 | private final TaskKeyLruCache<ThumbnailData> mThumbnailCache; |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 258 | private final TaskKeyLruCache<String> mActivityLabelCache; |
| 259 | private final TaskKeyLruCache<String> mContentDescriptionCache; |
| 260 | private final TaskResourceLoadQueue mLoadQueue; |
| 261 | private final BackgroundTaskLoader mLoader; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 262 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 263 | private final int mMaxThumbnailCacheSize; |
| 264 | private final int mMaxIconCacheSize; |
| 265 | private int mNumVisibleTasksLoaded; |
| 266 | private int mNumVisibleThumbnailsLoaded; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 267 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 268 | int mDefaultTaskBarBackgroundColor; |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 269 | int mDefaultTaskViewBackgroundColor; |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 270 | BitmapDrawable mDefaultIcon; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 271 | Bitmap mDefaultThumbnail; |
| 272 | |
Winson | a1b96b5 | 2016-03-14 18:58:43 -0700 | [diff] [blame] | 273 | private TaskKeyLruCache.EvictionCallback mClearActivityInfoOnEviction = |
| 274 | new TaskKeyLruCache.EvictionCallback() { |
| 275 | @Override |
| 276 | public void onEntryEvicted(Task.TaskKey key) { |
Winson | a1ededd | 2016-03-25 12:23:12 -0700 | [diff] [blame] | 277 | if (key != null) { |
| 278 | mActivityInfoCache.remove(key.getComponent()); |
| 279 | } |
Winson | a1b96b5 | 2016-03-14 18:58:43 -0700 | [diff] [blame] | 280 | } |
| 281 | }; |
| 282 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 283 | public RecentsTaskLoader(Context context) { |
| 284 | Resources res = context.getResources(); |
| 285 | mDefaultTaskBarBackgroundColor = |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 286 | context.getColor(R.color.recents_task_bar_default_background_color); |
| 287 | mDefaultTaskViewBackgroundColor = |
| 288 | context.getColor(R.color.recents_task_view_default_background_color); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 289 | mMaxThumbnailCacheSize = res.getInteger(R.integer.config_recents_max_thumbnail_count); |
| 290 | mMaxIconCacheSize = res.getInteger(R.integer.config_recents_max_icon_count); |
Winson | c742f97 | 2015-11-12 11:32:21 -0800 | [diff] [blame] | 291 | int iconCacheSize = RecentsDebugFlags.Static.DisableBackgroundCache ? 1 : |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 292 | mMaxIconCacheSize; |
Winson | c742f97 | 2015-11-12 11:32:21 -0800 | [diff] [blame] | 293 | int thumbnailCacheSize = RecentsDebugFlags.Static.DisableBackgroundCache ? 1 : |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 294 | mMaxThumbnailCacheSize; |
| 295 | |
Winson Chung | 7aceb9a | 2014-07-03 13:38:01 -0700 | [diff] [blame] | 296 | // Create the default assets |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 297 | Bitmap icon = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8); |
| 298 | icon.eraseColor(0); |
Winson Chung | 7aceb9a | 2014-07-03 13:38:01 -0700 | [diff] [blame] | 299 | mDefaultThumbnail = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); |
| 300 | mDefaultThumbnail.setHasAlpha(false); |
| 301 | mDefaultThumbnail.eraseColor(0xFFffffff); |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 302 | mDefaultIcon = new BitmapDrawable(context.getResources(), icon); |
Winson Chung | 7aceb9a | 2014-07-03 13:38:01 -0700 | [diff] [blame] | 303 | |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 304 | // Initialize the proxy, cache and loaders |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 305 | int numRecentTasks = ActivityManager.getMaxRecentTasksStatic(); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 306 | mLoadQueue = new TaskResourceLoadQueue(); |
Winson | a1b96b5 | 2016-03-14 18:58:43 -0700 | [diff] [blame] | 307 | mIconCache = new TaskKeyLruCache<>(iconCacheSize, mClearActivityInfoOnEviction); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 308 | mThumbnailCache = new TaskKeyLruCache<>(thumbnailCacheSize); |
Winson | a1b96b5 | 2016-03-14 18:58:43 -0700 | [diff] [blame] | 309 | mActivityLabelCache = new TaskKeyLruCache<>(numRecentTasks, mClearActivityInfoOnEviction); |
| 310 | mContentDescriptionCache = new TaskKeyLruCache<>(numRecentTasks, |
| 311 | mClearActivityInfoOnEviction); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 312 | mActivityInfoCache = new LruCache(numRecentTasks); |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 313 | mLoader = new BackgroundTaskLoader(mLoadQueue, mIconCache, mThumbnailCache, |
| 314 | mDefaultThumbnail, mDefaultIcon); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Winson Chung | a91c293 | 2014-11-07 15:02:38 -0800 | [diff] [blame] | 317 | /** Returns the size of the app icon cache. */ |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 318 | public int getIconCacheSize() { |
Winson Chung | a91c293 | 2014-11-07 15:02:38 -0800 | [diff] [blame] | 319 | return mMaxIconCacheSize; |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Winson Chung | 90d5136e | 2014-11-13 14:30:26 -0800 | [diff] [blame] | 322 | /** Returns the size of the thumbnail cache. */ |
| 323 | public int getThumbnailCacheSize() { |
| 324 | return mMaxThumbnailCacheSize; |
| 325 | } |
| 326 | |
| 327 | /** Creates a new plan for loading the recent tasks. */ |
Winson Chung | a91c293 | 2014-11-07 15:02:38 -0800 | [diff] [blame] | 328 | public RecentsTaskLoadPlan createLoadPlan(Context context) { |
Winson | 53ec42c | 2015-10-28 15:55:35 -0700 | [diff] [blame] | 329 | RecentsTaskLoadPlan plan = new RecentsTaskLoadPlan(context); |
Winson Chung | a91c293 | 2014-11-07 15:02:38 -0800 | [diff] [blame] | 330 | return plan; |
| 331 | } |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 332 | |
Winson Chung | 90d5136e | 2014-11-13 14:30:26 -0800 | [diff] [blame] | 333 | /** Preloads recents tasks using the specified plan to store the output. */ |
Winson | 65c851e | 2016-01-20 12:43:35 -0800 | [diff] [blame] | 334 | public void preloadTasks(RecentsTaskLoadPlan plan, int topTaskId, boolean isTopTaskHome) { |
| 335 | plan.preloadPlan(this, topTaskId, isTopTaskHome); |
Winson Chung | a91c293 | 2014-11-07 15:02:38 -0800 | [diff] [blame] | 336 | } |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 337 | |
Winson Chung | 90d5136e | 2014-11-13 14:30:26 -0800 | [diff] [blame] | 338 | /** Begins loading the heavy task data according to the specified options. */ |
Winson Chung | a91c293 | 2014-11-07 15:02:38 -0800 | [diff] [blame] | 339 | public void loadTasks(Context context, RecentsTaskLoadPlan plan, |
| 340 | RecentsTaskLoadPlan.Options opts) { |
| 341 | if (opts == null) { |
| 342 | throw new RuntimeException("Requires load options"); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 343 | } |
Winson Chung | 96d7041 | 2014-11-12 14:17:17 -0800 | [diff] [blame] | 344 | plan.executePlan(opts, this, mLoadQueue); |
Winson Chung | 90d5136e | 2014-11-13 14:30:26 -0800 | [diff] [blame] | 345 | if (!opts.onlyLoadForCache) { |
Winson Chung | a91c293 | 2014-11-07 15:02:38 -0800 | [diff] [blame] | 346 | mNumVisibleTasksLoaded = opts.numVisibleTasks; |
Winson Chung | 96d7041 | 2014-11-12 14:17:17 -0800 | [diff] [blame] | 347 | mNumVisibleThumbnailsLoaded = opts.numVisibleTaskThumbnails; |
Winson Chung | a91c293 | 2014-11-07 15:02:38 -0800 | [diff] [blame] | 348 | |
Winson Chung | 90d5136e | 2014-11-13 14:30:26 -0800 | [diff] [blame] | 349 | // Start the loader |
| 350 | mLoader.start(context); |
| 351 | } |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Winson | 6d5b8b8 | 2016-01-26 17:15:45 -0800 | [diff] [blame] | 354 | /** |
| 355 | * Acquires the task resource data directly from the cache, loading if necessary. |
| 356 | * |
| 357 | * @param fetchAndInvalidateThumbnails If set, will try loading thumbnails, invalidating them |
| 358 | * in the cache and loading if necessary. Otherwise, do not |
| 359 | * load the thumbnail unless the icon also has to be loaded. |
| 360 | */ |
| 361 | public void loadTaskData(Task t, boolean fetchAndInvalidateThumbnails) { |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 362 | Drawable icon = mIconCache.getAndInvalidateIfModified(t.key); |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 363 | Bitmap thumbnail = null; |
| 364 | ActivityManager.TaskThumbnailInfo thumbnailInfo = null; |
Winson | 6d5b8b8 | 2016-01-26 17:15:45 -0800 | [diff] [blame] | 365 | if (fetchAndInvalidateThumbnails) { |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 366 | ThumbnailData thumbnailData = mThumbnailCache.getAndInvalidateIfModified(t.key); |
| 367 | if (thumbnailData != null) { |
| 368 | thumbnail = thumbnailData.thumbnail; |
| 369 | thumbnailInfo = thumbnailData.thumbnailInfo; |
| 370 | } |
| 371 | } else { |
| 372 | thumbnail = mDefaultThumbnail; |
Winson | 6d5b8b8 | 2016-01-26 17:15:45 -0800 | [diff] [blame] | 373 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 374 | |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 375 | // Grab the thumbnail/icon from the cache, if either don't exist, then trigger a reload and |
| 376 | // use the default assets in their place until they load |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 377 | boolean requiresLoad = (icon == null) || (thumbnail == null); |
| 378 | icon = icon != null ? icon : mDefaultIcon; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 379 | if (requiresLoad) { |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 380 | mLoadQueue.addTask(t); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 381 | } |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 382 | t.notifyTaskDataLoaded(thumbnail == mDefaultThumbnail ? null : thumbnail, icon, |
| 383 | thumbnailInfo); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 386 | /** Releases the task resource data back into the pool. */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 387 | public void unloadTaskData(Task t) { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 388 | mLoadQueue.removeTask(t); |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 389 | t.notifyTaskDataUnloaded(null, mDefaultIcon); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 392 | /** Completely removes the resource data from the pool. */ |
Winson Chung | 5393dff | 2014-05-08 14:25:43 -0700 | [diff] [blame] | 393 | public void deleteTaskData(Task t, boolean notifyTaskDataUnloaded) { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 394 | mLoadQueue.removeTask(t); |
| 395 | mThumbnailCache.remove(t.key); |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 396 | mIconCache.remove(t.key); |
Winson | 6d5b8b8 | 2016-01-26 17:15:45 -0800 | [diff] [blame] | 397 | mActivityLabelCache.remove(t.key); |
| 398 | mContentDescriptionCache.remove(t.key); |
Winson Chung | 5393dff | 2014-05-08 14:25:43 -0700 | [diff] [blame] | 399 | if (notifyTaskDataUnloaded) { |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 400 | t.notifyTaskDataUnloaded(null, mDefaultIcon); |
Winson Chung | 5393dff | 2014-05-08 14:25:43 -0700 | [diff] [blame] | 401 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Winson Chung | 9f49df9 | 2014-05-07 18:08:34 -0700 | [diff] [blame] | 404 | /** |
| 405 | * Handles signals from the system, trimming memory when requested to prevent us from running |
| 406 | * out of memory. |
| 407 | */ |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 408 | public void onTrimMemory(int level) { |
Winson | 53ec42c | 2015-10-28 15:55:35 -0700 | [diff] [blame] | 409 | RecentsConfiguration config = Recents.getConfiguration(); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 410 | switch (level) { |
| 411 | case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN: |
Winson Chung | 47c4c69 | 2014-03-17 10:17:11 -0700 | [diff] [blame] | 412 | // Stop the loader immediately when the UI is no longer visible |
| 413 | stopLoader(); |
Winson Chung | 96d7041 | 2014-11-12 14:17:17 -0800 | [diff] [blame] | 414 | if (config.svelteLevel == RecentsConfiguration.SVELTE_NONE) { |
| 415 | mThumbnailCache.trimToSize(Math.max(mNumVisibleTasksLoaded, |
| 416 | mMaxThumbnailCacheSize / 2)); |
| 417 | } else if (config.svelteLevel == RecentsConfiguration.SVELTE_LIMIT_CACHE) { |
| 418 | mThumbnailCache.trimToSize(mNumVisibleThumbnailsLoaded); |
| 419 | } else if (config.svelteLevel >= RecentsConfiguration.SVELTE_DISABLE_CACHE) { |
| 420 | mThumbnailCache.evictAll(); |
| 421 | } |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 422 | mIconCache.trimToSize(Math.max(mNumVisibleTasksLoaded, |
Jorim Jaggi | 81e0c84 | 2014-09-12 23:28:58 +0200 | [diff] [blame] | 423 | mMaxIconCacheSize / 2)); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 424 | break; |
| 425 | case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE: |
| 426 | case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND: |
| 427 | // We are leaving recents, so trim the data a bit |
Winson Chung | a91c293 | 2014-11-07 15:02:38 -0800 | [diff] [blame] | 428 | mThumbnailCache.trimToSize(Math.max(1, mMaxThumbnailCacheSize / 2)); |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 429 | mIconCache.trimToSize(Math.max(1, mMaxIconCacheSize / 2)); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 430 | mActivityInfoCache.trimToSize(Math.max(1, |
| 431 | ActivityManager.getMaxRecentTasksStatic() / 2)); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 432 | break; |
| 433 | case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW: |
| 434 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 435 | // We are going to be low on memory |
Winson Chung | a91c293 | 2014-11-07 15:02:38 -0800 | [diff] [blame] | 436 | mThumbnailCache.trimToSize(Math.max(1, mMaxThumbnailCacheSize / 4)); |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 437 | mIconCache.trimToSize(Math.max(1, mMaxIconCacheSize / 4)); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 438 | mActivityInfoCache.trimToSize(Math.max(1, |
| 439 | ActivityManager.getMaxRecentTasksStatic() / 4)); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 440 | break; |
| 441 | case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL: |
| 442 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 443 | // We are low on memory, so release everything |
| 444 | mThumbnailCache.evictAll(); |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 445 | mIconCache.evictAll(); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 446 | mActivityInfoCache.evictAll(); |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 447 | // The cache is small, only clear the label cache when we are critical |
| 448 | mActivityLabelCache.evictAll(); |
Benjamin Franz | b7a42fd | 2015-04-23 15:33:32 +0100 | [diff] [blame] | 449 | mContentDescriptionCache.evictAll(); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 450 | break; |
| 451 | default: |
| 452 | break; |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 455 | |
| 456 | /** |
| 457 | * Returns the cached task label if the task key is not expired, updating the cache if it is. |
| 458 | */ |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 459 | String getAndUpdateActivityTitle(Task.TaskKey taskKey, ActivityManager.TaskDescription td) { |
| 460 | SystemServicesProxy ssp = Recents.getSystemServices(); |
| 461 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 462 | // Return the task description label if it exists |
| 463 | if (td != null && td.getLabel() != null) { |
| 464 | return td.getLabel(); |
| 465 | } |
| 466 | // Return the cached activity label if it exists |
| 467 | String label = mActivityLabelCache.getAndInvalidateIfModified(taskKey); |
| 468 | if (label != null) { |
| 469 | return label; |
| 470 | } |
| 471 | // All short paths failed, load the label from the activity info and cache it |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 472 | ActivityInfo activityInfo = getAndUpdateActivityInfo(taskKey); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 473 | if (activityInfo != null) { |
Winson | c5ef63f | 2016-01-21 14:39:23 -0800 | [diff] [blame] | 474 | label = ssp.getBadgedActivityLabel(activityInfo, taskKey.userId); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 475 | mActivityLabelCache.put(taskKey, label); |
| 476 | return label; |
| 477 | } |
| 478 | // If the activity info does not exist or fails to load, return an empty label for now, |
| 479 | // but do not cache it |
| 480 | return ""; |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Returns the cached task content description if the task key is not expired, updating the |
| 485 | * cache if it is. |
| 486 | */ |
Winson | c5ef63f | 2016-01-21 14:39:23 -0800 | [diff] [blame] | 487 | String getAndUpdateContentDescription(Task.TaskKey taskKey, Resources res) { |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 488 | SystemServicesProxy ssp = Recents.getSystemServices(); |
| 489 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 490 | // Return the cached content description if it exists |
| 491 | String label = mContentDescriptionCache.getAndInvalidateIfModified(taskKey); |
| 492 | if (label != null) { |
| 493 | return label; |
| 494 | } |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 495 | |
Winson | c5ef63f | 2016-01-21 14:39:23 -0800 | [diff] [blame] | 496 | // All short paths failed, load the label from the activity info and cache it |
| 497 | ActivityInfo activityInfo = getAndUpdateActivityInfo(taskKey); |
| 498 | if (activityInfo != null) { |
| 499 | label = ssp.getBadgedContentDescription(activityInfo, taskKey.userId, res); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 500 | mContentDescriptionCache.put(taskKey, label); |
| 501 | return label; |
| 502 | } |
| 503 | // If the content description does not exist, return an empty label for now, but do not |
| 504 | // cache it |
| 505 | return ""; |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Returns the cached task icon if the task key is not expired, updating the cache if it is. |
| 510 | */ |
| 511 | Drawable getAndUpdateActivityIcon(Task.TaskKey taskKey, ActivityManager.TaskDescription td, |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 512 | Resources res, boolean loadIfNotCached) { |
| 513 | SystemServicesProxy ssp = Recents.getSystemServices(); |
| 514 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 515 | // Return the cached activity icon if it exists |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 516 | Drawable icon = mIconCache.getAndInvalidateIfModified(taskKey); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 517 | if (icon != null) { |
| 518 | return icon; |
| 519 | } |
| 520 | |
| 521 | if (loadIfNotCached) { |
| 522 | // Return and cache the task description icon if it exists |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 523 | icon = ssp.getBadgedTaskDescriptionIcon(td, taskKey.userId, res); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 524 | if (icon != null) { |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 525 | mIconCache.put(taskKey, icon); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 526 | return icon; |
| 527 | } |
| 528 | |
| 529 | // Load the icon from the activity info and cache it |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 530 | ActivityInfo activityInfo = getAndUpdateActivityInfo(taskKey); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 531 | if (activityInfo != null) { |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 532 | icon = ssp.getBadgedActivityIcon(activityInfo, taskKey.userId); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 533 | if (icon != null) { |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 534 | mIconCache.put(taskKey, icon); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 535 | return icon; |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | // We couldn't load any icon |
| 540 | return null; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Returns the cached thumbnail if the task key is not expired, updating the cache if it is. |
| 545 | */ |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 546 | Bitmap getAndUpdateThumbnail(Task.TaskKey taskKey, boolean loadIfNotCached) { |
| 547 | SystemServicesProxy ssp = Recents.getSystemServices(); |
| 548 | |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 549 | // Return the cached thumbnail if it exists |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 550 | ThumbnailData thumbnailData = mThumbnailCache.getAndInvalidateIfModified(taskKey); |
| 551 | if (thumbnailData != null) { |
| 552 | return thumbnailData.thumbnail; |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | if (loadIfNotCached) { |
Winson | 53ec42c | 2015-10-28 15:55:35 -0700 | [diff] [blame] | 556 | RecentsConfiguration config = Recents.getConfiguration(); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 557 | if (config.svelteLevel < RecentsConfiguration.SVELTE_DISABLE_LOADING) { |
| 558 | // Load the thumbnail from the system |
Winson | 2170093 | 2016-03-24 17:26:23 -0700 | [diff] [blame] | 559 | thumbnailData = ssp.getTaskThumbnail(taskKey.id); |
| 560 | if (thumbnailData.thumbnail != null) { |
| 561 | mThumbnailCache.put(taskKey, thumbnailData); |
| 562 | return thumbnailData.thumbnail; |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | } |
| 566 | // We couldn't load any thumbnail |
| 567 | return null; |
| 568 | } |
| 569 | |
| 570 | /** |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 571 | * Returns the task's primary color if possible, defaulting to the default color if there is |
| 572 | * no specified primary color. |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 573 | */ |
| 574 | int getActivityPrimaryColor(ActivityManager.TaskDescription td) { |
| 575 | if (td != null && td.getPrimaryColor() != 0) { |
| 576 | return td.getPrimaryColor(); |
| 577 | } |
| 578 | return mDefaultTaskBarBackgroundColor; |
| 579 | } |
| 580 | |
| 581 | /** |
Winson Chung | 1af8eda | 2016-02-05 17:55:56 +0000 | [diff] [blame] | 582 | * Returns the task's background color if possible. |
| 583 | */ |
| 584 | int getActivityBackgroundColor(ActivityManager.TaskDescription td) { |
| 585 | if (td != null && td.getBackgroundColor() != 0) { |
| 586 | return td.getBackgroundColor(); |
| 587 | } |
| 588 | return mDefaultTaskViewBackgroundColor; |
| 589 | } |
| 590 | |
| 591 | /** |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 592 | * Returns the activity info for the given task key, retrieving one from the system if the |
| 593 | * task key is expired. |
| 594 | */ |
Winson | 8be1634 | 2016-02-09 11:53:27 -0800 | [diff] [blame] | 595 | ActivityInfo getAndUpdateActivityInfo(Task.TaskKey taskKey) { |
Winson Chung | 296278a | 2015-12-17 12:09:02 -0500 | [diff] [blame] | 596 | SystemServicesProxy ssp = Recents.getSystemServices(); |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 597 | ComponentName cn = taskKey.getComponent(); |
| 598 | ActivityInfo activityInfo = mActivityInfoCache.get(cn); |
| 599 | if (activityInfo == null) { |
| 600 | activityInfo = ssp.getActivityInfo(cn, taskKey.userId); |
Winson | eb1b65a | 2015-11-02 10:29:08 -0800 | [diff] [blame] | 601 | if (cn == null || activityInfo == null) { |
| 602 | Log.e(TAG, "Unexpected null component name or activity info: " + cn + ", " + |
| 603 | activityInfo); |
| 604 | return null; |
| 605 | } |
Winson | e7f138c | 2015-10-22 16:15:21 -0700 | [diff] [blame] | 606 | mActivityInfoCache.put(cn, activityInfo); |
| 607 | } |
| 608 | return activityInfo; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Stops the task loader and clears all queued, pending task loads. |
| 613 | */ |
| 614 | private void stopLoader() { |
| 615 | mLoader.stop(); |
| 616 | mLoadQueue.clearTasks(); |
| 617 | } |
| 618 | |
| 619 | /**** Event Bus Events ****/ |
| 620 | |
| 621 | public final void onBusEvent(PackagesChangedEvent event) { |
| 622 | // Remove all the cached activity infos for this package. The other caches do not need to |
| 623 | // be pruned at this time, as the TaskKey expiration checks will flush them next time their |
| 624 | // cached contents are requested |
| 625 | Map<ComponentName, ActivityInfo> activityInfoCache = mActivityInfoCache.snapshot(); |
| 626 | for (ComponentName cn : activityInfoCache.keySet()) { |
| 627 | if (cn.getPackageName().equals(event.packageName)) { |
| 628 | if (DEBUG) { |
| 629 | Log.d(TAG, "Removing activity info from cache: " + cn); |
| 630 | } |
| 631 | mActivityInfoCache.remove(cn); |
| 632 | } |
| 633 | } |
| 634 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 635 | } |