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