blob: a16067d1619d65b578f26ba5f3b7213f00aa73ef [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 Goyal316490e2015-06-02 09:38:28 -070072 @Thunk static final Object ICON_UPDATE_TOKEN = new Object();
Sunny Goyal75b0f552015-05-20 21:57:06 -070073
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<>();
Sunny Goyal316490e2015-06-02 09:38:28 -070082 @Thunk final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
Sunny Goyal8758ea02015-03-18 10:07:49 -070083
Daniel Sandlercc8befa2013-06-11 14:45:48 -040084 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070085 private final PackageManager mPackageManager;
Sunny Goyal316490e2015-06-02 09:38:28 -070086 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +010087 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;
Sunny Goyal316490e2015-06-02 09:38:28 -070091 @Thunk final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080092
Sunny Goyal316490e2015-06-02 09:38:28 -070093 @Thunk final Handler mWorkerHandler;
Sunny Goyal34b65272015-03-11 16:56:52 -070094
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 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) {
573 ComponentName cn = new ComponentName(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 Goyald180cf72015-04-06 12:45:40 -0700576 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700577 entry = new CacheEntry();
Winson Chungcdefc632015-05-28 12:53:44 -0700578 boolean entryUpdated = true;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700579
Sunny Goyald180cf72015-04-06 12:45:40 -0700580 // Check the DB first.
581 if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
582 try {
583 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
584 ApplicationInfo appInfo = info.applicationInfo;
585 if (appInfo == null) {
586 throw new NameNotFoundException("ApplicationInfo is null");
587 }
588 Drawable drawable = mUserManager.getBadgedDrawableForUser(
589 appInfo.loadIcon(mPackageManager), user);
590 entry.icon = Utilities.createIconBitmap(drawable, mContext);
591 entry.title = appInfo.loadLabel(mPackageManager);
592 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
593 entry.isLowResIcon = false;
594
595 // Add the icon in the DB here, since these do not get written during
596 // package updates.
597 ContentValues values =
598 mIconDb.newContentValues(entry.icon, entry.title.toString());
599 addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
600
601 } catch (NameNotFoundException e) {
602 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
Winson Chungcdefc632015-05-28 12:53:44 -0700603 entryUpdated = false;
Sunny Goyald180cf72015-04-06 12:45:40 -0700604 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700605 }
Winson Chungcdefc632015-05-28 12:53:44 -0700606
607 // Only add a filled-out entry to the cache
608 if (entryUpdated) {
609 mCache.put(cacheKey, entry);
610 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700611 }
612 return entry;
613 }
614
Chris Wren6d0dde02014-02-10 12:16:54 -0500615 /**
616 * Pre-load an icon into the persistent cache.
617 *
618 * <P>Queries for a component that does not exist in the package manager
619 * will be answered by the persistent cache.
620 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500621 * @param componentName the icon should be returned for this component
622 * @param icon the icon to be persisted
623 * @param dpi the native density of the icon
624 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800625 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
626 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500627 // TODO rescale to the correct native DPI
628 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800629 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500630 packageManager.getActivityIcon(componentName);
631 // component is present on the system already, do nothing
632 return;
633 } catch (PackageManager.NameNotFoundException e) {
634 // pass
635 }
636
Sunny Goyal34b65272015-03-11 16:56:52 -0700637 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800638 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
639 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800640 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
641 SQLiteDatabase.CONFLICT_REPLACE);
642 }
643
Sunny Goyal34b65272015-03-11 16:56:52 -0700644 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
645 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800646 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700647 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
648 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800649 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
650 new String[] {component.flattenToString(),
651 Long.toString(mUserManager.getSerialNumberForUser(user))},
652 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500653 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800654 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700655 entry.icon = loadIconNoResize(c, 0);
656 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800657 entry.title = c.getString(1);
658 if (entry.title == null) {
659 entry.title = "";
660 entry.contentDescription = "";
661 } else {
662 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500663 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800664 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500665 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500666 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800667 c.close();
668 }
669 return false;
670 }
671
Sunny Goyal34b65272015-03-11 16:56:52 -0700672 public static class IconLoadRequest {
673 private final Runnable mRunnable;
674 private final Handler mHandler;
675
676 IconLoadRequest(Runnable runnable, Handler handler) {
677 mRunnable = runnable;
678 mHandler = handler;
679 }
680
681 public void cancel() {
682 mHandler.removeCallbacks(mRunnable);
683 }
684 }
685
Sunny Goyal9ff98082015-05-15 16:59:36 -0700686 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -0700687 * A runnable that updates invalid icons and adds missing icons in the DB for the provided
688 * LauncherActivityInfoCompat list. Items are updated/added one at a time, so that the
689 * worker thread doesn't get blocked.
Sunny Goyal9ff98082015-05-15 16:59:36 -0700690 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700691 @Thunk class SerializedIconUpdateTask implements Runnable {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700692 private final long mUserSerial;
693 private final HashMap<String, PackageInfo> mPkgInfoMap;
694 private final Stack<LauncherActivityInfoCompat> mAppsToAdd;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700695 private final Stack<LauncherActivityInfoCompat> mAppsToUpdate;
696 private final HashSet<String> mUpdatedPackages = new HashSet<String>();
Sunny Goyal9ff98082015-05-15 16:59:36 -0700697
Sunny Goyal316490e2015-06-02 09:38:28 -0700698 @Thunk SerializedIconUpdateTask(long userSerial, HashMap<String, PackageInfo> pkgInfoMap,
Sunny Goyal75b0f552015-05-20 21:57:06 -0700699 Stack<LauncherActivityInfoCompat> appsToAdd,
700 Stack<LauncherActivityInfoCompat> appsToUpdate) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700701 mUserSerial = userSerial;
702 mPkgInfoMap = pkgInfoMap;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700703 mAppsToAdd = appsToAdd;
704 mAppsToUpdate = appsToUpdate;
Sunny Goyal9ff98082015-05-15 16:59:36 -0700705 }
706
707 @Override
708 public void run() {
Sunny Goyal75b0f552015-05-20 21:57:06 -0700709 if (!mAppsToUpdate.isEmpty()) {
710 LauncherActivityInfoCompat app = mAppsToUpdate.pop();
711 String cn = app.getComponentName().flattenToString();
712 ContentValues values = updateCacheAndGetContentValues(app, true);
713 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
714 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
715 new String[] {cn, Long.toString(mUserSerial)});
716 mUpdatedPackages.add(app.getComponentName().getPackageName());
717
718 if (mAppsToUpdate.isEmpty() && !mUpdatedPackages.isEmpty()) {
719 // No more app to update. Notify model.
720 LauncherAppState.getInstance().getModel().onPackageIconsUpdated(
721 mUpdatedPackages, mUserManager.getUserForSerialNumber(mUserSerial));
722 }
723
724 // Let it run one more time.
725 scheduleNext();
726 } else if (!mAppsToAdd.isEmpty()) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700727 LauncherActivityInfoCompat app = mAppsToAdd.pop();
728 PackageInfo info = mPkgInfoMap.get(app.getComponentName().getPackageName());
729 if (info != null) {
730 synchronized (IconCache.this) {
731 addIconToDBAndMemCache(app, info, mUserSerial);
732 }
733 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700734
735 if (!mAppsToAdd.isEmpty()) {
736 scheduleNext();
737 }
Sunny Goyal9ff98082015-05-15 16:59:36 -0700738 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700739 }
740
741 public void scheduleNext() {
742 mWorkerHandler.postAtTime(this, ICON_UPDATE_TOKEN, SystemClock.uptimeMillis() + 1);
Sunny Goyal9ff98082015-05-15 16:59:36 -0700743 }
744 }
745
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800746 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal77919b92015-05-06 16:53:21 -0700747 private final static int DB_VERSION = 4;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800748
749 private final static String TABLE_NAME = "icons";
750 private final static String COLUMN_ROWID = "rowid";
751 private final static String COLUMN_COMPONENT = "componentName";
752 private final static String COLUMN_USER = "profileId";
753 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
754 private final static String COLUMN_VERSION = "version";
755 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700756 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800757 private final static String COLUMN_LABEL = "label";
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700758 private final static String COLUMN_SYSTEM_STATE = "system_state";
759
760 public String mSystemState;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800761
762 public IconDB(Context context) {
763 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700764 updateSystemStateString(context);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800765 }
766
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700767 public void updateSystemStateString(Context c) {
768 mSystemState = Locale.getDefault().toString() + ","
769 + c.getResources().getConfiguration().mcc;
770 }
771
772
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800773 @Override
774 public void onCreate(SQLiteDatabase db) {
775 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
776 COLUMN_COMPONENT + " TEXT NOT NULL, " +
777 COLUMN_USER + " INTEGER NOT NULL, " +
778 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
779 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
780 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700781 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800782 COLUMN_LABEL + " TEXT, " +
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700783 COLUMN_SYSTEM_STATE + " TEXT, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800784 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
785 ");");
786 }
787
788 @Override
789 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
790 if (oldVersion != newVersion) {
791 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500792 }
793 }
794
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800795 @Override
796 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
797 if (oldVersion != newVersion) {
798 clearDB(db);
799 }
Kenny Guyed131872014-04-30 03:02:21 +0100800 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500801
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800802 private void clearDB(SQLiteDatabase db) {
803 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
804 onCreate(db);
805 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700806
807 public ContentValues newContentValues(Bitmap icon, String label) {
808 ContentValues values = new ContentValues();
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700809 values.put(COLUMN_ICON, Utilities.flattenBitmap(icon));
810 values.put(COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700811 Bitmap.createScaledBitmap(icon,
812 icon.getWidth() / LOW_RES_SCALE_FACTOR,
813 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700814 values.put(COLUMN_LABEL, label);
815 values.put(COLUMN_SYSTEM_STATE, mSystemState);
Sunny Goyal34b65272015-03-11 16:56:52 -0700816 return values;
817 }
818 }
819
820 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
821 byte[] data = c.getBlob(iconIndex);
822 try {
823 return BitmapFactory.decodeByteArray(data, 0, data.length);
824 } catch (Exception e) {
825 return null;
826 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500827 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800828}