blob: ac257b5c384b30d2bf1d764aa1f3b3b607e345fc [file] [log] [blame]
Joe Onorato0589f0f2010-02-08 13:44:00 -08001/*
2 * Copyright (C) 2008 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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Joe Onorato0589f0f2010-02-08 13:44:00 -080018
Winson Chungd83f5f42012-02-13 14:27:42 -080019import android.app.ActivityManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -080020import android.content.ComponentName;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080021import android.content.ContentValues;
Winson Chungd83f5f42012-02-13 14:27:42 -080022import android.content.Context;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent;
Michael Jurkadac85912012-05-18 15:04:49 -070024import android.content.pm.ActivityInfo;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070025import android.content.pm.ApplicationInfo;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080026import android.content.pm.PackageInfo;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.pm.PackageManager;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070028import android.content.pm.PackageManager.NameNotFoundException;
Michael Jurkac9a96192010-11-01 11:52:08 -070029import android.content.res.Resources;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080030import android.database.Cursor;
31import android.database.sqlite.SQLiteDatabase;
32import android.database.sqlite.SQLiteOpenHelper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080033import android.graphics.Bitmap;
Sunny Goyal34b65272015-03-11 16:56:52 -070034import android.graphics.BitmapFactory;
Romain Guya28fd3f2010-03-15 14:44:42 -070035import android.graphics.Canvas;
Joe Onorato0589f0f2010-02-08 13:44:00 -080036import android.graphics.drawable.Drawable;
Sunny Goyal34b65272015-03-11 16:56:52 -070037import android.os.Handler;
Sunny Goyal75b0f552015-05-20 21:57:06 -070038import android.os.SystemClock;
Sunny Goyal34942622014-08-29 17:20:55 -070039import android.text.TextUtils;
Chris Wren6d0dde02014-02-10 12:16:54 -050040import android.util.Log;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041
Kenny Guyed131872014-04-30 03:02:21 +010042import com.android.launcher3.compat.LauncherActivityInfoCompat;
43import com.android.launcher3.compat.LauncherAppsCompat;
44import com.android.launcher3.compat.UserHandleCompat;
45import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070046import com.android.launcher3.util.ComponentKey;
Adam Cohen091440a2015-03-18 14:16:05 -070047import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -070048import com.android.launcher3.widget.PackageItemInfo;
Kenny Guyed131872014-04-30 03:02:21 +010049
Joe Onorato0589f0f2010-02-08 13:44:00 -080050import java.util.HashMap;
Chris Wren6d0dde02014-02-10 12:16:54 -050051import java.util.HashSet;
Adam Cohenb6d33df2013-10-15 10:18:02 -070052import java.util.Iterator;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080053import java.util.List;
Sunny Goyal0c9a3542015-04-01 16:04:21 -070054import java.util.Locale;
Adam Cohenb6d33df2013-10-15 10:18:02 -070055import java.util.Map.Entry;
Sunny Goyal9ff98082015-05-15 16:59:36 -070056import java.util.Stack;
Joe Onorato0589f0f2010-02-08 13:44:00 -080057
58/**
59 * Cache of application icons. Icons can be made from any thread.
60 */
61public class IconCache {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070062
Joe Onorato0589f0f2010-02-08 13:44:00 -080063 private static final String TAG = "Launcher.IconCache";
64
65 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050066
Sunny Goyal0fc1be12014-08-11 17:05:23 -070067 // Empty class name is used for storing package default entry.
68 private static final String EMPTY_CLASS_NAME = ".";
69
Sunny Goyalbbef77d2014-09-09 16:27:55 -070070 private static final boolean DEBUG = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -080071
Sunny Goyal34b65272015-03-11 16:56:52 -070072 private static final int LOW_RES_SCALE_FACTOR = 8;
73
Sunny Goyal75b0f552015-05-20 21:57:06 -070074 private static final Object ICON_UPDATE_TOKEN = new Object();
75
Adam Cohen091440a2015-03-18 14:16:05 -070076 @Thunk static class CacheEntry {
Joe Onorato0589f0f2010-02-08 13:44:00 -080077 public Bitmap icon;
Kenny Guyd6fe5262014-07-21 17:11:41 +010078 public CharSequence title;
79 public CharSequence contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -070080 public boolean isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -080081 }
82
Sunny Goyal8758ea02015-03-18 10:07:49 -070083 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
84 private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
85
Daniel Sandlercc8befa2013-06-11 14:45:48 -040086 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070087 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010088 private final UserManagerCompat mUserManager;
89 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070090 private final HashMap<ComponentKey, CacheEntry> mCache =
91 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080092 private final int mIconDpi;
93 private final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080094
Sunny Goyal34b65272015-03-11 16:56:52 -070095 private final Handler mWorkerHandler;
96
Daniel Sandlercc8befa2013-06-11 14:45:48 -040097 public IconCache(Context context) {
Winson Chungd83f5f42012-02-13 14:27:42 -080098 ActivityManager activityManager =
99 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
100
Joe Onorato0589f0f2010-02-08 13:44:00 -0800101 mContext = context;
102 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +0100103 mUserManager = UserManagerCompat.getInstance(mContext);
104 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Winson Chungd83f5f42012-02-13 14:27:42 -0800105 mIconDpi = activityManager.getLauncherLargeIconDensity();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800106 mIconDb = new IconDB(context);
Sunny Goyal34b65272015-03-11 16:56:52 -0700107
Sunny Goyal756adbc2015-04-16 15:20:51 -0700108 mWorkerHandler = new Handler(LauncherModel.getWorkerLooper());
Romain Guya28fd3f2010-03-15 14:44:42 -0700109 }
110
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800111 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700112 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700113 }
114
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700115 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700116 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700117 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700118 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700119 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700120 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700121 }
Michael Jurka721d9722011-08-03 11:49:59 -0700122
123 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700124 }
125
Winson Chung0b9fcf52011-10-31 13:05:15 -0700126 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700127 Resources resources;
128 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700129 resources = mPackageManager.getResourcesForApplication(packageName);
130 } catch (PackageManager.NameNotFoundException e) {
131 resources = null;
132 }
133 if (resources != null) {
134 if (iconId != 0) {
135 return getFullResIcon(resources, iconId);
136 }
137 }
138 return getFullResDefaultActivityIcon();
139 }
140
Sunny Goyalffe83f12014-08-14 17:39:34 -0700141 public int getFullResIconDpi() {
142 return mIconDpi;
143 }
144
Michael Jurkadac85912012-05-18 15:04:49 -0700145 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700146 Resources resources;
147 try {
148 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700149 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700150 } catch (PackageManager.NameNotFoundException e) {
151 resources = null;
152 }
153 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700154 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700155 if (iconId != 0) {
156 return getFullResIcon(resources, iconId);
157 }
158 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500159
Michael Jurkac9a96192010-11-01 11:52:08 -0700160 return getFullResDefaultActivityIcon();
161 }
162
Kenny Guyed131872014-04-30 03:02:21 +0100163 private Bitmap makeDefaultIcon(UserHandleCompat user) {
164 Drawable unbadged = getFullResDefaultActivityIcon();
165 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700166 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
167 Math.max(d.getIntrinsicHeight(), 1),
168 Bitmap.Config.ARGB_8888);
169 Canvas c = new Canvas(b);
170 d.setBounds(0, 0, b.getWidth(), b.getHeight());
171 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700172 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700173 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800174 }
175
176 /**
177 * Remove any records for the supplied ComponentName.
178 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700179 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700180 mCache.remove(new ComponentKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800181 }
182
183 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800184 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500185 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800186 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700187 HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
188 for (ComponentKey key: mCache.keySet()) {
Kenny Guyed131872014-04-30 03:02:21 +0100189 if (key.componentName.getPackageName().equals(packageName)
190 && key.user.equals(user)) {
191 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500192 }
193 }
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700194 for (ComponentKey condemned: forDeletion) {
Kenny Guyed131872014-04-30 03:02:21 +0100195 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500196 }
197 }
198
199 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800200 * Updates the entries related to the given package in memory and persistent DB.
201 */
202 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
203 removeIconsForPkg(packageName, user);
204 try {
205 PackageInfo info = mPackageManager.getPackageInfo(packageName,
206 PackageManager.GET_UNINSTALLED_PACKAGES);
207 long userSerial = mUserManager.getSerialNumberForUser(user);
208 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700209 addIconToDBAndMemCache(app, info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800210 }
211 } catch (NameNotFoundException e) {
212 Log.d(TAG, "Package not found", e);
213 return;
214 }
215 }
216
217 /**
218 * Removes the entries related to the given package in memory and persistent DB.
219 */
220 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
221 removeFromMemCacheLocked(packageName, user);
222 long userSerial = mUserManager.getSerialNumberForUser(user);
223 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
224 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
225 new String[] {packageName + "/%", Long.toString(userSerial)});
226 }
227
Sunny Goyal75b0f552015-05-20 21:57:06 -0700228 public void updateDbIcons() {
229 // Remove all active icon update tasks.
230 mWorkerHandler.removeCallbacksAndMessages(ICON_UPDATE_TOKEN);
231
232 mIconDb.updateSystemStateString(mContext);
233 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
234 // Query for the set of apps
235 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
236 // Fail if we don't have any apps
237 // TODO: Fix this. Only fail for the current user.
238 if (apps == null || apps.isEmpty()) {
239 return;
240 }
241
242 // Update icon cache. This happens in segments and {@link #onPackageIconsUpdated}
243 // is called by the icon cache when the job is complete.
244 updateDBIcons(user, apps);
245 }
246 }
247
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800248 /**
249 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
250 * the DB and are updated.
251 * @return The set of packages for which icons have updated.
252 */
Sunny Goyal75b0f552015-05-20 21:57:06 -0700253 private void updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800254 long userSerial = mUserManager.getSerialNumberForUser(user);
255 PackageManager pm = mContext.getPackageManager();
256 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
257 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
258 pkgInfoMap.put(info.packageName, info);
259 }
260
261 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
262 for (LauncherActivityInfoCompat app : apps) {
263 componentMap.put(app.getComponentName(), app);
264 }
265
266 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
267 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700268 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION,
269 IconDB.COLUMN_SYSTEM_STATE},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800270 IconDB.COLUMN_USER + " = ? ",
271 new String[] {Long.toString(userSerial)},
272 null, null, null);
273
274 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
275 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
276 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
277 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700278 final int systemStateIndex = c.getColumnIndex(IconDB.COLUMN_SYSTEM_STATE);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800279
280 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
Sunny Goyal75b0f552015-05-20 21:57:06 -0700281 Stack<LauncherActivityInfoCompat> appsToUpdate = new Stack<>();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800282
283 while (c.moveToNext()) {
284 String cn = c.getString(indexComponent);
285 ComponentName component = ComponentName.unflattenFromString(cn);
286 PackageInfo info = pkgInfoMap.get(component.getPackageName());
287 if (info == null) {
288 itemsToRemove.add(c.getInt(rowIndex));
289 continue;
290 }
291 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
292 // Application is not present
293 continue;
294 }
295
296 long updateTime = c.getLong(indexLastUpdate);
297 int version = c.getInt(indexVersion);
298 LauncherActivityInfoCompat app = componentMap.remove(component);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700299 if (version == info.versionCode && updateTime == info.lastUpdateTime &&
300 TextUtils.equals(mIconDb.mSystemState, c.getString(systemStateIndex))) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800301 continue;
302 }
303 if (app == null) {
304 itemsToRemove.add(c.getInt(rowIndex));
Sunny Goyal75b0f552015-05-20 21:57:06 -0700305 } else {
306 appsToUpdate.add(app);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800307 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800308 }
309 c.close();
310 if (!itemsToRemove.isEmpty()) {
311 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
312 IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )",
313 null);
314 }
315
316 // Insert remaining apps.
Sunny Goyal75b0f552015-05-20 21:57:06 -0700317 if (!componentMap.isEmpty() || !appsToUpdate.isEmpty()) {
318 Stack<LauncherActivityInfoCompat> appsToAdd = new Stack<>();
319 appsToAdd.addAll(componentMap.values());
320 new SerializedIconUpdateTask(userSerial, pkgInfoMap,
321 appsToAdd, appsToUpdate).scheduleNext();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800322 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800323 }
324
Sunny Goyald180cf72015-04-06 12:45:40 -0700325 private void addIconToDBAndMemCache(LauncherActivityInfoCompat app, PackageInfo info,
326 long userSerial) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700327 // Reuse the existing entry if it already exists in the DB. This ensures that we do not
328 // create bitmap if it was already created during loader.
329 ContentValues values = updateCacheAndGetContentValues(app, false);
Sunny Goyald180cf72015-04-06 12:45:40 -0700330 addIconToDB(values, app.getComponentName(), info, userSerial);
Sunny Goyald180cf72015-04-06 12:45:40 -0700331 }
332
333 /**
334 * Updates {@param values} to contain versoning information and adds it to the DB.
335 * @param values {@link ContentValues} containing icon & title
336 */
337 private void addIconToDB(ContentValues values, ComponentName key,
338 PackageInfo info, long userSerial) {
339 values.put(IconDB.COLUMN_COMPONENT, key.flattenToString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800340 values.put(IconDB.COLUMN_USER, userSerial);
341 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
342 values.put(IconDB.COLUMN_VERSION, info.versionCode);
343 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
344 SQLiteDatabase.CONFLICT_REPLACE);
345 }
346
Sunny Goyal9ff98082015-05-15 16:59:36 -0700347 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app,
348 boolean replaceExisting) {
349 final ComponentKey key = new ComponentKey(app.getComponentName(), app.getUser());
350 CacheEntry entry = null;
351 if (!replaceExisting) {
352 entry = mCache.get(key);
353 // We can't reuse the entry if the high-res icon is not present.
354 if (entry == null || entry.isLowResIcon || entry.icon == null) {
355 entry = null;
356 }
357 }
358 if (entry == null) {
359 entry = new CacheEntry();
360 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
361 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800362 entry.title = app.getLabel();
363 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700364 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800365
Sunny Goyal34b65272015-03-11 16:56:52 -0700366 return mIconDb.newContentValues(entry.icon, entry.title.toString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800367 }
368
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800369 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800370 * Empty out the cache.
371 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700372 public synchronized void flush() {
373 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800374 }
375
376 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700377 * Empty out the cache that aren't of the correct grid size
378 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700379 public synchronized void flushInvalidIcons(DeviceProfile grid) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700380 Iterator<Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator();
Sunny Goyal736f5af2014-10-16 14:07:29 -0700381 while (it.hasNext()) {
382 final CacheEntry e = it.next().getValue();
383 if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
384 || e.icon.getHeight() < grid.iconSizePx)) {
385 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700386 }
387 }
388 }
389
390 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700391 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
392 * @return a request ID that can be used to cancel the request.
393 */
394 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
395 Runnable request = new Runnable() {
396
397 @Override
398 public void run() {
399 if (info instanceof AppInfo) {
400 getTitleAndIcon((AppInfo) info, null, false);
401 } else if (info instanceof ShortcutInfo) {
402 ShortcutInfo st = (ShortcutInfo) info;
403 getTitleAndIcon(st,
404 st.promisedIntent != null ? st.promisedIntent : st.intent,
405 st.user, false);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700406 } else if (info instanceof PackageItemInfo) {
407 PackageItemInfo pti = (PackageItemInfo) info;
408 getTitleAndIconForApp(pti.packageName, pti.user, false, pti);
Sunny Goyal34b65272015-03-11 16:56:52 -0700409 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700410 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700411
412 @Override
413 public void run() {
414 caller.reapplyItemInfo(info);
415 }
416 });
417 }
418 };
419 mWorkerHandler.post(request);
420 return new IconLoadRequest(request, mWorkerHandler);
421 }
422
Sunny Goyal756adbc2015-04-16 15:20:51 -0700423 private Bitmap getNonNullIcon(CacheEntry entry, UserHandleCompat user) {
424 return entry.icon == null ? getDefaultIcon(user) : entry.icon;
425 }
426
Sunny Goyal34b65272015-03-11 16:56:52 -0700427 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800428 * Fill in "application" with the icon and label for "info."
429 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700430 public synchronized void getTitleAndIcon(AppInfo application,
431 LauncherActivityInfoCompat info, boolean useLowResIcon) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700432 UserHandleCompat user = info == null ? application.user : info.getUser();
433 CacheEntry entry = cacheLocked(application.componentName, info, user,
Sunny Goyal34b65272015-03-11 16:56:52 -0700434 false, useLowResIcon);
Winson Chung82b016c2015-05-08 17:00:10 -0700435 application.title = Utilities.trim(entry.title);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700436 application.iconBitmap = getNonNullIcon(entry, user);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700437 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700438 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800439 }
440
Sunny Goyal34b65272015-03-11 16:56:52 -0700441 /**
Sunny Goyal77919b92015-05-06 16:53:21 -0700442 * Updates {@param application} only if a valid entry is found.
443 */
444 public synchronized void updateTitleAndIcon(AppInfo application) {
445 CacheEntry entry = cacheLocked(application.componentName, null, application.user,
446 false, application.usingLowResIcon);
447 if (entry.icon != null && !isDefaultIcon(entry.icon, application.user)) {
Winson Chung82b016c2015-05-08 17:00:10 -0700448 application.title = Utilities.trim(entry.title);
Sunny Goyal77919b92015-05-06 16:53:21 -0700449 application.iconBitmap = entry.icon;
450 application.contentDescription = entry.contentDescription;
451 application.usingLowResIcon = entry.isLowResIcon;
452 }
453 }
454
455 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700456 * Returns a high res icon for the given intent and user
457 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700458 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
459 ComponentName component = intent.getComponent();
460 // null info means not installed, but if we have a component from the intent then
461 // we should still look in the cache for restored app icons.
462 if (component == null) {
463 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800464 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700465
466 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700467 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700468 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800469 }
470
Sunny Goyal34942622014-08-29 17:20:55 -0700471 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800472 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
473 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700474 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700475 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700476 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700477 ComponentName component = intent.getComponent();
478 // null info means not installed, but if we have a component from the intent then
479 // we should still look in the cache for restored app icons.
480 if (component == null) {
481 shortcutInfo.setIcon(getDefaultIcon(user));
482 shortcutInfo.title = "";
483 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700484 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700485 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800486 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700487 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700488 }
489 }
490
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800491 /**
492 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
493 */
494 public synchronized void getTitleAndIcon(
495 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700496 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
497 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700498 shortcutInfo.setIcon(getNonNullIcon(entry, user));
Winson Chung82b016c2015-05-08 17:00:10 -0700499 shortcutInfo.title = Utilities.trim(entry.title);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800500 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700501 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800502 }
Sunny Goyal34942622014-08-29 17:20:55 -0700503
Sunny Goyald180cf72015-04-06 12:45:40 -0700504 /**
505 * Fill in {@param appInfo} with the icon and label for {@param packageName}
506 */
507 public synchronized void getTitleAndIconForApp(
Hyunyoung Song3f471442015-04-08 19:01:34 -0700508 String packageName, UserHandleCompat user, boolean useLowResIcon,
509 PackageItemInfo infoOut) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700510 CacheEntry entry = getEntryForPackageLocked(packageName, user, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700511 infoOut.iconBitmap = getNonNullIcon(entry, user);
Winson Chung82b016c2015-05-08 17:00:10 -0700512 infoOut.title = Utilities.trim(entry.title);
Hyunyoung Song3f471442015-04-08 19:01:34 -0700513 infoOut.usingLowResIcon = entry.isLowResIcon;
514 infoOut.contentDescription = entry.contentDescription;
Sunny Goyald180cf72015-04-06 12:45:40 -0700515 }
516
Sunny Goyal736f5af2014-10-16 14:07:29 -0700517 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100518 if (!mDefaultIcons.containsKey(user)) {
519 mDefaultIcons.put(user, makeDefaultIcon(user));
520 }
521 return mDefaultIcons.get(user);
522 }
523
Kenny Guyed131872014-04-30 03:02:21 +0100524 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
525 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700526 }
527
Sunny Goyal736f5af2014-10-16 14:07:29 -0700528 /**
529 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
530 * This method is not thread safe, it must be called from a synchronized method.
531 */
Kenny Guyed131872014-04-30 03:02:21 +0100532 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700533 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700534 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100535 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700536 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800537 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100538 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500539
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800540 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700541 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800542 if (info != null) {
543 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500544 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700545 if (usePackageIcon) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700546 CacheEntry packageEntry = getEntryForPackageLocked(
547 componentName.getPackageName(), user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700548 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700549 if (DEBUG) Log.d(TAG, "using package default icon for " +
550 componentName.toShortString());
551 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700552 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800553 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700554 }
555 }
556 if (entry.icon == null) {
557 if (DEBUG) Log.d(TAG, "using default icon for " +
558 componentName.toShortString());
559 entry.icon = getDefaultIcon(user);
560 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700561 }
562 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800563
564 if (TextUtils.isEmpty(entry.title) && info != null) {
Winson Chung82b016c2015-05-08 17:00:10 -0700565 entry.title = info.getLabel();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800566 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
567 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800568 }
569 return entry;
570 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400571
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700572 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700573 * Adds a default package entry in the cache. This entry is not persisted and will be removed
574 * when the cache is flushed.
575 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700576 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700577 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800578 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700579
Sunny Goyald180cf72015-04-06 12:45:40 -0700580 CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700581 if (!TextUtils.isEmpty(title)) {
582 entry.title = title;
583 }
584 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700585 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700586 }
587 }
588
589 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700590 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700591 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyald180cf72015-04-06 12:45:40 -0700592 *
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700593 */
Sunny Goyald180cf72015-04-06 12:45:40 -0700594 private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
595 boolean useLowResIcon) {
596 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700597 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700598 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyald180cf72015-04-06 12:45:40 -0700599 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700600 entry = new CacheEntry();
601 mCache.put(cacheKey, entry);
602
Sunny Goyald180cf72015-04-06 12:45:40 -0700603 // Check the DB first.
604 if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
605 try {
606 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
607 ApplicationInfo appInfo = info.applicationInfo;
608 if (appInfo == null) {
609 throw new NameNotFoundException("ApplicationInfo is null");
610 }
611 Drawable drawable = mUserManager.getBadgedDrawableForUser(
612 appInfo.loadIcon(mPackageManager), user);
613 entry.icon = Utilities.createIconBitmap(drawable, mContext);
614 entry.title = appInfo.loadLabel(mPackageManager);
615 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
616 entry.isLowResIcon = false;
617
618 // Add the icon in the DB here, since these do not get written during
619 // package updates.
620 ContentValues values =
621 mIconDb.newContentValues(entry.icon, entry.title.toString());
622 addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
623
624 } catch (NameNotFoundException e) {
625 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
626 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700627 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700628 }
629 return entry;
630 }
631
Chris Wren6d0dde02014-02-10 12:16:54 -0500632 /**
633 * Pre-load an icon into the persistent cache.
634 *
635 * <P>Queries for a component that does not exist in the package manager
636 * will be answered by the persistent cache.
637 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500638 * @param componentName the icon should be returned for this component
639 * @param icon the icon to be persisted
640 * @param dpi the native density of the icon
641 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800642 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
643 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500644 // TODO rescale to the correct native DPI
645 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800646 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500647 packageManager.getActivityIcon(componentName);
648 // component is present on the system already, do nothing
649 return;
650 } catch (PackageManager.NameNotFoundException e) {
651 // pass
652 }
653
Sunny Goyal34b65272015-03-11 16:56:52 -0700654 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800655 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
656 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800657 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
658 SQLiteDatabase.CONFLICT_REPLACE);
659 }
660
Sunny Goyal34b65272015-03-11 16:56:52 -0700661 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
662 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800663 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700664 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
665 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800666 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
667 new String[] {component.flattenToString(),
668 Long.toString(mUserManager.getSerialNumberForUser(user))},
669 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500670 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800671 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700672 entry.icon = loadIconNoResize(c, 0);
673 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800674 entry.title = c.getString(1);
675 if (entry.title == null) {
676 entry.title = "";
677 entry.contentDescription = "";
678 } else {
679 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500680 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800681 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500682 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500683 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800684 c.close();
685 }
686 return false;
687 }
688
Sunny Goyal34b65272015-03-11 16:56:52 -0700689 public static class IconLoadRequest {
690 private final Runnable mRunnable;
691 private final Handler mHandler;
692
693 IconLoadRequest(Runnable runnable, Handler handler) {
694 mRunnable = runnable;
695 mHandler = handler;
696 }
697
698 public void cancel() {
699 mHandler.removeCallbacks(mRunnable);
700 }
701 }
702
Sunny Goyal9ff98082015-05-15 16:59:36 -0700703 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -0700704 * A runnable that updates invalid icons and adds missing icons in the DB for the provided
705 * LauncherActivityInfoCompat list. Items are updated/added one at a time, so that the
706 * worker thread doesn't get blocked.
Sunny Goyal9ff98082015-05-15 16:59:36 -0700707 */
Sunny Goyal75b0f552015-05-20 21:57:06 -0700708 private class SerializedIconUpdateTask implements Runnable {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700709 private final long mUserSerial;
710 private final HashMap<String, PackageInfo> mPkgInfoMap;
711 private final Stack<LauncherActivityInfoCompat> mAppsToAdd;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700712 private final Stack<LauncherActivityInfoCompat> mAppsToUpdate;
713 private final HashSet<String> mUpdatedPackages = new HashSet<String>();
Sunny Goyal9ff98082015-05-15 16:59:36 -0700714
Sunny Goyal75b0f552015-05-20 21:57:06 -0700715 private SerializedIconUpdateTask(long userSerial, HashMap<String, PackageInfo> pkgInfoMap,
716 Stack<LauncherActivityInfoCompat> appsToAdd,
717 Stack<LauncherActivityInfoCompat> appsToUpdate) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700718 mUserSerial = userSerial;
719 mPkgInfoMap = pkgInfoMap;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700720 mAppsToAdd = appsToAdd;
721 mAppsToUpdate = appsToUpdate;
Sunny Goyal9ff98082015-05-15 16:59:36 -0700722 }
723
724 @Override
725 public void run() {
Sunny Goyal75b0f552015-05-20 21:57:06 -0700726 if (!mAppsToUpdate.isEmpty()) {
727 LauncherActivityInfoCompat app = mAppsToUpdate.pop();
728 String cn = app.getComponentName().flattenToString();
729 ContentValues values = updateCacheAndGetContentValues(app, true);
730 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
731 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
732 new String[] {cn, Long.toString(mUserSerial)});
733 mUpdatedPackages.add(app.getComponentName().getPackageName());
734
735 if (mAppsToUpdate.isEmpty() && !mUpdatedPackages.isEmpty()) {
736 // No more app to update. Notify model.
737 LauncherAppState.getInstance().getModel().onPackageIconsUpdated(
738 mUpdatedPackages, mUserManager.getUserForSerialNumber(mUserSerial));
739 }
740
741 // Let it run one more time.
742 scheduleNext();
743 } else if (!mAppsToAdd.isEmpty()) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700744 LauncherActivityInfoCompat app = mAppsToAdd.pop();
745 PackageInfo info = mPkgInfoMap.get(app.getComponentName().getPackageName());
746 if (info != null) {
747 synchronized (IconCache.this) {
748 addIconToDBAndMemCache(app, info, mUserSerial);
749 }
750 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700751
752 if (!mAppsToAdd.isEmpty()) {
753 scheduleNext();
754 }
Sunny Goyal9ff98082015-05-15 16:59:36 -0700755 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700756 }
757
758 public void scheduleNext() {
759 mWorkerHandler.postAtTime(this, ICON_UPDATE_TOKEN, SystemClock.uptimeMillis() + 1);
Sunny Goyal9ff98082015-05-15 16:59:36 -0700760 }
761 }
762
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800763 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal77919b92015-05-06 16:53:21 -0700764 private final static int DB_VERSION = 4;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800765
766 private final static String TABLE_NAME = "icons";
767 private final static String COLUMN_ROWID = "rowid";
768 private final static String COLUMN_COMPONENT = "componentName";
769 private final static String COLUMN_USER = "profileId";
770 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
771 private final static String COLUMN_VERSION = "version";
772 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700773 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800774 private final static String COLUMN_LABEL = "label";
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700775 private final static String COLUMN_SYSTEM_STATE = "system_state";
776
777 public String mSystemState;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800778
779 public IconDB(Context context) {
780 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700781 updateSystemStateString(context);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800782 }
783
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700784 public void updateSystemStateString(Context c) {
785 mSystemState = Locale.getDefault().toString() + ","
786 + c.getResources().getConfiguration().mcc;
787 }
788
789
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800790 @Override
791 public void onCreate(SQLiteDatabase db) {
792 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
793 COLUMN_COMPONENT + " TEXT NOT NULL, " +
794 COLUMN_USER + " INTEGER NOT NULL, " +
795 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
796 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
797 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700798 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800799 COLUMN_LABEL + " TEXT, " +
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700800 COLUMN_SYSTEM_STATE + " TEXT, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800801 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
802 ");");
803 }
804
805 @Override
806 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
807 if (oldVersion != newVersion) {
808 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500809 }
810 }
811
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800812 @Override
813 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
814 if (oldVersion != newVersion) {
815 clearDB(db);
816 }
Kenny Guyed131872014-04-30 03:02:21 +0100817 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500818
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800819 private void clearDB(SQLiteDatabase db) {
820 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
821 onCreate(db);
822 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700823
824 public ContentValues newContentValues(Bitmap icon, String label) {
825 ContentValues values = new ContentValues();
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700826 values.put(COLUMN_ICON, Utilities.flattenBitmap(icon));
827 values.put(COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700828 Bitmap.createScaledBitmap(icon,
829 icon.getWidth() / LOW_RES_SCALE_FACTOR,
830 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700831 values.put(COLUMN_LABEL, label);
832 values.put(COLUMN_SYSTEM_STATE, mSystemState);
Sunny Goyal34b65272015-03-11 16:56:52 -0700833 return values;
834 }
835 }
836
837 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
838 byte[] data = c.getBlob(iconIndex);
839 try {
840 return BitmapFactory.decodeByteArray(data, 0, data.length);
841 } catch (Exception e) {
842 return null;
843 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500844 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800845}