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 Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.pm.ActivityInfo; |
Winson Chung | 37c8d8e | 2014-03-24 14:53:07 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 25 | import android.graphics.drawable.BitmapDrawable; |
| 26 | import android.graphics.drawable.Drawable; |
| 27 | import android.os.Handler; |
| 28 | import android.os.HandlerThread; |
| 29 | import android.os.UserHandle; |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 30 | import android.util.Log; |
| 31 | |
Jorim Jaggi | 81e0c84 | 2014-09-12 23:28:58 +0200 | [diff] [blame] | 32 | import com.android.systemui.R; |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 33 | import com.android.systemui.recents.Constants; |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 34 | import com.android.systemui.recents.RecentsConfiguration; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 35 | import com.android.systemui.recents.misc.SystemServicesProxy; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 36 | |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 38 | import java.util.Collection; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 39 | import java.util.Collections; |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 40 | import java.util.HashMap; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 41 | import java.util.List; |
| 42 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 43 | |
| 44 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 45 | /** Handle to an ActivityInfo */ |
| 46 | class ActivityInfoHandle { |
| 47 | ActivityInfo info; |
| 48 | } |
| 49 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 50 | /** A bitmap load queue */ |
| 51 | class TaskResourceLoadQueue { |
| 52 | ConcurrentLinkedQueue<Task> mQueue = new ConcurrentLinkedQueue<Task>(); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 53 | |
Winson Chung | 4c71aef | 2014-03-21 15:15:11 -0700 | [diff] [blame] | 54 | /** Adds a new task to the load queue */ |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 55 | void addTasks(Collection<Task> tasks) { |
| 56 | for (Task t : tasks) { |
| 57 | if (!mQueue.contains(t)) { |
| 58 | mQueue.add(t); |
| 59 | } |
| 60 | } |
| 61 | synchronized(this) { |
| 62 | notifyAll(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** Adds a new task to the load queue */ |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 67 | void addTask(Task t) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 68 | if (!mQueue.contains(t)) { |
| 69 | mQueue.add(t); |
| 70 | } |
| 71 | synchronized(this) { |
| 72 | notifyAll(); |
| 73 | } |
| 74 | } |
| 75 | |
Winson Chung | 4c71aef | 2014-03-21 15:15:11 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Retrieves the next task from the load queue, as well as whether we want that task to be |
| 78 | * force reloaded. |
| 79 | */ |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 80 | Task nextTask() { |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 81 | return mQueue.poll(); |
Winson Chung | 4c71aef | 2014-03-21 15:15:11 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** Removes a task from the load queue */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 85 | void removeTask(Task t) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 86 | mQueue.remove(t); |
| 87 | } |
| 88 | |
Winson Chung | 4c71aef | 2014-03-21 15:15:11 -0700 | [diff] [blame] | 89 | /** Clears all the tasks from the load queue */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 90 | void clearTasks() { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 91 | mQueue.clear(); |
| 92 | } |
| 93 | |
Winson Chung | 4c71aef | 2014-03-21 15:15:11 -0700 | [diff] [blame] | 94 | /** Returns whether the load queue is empty */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 95 | boolean isEmpty() { |
| 96 | return mQueue.isEmpty(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* Task resource loader */ |
| 101 | class TaskResourceLoader implements Runnable { |
| 102 | Context mContext; |
| 103 | HandlerThread mLoadThread; |
| 104 | Handler mLoadThreadHandler; |
| 105 | Handler mMainThreadHandler; |
| 106 | |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 107 | SystemServicesProxy mSystemServicesProxy; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 108 | TaskResourceLoadQueue mLoadQueue; |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 109 | DrawableLruCache mApplicationIconCache; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 110 | BitmapLruCache mThumbnailCache; |
Winson Chung | c9567c0 | 2014-06-16 20:25:51 -0700 | [diff] [blame] | 111 | Bitmap mDefaultThumbnail; |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 112 | BitmapDrawable mDefaultApplicationIcon; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 113 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 114 | boolean mCancelled; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 115 | boolean mWaitingOnLoadQueue; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 116 | |
| 117 | /** Constructor, creates a new loading thread that loads task resources in the background */ |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 118 | public TaskResourceLoader(TaskResourceLoadQueue loadQueue, DrawableLruCache applicationIconCache, |
| 119 | BitmapLruCache thumbnailCache, Bitmap defaultThumbnail, |
| 120 | BitmapDrawable defaultApplicationIcon) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 121 | mLoadQueue = loadQueue; |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 122 | mApplicationIconCache = applicationIconCache; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 123 | mThumbnailCache = thumbnailCache; |
Winson Chung | c9567c0 | 2014-06-16 20:25:51 -0700 | [diff] [blame] | 124 | mDefaultThumbnail = defaultThumbnail; |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 125 | mDefaultApplicationIcon = defaultApplicationIcon; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 126 | mMainThreadHandler = new Handler(); |
Jorim Jaggi | 81e0c84 | 2014-09-12 23:28:58 +0200 | [diff] [blame] | 127 | mLoadThread = new HandlerThread("Recents-TaskResourceLoader", |
Jorim Jaggi | 50949ea | 2014-09-15 16:07:29 +0200 | [diff] [blame] | 128 | android.os.Process.THREAD_PRIORITY_BACKGROUND); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 129 | mLoadThread.start(); |
| 130 | mLoadThreadHandler = new Handler(mLoadThread.getLooper()); |
| 131 | mLoadThreadHandler.post(this); |
| 132 | } |
| 133 | |
| 134 | /** Restarts the loader thread */ |
| 135 | void start(Context context) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 136 | mContext = context; |
| 137 | mCancelled = false; |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 138 | mSystemServicesProxy = new SystemServicesProxy(context); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 139 | // Notify the load thread to start loading |
| 140 | synchronized(mLoadThread) { |
| 141 | mLoadThread.notifyAll(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** Requests the loader thread to stop after the current iteration */ |
| 146 | void stop() { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 147 | // Mark as cancelled for the thread to pick up |
| 148 | mCancelled = true; |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 149 | mSystemServicesProxy = null; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 150 | // If we are waiting for the load queue for more tasks, then we can just reset the |
| 151 | // Context now, since nothing is using it |
| 152 | if (mWaitingOnLoadQueue) { |
| 153 | mContext = null; |
| 154 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void run() { |
| 159 | while (true) { |
Winson Chung | 10f8139 | 2014-05-20 16:21:31 -0700 | [diff] [blame] | 160 | if (mCancelled) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 161 | // We have to unset the context here, since the background thread may be using it |
| 162 | // when we call stop() |
| 163 | mContext = null; |
| 164 | // If we are cancelled, then wait until we are started again |
| 165 | synchronized(mLoadThread) { |
| 166 | try { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 167 | mLoadThread.wait(); |
| 168 | } catch (InterruptedException ie) { |
| 169 | ie.printStackTrace(); |
| 170 | } |
| 171 | } |
| 172 | } else { |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 173 | SystemServicesProxy ssp = mSystemServicesProxy; |
| 174 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 175 | // Load the next item from the queue |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 176 | final Task t = mLoadQueue.nextTask(); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 177 | if (t != null) { |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 178 | Drawable cachedIcon = mApplicationIconCache.get(t.key); |
| 179 | Bitmap cachedThumbnail = mThumbnailCache.get(t.key); |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 180 | |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 181 | // Load the application icon if it is stale or we haven't cached one yet |
| 182 | if (cachedIcon == null) { |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 183 | cachedIcon = getTaskDescriptionIcon(t.key, t.icon, t.iconFilename, ssp, |
| 184 | mContext.getResources()); |
| 185 | |
| 186 | if (cachedIcon == null) { |
| 187 | ActivityInfo info = ssp.getActivityInfo(t.key.baseIntent.getComponent(), |
| 188 | t.key.userId); |
| 189 | if (info != null) { |
| 190 | cachedIcon = ssp.getActivityIcon(info, t.key.userId); |
| 191 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 192 | } |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 193 | |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 194 | if (cachedIcon == null) { |
| 195 | cachedIcon = mDefaultApplicationIcon; |
| 196 | } |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 197 | |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 198 | // At this point, even if we can't load the icon, we will set the default |
| 199 | // icon. |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 200 | mApplicationIconCache.put(t.key, cachedIcon); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 201 | } |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 202 | // Load the thumbnail if it is stale or we haven't cached one yet |
| 203 | if (cachedThumbnail == null) { |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 204 | cachedThumbnail = ssp.getTaskThumbnail(t.key.id); |
| 205 | if (cachedThumbnail != null) { |
| 206 | cachedThumbnail.setHasAlpha(false); |
| 207 | } else { |
| 208 | cachedThumbnail = mDefaultThumbnail; |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 209 | } |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 210 | mThumbnailCache.put(t.key, cachedThumbnail); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 211 | } |
| 212 | if (!mCancelled) { |
| 213 | // Notify that the task data has changed |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 214 | final Drawable newIcon = cachedIcon; |
Selim Cinek | e8199c5 | 2014-09-17 04:03:52 +0200 | [diff] [blame] | 215 | final Bitmap newThumbnail = cachedThumbnail == mDefaultThumbnail |
| 216 | ? null : cachedThumbnail; |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 217 | mMainThreadHandler.post(new Runnable() { |
| 218 | @Override |
| 219 | public void run() { |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 220 | t.notifyTaskDataLoaded(newThumbnail, newIcon); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 221 | } |
| 222 | }); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | // If there are no other items in the list, then just wait until something is added |
| 227 | if (!mCancelled && mLoadQueue.isEmpty()) { |
| 228 | synchronized(mLoadQueue) { |
| 229 | try { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 230 | mWaitingOnLoadQueue = true; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 231 | mLoadQueue.wait(); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 232 | mWaitingOnLoadQueue = false; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 233 | } catch (InterruptedException ie) { |
| 234 | ie.printStackTrace(); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 241 | |
| 242 | Drawable getTaskDescriptionIcon(Task.TaskKey taskKey, Bitmap iconBitmap, String iconFilename, |
| 243 | SystemServicesProxy ssp, Resources res) { |
| 244 | Bitmap tdIcon = iconBitmap != null |
| 245 | ? iconBitmap |
| 246 | : ActivityManager.TaskDescription.loadTaskDescriptionIcon(iconFilename); |
| 247 | if (tdIcon != null) { |
| 248 | return ssp.getBadgedIcon(new BitmapDrawable(res, tdIcon), taskKey.userId); |
| 249 | } |
| 250 | return null; |
| 251 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 254 | /* Recents task loader |
| 255 | * NOTE: We should not hold any references to a Context from a static instance */ |
| 256 | public class RecentsTaskLoader { |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 257 | private static final String TAG = "RecentsTaskLoader"; |
| 258 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 259 | static RecentsTaskLoader sInstance; |
| 260 | |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 261 | SystemServicesProxy mSystemServicesProxy; |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 262 | DrawableLruCache mApplicationIconCache; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 263 | BitmapLruCache mThumbnailCache; |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 264 | StringLruCache mActivityLabelCache; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 265 | TaskResourceLoadQueue mLoadQueue; |
| 266 | TaskResourceLoader mLoader; |
| 267 | |
Winson Chung | 9f49df9 | 2014-05-07 18:08:34 -0700 | [diff] [blame] | 268 | RecentsPackageMonitor mPackageMonitor; |
| 269 | |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 270 | int mMaxThumbnailCacheSize; |
| 271 | int mMaxIconCacheSize; |
| 272 | |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 273 | BitmapDrawable mDefaultApplicationIcon; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 274 | Bitmap mDefaultThumbnail; |
| 275 | |
| 276 | /** Private Constructor */ |
| 277 | private RecentsTaskLoader(Context context) { |
Jorim Jaggi | 81e0c84 | 2014-09-12 23:28:58 +0200 | [diff] [blame] | 278 | mMaxThumbnailCacheSize = context.getResources().getInteger( |
| 279 | R.integer.config_recents_max_thumbnail_count); |
| 280 | mMaxIconCacheSize = context.getResources().getInteger( |
| 281 | R.integer.config_recents_max_icon_count); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 282 | int iconCacheSize = Constants.DebugFlags.App.DisableBackgroundCache ? 1 : |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 283 | mMaxIconCacheSize; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 284 | int thumbnailCacheSize = Constants.DebugFlags.App.DisableBackgroundCache ? 1 : |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 285 | mMaxThumbnailCacheSize; |
| 286 | |
Winson Chung | 7aceb9a | 2014-07-03 13:38:01 -0700 | [diff] [blame] | 287 | // Create the default assets |
| 288 | Bitmap icon = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); |
| 289 | icon.eraseColor(0x00000000); |
| 290 | mDefaultThumbnail = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); |
| 291 | mDefaultThumbnail.setHasAlpha(false); |
| 292 | mDefaultThumbnail.eraseColor(0xFFffffff); |
Winson Chung | 7aceb9a | 2014-07-03 13:38:01 -0700 | [diff] [blame] | 293 | mDefaultApplicationIcon = new BitmapDrawable(context.getResources(), icon); |
| 294 | |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 295 | // Initialize the proxy, cache and loaders |
| 296 | mSystemServicesProxy = new SystemServicesProxy(context); |
Winson Chung | d543c1b | 2014-06-23 15:06:45 -0700 | [diff] [blame] | 297 | mPackageMonitor = new RecentsPackageMonitor(); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 298 | mLoadQueue = new TaskResourceLoadQueue(); |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 299 | mApplicationIconCache = new DrawableLruCache(iconCacheSize); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 300 | mThumbnailCache = new BitmapLruCache(thumbnailCacheSize); |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 301 | mActivityLabelCache = new StringLruCache(100); |
Winson Chung | c9567c0 | 2014-06-16 20:25:51 -0700 | [diff] [blame] | 302 | mLoader = new TaskResourceLoader(mLoadQueue, mApplicationIconCache, mThumbnailCache, |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 303 | mDefaultThumbnail, mDefaultApplicationIcon); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** Initializes the recents task loader */ |
| 307 | public static RecentsTaskLoader initialize(Context context) { |
| 308 | if (sInstance == null) { |
| 309 | sInstance = new RecentsTaskLoader(context); |
| 310 | } |
| 311 | return sInstance; |
| 312 | } |
| 313 | |
| 314 | /** Returns the current recents task loader */ |
| 315 | public static RecentsTaskLoader getInstance() { |
| 316 | return sInstance; |
| 317 | } |
| 318 | |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 319 | /** Returns the system services proxy */ |
| 320 | public SystemServicesProxy getSystemServicesProxy() { |
| 321 | return mSystemServicesProxy; |
| 322 | } |
| 323 | |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 324 | /** Gets the list of recent tasks, ordered from back to front. */ |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 325 | private static List<ActivityManager.RecentTaskInfo> getRecentTasks(SystemServicesProxy ssp) { |
| 326 | RecentsConfiguration config = RecentsConfiguration.getInstance(); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 327 | List<ActivityManager.RecentTaskInfo> tasks = |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 328 | ssp.getRecentTasks(config.maxNumTasksToLoad, |
| 329 | UserHandle.CURRENT.getIdentifier()); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 330 | Collections.reverse(tasks); |
Winson Chung | 0d76755 | 2014-04-09 14:33:23 -0700 | [diff] [blame] | 331 | return tasks; |
| 332 | } |
| 333 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 334 | /** Returns the activity icon using as many cached values as we can. */ |
| 335 | public Drawable getAndUpdateActivityIcon(Task.TaskKey taskKey, |
| 336 | ActivityManager.TaskDescription td, SystemServicesProxy ssp, |
| 337 | Resources res, ActivityInfoHandle infoHandle, boolean preloadTask) { |
| 338 | // Return the cached activity icon if it exists |
| 339 | Drawable icon = mApplicationIconCache.getAndInvalidateIfModified(taskKey); |
| 340 | if (icon != null) { |
| 341 | return icon; |
| 342 | } |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 343 | |
| 344 | // If we are preloading this task, continue to load the task description icon or the |
| 345 | // activity icon |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 346 | if (preloadTask) { |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 347 | |
| 348 | // Return and cache the task description icon if it exists |
| 349 | Drawable tdDrawable = mLoader.getTaskDescriptionIcon(taskKey, td.getInMemoryIcon(), |
| 350 | td.getIconFilename(), ssp, res); |
| 351 | if (tdDrawable != null) { |
| 352 | mApplicationIconCache.put(taskKey, tdDrawable); |
| 353 | return tdDrawable; |
| 354 | } |
| 355 | |
| 356 | // Load the icon from the activity info and cache it |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 357 | if (infoHandle.info == null) { |
| 358 | infoHandle.info = ssp.getActivityInfo(taskKey.baseIntent.getComponent(), |
| 359 | taskKey.userId); |
| 360 | } |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 361 | if (infoHandle.info != null) { |
| 362 | icon = ssp.getActivityIcon(infoHandle.info, taskKey.userId); |
| 363 | if (icon != null) { |
| 364 | mApplicationIconCache.put(taskKey, icon); |
| 365 | return icon; |
| 366 | } |
| 367 | } |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 368 | } |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 369 | // If we couldn't load any icon, return null |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 370 | return null; |
| 371 | } |
| 372 | |
| 373 | /** Returns the activity label using as many cached values as we can. */ |
| 374 | public String getAndUpdateActivityLabel(Task.TaskKey taskKey, |
| 375 | ActivityManager.TaskDescription td, SystemServicesProxy ssp, |
| 376 | ActivityInfoHandle infoHandle) { |
| 377 | // Return the task description label if it exists |
| 378 | if (td != null && td.getLabel() != null) { |
| 379 | return td.getLabel(); |
| 380 | } |
| 381 | // Return the cached activity label if it exists |
| 382 | String label = mActivityLabelCache.getAndInvalidateIfModified(taskKey); |
| 383 | if (label != null) { |
| 384 | return label; |
| 385 | } |
| 386 | // All short paths failed, load the label from the activity info and cache it |
| 387 | if (infoHandle.info == null) { |
| 388 | infoHandle.info = ssp.getActivityInfo(taskKey.baseIntent.getComponent(), |
| 389 | taskKey.userId); |
| 390 | } |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 391 | if (infoHandle.info != null) { |
| 392 | label = ssp.getActivityLabel(infoHandle.info); |
| 393 | mActivityLabelCache.put(taskKey, label); |
| 394 | } else { |
| 395 | Log.w(TAG, "Missing ActivityInfo for " + taskKey.baseIntent.getComponent() |
| 396 | + " u=" + taskKey.userId); |
| 397 | } |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 398 | return label; |
| 399 | } |
| 400 | |
| 401 | /** Returns the activity's primary color. */ |
| 402 | public int getActivityPrimaryColor(ActivityManager.TaskDescription td, |
| 403 | RecentsConfiguration config) { |
| 404 | if (td != null && td.getPrimaryColor() != 0) { |
| 405 | return td.getPrimaryColor(); |
| 406 | } |
| 407 | return config.taskBarViewDefaultBackgroundColor; |
| 408 | } |
| 409 | |
Winson Chung | 0d76755 | 2014-04-09 14:33:23 -0700 | [diff] [blame] | 410 | /** Reload the set of recent tasks */ |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 411 | public SpaceNode reload(Context context, int preloadCount) { |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 412 | ArrayList<Task.TaskKey> taskKeys = new ArrayList<Task.TaskKey>(); |
| 413 | ArrayList<Task> tasksToLoad = new ArrayList<Task>(); |
| 414 | TaskStack stack = getTaskStack(mSystemServicesProxy, context.getResources(), |
| 415 | -1, preloadCount, true, taskKeys, tasksToLoad); |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 416 | SpaceNode root = new SpaceNode(); |
Winson Chung | 0d76755 | 2014-04-09 14:33:23 -0700 | [diff] [blame] | 417 | root.setStack(stack); |
| 418 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 419 | // Start the task loader and add all the tasks we need to load |
| 420 | mLoader.start(context); |
| 421 | mLoadQueue.addTasks(tasksToLoad); |
Winson Chung | 0d76755 | 2014-04-09 14:33:23 -0700 | [diff] [blame] | 422 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 423 | // Update the package monitor with the list of packages to listen for |
| 424 | mPackageMonitor.setTasks(taskKeys); |
| 425 | |
| 426 | return root; |
| 427 | } |
| 428 | |
| 429 | /** Creates a lightweight stack of the current recent tasks, without thumbnails and icons. */ |
| 430 | public TaskStack getTaskStack(SystemServicesProxy ssp, Resources res, |
| 431 | int preloadTaskId, int preloadTaskCount, |
| 432 | boolean loadTaskThumbnails, List<Task.TaskKey> taskKeysOut, |
| 433 | List<Task> tasksToLoadOut) { |
| 434 | RecentsConfiguration config = RecentsConfiguration.getInstance(); |
| 435 | List<ActivityManager.RecentTaskInfo> tasks = getRecentTasks(ssp); |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 436 | HashMap<Task.ComponentNameKey, ActivityInfoHandle> activityInfoCache = |
| 437 | new HashMap<Task.ComponentNameKey, ActivityInfoHandle>(); |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 438 | ArrayList<Task> tasksToAdd = new ArrayList<Task>(); |
| 439 | TaskStack stack = new TaskStack(); |
| 440 | |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 441 | int taskCount = tasks.size(); |
| 442 | for (int i = 0; i < taskCount; i++) { |
| 443 | ActivityManager.RecentTaskInfo t = tasks.get(i); |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 444 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 445 | // Compose the task key |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 446 | Task.TaskKey taskKey = new Task.TaskKey(t.persistentId, t.baseIntent, t.userId, |
| 447 | t.firstActiveTime, t.lastActiveTime); |
Winson Chung | 80693f9 | 2014-04-23 15:19:56 -0700 | [diff] [blame] | 448 | |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 449 | // Get an existing activity info handle if possible |
| 450 | Task.ComponentNameKey cnKey = taskKey.getComponentNameKey(); |
| 451 | ActivityInfoHandle infoHandle; |
| 452 | boolean hasCachedActivityInfo = false; |
| 453 | if (activityInfoCache.containsKey(cnKey)) { |
| 454 | infoHandle = activityInfoCache.get(cnKey); |
| 455 | hasCachedActivityInfo = true; |
| 456 | } else { |
| 457 | infoHandle = new ActivityInfoHandle(); |
| 458 | } |
| 459 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 460 | // Determine whether to preload this task |
| 461 | boolean preloadTask = false; |
| 462 | if (preloadTaskId > 0) { |
| 463 | preloadTask = (t.id == preloadTaskId); |
| 464 | } else if (preloadTaskCount > 0) { |
| 465 | preloadTask = (i >= (taskCount - preloadTaskCount)); |
Winson Chung | 11e41ba | 2014-04-21 12:39:20 -0700 | [diff] [blame] | 466 | } |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 467 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 468 | // Load the label, icon, and color |
| 469 | String activityLabel = getAndUpdateActivityLabel(taskKey, t.taskDescription, |
| 470 | ssp, infoHandle); |
| 471 | Drawable activityIcon = getAndUpdateActivityIcon(taskKey, t.taskDescription, |
| 472 | ssp, res, infoHandle, preloadTask); |
| 473 | int activityColor = getActivityPrimaryColor(t.taskDescription, config); |
| 474 | |
| 475 | // Update the activity info cache |
| 476 | if (!hasCachedActivityInfo && infoHandle.info != null) { |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 477 | activityInfoCache.put(cnKey, infoHandle); |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 480 | Bitmap icon = t.taskDescription != null |
| 481 | ? t.taskDescription.getInMemoryIcon() |
| 482 | : null; |
| 483 | String iconFilename = t.taskDescription != null |
| 484 | ? t.taskDescription.getIconFilename() |
| 485 | : null; |
| 486 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 487 | // Add the task to the stack |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 488 | Task task = new Task(taskKey, (t.id > -1), t.affiliatedTaskId, t.affiliatedTaskColor, |
| 489 | activityLabel, activityIcon, activityColor, (i == (taskCount - 1)), |
Jorim Jaggi | bdd4b20 | 2014-09-26 18:29:07 +0200 | [diff] [blame] | 490 | config.lockToAppEnabled, icon, iconFilename); |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 491 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 492 | if (preloadTask && loadTaskThumbnails) { |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 493 | // Load the thumbnail from the cache if possible |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 494 | task.thumbnail = mThumbnailCache.getAndInvalidateIfModified(taskKey); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 495 | if (task.thumbnail == null) { |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 496 | // Load the thumbnail from the system |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 497 | task.thumbnail = ssp.getTaskThumbnail(taskKey.id); |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 498 | if (task.thumbnail != null) { |
| 499 | task.thumbnail.setHasAlpha(false); |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 500 | mThumbnailCache.put(taskKey, task.thumbnail); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 501 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 502 | } |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 503 | if (task.thumbnail == null && tasksToLoadOut != null) { |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 504 | // Either the task has changed since the last active time, or it was not |
| 505 | // previously cached, so try and load the task anew. |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 506 | tasksToLoadOut.add(task); |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 507 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 508 | } |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 509 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 510 | // Add to the list of task keys |
| 511 | if (taskKeysOut != null) { |
| 512 | taskKeysOut.add(taskKey); |
| 513 | } |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 514 | // Add the task to the stack |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 515 | tasksToAdd.add(task); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 516 | } |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 517 | stack.setTasks(tasksToAdd); |
Winson Chung | ec396d6 | 2014-08-06 17:08:00 -0700 | [diff] [blame] | 518 | stack.createAffiliatedGroupings(config); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 519 | return stack; |
| 520 | } |
| 521 | |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 522 | /** Acquires the task resource data directly from the pool. */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 523 | public void loadTaskData(Task t) { |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 524 | Drawable applicationIcon = mApplicationIconCache.getAndInvalidateIfModified(t.key); |
| 525 | Bitmap thumbnail = mThumbnailCache.getAndInvalidateIfModified(t.key); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 526 | |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 527 | // Grab the thumbnail/icon from the cache, if either don't exist, then trigger a reload and |
| 528 | // use the default assets in their place until they load |
| 529 | boolean requiresLoad = (applicationIcon == null) || (thumbnail == null); |
| 530 | applicationIcon = applicationIcon != null ? applicationIcon : mDefaultApplicationIcon; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 531 | if (requiresLoad) { |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 532 | mLoadQueue.addTask(t); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 533 | } |
Selim Cinek | e8199c5 | 2014-09-17 04:03:52 +0200 | [diff] [blame] | 534 | t.notifyTaskDataLoaded(thumbnail == mDefaultThumbnail ? null : thumbnail, applicationIcon); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 537 | /** Releases the task resource data back into the pool. */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 538 | public void unloadTaskData(Task t) { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 539 | mLoadQueue.removeTask(t); |
Selim Cinek | e8199c5 | 2014-09-17 04:03:52 +0200 | [diff] [blame] | 540 | t.notifyTaskDataUnloaded(null, mDefaultApplicationIcon); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 543 | /** Completely removes the resource data from the pool. */ |
Winson Chung | 5393dff | 2014-05-08 14:25:43 -0700 | [diff] [blame] | 544 | public void deleteTaskData(Task t, boolean notifyTaskDataUnloaded) { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 545 | mLoadQueue.removeTask(t); |
| 546 | mThumbnailCache.remove(t.key); |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 547 | mApplicationIconCache.remove(t.key); |
Winson Chung | 5393dff | 2014-05-08 14:25:43 -0700 | [diff] [blame] | 548 | if (notifyTaskDataUnloaded) { |
Selim Cinek | e8199c5 | 2014-09-17 04:03:52 +0200 | [diff] [blame] | 549 | t.notifyTaskDataUnloaded(null, mDefaultApplicationIcon); |
Winson Chung | 5393dff | 2014-05-08 14:25:43 -0700 | [diff] [blame] | 550 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 553 | /** Stops the task loader and clears all pending tasks */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 554 | void stopLoader() { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 555 | mLoader.stop(); |
| 556 | mLoadQueue.clearTasks(); |
| 557 | } |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 558 | |
Winson Chung | 9f49df9 | 2014-05-07 18:08:34 -0700 | [diff] [blame] | 559 | /** Registers any broadcast receivers. */ |
| 560 | public void registerReceivers(Context context, RecentsPackageMonitor.PackageCallbacks cb) { |
| 561 | // Register the broadcast receiver to handle messages related to packages being added/removed |
| 562 | mPackageMonitor.register(context, cb); |
| 563 | } |
| 564 | |
| 565 | /** Unregisters any broadcast receivers. */ |
| 566 | public void unregisterReceivers() { |
| 567 | mPackageMonitor.unregister(); |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Handles signals from the system, trimming memory when requested to prevent us from running |
| 572 | * out of memory. |
| 573 | */ |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 574 | public void onTrimMemory(int level) { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 575 | switch (level) { |
| 576 | case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN: |
Winson Chung | 47c4c69 | 2014-03-17 10:17:11 -0700 | [diff] [blame] | 577 | // Stop the loader immediately when the UI is no longer visible |
| 578 | stopLoader(); |
Jorim Jaggi | 81e0c84 | 2014-09-12 23:28:58 +0200 | [diff] [blame] | 579 | mThumbnailCache.trimToSize(Math.max( |
| 580 | Constants.Values.RecentsTaskLoader.PreloadFirstTasksCount, |
| 581 | mMaxThumbnailCacheSize / 2)); |
| 582 | mApplicationIconCache.trimToSize(Math.max( |
| 583 | Constants.Values.RecentsTaskLoader.PreloadFirstTasksCount, |
| 584 | mMaxIconCacheSize / 2)); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 585 | break; |
| 586 | case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE: |
| 587 | case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND: |
| 588 | // We are leaving recents, so trim the data a bit |
| 589 | mThumbnailCache.trimToSize(mMaxThumbnailCacheSize / 2); |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 590 | mApplicationIconCache.trimToSize(mMaxIconCacheSize / 2); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 591 | break; |
| 592 | case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW: |
| 593 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 594 | // We are going to be low on memory |
| 595 | mThumbnailCache.trimToSize(mMaxThumbnailCacheSize / 4); |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 596 | mApplicationIconCache.trimToSize(mMaxIconCacheSize / 4); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 597 | break; |
| 598 | case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL: |
| 599 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 600 | // We are low on memory, so release everything |
| 601 | mThumbnailCache.evictAll(); |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 602 | mApplicationIconCache.evictAll(); |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 603 | // The cache is small, only clear the label cache when we are critical |
| 604 | mActivityLabelCache.evictAll(); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 605 | break; |
| 606 | default: |
| 607 | break; |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 608 | } |
| 609 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 610 | } |