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