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