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