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