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); |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 180 | // Load the application icon if it is stale or we haven't cached one yet |
| 181 | if (cachedIcon == null) { |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 182 | ActivityInfo info = ssp.getActivityInfo(t.key.baseIntent.getComponent(), |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 183 | t.key.userId); |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 184 | if (info != null) { |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 185 | cachedIcon = ssp.getActivityIcon(info, t.key.userId); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 186 | } |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 187 | if (cachedIcon == null) { |
| 188 | cachedIcon = mDefaultApplicationIcon; |
| 189 | } |
| 190 | // At this point, even if we can't load the icon, we will set the default |
| 191 | // icon. |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 192 | mApplicationIconCache.put(t.key, cachedIcon); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 193 | } |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 194 | // Load the thumbnail if it is stale or we haven't cached one yet |
| 195 | if (cachedThumbnail == null) { |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 196 | cachedThumbnail = ssp.getTaskThumbnail(t.key.id); |
| 197 | if (cachedThumbnail != null) { |
| 198 | cachedThumbnail.setHasAlpha(false); |
| 199 | } else { |
| 200 | cachedThumbnail = mDefaultThumbnail; |
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 | mThumbnailCache.put(t.key, cachedThumbnail); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 203 | } |
| 204 | if (!mCancelled) { |
| 205 | // Notify that the task data has changed |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 206 | final Drawable newIcon = cachedIcon; |
Selim Cinek | e8199c5 | 2014-09-17 04:03:52 +0200 | [diff] [blame] | 207 | final Bitmap newThumbnail = cachedThumbnail == mDefaultThumbnail |
| 208 | ? null : cachedThumbnail; |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 209 | mMainThreadHandler.post(new Runnable() { |
| 210 | @Override |
| 211 | public void run() { |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 212 | t.notifyTaskDataLoaded(newThumbnail, newIcon); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 213 | } |
| 214 | }); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | // If there are no other items in the list, then just wait until something is added |
| 219 | if (!mCancelled && mLoadQueue.isEmpty()) { |
| 220 | synchronized(mLoadQueue) { |
| 221 | try { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 222 | mWaitingOnLoadQueue = true; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 223 | mLoadQueue.wait(); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 224 | mWaitingOnLoadQueue = false; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 225 | } catch (InterruptedException ie) { |
| 226 | ie.printStackTrace(); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 235 | /* Recents task loader |
| 236 | * NOTE: We should not hold any references to a Context from a static instance */ |
| 237 | public class RecentsTaskLoader { |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 238 | private static final String TAG = "RecentsTaskLoader"; |
| 239 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 240 | static RecentsTaskLoader sInstance; |
| 241 | |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 242 | SystemServicesProxy mSystemServicesProxy; |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 243 | DrawableLruCache mApplicationIconCache; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 244 | BitmapLruCache mThumbnailCache; |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 245 | StringLruCache mActivityLabelCache; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 246 | TaskResourceLoadQueue mLoadQueue; |
| 247 | TaskResourceLoader mLoader; |
| 248 | |
Winson Chung | 9f49df9 | 2014-05-07 18:08:34 -0700 | [diff] [blame] | 249 | RecentsPackageMonitor mPackageMonitor; |
| 250 | |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 251 | int mMaxThumbnailCacheSize; |
| 252 | int mMaxIconCacheSize; |
| 253 | |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 254 | BitmapDrawable mDefaultApplicationIcon; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 255 | Bitmap mDefaultThumbnail; |
| 256 | |
| 257 | /** Private Constructor */ |
| 258 | private RecentsTaskLoader(Context context) { |
Jorim Jaggi | 81e0c84 | 2014-09-12 23:28:58 +0200 | [diff] [blame] | 259 | mMaxThumbnailCacheSize = context.getResources().getInteger( |
| 260 | R.integer.config_recents_max_thumbnail_count); |
| 261 | mMaxIconCacheSize = context.getResources().getInteger( |
| 262 | R.integer.config_recents_max_icon_count); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 263 | int iconCacheSize = Constants.DebugFlags.App.DisableBackgroundCache ? 1 : |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 264 | mMaxIconCacheSize; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 265 | int thumbnailCacheSize = Constants.DebugFlags.App.DisableBackgroundCache ? 1 : |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 266 | mMaxThumbnailCacheSize; |
| 267 | |
Winson Chung | 7aceb9a | 2014-07-03 13:38:01 -0700 | [diff] [blame] | 268 | // Create the default assets |
| 269 | Bitmap icon = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); |
| 270 | icon.eraseColor(0x00000000); |
| 271 | mDefaultThumbnail = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); |
| 272 | mDefaultThumbnail.setHasAlpha(false); |
| 273 | mDefaultThumbnail.eraseColor(0xFFffffff); |
Winson Chung | 7aceb9a | 2014-07-03 13:38:01 -0700 | [diff] [blame] | 274 | mDefaultApplicationIcon = new BitmapDrawable(context.getResources(), icon); |
| 275 | |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 276 | // Initialize the proxy, cache and loaders |
| 277 | mSystemServicesProxy = new SystemServicesProxy(context); |
Winson Chung | d543c1b | 2014-06-23 15:06:45 -0700 | [diff] [blame] | 278 | mPackageMonitor = new RecentsPackageMonitor(); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 279 | mLoadQueue = new TaskResourceLoadQueue(); |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 280 | mApplicationIconCache = new DrawableLruCache(iconCacheSize); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 281 | mThumbnailCache = new BitmapLruCache(thumbnailCacheSize); |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 282 | mActivityLabelCache = new StringLruCache(100); |
Winson Chung | c9567c0 | 2014-06-16 20:25:51 -0700 | [diff] [blame] | 283 | mLoader = new TaskResourceLoader(mLoadQueue, mApplicationIconCache, mThumbnailCache, |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 284 | mDefaultThumbnail, mDefaultApplicationIcon); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** Initializes the recents task loader */ |
| 288 | public static RecentsTaskLoader initialize(Context context) { |
| 289 | if (sInstance == null) { |
| 290 | sInstance = new RecentsTaskLoader(context); |
| 291 | } |
| 292 | return sInstance; |
| 293 | } |
| 294 | |
| 295 | /** Returns the current recents task loader */ |
| 296 | public static RecentsTaskLoader getInstance() { |
| 297 | return sInstance; |
| 298 | } |
| 299 | |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 300 | /** Returns the system services proxy */ |
| 301 | public SystemServicesProxy getSystemServicesProxy() { |
| 302 | return mSystemServicesProxy; |
| 303 | } |
| 304 | |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 305 | /** Gets the list of recent tasks, ordered from back to front. */ |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 306 | private static List<ActivityManager.RecentTaskInfo> getRecentTasks(SystemServicesProxy ssp) { |
| 307 | RecentsConfiguration config = RecentsConfiguration.getInstance(); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 308 | List<ActivityManager.RecentTaskInfo> tasks = |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 309 | ssp.getRecentTasks(config.maxNumTasksToLoad, |
| 310 | UserHandle.CURRENT.getIdentifier()); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 311 | Collections.reverse(tasks); |
Winson Chung | 0d76755 | 2014-04-09 14:33:23 -0700 | [diff] [blame] | 312 | return tasks; |
| 313 | } |
| 314 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 315 | /** Returns the activity icon using as many cached values as we can. */ |
| 316 | public Drawable getAndUpdateActivityIcon(Task.TaskKey taskKey, |
| 317 | ActivityManager.TaskDescription td, SystemServicesProxy ssp, |
| 318 | Resources res, ActivityInfoHandle infoHandle, boolean preloadTask) { |
| 319 | // Return the cached activity icon if it exists |
| 320 | Drawable icon = mApplicationIconCache.getAndInvalidateIfModified(taskKey); |
| 321 | if (icon != null) { |
| 322 | return icon; |
| 323 | } |
| 324 | // Return the task description icon if it exists |
| 325 | if (td != null && td.getIcon() != null) { |
| 326 | icon = ssp.getBadgedIcon(new BitmapDrawable(res, td.getIcon()), taskKey.userId); |
| 327 | mApplicationIconCache.put(taskKey, icon); |
| 328 | return icon; |
| 329 | } |
| 330 | // If we are preloading this task, continue to load the activity icon |
| 331 | if (preloadTask) { |
| 332 | // All short paths failed, load the icon from the activity info and cache it |
| 333 | if (infoHandle.info == null) { |
| 334 | infoHandle.info = ssp.getActivityInfo(taskKey.baseIntent.getComponent(), |
| 335 | taskKey.userId); |
| 336 | } |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 337 | if (infoHandle.info != null) { |
| 338 | icon = ssp.getActivityIcon(infoHandle.info, taskKey.userId); |
| 339 | if (icon != null) { |
| 340 | mApplicationIconCache.put(taskKey, icon); |
| 341 | return icon; |
| 342 | } |
| 343 | } |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 344 | } |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 345 | // If we couldn't load any icon, return null |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 346 | return null; |
| 347 | } |
| 348 | |
| 349 | /** Returns the activity label using as many cached values as we can. */ |
| 350 | public String getAndUpdateActivityLabel(Task.TaskKey taskKey, |
| 351 | ActivityManager.TaskDescription td, SystemServicesProxy ssp, |
| 352 | ActivityInfoHandle infoHandle) { |
| 353 | // Return the task description label if it exists |
| 354 | if (td != null && td.getLabel() != null) { |
| 355 | return td.getLabel(); |
| 356 | } |
| 357 | // Return the cached activity label if it exists |
| 358 | String label = mActivityLabelCache.getAndInvalidateIfModified(taskKey); |
| 359 | if (label != null) { |
| 360 | return label; |
| 361 | } |
| 362 | // All short paths failed, load the label from the activity info and cache it |
| 363 | if (infoHandle.info == null) { |
| 364 | infoHandle.info = ssp.getActivityInfo(taskKey.baseIntent.getComponent(), |
| 365 | taskKey.userId); |
| 366 | } |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 367 | if (infoHandle.info != null) { |
| 368 | label = ssp.getActivityLabel(infoHandle.info); |
| 369 | mActivityLabelCache.put(taskKey, label); |
| 370 | } else { |
| 371 | Log.w(TAG, "Missing ActivityInfo for " + taskKey.baseIntent.getComponent() |
| 372 | + " u=" + taskKey.userId); |
| 373 | } |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 374 | return label; |
| 375 | } |
| 376 | |
| 377 | /** Returns the activity's primary color. */ |
| 378 | public int getActivityPrimaryColor(ActivityManager.TaskDescription td, |
| 379 | RecentsConfiguration config) { |
| 380 | if (td != null && td.getPrimaryColor() != 0) { |
| 381 | return td.getPrimaryColor(); |
| 382 | } |
| 383 | return config.taskBarViewDefaultBackgroundColor; |
| 384 | } |
| 385 | |
Winson Chung | 0d76755 | 2014-04-09 14:33:23 -0700 | [diff] [blame] | 386 | /** Reload the set of recent tasks */ |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 387 | public SpaceNode reload(Context context, int preloadCount) { |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 388 | ArrayList<Task.TaskKey> taskKeys = new ArrayList<Task.TaskKey>(); |
| 389 | ArrayList<Task> tasksToLoad = new ArrayList<Task>(); |
| 390 | TaskStack stack = getTaskStack(mSystemServicesProxy, context.getResources(), |
| 391 | -1, preloadCount, true, taskKeys, tasksToLoad); |
Winson Chung | 083baf9 | 2014-07-11 10:32:42 -0700 | [diff] [blame] | 392 | SpaceNode root = new SpaceNode(); |
Winson Chung | 0d76755 | 2014-04-09 14:33:23 -0700 | [diff] [blame] | 393 | root.setStack(stack); |
| 394 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 395 | // Start the task loader and add all the tasks we need to load |
| 396 | mLoader.start(context); |
| 397 | mLoadQueue.addTasks(tasksToLoad); |
Winson Chung | 0d76755 | 2014-04-09 14:33:23 -0700 | [diff] [blame] | 398 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 399 | // Update the package monitor with the list of packages to listen for |
| 400 | mPackageMonitor.setTasks(taskKeys); |
| 401 | |
| 402 | return root; |
| 403 | } |
| 404 | |
| 405 | /** Creates a lightweight stack of the current recent tasks, without thumbnails and icons. */ |
| 406 | public TaskStack getTaskStack(SystemServicesProxy ssp, Resources res, |
| 407 | int preloadTaskId, int preloadTaskCount, |
| 408 | boolean loadTaskThumbnails, List<Task.TaskKey> taskKeysOut, |
| 409 | List<Task> tasksToLoadOut) { |
| 410 | RecentsConfiguration config = RecentsConfiguration.getInstance(); |
| 411 | List<ActivityManager.RecentTaskInfo> tasks = getRecentTasks(ssp); |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 412 | HashMap<Task.ComponentNameKey, ActivityInfoHandle> activityInfoCache = |
| 413 | new HashMap<Task.ComponentNameKey, ActivityInfoHandle>(); |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 414 | ArrayList<Task> tasksToAdd = new ArrayList<Task>(); |
| 415 | TaskStack stack = new TaskStack(); |
| 416 | |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 417 | int taskCount = tasks.size(); |
| 418 | for (int i = 0; i < taskCount; i++) { |
| 419 | ActivityManager.RecentTaskInfo t = tasks.get(i); |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 420 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 421 | // Compose the task key |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 422 | Task.TaskKey taskKey = new Task.TaskKey(t.persistentId, t.baseIntent, t.userId, |
| 423 | t.firstActiveTime, t.lastActiveTime); |
Winson Chung | 80693f9 | 2014-04-23 15:19:56 -0700 | [diff] [blame] | 424 | |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 425 | // Get an existing activity info handle if possible |
| 426 | Task.ComponentNameKey cnKey = taskKey.getComponentNameKey(); |
| 427 | ActivityInfoHandle infoHandle; |
| 428 | boolean hasCachedActivityInfo = false; |
| 429 | if (activityInfoCache.containsKey(cnKey)) { |
| 430 | infoHandle = activityInfoCache.get(cnKey); |
| 431 | hasCachedActivityInfo = true; |
| 432 | } else { |
| 433 | infoHandle = new ActivityInfoHandle(); |
| 434 | } |
| 435 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 436 | // Determine whether to preload this task |
| 437 | boolean preloadTask = false; |
| 438 | if (preloadTaskId > 0) { |
| 439 | preloadTask = (t.id == preloadTaskId); |
| 440 | } else if (preloadTaskCount > 0) { |
| 441 | preloadTask = (i >= (taskCount - preloadTaskCount)); |
Winson Chung | 11e41ba | 2014-04-21 12:39:20 -0700 | [diff] [blame] | 442 | } |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 443 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 444 | // Load the label, icon, and color |
| 445 | String activityLabel = getAndUpdateActivityLabel(taskKey, t.taskDescription, |
| 446 | ssp, infoHandle); |
| 447 | Drawable activityIcon = getAndUpdateActivityIcon(taskKey, t.taskDescription, |
| 448 | ssp, res, infoHandle, preloadTask); |
| 449 | int activityColor = getActivityPrimaryColor(t.taskDescription, config); |
| 450 | |
| 451 | // Update the activity info cache |
| 452 | if (!hasCachedActivityInfo && infoHandle.info != null) { |
Kenny Guy | a734fc1 | 2014-08-28 21:06:27 +0100 | [diff] [blame] | 453 | activityInfoCache.put(cnKey, infoHandle); |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | // Add the task to the stack |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 457 | Task task = new Task(taskKey, (t.id > -1), t.affiliatedTaskId, t.affiliatedTaskColor, |
| 458 | activityLabel, activityIcon, activityColor, (i == (taskCount - 1)), |
Winson Chung | ec396d6 | 2014-08-06 17:08:00 -0700 | [diff] [blame] | 459 | config.lockToAppEnabled); |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 460 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 461 | if (preloadTask && loadTaskThumbnails) { |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 462 | // Load the thumbnail from the cache if possible |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 463 | task.thumbnail = mThumbnailCache.getAndInvalidateIfModified(taskKey); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 464 | if (task.thumbnail == null) { |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 465 | // Load the thumbnail from the system |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 466 | task.thumbnail = ssp.getTaskThumbnail(taskKey.id); |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 467 | if (task.thumbnail != null) { |
| 468 | task.thumbnail.setHasAlpha(false); |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 469 | mThumbnailCache.put(taskKey, task.thumbnail); |
Winson Chung | a10370f | 2014-04-02 12:25:04 -0700 | [diff] [blame] | 470 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 471 | } |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 472 | if (task.thumbnail == null && tasksToLoadOut != null) { |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 473 | // Either the task has changed since the last active time, or it was not |
| 474 | // previously cached, so try and load the task anew. |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 475 | tasksToLoadOut.add(task); |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 476 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 477 | } |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 478 | |
Winson Chung | ebfc698 | 2014-08-26 12:25:34 -0700 | [diff] [blame] | 479 | // Add to the list of task keys |
| 480 | if (taskKeysOut != null) { |
| 481 | taskKeysOut.add(taskKey); |
| 482 | } |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 483 | // Add the task to the stack |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 484 | tasksToAdd.add(task); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 485 | } |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 486 | stack.setTasks(tasksToAdd); |
Winson Chung | ec396d6 | 2014-08-06 17:08:00 -0700 | [diff] [blame] | 487 | stack.createAffiliatedGroupings(config); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 488 | return stack; |
| 489 | } |
| 490 | |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 491 | /** Acquires the task resource data directly from the pool. */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 492 | public void loadTaskData(Task t) { |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 493 | Drawable applicationIcon = mApplicationIconCache.getAndInvalidateIfModified(t.key); |
| 494 | Bitmap thumbnail = mThumbnailCache.getAndInvalidateIfModified(t.key); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 495 | |
Winson Chung | ff88d7b | 2014-07-17 12:30:07 -0700 | [diff] [blame] | 496 | // Grab the thumbnail/icon from the cache, if either don't exist, then trigger a reload and |
| 497 | // use the default assets in their place until they load |
| 498 | boolean requiresLoad = (applicationIcon == null) || (thumbnail == null); |
| 499 | applicationIcon = applicationIcon != null ? applicationIcon : mDefaultApplicationIcon; |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 500 | if (requiresLoad) { |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 501 | mLoadQueue.addTask(t); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 502 | } |
Selim Cinek | e8199c5 | 2014-09-17 04:03:52 +0200 | [diff] [blame] | 503 | t.notifyTaskDataLoaded(thumbnail == mDefaultThumbnail ? null : thumbnail, applicationIcon); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 506 | /** Releases the task resource data back into the pool. */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 507 | public void unloadTaskData(Task t) { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 508 | mLoadQueue.removeTask(t); |
Selim Cinek | e8199c5 | 2014-09-17 04:03:52 +0200 | [diff] [blame] | 509 | t.notifyTaskDataUnloaded(null, mDefaultApplicationIcon); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 512 | /** Completely removes the resource data from the pool. */ |
Winson Chung | 5393dff | 2014-05-08 14:25:43 -0700 | [diff] [blame] | 513 | public void deleteTaskData(Task t, boolean notifyTaskDataUnloaded) { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 514 | mLoadQueue.removeTask(t); |
| 515 | mThumbnailCache.remove(t.key); |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 516 | mApplicationIconCache.remove(t.key); |
Winson Chung | 5393dff | 2014-05-08 14:25:43 -0700 | [diff] [blame] | 517 | if (notifyTaskDataUnloaded) { |
Selim Cinek | e8199c5 | 2014-09-17 04:03:52 +0200 | [diff] [blame] | 518 | t.notifyTaskDataUnloaded(null, mDefaultApplicationIcon); |
Winson Chung | 5393dff | 2014-05-08 14:25:43 -0700 | [diff] [blame] | 519 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 522 | /** Stops the task loader and clears all pending tasks */ |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 523 | void stopLoader() { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 524 | mLoader.stop(); |
| 525 | mLoadQueue.clearTasks(); |
| 526 | } |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 527 | |
Winson Chung | 9f49df9 | 2014-05-07 18:08:34 -0700 | [diff] [blame] | 528 | /** Registers any broadcast receivers. */ |
| 529 | public void registerReceivers(Context context, RecentsPackageMonitor.PackageCallbacks cb) { |
| 530 | // Register the broadcast receiver to handle messages related to packages being added/removed |
| 531 | mPackageMonitor.register(context, cb); |
| 532 | } |
| 533 | |
| 534 | /** Unregisters any broadcast receivers. */ |
| 535 | public void unregisterReceivers() { |
| 536 | mPackageMonitor.unregister(); |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Handles signals from the system, trimming memory when requested to prevent us from running |
| 541 | * out of memory. |
| 542 | */ |
Winson Chung | f1fbd77 | 2014-06-24 18:06:58 -0700 | [diff] [blame] | 543 | public void onTrimMemory(int level) { |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 544 | switch (level) { |
| 545 | case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN: |
Winson Chung | 47c4c69 | 2014-03-17 10:17:11 -0700 | [diff] [blame] | 546 | // Stop the loader immediately when the UI is no longer visible |
| 547 | stopLoader(); |
Jorim Jaggi | 81e0c84 | 2014-09-12 23:28:58 +0200 | [diff] [blame] | 548 | mThumbnailCache.trimToSize(Math.max( |
| 549 | Constants.Values.RecentsTaskLoader.PreloadFirstTasksCount, |
| 550 | mMaxThumbnailCacheSize / 2)); |
| 551 | mApplicationIconCache.trimToSize(Math.max( |
| 552 | Constants.Values.RecentsTaskLoader.PreloadFirstTasksCount, |
| 553 | mMaxIconCacheSize / 2)); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 554 | break; |
| 555 | case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE: |
| 556 | case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND: |
| 557 | // We are leaving recents, so trim the data a bit |
| 558 | mThumbnailCache.trimToSize(mMaxThumbnailCacheSize / 2); |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 559 | mApplicationIconCache.trimToSize(mMaxIconCacheSize / 2); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 560 | break; |
| 561 | case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW: |
| 562 | case ComponentCallbacks2.TRIM_MEMORY_MODERATE: |
| 563 | // We are going to be low on memory |
| 564 | mThumbnailCache.trimToSize(mMaxThumbnailCacheSize / 4); |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 565 | mApplicationIconCache.trimToSize(mMaxIconCacheSize / 4); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 566 | break; |
| 567 | case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL: |
| 568 | case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: |
| 569 | // We are low on memory, so release everything |
| 570 | mThumbnailCache.evictAll(); |
Winson Chung | 5e3e5d8 | 2014-04-02 15:44:55 -0700 | [diff] [blame] | 571 | mApplicationIconCache.evictAll(); |
Winson Chung | a4ccb86 | 2014-08-22 15:26:27 -0700 | [diff] [blame] | 572 | // The cache is small, only clear the label cache when we are critical |
| 573 | mActivityLabelCache.evictAll(); |
Winson Chung | 04dfe0d | 2014-03-14 14:06:29 -0700 | [diff] [blame] | 574 | break; |
| 575 | default: |
| 576 | break; |
Winson Chung | 4d7b092 | 2014-03-13 17:14:17 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 579 | } |