blob: a3376c42fb151cdcc25e9525f06307712e6b5a43 [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;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070046import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070047import com.android.launcher3.util.ComponentKey;
Adam Cohen091440a2015-03-18 14:16:05 -070048import com.android.launcher3.util.Thunk;
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;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080052import java.util.List;
Sunny Goyal0c9a3542015-04-01 16:04:21 -070053import java.util.Locale;
Sunny Goyal9ff98082015-05-15 16:59:36 -070054import java.util.Stack;
Joe Onorato0589f0f2010-02-08 13:44:00 -080055
56/**
57 * Cache of application icons. Icons can be made from any thread.
58 */
59public class IconCache {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070060
Joe Onorato0589f0f2010-02-08 13:44:00 -080061 private static final String TAG = "Launcher.IconCache";
62
63 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050064
Sunny Goyal0fc1be12014-08-11 17:05:23 -070065 // Empty class name is used for storing package default entry.
66 private static final String EMPTY_CLASS_NAME = ".";
67
Sunny Goyalbbef77d2014-09-09 16:27:55 -070068 private static final boolean DEBUG = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -080069
Sunny Goyal34b65272015-03-11 16:56:52 -070070 private static final int LOW_RES_SCALE_FACTOR = 8;
71
Sunny Goyal75b0f552015-05-20 21:57:06 -070072 private static final Object ICON_UPDATE_TOKEN = new Object();
73
Adam Cohen091440a2015-03-18 14:16:05 -070074 @Thunk static class CacheEntry {
Joe Onorato0589f0f2010-02-08 13:44:00 -080075 public Bitmap icon;
Winson Chungcdefc632015-05-28 12:53:44 -070076 public CharSequence title = "";
77 public CharSequence contentDescription = "";
Sunny Goyal34b65272015-03-11 16:56:52 -070078 public boolean isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -080079 }
80
Sunny Goyal8758ea02015-03-18 10:07:49 -070081 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
82 private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
83
Daniel Sandlercc8befa2013-06-11 14:45:48 -040084 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070085 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010086 private final UserManagerCompat mUserManager;
87 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070088 private final HashMap<ComponentKey, CacheEntry> mCache =
89 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080090 private final int mIconDpi;
91 private final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080092
Sunny Goyal34b65272015-03-11 16:56:52 -070093 private final Handler mWorkerHandler;
94
Adam Cohen2e6da152015-05-06 11:42:25 -070095 public IconCache(Context context, InvariantDeviceProfile inv) {
Winson Chungd83f5f42012-02-13 14:27:42 -080096 ActivityManager activityManager =
97 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
98
Joe Onorato0589f0f2010-02-08 13:44:00 -080099 mContext = context;
100 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +0100101 mUserManager = UserManagerCompat.getInstance(mContext);
102 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Winson Chungd83f5f42012-02-13 14:27:42 -0800103 mIconDpi = activityManager.getLauncherLargeIconDensity();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800104 mIconDb = new IconDB(context);
Sunny Goyal34b65272015-03-11 16:56:52 -0700105
Sunny Goyal756adbc2015-04-16 15:20:51 -0700106 mWorkerHandler = new Handler(LauncherModel.getWorkerLooper());
Romain Guya28fd3f2010-03-15 14:44:42 -0700107 }
108
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800109 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700110 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700111 }
112
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700113 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700114 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700115 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700116 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700117 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700118 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700119 }
Michael Jurka721d9722011-08-03 11:49:59 -0700120
121 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700122 }
123
Winson Chung0b9fcf52011-10-31 13:05:15 -0700124 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700125 Resources resources;
126 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700127 resources = mPackageManager.getResourcesForApplication(packageName);
128 } catch (PackageManager.NameNotFoundException e) {
129 resources = null;
130 }
131 if (resources != null) {
132 if (iconId != 0) {
133 return getFullResIcon(resources, iconId);
134 }
135 }
136 return getFullResDefaultActivityIcon();
137 }
138
Sunny Goyalffe83f12014-08-14 17:39:34 -0700139 public int getFullResIconDpi() {
140 return mIconDpi;
141 }
142
Michael Jurkadac85912012-05-18 15:04:49 -0700143 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700144 Resources resources;
145 try {
146 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700147 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700148 } catch (PackageManager.NameNotFoundException e) {
149 resources = null;
150 }
151 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700152 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700153 if (iconId != 0) {
154 return getFullResIcon(resources, iconId);
155 }
156 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500157
Michael Jurkac9a96192010-11-01 11:52:08 -0700158 return getFullResDefaultActivityIcon();
159 }
160
Kenny Guyed131872014-04-30 03:02:21 +0100161 private Bitmap makeDefaultIcon(UserHandleCompat user) {
162 Drawable unbadged = getFullResDefaultActivityIcon();
163 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700164 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
165 Math.max(d.getIntrinsicHeight(), 1),
166 Bitmap.Config.ARGB_8888);
167 Canvas c = new Canvas(b);
168 d.setBounds(0, 0, b.getWidth(), b.getHeight());
169 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700170 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700171 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800172 }
173
174 /**
175 * Remove any records for the supplied ComponentName.
176 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700177 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700178 mCache.remove(new ComponentKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800179 }
180
181 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800182 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500183 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800184 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700185 HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
186 for (ComponentKey key: mCache.keySet()) {
Kenny Guyed131872014-04-30 03:02:21 +0100187 if (key.componentName.getPackageName().equals(packageName)
188 && key.user.equals(user)) {
189 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500190 }
191 }
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700192 for (ComponentKey condemned: forDeletion) {
Kenny Guyed131872014-04-30 03:02:21 +0100193 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500194 }
195 }
196
197 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800198 * Updates the entries related to the given package in memory and persistent DB.
199 */
200 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
201 removeIconsForPkg(packageName, user);
202 try {
203 PackageInfo info = mPackageManager.getPackageInfo(packageName,
204 PackageManager.GET_UNINSTALLED_PACKAGES);
205 long userSerial = mUserManager.getSerialNumberForUser(user);
206 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700207 addIconToDBAndMemCache(app, info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800208 }
209 } catch (NameNotFoundException e) {
210 Log.d(TAG, "Package not found", e);
211 return;
212 }
213 }
214
215 /**
216 * Removes the entries related to the given package in memory and persistent DB.
217 */
218 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
219 removeFromMemCacheLocked(packageName, user);
220 long userSerial = mUserManager.getSerialNumberForUser(user);
221 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
222 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
223 new String[] {packageName + "/%", Long.toString(userSerial)});
224 }
225
Sunny Goyal75b0f552015-05-20 21:57:06 -0700226 public void updateDbIcons() {
227 // Remove all active icon update tasks.
228 mWorkerHandler.removeCallbacksAndMessages(ICON_UPDATE_TOKEN);
229
230 mIconDb.updateSystemStateString(mContext);
231 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
232 // Query for the set of apps
233 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
234 // Fail if we don't have any apps
235 // TODO: Fix this. Only fail for the current user.
236 if (apps == null || apps.isEmpty()) {
237 return;
238 }
239
240 // Update icon cache. This happens in segments and {@link #onPackageIconsUpdated}
241 // is called by the icon cache when the job is complete.
242 updateDBIcons(user, apps);
243 }
244 }
245
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800246 /**
247 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
248 * the DB and are updated.
249 * @return The set of packages for which icons have updated.
250 */
Sunny Goyal75b0f552015-05-20 21:57:06 -0700251 private void updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800252 long userSerial = mUserManager.getSerialNumberForUser(user);
253 PackageManager pm = mContext.getPackageManager();
254 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
255 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
256 pkgInfoMap.put(info.packageName, info);
257 }
258
259 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
260 for (LauncherActivityInfoCompat app : apps) {
261 componentMap.put(app.getComponentName(), app);
262 }
263
264 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
265 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700266 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION,
267 IconDB.COLUMN_SYSTEM_STATE},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800268 IconDB.COLUMN_USER + " = ? ",
269 new String[] {Long.toString(userSerial)},
270 null, null, null);
271
272 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
273 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
274 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
275 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700276 final int systemStateIndex = c.getColumnIndex(IconDB.COLUMN_SYSTEM_STATE);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800277
278 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
Sunny Goyal75b0f552015-05-20 21:57:06 -0700279 Stack<LauncherActivityInfoCompat> appsToUpdate = new Stack<>();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800280
281 while (c.moveToNext()) {
282 String cn = c.getString(indexComponent);
283 ComponentName component = ComponentName.unflattenFromString(cn);
284 PackageInfo info = pkgInfoMap.get(component.getPackageName());
285 if (info == null) {
286 itemsToRemove.add(c.getInt(rowIndex));
287 continue;
288 }
289 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
290 // Application is not present
291 continue;
292 }
293
294 long updateTime = c.getLong(indexLastUpdate);
295 int version = c.getInt(indexVersion);
296 LauncherActivityInfoCompat app = componentMap.remove(component);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700297 if (version == info.versionCode && updateTime == info.lastUpdateTime &&
298 TextUtils.equals(mIconDb.mSystemState, c.getString(systemStateIndex))) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800299 continue;
300 }
301 if (app == null) {
302 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,
310 IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )",
311 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 Goyald180cf72015-04-06 12:45:40 -0700323 private void addIconToDBAndMemCache(LauncherActivityInfoCompat app, PackageInfo info,
324 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 Goyal9ff98082015-05-15 16:59:36 -0700345 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app,
346 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 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800368 * Empty out the cache.
369 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700370 public synchronized void flush() {
371 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800372 }
373
374 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700375 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
376 * @return a request ID that can be used to cancel the request.
377 */
378 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
379 Runnable request = new Runnable() {
380
381 @Override
382 public void run() {
383 if (info instanceof AppInfo) {
384 getTitleAndIcon((AppInfo) info, null, false);
385 } else if (info instanceof ShortcutInfo) {
386 ShortcutInfo st = (ShortcutInfo) info;
387 getTitleAndIcon(st,
388 st.promisedIntent != null ? st.promisedIntent : st.intent,
389 st.user, false);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700390 } else if (info instanceof PackageItemInfo) {
391 PackageItemInfo pti = (PackageItemInfo) info;
392 getTitleAndIconForApp(pti.packageName, pti.user, false, pti);
Sunny Goyal34b65272015-03-11 16:56:52 -0700393 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700394 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700395
396 @Override
397 public void run() {
398 caller.reapplyItemInfo(info);
399 }
400 });
401 }
402 };
403 mWorkerHandler.post(request);
404 return new IconLoadRequest(request, mWorkerHandler);
405 }
406
Sunny Goyal756adbc2015-04-16 15:20:51 -0700407 private Bitmap getNonNullIcon(CacheEntry entry, UserHandleCompat user) {
408 return entry.icon == null ? getDefaultIcon(user) : entry.icon;
409 }
410
Sunny Goyal34b65272015-03-11 16:56:52 -0700411 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800412 * Fill in "application" with the icon and label for "info."
413 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700414 public synchronized void getTitleAndIcon(AppInfo application,
415 LauncherActivityInfoCompat info, boolean useLowResIcon) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700416 UserHandleCompat user = info == null ? application.user : info.getUser();
417 CacheEntry entry = cacheLocked(application.componentName, info, user,
Sunny Goyal34b65272015-03-11 16:56:52 -0700418 false, useLowResIcon);
Winson Chung82b016c2015-05-08 17:00:10 -0700419 application.title = Utilities.trim(entry.title);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700420 application.iconBitmap = getNonNullIcon(entry, user);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700421 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700422 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800423 }
424
Sunny Goyal34b65272015-03-11 16:56:52 -0700425 /**
Sunny Goyal77919b92015-05-06 16:53:21 -0700426 * Updates {@param application} only if a valid entry is found.
427 */
428 public synchronized void updateTitleAndIcon(AppInfo application) {
429 CacheEntry entry = cacheLocked(application.componentName, null, application.user,
430 false, application.usingLowResIcon);
431 if (entry.icon != null && !isDefaultIcon(entry.icon, application.user)) {
Winson Chung82b016c2015-05-08 17:00:10 -0700432 application.title = Utilities.trim(entry.title);
Sunny Goyal77919b92015-05-06 16:53:21 -0700433 application.iconBitmap = entry.icon;
434 application.contentDescription = entry.contentDescription;
435 application.usingLowResIcon = entry.isLowResIcon;
436 }
437 }
438
439 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700440 * Returns a high res icon for the given intent and user
441 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700442 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
443 ComponentName component = intent.getComponent();
444 // null info means not installed, but if we have a component from the intent then
445 // we should still look in the cache for restored app icons.
446 if (component == null) {
447 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800448 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700449
450 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700451 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700452 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800453 }
454
Sunny Goyal34942622014-08-29 17:20:55 -0700455 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800456 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
457 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700458 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700459 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700460 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700461 ComponentName component = intent.getComponent();
462 // null info means not installed, but if we have a component from the intent then
463 // we should still look in the cache for restored app icons.
464 if (component == null) {
465 shortcutInfo.setIcon(getDefaultIcon(user));
466 shortcutInfo.title = "";
467 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700468 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700469 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800470 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700471 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700472 }
473 }
474
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800475 /**
476 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
477 */
478 public synchronized void getTitleAndIcon(
479 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700480 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
481 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700482 shortcutInfo.setIcon(getNonNullIcon(entry, user));
Winson Chung82b016c2015-05-08 17:00:10 -0700483 shortcutInfo.title = Utilities.trim(entry.title);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800484 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700485 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800486 }
Sunny Goyal34942622014-08-29 17:20:55 -0700487
Sunny Goyald180cf72015-04-06 12:45:40 -0700488 /**
489 * Fill in {@param appInfo} with the icon and label for {@param packageName}
490 */
491 public synchronized void getTitleAndIconForApp(
Hyunyoung Song3f471442015-04-08 19:01:34 -0700492 String packageName, UserHandleCompat user, boolean useLowResIcon,
493 PackageItemInfo infoOut) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700494 CacheEntry entry = getEntryForPackageLocked(packageName, user, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700495 infoOut.iconBitmap = getNonNullIcon(entry, user);
Winson Chung82b016c2015-05-08 17:00:10 -0700496 infoOut.title = Utilities.trim(entry.title);
Hyunyoung Song3f471442015-04-08 19:01:34 -0700497 infoOut.usingLowResIcon = entry.isLowResIcon;
498 infoOut.contentDescription = entry.contentDescription;
Sunny Goyald180cf72015-04-06 12:45:40 -0700499 }
500
Sunny Goyal736f5af2014-10-16 14:07:29 -0700501 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100502 if (!mDefaultIcons.containsKey(user)) {
503 mDefaultIcons.put(user, makeDefaultIcon(user));
504 }
505 return mDefaultIcons.get(user);
506 }
507
Kenny Guyed131872014-04-30 03:02:21 +0100508 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
509 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700510 }
511
Sunny Goyal736f5af2014-10-16 14:07:29 -0700512 /**
513 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
514 * This method is not thread safe, it must be called from a synchronized method.
515 */
Kenny Guyed131872014-04-30 03:02:21 +0100516 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700517 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700518 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100519 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700520 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800521 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100522 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500523
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800524 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700525 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800526 if (info != null) {
527 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500528 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700529 if (usePackageIcon) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700530 CacheEntry packageEntry = getEntryForPackageLocked(
531 componentName.getPackageName(), user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700532 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700533 if (DEBUG) Log.d(TAG, "using package default icon for " +
534 componentName.toShortString());
535 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700536 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800537 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700538 }
539 }
540 if (entry.icon == null) {
541 if (DEBUG) Log.d(TAG, "using default icon for " +
542 componentName.toShortString());
543 entry.icon = getDefaultIcon(user);
544 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700545 }
546 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800547
548 if (TextUtils.isEmpty(entry.title) && info != null) {
Winson Chung82b016c2015-05-08 17:00:10 -0700549 entry.title = info.getLabel();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800550 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
551 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800552 }
553 return entry;
554 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400555
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700556 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700557 * Adds a default package entry in the cache. This entry is not persisted and will be removed
558 * when the cache is flushed.
559 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700560 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700561 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800562 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700563
Sunny Goyald180cf72015-04-06 12:45:40 -0700564 CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700565 if (!TextUtils.isEmpty(title)) {
566 entry.title = title;
567 }
568 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700569 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700570 }
571 }
572
573 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700574 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700575 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyald180cf72015-04-06 12:45:40 -0700576 *
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700577 */
Sunny Goyald180cf72015-04-06 12:45:40 -0700578 private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
579 boolean useLowResIcon) {
580 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700581 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700582 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyald180cf72015-04-06 12:45:40 -0700583 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700584 entry = new CacheEntry();
Winson Chungcdefc632015-05-28 12:53:44 -0700585 boolean entryUpdated = true;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700586
Sunny Goyald180cf72015-04-06 12:45:40 -0700587 // Check the DB first.
588 if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
589 try {
590 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
591 ApplicationInfo appInfo = info.applicationInfo;
592 if (appInfo == null) {
593 throw new NameNotFoundException("ApplicationInfo is null");
594 }
595 Drawable drawable = mUserManager.getBadgedDrawableForUser(
596 appInfo.loadIcon(mPackageManager), user);
597 entry.icon = Utilities.createIconBitmap(drawable, mContext);
598 entry.title = appInfo.loadLabel(mPackageManager);
599 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
600 entry.isLowResIcon = false;
601
602 // Add the icon in the DB here, since these do not get written during
603 // package updates.
604 ContentValues values =
605 mIconDb.newContentValues(entry.icon, entry.title.toString());
606 addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
607
608 } catch (NameNotFoundException e) {
609 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
Winson Chungcdefc632015-05-28 12:53:44 -0700610 entryUpdated = false;
Sunny Goyald180cf72015-04-06 12:45:40 -0700611 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700612 }
Winson Chungcdefc632015-05-28 12:53:44 -0700613
614 // Only add a filled-out entry to the cache
615 if (entryUpdated) {
616 mCache.put(cacheKey, entry);
617 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700618 }
619 return entry;
620 }
621
Chris Wren6d0dde02014-02-10 12:16:54 -0500622 /**
623 * Pre-load an icon into the persistent cache.
624 *
625 * <P>Queries for a component that does not exist in the package manager
626 * will be answered by the persistent cache.
627 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500628 * @param componentName the icon should be returned for this component
629 * @param icon the icon to be persisted
630 * @param dpi the native density of the icon
631 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800632 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
633 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500634 // TODO rescale to the correct native DPI
635 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800636 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500637 packageManager.getActivityIcon(componentName);
638 // component is present on the system already, do nothing
639 return;
640 } catch (PackageManager.NameNotFoundException e) {
641 // pass
642 }
643
Sunny Goyal34b65272015-03-11 16:56:52 -0700644 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800645 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
646 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800647 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
648 SQLiteDatabase.CONFLICT_REPLACE);
649 }
650
Sunny Goyal34b65272015-03-11 16:56:52 -0700651 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
652 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800653 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700654 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
655 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800656 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
657 new String[] {component.flattenToString(),
658 Long.toString(mUserManager.getSerialNumberForUser(user))},
659 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500660 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800661 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700662 entry.icon = loadIconNoResize(c, 0);
663 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800664 entry.title = c.getString(1);
665 if (entry.title == null) {
666 entry.title = "";
667 entry.contentDescription = "";
668 } else {
669 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500670 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800671 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500672 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500673 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800674 c.close();
675 }
676 return false;
677 }
678
Sunny Goyal34b65272015-03-11 16:56:52 -0700679 public static class IconLoadRequest {
680 private final Runnable mRunnable;
681 private final Handler mHandler;
682
683 IconLoadRequest(Runnable runnable, Handler handler) {
684 mRunnable = runnable;
685 mHandler = handler;
686 }
687
688 public void cancel() {
689 mHandler.removeCallbacks(mRunnable);
690 }
691 }
692
Sunny Goyal9ff98082015-05-15 16:59:36 -0700693 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -0700694 * A runnable that updates invalid icons and adds missing icons in the DB for the provided
695 * LauncherActivityInfoCompat list. Items are updated/added one at a time, so that the
696 * worker thread doesn't get blocked.
Sunny Goyal9ff98082015-05-15 16:59:36 -0700697 */
Sunny Goyal75b0f552015-05-20 21:57:06 -0700698 private class SerializedIconUpdateTask implements Runnable {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700699 private final long mUserSerial;
700 private final HashMap<String, PackageInfo> mPkgInfoMap;
701 private final Stack<LauncherActivityInfoCompat> mAppsToAdd;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700702 private final Stack<LauncherActivityInfoCompat> mAppsToUpdate;
703 private final HashSet<String> mUpdatedPackages = new HashSet<String>();
Sunny Goyal9ff98082015-05-15 16:59:36 -0700704
Sunny Goyal75b0f552015-05-20 21:57:06 -0700705 private SerializedIconUpdateTask(long userSerial, HashMap<String, PackageInfo> pkgInfoMap,
706 Stack<LauncherActivityInfoCompat> appsToAdd,
707 Stack<LauncherActivityInfoCompat> appsToUpdate) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700708 mUserSerial = userSerial;
709 mPkgInfoMap = pkgInfoMap;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700710 mAppsToAdd = appsToAdd;
711 mAppsToUpdate = appsToUpdate;
Sunny Goyal9ff98082015-05-15 16:59:36 -0700712 }
713
714 @Override
715 public void run() {
Sunny Goyal75b0f552015-05-20 21:57:06 -0700716 if (!mAppsToUpdate.isEmpty()) {
717 LauncherActivityInfoCompat app = mAppsToUpdate.pop();
718 String cn = app.getComponentName().flattenToString();
719 ContentValues values = updateCacheAndGetContentValues(app, true);
720 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
721 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
722 new String[] {cn, Long.toString(mUserSerial)});
723 mUpdatedPackages.add(app.getComponentName().getPackageName());
724
725 if (mAppsToUpdate.isEmpty() && !mUpdatedPackages.isEmpty()) {
726 // No more app to update. Notify model.
727 LauncherAppState.getInstance().getModel().onPackageIconsUpdated(
728 mUpdatedPackages, mUserManager.getUserForSerialNumber(mUserSerial));
729 }
730
731 // Let it run one more time.
732 scheduleNext();
733 } else if (!mAppsToAdd.isEmpty()) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700734 LauncherActivityInfoCompat app = mAppsToAdd.pop();
735 PackageInfo info = mPkgInfoMap.get(app.getComponentName().getPackageName());
736 if (info != null) {
737 synchronized (IconCache.this) {
738 addIconToDBAndMemCache(app, info, mUserSerial);
739 }
740 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700741
742 if (!mAppsToAdd.isEmpty()) {
743 scheduleNext();
744 }
Sunny Goyal9ff98082015-05-15 16:59:36 -0700745 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700746 }
747
748 public void scheduleNext() {
749 mWorkerHandler.postAtTime(this, ICON_UPDATE_TOKEN, SystemClock.uptimeMillis() + 1);
Sunny Goyal9ff98082015-05-15 16:59:36 -0700750 }
751 }
752
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800753 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal77919b92015-05-06 16:53:21 -0700754 private final static int DB_VERSION = 4;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800755
756 private final static String TABLE_NAME = "icons";
757 private final static String COLUMN_ROWID = "rowid";
758 private final static String COLUMN_COMPONENT = "componentName";
759 private final static String COLUMN_USER = "profileId";
760 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
761 private final static String COLUMN_VERSION = "version";
762 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700763 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800764 private final static String COLUMN_LABEL = "label";
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700765 private final static String COLUMN_SYSTEM_STATE = "system_state";
766
767 public String mSystemState;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800768
769 public IconDB(Context context) {
770 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700771 updateSystemStateString(context);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800772 }
773
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700774 public void updateSystemStateString(Context c) {
775 mSystemState = Locale.getDefault().toString() + ","
776 + c.getResources().getConfiguration().mcc;
777 }
778
779
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800780 @Override
781 public void onCreate(SQLiteDatabase db) {
782 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
783 COLUMN_COMPONENT + " TEXT NOT NULL, " +
784 COLUMN_USER + " INTEGER NOT NULL, " +
785 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
786 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
787 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700788 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800789 COLUMN_LABEL + " TEXT, " +
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700790 COLUMN_SYSTEM_STATE + " TEXT, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800791 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
792 ");");
793 }
794
795 @Override
796 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
797 if (oldVersion != newVersion) {
798 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500799 }
800 }
801
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800802 @Override
803 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
804 if (oldVersion != newVersion) {
805 clearDB(db);
806 }
Kenny Guyed131872014-04-30 03:02:21 +0100807 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500808
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800809 private void clearDB(SQLiteDatabase db) {
810 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
811 onCreate(db);
812 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700813
814 public ContentValues newContentValues(Bitmap icon, String label) {
815 ContentValues values = new ContentValues();
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700816 values.put(COLUMN_ICON, Utilities.flattenBitmap(icon));
817 values.put(COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700818 Bitmap.createScaledBitmap(icon,
819 icon.getWidth() / LOW_RES_SCALE_FACTOR,
820 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700821 values.put(COLUMN_LABEL, label);
822 values.put(COLUMN_SYSTEM_STATE, mSystemState);
Sunny Goyal34b65272015-03-11 16:56:52 -0700823 return values;
824 }
825 }
826
827 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
828 byte[] data = c.getBlob(iconIndex);
829 try {
830 return BitmapFactory.decodeByteArray(data, 0, data.length);
831 } catch (Exception e) {
832 return null;
833 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500834 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800835}