blob: 432b33c6bce8aa88408c428ed8a64006116f3073 [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
19import android.content.ComponentName;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080020import android.content.ContentValues;
Winson Chungd83f5f42012-02-13 14:27:42 -080021import android.content.Context;
Joe Onorato0589f0f2010-02-08 13:44:00 -080022import android.content.Intent;
Michael Jurkadac85912012-05-18 15:04:49 -070023import android.content.pm.ActivityInfo;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070024import android.content.pm.ApplicationInfo;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080025import android.content.pm.PackageInfo;
Joe Onorato0589f0f2010-02-08 13:44:00 -080026import android.content.pm.PackageManager;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070027import android.content.pm.PackageManager.NameNotFoundException;
Michael Jurkac9a96192010-11-01 11:52:08 -070028import android.content.res.Resources;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080029import android.database.Cursor;
30import android.database.sqlite.SQLiteDatabase;
31import android.database.sqlite.SQLiteOpenHelper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080032import android.graphics.Bitmap;
Sunny Goyal34b65272015-03-11 16:56:52 -070033import android.graphics.BitmapFactory;
Romain Guya28fd3f2010-03-15 14:44:42 -070034import android.graphics.Canvas;
Joe Onorato0589f0f2010-02-08 13:44:00 -080035import android.graphics.drawable.Drawable;
Sunny Goyal34b65272015-03-11 16:56:52 -070036import android.os.Handler;
Sunny Goyal75b0f552015-05-20 21:57:06 -070037import android.os.SystemClock;
Sunny Goyal34942622014-08-29 17:20:55 -070038import android.text.TextUtils;
Chris Wren6d0dde02014-02-10 12:16:54 -050039import android.util.Log;
Joe Onorato0589f0f2010-02-08 13:44:00 -080040
Kenny Guyed131872014-04-30 03:02:21 +010041import com.android.launcher3.compat.LauncherActivityInfoCompat;
42import com.android.launcher3.compat.LauncherAppsCompat;
43import com.android.launcher3.compat.UserHandleCompat;
44import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070045import com.android.launcher3.model.PackageItemInfo;
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;
Kenny Guyed131872014-04-30 03:02:21 +010048
Joe Onorato0589f0f2010-02-08 13:44:00 -080049import java.util.HashMap;
Chris Wren6d0dde02014-02-10 12:16:54 -050050import java.util.HashSet;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080051import java.util.List;
Sunny Goyal0c9a3542015-04-01 16:04:21 -070052import java.util.Locale;
Sunny Goyal9ff98082015-05-15 16:59:36 -070053import java.util.Stack;
Joe Onorato0589f0f2010-02-08 13:44:00 -080054
55/**
56 * Cache of application icons. Icons can be made from any thread.
57 */
58public class IconCache {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070059
Joe Onorato0589f0f2010-02-08 13:44:00 -080060 private static final String TAG = "Launcher.IconCache";
61
62 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050063
Sunny Goyal0fc1be12014-08-11 17:05:23 -070064 // Empty class name is used for storing package default entry.
65 private static final String EMPTY_CLASS_NAME = ".";
66
Sunny Goyalbbef77d2014-09-09 16:27:55 -070067 private static final boolean DEBUG = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -080068
Sunny Goyal34b65272015-03-11 16:56:52 -070069 private static final int LOW_RES_SCALE_FACTOR = 8;
70
Sunny Goyal316490e2015-06-02 09:38:28 -070071 @Thunk static final Object ICON_UPDATE_TOKEN = new Object();
Sunny Goyal75b0f552015-05-20 21:57:06 -070072
Adam Cohen091440a2015-03-18 14:16:05 -070073 @Thunk static class CacheEntry {
Joe Onorato0589f0f2010-02-08 13:44:00 -080074 public Bitmap icon;
Winson Chungcdefc632015-05-28 12:53:44 -070075 public CharSequence title = "";
76 public CharSequence contentDescription = "";
Sunny Goyal34b65272015-03-11 16:56:52 -070077 public boolean isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -080078 }
79
Sunny Goyal8758ea02015-03-18 10:07:49 -070080 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
Sunny Goyal316490e2015-06-02 09:38:28 -070081 @Thunk final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
Sunny Goyal8758ea02015-03-18 10:07:49 -070082
Daniel Sandlercc8befa2013-06-11 14:45:48 -040083 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070084 private final PackageManager mPackageManager;
Sunny Goyal316490e2015-06-02 09:38:28 -070085 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +010086 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070087 private final HashMap<ComponentKey, CacheEntry> mCache =
88 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080089 private final int mIconDpi;
Sunny Goyal316490e2015-06-02 09:38:28 -070090 @Thunk final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080091
Sunny Goyal316490e2015-06-02 09:38:28 -070092 @Thunk final Handler mWorkerHandler;
Sunny Goyal34b65272015-03-11 16:56:52 -070093
Adam Cohen2e6da152015-05-06 11:42:25 -070094 public IconCache(Context context, InvariantDeviceProfile inv) {
Joe Onorato0589f0f2010-02-08 13:44:00 -080095 mContext = context;
96 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +010097 mUserManager = UserManagerCompat.getInstance(mContext);
98 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Sunny Goyal53d7ee42015-05-22 12:25:45 -070099 mIconDpi = inv.fillResIconDpi;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800100 mIconDb = new IconDB(context);
Sunny Goyal34b65272015-03-11 16:56:52 -0700101
Sunny Goyal756adbc2015-04-16 15:20:51 -0700102 mWorkerHandler = new Handler(LauncherModel.getWorkerLooper());
Romain Guya28fd3f2010-03-15 14:44:42 -0700103 }
104
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800105 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700106 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700107 }
108
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700109 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700110 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700111 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700112 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700113 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700114 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700115 }
Michael Jurka721d9722011-08-03 11:49:59 -0700116
117 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700118 }
119
Winson Chung0b9fcf52011-10-31 13:05:15 -0700120 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700121 Resources resources;
122 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700123 resources = mPackageManager.getResourcesForApplication(packageName);
124 } catch (PackageManager.NameNotFoundException e) {
125 resources = null;
126 }
127 if (resources != null) {
128 if (iconId != 0) {
129 return getFullResIcon(resources, iconId);
130 }
131 }
132 return getFullResDefaultActivityIcon();
133 }
134
Michael Jurkadac85912012-05-18 15:04:49 -0700135 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700136 Resources resources;
137 try {
138 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700139 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700140 } catch (PackageManager.NameNotFoundException e) {
141 resources = null;
142 }
143 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700144 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700145 if (iconId != 0) {
146 return getFullResIcon(resources, iconId);
147 }
148 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500149
Michael Jurkac9a96192010-11-01 11:52:08 -0700150 return getFullResDefaultActivityIcon();
151 }
152
Kenny Guyed131872014-04-30 03:02:21 +0100153 private Bitmap makeDefaultIcon(UserHandleCompat user) {
154 Drawable unbadged = getFullResDefaultActivityIcon();
155 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700156 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
157 Math.max(d.getIntrinsicHeight(), 1),
158 Bitmap.Config.ARGB_8888);
159 Canvas c = new Canvas(b);
160 d.setBounds(0, 0, b.getWidth(), b.getHeight());
161 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700162 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700163 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800164 }
165
166 /**
167 * Remove any records for the supplied ComponentName.
168 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700169 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700170 mCache.remove(new ComponentKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800171 }
172
173 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800174 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500175 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800176 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700177 HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
178 for (ComponentKey key: mCache.keySet()) {
Kenny Guyed131872014-04-30 03:02:21 +0100179 if (key.componentName.getPackageName().equals(packageName)
180 && key.user.equals(user)) {
181 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500182 }
183 }
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700184 for (ComponentKey condemned: forDeletion) {
Kenny Guyed131872014-04-30 03:02:21 +0100185 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500186 }
187 }
188
189 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800190 * Updates the entries related to the given package in memory and persistent DB.
191 */
192 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
193 removeIconsForPkg(packageName, user);
194 try {
195 PackageInfo info = mPackageManager.getPackageInfo(packageName,
196 PackageManager.GET_UNINSTALLED_PACKAGES);
197 long userSerial = mUserManager.getSerialNumberForUser(user);
198 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700199 addIconToDBAndMemCache(app, info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800200 }
201 } catch (NameNotFoundException e) {
202 Log.d(TAG, "Package not found", e);
203 return;
204 }
205 }
206
207 /**
208 * Removes the entries related to the given package in memory and persistent DB.
209 */
210 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
211 removeFromMemCacheLocked(packageName, user);
212 long userSerial = mUserManager.getSerialNumberForUser(user);
213 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
214 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
215 new String[] {packageName + "/%", Long.toString(userSerial)});
216 }
217
Sunny Goyal75b0f552015-05-20 21:57:06 -0700218 public void updateDbIcons() {
219 // Remove all active icon update tasks.
220 mWorkerHandler.removeCallbacksAndMessages(ICON_UPDATE_TOKEN);
221
222 mIconDb.updateSystemStateString(mContext);
223 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
224 // Query for the set of apps
225 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
226 // Fail if we don't have any apps
227 // TODO: Fix this. Only fail for the current user.
228 if (apps == null || apps.isEmpty()) {
229 return;
230 }
231
232 // Update icon cache. This happens in segments and {@link #onPackageIconsUpdated}
233 // is called by the icon cache when the job is complete.
234 updateDBIcons(user, apps);
235 }
236 }
237
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800238 /**
239 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
240 * the DB and are updated.
241 * @return The set of packages for which icons have updated.
242 */
Sunny Goyal75b0f552015-05-20 21:57:06 -0700243 private void updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800244 long userSerial = mUserManager.getSerialNumberForUser(user);
245 PackageManager pm = mContext.getPackageManager();
246 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
247 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
248 pkgInfoMap.put(info.packageName, info);
249 }
250
251 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
252 for (LauncherActivityInfoCompat app : apps) {
253 componentMap.put(app.getComponentName(), app);
254 }
255
256 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
257 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700258 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION,
259 IconDB.COLUMN_SYSTEM_STATE},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800260 IconDB.COLUMN_USER + " = ? ",
261 new String[] {Long.toString(userSerial)},
262 null, null, null);
263
264 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
265 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
266 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
267 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700268 final int systemStateIndex = c.getColumnIndex(IconDB.COLUMN_SYSTEM_STATE);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800269
270 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
Sunny Goyal75b0f552015-05-20 21:57:06 -0700271 Stack<LauncherActivityInfoCompat> appsToUpdate = new Stack<>();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800272
273 while (c.moveToNext()) {
274 String cn = c.getString(indexComponent);
275 ComponentName component = ComponentName.unflattenFromString(cn);
276 PackageInfo info = pkgInfoMap.get(component.getPackageName());
277 if (info == null) {
Sunny Goyal091f0ff2015-06-04 15:19:31 -0700278 remove(component, user);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800279 itemsToRemove.add(c.getInt(rowIndex));
280 continue;
281 }
282 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
283 // Application is not present
284 continue;
285 }
286
287 long updateTime = c.getLong(indexLastUpdate);
288 int version = c.getInt(indexVersion);
289 LauncherActivityInfoCompat app = componentMap.remove(component);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700290 if (version == info.versionCode && updateTime == info.lastUpdateTime &&
291 TextUtils.equals(mIconDb.mSystemState, c.getString(systemStateIndex))) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800292 continue;
293 }
294 if (app == null) {
Sunny Goyal091f0ff2015-06-04 15:19:31 -0700295 remove(component, user);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800296 itemsToRemove.add(c.getInt(rowIndex));
Sunny Goyal75b0f552015-05-20 21:57:06 -0700297 } else {
298 appsToUpdate.add(app);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800299 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800300 }
301 c.close();
302 if (!itemsToRemove.isEmpty()) {
303 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
Sunny Goyalb1622cc2015-06-10 16:00:42 -0700304 Utilities.createDbSelectionQuery(IconDB.COLUMN_ROWID, itemsToRemove),
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800305 null);
306 }
307
308 // Insert remaining apps.
Sunny Goyal75b0f552015-05-20 21:57:06 -0700309 if (!componentMap.isEmpty() || !appsToUpdate.isEmpty()) {
310 Stack<LauncherActivityInfoCompat> appsToAdd = new Stack<>();
311 appsToAdd.addAll(componentMap.values());
312 new SerializedIconUpdateTask(userSerial, pkgInfoMap,
313 appsToAdd, appsToUpdate).scheduleNext();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800314 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800315 }
316
Sunny Goyal316490e2015-06-02 09:38:28 -0700317 @Thunk void addIconToDBAndMemCache(LauncherActivityInfoCompat app, PackageInfo info,
Sunny Goyald180cf72015-04-06 12:45:40 -0700318 long userSerial) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700319 // Reuse the existing entry if it already exists in the DB. This ensures that we do not
320 // create bitmap if it was already created during loader.
321 ContentValues values = updateCacheAndGetContentValues(app, false);
Sunny Goyald180cf72015-04-06 12:45:40 -0700322 addIconToDB(values, app.getComponentName(), info, userSerial);
Sunny Goyald180cf72015-04-06 12:45:40 -0700323 }
324
325 /**
326 * Updates {@param values} to contain versoning information and adds it to the DB.
327 * @param values {@link ContentValues} containing icon & title
328 */
329 private void addIconToDB(ContentValues values, ComponentName key,
330 PackageInfo info, long userSerial) {
331 values.put(IconDB.COLUMN_COMPONENT, key.flattenToString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800332 values.put(IconDB.COLUMN_USER, userSerial);
333 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
334 values.put(IconDB.COLUMN_VERSION, info.versionCode);
335 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
336 SQLiteDatabase.CONFLICT_REPLACE);
337 }
338
Sunny Goyal316490e2015-06-02 09:38:28 -0700339 @Thunk ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app,
Sunny Goyal9ff98082015-05-15 16:59:36 -0700340 boolean replaceExisting) {
341 final ComponentKey key = new ComponentKey(app.getComponentName(), app.getUser());
342 CacheEntry entry = null;
343 if (!replaceExisting) {
344 entry = mCache.get(key);
345 // We can't reuse the entry if the high-res icon is not present.
346 if (entry == null || entry.isLowResIcon || entry.icon == null) {
347 entry = null;
348 }
349 }
350 if (entry == null) {
351 entry = new CacheEntry();
352 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
353 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800354 entry.title = app.getLabel();
355 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700356 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800357
Sunny Goyal34b65272015-03-11 16:56:52 -0700358 return mIconDb.newContentValues(entry.icon, entry.title.toString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800359 }
360
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800361 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700362 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
363 * @return a request ID that can be used to cancel the request.
364 */
365 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
366 Runnable request = new Runnable() {
367
368 @Override
369 public void run() {
370 if (info instanceof AppInfo) {
371 getTitleAndIcon((AppInfo) info, null, false);
372 } else if (info instanceof ShortcutInfo) {
373 ShortcutInfo st = (ShortcutInfo) info;
374 getTitleAndIcon(st,
375 st.promisedIntent != null ? st.promisedIntent : st.intent,
376 st.user, false);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700377 } else if (info instanceof PackageItemInfo) {
378 PackageItemInfo pti = (PackageItemInfo) info;
379 getTitleAndIconForApp(pti.packageName, pti.user, false, pti);
Sunny Goyal34b65272015-03-11 16:56:52 -0700380 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700381 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700382
383 @Override
384 public void run() {
385 caller.reapplyItemInfo(info);
386 }
387 });
388 }
389 };
390 mWorkerHandler.post(request);
391 return new IconLoadRequest(request, mWorkerHandler);
392 }
393
Sunny Goyal756adbc2015-04-16 15:20:51 -0700394 private Bitmap getNonNullIcon(CacheEntry entry, UserHandleCompat user) {
395 return entry.icon == null ? getDefaultIcon(user) : entry.icon;
396 }
397
Sunny Goyal34b65272015-03-11 16:56:52 -0700398 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800399 * Fill in "application" with the icon and label for "info."
400 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700401 public synchronized void getTitleAndIcon(AppInfo application,
402 LauncherActivityInfoCompat info, boolean useLowResIcon) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700403 UserHandleCompat user = info == null ? application.user : info.getUser();
404 CacheEntry entry = cacheLocked(application.componentName, info, user,
Sunny Goyal34b65272015-03-11 16:56:52 -0700405 false, useLowResIcon);
Winson Chung82b016c2015-05-08 17:00:10 -0700406 application.title = Utilities.trim(entry.title);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700407 application.iconBitmap = getNonNullIcon(entry, user);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700408 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700409 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800410 }
411
Sunny Goyal34b65272015-03-11 16:56:52 -0700412 /**
Sunny Goyal77919b92015-05-06 16:53:21 -0700413 * Updates {@param application} only if a valid entry is found.
414 */
415 public synchronized void updateTitleAndIcon(AppInfo application) {
416 CacheEntry entry = cacheLocked(application.componentName, null, application.user,
417 false, application.usingLowResIcon);
418 if (entry.icon != null && !isDefaultIcon(entry.icon, application.user)) {
Winson Chung82b016c2015-05-08 17:00:10 -0700419 application.title = Utilities.trim(entry.title);
Sunny Goyal77919b92015-05-06 16:53:21 -0700420 application.iconBitmap = entry.icon;
421 application.contentDescription = entry.contentDescription;
422 application.usingLowResIcon = entry.isLowResIcon;
423 }
424 }
425
426 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700427 * Returns a high res icon for the given intent and user
428 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700429 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
430 ComponentName component = intent.getComponent();
431 // null info means not installed, but if we have a component from the intent then
432 // we should still look in the cache for restored app icons.
433 if (component == null) {
434 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800435 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700436
437 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700438 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700439 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800440 }
441
Sunny Goyal34942622014-08-29 17:20:55 -0700442 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800443 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
444 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700445 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700446 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700447 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700448 ComponentName component = intent.getComponent();
449 // null info means not installed, but if we have a component from the intent then
450 // we should still look in the cache for restored app icons.
451 if (component == null) {
452 shortcutInfo.setIcon(getDefaultIcon(user));
453 shortcutInfo.title = "";
454 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700455 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700456 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800457 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700458 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700459 }
460 }
461
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800462 /**
463 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
464 */
465 public synchronized void getTitleAndIcon(
466 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700467 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
468 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700469 shortcutInfo.setIcon(getNonNullIcon(entry, user));
Winson Chung82b016c2015-05-08 17:00:10 -0700470 shortcutInfo.title = Utilities.trim(entry.title);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800471 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700472 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800473 }
Sunny Goyal34942622014-08-29 17:20:55 -0700474
Sunny Goyald180cf72015-04-06 12:45:40 -0700475 /**
476 * Fill in {@param appInfo} with the icon and label for {@param packageName}
477 */
478 public synchronized void getTitleAndIconForApp(
Hyunyoung Song3f471442015-04-08 19:01:34 -0700479 String packageName, UserHandleCompat user, boolean useLowResIcon,
480 PackageItemInfo infoOut) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700481 CacheEntry entry = getEntryForPackageLocked(packageName, user, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700482 infoOut.iconBitmap = getNonNullIcon(entry, user);
Winson Chung82b016c2015-05-08 17:00:10 -0700483 infoOut.title = Utilities.trim(entry.title);
Hyunyoung Song3f471442015-04-08 19:01:34 -0700484 infoOut.usingLowResIcon = entry.isLowResIcon;
485 infoOut.contentDescription = entry.contentDescription;
Sunny Goyald180cf72015-04-06 12:45:40 -0700486 }
487
Sunny Goyal736f5af2014-10-16 14:07:29 -0700488 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100489 if (!mDefaultIcons.containsKey(user)) {
490 mDefaultIcons.put(user, makeDefaultIcon(user));
491 }
492 return mDefaultIcons.get(user);
493 }
494
Kenny Guyed131872014-04-30 03:02:21 +0100495 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
496 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700497 }
498
Sunny Goyal736f5af2014-10-16 14:07:29 -0700499 /**
500 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
501 * This method is not thread safe, it must be called from a synchronized method.
502 */
Kenny Guyed131872014-04-30 03:02:21 +0100503 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700504 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700505 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100506 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700507 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800508 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100509 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500510
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800511 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700512 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800513 if (info != null) {
514 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500515 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700516 if (usePackageIcon) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700517 CacheEntry packageEntry = getEntryForPackageLocked(
518 componentName.getPackageName(), user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700519 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700520 if (DEBUG) Log.d(TAG, "using package default icon for " +
521 componentName.toShortString());
522 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700523 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800524 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700525 }
526 }
527 if (entry.icon == null) {
528 if (DEBUG) Log.d(TAG, "using default icon for " +
529 componentName.toShortString());
530 entry.icon = getDefaultIcon(user);
531 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700532 }
533 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800534
535 if (TextUtils.isEmpty(entry.title) && info != null) {
Winson Chung82b016c2015-05-08 17:00:10 -0700536 entry.title = info.getLabel();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800537 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
538 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800539 }
540 return entry;
541 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400542
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700543 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700544 * Adds a default package entry in the cache. This entry is not persisted and will be removed
545 * when the cache is flushed.
546 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700547 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700548 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800549 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700550
Sunny Goyald180cf72015-04-06 12:45:40 -0700551 CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700552 if (!TextUtils.isEmpty(title)) {
553 entry.title = title;
554 }
555 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700556 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700557 }
558 }
559
560 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700561 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700562 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyald180cf72015-04-06 12:45:40 -0700563 *
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700564 */
Sunny Goyald180cf72015-04-06 12:45:40 -0700565 private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
566 boolean useLowResIcon) {
Sunny Goyal091f0ff2015-06-04 15:19:31 -0700567 ComponentName cn = new ComponentName(packageName, packageName + EMPTY_CLASS_NAME);
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700568 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700569 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal091f0ff2015-06-04 15:19:31 -0700570
Sunny Goyald180cf72015-04-06 12:45:40 -0700571 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700572 entry = new CacheEntry();
Winson Chungcdefc632015-05-28 12:53:44 -0700573 boolean entryUpdated = true;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700574
Sunny Goyald180cf72015-04-06 12:45:40 -0700575 // Check the DB first.
576 if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
577 try {
578 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
579 ApplicationInfo appInfo = info.applicationInfo;
580 if (appInfo == null) {
581 throw new NameNotFoundException("ApplicationInfo is null");
582 }
583 Drawable drawable = mUserManager.getBadgedDrawableForUser(
584 appInfo.loadIcon(mPackageManager), user);
585 entry.icon = Utilities.createIconBitmap(drawable, mContext);
586 entry.title = appInfo.loadLabel(mPackageManager);
587 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
588 entry.isLowResIcon = false;
589
590 // Add the icon in the DB here, since these do not get written during
591 // package updates.
592 ContentValues values =
593 mIconDb.newContentValues(entry.icon, entry.title.toString());
594 addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
595
596 } catch (NameNotFoundException e) {
597 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
Winson Chungcdefc632015-05-28 12:53:44 -0700598 entryUpdated = false;
Sunny Goyald180cf72015-04-06 12:45:40 -0700599 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700600 }
Winson Chungcdefc632015-05-28 12:53:44 -0700601
602 // Only add a filled-out entry to the cache
603 if (entryUpdated) {
604 mCache.put(cacheKey, entry);
605 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700606 }
607 return entry;
608 }
609
Chris Wren6d0dde02014-02-10 12:16:54 -0500610 /**
611 * Pre-load an icon into the persistent cache.
612 *
613 * <P>Queries for a component that does not exist in the package manager
614 * will be answered by the persistent cache.
615 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500616 * @param componentName the icon should be returned for this component
617 * @param icon the icon to be persisted
618 * @param dpi the native density of the icon
619 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800620 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
621 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500622 // TODO rescale to the correct native DPI
623 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800624 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500625 packageManager.getActivityIcon(componentName);
626 // component is present on the system already, do nothing
627 return;
628 } catch (PackageManager.NameNotFoundException e) {
629 // pass
630 }
631
Sunny Goyal34b65272015-03-11 16:56:52 -0700632 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800633 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
634 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800635 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
636 SQLiteDatabase.CONFLICT_REPLACE);
637 }
638
Sunny Goyal34b65272015-03-11 16:56:52 -0700639 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
640 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800641 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700642 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
643 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800644 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
645 new String[] {component.flattenToString(),
646 Long.toString(mUserManager.getSerialNumberForUser(user))},
647 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500648 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800649 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700650 entry.icon = loadIconNoResize(c, 0);
651 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800652 entry.title = c.getString(1);
653 if (entry.title == null) {
654 entry.title = "";
655 entry.contentDescription = "";
656 } else {
657 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500658 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800659 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500660 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500661 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800662 c.close();
663 }
664 return false;
665 }
666
Sunny Goyal34b65272015-03-11 16:56:52 -0700667 public static class IconLoadRequest {
668 private final Runnable mRunnable;
669 private final Handler mHandler;
670
671 IconLoadRequest(Runnable runnable, Handler handler) {
672 mRunnable = runnable;
673 mHandler = handler;
674 }
675
676 public void cancel() {
677 mHandler.removeCallbacks(mRunnable);
678 }
679 }
680
Sunny Goyal9ff98082015-05-15 16:59:36 -0700681 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -0700682 * A runnable that updates invalid icons and adds missing icons in the DB for the provided
683 * LauncherActivityInfoCompat list. Items are updated/added one at a time, so that the
684 * worker thread doesn't get blocked.
Sunny Goyal9ff98082015-05-15 16:59:36 -0700685 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700686 @Thunk class SerializedIconUpdateTask implements Runnable {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700687 private final long mUserSerial;
688 private final HashMap<String, PackageInfo> mPkgInfoMap;
689 private final Stack<LauncherActivityInfoCompat> mAppsToAdd;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700690 private final Stack<LauncherActivityInfoCompat> mAppsToUpdate;
691 private final HashSet<String> mUpdatedPackages = new HashSet<String>();
Sunny Goyal9ff98082015-05-15 16:59:36 -0700692
Sunny Goyal316490e2015-06-02 09:38:28 -0700693 @Thunk SerializedIconUpdateTask(long userSerial, HashMap<String, PackageInfo> pkgInfoMap,
Sunny Goyal75b0f552015-05-20 21:57:06 -0700694 Stack<LauncherActivityInfoCompat> appsToAdd,
695 Stack<LauncherActivityInfoCompat> appsToUpdate) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700696 mUserSerial = userSerial;
697 mPkgInfoMap = pkgInfoMap;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700698 mAppsToAdd = appsToAdd;
699 mAppsToUpdate = appsToUpdate;
Sunny Goyal9ff98082015-05-15 16:59:36 -0700700 }
701
702 @Override
703 public void run() {
Sunny Goyal75b0f552015-05-20 21:57:06 -0700704 if (!mAppsToUpdate.isEmpty()) {
705 LauncherActivityInfoCompat app = mAppsToUpdate.pop();
706 String cn = app.getComponentName().flattenToString();
707 ContentValues values = updateCacheAndGetContentValues(app, true);
708 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
709 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
710 new String[] {cn, Long.toString(mUserSerial)});
711 mUpdatedPackages.add(app.getComponentName().getPackageName());
712
713 if (mAppsToUpdate.isEmpty() && !mUpdatedPackages.isEmpty()) {
714 // No more app to update. Notify model.
715 LauncherAppState.getInstance().getModel().onPackageIconsUpdated(
716 mUpdatedPackages, mUserManager.getUserForSerialNumber(mUserSerial));
717 }
718
719 // Let it run one more time.
720 scheduleNext();
721 } else if (!mAppsToAdd.isEmpty()) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700722 LauncherActivityInfoCompat app = mAppsToAdd.pop();
723 PackageInfo info = mPkgInfoMap.get(app.getComponentName().getPackageName());
724 if (info != null) {
725 synchronized (IconCache.this) {
726 addIconToDBAndMemCache(app, info, mUserSerial);
727 }
728 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700729
730 if (!mAppsToAdd.isEmpty()) {
731 scheduleNext();
732 }
Sunny Goyal9ff98082015-05-15 16:59:36 -0700733 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700734 }
735
736 public void scheduleNext() {
737 mWorkerHandler.postAtTime(this, ICON_UPDATE_TOKEN, SystemClock.uptimeMillis() + 1);
Sunny Goyal9ff98082015-05-15 16:59:36 -0700738 }
739 }
740
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800741 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700742 private final static int DB_VERSION = 5;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800743
744 private final static String TABLE_NAME = "icons";
745 private final static String COLUMN_ROWID = "rowid";
746 private final static String COLUMN_COMPONENT = "componentName";
747 private final static String COLUMN_USER = "profileId";
748 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
749 private final static String COLUMN_VERSION = "version";
750 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700751 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800752 private final static String COLUMN_LABEL = "label";
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700753 private final static String COLUMN_SYSTEM_STATE = "system_state";
754
755 public String mSystemState;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800756
757 public IconDB(Context context) {
758 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700759 updateSystemStateString(context);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800760 }
761
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700762 public void updateSystemStateString(Context c) {
763 mSystemState = Locale.getDefault().toString() + ","
764 + c.getResources().getConfiguration().mcc;
765 }
766
767
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800768 @Override
769 public void onCreate(SQLiteDatabase db) {
770 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
771 COLUMN_COMPONENT + " TEXT NOT NULL, " +
772 COLUMN_USER + " INTEGER NOT NULL, " +
773 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
774 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
775 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700776 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800777 COLUMN_LABEL + " TEXT, " +
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700778 COLUMN_SYSTEM_STATE + " TEXT, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800779 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
780 ");");
781 }
782
783 @Override
784 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
785 if (oldVersion != newVersion) {
786 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500787 }
788 }
789
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800790 @Override
791 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
792 if (oldVersion != newVersion) {
793 clearDB(db);
794 }
Kenny Guyed131872014-04-30 03:02:21 +0100795 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500796
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800797 private void clearDB(SQLiteDatabase db) {
798 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
799 onCreate(db);
800 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700801
802 public ContentValues newContentValues(Bitmap icon, String label) {
803 ContentValues values = new ContentValues();
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700804 values.put(COLUMN_ICON, Utilities.flattenBitmap(icon));
805 values.put(COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700806 Bitmap.createScaledBitmap(icon,
807 icon.getWidth() / LOW_RES_SCALE_FACTOR,
808 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700809 values.put(COLUMN_LABEL, label);
810 values.put(COLUMN_SYSTEM_STATE, mSystemState);
Sunny Goyal34b65272015-03-11 16:56:52 -0700811 return values;
812 }
813 }
814
815 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
816 byte[] data = c.getBlob(iconIndex);
817 try {
818 return BitmapFactory.decodeByteArray(data, 0, data.length);
819 } catch (Exception e) {
820 return null;
821 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500822 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800823}