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