blob: 0c91a71134a2c8a93a2dfca42b8d4d8a35d2a5db [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;
Adam Cohenb6d33df2013-10-15 10:18:02 -070052import java.util.Iterator;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080053import java.util.List;
Sunny Goyal0c9a3542015-04-01 16:04:21 -070054import java.util.Locale;
Adam Cohenb6d33df2013-10-15 10:18:02 -070055import java.util.Map.Entry;
Sunny Goyal9ff98082015-05-15 16:59:36 -070056import java.util.Stack;
Joe Onorato0589f0f2010-02-08 13:44:00 -080057
58/**
59 * Cache of application icons. Icons can be made from any thread.
60 */
61public class IconCache {
Sunny Goyal0fc1be12014-08-11 17:05:23 -070062
Joe Onorato0589f0f2010-02-08 13:44:00 -080063 private static final String TAG = "Launcher.IconCache";
64
65 private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
Chris Wren6d0dde02014-02-10 12:16:54 -050066
Sunny Goyal0fc1be12014-08-11 17:05:23 -070067 // Empty class name is used for storing package default entry.
68 private static final String EMPTY_CLASS_NAME = ".";
69
Sunny Goyalbbef77d2014-09-09 16:27:55 -070070 private static final boolean DEBUG = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -080071
Sunny Goyal34b65272015-03-11 16:56:52 -070072 private static final int LOW_RES_SCALE_FACTOR = 8;
73
Sunny Goyal75b0f552015-05-20 21:57:06 -070074 private static final Object ICON_UPDATE_TOKEN = new Object();
75
Adam Cohen091440a2015-03-18 14:16:05 -070076 @Thunk static class CacheEntry {
Joe Onorato0589f0f2010-02-08 13:44:00 -080077 public Bitmap icon;
Kenny Guyd6fe5262014-07-21 17:11:41 +010078 public CharSequence title;
79 public CharSequence contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -070080 public boolean isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -080081 }
82
Sunny Goyal8758ea02015-03-18 10:07:49 -070083 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
84 private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
85
Daniel Sandlercc8befa2013-06-11 14:45:48 -040086 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070087 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010088 private final UserManagerCompat mUserManager;
89 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070090 private final HashMap<ComponentKey, CacheEntry> mCache =
91 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080092 private final int mIconDpi;
93 private final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080094
Sunny Goyal34b65272015-03-11 16:56:52 -070095 private final Handler mWorkerHandler;
96
Adam Cohen2e6da152015-05-06 11:42:25 -070097 public IconCache(Context context, InvariantDeviceProfile inv) {
Winson Chungd83f5f42012-02-13 14:27:42 -080098 ActivityManager activityManager =
99 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
100
Joe Onorato0589f0f2010-02-08 13:44:00 -0800101 mContext = context;
102 mPackageManager = context.getPackageManager();
Kenny Guyed131872014-04-30 03:02:21 +0100103 mUserManager = UserManagerCompat.getInstance(mContext);
104 mLauncherApps = LauncherAppsCompat.getInstance(mContext);
Winson Chungd83f5f42012-02-13 14:27:42 -0800105 mIconDpi = activityManager.getLauncherLargeIconDensity();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800106 mIconDb = new IconDB(context);
Sunny Goyal34b65272015-03-11 16:56:52 -0700107
Sunny Goyal756adbc2015-04-16 15:20:51 -0700108 mWorkerHandler = new Handler(LauncherModel.getWorkerLooper());
Romain Guya28fd3f2010-03-15 14:44:42 -0700109 }
110
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800111 private Drawable getFullResDefaultActivityIcon() {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700112 return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon);
Michael Jurkac9a96192010-11-01 11:52:08 -0700113 }
114
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700115 private Drawable getFullResIcon(Resources resources, int iconId) {
Michael Jurka721d9722011-08-03 11:49:59 -0700116 Drawable d;
Michael Jurka4842ed02011-07-07 15:33:20 -0700117 try {
Michael Jurka721d9722011-08-03 11:49:59 -0700118 d = resources.getDrawableForDensity(iconId, mIconDpi);
Michael Jurka4842ed02011-07-07 15:33:20 -0700119 } catch (Resources.NotFoundException e) {
Michael Jurka721d9722011-08-03 11:49:59 -0700120 d = null;
Michael Jurka4842ed02011-07-07 15:33:20 -0700121 }
Michael Jurka721d9722011-08-03 11:49:59 -0700122
123 return (d != null) ? d : getFullResDefaultActivityIcon();
Michael Jurkac9a96192010-11-01 11:52:08 -0700124 }
125
Winson Chung0b9fcf52011-10-31 13:05:15 -0700126 public Drawable getFullResIcon(String packageName, int iconId) {
Michael Jurkac9a96192010-11-01 11:52:08 -0700127 Resources resources;
128 try {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700129 resources = mPackageManager.getResourcesForApplication(packageName);
130 } catch (PackageManager.NameNotFoundException e) {
131 resources = null;
132 }
133 if (resources != null) {
134 if (iconId != 0) {
135 return getFullResIcon(resources, iconId);
136 }
137 }
138 return getFullResDefaultActivityIcon();
139 }
140
Sunny Goyalffe83f12014-08-14 17:39:34 -0700141 public int getFullResIconDpi() {
142 return mIconDpi;
143 }
144
Michael Jurkadac85912012-05-18 15:04:49 -0700145 public Drawable getFullResIcon(ActivityInfo info) {
Winson Chung0b9fcf52011-10-31 13:05:15 -0700146 Resources resources;
147 try {
148 resources = mPackageManager.getResourcesForApplication(
Michael Jurkadac85912012-05-18 15:04:49 -0700149 info.applicationInfo);
Michael Jurkac9a96192010-11-01 11:52:08 -0700150 } catch (PackageManager.NameNotFoundException e) {
151 resources = null;
152 }
153 if (resources != null) {
Michael Jurkadac85912012-05-18 15:04:49 -0700154 int iconId = info.getIconResource();
Michael Jurkac9a96192010-11-01 11:52:08 -0700155 if (iconId != 0) {
156 return getFullResIcon(resources, iconId);
157 }
158 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500159
Michael Jurkac9a96192010-11-01 11:52:08 -0700160 return getFullResDefaultActivityIcon();
161 }
162
Kenny Guyed131872014-04-30 03:02:21 +0100163 private Bitmap makeDefaultIcon(UserHandleCompat user) {
164 Drawable unbadged = getFullResDefaultActivityIcon();
165 Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
Romain Guya28fd3f2010-03-15 14:44:42 -0700166 Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1),
167 Math.max(d.getIntrinsicHeight(), 1),
168 Bitmap.Config.ARGB_8888);
169 Canvas c = new Canvas(b);
170 d.setBounds(0, 0, b.getWidth(), b.getHeight());
171 d.draw(c);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700172 c.setBitmap(null);
Romain Guya28fd3f2010-03-15 14:44:42 -0700173 return b;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800174 }
175
176 /**
177 * Remove any records for the supplied ComponentName.
178 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700179 public synchronized void remove(ComponentName componentName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700180 mCache.remove(new ComponentKey(componentName, user));
Joe Onorato0589f0f2010-02-08 13:44:00 -0800181 }
182
183 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800184 * Remove any records for the supplied package name from memory.
Chris Wren6d0dde02014-02-10 12:16:54 -0500185 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800186 private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700187 HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>();
188 for (ComponentKey key: mCache.keySet()) {
Kenny Guyed131872014-04-30 03:02:21 +0100189 if (key.componentName.getPackageName().equals(packageName)
190 && key.user.equals(user)) {
191 forDeletion.add(key);
Chris Wren6d0dde02014-02-10 12:16:54 -0500192 }
193 }
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700194 for (ComponentKey condemned: forDeletion) {
Kenny Guyed131872014-04-30 03:02:21 +0100195 mCache.remove(condemned);
Chris Wren6d0dde02014-02-10 12:16:54 -0500196 }
197 }
198
199 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800200 * Updates the entries related to the given package in memory and persistent DB.
201 */
202 public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) {
203 removeIconsForPkg(packageName, user);
204 try {
205 PackageInfo info = mPackageManager.getPackageInfo(packageName,
206 PackageManager.GET_UNINSTALLED_PACKAGES);
207 long userSerial = mUserManager.getSerialNumberForUser(user);
208 for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700209 addIconToDBAndMemCache(app, info, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800210 }
211 } catch (NameNotFoundException e) {
212 Log.d(TAG, "Package not found", e);
213 return;
214 }
215 }
216
217 /**
218 * Removes the entries related to the given package in memory and persistent DB.
219 */
220 public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) {
221 removeFromMemCacheLocked(packageName, user);
222 long userSerial = mUserManager.getSerialNumberForUser(user);
223 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
224 IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?",
225 new String[] {packageName + "/%", Long.toString(userSerial)});
226 }
227
Sunny Goyal75b0f552015-05-20 21:57:06 -0700228 public void updateDbIcons() {
229 // Remove all active icon update tasks.
230 mWorkerHandler.removeCallbacksAndMessages(ICON_UPDATE_TOKEN);
231
232 mIconDb.updateSystemStateString(mContext);
233 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
234 // Query for the set of apps
235 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
236 // Fail if we don't have any apps
237 // TODO: Fix this. Only fail for the current user.
238 if (apps == null || apps.isEmpty()) {
239 return;
240 }
241
242 // Update icon cache. This happens in segments and {@link #onPackageIconsUpdated}
243 // is called by the icon cache when the job is complete.
244 updateDBIcons(user, apps);
245 }
246 }
247
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800248 /**
249 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
250 * the DB and are updated.
251 * @return The set of packages for which icons have updated.
252 */
Sunny Goyal75b0f552015-05-20 21:57:06 -0700253 private void updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800254 long userSerial = mUserManager.getSerialNumberForUser(user);
255 PackageManager pm = mContext.getPackageManager();
256 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
257 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
258 pkgInfoMap.put(info.packageName, info);
259 }
260
261 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
262 for (LauncherActivityInfoCompat app : apps) {
263 componentMap.put(app.getComponentName(), app);
264 }
265
266 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
267 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700268 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION,
269 IconDB.COLUMN_SYSTEM_STATE},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800270 IconDB.COLUMN_USER + " = ? ",
271 new String[] {Long.toString(userSerial)},
272 null, null, null);
273
274 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
275 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
276 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
277 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700278 final int systemStateIndex = c.getColumnIndex(IconDB.COLUMN_SYSTEM_STATE);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800279
280 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
Sunny Goyal75b0f552015-05-20 21:57:06 -0700281 Stack<LauncherActivityInfoCompat> appsToUpdate = new Stack<>();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800282
283 while (c.moveToNext()) {
284 String cn = c.getString(indexComponent);
285 ComponentName component = ComponentName.unflattenFromString(cn);
286 PackageInfo info = pkgInfoMap.get(component.getPackageName());
287 if (info == null) {
288 itemsToRemove.add(c.getInt(rowIndex));
289 continue;
290 }
291 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
292 // Application is not present
293 continue;
294 }
295
296 long updateTime = c.getLong(indexLastUpdate);
297 int version = c.getInt(indexVersion);
298 LauncherActivityInfoCompat app = componentMap.remove(component);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700299 if (version == info.versionCode && updateTime == info.lastUpdateTime &&
300 TextUtils.equals(mIconDb.mSystemState, c.getString(systemStateIndex))) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800301 continue;
302 }
303 if (app == null) {
304 itemsToRemove.add(c.getInt(rowIndex));
Sunny Goyal75b0f552015-05-20 21:57:06 -0700305 } else {
306 appsToUpdate.add(app);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800307 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800308 }
309 c.close();
310 if (!itemsToRemove.isEmpty()) {
311 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
312 IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )",
313 null);
314 }
315
316 // Insert remaining apps.
Sunny Goyal75b0f552015-05-20 21:57:06 -0700317 if (!componentMap.isEmpty() || !appsToUpdate.isEmpty()) {
318 Stack<LauncherActivityInfoCompat> appsToAdd = new Stack<>();
319 appsToAdd.addAll(componentMap.values());
320 new SerializedIconUpdateTask(userSerial, pkgInfoMap,
321 appsToAdd, appsToUpdate).scheduleNext();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800322 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800323 }
324
Sunny Goyald180cf72015-04-06 12:45:40 -0700325 private void addIconToDBAndMemCache(LauncherActivityInfoCompat app, PackageInfo info,
326 long userSerial) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700327 // Reuse the existing entry if it already exists in the DB. This ensures that we do not
328 // create bitmap if it was already created during loader.
329 ContentValues values = updateCacheAndGetContentValues(app, false);
Sunny Goyald180cf72015-04-06 12:45:40 -0700330 addIconToDB(values, app.getComponentName(), info, userSerial);
Sunny Goyald180cf72015-04-06 12:45:40 -0700331 }
332
333 /**
334 * Updates {@param values} to contain versoning information and adds it to the DB.
335 * @param values {@link ContentValues} containing icon & title
336 */
337 private void addIconToDB(ContentValues values, ComponentName key,
338 PackageInfo info, long userSerial) {
339 values.put(IconDB.COLUMN_COMPONENT, key.flattenToString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800340 values.put(IconDB.COLUMN_USER, userSerial);
341 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
342 values.put(IconDB.COLUMN_VERSION, info.versionCode);
343 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
344 SQLiteDatabase.CONFLICT_REPLACE);
345 }
346
Sunny Goyal9ff98082015-05-15 16:59:36 -0700347 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app,
348 boolean replaceExisting) {
349 final ComponentKey key = new ComponentKey(app.getComponentName(), app.getUser());
350 CacheEntry entry = null;
351 if (!replaceExisting) {
352 entry = mCache.get(key);
353 // We can't reuse the entry if the high-res icon is not present.
354 if (entry == null || entry.isLowResIcon || entry.icon == null) {
355 entry = null;
356 }
357 }
358 if (entry == null) {
359 entry = new CacheEntry();
360 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
361 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800362 entry.title = app.getLabel();
363 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700364 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800365
Sunny Goyal34b65272015-03-11 16:56:52 -0700366 return mIconDb.newContentValues(entry.icon, entry.title.toString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800367 }
368
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800369 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800370 * Empty out the cache.
371 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700372 public synchronized void flush() {
373 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800374 }
375
376 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700377 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
378 * @return a request ID that can be used to cancel the request.
379 */
380 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
381 Runnable request = new Runnable() {
382
383 @Override
384 public void run() {
385 if (info instanceof AppInfo) {
386 getTitleAndIcon((AppInfo) info, null, false);
387 } else if (info instanceof ShortcutInfo) {
388 ShortcutInfo st = (ShortcutInfo) info;
389 getTitleAndIcon(st,
390 st.promisedIntent != null ? st.promisedIntent : st.intent,
391 st.user, false);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700392 } else if (info instanceof PackageItemInfo) {
393 PackageItemInfo pti = (PackageItemInfo) info;
394 getTitleAndIconForApp(pti.packageName, pti.user, false, pti);
Sunny Goyal34b65272015-03-11 16:56:52 -0700395 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700396 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700397
398 @Override
399 public void run() {
400 caller.reapplyItemInfo(info);
401 }
402 });
403 }
404 };
405 mWorkerHandler.post(request);
406 return new IconLoadRequest(request, mWorkerHandler);
407 }
408
Sunny Goyal756adbc2015-04-16 15:20:51 -0700409 private Bitmap getNonNullIcon(CacheEntry entry, UserHandleCompat user) {
410 return entry.icon == null ? getDefaultIcon(user) : entry.icon;
411 }
412
Sunny Goyal34b65272015-03-11 16:56:52 -0700413 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800414 * Fill in "application" with the icon and label for "info."
415 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700416 public synchronized void getTitleAndIcon(AppInfo application,
417 LauncherActivityInfoCompat info, boolean useLowResIcon) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700418 UserHandleCompat user = info == null ? application.user : info.getUser();
419 CacheEntry entry = cacheLocked(application.componentName, info, user,
Sunny Goyal34b65272015-03-11 16:56:52 -0700420 false, useLowResIcon);
Winson Chung82b016c2015-05-08 17:00:10 -0700421 application.title = Utilities.trim(entry.title);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700422 application.iconBitmap = getNonNullIcon(entry, user);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700423 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700424 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800425 }
426
Sunny Goyal34b65272015-03-11 16:56:52 -0700427 /**
Sunny Goyal77919b92015-05-06 16:53:21 -0700428 * Updates {@param application} only if a valid entry is found.
429 */
430 public synchronized void updateTitleAndIcon(AppInfo application) {
431 CacheEntry entry = cacheLocked(application.componentName, null, application.user,
432 false, application.usingLowResIcon);
433 if (entry.icon != null && !isDefaultIcon(entry.icon, application.user)) {
Winson Chung82b016c2015-05-08 17:00:10 -0700434 application.title = Utilities.trim(entry.title);
Sunny Goyal77919b92015-05-06 16:53:21 -0700435 application.iconBitmap = entry.icon;
436 application.contentDescription = entry.contentDescription;
437 application.usingLowResIcon = entry.isLowResIcon;
438 }
439 }
440
441 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700442 * Returns a high res icon for the given intent and user
443 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700444 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
445 ComponentName component = intent.getComponent();
446 // null info means not installed, but if we have a component from the intent then
447 // we should still look in the cache for restored app icons.
448 if (component == null) {
449 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800450 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700451
452 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700453 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700454 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800455 }
456
Sunny Goyal34942622014-08-29 17:20:55 -0700457 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800458 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
459 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700460 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700461 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700462 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700463 ComponentName component = intent.getComponent();
464 // null info means not installed, but if we have a component from the intent then
465 // we should still look in the cache for restored app icons.
466 if (component == null) {
467 shortcutInfo.setIcon(getDefaultIcon(user));
468 shortcutInfo.title = "";
469 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700470 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700471 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800472 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700473 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700474 }
475 }
476
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800477 /**
478 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
479 */
480 public synchronized void getTitleAndIcon(
481 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700482 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
483 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700484 shortcutInfo.setIcon(getNonNullIcon(entry, user));
Winson Chung82b016c2015-05-08 17:00:10 -0700485 shortcutInfo.title = Utilities.trim(entry.title);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800486 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700487 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800488 }
Sunny Goyal34942622014-08-29 17:20:55 -0700489
Sunny Goyald180cf72015-04-06 12:45:40 -0700490 /**
491 * Fill in {@param appInfo} with the icon and label for {@param packageName}
492 */
493 public synchronized void getTitleAndIconForApp(
Hyunyoung Song3f471442015-04-08 19:01:34 -0700494 String packageName, UserHandleCompat user, boolean useLowResIcon,
495 PackageItemInfo infoOut) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700496 CacheEntry entry = getEntryForPackageLocked(packageName, user, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700497 infoOut.iconBitmap = getNonNullIcon(entry, user);
Winson Chung82b016c2015-05-08 17:00:10 -0700498 infoOut.title = Utilities.trim(entry.title);
Hyunyoung Song3f471442015-04-08 19:01:34 -0700499 infoOut.usingLowResIcon = entry.isLowResIcon;
500 infoOut.contentDescription = entry.contentDescription;
Sunny Goyald180cf72015-04-06 12:45:40 -0700501 }
502
Sunny Goyal736f5af2014-10-16 14:07:29 -0700503 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100504 if (!mDefaultIcons.containsKey(user)) {
505 mDefaultIcons.put(user, makeDefaultIcon(user));
506 }
507 return mDefaultIcons.get(user);
508 }
509
Kenny Guyed131872014-04-30 03:02:21 +0100510 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
511 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700512 }
513
Sunny Goyal736f5af2014-10-16 14:07:29 -0700514 /**
515 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
516 * This method is not thread safe, it must be called from a synchronized method.
517 */
Kenny Guyed131872014-04-30 03:02:21 +0100518 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700519 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700520 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100521 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700522 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800523 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100524 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500525
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800526 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700527 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800528 if (info != null) {
529 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500530 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700531 if (usePackageIcon) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700532 CacheEntry packageEntry = getEntryForPackageLocked(
533 componentName.getPackageName(), user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700534 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700535 if (DEBUG) Log.d(TAG, "using package default icon for " +
536 componentName.toShortString());
537 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700538 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800539 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700540 }
541 }
542 if (entry.icon == null) {
543 if (DEBUG) Log.d(TAG, "using default icon for " +
544 componentName.toShortString());
545 entry.icon = getDefaultIcon(user);
546 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700547 }
548 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800549
550 if (TextUtils.isEmpty(entry.title) && info != null) {
Winson Chung82b016c2015-05-08 17:00:10 -0700551 entry.title = info.getLabel();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800552 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
553 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800554 }
555 return entry;
556 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400557
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700558 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700559 * Adds a default package entry in the cache. This entry is not persisted and will be removed
560 * when the cache is flushed.
561 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700562 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700563 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800564 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700565
Sunny Goyald180cf72015-04-06 12:45:40 -0700566 CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700567 if (!TextUtils.isEmpty(title)) {
568 entry.title = title;
569 }
570 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700571 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700572 }
573 }
574
575 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700576 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700577 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyald180cf72015-04-06 12:45:40 -0700578 *
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700579 */
Sunny Goyald180cf72015-04-06 12:45:40 -0700580 private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
581 boolean useLowResIcon) {
582 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700583 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700584 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyald180cf72015-04-06 12:45:40 -0700585 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700586 entry = new CacheEntry();
587 mCache.put(cacheKey, entry);
588
Sunny Goyald180cf72015-04-06 12:45:40 -0700589 // Check the DB first.
590 if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
591 try {
592 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
593 ApplicationInfo appInfo = info.applicationInfo;
594 if (appInfo == null) {
595 throw new NameNotFoundException("ApplicationInfo is null");
596 }
597 Drawable drawable = mUserManager.getBadgedDrawableForUser(
598 appInfo.loadIcon(mPackageManager), user);
599 entry.icon = Utilities.createIconBitmap(drawable, mContext);
600 entry.title = appInfo.loadLabel(mPackageManager);
601 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
602 entry.isLowResIcon = false;
603
604 // Add the icon in the DB here, since these do not get written during
605 // package updates.
606 ContentValues values =
607 mIconDb.newContentValues(entry.icon, entry.title.toString());
608 addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
609
610 } catch (NameNotFoundException e) {
611 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
612 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700613 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700614 }
615 return entry;
616 }
617
Chris Wren6d0dde02014-02-10 12:16:54 -0500618 /**
619 * Pre-load an icon into the persistent cache.
620 *
621 * <P>Queries for a component that does not exist in the package manager
622 * will be answered by the persistent cache.
623 *
Chris Wren6d0dde02014-02-10 12:16:54 -0500624 * @param componentName the icon should be returned for this component
625 * @param icon the icon to be persisted
626 * @param dpi the native density of the icon
627 */
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800628 public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
629 long userSerial) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500630 // TODO rescale to the correct native DPI
631 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800632 PackageManager packageManager = mContext.getPackageManager();
Chris Wren6d0dde02014-02-10 12:16:54 -0500633 packageManager.getActivityIcon(componentName);
634 // component is present on the system already, do nothing
635 return;
636 } catch (PackageManager.NameNotFoundException e) {
637 // pass
638 }
639
Sunny Goyal34b65272015-03-11 16:56:52 -0700640 ContentValues values = mIconDb.newContentValues(icon, label);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800641 values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
642 values.put(IconDB.COLUMN_USER, userSerial);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800643 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
644 SQLiteDatabase.CONFLICT_REPLACE);
645 }
646
Sunny Goyal34b65272015-03-11 16:56:52 -0700647 private boolean getEntryFromDB(ComponentName component, UserHandleCompat user,
648 CacheEntry entry, boolean lowRes) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800649 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
Sunny Goyal34b65272015-03-11 16:56:52 -0700650 new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON,
651 IconDB.COLUMN_LABEL},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800652 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
653 new String[] {component.flattenToString(),
654 Long.toString(mUserManager.getSerialNumberForUser(user))},
655 null, null, null);
Chris Wren6d0dde02014-02-10 12:16:54 -0500656 try {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800657 if (c.moveToNext()) {
Sunny Goyal34b65272015-03-11 16:56:52 -0700658 entry.icon = loadIconNoResize(c, 0);
659 entry.isLowResIcon = lowRes;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800660 entry.title = c.getString(1);
661 if (entry.title == null) {
662 entry.title = "";
663 entry.contentDescription = "";
664 } else {
665 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500666 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800667 return true;
Chris Wren6d0dde02014-02-10 12:16:54 -0500668 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500669 } finally {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800670 c.close();
671 }
672 return false;
673 }
674
Sunny Goyal34b65272015-03-11 16:56:52 -0700675 public static class IconLoadRequest {
676 private final Runnable mRunnable;
677 private final Handler mHandler;
678
679 IconLoadRequest(Runnable runnable, Handler handler) {
680 mRunnable = runnable;
681 mHandler = handler;
682 }
683
684 public void cancel() {
685 mHandler.removeCallbacks(mRunnable);
686 }
687 }
688
Sunny Goyal9ff98082015-05-15 16:59:36 -0700689 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -0700690 * A runnable that updates invalid icons and adds missing icons in the DB for the provided
691 * LauncherActivityInfoCompat list. Items are updated/added one at a time, so that the
692 * worker thread doesn't get blocked.
Sunny Goyal9ff98082015-05-15 16:59:36 -0700693 */
Sunny Goyal75b0f552015-05-20 21:57:06 -0700694 private class SerializedIconUpdateTask implements Runnable {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700695 private final long mUserSerial;
696 private final HashMap<String, PackageInfo> mPkgInfoMap;
697 private final Stack<LauncherActivityInfoCompat> mAppsToAdd;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700698 private final Stack<LauncherActivityInfoCompat> mAppsToUpdate;
699 private final HashSet<String> mUpdatedPackages = new HashSet<String>();
Sunny Goyal9ff98082015-05-15 16:59:36 -0700700
Sunny Goyal75b0f552015-05-20 21:57:06 -0700701 private SerializedIconUpdateTask(long userSerial, HashMap<String, PackageInfo> pkgInfoMap,
702 Stack<LauncherActivityInfoCompat> appsToAdd,
703 Stack<LauncherActivityInfoCompat> appsToUpdate) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700704 mUserSerial = userSerial;
705 mPkgInfoMap = pkgInfoMap;
Sunny Goyal75b0f552015-05-20 21:57:06 -0700706 mAppsToAdd = appsToAdd;
707 mAppsToUpdate = appsToUpdate;
Sunny Goyal9ff98082015-05-15 16:59:36 -0700708 }
709
710 @Override
711 public void run() {
Sunny Goyal75b0f552015-05-20 21:57:06 -0700712 if (!mAppsToUpdate.isEmpty()) {
713 LauncherActivityInfoCompat app = mAppsToUpdate.pop();
714 String cn = app.getComponentName().flattenToString();
715 ContentValues values = updateCacheAndGetContentValues(app, true);
716 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
717 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
718 new String[] {cn, Long.toString(mUserSerial)});
719 mUpdatedPackages.add(app.getComponentName().getPackageName());
720
721 if (mAppsToUpdate.isEmpty() && !mUpdatedPackages.isEmpty()) {
722 // No more app to update. Notify model.
723 LauncherAppState.getInstance().getModel().onPackageIconsUpdated(
724 mUpdatedPackages, mUserManager.getUserForSerialNumber(mUserSerial));
725 }
726
727 // Let it run one more time.
728 scheduleNext();
729 } else if (!mAppsToAdd.isEmpty()) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700730 LauncherActivityInfoCompat app = mAppsToAdd.pop();
731 PackageInfo info = mPkgInfoMap.get(app.getComponentName().getPackageName());
732 if (info != null) {
733 synchronized (IconCache.this) {
734 addIconToDBAndMemCache(app, info, mUserSerial);
735 }
736 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700737
738 if (!mAppsToAdd.isEmpty()) {
739 scheduleNext();
740 }
Sunny Goyal9ff98082015-05-15 16:59:36 -0700741 }
Sunny Goyal75b0f552015-05-20 21:57:06 -0700742 }
743
744 public void scheduleNext() {
745 mWorkerHandler.postAtTime(this, ICON_UPDATE_TOKEN, SystemClock.uptimeMillis() + 1);
Sunny Goyal9ff98082015-05-15 16:59:36 -0700746 }
747 }
748
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800749 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal77919b92015-05-06 16:53:21 -0700750 private final static int DB_VERSION = 4;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800751
752 private final static String TABLE_NAME = "icons";
753 private final static String COLUMN_ROWID = "rowid";
754 private final static String COLUMN_COMPONENT = "componentName";
755 private final static String COLUMN_USER = "profileId";
756 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
757 private final static String COLUMN_VERSION = "version";
758 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700759 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800760 private final static String COLUMN_LABEL = "label";
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700761 private final static String COLUMN_SYSTEM_STATE = "system_state";
762
763 public String mSystemState;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800764
765 public IconDB(Context context) {
766 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700767 updateSystemStateString(context);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800768 }
769
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700770 public void updateSystemStateString(Context c) {
771 mSystemState = Locale.getDefault().toString() + ","
772 + c.getResources().getConfiguration().mcc;
773 }
774
775
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800776 @Override
777 public void onCreate(SQLiteDatabase db) {
778 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
779 COLUMN_COMPONENT + " TEXT NOT NULL, " +
780 COLUMN_USER + " INTEGER NOT NULL, " +
781 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
782 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
783 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700784 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800785 COLUMN_LABEL + " TEXT, " +
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700786 COLUMN_SYSTEM_STATE + " TEXT, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800787 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
788 ");");
789 }
790
791 @Override
792 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
793 if (oldVersion != newVersion) {
794 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500795 }
796 }
797
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800798 @Override
799 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
800 if (oldVersion != newVersion) {
801 clearDB(db);
802 }
Kenny Guyed131872014-04-30 03:02:21 +0100803 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500804
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800805 private void clearDB(SQLiteDatabase db) {
806 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
807 onCreate(db);
808 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700809
810 public ContentValues newContentValues(Bitmap icon, String label) {
811 ContentValues values = new ContentValues();
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700812 values.put(COLUMN_ICON, Utilities.flattenBitmap(icon));
813 values.put(COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700814 Bitmap.createScaledBitmap(icon,
815 icon.getWidth() / LOW_RES_SCALE_FACTOR,
816 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700817 values.put(COLUMN_LABEL, label);
818 values.put(COLUMN_SYSTEM_STATE, mSystemState);
Sunny Goyal34b65272015-03-11 16:56:52 -0700819 return values;
820 }
821 }
822
823 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
824 byte[] data = c.getBlob(iconIndex);
825 try {
826 return BitmapFactory.decodeByteArray(data, 0, data.length);
827 } catch (Exception e) {
828 return null;
829 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500830 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800831}