blob: 1fac5a1c2357f6f06fb2e6560486902ab771bb1e [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 Goyal34942622014-08-29 17:20:55 -070038import android.text.TextUtils;
Chris Wren6d0dde02014-02-10 12:16:54 -050039import android.util.Log;
Joe Onorato0589f0f2010-02-08 13:44:00 -080040
Kenny Guyed131872014-04-30 03:02:21 +010041import com.android.launcher3.compat.LauncherActivityInfoCompat;
42import com.android.launcher3.compat.LauncherAppsCompat;
43import com.android.launcher3.compat.UserHandleCompat;
44import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070045import com.android.launcher3.util.ComponentKey;
Adam Cohen091440a2015-03-18 14:16:05 -070046import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -070047import com.android.launcher3.widget.PackageItemInfo;
Kenny Guyed131872014-04-30 03:02:21 +010048
Sunny Goyal9ff98082015-05-15 16:59:36 -070049import java.util.Collection;
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
Adam Cohen091440a2015-03-18 14:16:05 -070074 @Thunk static class CacheEntry {
Joe Onorato0589f0f2010-02-08 13:44:00 -080075 public Bitmap icon;
Kenny Guyd6fe5262014-07-21 17:11:41 +010076 public CharSequence title;
77 public CharSequence contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -070078 public boolean isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -080079 }
80
Sunny Goyal8758ea02015-03-18 10:07:49 -070081 private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
82 private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
83
Daniel Sandlercc8befa2013-06-11 14:45:48 -040084 private final Context mContext;
Romain Guya28fd3f2010-03-15 14:44:42 -070085 private final PackageManager mPackageManager;
Kenny Guyed131872014-04-30 03:02:21 +010086 private final UserManagerCompat mUserManager;
87 private final LauncherAppsCompat mLauncherApps;
Sunny Goyalb4cd42a2015-03-16 14:10:24 -070088 private final HashMap<ComponentKey, CacheEntry> mCache =
89 new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY);
Sunny Goyal4fbc3822015-02-18 16:46:50 -080090 private final int mIconDpi;
91 private final IconDB mIconDb;
Joe Onorato0589f0f2010-02-08 13:44:00 -080092
Sunny Goyal34b65272015-03-11 16:56:52 -070093 private final Handler mWorkerHandler;
94
Daniel Sandlercc8befa2013-06-11 14:45:48 -040095 public IconCache(Context context) {
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
226 /**
227 * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in
228 * the DB and are updated.
229 * @return The set of packages for which icons have updated.
230 */
231 public HashSet<String> updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) {
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700232 mIconDb.updateSystemStateString(mContext);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800233 long userSerial = mUserManager.getSerialNumberForUser(user);
234 PackageManager pm = mContext.getPackageManager();
235 HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>();
236 for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
237 pkgInfoMap.put(info.packageName, info);
238 }
239
240 HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>();
241 for (LauncherActivityInfoCompat app : apps) {
242 componentMap.put(app.getComponentName(), app);
243 }
244
245 Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME,
246 new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT,
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700247 IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION,
248 IconDB.COLUMN_SYSTEM_STATE},
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800249 IconDB.COLUMN_USER + " = ? ",
250 new String[] {Long.toString(userSerial)},
251 null, null, null);
252
253 final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT);
254 final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED);
255 final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION);
256 final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700257 final int systemStateIndex = c.getColumnIndex(IconDB.COLUMN_SYSTEM_STATE);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800258
259 HashSet<Integer> itemsToRemove = new HashSet<Integer>();
260 HashSet<String> updatedPackages = new HashSet<String>();
261
262 while (c.moveToNext()) {
263 String cn = c.getString(indexComponent);
264 ComponentName component = ComponentName.unflattenFromString(cn);
265 PackageInfo info = pkgInfoMap.get(component.getPackageName());
266 if (info == null) {
267 itemsToRemove.add(c.getInt(rowIndex));
268 continue;
269 }
270 if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) {
271 // Application is not present
272 continue;
273 }
274
275 long updateTime = c.getLong(indexLastUpdate);
276 int version = c.getInt(indexVersion);
277 LauncherActivityInfoCompat app = componentMap.remove(component);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700278 if (version == info.versionCode && updateTime == info.lastUpdateTime &&
279 TextUtils.equals(mIconDb.mSystemState, c.getString(systemStateIndex))) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800280 continue;
281 }
282 if (app == null) {
283 itemsToRemove.add(c.getInt(rowIndex));
284 continue;
285 }
Sunny Goyal9ff98082015-05-15 16:59:36 -0700286 ContentValues values = updateCacheAndGetContentValues(app, true);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800287 mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values,
Sunny Goyalece6c8b2015-04-13 11:47:00 -0700288 IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?",
289 new String[] {cn, Long.toString(userSerial)});
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800290
291 updatedPackages.add(component.getPackageName());
292 }
293 c.close();
294 if (!itemsToRemove.isEmpty()) {
295 mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME,
296 IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )",
297 null);
298 }
299
300 // Insert remaining apps.
Sunny Goyal9ff98082015-05-15 16:59:36 -0700301 if (!componentMap.isEmpty()) {
302 mWorkerHandler.post(new SerializedIconAdditionTask(userSerial, pkgInfoMap,
303 componentMap.values()));
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800304 }
305 return updatedPackages;
306 }
307
Sunny Goyald180cf72015-04-06 12:45:40 -0700308 private void addIconToDBAndMemCache(LauncherActivityInfoCompat app, PackageInfo info,
309 long userSerial) {
Sunny Goyal9ff98082015-05-15 16:59:36 -0700310 // Reuse the existing entry if it already exists in the DB. This ensures that we do not
311 // create bitmap if it was already created during loader.
312 ContentValues values = updateCacheAndGetContentValues(app, false);
Sunny Goyald180cf72015-04-06 12:45:40 -0700313 addIconToDB(values, app.getComponentName(), info, userSerial);
Sunny Goyald180cf72015-04-06 12:45:40 -0700314 }
315
316 /**
317 * Updates {@param values} to contain versoning information and adds it to the DB.
318 * @param values {@link ContentValues} containing icon & title
319 */
320 private void addIconToDB(ContentValues values, ComponentName key,
321 PackageInfo info, long userSerial) {
322 values.put(IconDB.COLUMN_COMPONENT, key.flattenToString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800323 values.put(IconDB.COLUMN_USER, userSerial);
324 values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime);
325 values.put(IconDB.COLUMN_VERSION, info.versionCode);
326 mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
327 SQLiteDatabase.CONFLICT_REPLACE);
328 }
329
Sunny Goyal9ff98082015-05-15 16:59:36 -0700330 private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app,
331 boolean replaceExisting) {
332 final ComponentKey key = new ComponentKey(app.getComponentName(), app.getUser());
333 CacheEntry entry = null;
334 if (!replaceExisting) {
335 entry = mCache.get(key);
336 // We can't reuse the entry if the high-res icon is not present.
337 if (entry == null || entry.isLowResIcon || entry.icon == null) {
338 entry = null;
339 }
340 }
341 if (entry == null) {
342 entry = new CacheEntry();
343 entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext);
344 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800345 entry.title = app.getLabel();
346 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700347 mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800348
Sunny Goyal34b65272015-03-11 16:56:52 -0700349 return mIconDb.newContentValues(entry.icon, entry.title.toString());
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800350 }
351
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800352 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800353 * Empty out the cache.
354 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700355 public synchronized void flush() {
356 mCache.clear();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800357 }
358
359 /**
Winson Chunge5467dc2013-10-14 17:03:04 -0700360 * Empty out the cache that aren't of the correct grid size
361 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700362 public synchronized void flushInvalidIcons(DeviceProfile grid) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700363 Iterator<Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator();
Sunny Goyal736f5af2014-10-16 14:07:29 -0700364 while (it.hasNext()) {
365 final CacheEntry e = it.next().getValue();
366 if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx
367 || e.icon.getHeight() < grid.iconSizePx)) {
368 it.remove();
Winson Chunge5467dc2013-10-14 17:03:04 -0700369 }
370 }
371 }
372
373 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700374 * Fetches high-res icon for the provided ItemInfo and updates the caller when done.
375 * @return a request ID that can be used to cancel the request.
376 */
377 public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) {
378 Runnable request = new Runnable() {
379
380 @Override
381 public void run() {
382 if (info instanceof AppInfo) {
383 getTitleAndIcon((AppInfo) info, null, false);
384 } else if (info instanceof ShortcutInfo) {
385 ShortcutInfo st = (ShortcutInfo) info;
386 getTitleAndIcon(st,
387 st.promisedIntent != null ? st.promisedIntent : st.intent,
388 st.user, false);
Sunny Goyal0e08f162015-05-12 11:32:39 -0700389 } else if (info instanceof PackageItemInfo) {
390 PackageItemInfo pti = (PackageItemInfo) info;
391 getTitleAndIconForApp(pti.packageName, pti.user, false, pti);
Sunny Goyal34b65272015-03-11 16:56:52 -0700392 }
Sunny Goyal8758ea02015-03-18 10:07:49 -0700393 mMainThreadExecutor.execute(new Runnable() {
Sunny Goyal34b65272015-03-11 16:56:52 -0700394
395 @Override
396 public void run() {
397 caller.reapplyItemInfo(info);
398 }
399 });
400 }
401 };
402 mWorkerHandler.post(request);
403 return new IconLoadRequest(request, mWorkerHandler);
404 }
405
Sunny Goyal756adbc2015-04-16 15:20:51 -0700406 private Bitmap getNonNullIcon(CacheEntry entry, UserHandleCompat user) {
407 return entry.icon == null ? getDefaultIcon(user) : entry.icon;
408 }
409
Sunny Goyal34b65272015-03-11 16:56:52 -0700410 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -0800411 * Fill in "application" with the icon and label for "info."
412 */
Sunny Goyal34b65272015-03-11 16:56:52 -0700413 public synchronized void getTitleAndIcon(AppInfo application,
414 LauncherActivityInfoCompat info, boolean useLowResIcon) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700415 UserHandleCompat user = info == null ? application.user : info.getUser();
416 CacheEntry entry = cacheLocked(application.componentName, info, user,
Sunny Goyal34b65272015-03-11 16:56:52 -0700417 false, useLowResIcon);
Winson Chung82b016c2015-05-08 17:00:10 -0700418 application.title = Utilities.trim(entry.title);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700419 application.iconBitmap = getNonNullIcon(entry, user);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700420 application.contentDescription = entry.contentDescription;
Sunny Goyal34b65272015-03-11 16:56:52 -0700421 application.usingLowResIcon = entry.isLowResIcon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800422 }
423
Sunny Goyal34b65272015-03-11 16:56:52 -0700424 /**
Sunny Goyal77919b92015-05-06 16:53:21 -0700425 * Updates {@param application} only if a valid entry is found.
426 */
427 public synchronized void updateTitleAndIcon(AppInfo application) {
428 CacheEntry entry = cacheLocked(application.componentName, null, application.user,
429 false, application.usingLowResIcon);
430 if (entry.icon != null && !isDefaultIcon(entry.icon, application.user)) {
Winson Chung82b016c2015-05-08 17:00:10 -0700431 application.title = Utilities.trim(entry.title);
Sunny Goyal77919b92015-05-06 16:53:21 -0700432 application.iconBitmap = entry.icon;
433 application.contentDescription = entry.contentDescription;
434 application.usingLowResIcon = entry.isLowResIcon;
435 }
436 }
437
438 /**
Sunny Goyal34b65272015-03-11 16:56:52 -0700439 * Returns a high res icon for the given intent and user
440 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700441 public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) {
442 ComponentName component = intent.getComponent();
443 // null info means not installed, but if we have a component from the intent then
444 // we should still look in the cache for restored app icons.
445 if (component == null) {
446 return getDefaultIcon(user);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800447 }
Sunny Goyal736f5af2014-10-16 14:07:29 -0700448
449 LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700450 CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true);
Sunny Goyal736f5af2014-10-16 14:07:29 -0700451 return entry.icon;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800452 }
453
Sunny Goyal34942622014-08-29 17:20:55 -0700454 /**
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800455 * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the
456 * corresponding activity is not found, it reverts to the package icon.
Sunny Goyal34942622014-08-29 17:20:55 -0700457 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700458 public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent,
Sunny Goyal34b65272015-03-11 16:56:52 -0700459 UserHandleCompat user, boolean useLowResIcon) {
Sunny Goyal736f5af2014-10-16 14:07:29 -0700460 ComponentName component = intent.getComponent();
461 // null info means not installed, but if we have a component from the intent then
462 // we should still look in the cache for restored app icons.
463 if (component == null) {
464 shortcutInfo.setIcon(getDefaultIcon(user));
465 shortcutInfo.title = "";
466 shortcutInfo.usingFallbackIcon = true;
Sunny Goyal34b65272015-03-11 16:56:52 -0700467 shortcutInfo.usingLowResIcon = false;
Sunny Goyal736f5af2014-10-16 14:07:29 -0700468 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800469 LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700470 getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon);
Sunny Goyal34942622014-08-29 17:20:55 -0700471 }
472 }
473
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800474 /**
475 * Fill in {@param shortcutInfo} with the icon and label for {@param info}
476 */
477 public synchronized void getTitleAndIcon(
478 ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700479 UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) {
480 CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700481 shortcutInfo.setIcon(getNonNullIcon(entry, user));
Winson Chung82b016c2015-05-08 17:00:10 -0700482 shortcutInfo.title = Utilities.trim(entry.title);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800483 shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user);
Sunny Goyal34b65272015-03-11 16:56:52 -0700484 shortcutInfo.usingLowResIcon = entry.isLowResIcon;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800485 }
Sunny Goyal34942622014-08-29 17:20:55 -0700486
Sunny Goyald180cf72015-04-06 12:45:40 -0700487 /**
488 * Fill in {@param appInfo} with the icon and label for {@param packageName}
489 */
490 public synchronized void getTitleAndIconForApp(
Hyunyoung Song3f471442015-04-08 19:01:34 -0700491 String packageName, UserHandleCompat user, boolean useLowResIcon,
492 PackageItemInfo infoOut) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700493 CacheEntry entry = getEntryForPackageLocked(packageName, user, useLowResIcon);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700494 infoOut.iconBitmap = getNonNullIcon(entry, user);
Winson Chung82b016c2015-05-08 17:00:10 -0700495 infoOut.title = Utilities.trim(entry.title);
Hyunyoung Song3f471442015-04-08 19:01:34 -0700496 infoOut.usingLowResIcon = entry.isLowResIcon;
497 infoOut.contentDescription = entry.contentDescription;
Sunny Goyald180cf72015-04-06 12:45:40 -0700498 }
499
Sunny Goyal736f5af2014-10-16 14:07:29 -0700500 public synchronized Bitmap getDefaultIcon(UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100501 if (!mDefaultIcons.containsKey(user)) {
502 mDefaultIcons.put(user, makeDefaultIcon(user));
503 }
504 return mDefaultIcons.get(user);
505 }
506
Kenny Guyed131872014-04-30 03:02:21 +0100507 public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) {
508 return mDefaultIcons.get(user) == icon;
Joe Onoratoddc9c1f2010-08-30 18:30:15 -0700509 }
510
Sunny Goyal736f5af2014-10-16 14:07:29 -0700511 /**
512 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
513 * This method is not thread safe, it must be called from a synchronized method.
514 */
Kenny Guyed131872014-04-30 03:02:21 +0100515 private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
Sunny Goyal34b65272015-03-11 16:56:52 -0700516 UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) {
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700517 ComponentKey cacheKey = new ComponentKey(componentName, user);
Kenny Guyed131872014-04-30 03:02:21 +0100518 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyal34b65272015-03-11 16:56:52 -0700519 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800520 entry = new CacheEntry();
Kenny Guyed131872014-04-30 03:02:21 +0100521 mCache.put(cacheKey, entry);
Joe Onorato84f6a8d2010-02-12 17:53:35 -0500522
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800523 // Check the DB first.
Sunny Goyal34b65272015-03-11 16:56:52 -0700524 if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800525 if (info != null) {
526 entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext);
Chris Wren6d0dde02014-02-10 12:16:54 -0500527 } else {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700528 if (usePackageIcon) {
Sunny Goyald180cf72015-04-06 12:45:40 -0700529 CacheEntry packageEntry = getEntryForPackageLocked(
530 componentName.getPackageName(), user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700531 if (packageEntry != null) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700532 if (DEBUG) Log.d(TAG, "using package default icon for " +
533 componentName.toShortString());
534 entry.icon = packageEntry.icon;
Sunny Goyal34942622014-08-29 17:20:55 -0700535 entry.title = packageEntry.title;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800536 entry.contentDescription = packageEntry.contentDescription;
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700537 }
538 }
539 if (entry.icon == null) {
540 if (DEBUG) Log.d(TAG, "using default icon for " +
541 componentName.toShortString());
542 entry.icon = getDefaultIcon(user);
543 }
Winson Chungc3eecff2011-07-11 17:44:15 -0700544 }
545 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800546
547 if (TextUtils.isEmpty(entry.title) && info != null) {
Winson Chung82b016c2015-05-08 17:00:10 -0700548 entry.title = info.getLabel();
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800549 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
550 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800551 }
552 return entry;
553 }
Daniel Sandler4e1cd232011-05-12 00:06:32 -0400554
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700555 /**
Sunny Goyal34942622014-08-29 17:20:55 -0700556 * Adds a default package entry in the cache. This entry is not persisted and will be removed
557 * when the cache is flushed.
558 */
Sunny Goyal736f5af2014-10-16 14:07:29 -0700559 public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user,
Sunny Goyal34942622014-08-29 17:20:55 -0700560 Bitmap icon, CharSequence title) {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800561 removeFromMemCacheLocked(packageName, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700562
Sunny Goyald180cf72015-04-06 12:45:40 -0700563 CacheEntry entry = getEntryForPackageLocked(packageName, user, false);
Sunny Goyal34942622014-08-29 17:20:55 -0700564 if (!TextUtils.isEmpty(title)) {
565 entry.title = title;
566 }
567 if (icon != null) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700568 entry.icon = Utilities.createIconBitmap(icon, mContext);
Sunny Goyal34942622014-08-29 17:20:55 -0700569 }
570 }
571
572 /**
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700573 * Gets an entry for the package, which can be used as a fallback entry for various components.
Sunny Goyal736f5af2014-10-16 14:07:29 -0700574 * This method is not thread safe, it must be called from a synchronized method.
Sunny Goyald180cf72015-04-06 12:45:40 -0700575 *
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700576 */
Sunny Goyald180cf72015-04-06 12:45:40 -0700577 private CacheEntry getEntryForPackageLocked(String packageName, UserHandleCompat user,
578 boolean useLowResIcon) {
579 ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);
Sunny Goyalb4cd42a2015-03-16 14:10:24 -0700580 ComponentKey cacheKey = new ComponentKey(cn, user);
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700581 CacheEntry entry = mCache.get(cacheKey);
Sunny Goyald180cf72015-04-06 12:45:40 -0700582 if (entry == null || (entry.isLowResIcon && !useLowResIcon)) {
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700583 entry = new CacheEntry();
584 mCache.put(cacheKey, entry);
585
Sunny Goyald180cf72015-04-06 12:45:40 -0700586 // Check the DB first.
587 if (!getEntryFromDB(cn, user, entry, useLowResIcon)) {
588 try {
589 PackageInfo info = mPackageManager.getPackageInfo(packageName, 0);
590 ApplicationInfo appInfo = info.applicationInfo;
591 if (appInfo == null) {
592 throw new NameNotFoundException("ApplicationInfo is null");
593 }
594 Drawable drawable = mUserManager.getBadgedDrawableForUser(
595 appInfo.loadIcon(mPackageManager), user);
596 entry.icon = Utilities.createIconBitmap(drawable, mContext);
597 entry.title = appInfo.loadLabel(mPackageManager);
598 entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
599 entry.isLowResIcon = false;
600
601 // Add the icon in the DB here, since these do not get written during
602 // package updates.
603 ContentValues values =
604 mIconDb.newContentValues(entry.icon, entry.title.toString());
605 addIconToDB(values, cn, info, mUserManager.getSerialNumberForUser(user));
606
607 } catch (NameNotFoundException e) {
608 if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
609 }
Sunny Goyal0fc1be12014-08-11 17:05:23 -0700610 }
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 /**
687 * A runnable that adds icons in the DB for the provided LauncherActivityInfoCompat list.
688 * Items are added one at a time, to that the worker thread does not get blocked.
689 */
690 private class SerializedIconAdditionTask implements Runnable {
691 private final long mUserSerial;
692 private final HashMap<String, PackageInfo> mPkgInfoMap;
693 private final Stack<LauncherActivityInfoCompat> mAppsToAdd;
694
695 private SerializedIconAdditionTask(long userSerial, HashMap<String, PackageInfo> pkgInfoMap,
696 Collection<LauncherActivityInfoCompat> appsToAdd) {
697 mUserSerial = userSerial;
698 mPkgInfoMap = pkgInfoMap;
699 mAppsToAdd = new Stack<LauncherActivityInfoCompat>();
700 mAppsToAdd.addAll(appsToAdd);
701 }
702
703 @Override
704 public void run() {
705 if (!mAppsToAdd.isEmpty()) {
706 LauncherActivityInfoCompat app = mAppsToAdd.pop();
707 PackageInfo info = mPkgInfoMap.get(app.getComponentName().getPackageName());
708 if (info != null) {
709 synchronized (IconCache.this) {
710 addIconToDBAndMemCache(app, info, mUserSerial);
711 }
712 }
713 }
714 if (!mAppsToAdd.isEmpty()) {
715 mWorkerHandler.post(this);
716 }
717 }
718 }
719
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800720 private static final class IconDB extends SQLiteOpenHelper {
Sunny Goyal77919b92015-05-06 16:53:21 -0700721 private final static int DB_VERSION = 4;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800722
723 private final static String TABLE_NAME = "icons";
724 private final static String COLUMN_ROWID = "rowid";
725 private final static String COLUMN_COMPONENT = "componentName";
726 private final static String COLUMN_USER = "profileId";
727 private final static String COLUMN_LAST_UPDATED = "lastUpdated";
728 private final static String COLUMN_VERSION = "version";
729 private final static String COLUMN_ICON = "icon";
Sunny Goyal34b65272015-03-11 16:56:52 -0700730 private final static String COLUMN_ICON_LOW_RES = "icon_low_res";
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800731 private final static String COLUMN_LABEL = "label";
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700732 private final static String COLUMN_SYSTEM_STATE = "system_state";
733
734 public String mSystemState;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800735
736 public IconDB(Context context) {
737 super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION);
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700738 updateSystemStateString(context);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800739 }
740
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700741 public void updateSystemStateString(Context c) {
742 mSystemState = Locale.getDefault().toString() + ","
743 + c.getResources().getConfiguration().mcc;
744 }
745
746
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800747 @Override
748 public void onCreate(SQLiteDatabase db) {
749 db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
750 COLUMN_COMPONENT + " TEXT NOT NULL, " +
751 COLUMN_USER + " INTEGER NOT NULL, " +
752 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
753 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
754 COLUMN_ICON + " BLOB, " +
Sunny Goyal34b65272015-03-11 16:56:52 -0700755 COLUMN_ICON_LOW_RES + " BLOB, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800756 COLUMN_LABEL + " TEXT, " +
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700757 COLUMN_SYSTEM_STATE + " TEXT, " +
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800758 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " +
759 ");");
760 }
761
762 @Override
763 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
764 if (oldVersion != newVersion) {
765 clearDB(db);
Chris Wren6d0dde02014-02-10 12:16:54 -0500766 }
767 }
768
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800769 @Override
770 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
771 if (oldVersion != newVersion) {
772 clearDB(db);
773 }
Kenny Guyed131872014-04-30 03:02:21 +0100774 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500775
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800776 private void clearDB(SQLiteDatabase db) {
777 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
778 onCreate(db);
779 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700780
781 public ContentValues newContentValues(Bitmap icon, String label) {
782 ContentValues values = new ContentValues();
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700783 values.put(COLUMN_ICON, Utilities.flattenBitmap(icon));
784 values.put(COLUMN_ICON_LOW_RES, Utilities.flattenBitmap(
Sunny Goyal34b65272015-03-11 16:56:52 -0700785 Bitmap.createScaledBitmap(icon,
786 icon.getWidth() / LOW_RES_SCALE_FACTOR,
787 icon.getHeight() / LOW_RES_SCALE_FACTOR, true)));
Sunny Goyal0c9a3542015-04-01 16:04:21 -0700788 values.put(COLUMN_LABEL, label);
789 values.put(COLUMN_SYSTEM_STATE, mSystemState);
Sunny Goyal34b65272015-03-11 16:56:52 -0700790 return values;
791 }
792 }
793
794 private static Bitmap loadIconNoResize(Cursor c, int iconIndex) {
795 byte[] data = c.getBlob(iconIndex);
796 try {
797 return BitmapFactory.decodeByteArray(data, 0, data.length);
798 } catch (Exception e) {
799 return null;
800 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500801 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800802}