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