Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 18 | |
Winson Chung | d83f5f4 | 2012-02-13 14:27:42 -0800 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 20 | import android.content.ComponentName; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 21 | import android.content.ContentValues; |
Winson Chung | d83f5f4 | 2012-02-13 14:27:42 -0800 | [diff] [blame] | 22 | import android.content.Context; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 23 | import android.content.Intent; |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 24 | import android.content.pm.ActivityInfo; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 25 | import android.content.pm.ApplicationInfo; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 26 | import android.content.pm.PackageInfo; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager.NameNotFoundException; |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 29 | import android.content.res.Resources; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 30 | import android.database.Cursor; |
| 31 | import android.database.sqlite.SQLiteDatabase; |
| 32 | import android.database.sqlite.SQLiteOpenHelper; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 33 | import android.graphics.Bitmap; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 34 | import android.graphics.BitmapFactory; |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 35 | import android.graphics.Canvas; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 36 | import android.graphics.drawable.Drawable; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 37 | import android.os.Handler; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 38 | import android.text.TextUtils; |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 39 | import android.util.Log; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 40 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 41 | import com.android.launcher3.compat.LauncherActivityInfoCompat; |
| 42 | import com.android.launcher3.compat.LauncherAppsCompat; |
| 43 | import com.android.launcher3.compat.UserHandleCompat; |
| 44 | import com.android.launcher3.compat.UserManagerCompat; |
Sunny Goyal | b4cd42a | 2015-03-16 14:10:24 -0700 | [diff] [blame] | 45 | import com.android.launcher3.util.ComponentKey; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 46 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 47 | import java.util.HashMap; |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 48 | import java.util.HashSet; |
Adam Cohen | b6d33df | 2013-10-15 10:18:02 -0700 | [diff] [blame] | 49 | import java.util.Iterator; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 50 | import java.util.List; |
Adam Cohen | b6d33df | 2013-10-15 10:18:02 -0700 | [diff] [blame] | 51 | import java.util.Map.Entry; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Cache of application icons. Icons can be made from any thread. |
| 55 | */ |
| 56 | public class IconCache { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 57 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 58 | private static final String TAG = "Launcher.IconCache"; |
| 59 | |
| 60 | private static final int INITIAL_ICON_CACHE_CAPACITY = 50; |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 61 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 62 | // Empty class name is used for storing package default entry. |
| 63 | private static final String EMPTY_CLASS_NAME = "."; |
| 64 | |
Sunny Goyal | bbef77d | 2014-09-09 16:27:55 -0700 | [diff] [blame] | 65 | private static final boolean DEBUG = false; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 66 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 67 | private static final int LOW_RES_SCALE_FACTOR = 8; |
| 68 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 69 | private static class CacheEntry { |
| 70 | public Bitmap icon; |
Kenny Guy | d6fe526 | 2014-07-21 17:11:41 +0100 | [diff] [blame] | 71 | public CharSequence title; |
| 72 | public CharSequence contentDescription; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 73 | public boolean isLowResIcon; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 76 | private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = |
| 77 | new HashMap<UserHandleCompat, Bitmap>(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 78 | private final Context mContext; |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 79 | private final PackageManager mPackageManager; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 80 | private final UserManagerCompat mUserManager; |
| 81 | private final LauncherAppsCompat mLauncherApps; |
Sunny Goyal | b4cd42a | 2015-03-16 14:10:24 -0700 | [diff] [blame] | 82 | private final HashMap<ComponentKey, CacheEntry> mCache = |
| 83 | new HashMap<ComponentKey, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 84 | private final int mIconDpi; |
| 85 | private final IconDB mIconDb; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 86 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 87 | private final Handler mWorkerHandler; |
| 88 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 89 | public IconCache(Context context) { |
Winson Chung | d83f5f4 | 2012-02-13 14:27:42 -0800 | [diff] [blame] | 90 | ActivityManager activityManager = |
| 91 | (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
| 92 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 93 | mContext = context; |
| 94 | mPackageManager = context.getPackageManager(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 95 | mUserManager = UserManagerCompat.getInstance(mContext); |
| 96 | mLauncherApps = LauncherAppsCompat.getInstance(mContext); |
Winson Chung | d83f5f4 | 2012-02-13 14:27:42 -0800 | [diff] [blame] | 97 | mIconDpi = activityManager.getLauncherLargeIconDensity(); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 98 | mIconDb = new IconDB(context); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 99 | |
| 100 | mWorkerHandler = new Handler(LauncherModel.sWorkerThread.getLooper()); |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 103 | private Drawable getFullResDefaultActivityIcon() { |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 104 | return getFullResIcon(Resources.getSystem(), android.R.mipmap.sym_def_app_icon); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 107 | private Drawable getFullResIcon(Resources resources, int iconId) { |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 108 | Drawable d; |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 109 | try { |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 110 | d = resources.getDrawableForDensity(iconId, mIconDpi); |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 111 | } catch (Resources.NotFoundException e) { |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 112 | d = null; |
Michael Jurka | 4842ed0 | 2011-07-07 15:33:20 -0700 | [diff] [blame] | 113 | } |
Michael Jurka | 721d972 | 2011-08-03 11:49:59 -0700 | [diff] [blame] | 114 | |
| 115 | return (d != null) ? d : getFullResDefaultActivityIcon(); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 118 | public Drawable getFullResIcon(String packageName, int iconId) { |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 119 | Resources resources; |
| 120 | try { |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 121 | resources = mPackageManager.getResourcesForApplication(packageName); |
| 122 | } catch (PackageManager.NameNotFoundException e) { |
| 123 | resources = null; |
| 124 | } |
| 125 | if (resources != null) { |
| 126 | if (iconId != 0) { |
| 127 | return getFullResIcon(resources, iconId); |
| 128 | } |
| 129 | } |
| 130 | return getFullResDefaultActivityIcon(); |
| 131 | } |
| 132 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 133 | public int getFullResIconDpi() { |
| 134 | return mIconDpi; |
| 135 | } |
| 136 | |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 137 | public Drawable getFullResIcon(ActivityInfo info) { |
Winson Chung | 0b9fcf5 | 2011-10-31 13:05:15 -0700 | [diff] [blame] | 138 | Resources resources; |
| 139 | try { |
| 140 | resources = mPackageManager.getResourcesForApplication( |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 141 | info.applicationInfo); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 142 | } catch (PackageManager.NameNotFoundException e) { |
| 143 | resources = null; |
| 144 | } |
| 145 | if (resources != null) { |
Michael Jurka | dac8591 | 2012-05-18 15:04:49 -0700 | [diff] [blame] | 146 | int iconId = info.getIconResource(); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 147 | if (iconId != 0) { |
| 148 | return getFullResIcon(resources, iconId); |
| 149 | } |
| 150 | } |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 151 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 152 | return getFullResDefaultActivityIcon(); |
| 153 | } |
| 154 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 155 | private Bitmap makeDefaultIcon(UserHandleCompat user) { |
| 156 | Drawable unbadged = getFullResDefaultActivityIcon(); |
| 157 | Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user); |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 158 | Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1), |
| 159 | Math.max(d.getIntrinsicHeight(), 1), |
| 160 | Bitmap.Config.ARGB_8888); |
| 161 | Canvas c = new Canvas(b); |
| 162 | d.setBounds(0, 0, b.getWidth(), b.getHeight()); |
| 163 | d.draw(c); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 164 | c.setBitmap(null); |
Romain Guy | a28fd3f | 2010-03-15 14:44:42 -0700 | [diff] [blame] | 165 | return b; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Remove any records for the supplied ComponentName. |
| 170 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 171 | public synchronized void remove(ComponentName componentName, UserHandleCompat user) { |
Sunny Goyal | b4cd42a | 2015-03-16 14:10:24 -0700 | [diff] [blame] | 172 | mCache.remove(new ComponentKey(componentName, user)); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 176 | * Remove any records for the supplied package name from memory. |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 177 | */ |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 178 | private void removeFromMemCacheLocked(String packageName, UserHandleCompat user) { |
Sunny Goyal | b4cd42a | 2015-03-16 14:10:24 -0700 | [diff] [blame] | 179 | HashSet<ComponentKey> forDeletion = new HashSet<ComponentKey>(); |
| 180 | for (ComponentKey key: mCache.keySet()) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 181 | if (key.componentName.getPackageName().equals(packageName) |
| 182 | && key.user.equals(user)) { |
| 183 | forDeletion.add(key); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 184 | } |
| 185 | } |
Sunny Goyal | b4cd42a | 2015-03-16 14:10:24 -0700 | [diff] [blame] | 186 | for (ComponentKey condemned: forDeletion) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 187 | mCache.remove(condemned); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 192 | * Updates the entries related to the given package in memory and persistent DB. |
| 193 | */ |
| 194 | public synchronized void updateIconsForPkg(String packageName, UserHandleCompat user) { |
| 195 | removeIconsForPkg(packageName, user); |
| 196 | try { |
| 197 | PackageInfo info = mPackageManager.getPackageInfo(packageName, |
| 198 | PackageManager.GET_UNINSTALLED_PACKAGES); |
| 199 | long userSerial = mUserManager.getSerialNumberForUser(user); |
| 200 | for (LauncherActivityInfoCompat app : mLauncherApps.getActivityList(packageName, user)) { |
| 201 | addIconToDB(app, info, userSerial); |
| 202 | } |
| 203 | } catch (NameNotFoundException e) { |
| 204 | Log.d(TAG, "Package not found", e); |
| 205 | return; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Removes the entries related to the given package in memory and persistent DB. |
| 211 | */ |
| 212 | public synchronized void removeIconsForPkg(String packageName, UserHandleCompat user) { |
| 213 | removeFromMemCacheLocked(packageName, user); |
| 214 | long userSerial = mUserManager.getSerialNumberForUser(user); |
| 215 | mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME, |
| 216 | IconDB.COLUMN_COMPONENT + " LIKE ? AND " + IconDB.COLUMN_USER + " = ?", |
| 217 | new String[] {packageName + "/%", Long.toString(userSerial)}); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Updates the persistent DB, such that only entries corresponding to {@param apps} remain in |
| 222 | * the DB and are updated. |
| 223 | * @return The set of packages for which icons have updated. |
| 224 | */ |
| 225 | public HashSet<String> updateDBIcons(UserHandleCompat user, List<LauncherActivityInfoCompat> apps) { |
| 226 | long userSerial = mUserManager.getSerialNumberForUser(user); |
| 227 | PackageManager pm = mContext.getPackageManager(); |
| 228 | HashMap<String, PackageInfo> pkgInfoMap = new HashMap<String, PackageInfo>(); |
| 229 | for (PackageInfo info : pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) { |
| 230 | pkgInfoMap.put(info.packageName, info); |
| 231 | } |
| 232 | |
| 233 | HashMap<ComponentName, LauncherActivityInfoCompat> componentMap = new HashMap<>(); |
| 234 | for (LauncherActivityInfoCompat app : apps) { |
| 235 | componentMap.put(app.getComponentName(), app); |
| 236 | } |
| 237 | |
| 238 | Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME, |
| 239 | new String[] {IconDB.COLUMN_ROWID, IconDB.COLUMN_COMPONENT, |
| 240 | IconDB.COLUMN_LAST_UPDATED, IconDB.COLUMN_VERSION}, |
| 241 | IconDB.COLUMN_USER + " = ? ", |
| 242 | new String[] {Long.toString(userSerial)}, |
| 243 | null, null, null); |
| 244 | |
| 245 | final int indexComponent = c.getColumnIndex(IconDB.COLUMN_COMPONENT); |
| 246 | final int indexLastUpdate = c.getColumnIndex(IconDB.COLUMN_LAST_UPDATED); |
| 247 | final int indexVersion = c.getColumnIndex(IconDB.COLUMN_VERSION); |
| 248 | final int rowIndex = c.getColumnIndex(IconDB.COLUMN_ROWID); |
| 249 | |
| 250 | HashSet<Integer> itemsToRemove = new HashSet<Integer>(); |
| 251 | HashSet<String> updatedPackages = new HashSet<String>(); |
| 252 | |
| 253 | while (c.moveToNext()) { |
| 254 | String cn = c.getString(indexComponent); |
| 255 | ComponentName component = ComponentName.unflattenFromString(cn); |
| 256 | PackageInfo info = pkgInfoMap.get(component.getPackageName()); |
| 257 | if (info == null) { |
| 258 | itemsToRemove.add(c.getInt(rowIndex)); |
| 259 | continue; |
| 260 | } |
| 261 | if ((info.applicationInfo.flags & ApplicationInfo.FLAG_IS_DATA_ONLY) != 0) { |
| 262 | // Application is not present |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | long updateTime = c.getLong(indexLastUpdate); |
| 267 | int version = c.getInt(indexVersion); |
| 268 | LauncherActivityInfoCompat app = componentMap.remove(component); |
| 269 | if (version == info.versionCode && updateTime == info.lastUpdateTime) { |
| 270 | continue; |
| 271 | } |
| 272 | if (app == null) { |
| 273 | itemsToRemove.add(c.getInt(rowIndex)); |
| 274 | continue; |
| 275 | } |
| 276 | ContentValues values = updateCacheAndGetContentValues(app); |
| 277 | mIconDb.getWritableDatabase().update(IconDB.TABLE_NAME, values, |
| 278 | IconDB.COLUMN_COMPONENT + " = ?", |
| 279 | new String[] { cn }); |
| 280 | |
| 281 | updatedPackages.add(component.getPackageName()); |
| 282 | } |
| 283 | c.close(); |
| 284 | if (!itemsToRemove.isEmpty()) { |
| 285 | mIconDb.getWritableDatabase().delete(IconDB.TABLE_NAME, |
| 286 | IconDB.COLUMN_ROWID + " IN ( " + TextUtils.join(", ", itemsToRemove) +" )", |
| 287 | null); |
| 288 | } |
| 289 | |
| 290 | // Insert remaining apps. |
| 291 | for (LauncherActivityInfoCompat app : componentMap.values()) { |
| 292 | PackageInfo info = pkgInfoMap.get(app.getComponentName().getPackageName()); |
| 293 | if (info == null) { |
| 294 | continue; |
| 295 | } |
| 296 | addIconToDB(app, info, userSerial); |
| 297 | } |
| 298 | return updatedPackages; |
| 299 | } |
| 300 | |
| 301 | private void addIconToDB(LauncherActivityInfoCompat app, PackageInfo info, long userSerial) { |
| 302 | ContentValues values = updateCacheAndGetContentValues(app); |
| 303 | values.put(IconDB.COLUMN_COMPONENT, app.getComponentName().flattenToString()); |
| 304 | values.put(IconDB.COLUMN_USER, userSerial); |
| 305 | values.put(IconDB.COLUMN_LAST_UPDATED, info.lastUpdateTime); |
| 306 | values.put(IconDB.COLUMN_VERSION, info.versionCode); |
| 307 | mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values, |
| 308 | SQLiteDatabase.CONFLICT_REPLACE); |
| 309 | } |
| 310 | |
| 311 | private ContentValues updateCacheAndGetContentValues(LauncherActivityInfoCompat app) { |
| 312 | CacheEntry entry = new CacheEntry(); |
| 313 | entry.icon = Utilities.createIconBitmap(app.getBadgedIcon(mIconDpi), mContext); |
| 314 | entry.title = app.getLabel(); |
| 315 | entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser()); |
Sunny Goyal | b4cd42a | 2015-03-16 14:10:24 -0700 | [diff] [blame] | 316 | mCache.put(new ComponentKey(app.getComponentName(), app.getUser()), entry); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 317 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 318 | return mIconDb.newContentValues(entry.icon, entry.title.toString()); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | |
| 322 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 323 | * Empty out the cache. |
| 324 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 325 | public synchronized void flush() { |
| 326 | mCache.clear(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
Winson Chung | e5467dc | 2013-10-14 17:03:04 -0700 | [diff] [blame] | 330 | * Empty out the cache that aren't of the correct grid size |
| 331 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 332 | public synchronized void flushInvalidIcons(DeviceProfile grid) { |
Sunny Goyal | b4cd42a | 2015-03-16 14:10:24 -0700 | [diff] [blame] | 333 | Iterator<Entry<ComponentKey, CacheEntry>> it = mCache.entrySet().iterator(); |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 334 | while (it.hasNext()) { |
| 335 | final CacheEntry e = it.next().getValue(); |
| 336 | if ((e.icon != null) && (e.icon.getWidth() < grid.iconSizePx |
| 337 | || e.icon.getHeight() < grid.iconSizePx)) { |
| 338 | it.remove(); |
Winson Chung | e5467dc | 2013-10-14 17:03:04 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /** |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 344 | * Fetches high-res icon for the provided ItemInfo and updates the caller when done. |
| 345 | * @return a request ID that can be used to cancel the request. |
| 346 | */ |
| 347 | public IconLoadRequest updateIconInBackground(final BubbleTextView caller, final ItemInfo info) { |
| 348 | Runnable request = new Runnable() { |
| 349 | |
| 350 | @Override |
| 351 | public void run() { |
| 352 | if (info instanceof AppInfo) { |
| 353 | getTitleAndIcon((AppInfo) info, null, false); |
| 354 | } else if (info instanceof ShortcutInfo) { |
| 355 | ShortcutInfo st = (ShortcutInfo) info; |
| 356 | getTitleAndIcon(st, |
| 357 | st.promisedIntent != null ? st.promisedIntent : st.intent, |
| 358 | st.user, false); |
| 359 | } |
| 360 | caller.post(new Runnable() { |
| 361 | |
| 362 | @Override |
| 363 | public void run() { |
| 364 | caller.reapplyItemInfo(info); |
| 365 | } |
| 366 | }); |
| 367 | } |
| 368 | }; |
| 369 | mWorkerHandler.post(request); |
| 370 | return new IconLoadRequest(request, mWorkerHandler); |
| 371 | } |
| 372 | |
| 373 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 374 | * Fill in "application" with the icon and label for "info." |
| 375 | */ |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 376 | public synchronized void getTitleAndIcon(AppInfo application, |
| 377 | LauncherActivityInfoCompat info, boolean useLowResIcon) { |
| 378 | CacheEntry entry = cacheLocked(application.componentName, info, |
| 379 | info == null ? application.user : info.getUser(), |
| 380 | false, useLowResIcon); |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 381 | application.title = entry.title; |
| 382 | application.iconBitmap = entry.icon; |
| 383 | application.contentDescription = entry.contentDescription; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 384 | application.usingLowResIcon = entry.isLowResIcon; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 387 | /** |
| 388 | * Returns a high res icon for the given intent and user |
| 389 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 390 | public synchronized Bitmap getIcon(Intent intent, UserHandleCompat user) { |
| 391 | ComponentName component = intent.getComponent(); |
| 392 | // null info means not installed, but if we have a component from the intent then |
| 393 | // we should still look in the cache for restored app icons. |
| 394 | if (component == null) { |
| 395 | return getDefaultIcon(user); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 396 | } |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 397 | |
| 398 | LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 399 | CacheEntry entry = cacheLocked(component, launcherActInfo, user, true, true); |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 400 | return entry.icon; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 403 | /** |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 404 | * Fill in {@param shortcutInfo} with the icon and label for {@param intent}. If the |
| 405 | * corresponding activity is not found, it reverts to the package icon. |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 406 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 407 | public synchronized void getTitleAndIcon(ShortcutInfo shortcutInfo, Intent intent, |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 408 | UserHandleCompat user, boolean useLowResIcon) { |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 409 | ComponentName component = intent.getComponent(); |
| 410 | // null info means not installed, but if we have a component from the intent then |
| 411 | // we should still look in the cache for restored app icons. |
| 412 | if (component == null) { |
| 413 | shortcutInfo.setIcon(getDefaultIcon(user)); |
| 414 | shortcutInfo.title = ""; |
| 415 | shortcutInfo.usingFallbackIcon = true; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 416 | shortcutInfo.usingLowResIcon = false; |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 417 | } else { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 418 | LauncherActivityInfoCompat info = mLauncherApps.resolveActivity(intent, user); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 419 | getTitleAndIcon(shortcutInfo, component, info, user, true, useLowResIcon); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 423 | /** |
| 424 | * Fill in {@param shortcutInfo} with the icon and label for {@param info} |
| 425 | */ |
| 426 | public synchronized void getTitleAndIcon( |
| 427 | ShortcutInfo shortcutInfo, ComponentName component, LauncherActivityInfoCompat info, |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 428 | UserHandleCompat user, boolean usePkgIcon, boolean useLowResIcon) { |
| 429 | CacheEntry entry = cacheLocked(component, info, user, usePkgIcon, useLowResIcon); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 430 | shortcutInfo.setIcon(entry.icon); |
| 431 | shortcutInfo.title = entry.title; |
| 432 | shortcutInfo.usingFallbackIcon = isDefaultIcon(entry.icon, user); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 433 | shortcutInfo.usingLowResIcon = entry.isLowResIcon; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 434 | } |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 435 | |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 436 | public synchronized Bitmap getDefaultIcon(UserHandleCompat user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 437 | if (!mDefaultIcons.containsKey(user)) { |
| 438 | mDefaultIcons.put(user, makeDefaultIcon(user)); |
| 439 | } |
| 440 | return mDefaultIcons.get(user); |
| 441 | } |
| 442 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 443 | public boolean isDefaultIcon(Bitmap icon, UserHandleCompat user) { |
| 444 | return mDefaultIcons.get(user) == icon; |
Joe Onorato | ddc9c1f | 2010-08-30 18:30:15 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 447 | /** |
| 448 | * Retrieves the entry from the cache. If the entry is not present, it creates a new entry. |
| 449 | * This method is not thread safe, it must be called from a synchronized method. |
| 450 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 451 | private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info, |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 452 | UserHandleCompat user, boolean usePackageIcon, boolean useLowResIcon) { |
Sunny Goyal | b4cd42a | 2015-03-16 14:10:24 -0700 | [diff] [blame] | 453 | ComponentKey cacheKey = new ComponentKey(componentName, user); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 454 | CacheEntry entry = mCache.get(cacheKey); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 455 | if (entry == null || (entry.isLowResIcon && !useLowResIcon)) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 456 | entry = new CacheEntry(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 457 | mCache.put(cacheKey, entry); |
Joe Onorato | 84f6a8d | 2010-02-12 17:53:35 -0500 | [diff] [blame] | 458 | |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 459 | // Check the DB first. |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 460 | if (!getEntryFromDB(componentName, user, entry, useLowResIcon)) { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 461 | if (info != null) { |
| 462 | entry.icon = Utilities.createIconBitmap(info.getBadgedIcon(mIconDpi), mContext); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 463 | } else { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 464 | if (usePackageIcon) { |
| 465 | CacheEntry packageEntry = getEntryForPackage( |
| 466 | componentName.getPackageName(), user); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 467 | if (packageEntry != null) { |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 468 | if (DEBUG) Log.d(TAG, "using package default icon for " + |
| 469 | componentName.toShortString()); |
| 470 | entry.icon = packageEntry.icon; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 471 | entry.title = packageEntry.title; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 472 | entry.contentDescription = packageEntry.contentDescription; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | if (entry.icon == null) { |
| 476 | if (DEBUG) Log.d(TAG, "using default icon for " + |
| 477 | componentName.toShortString()); |
| 478 | entry.icon = getDefaultIcon(user); |
| 479 | } |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 482 | |
| 483 | if (TextUtils.isEmpty(entry.title) && info != null) { |
| 484 | entry.title = info.getLabel().toString(); |
| 485 | entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user); |
| 486 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 487 | } |
| 488 | return entry; |
| 489 | } |
Daniel Sandler | 4e1cd23 | 2011-05-12 00:06:32 -0400 | [diff] [blame] | 490 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 491 | /** |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 492 | * Adds a default package entry in the cache. This entry is not persisted and will be removed |
| 493 | * when the cache is flushed. |
| 494 | */ |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 495 | public synchronized void cachePackageInstallInfo(String packageName, UserHandleCompat user, |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 496 | Bitmap icon, CharSequence title) { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 497 | removeFromMemCacheLocked(packageName, user); |
Sunny Goyal | a22666f | 2014-09-18 13:25:15 -0700 | [diff] [blame] | 498 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 499 | CacheEntry entry = getEntryForPackage(packageName, user); |
| 500 | if (!TextUtils.isEmpty(title)) { |
| 501 | entry.title = title; |
| 502 | } |
| 503 | if (icon != null) { |
Sunny Goyal | 2fce90c | 2014-10-07 12:01:58 -0700 | [diff] [blame] | 504 | entry.icon = Utilities.createIconBitmap(icon, mContext); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
| 508 | /** |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 509 | * Gets an entry for the package, which can be used as a fallback entry for various components. |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 510 | * This method is not thread safe, it must be called from a synchronized method. |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 511 | */ |
| 512 | private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) { |
Sunny Goyal | 736f5af | 2014-10-16 14:07:29 -0700 | [diff] [blame] | 513 | ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);; |
Sunny Goyal | b4cd42a | 2015-03-16 14:10:24 -0700 | [diff] [blame] | 514 | ComponentKey cacheKey = new ComponentKey(cn, user); |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 515 | CacheEntry entry = mCache.get(cacheKey); |
| 516 | if (entry == null) { |
| 517 | entry = new CacheEntry(); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 518 | entry.title = ""; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 519 | entry.contentDescription = ""; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 520 | mCache.put(cacheKey, entry); |
| 521 | |
| 522 | try { |
| 523 | ApplicationInfo info = mPackageManager.getApplicationInfo(packageName, 0); |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 524 | entry.icon = Utilities.createIconBitmap(info.loadIcon(mPackageManager), mContext); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 525 | entry.title = info.loadLabel(mPackageManager); |
| 526 | entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user); |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 527 | } catch (NameNotFoundException e) { |
| 528 | if (DEBUG) Log.d(TAG, "Application not installed " + packageName); |
| 529 | } |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 530 | } |
| 531 | return entry; |
| 532 | } |
| 533 | |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 534 | /** |
| 535 | * Pre-load an icon into the persistent cache. |
| 536 | * |
| 537 | * <P>Queries for a component that does not exist in the package manager |
| 538 | * will be answered by the persistent cache. |
| 539 | * |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 540 | * @param componentName the icon should be returned for this component |
| 541 | * @param icon the icon to be persisted |
| 542 | * @param dpi the native density of the icon |
| 543 | */ |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 544 | public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label, |
| 545 | long userSerial) { |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 546 | // TODO rescale to the correct native DPI |
| 547 | try { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 548 | PackageManager packageManager = mContext.getPackageManager(); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 549 | packageManager.getActivityIcon(componentName); |
| 550 | // component is present on the system already, do nothing |
| 551 | return; |
| 552 | } catch (PackageManager.NameNotFoundException e) { |
| 553 | // pass |
| 554 | } |
| 555 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 556 | ContentValues values = mIconDb.newContentValues(icon, label); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 557 | values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString()); |
| 558 | values.put(IconDB.COLUMN_USER, userSerial); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 559 | mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values, |
| 560 | SQLiteDatabase.CONFLICT_REPLACE); |
| 561 | } |
| 562 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 563 | private boolean getEntryFromDB(ComponentName component, UserHandleCompat user, |
| 564 | CacheEntry entry, boolean lowRes) { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 565 | Cursor c = mIconDb.getReadableDatabase().query(IconDB.TABLE_NAME, |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 566 | new String[] {lowRes ? IconDB.COLUMN_ICON_LOW_RES : IconDB.COLUMN_ICON, |
| 567 | IconDB.COLUMN_LABEL}, |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 568 | IconDB.COLUMN_COMPONENT + " = ? AND " + IconDB.COLUMN_USER + " = ?", |
| 569 | new String[] {component.flattenToString(), |
| 570 | Long.toString(mUserManager.getSerialNumberForUser(user))}, |
| 571 | null, null, null); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 572 | try { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 573 | if (c.moveToNext()) { |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 574 | entry.icon = loadIconNoResize(c, 0); |
| 575 | entry.isLowResIcon = lowRes; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 576 | entry.title = c.getString(1); |
| 577 | if (entry.title == null) { |
| 578 | entry.title = ""; |
| 579 | entry.contentDescription = ""; |
| 580 | } else { |
| 581 | entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 582 | } |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 583 | return true; |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 584 | } |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 585 | } finally { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 586 | c.close(); |
| 587 | } |
| 588 | return false; |
| 589 | } |
| 590 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 591 | public static class IconLoadRequest { |
| 592 | private final Runnable mRunnable; |
| 593 | private final Handler mHandler; |
| 594 | |
| 595 | IconLoadRequest(Runnable runnable, Handler handler) { |
| 596 | mRunnable = runnable; |
| 597 | mHandler = handler; |
| 598 | } |
| 599 | |
| 600 | public void cancel() { |
| 601 | mHandler.removeCallbacks(mRunnable); |
| 602 | } |
| 603 | } |
| 604 | |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 605 | private static final class IconDB extends SQLiteOpenHelper { |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 606 | private final static int DB_VERSION = 2; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 607 | |
| 608 | private final static String TABLE_NAME = "icons"; |
| 609 | private final static String COLUMN_ROWID = "rowid"; |
| 610 | private final static String COLUMN_COMPONENT = "componentName"; |
| 611 | private final static String COLUMN_USER = "profileId"; |
| 612 | private final static String COLUMN_LAST_UPDATED = "lastUpdated"; |
| 613 | private final static String COLUMN_VERSION = "version"; |
| 614 | private final static String COLUMN_ICON = "icon"; |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 615 | private final static String COLUMN_ICON_LOW_RES = "icon_low_res"; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 616 | private final static String COLUMN_LABEL = "label"; |
| 617 | |
| 618 | public IconDB(Context context) { |
| 619 | super(context, LauncherFiles.APP_ICONS_DB, null, DB_VERSION); |
| 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public void onCreate(SQLiteDatabase db) { |
| 624 | db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + |
| 625 | COLUMN_COMPONENT + " TEXT NOT NULL, " + |
| 626 | COLUMN_USER + " INTEGER NOT NULL, " + |
| 627 | COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " + |
| 628 | COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " + |
| 629 | COLUMN_ICON + " BLOB, " + |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 630 | COLUMN_ICON_LOW_RES + " BLOB, " + |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 631 | COLUMN_LABEL + " TEXT, " + |
| 632 | "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ") " + |
| 633 | ");"); |
| 634 | } |
| 635 | |
| 636 | @Override |
| 637 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 638 | if (oldVersion != newVersion) { |
| 639 | clearDB(db); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 643 | @Override |
| 644 | public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 645 | if (oldVersion != newVersion) { |
| 646 | clearDB(db); |
| 647 | } |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 648 | } |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 649 | |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 650 | private void clearDB(SQLiteDatabase db) { |
| 651 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); |
| 652 | onCreate(db); |
| 653 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame^] | 654 | |
| 655 | public ContentValues newContentValues(Bitmap icon, String label) { |
| 656 | ContentValues values = new ContentValues(); |
| 657 | values.put(IconDB.COLUMN_ICON, ItemInfo.flattenBitmap(icon)); |
| 658 | values.put(IconDB.COLUMN_ICON_LOW_RES, ItemInfo.flattenBitmap( |
| 659 | Bitmap.createScaledBitmap(icon, |
| 660 | icon.getWidth() / LOW_RES_SCALE_FACTOR, |
| 661 | icon.getHeight() / LOW_RES_SCALE_FACTOR, true))); |
| 662 | values.put(IconDB.COLUMN_LABEL, label); |
| 663 | return values; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | private static Bitmap loadIconNoResize(Cursor c, int iconIndex) { |
| 668 | byte[] data = c.getBlob(iconIndex); |
| 669 | try { |
| 670 | return BitmapFactory.decodeByteArray(data, 0, data.length); |
| 671 | } catch (Exception e) { |
| 672 | return null; |
| 673 | } |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 674 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 675 | } |