blob: b9ac2a47ab792c26b8a192ad9017ceef7c561604 [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;
Sunny Goyal360bc252015-07-01 11:17:15 -070035import android.graphics.Color;
36import android.graphics.Paint;
37import android.graphics.Rect;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.graphics.drawable.Drawable;
Sunny Goyal34b65272015-03-11 16:56:52 -070039import android.os.Handler;
Sunny Goyal75b0f552015-05-20 21:57:06 -070040import android.os.SystemClock;
Sunny Goyal34942622014-08-29 17:20:55 -070041import android.text.TextUtils;
Chris Wren6d0dde02014-02-10 12:16:54 -050042import android.util.Log;
Joe Onorato0589f0f2010-02-08 13:44:00 -080043
Kenny Guyed131872014-04-30 03:02:21 +010044import com.android.launcher3.compat.LauncherActivityInfoCompat;
45import com.android.launcher3.compat.LauncherAppsCompat;
46import com.android.launcher3.compat.UserHandleCompat;
47import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070048import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070049import com.android.launcher3.util.ComponentKey;
Adam Cohen091440a2015-03-18 14:16:05 -070050import com.android.launcher3.util.Thunk;
Kenny Guyed131872014-04-30 03:02:21 +010051
Sunny Goyal4e5cc642015-06-25 16:37:44 -070052import java.util.Collections;
Joe Onorato0589f0f2010-02-08 13:44:00 -080053import java.util.HashMap;
Chris Wren6d0dde02014-02-10 12:16:54 -050054import java.util.HashSet;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080055import java.util.List;
Sunny Goyal0c9a3542015-04-01 16:04:21 -070056import java.util.Locale;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070057import java.util.Set;
Sunny Goyal9ff98082015-05-15 16:59:36 -070058import java.util.Stack;
Joe Onorato0589f0f2010-02-08 13:44:00 -080059
60/**
61 * Cache of application icons. Icons can be made from any thread.
62 */
63public class IconCache {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070064
Joe Onorato0589f0f2010-02-08 13:44:00 -080065 private static final String TAG = "Launcher.IconCache";
66
67 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050068
Sunny Goyal0fc1be12014-08-11 17:05:23 -070069 // Empty class name is used for storing package default entry.
70 private static final String EMPTY_CLASS_NAME = ".";
71
Sunny Goyalbbef77d2014-09-09 16:27:55 -070072 private static final boolean DEBUG = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -080073
Sunny Goyal360bc252015-07-01 11:17:15 -070074 private static final int LOW_RES_SCALE_FACTOR = 5;
Sunny Goyal34b65272015-03-11 16:56:52 -070075
Sunny Goyal316490e2015-06-02 09:38:28 -070076 @Thunk static final Object ICON_UPDATE_TOKEN = new Object();
Sunny Goyal75b0f552015-05-20 21:57:06 -070077
Adam Cohen091440a2015-03-18 14:16:05 -070078 @Thunk static class CacheEntry {
Joe Onorato0589f0f2010-02-08 13:44:00 -080079 public Bitmap icon;
Winson Chungcdefc632015-05-28 12:53:44 -070080 public CharSequence title = "";
81 public CharSequence contentDescription = "";
Sunny Goyal34b65272015-03-11 16:56:52 -070082 public boolean isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -080083 }
84
Sunny Goyal8758ea02015-03-18 10:07:49 -070085 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
Sunny Goyal316490e2015-06-02 09:38:28 -070086 @Thunk final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
Sunny Goyal8758ea02015-03-18 10:07:49 -070087
Daniel Sandlercc8befa2013-06-11 14:45:48 -040088 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070089 private final PackageManager mPackageManager;
Sunny Goyal316490e2015-06-02 09:38:28 -070090 @Thunk final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +010091 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070092 private final HashMap<ComponentKey, CacheEntry> mCache =
93 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080094 private final int mIconDpi;
Sunny Goyal316490e2015-06-02 09:38:28 -070095 @Thunk final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080096
Sunny Goyal316490e2015-06-02 09:38:28 -070097 @Thunk final Handler mWorkerHandler;
Sunny Goyal34b65272015-03-11 16:56:52 -070098
Sunny Goyal360bc252015-07-01 11:17:15 -070099 // The background color used for activity icons. Since these icons are displayed in all-apps
100 // and folders, this would be same as the light quantum panel background. This color
101 // is used to convert icons to RGB_565.
102 private final int mActivityBgColor;
103 // The background color used for package icons. These are displayed in widget tray, which
104 // has a dark quantum panel background.
105 private final int mPackageBgColor;
106 private final BitmapFactory.Options mLowResOptions;
107
108 private String mSystemState;
109 private Bitmap mLowResBitmap;
110 private Canvas mLowResCanvas;
111 private Paint mLowResPaint;
112
Adam Cohen2e6da152015-05-06 11:42:25 -0700113 public IconCache(Context context, InvariantDeviceProfile inv) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800114 mContext = context;
115 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +0100116 mUserManager = UserManagerCompat.getInstance(mContext);
117 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700118 mIconDpi = inv.fillResIconDpi;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800119 mIconDb = new IconDB(context);
Sunny Goyal34b65272015-03-11 16:56:52 -0700120
Sunny Goyal756adbc2015-04-16 15:20:51 -0700121 mWorkerHandler = new Handler(LauncherModel.getWorkerLooper());
Sunny Goyal360bc252015-07-01 11:17:15 -0700122
123 mActivityBgColor = context.getResources().getColor(R.color.quantum_panel_bg_color);
124 mPackageBgColor = context.getResources().getColor(R.color.quantum_panel_bg_color_dark);
125 mLowResOptions = new BitmapFactory.Options();
126 // Always prefer RGB_565 config for low res. If the bitmap has transparency, it will
127 // automatically be loaded as ALPHA_8888.
128 mLowResOptions.inPreferredConfig = Bitmap.Config.RGB_565;
129 updateSystemStateString();
Romain Guya28fd3f2010-03-15 14:44:42 -0700130 }
131
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800132 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700133 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700134 }
135
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700136 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700137 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700138 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700139 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700140 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700141 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700142 }
Michael Jurka721d9722011-08-03 11:49:59 -0700143
144 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700145 }
146
Winson Chung0b9fcf52011-10-31 13:05:15 -0700147 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700148 Resources resources;
149 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700150 resources = mPackageManager.getResourcesForApplication(packageName);
151 } catch (PackageManager.NameNotFoundException e) {
152 resources = null;
153 }
154 if (resources != null) {
155 if (iconId != 0) {
156 return getFullResIcon(resources, iconId);
157 }
158 }
159 return getFullResDefaultActivityIcon();
160 }
161
Michael Jurkadac85912012-05-18 15:04:49 -0700162 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700163 Resources resources;
164 try {
165 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700166 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700167 } catch (PackageManager.NameNotFoundException e) {
168 resources = null;
169 }
170 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700171 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700172 if (iconId != 0) {
173 return getFullResIcon(resources, iconId);
174 }
175 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500176
Michael Jurkac9a96192010-11-01 11:52:08 -0700177 return getFullResDefaultActivityIcon();
178 }
179
Kenny Guyed131872014-04-30 03:02:21 +0100180 private Bitmap makeDefaultIcon(UserHandleCompat user) {
181 Drawable unbadged = getFullResDefaultActivityIcon();
182 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700183 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
184 Math.max(d.getIntrinsicHeight(), 1),
185 Bitmap.Config.ARGB_8888);
186 Canvas c = new Canvas(b);
187 d.setBounds(0, 0, b.getWidth(), b.getHeight());
188 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700189 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700190 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800191 }
192
193 /**
194 * Remove any records for the supplied ComponentName.
195 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700196 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700197 mCache.remove(new ComponentKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800198 }
199
200 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800201 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500202 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800203 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700204 HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
205 for (ComponentKey key: mCache.keySet()) {
Kenny Guyed131872014-04-30 03:02:21 +0100206 if (key.componentName.getPackageName().equals(packageName)
207 && key.user.equals(user)) {
208 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500209 }
210 }
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700211 for (ComponentKey condemned: forDeletion) {
Kenny Guyed131872014-04-30 03:02:21 +0100212 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500213 }
214 }
215
216 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800217 * Updates the entries related to the given package in memory and persistent DB.
218 */
219 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
220 removeIconsForPkg(packageName, user);
221 try {
222 PackageInfo info = mPackageManager.getPackageInfo(packageName,
223 PackageManager.GET_UNINSTALLED_PACKAGES);
224 long userSerial = mUserManager.getSerialNumberForUser(user);
225 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700226 addIconToDBAndMemCache(app, info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800227 }
228 } catch (NameNotFoundException e) {
229 Log.d(TAG, "Package not found", e);
230 return;
231 }
232 }
233
234 /**
235 * Removes the entries related to the given package in memory and persistent DB.
236 */
237 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
238 removeFromMemCacheLocked(packageName, user);
239 long userSerial = mUserManager.getSerialNumberForUser(user);
240 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
241 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
242 new String[] {packageName + "/%", Long.toString(userSerial)});
243 }
244
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700245 public void updateDbIcons(Set<String> ignorePackagesForMainUser) {
Sunny Goyal75b0f552015-05-20 21:57:06 -0700246 // Remove all active icon update tasks.
247 mWorkerHandler.removeCallbacksAndMessages(ICON_UPDATE_TOKEN);
248
Sunny Goyal360bc252015-07-01 11:17:15 -0700249 updateSystemStateString();
Sunny Goyal75b0f552015-05-20 21:57:06 -0700250 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
251 // Query for the set of apps
252 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
253 // Fail if we don't have any apps
254 // TODO: Fix this. Only fail for the current user.
255 if (apps == null || apps.isEmpty()) {
256 return;
257 }
258
259 // Update icon cache. This happens in segments and {@link #onPackageIconsUpdated}
260 // is called by the icon cache when the job is complete.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700261 updateDBIcons(user, apps, UserHandleCompat.myUserHandle().equals(user)
262 ? ignorePackagesForMainUser : Collections.<String>emptySet());
Sunny Goyal75b0f552015-05-20 21:57:06 -0700263 }
264 }
265
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800266 /**
267 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
268 * the DB and are updated.
269 * @return The set of packages for which icons have updated.
270 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700271 private void updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps,
272 Set<String> ignorePackages) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800273 long userSerial = mUserManager.getSerialNumberForUser(user);
274 PackageManager pm = mContext.getPackageManager();
275 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
276 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
277 pkgInfoMap.put(info.packageName, info);
278 }
279
280 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
281 for (LauncherActivityInfoCompat app : apps) {
282 componentMap.put(app.getComponentName(), app);
283 }
284
285 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
286 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700287 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION,
288 IconDB.COLUMN_SYSTEM_STATE},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800289 IconDB.COLUMN_USER + " = ? ",
290 new String[] {Long.toString(userSerial)},
291 null, null, null);
292
293 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
294 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
295 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
296 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700297 final int systemStateIndex = c.getColumnIndex(IconDB.COLUMN_SYSTEM_STATE);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800298
299 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
Sunny Goyal75b0f552015-05-20 21:57:06 -0700300 Stack<LauncherActivityInfoCompat> appsToUpdate = new Stack<>();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800301
302 while (c.moveToNext()) {
303 String cn = c.getString(indexComponent);
304 ComponentName component = ComponentName.unflattenFromString(cn);
305 PackageInfo info = pkgInfoMap.get(component.getPackageName());
306 if (info == null) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700307 if (!ignorePackages.contains(component.getPackageName())) {
308 remove(component, user);
309 itemsToRemove.add(c.getInt(rowIndex));
310 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800311 continue;
312 }
313 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
314 // Application is not present
315 continue;
316 }
317
318 long updateTime = c.getLong(indexLastUpdate);
319 int version = c.getInt(indexVersion);
320 LauncherActivityInfoCompat app = componentMap.remove(component);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700321 if (version == info.versionCode && updateTime == info.lastUpdateTime &&
Sunny Goyal360bc252015-07-01 11:17:15 -0700322 TextUtils.equals(mSystemState, c.getString(systemStateIndex))) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800323 continue;
324 }
325 if (app == null) {
Sunny Goyal091f0ff2015-06-04 15:19:31 -0700326 remove(component, user);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800327 itemsToRemove.add(c.getInt(rowIndex));
Sunny Goyal75b0f552015-05-20 21:57:06 -0700328 } else {
329 appsToUpdate.add(app);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800330 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800331 }
332 c.close();
333 if (!itemsToRemove.isEmpty()) {
334 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
Sunny Goyalb1622cc2015-06-10 16:00:42 -0700335 Utilities.createDbSelectionQuery(IconDB.COLUMN_ROWID, itemsToRemove),
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800336 null);
337 }
338
339 // Insert remaining apps.
Sunny Goyal75b0f552015-05-20 21:57:06 -0700340 if (!componentMap.isEmpty() || !appsToUpdate.isEmpty()) {
341 Stack<LauncherActivityInfoCompat> appsToAdd = new Stack<>();
342 appsToAdd.addAll(componentMap.values());
343 new SerializedIconUpdateTask(userSerial, pkgInfoMap,
344 appsToAdd, appsToUpdate).scheduleNext();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800345 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800346 }
347
Sunny Goyal316490e2015-06-02 09:38:28 -0700348 @Thunk void addIconToDBAndMemCache(LauncherActivityInfoCompat app, PackageInfo info,
Sunny Goyald180cf72015-04-06 12:45:40 -0700349 long userSerial) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700350 // Reuse the existing entry if it already exists in the DB. This ensures that we do not
351 // create bitmap if it was already created during loader.
352 ContentValues values = updateCacheAndGetContentValues(app, false);
Sunny Goyald180cf72015-04-06 12:45:40 -0700353 addIconToDB(values, app.getComponentName(), info, userSerial);
Sunny Goyald180cf72015-04-06 12:45:40 -0700354 }
355
356 /**
357 * Updates {@param values} to contain versoning information and adds it to the DB.
358 * @param values {@link ContentValues} containing icon & title
359 */
360 private void addIconToDB(ContentValues values, ComponentName key,
361 PackageInfo info, long userSerial) {
362 values.put(IconDB.COLUMN_COMPONENT, key.flattenToString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800363 values.put(IconDB.COLUMN_USER, userSerial);
364 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
365 values.put(IconDB.COLUMN_VERSION, info.versionCode);
366 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
367 SQLiteDatabase.CONFLICT_REPLACE);
368 }
369
Sunny Goyal316490e2015-06-02 09:38:28 -0700370 @Thunk ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app,
Sunny Goyal9ff98082015-05-15 16:59:36 -0700371 boolean replaceExisting) {
372 final ComponentKey key = new ComponentKey(app.getComponentName(), app.getUser());
373 CacheEntry entry = null;
374 if (!replaceExisting) {
375 entry = mCache.get(key);
376 // We can't reuse the entry if the high-res icon is not present.
377 if (entry == null || entry.isLowResIcon || entry.icon == null) {
378 entry = null;
379 }
380 }
381 if (entry == null) {
382 entry = new CacheEntry();
383 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
384 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800385 entry.title = app.getLabel();
386 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700387 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800388
Sunny Goyal360bc252015-07-01 11:17:15 -0700389 return newContentValues(entry.icon, entry.title.toString(), mActivityBgColor);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800390 }
391
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800392 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700393 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
394 * @return a request ID that can be used to cancel the request.
395 */
396 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
397 Runnable request = new Runnable() {
398
399 @Override
400 public void run() {
401 if (info instanceof AppInfo) {
402 getTitleAndIcon((AppInfo) info, null, false);
403 } else if (info instanceof ShortcutInfo) {
404 ShortcutInfo st = (ShortcutInfo) info;
405 getTitleAndIcon(st,
406 st.promisedIntent != null ? st.promisedIntent : st.intent,
407 st.user, false);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700408 } else if (info instanceof PackageItemInfo) {
409 PackageItemInfo pti = (PackageItemInfo) info;
410 getTitleAndIconForApp(pti.packageName, pti.user, false, pti);
Sunny Goyal34b65272015-03-11 16:56:52 -0700411 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700412 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700413
414 @Override
415 public void run() {
416 caller.reapplyItemInfo(info);
417 }
418 });
419 }
420 };
421 mWorkerHandler.post(request);
422 return new IconLoadRequest(request, mWorkerHandler);
423 }
424
Sunny Goyal756adbc2015-04-16 15:20:51 -0700425 private Bitmap getNonNullIcon(CacheEntry entry, UserHandleCompat user) {
426 return entry.icon == null ? getDefaultIcon(user) : entry.icon;
427 }
428
Sunny Goyal34b65272015-03-11 16:56:52 -0700429 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800430 * Fill in "application" with the icon and label for "info."
431 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700432 public synchronized void getTitleAndIcon(AppInfo application,
433 LauncherActivityInfoCompat info, boolean useLowResIcon) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700434 UserHandleCompat user = info == null ? application.user : info.getUser();
435 CacheEntry entry = cacheLocked(application.componentName, info, user,
Sunny Goyal34b65272015-03-11 16:56:52 -0700436 false, useLowResIcon);
Winson Chung82b016c2015-05-08 17:00:10 -0700437 application.title = Utilities.trim(entry.title);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700438 application.iconBitmap = getNonNullIcon(entry, user);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700439 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700440 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800441 }
442
Sunny Goyal34b65272015-03-11 16:56:52 -0700443 /**
Sunny Goyal77919b92015-05-06 16:53:21 -0700444 * Updates {@param application} only if a valid entry is found.
445 */
446 public synchronized void updateTitleAndIcon(AppInfo application) {
447 CacheEntry entry = cacheLocked(application.componentName, null, application.user,
448 false, application.usingLowResIcon);
449 if (entry.icon != null && !isDefaultIcon(entry.icon, application.user)) {
Winson Chung82b016c2015-05-08 17:00:10 -0700450 application.title = Utilities.trim(entry.title);
Sunny Goyal77919b92015-05-06 16:53:21 -0700451 application.iconBitmap = entry.icon;
452 application.contentDescription = entry.contentDescription;
453 application.usingLowResIcon = entry.isLowResIcon;
454 }
455 }
456
457 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700458 * Returns a high res icon for the given intent and user
459 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700460 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
461 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 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800466 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700467
468 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700469 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700470 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800471 }
472
Sunny Goyal34942622014-08-29 17:20:55 -0700473 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800474 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
475 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700476 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700477 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700478 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700479 ComponentName component = intent.getComponent();
480 // null info means not installed, but if we have a component from the intent then
481 // we should still look in the cache for restored app icons.
482 if (component == null) {
483 shortcutInfo.setIcon(getDefaultIcon(user));
484 shortcutInfo.title = "";
485 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700486 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700487 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800488 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700489 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700490 }
491 }
492
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800493 /**
494 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
495 */
496 public synchronized void getTitleAndIcon(
497 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700498 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
499 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700500 shortcutInfo.setIcon(getNonNullIcon(entry, user));
Winson Chung82b016c2015-05-08 17:00:10 -0700501 shortcutInfo.title = Utilities.trim(entry.title);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800502 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700503 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800504 }
Sunny Goyal34942622014-08-29 17:20:55 -0700505
Sunny Goyald180cf72015-04-06 12:45:40 -0700506 /**
507 * Fill in {@param appInfo} with the icon and label for {@param packageName}
508 */
509 public synchronized void getTitleAndIconForApp(
Hyunyoung Song3f471442015-04-08 19:01:34 -0700510 String packageName, UserHandleCompat user, boolean useLowResIcon,
511 PackageItemInfo infoOut) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700512 CacheEntry entry = getEntryForPackageLocked(packageName, user, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700513 infoOut.iconBitmap = getNonNullIcon(entry, user);
Winson Chung82b016c2015-05-08 17:00:10 -0700514 infoOut.title = Utilities.trim(entry.title);
Hyunyoung Song3f471442015-04-08 19:01:34 -0700515 infoOut.usingLowResIcon = entry.isLowResIcon;
516 infoOut.contentDescription = entry.contentDescription;
Sunny Goyald180cf72015-04-06 12:45:40 -0700517 }
518
Sunny Goyal736f5af2014-10-16 14:07:29 -0700519 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100520 if (!mDefaultIcons.containsKey(user)) {
521 mDefaultIcons.put(user, makeDefaultIcon(user));
522 }
523 return mDefaultIcons.get(user);
524 }
525
Kenny Guyed131872014-04-30 03:02:21 +0100526 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
527 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700528 }
529
Sunny Goyal736f5af2014-10-16 14:07:29 -0700530 /**
531 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
532 * This method is not thread safe, it must be called from a synchronized method.
533 */
Kenny Guyed131872014-04-30 03:02:21 +0100534 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700535 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700536 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100537 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700538 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800539 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100540 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500541
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800542 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700543 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800544 if (info != null) {
545 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500546 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700547 if (usePackageIcon) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700548 CacheEntry packageEntry = getEntryForPackageLocked(
549 componentName.getPackageName(), user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700550 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700551 if (DEBUG) Log.d(TAG, "using package default icon for " +
552 componentName.toShortString());
553 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700554 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800555 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700556 }
557 }
558 if (entry.icon == null) {
559 if (DEBUG) Log.d(TAG, "using default icon for " +
560 componentName.toShortString());
561 entry.icon = getDefaultIcon(user);
562 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700563 }
564 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800565
566 if (TextUtils.isEmpty(entry.title) && info != null) {
Winson Chung82b016c2015-05-08 17:00:10 -0700567 entry.title = info.getLabel();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800568 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
569 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800570 }
571 return entry;
572 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400573
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700574 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700575 * Adds a default package entry in the cache. This entry is not persisted and will be removed
576 * when the cache is flushed.
577 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700578 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700579 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800580 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700581
Sunny Goyald180cf72015-04-06 12:45:40 -0700582 CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700583 if (!TextUtils.isEmpty(title)) {
584 entry.title = title;
585 }
586 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700587 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700588 }
589 }
590
591 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700592 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700593 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyald180cf72015-04-06 12:45:40 -0700594 *
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700595 */
Sunny Goyald180cf72015-04-06 12:45:40 -0700596 private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
597 boolean useLowResIcon) {
Sunny Goyal091f0ff2015-06-04 15:19:31 -0700598 ComponentName cn = new ComponentName(packageName, packageName + EMPTY_CLASS_NAME);
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700599 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700600 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal091f0ff2015-06-04 15:19:31 -0700601
Sunny Goyald180cf72015-04-06 12:45:40 -0700602 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700603 entry = new CacheEntry();
Winson Chungcdefc632015-05-28 12:53:44 -0700604 boolean entryUpdated = true;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700605
Sunny Goyald180cf72015-04-06 12:45:40 -0700606 // Check the DB first.
607 if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
608 try {
609 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
610 ApplicationInfo appInfo = info.applicationInfo;
611 if (appInfo == null) {
612 throw new NameNotFoundException("ApplicationInfo is null");
613 }
614 Drawable drawable = mUserManager.getBadgedDrawableForUser(
615 appInfo.loadIcon(mPackageManager), user);
616 entry.icon = Utilities.createIconBitmap(drawable, mContext);
617 entry.title = appInfo.loadLabel(mPackageManager);
618 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
619 entry.isLowResIcon = false;
620
621 // Add the icon in the DB here, since these do not get written during
622 // package updates.
623 ContentValues values =
Sunny Goyal360bc252015-07-01 11:17:15 -0700624 newContentValues(entry.icon, entry.title.toString(), mPackageBgColor);
Sunny Goyald180cf72015-04-06 12:45:40 -0700625 addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
626
627 } catch (NameNotFoundException e) {
628 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
Winson Chungcdefc632015-05-28 12:53:44 -0700629 entryUpdated = false;
Sunny Goyald180cf72015-04-06 12:45:40 -0700630 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700631 }
Winson Chungcdefc632015-05-28 12:53:44 -0700632
633 // Only add a filled-out entry to the cache
634 if (entryUpdated) {
635 mCache.put(cacheKey, entry);
636 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700637 }
638 return entry;
639 }
640
Chris Wren6d0dde02014-02-10 12:16:54 -0500641 /**
642 * Pre-load an icon into the persistent cache.
643 *
644 * <P>Queries for a component that does not exist in the package manager
645 * will be answered by the persistent cache.
646 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500647 * @param componentName the icon should be returned for this component
648 * @param icon the icon to be persisted
649 * @param dpi the native density of the icon
650 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800651 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
652 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500653 // TODO rescale to the correct native DPI
654 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800655 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500656 packageManager.getActivityIcon(componentName);
657 // component is present on the system already, do nothing
658 return;
659 } catch (PackageManager.NameNotFoundException e) {
660 // pass
661 }
662
Sunny Goyal360bc252015-07-01 11:17:15 -0700663 ContentValues values = newContentValues(icon, label, Color.TRANSPARENT);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800664 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
665 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800666 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
667 SQLiteDatabase.CONFLICT_REPLACE);
668 }
669
Sunny Goyal34b65272015-03-11 16:56:52 -0700670 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
671 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800672 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700673 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
674 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800675 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
676 new String[] {component.flattenToString(),
677 Long.toString(mUserManager.getSerialNumberForUser(user))},
678 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500679 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800680 if (c.moveToNext()) {
Sunny Goyal360bc252015-07-01 11:17:15 -0700681 entry.icon = loadIconNoResize(c, 0, lowRes ? mLowResOptions : null);
Sunny Goyal34b65272015-03-11 16:56:52 -0700682 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800683 entry.title = c.getString(1);
684 if (entry.title == null) {
685 entry.title = "";
686 entry.contentDescription = "";
687 } else {
688 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500689 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800690 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500691 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500692 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800693 c.close();
694 }
695 return false;
696 }
697
Sunny Goyal34b65272015-03-11 16:56:52 -0700698 public static class IconLoadRequest {
699 private final Runnable mRunnable;
700 private final Handler mHandler;
701
702 IconLoadRequest(Runnable runnable, Handler handler) {
703 mRunnable = runnable;
704 mHandler = handler;
705 }
706
707 public void cancel() {
708 mHandler.removeCallbacks(mRunnable);
709 }
710 }
711
Sunny Goyal9ff98082015-05-15 16:59:36 -0700712 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -0700713 * A runnable that updates invalid icons and adds missing icons in the DB for the provided
714 * LauncherActivityInfoCompat list. Items are updated/added one at a time, so that the
715 * worker thread doesn't get blocked.
Sunny Goyal9ff98082015-05-15 16:59:36 -0700716 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700717 @Thunk class SerializedIconUpdateTask implements Runnable {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700718 private final long mUserSerial;
719 private final HashMap<String, PackageInfo> mPkgInfoMap;
720 private final Stack<LauncherActivityInfoCompat> mAppsToAdd;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700721 private final Stack<LauncherActivityInfoCompat> mAppsToUpdate;
722 private final HashSet<String> mUpdatedPackages = new HashSet<String>();
Sunny Goyal9ff98082015-05-15 16:59:36 -0700723
Sunny Goyal316490e2015-06-02 09:38:28 -0700724 @Thunk SerializedIconUpdateTask(long userSerial, HashMap<String, PackageInfo> pkgInfoMap,
Sunny Goyal75b0f552015-05-20 21:57:06 -0700725 Stack<LauncherActivityInfoCompat> appsToAdd,
726 Stack<LauncherActivityInfoCompat> appsToUpdate) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700727 mUserSerial = userSerial;
728 mPkgInfoMap = pkgInfoMap;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700729 mAppsToAdd = appsToAdd;
730 mAppsToUpdate = appsToUpdate;
Sunny Goyal9ff98082015-05-15 16:59:36 -0700731 }
732
733 @Override
734 public void run() {
Sunny Goyal75b0f552015-05-20 21:57:06 -0700735 if (!mAppsToUpdate.isEmpty()) {
736 LauncherActivityInfoCompat app = mAppsToUpdate.pop();
737 String cn = app.getComponentName().flattenToString();
738 ContentValues values = updateCacheAndGetContentValues(app, true);
739 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
740 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
741 new String[] {cn, Long.toString(mUserSerial)});
742 mUpdatedPackages.add(app.getComponentName().getPackageName());
743
744 if (mAppsToUpdate.isEmpty() && !mUpdatedPackages.isEmpty()) {
745 // No more app to update. Notify model.
746 LauncherAppState.getInstance().getModel().onPackageIconsUpdated(
747 mUpdatedPackages, mUserManager.getUserForSerialNumber(mUserSerial));
748 }
749
750 // Let it run one more time.
751 scheduleNext();
752 } else if (!mAppsToAdd.isEmpty()) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700753 LauncherActivityInfoCompat app = mAppsToAdd.pop();
754 PackageInfo info = mPkgInfoMap.get(app.getComponentName().getPackageName());
755 if (info != null) {
756 synchronized (IconCache.this) {
757 addIconToDBAndMemCache(app, info, mUserSerial);
758 }
759 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700760
761 if (!mAppsToAdd.isEmpty()) {
762 scheduleNext();
763 }
Sunny Goyal9ff98082015-05-15 16:59:36 -0700764 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700765 }
766
767 public void scheduleNext() {
768 mWorkerHandler.postAtTime(this, ICON_UPDATE_TOKEN, SystemClock.uptimeMillis() + 1);
Sunny Goyal9ff98082015-05-15 16:59:36 -0700769 }
770 }
771
Sunny Goyal360bc252015-07-01 11:17:15 -0700772 private void updateSystemStateString() {
773 mSystemState = Locale.getDefault().toString();
774 }
775
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800776 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal360bc252015-07-01 11:17:15 -0700777 private final static int DB_VERSION = 6;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800778
779 private final static String TABLE_NAME = "icons";
780 private final static String COLUMN_ROWID = "rowid";
781 private final static String COLUMN_COMPONENT = "componentName";
782 private final static String COLUMN_USER = "profileId";
783 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
784 private final static String COLUMN_VERSION = "version";
785 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700786 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800787 private final static String COLUMN_LABEL = "label";
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700788 private final static String COLUMN_SYSTEM_STATE = "system_state";
789
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800790 public IconDB(Context context) {
791 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700792 }
793
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800794 @Override
795 public void onCreate(SQLiteDatabase db) {
796 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
797 COLUMN_COMPONENT + " TEXT NOT NULL, " +
798 COLUMN_USER + " INTEGER NOT NULL, " +
799 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
800 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
801 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700802 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800803 COLUMN_LABEL + " TEXT, " +
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700804 COLUMN_SYSTEM_STATE + " TEXT, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800805 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
806 ");");
807 }
808
809 @Override
810 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
811 if (oldVersion != newVersion) {
812 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500813 }
814 }
815
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800816 @Override
817 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
818 if (oldVersion != newVersion) {
819 clearDB(db);
820 }
Kenny Guyed131872014-04-30 03:02:21 +0100821 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500822
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800823 private void clearDB(SQLiteDatabase db) {
824 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
825 onCreate(db);
826 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700827 }
828
Sunny Goyal360bc252015-07-01 11:17:15 -0700829 private ContentValues newContentValues(Bitmap icon, String label, int lowResBackgroundColor) {
830 ContentValues values = new ContentValues();
831 values.put(IconDB.COLUMN_ICON, Utilities.flattenBitmap(icon));
832
833 values.put(IconDB.COLUMN_LABEL, label);
834 values.put(IconDB.COLUMN_SYSTEM_STATE, mSystemState);
835
836 if (lowResBackgroundColor == Color.TRANSPARENT) {
837 values.put(IconDB.COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
838 Bitmap.createScaledBitmap(icon,
839 icon.getWidth() / LOW_RES_SCALE_FACTOR,
840 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
841 } else {
842 synchronized (this) {
843 if (mLowResBitmap == null) {
844 mLowResBitmap = Bitmap.createBitmap(icon.getWidth() / LOW_RES_SCALE_FACTOR,
845 icon.getHeight() / LOW_RES_SCALE_FACTOR, Bitmap.Config.RGB_565);
846 mLowResCanvas = new Canvas(mLowResBitmap);
847 mLowResPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
848 }
849 mLowResCanvas.drawColor(lowResBackgroundColor);
850 mLowResCanvas.drawBitmap(icon, new Rect(0, 0, icon.getWidth(), icon.getHeight()),
851 new Rect(0, 0, mLowResBitmap.getWidth(), mLowResBitmap.getHeight()),
852 mLowResPaint);
853 values.put(IconDB.COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(mLowResBitmap));
854 }
855 }
856 return values;
857 }
858
859 private static Bitmap loadIconNoResize(Cursor c, int iconIndex, BitmapFactory.Options options) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700860 byte[] data = c.getBlob(iconIndex);
861 try {
Sunny Goyal360bc252015-07-01 11:17:15 -0700862 return BitmapFactory.decodeByteArray(data, 0, data.length, options);
Sunny Goyal34b65272015-03-11 16:56:52 -0700863 } catch (Exception e) {
864 return null;
865 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500866 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800867}