blob: 14ad337998cfaa92b1600ccb8f62b01135939667 [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
Sunny Goyal4e5cc642015-06-25 16:37:44 -070049import java.util.Collections;
Joe Onorato0589f0f2010-02-08 13:44:00 -080050import java.util.HashMap;
Chris Wren6d0dde02014-02-10 12:16:54 -050051import java.util.HashSet;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080052import java.util.List;
Sunny Goyal0c9a3542015-04-01 16:04:21 -070053import java.util.Locale;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070054import java.util.Set;
Sunny Goyal9ff98082015-05-15 16:59:36 -070055import java.util.Stack;
Joe Onorato0589f0f2010-02-08 13:44:00 -080056
57/**
58 * Cache of application icons. Icons can be made from any thread.
59 */
60public class IconCache {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070061
Joe Onorato0589f0f2010-02-08 13:44:00 -080062 private static final String TAG = "Launcher.IconCache";
63
64 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050065
Sunny Goyal0fc1be12014-08-11 17:05:23 -070066 // Empty class name is used for storing package default entry.
67 private static final String EMPTY_CLASS_NAME = ".";
68
Sunny Goyalbbef77d2014-09-09 16:27:55 -070069 private static final boolean DEBUG = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -080070
Sunny Goyal34b65272015-03-11 16:56:52 -070071 private static final int LOW_RES_SCALE_FACTOR = 8;
72
Sunny Goyal316490e2015-06-02 09:38:28 -070073 @Thunk static final Object ICON_UPDATE_TOKEN = new Object();
Sunny Goyal75b0f552015-05-20 21:57:06 -070074
Adam Cohen091440a2015-03-18 14:16:05 -070075 @Thunk static class CacheEntry {
Joe Onorato0589f0f2010-02-08 13:44:00 -080076 public Bitmap icon;
Winson Chungcdefc632015-05-28 12:53:44 -070077 public CharSequence title = "";
78 public CharSequence contentDescription = "";
Sunny Goyal34b65272015-03-11 16:56:52 -070079 public boolean isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -080080 }
81
Sunny Goyal8758ea02015-03-18 10:07:49 -070082 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
Sunny Goyal316490e2015-06-02 09:38:28 -070083 @Thunk final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
Sunny Goyal8758ea02015-03-18 10:07:49 -070084
Daniel Sandlercc8befa2013-06-11 14:45:48 -040085 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070086 private final PackageManager mPackageManager;
Sunny Goyal316490e2015-06-02 09:38:28 -070087 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +010088 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070089 private final HashMap<ComponentKey, CacheEntry> mCache =
90 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080091 private final int mIconDpi;
Sunny Goyal316490e2015-06-02 09:38:28 -070092 @Thunk final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080093
Sunny Goyal316490e2015-06-02 09:38:28 -070094 @Thunk final Handler mWorkerHandler;
Sunny Goyal34b65272015-03-11 16:56:52 -070095
Adam Cohen2e6da152015-05-06 11:42:25 -070096 public IconCache(Context context, InvariantDeviceProfile inv) {
Joe Onorato0589f0f2010-02-08 13:44:00 -080097 mContext = context;
98 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +010099 mUserManager = UserManagerCompat.getInstance(mContext);
100 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700101 mIconDpi = inv.fillResIconDpi;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800102 mIconDb = new IconDB(context);
Sunny Goyal34b65272015-03-11 16:56:52 -0700103
Sunny Goyal756adbc2015-04-16 15:20:51 -0700104 mWorkerHandler = new Handler(LauncherModel.getWorkerLooper());
Romain Guya28fd3f2010-03-15 14:44:42 -0700105 }
106
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800107 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700108 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700109 }
110
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700111 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700112 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700113 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700114 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700115 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700116 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700117 }
Michael Jurka721d9722011-08-03 11:49:59 -0700118
119 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700120 }
121
Winson Chung0b9fcf52011-10-31 13:05:15 -0700122 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700123 Resources resources;
124 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700125 resources = mPackageManager.getResourcesForApplication(packageName);
126 } catch (PackageManager.NameNotFoundException e) {
127 resources = null;
128 }
129 if (resources != null) {
130 if (iconId != 0) {
131 return getFullResIcon(resources, iconId);
132 }
133 }
134 return getFullResDefaultActivityIcon();
135 }
136
Michael Jurkadac85912012-05-18 15:04:49 -0700137 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700138 Resources resources;
139 try {
140 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700141 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700142 } catch (PackageManager.NameNotFoundException e) {
143 resources = null;
144 }
145 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700146 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700147 if (iconId != 0) {
148 return getFullResIcon(resources, iconId);
149 }
150 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500151
Michael Jurkac9a96192010-11-01 11:52:08 -0700152 return getFullResDefaultActivityIcon();
153 }
154
Kenny Guyed131872014-04-30 03:02:21 +0100155 private Bitmap makeDefaultIcon(UserHandleCompat user) {
156 Drawable unbadged = getFullResDefaultActivityIcon();
157 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700158 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
159 Math.max(d.getIntrinsicHeight(), 1),
160 Bitmap.Config.ARGB_8888);
161 Canvas c = new Canvas(b);
162 d.setBounds(0, 0, b.getWidth(), b.getHeight());
163 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700164 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700165 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800166 }
167
168 /**
169 * Remove any records for the supplied ComponentName.
170 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700171 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700172 mCache.remove(new ComponentKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800173 }
174
175 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800176 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500177 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800178 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700179 HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
180 for (ComponentKey key: mCache.keySet()) {
Kenny Guyed131872014-04-30 03:02:21 +0100181 if (key.componentName.getPackageName().equals(packageName)
182 && key.user.equals(user)) {
183 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500184 }
185 }
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700186 for (ComponentKey condemned: forDeletion) {
Kenny Guyed131872014-04-30 03:02:21 +0100187 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500188 }
189 }
190
191 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800192 * Updates the entries related to the given package in memory and persistent DB.
193 */
194 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
195 removeIconsForPkg(packageName, user);
196 try {
197 PackageInfo info = mPackageManager.getPackageInfo(packageName,
198 PackageManager.GET_UNINSTALLED_PACKAGES);
199 long userSerial = mUserManager.getSerialNumberForUser(user);
200 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700201 addIconToDBAndMemCache(app, info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800202 }
203 } catch (NameNotFoundException e) {
204 Log.d(TAG, "Package not found", e);
205 return;
206 }
207 }
208
209 /**
210 * Removes the entries related to the given package in memory and persistent DB.
211 */
212 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
213 removeFromMemCacheLocked(packageName, user);
214 long userSerial = mUserManager.getSerialNumberForUser(user);
215 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
216 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
217 new String[] {packageName + "/%", Long.toString(userSerial)});
218 }
219
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700220 public void updateDbIcons(Set<String> ignorePackagesForMainUser) {
Sunny Goyal75b0f552015-05-20 21:57:06 -0700221 // Remove all active icon update tasks.
222 mWorkerHandler.removeCallbacksAndMessages(ICON_UPDATE_TOKEN);
223
Sunny Goyal2003c752015-06-18 13:56:59 -0700224 mIconDb.updateSystemStateString();
Sunny Goyal75b0f552015-05-20 21:57:06 -0700225 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
226 // Query for the set of apps
227 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
228 // Fail if we don't have any apps
229 // TODO: Fix this. Only fail for the current user.
230 if (apps == null || apps.isEmpty()) {
231 return;
232 }
233
234 // Update icon cache. This happens in segments and {@link #onPackageIconsUpdated}
235 // is called by the icon cache when the job is complete.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700236 updateDBIcons(user, apps, UserHandleCompat.myUserHandle().equals(user)
237 ? ignorePackagesForMainUser : Collections.<String>emptySet());
Sunny Goyal75b0f552015-05-20 21:57:06 -0700238 }
239 }
240
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800241 /**
242 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
243 * the DB and are updated.
244 * @return The set of packages for which icons have updated.
245 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700246 private void updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps,
247 Set<String> ignorePackages) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800248 long userSerial = mUserManager.getSerialNumberForUser(user);
249 PackageManager pm = mContext.getPackageManager();
250 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
251 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
252 pkgInfoMap.put(info.packageName, info);
253 }
254
255 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
256 for (LauncherActivityInfoCompat app : apps) {
257 componentMap.put(app.getComponentName(), app);
258 }
259
260 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
261 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700262 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION,
263 IconDB.COLUMN_SYSTEM_STATE},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800264 IconDB.COLUMN_USER + " = ? ",
265 new String[] {Long.toString(userSerial)},
266 null, null, null);
267
268 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
269 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
270 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
271 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700272 final int systemStateIndex = c.getColumnIndex(IconDB.COLUMN_SYSTEM_STATE);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800273
274 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
Sunny Goyal75b0f552015-05-20 21:57:06 -0700275 Stack<LauncherActivityInfoCompat> appsToUpdate = new Stack<>();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800276
277 while (c.moveToNext()) {
278 String cn = c.getString(indexComponent);
279 ComponentName component = ComponentName.unflattenFromString(cn);
280 PackageInfo info = pkgInfoMap.get(component.getPackageName());
281 if (info == null) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700282 if (!ignorePackages.contains(component.getPackageName())) {
283 remove(component, user);
284 itemsToRemove.add(c.getInt(rowIndex));
285 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800286 continue;
287 }
288 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
289 // Application is not present
290 continue;
291 }
292
293 long updateTime = c.getLong(indexLastUpdate);
294 int version = c.getInt(indexVersion);
295 LauncherActivityInfoCompat app = componentMap.remove(component);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700296 if (version == info.versionCode && updateTime == info.lastUpdateTime &&
297 TextUtils.equals(mIconDb.mSystemState, c.getString(systemStateIndex))) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800298 continue;
299 }
300 if (app == null) {
Sunny Goyal091f0ff2015-06-04 15:19:31 -0700301 remove(component, user);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800302 itemsToRemove.add(c.getInt(rowIndex));
Sunny Goyal75b0f552015-05-20 21:57:06 -0700303 } else {
304 appsToUpdate.add(app);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800305 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800306 }
307 c.close();
308 if (!itemsToRemove.isEmpty()) {
309 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
Sunny Goyalb1622cc2015-06-10 16:00:42 -0700310 Utilities.createDbSelectionQuery(IconDB.COLUMN_ROWID, itemsToRemove),
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800311 null);
312 }
313
314 // Insert remaining apps.
Sunny Goyal75b0f552015-05-20 21:57:06 -0700315 if (!componentMap.isEmpty() || !appsToUpdate.isEmpty()) {
316 Stack<LauncherActivityInfoCompat> appsToAdd = new Stack<>();
317 appsToAdd.addAll(componentMap.values());
318 new SerializedIconUpdateTask(userSerial, pkgInfoMap,
319 appsToAdd, appsToUpdate).scheduleNext();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800320 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800321 }
322
Sunny Goyal316490e2015-06-02 09:38:28 -0700323 @Thunk void addIconToDBAndMemCache(LauncherActivityInfoCompat app, PackageInfo info,
Sunny Goyald180cf72015-04-06 12:45:40 -0700324 long userSerial) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700325 // Reuse the existing entry if it already exists in the DB. This ensures that we do not
326 // create bitmap if it was already created during loader.
327 ContentValues values = updateCacheAndGetContentValues(app, false);
Sunny Goyald180cf72015-04-06 12:45:40 -0700328 addIconToDB(values, app.getComponentName(), info, userSerial);
Sunny Goyald180cf72015-04-06 12:45:40 -0700329 }
330
331 /**
332 * Updates {@param values} to contain versoning information and adds it to the DB.
333 * @param values {@link ContentValues} containing icon & title
334 */
335 private void addIconToDB(ContentValues values, ComponentName key,
336 PackageInfo info, long userSerial) {
337 values.put(IconDB.COLUMN_COMPONENT, key.flattenToString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800338 values.put(IconDB.COLUMN_USER, userSerial);
339 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
340 values.put(IconDB.COLUMN_VERSION, info.versionCode);
341 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
342 SQLiteDatabase.CONFLICT_REPLACE);
343 }
344
Sunny Goyal316490e2015-06-02 09:38:28 -0700345 @Thunk ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app,
Sunny Goyal9ff98082015-05-15 16:59:36 -0700346 boolean replaceExisting) {
347 final ComponentKey key = new ComponentKey(app.getComponentName(), app.getUser());
348 CacheEntry entry = null;
349 if (!replaceExisting) {
350 entry = mCache.get(key);
351 // We can't reuse the entry if the high-res icon is not present.
352 if (entry == null || entry.isLowResIcon || entry.icon == null) {
353 entry = null;
354 }
355 }
356 if (entry == null) {
357 entry = new CacheEntry();
358 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
359 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800360 entry.title = app.getLabel();
361 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700362 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800363
Sunny Goyal34b65272015-03-11 16:56:52 -0700364 return mIconDb.newContentValues(entry.icon, entry.title.toString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800365 }
366
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800367 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700368 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
369 * @return a request ID that can be used to cancel the request.
370 */
371 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
372 Runnable request = new Runnable() {
373
374 @Override
375 public void run() {
376 if (info instanceof AppInfo) {
377 getTitleAndIcon((AppInfo) info, null, false);
378 } else if (info instanceof ShortcutInfo) {
379 ShortcutInfo st = (ShortcutInfo) info;
380 getTitleAndIcon(st,
381 st.promisedIntent != null ? st.promisedIntent : st.intent,
382 st.user, false);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700383 } else if (info instanceof PackageItemInfo) {
384 PackageItemInfo pti = (PackageItemInfo) info;
385 getTitleAndIconForApp(pti.packageName, pti.user, false, pti);
Sunny Goyal34b65272015-03-11 16:56:52 -0700386 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700387 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700388
389 @Override
390 public void run() {
391 caller.reapplyItemInfo(info);
392 }
393 });
394 }
395 };
396 mWorkerHandler.post(request);
397 return new IconLoadRequest(request, mWorkerHandler);
398 }
399
Sunny Goyal756adbc2015-04-16 15:20:51 -0700400 private Bitmap getNonNullIcon(CacheEntry entry, UserHandleCompat user) {
401 return entry.icon == null ? getDefaultIcon(user) : entry.icon;
402 }
403
Sunny Goyal34b65272015-03-11 16:56:52 -0700404 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800405 * Fill in "application" with the icon and label for "info."
406 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700407 public synchronized void getTitleAndIcon(AppInfo application,
408 LauncherActivityInfoCompat info, boolean useLowResIcon) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700409 UserHandleCompat user = info == null ? application.user : info.getUser();
410 CacheEntry entry = cacheLocked(application.componentName, info, user,
Sunny Goyal34b65272015-03-11 16:56:52 -0700411 false, useLowResIcon);
Winson Chung82b016c2015-05-08 17:00:10 -0700412 application.title = Utilities.trim(entry.title);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700413 application.iconBitmap = getNonNullIcon(entry, user);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700414 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700415 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800416 }
417
Sunny Goyal34b65272015-03-11 16:56:52 -0700418 /**
Sunny Goyal77919b92015-05-06 16:53:21 -0700419 * Updates {@param application} only if a valid entry is found.
420 */
421 public synchronized void updateTitleAndIcon(AppInfo application) {
422 CacheEntry entry = cacheLocked(application.componentName, null, application.user,
423 false, application.usingLowResIcon);
424 if (entry.icon != null && !isDefaultIcon(entry.icon, application.user)) {
Winson Chung82b016c2015-05-08 17:00:10 -0700425 application.title = Utilities.trim(entry.title);
Sunny Goyal77919b92015-05-06 16:53:21 -0700426 application.iconBitmap = entry.icon;
427 application.contentDescription = entry.contentDescription;
428 application.usingLowResIcon = entry.isLowResIcon;
429 }
430 }
431
432 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700433 * Returns a high res icon for the given intent and user
434 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700435 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
436 ComponentName component = intent.getComponent();
437 // null info means not installed, but if we have a component from the intent then
438 // we should still look in the cache for restored app icons.
439 if (component == null) {
440 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800441 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700442
443 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700444 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700445 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800446 }
447
Sunny Goyal34942622014-08-29 17:20:55 -0700448 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800449 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
450 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700451 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700452 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700453 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700454 ComponentName component = intent.getComponent();
455 // null info means not installed, but if we have a component from the intent then
456 // we should still look in the cache for restored app icons.
457 if (component == null) {
458 shortcutInfo.setIcon(getDefaultIcon(user));
459 shortcutInfo.title = "";
460 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700461 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700462 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800463 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700464 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700465 }
466 }
467
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800468 /**
469 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
470 */
471 public synchronized void getTitleAndIcon(
472 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700473 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
474 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700475 shortcutInfo.setIcon(getNonNullIcon(entry, user));
Winson Chung82b016c2015-05-08 17:00:10 -0700476 shortcutInfo.title = Utilities.trim(entry.title);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800477 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700478 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800479 }
Sunny Goyal34942622014-08-29 17:20:55 -0700480
Sunny Goyald180cf72015-04-06 12:45:40 -0700481 /**
482 * Fill in {@param appInfo} with the icon and label for {@param packageName}
483 */
484 public synchronized void getTitleAndIconForApp(
Hyunyoung Song3f471442015-04-08 19:01:34 -0700485 String packageName, UserHandleCompat user, boolean useLowResIcon,
486 PackageItemInfo infoOut) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700487 CacheEntry entry = getEntryForPackageLocked(packageName, user, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700488 infoOut.iconBitmap = getNonNullIcon(entry, user);
Winson Chung82b016c2015-05-08 17:00:10 -0700489 infoOut.title = Utilities.trim(entry.title);
Hyunyoung Song3f471442015-04-08 19:01:34 -0700490 infoOut.usingLowResIcon = entry.isLowResIcon;
491 infoOut.contentDescription = entry.contentDescription;
Sunny Goyald180cf72015-04-06 12:45:40 -0700492 }
493
Sunny Goyal736f5af2014-10-16 14:07:29 -0700494 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100495 if (!mDefaultIcons.containsKey(user)) {
496 mDefaultIcons.put(user, makeDefaultIcon(user));
497 }
498 return mDefaultIcons.get(user);
499 }
500
Kenny Guyed131872014-04-30 03:02:21 +0100501 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
502 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700503 }
504
Sunny Goyal736f5af2014-10-16 14:07:29 -0700505 /**
506 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
507 * This method is not thread safe, it must be called from a synchronized method.
508 */
Kenny Guyed131872014-04-30 03:02:21 +0100509 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700510 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700511 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100512 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700513 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800514 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100515 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500516
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800517 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700518 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800519 if (info != null) {
520 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500521 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700522 if (usePackageIcon) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700523 CacheEntry packageEntry = getEntryForPackageLocked(
524 componentName.getPackageName(), user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700525 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700526 if (DEBUG) Log.d(TAG, "using package default icon for " +
527 componentName.toShortString());
528 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700529 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800530 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700531 }
532 }
533 if (entry.icon == null) {
534 if (DEBUG) Log.d(TAG, "using default icon for " +
535 componentName.toShortString());
536 entry.icon = getDefaultIcon(user);
537 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700538 }
539 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800540
541 if (TextUtils.isEmpty(entry.title) && info != null) {
Winson Chung82b016c2015-05-08 17:00:10 -0700542 entry.title = info.getLabel();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800543 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
544 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800545 }
546 return entry;
547 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400548
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700549 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700550 * Adds a default package entry in the cache. This entry is not persisted and will be removed
551 * when the cache is flushed.
552 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700553 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700554 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800555 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700556
Sunny Goyald180cf72015-04-06 12:45:40 -0700557 CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700558 if (!TextUtils.isEmpty(title)) {
559 entry.title = title;
560 }
561 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700562 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700563 }
564 }
565
566 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700567 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700568 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyald180cf72015-04-06 12:45:40 -0700569 *
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700570 */
Sunny Goyald180cf72015-04-06 12:45:40 -0700571 private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
572 boolean useLowResIcon) {
Sunny Goyal091f0ff2015-06-04 15:19:31 -0700573 ComponentName cn = new ComponentName(packageName, packageName + EMPTY_CLASS_NAME);
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700574 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700575 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal091f0ff2015-06-04 15:19:31 -0700576
Sunny Goyald180cf72015-04-06 12:45:40 -0700577 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700578 entry = new CacheEntry();
Winson Chungcdefc632015-05-28 12:53:44 -0700579 boolean entryUpdated = true;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700580
Sunny Goyald180cf72015-04-06 12:45:40 -0700581 // Check the DB first.
582 if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
583 try {
584 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
585 ApplicationInfo appInfo = info.applicationInfo;
586 if (appInfo == null) {
587 throw new NameNotFoundException("ApplicationInfo is null");
588 }
589 Drawable drawable = mUserManager.getBadgedDrawableForUser(
590 appInfo.loadIcon(mPackageManager), user);
591 entry.icon = Utilities.createIconBitmap(drawable, mContext);
592 entry.title = appInfo.loadLabel(mPackageManager);
593 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
594 entry.isLowResIcon = false;
595
596 // Add the icon in the DB here, since these do not get written during
597 // package updates.
598 ContentValues values =
599 mIconDb.newContentValues(entry.icon, entry.title.toString());
600 addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
601
602 } catch (NameNotFoundException e) {
603 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
Winson Chungcdefc632015-05-28 12:53:44 -0700604 entryUpdated = false;
Sunny Goyald180cf72015-04-06 12:45:40 -0700605 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700606 }
Winson Chungcdefc632015-05-28 12:53:44 -0700607
608 // Only add a filled-out entry to the cache
609 if (entryUpdated) {
610 mCache.put(cacheKey, entry);
611 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700612 }
613 return entry;
614 }
615
Chris Wren6d0dde02014-02-10 12:16:54 -0500616 /**
617 * Pre-load an icon into the persistent cache.
618 *
619 * <P>Queries for a component that does not exist in the package manager
620 * will be answered by the persistent cache.
621 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500622 * @param componentName the icon should be returned for this component
623 * @param icon the icon to be persisted
624 * @param dpi the native density of the icon
625 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800626 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
627 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500628 // TODO rescale to the correct native DPI
629 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800630 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500631 packageManager.getActivityIcon(componentName);
632 // component is present on the system already, do nothing
633 return;
634 } catch (PackageManager.NameNotFoundException e) {
635 // pass
636 }
637
Sunny Goyal34b65272015-03-11 16:56:52 -0700638 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800639 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
640 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800641 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
642 SQLiteDatabase.CONFLICT_REPLACE);
643 }
644
Sunny Goyal34b65272015-03-11 16:56:52 -0700645 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
646 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800647 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700648 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
649 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800650 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
651 new String[] {component.flattenToString(),
652 Long.toString(mUserManager.getSerialNumberForUser(user))},
653 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500654 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800655 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700656 entry.icon = loadIconNoResize(c, 0);
657 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800658 entry.title = c.getString(1);
659 if (entry.title == null) {
660 entry.title = "";
661 entry.contentDescription = "";
662 } else {
663 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500664 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800665 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500666 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500667 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800668 c.close();
669 }
670 return false;
671 }
672
Sunny Goyal34b65272015-03-11 16:56:52 -0700673 public static class IconLoadRequest {
674 private final Runnable mRunnable;
675 private final Handler mHandler;
676
677 IconLoadRequest(Runnable runnable, Handler handler) {
678 mRunnable = runnable;
679 mHandler = handler;
680 }
681
682 public void cancel() {
683 mHandler.removeCallbacks(mRunnable);
684 }
685 }
686
Sunny Goyal9ff98082015-05-15 16:59:36 -0700687 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -0700688 * A runnable that updates invalid icons and adds missing icons in the DB for the provided
689 * LauncherActivityInfoCompat list. Items are updated/added one at a time, so that the
690 * worker thread doesn't get blocked.
Sunny Goyal9ff98082015-05-15 16:59:36 -0700691 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700692 @Thunk class SerializedIconUpdateTask implements Runnable {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700693 private final long mUserSerial;
694 private final HashMap<String, PackageInfo> mPkgInfoMap;
695 private final Stack<LauncherActivityInfoCompat> mAppsToAdd;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700696 private final Stack<LauncherActivityInfoCompat> mAppsToUpdate;
697 private final HashSet<String> mUpdatedPackages = new HashSet<String>();
Sunny Goyal9ff98082015-05-15 16:59:36 -0700698
Sunny Goyal316490e2015-06-02 09:38:28 -0700699 @Thunk SerializedIconUpdateTask(long userSerial, HashMap<String, PackageInfo> pkgInfoMap,
Sunny Goyal75b0f552015-05-20 21:57:06 -0700700 Stack<LauncherActivityInfoCompat> appsToAdd,
701 Stack<LauncherActivityInfoCompat> appsToUpdate) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700702 mUserSerial = userSerial;
703 mPkgInfoMap = pkgInfoMap;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700704 mAppsToAdd = appsToAdd;
705 mAppsToUpdate = appsToUpdate;
Sunny Goyal9ff98082015-05-15 16:59:36 -0700706 }
707
708 @Override
709 public void run() {
Sunny Goyal75b0f552015-05-20 21:57:06 -0700710 if (!mAppsToUpdate.isEmpty()) {
711 LauncherActivityInfoCompat app = mAppsToUpdate.pop();
712 String cn = app.getComponentName().flattenToString();
713 ContentValues values = updateCacheAndGetContentValues(app, true);
714 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
715 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
716 new String[] {cn, Long.toString(mUserSerial)});
717 mUpdatedPackages.add(app.getComponentName().getPackageName());
718
719 if (mAppsToUpdate.isEmpty() && !mUpdatedPackages.isEmpty()) {
720 // No more app to update. Notify model.
721 LauncherAppState.getInstance().getModel().onPackageIconsUpdated(
722 mUpdatedPackages, mUserManager.getUserForSerialNumber(mUserSerial));
723 }
724
725 // Let it run one more time.
726 scheduleNext();
727 } else if (!mAppsToAdd.isEmpty()) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700728 LauncherActivityInfoCompat app = mAppsToAdd.pop();
729 PackageInfo info = mPkgInfoMap.get(app.getComponentName().getPackageName());
730 if (info != null) {
731 synchronized (IconCache.this) {
732 addIconToDBAndMemCache(app, info, mUserSerial);
733 }
734 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700735
736 if (!mAppsToAdd.isEmpty()) {
737 scheduleNext();
738 }
Sunny Goyal9ff98082015-05-15 16:59:36 -0700739 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700740 }
741
742 public void scheduleNext() {
743 mWorkerHandler.postAtTime(this, ICON_UPDATE_TOKEN, SystemClock.uptimeMillis() + 1);
Sunny Goyal9ff98082015-05-15 16:59:36 -0700744 }
745 }
746
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800747 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700748 private final static int DB_VERSION = 5;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800749
750 private final static String TABLE_NAME = "icons";
751 private final static String COLUMN_ROWID = "rowid";
752 private final static String COLUMN_COMPONENT = "componentName";
753 private final static String COLUMN_USER = "profileId";
754 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
755 private final static String COLUMN_VERSION = "version";
756 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700757 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800758 private final static String COLUMN_LABEL = "label";
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700759 private final static String COLUMN_SYSTEM_STATE = "system_state";
760
761 public String mSystemState;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800762
763 public IconDB(Context context) {
764 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
Sunny Goyal2003c752015-06-18 13:56:59 -0700765 updateSystemStateString();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800766 }
767
Sunny Goyal2003c752015-06-18 13:56:59 -0700768 public void updateSystemStateString() {
769 mSystemState = Locale.getDefault().toString();
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700770 }
771
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800772 @Override
773 public void onCreate(SQLiteDatabase db) {
774 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
775 COLUMN_COMPONENT + " TEXT NOT NULL, " +
776 COLUMN_USER + " INTEGER NOT NULL, " +
777 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
778 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
779 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700780 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800781 COLUMN_LABEL + " TEXT, " +
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700782 COLUMN_SYSTEM_STATE + " TEXT, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800783 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
784 ");");
785 }
786
787 @Override
788 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
789 if (oldVersion != newVersion) {
790 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500791 }
792 }
793
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800794 @Override
795 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
796 if (oldVersion != newVersion) {
797 clearDB(db);
798 }
Kenny Guyed131872014-04-30 03:02:21 +0100799 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500800
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800801 private void clearDB(SQLiteDatabase db) {
802 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
803 onCreate(db);
804 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700805
806 public ContentValues newContentValues(Bitmap icon, String label) {
807 ContentValues values = new ContentValues();
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700808 values.put(COLUMN_ICON, Utilities.flattenBitmap(icon));
809 values.put(COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700810 Bitmap.createScaledBitmap(icon,
811 icon.getWidth() / LOW_RES_SCALE_FACTOR,
812 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700813 values.put(COLUMN_LABEL, label);
814 values.put(COLUMN_SYSTEM_STATE, mSystemState);
Sunny Goyal34b65272015-03-11 16:56:52 -0700815 return values;
816 }
817 }
818
819 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
820 byte[] data = c.getBlob(iconIndex);
821 try {
822 return BitmapFactory.decodeByteArray(data, 0, data.length);
823 } catch (Exception e) {
824 return null;
825 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500826 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800827}