Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 1 | package com.android.launcher3; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 2 | |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 3 | import android.content.ComponentName; |
| 4 | import android.content.ContentValues; |
| 5 | import android.content.Context; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 6 | import android.content.pm.PackageInfo; |
Sunny Goyal | 4ddc401 | 2016-03-10 12:02:29 -0800 | [diff] [blame] | 7 | import android.content.pm.PackageManager; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 8 | import android.content.pm.PackageManager.NameNotFoundException; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 9 | import android.content.res.Resources; |
| 10 | import android.database.Cursor; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 11 | import android.database.SQLException; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 12 | import android.database.sqlite.SQLiteDatabase; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 13 | import android.graphics.Bitmap; |
| 14 | import android.graphics.Bitmap.Config; |
| 15 | import android.graphics.BitmapFactory; |
| 16 | import android.graphics.Canvas; |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 17 | import android.graphics.Color; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 18 | import android.graphics.Paint; |
| 19 | import android.graphics.PorterDuff; |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 20 | import android.graphics.PorterDuffXfermode; |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 21 | import android.graphics.Rect; |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 22 | import android.graphics.RectF; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
| 24 | import android.os.AsyncTask; |
Sunny Goyal | c65a008 | 2017-03-08 14:49:55 -0800 | [diff] [blame^] | 25 | import android.os.CancellationSignal; |
Winson Chung | 05304db | 2015-05-18 16:53:20 -0700 | [diff] [blame] | 26 | import android.os.Handler; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 27 | import android.os.UserHandle; |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 28 | import android.support.v4.graphics.ColorUtils; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 29 | import android.util.Log; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 30 | import android.util.LongSparseArray; |
Sunny Goyal | 383c507 | 2015-06-12 21:18:53 -0700 | [diff] [blame] | 31 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 32 | import com.android.launcher3.compat.AppWidgetManagerCompat; |
Sunny Goyal | 782f0c9 | 2017-01-19 10:27:54 -0800 | [diff] [blame] | 33 | import com.android.launcher3.compat.ShortcutConfigActivityInfo; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 34 | import com.android.launcher3.compat.UserManagerCompat; |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 35 | import com.android.launcher3.graphics.LauncherIcons; |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 36 | import com.android.launcher3.graphics.ShadowGenerator; |
Sunny Goyal | 4ddc401 | 2016-03-10 12:02:29 -0800 | [diff] [blame] | 37 | import com.android.launcher3.model.WidgetItem; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 38 | import com.android.launcher3.util.ComponentKey; |
Sunny Goyal | 6388b2c | 2016-04-15 18:03:27 -0700 | [diff] [blame] | 39 | import com.android.launcher3.util.Preconditions; |
Sunny Goyal | 6f70936 | 2015-12-17 17:09:36 -0800 | [diff] [blame] | 40 | import com.android.launcher3.util.SQLiteCacheHelper; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 41 | import com.android.launcher3.util.Thunk; |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 42 | import com.android.launcher3.widget.WidgetCell; |
Hyunyoung Song | 8821ca9 | 2015-05-04 16:28:20 -0700 | [diff] [blame] | 43 | |
| 44 | import java.util.ArrayList; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 45 | import java.util.Collections; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 46 | import java.util.HashMap; |
| 47 | import java.util.HashSet; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 48 | import java.util.Set; |
| 49 | import java.util.WeakHashMap; |
Adrian Roos | 65d60e2 | 2014-04-15 21:07:49 +0200 | [diff] [blame] | 50 | import java.util.concurrent.Callable; |
| 51 | import java.util.concurrent.ExecutionException; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 52 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 53 | public class WidgetPreviewLoader { |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 54 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 55 | private static final String TAG = "WidgetPreviewLoader"; |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 56 | private static final boolean DEBUG = false; |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 57 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 58 | private final HashMap<String, long[]> mPackageVersions = new HashMap<>(); |
Hyunyoung Song | 559d90d | 2015-04-28 15:06:45 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Weak reference objects, do not prevent their referents from being made finalizable, |
| 62 | * finalized, and then reclaimed. |
Hyunyoung Song | e98f4a4 | 2015-06-16 10:45:24 -0700 | [diff] [blame] | 63 | * Note: synchronized block used for this variable is expensive and the block should always |
| 64 | * be posted to a background thread. |
Hyunyoung Song | 559d90d | 2015-04-28 15:06:45 -0700 | [diff] [blame] | 65 | */ |
Sunny Goyal | b4cbea4 | 2015-06-16 15:10:36 -0700 | [diff] [blame] | 66 | @Thunk final Set<Bitmap> mUnusedBitmaps = |
Hyunyoung Song | 559d90d | 2015-04-28 15:06:45 -0700 | [diff] [blame] | 67 | Collections.newSetFromMap(new WeakHashMap<Bitmap, Boolean>()); |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 68 | |
| 69 | private final Context mContext; |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 70 | private final IconCache mIconCache; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 71 | private final UserManagerCompat mUserManager; |
Hyunyoung Song | 3e840f4 | 2016-03-01 11:57:44 -0800 | [diff] [blame] | 72 | private final AppWidgetManagerCompat mWidgetManager; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 73 | private final CacheDb mDb; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 74 | |
Adrian Roos | 65d60e2 | 2014-04-15 21:07:49 +0200 | [diff] [blame] | 75 | private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor(); |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 76 | @Thunk final Handler mWorkerHandler; |
Adrian Roos | 65d60e2 | 2014-04-15 21:07:49 +0200 | [diff] [blame] | 77 | |
Sunny Goyal | 383c507 | 2015-06-12 21:18:53 -0700 | [diff] [blame] | 78 | public WidgetPreviewLoader(Context context, IconCache iconCache) { |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 79 | mContext = context; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 80 | mIconCache = iconCache; |
Hyunyoung Song | 3e840f4 | 2016-03-01 11:57:44 -0800 | [diff] [blame] | 81 | mWidgetManager = AppWidgetManagerCompat.getInstance(context); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 82 | mUserManager = UserManagerCompat.getInstance(context); |
| 83 | mDb = new CacheDb(context); |
Winson Chung | 05304db | 2015-05-18 16:53:20 -0700 | [diff] [blame] | 84 | mWorkerHandler = new Handler(LauncherModel.getWorkerLooper()); |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 85 | } |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 86 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Generates the widget preview on {@link AsyncTask#THREAD_POOL_EXECUTOR}. Must be |
| 89 | * called on UI thread |
| 90 | * |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 91 | * @return a request id which can be used to cancel the request. |
| 92 | */ |
Sunny Goyal | c65a008 | 2017-03-08 14:49:55 -0800 | [diff] [blame^] | 93 | public CancellationSignal getPreview(WidgetItem item, int previewWidth, |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 94 | int previewHeight, WidgetCell caller) { |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 95 | String size = previewWidth + "x" + previewHeight; |
Sunny Goyal | 4ddc401 | 2016-03-10 12:02:29 -0800 | [diff] [blame] | 96 | WidgetCacheKey key = new WidgetCacheKey(item.componentName, item.user, size); |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 97 | |
Sunny Goyal | 4ddc401 | 2016-03-10 12:02:29 -0800 | [diff] [blame] | 98 | PreviewLoadTask task = new PreviewLoadTask(key, item, previewWidth, previewHeight, caller); |
Sunny Goyal | 8ac727b | 2015-09-23 15:38:09 -0700 | [diff] [blame] | 99 | task.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR); |
Sunny Goyal | c65a008 | 2017-03-08 14:49:55 -0800 | [diff] [blame^] | 100 | |
| 101 | CancellationSignal signal = new CancellationSignal(); |
| 102 | signal.setOnCancelListener(task); |
| 103 | return signal; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 106 | /** |
| 107 | * The DB holds the generated previews for various components. Previews can also have different |
| 108 | * sizes (landscape vs portrait). |
| 109 | */ |
Sunny Goyal | 6f70936 | 2015-12-17 17:09:36 -0800 | [diff] [blame] | 110 | private static class CacheDb extends SQLiteCacheHelper { |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 111 | private static final int DB_VERSION = 6; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 112 | |
| 113 | private static final String TABLE_NAME = "shortcut_and_widget_previews"; |
| 114 | private static final String COLUMN_COMPONENT = "componentName"; |
| 115 | private static final String COLUMN_USER = "profileId"; |
| 116 | private static final String COLUMN_SIZE = "size"; |
| 117 | private static final String COLUMN_PACKAGE = "packageName"; |
| 118 | private static final String COLUMN_LAST_UPDATED = "lastUpdated"; |
| 119 | private static final String COLUMN_VERSION = "version"; |
| 120 | private static final String COLUMN_PREVIEW_BITMAP = "preview_bitmap"; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 121 | |
Michael Jurka | d9cb4a1 | 2013-03-19 12:01:06 +0100 | [diff] [blame] | 122 | public CacheDb(Context context) { |
Sunny Goyal | 6f70936 | 2015-12-17 17:09:36 -0800 | [diff] [blame] | 123 | super(context, LauncherFiles.WIDGET_PREVIEWS_DB, DB_VERSION, TABLE_NAME); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
Sunny Goyal | 6f70936 | 2015-12-17 17:09:36 -0800 | [diff] [blame] | 127 | public void onCreateTable(SQLiteDatabase database) { |
Michael Jurka | 32b7a09 | 2013-02-07 20:06:49 +0100 | [diff] [blame] | 128 | database.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 129 | COLUMN_COMPONENT + " TEXT NOT NULL, " + |
| 130 | COLUMN_USER + " INTEGER NOT NULL, " + |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 131 | COLUMN_SIZE + " TEXT NOT NULL, " + |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 132 | COLUMN_PACKAGE + " TEXT NOT NULL, " + |
| 133 | COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " + |
| 134 | COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " + |
| 135 | COLUMN_PREVIEW_BITMAP + " BLOB, " + |
| 136 | "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ", " + COLUMN_SIZE + ") " + |
Michael Jurka | 32b7a09 | 2013-02-07 20:06:49 +0100 | [diff] [blame] | 137 | ");"); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 138 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 141 | @Thunk void writeToDb(WidgetCacheKey key, long[] versions, Bitmap preview) { |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 142 | ContentValues values = new ContentValues(); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 143 | values.put(CacheDb.COLUMN_COMPONENT, key.componentName.flattenToShortString()); |
| 144 | values.put(CacheDb.COLUMN_USER, mUserManager.getSerialNumberForUser(key.user)); |
| 145 | values.put(CacheDb.COLUMN_SIZE, key.size); |
| 146 | values.put(CacheDb.COLUMN_PACKAGE, key.componentName.getPackageName()); |
| 147 | values.put(CacheDb.COLUMN_VERSION, versions[0]); |
| 148 | values.put(CacheDb.COLUMN_LAST_UPDATED, versions[1]); |
| 149 | values.put(CacheDb.COLUMN_PREVIEW_BITMAP, Utilities.flattenBitmap(preview)); |
Sunny Goyal | 6f70936 | 2015-12-17 17:09:36 -0800 | [diff] [blame] | 150 | mDb.insertOrReplace(values); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 153 | public void removePackage(String packageName, UserHandle user) { |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 154 | removePackage(packageName, user, mUserManager.getSerialNumberForUser(user)); |
| 155 | } |
| 156 | |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 157 | private void removePackage(String packageName, UserHandle user, long userSerial) { |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 158 | synchronized(mPackageVersions) { |
| 159 | mPackageVersions.remove(packageName); |
| 160 | } |
| 161 | |
Sunny Goyal | 6f70936 | 2015-12-17 17:09:36 -0800 | [diff] [blame] | 162 | mDb.delete( |
| 163 | CacheDb.COLUMN_PACKAGE + " = ? AND " + CacheDb.COLUMN_USER + " = ?", |
| 164 | new String[]{packageName, Long.toString(userSerial)}); |
Michael Jurka | 8ff02ca | 2013-11-01 14:19:27 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Updates the persistent DB: |
| 169 | * 1. Any preview generated for an old package version is removed |
| 170 | * 2. Any preview for an absent package is removed |
| 171 | * This ensures that we remove entries for packages which changed while the launcher was dead. |
| 172 | */ |
Sunny Goyal | 4ddc401 | 2016-03-10 12:02:29 -0800 | [diff] [blame] | 173 | public void removeObsoletePreviews(ArrayList<? extends ComponentKey> list) { |
Sunny Goyal | 6388b2c | 2016-04-15 18:03:27 -0700 | [diff] [blame] | 174 | Preconditions.assertWorkerThread(); |
Hyunyoung Song | 8821ca9 | 2015-05-04 16:28:20 -0700 | [diff] [blame] | 175 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 176 | LongSparseArray<HashSet<String>> validPackages = new LongSparseArray<>(); |
| 177 | |
Sunny Goyal | 4ddc401 | 2016-03-10 12:02:29 -0800 | [diff] [blame] | 178 | for (ComponentKey key : list) { |
| 179 | final long userId = mUserManager.getSerialNumberForUser(key.user); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 180 | HashSet<String> packages = validPackages.get(userId); |
| 181 | if (packages == null) { |
| 182 | packages = new HashSet<>(); |
| 183 | validPackages.put(userId, packages); |
| 184 | } |
Sunny Goyal | 4ddc401 | 2016-03-10 12:02:29 -0800 | [diff] [blame] | 185 | packages.add(key.componentName.getPackageName()); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 186 | } |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 187 | |
| 188 | LongSparseArray<HashSet<String>> packagesToDelete = new LongSparseArray<>(); |
| 189 | Cursor c = null; |
| 190 | try { |
Sunny Goyal | 6f70936 | 2015-12-17 17:09:36 -0800 | [diff] [blame] | 191 | c = mDb.query( |
| 192 | new String[]{CacheDb.COLUMN_USER, CacheDb.COLUMN_PACKAGE, |
| 193 | CacheDb.COLUMN_LAST_UPDATED, CacheDb.COLUMN_VERSION}, |
| 194 | null, null); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 195 | while (c.moveToNext()) { |
| 196 | long userId = c.getLong(0); |
| 197 | String pkg = c.getString(1); |
| 198 | long lastUpdated = c.getLong(2); |
| 199 | long version = c.getLong(3); |
| 200 | |
| 201 | HashSet<String> packages = validPackages.get(userId); |
| 202 | if (packages != null && packages.contains(pkg)) { |
| 203 | long[] versions = getPackageVersion(pkg); |
| 204 | if (versions[0] == version && versions[1] == lastUpdated) { |
| 205 | // Every thing checks out |
| 206 | continue; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // We need to delete this package. |
| 211 | packages = packagesToDelete.get(userId); |
| 212 | if (packages == null) { |
| 213 | packages = new HashSet<>(); |
| 214 | packagesToDelete.put(userId, packages); |
| 215 | } |
| 216 | packages.add(pkg); |
| 217 | } |
| 218 | |
| 219 | for (int i = 0; i < packagesToDelete.size(); i++) { |
| 220 | long userId = packagesToDelete.keyAt(i); |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 221 | UserHandle user = mUserManager.getUserForSerialNumber(userId); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 222 | for (String pkg : packagesToDelete.valueAt(i)) { |
| 223 | removePackage(pkg, user, userId); |
| 224 | } |
| 225 | } |
| 226 | } catch (SQLException e) { |
Sunny Goyal | 6f70936 | 2015-12-17 17:09:36 -0800 | [diff] [blame] | 227 | Log.e(TAG, "Error updating widget previews", e); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 228 | } finally { |
| 229 | if (c != null) { |
| 230 | c.close(); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Winson Chung | 05304db | 2015-05-18 16:53:20 -0700 | [diff] [blame] | 235 | /** |
| 236 | * Reads the preview bitmap from the DB or null if the preview is not in the DB. |
| 237 | */ |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 238 | @Thunk Bitmap readFromDb(WidgetCacheKey key, Bitmap recycle, PreviewLoadTask loadTask) { |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 239 | Cursor cursor = null; |
| 240 | try { |
Sunny Goyal | 6f70936 | 2015-12-17 17:09:36 -0800 | [diff] [blame] | 241 | cursor = mDb.query( |
| 242 | new String[]{CacheDb.COLUMN_PREVIEW_BITMAP}, |
| 243 | CacheDb.COLUMN_COMPONENT + " = ? AND " + CacheDb.COLUMN_USER + " = ? AND " |
| 244 | + CacheDb.COLUMN_SIZE + " = ?", |
| 245 | new String[]{ |
Sunny Goyal | 3ef8691 | 2016-10-27 11:40:06 -0700 | [diff] [blame] | 246 | key.componentName.flattenToShortString(), |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 247 | Long.toString(mUserManager.getSerialNumberForUser(key.user)), |
| 248 | key.size |
Sunny Goyal | 6f70936 | 2015-12-17 17:09:36 -0800 | [diff] [blame] | 249 | }); |
Winson Chung | 05304db | 2015-05-18 16:53:20 -0700 | [diff] [blame] | 250 | // If cancelled, skip getting the blob and decoding it into a bitmap |
| 251 | if (loadTask.isCancelled()) { |
| 252 | return null; |
| 253 | } |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 254 | if (cursor.moveToNext()) { |
| 255 | byte[] blob = cursor.getBlob(0); |
| 256 | BitmapFactory.Options opts = new BitmapFactory.Options(); |
| 257 | opts.inBitmap = recycle; |
Michael Jurka | 6e27f64 | 2013-12-10 13:40:30 +0100 | [diff] [blame] | 258 | try { |
Winson Chung | 05304db | 2015-05-18 16:53:20 -0700 | [diff] [blame] | 259 | if (!loadTask.isCancelled()) { |
| 260 | return BitmapFactory.decodeByteArray(blob, 0, blob.length, opts); |
| 261 | } |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 262 | } catch (Exception e) { |
| 263 | return null; |
Michael Jurka | 6e27f64 | 2013-12-10 13:40:30 +0100 | [diff] [blame] | 264 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 265 | } |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 266 | } catch (SQLException e) { |
| 267 | Log.w(TAG, "Error loading preview from DB", e); |
| 268 | } finally { |
| 269 | if (cursor != null) { |
| 270 | cursor.close(); |
| 271 | } |
| 272 | } |
| 273 | return null; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 276 | private Bitmap generatePreview(BaseActivity launcher, WidgetItem item, Bitmap recycle, |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 277 | int previewWidth, int previewHeight) { |
Sunny Goyal | 4ddc401 | 2016-03-10 12:02:29 -0800 | [diff] [blame] | 278 | if (item.widgetInfo != null) { |
| 279 | return generateWidgetPreview(launcher, item.widgetInfo, |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 280 | previewWidth, recycle, null); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 281 | } else { |
Sunny Goyal | 4ddc401 | 2016-03-10 12:02:29 -0800 | [diff] [blame] | 282 | return generateShortcutPreview(launcher, item.activityInfo, |
| 283 | previewWidth, previewHeight, recycle); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Hyunyoung Song | 3e840f4 | 2016-03-01 11:57:44 -0800 | [diff] [blame] | 287 | /** |
| 288 | * Generates the widget preview from either the {@link AppWidgetManagerCompat} or cache |
| 289 | * and add badge at the bottom right corner. |
| 290 | * |
| 291 | * @param launcher |
| 292 | * @param info information about the widget |
| 293 | * @param maxPreviewWidth width of the preview on either workspace or tray |
| 294 | * @param preview bitmap that can be recycled |
| 295 | * @param preScaledWidthOut return the width of the returned bitmap |
| 296 | * @return |
| 297 | */ |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 298 | public Bitmap generateWidgetPreview(BaseActivity launcher, LauncherAppWidgetProviderInfo info, |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 299 | int maxPreviewWidth, Bitmap preview, int[] preScaledWidthOut) { |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 300 | // Load the preview image if possible |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 301 | if (maxPreviewWidth < 0) maxPreviewWidth = Integer.MAX_VALUE; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 302 | |
| 303 | Drawable drawable = null; |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 304 | if (info.previewImage != 0) { |
Sunny Goyal | 2570d10 | 2016-10-27 11:38:45 -0700 | [diff] [blame] | 305 | try { |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 306 | drawable = info.loadPreviewImage(mContext, 0); |
Sunny Goyal | 2570d10 | 2016-10-27 11:38:45 -0700 | [diff] [blame] | 307 | } catch (OutOfMemoryError e) { |
| 308 | Log.w(TAG, "Error loading widget preview for: " + info.provider, e); |
| 309 | // During OutOfMemoryError, the previous heap stack is not affected. Catching |
| 310 | // an OOM error here should be safe & not affect other parts of launcher. |
| 311 | drawable = null; |
| 312 | } |
Adrian Roos | fa9ffc2 | 2014-05-12 15:59:59 +0200 | [diff] [blame] | 313 | if (drawable != null) { |
| 314 | drawable = mutateOnMainThread(drawable); |
| 315 | } else { |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 316 | Log.w(TAG, "Can't load widget preview drawable 0x" + |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 317 | Integer.toHexString(info.previewImage) + " for provider: " + info.provider); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 321 | final boolean widgetPreviewExists = (drawable != null); |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 322 | final int spanX = info.spanX; |
| 323 | final int spanY = info.spanY; |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 324 | |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 325 | int previewWidth; |
| 326 | int previewHeight; |
Hyunyoung Song | 3e840f4 | 2016-03-01 11:57:44 -0800 | [diff] [blame] | 327 | |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 328 | if (widgetPreviewExists) { |
| 329 | previewWidth = drawable.getIntrinsicWidth(); |
| 330 | previewHeight = drawable.getIntrinsicHeight(); |
| 331 | } else { |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 332 | DeviceProfile dp = launcher.getDeviceProfile(); |
| 333 | int tileSize = Math.min(dp.cellWidthPx, dp.cellHeightPx); |
| 334 | previewWidth = tileSize * spanX; |
| 335 | previewHeight = tileSize * spanY; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // Scale to fit width only - let the widget preview be clipped in the |
| 339 | // vertical dimension |
| 340 | float scale = 1f; |
| 341 | if (preScaledWidthOut != null) { |
| 342 | preScaledWidthOut[0] = previewWidth; |
| 343 | } |
| 344 | if (previewWidth > maxPreviewWidth) { |
Sunny Goyal | a2441e8 | 2017-01-14 13:40:15 -0800 | [diff] [blame] | 345 | scale = maxPreviewWidth / (float) (previewWidth); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 346 | } |
| 347 | if (scale != 1f) { |
| 348 | previewWidth = (int) (scale * previewWidth); |
| 349 | previewHeight = (int) (scale * previewHeight); |
| 350 | } |
| 351 | |
| 352 | // If a bitmap is passed in, we use it; otherwise, we create a bitmap of the right size |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 353 | final Canvas c = new Canvas(); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 354 | if (preview == null) { |
| 355 | preview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888); |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 356 | c.setBitmap(preview); |
| 357 | } else { |
Sunny Goyal | a2441e8 | 2017-01-14 13:40:15 -0800 | [diff] [blame] | 358 | // We use the preview bitmap height to determine where the badge will be drawn in the |
| 359 | // UI. If its larger than what we need, resize the preview bitmap so that there are |
| 360 | // no transparent pixels between the preview and the badge. |
| 361 | if (preview.getHeight() > previewHeight) { |
| 362 | preview.reconfigure(preview.getWidth(), previewHeight, preview.getConfig()); |
| 363 | } |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 364 | // Reusing bitmap. Clear it. |
| 365 | c.setBitmap(preview); |
| 366 | c.drawColor(0, PorterDuff.Mode.CLEAR); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | // Draw the scaled preview into the final bitmap |
Hyunyoung Song | b9f932e | 2015-07-30 15:04:59 -0700 | [diff] [blame] | 370 | int x = (preview.getWidth() - previewWidth) / 2; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 371 | if (widgetPreviewExists) { |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 372 | drawable.setBounds(x, 0, x + previewWidth, previewHeight); |
| 373 | drawable.draw(c); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 374 | } else { |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 375 | final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 376 | RectF boxRect = drawBoxWithShadow(c, p, previewWidth, previewHeight); |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 377 | |
| 378 | // Draw horizontal and vertical lines to represent individual columns. |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 379 | p.setStyle(Paint.Style.STROKE); |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 380 | p.setStrokeWidth(mContext.getResources() |
| 381 | .getDimension(R.dimen.widget_preview_cell_divider_width)); |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 382 | p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); |
| 383 | |
| 384 | float t = boxRect.left; |
| 385 | float tileSize = boxRect.width() / spanX; |
| 386 | for (int i = 1; i < spanX; i++) { |
| 387 | t += tileSize; |
| 388 | c.drawLine(t, 0, t, previewHeight, p); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 389 | } |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 390 | |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 391 | t = boxRect.top; |
| 392 | tileSize = boxRect.height() / spanY; |
| 393 | for (int i = 1; i < spanY; i++) { |
| 394 | t += tileSize; |
| 395 | c.drawLine(0, t, previewWidth, t, p); |
| 396 | } |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 397 | |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 398 | // Draw icon in the center. |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 399 | try { |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 400 | Drawable icon = info.getIcon(launcher, mIconCache); |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 401 | if (icon != null) { |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 402 | int appIconSize = launcher.getDeviceProfile().iconSizePx; |
| 403 | int iconSize = (int) Math.min(appIconSize * scale, |
| 404 | Math.min(boxRect.width(), boxRect.height())); |
| 405 | |
Sunny Goyal | 1ba7e36 | 2015-10-26 10:42:12 -0700 | [diff] [blame] | 406 | icon = mutateOnMainThread(icon); |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 407 | int hoffset = (previewWidth - iconSize) / 2; |
| 408 | int yoffset = (previewHeight - iconSize) / 2; |
| 409 | icon.setBounds(hoffset, yoffset, hoffset + iconSize, yoffset + iconSize); |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 410 | icon.draw(c); |
| 411 | } |
Sunny Goyal | 92bbfa1 | 2017-02-07 15:06:32 -0800 | [diff] [blame] | 412 | } catch (Resources.NotFoundException e) { } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 413 | c.setBitmap(null); |
| 414 | } |
Sunny Goyal | a2441e8 | 2017-01-14 13:40:15 -0800 | [diff] [blame] | 415 | return preview; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 418 | private RectF drawBoxWithShadow(Canvas c, Paint p, int width, int height) { |
| 419 | Resources res = mContext.getResources(); |
| 420 | float shadowBlur = res.getDimension(R.dimen.widget_preview_shadow_blur); |
| 421 | float keyShadowDistance = res.getDimension(R.dimen.widget_preview_key_shadow_distance); |
| 422 | float corner = res.getDimension(R.dimen.widget_preview_corner_radius); |
| 423 | |
| 424 | RectF bounds = new RectF(shadowBlur, shadowBlur, |
| 425 | width - shadowBlur, height - shadowBlur - keyShadowDistance); |
| 426 | p.setColor(Color.WHITE); |
| 427 | |
| 428 | // Key shadow |
| 429 | p.setShadowLayer(shadowBlur, 0, keyShadowDistance, |
| 430 | ShadowGenerator.KEY_SHADOW_ALPHA << 24); |
| 431 | c.drawRoundRect(bounds, corner, corner, p); |
| 432 | |
| 433 | // Ambient shadow |
| 434 | p.setShadowLayer(shadowBlur, 0, 0, |
| 435 | ColorUtils.setAlphaComponent(Color.BLACK, ShadowGenerator.AMBIENT_SHADOW_ALPHA)); |
| 436 | c.drawRoundRect(bounds, corner, corner, p); |
| 437 | |
| 438 | p.clearShadowLayer(); |
| 439 | return bounds; |
| 440 | } |
| 441 | |
Sunny Goyal | 782f0c9 | 2017-01-19 10:27:54 -0800 | [diff] [blame] | 442 | private Bitmap generateShortcutPreview(BaseActivity launcher, ShortcutConfigActivityInfo info, |
| 443 | int maxWidth, int maxHeight, Bitmap preview) { |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 444 | int iconSize = launcher.getDeviceProfile().iconSizePx; |
| 445 | int padding = launcher.getResources() |
| 446 | .getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding); |
| 447 | |
| 448 | int size = iconSize + 2 * padding; |
| 449 | if (maxHeight < size || maxWidth < size) { |
| 450 | throw new RuntimeException("Max size is too small for preview"); |
| 451 | } |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 452 | final Canvas c = new Canvas(); |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 453 | if (preview == null || preview.getWidth() < size || preview.getHeight() < size) { |
| 454 | preview = Bitmap.createBitmap(size, size, Config.ARGB_8888); |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 455 | c.setBitmap(preview); |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 456 | } else { |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 457 | if (preview.getWidth() > size || preview.getHeight() > size) { |
| 458 | preview.reconfigure(size, size, preview.getConfig()); |
| 459 | } |
| 460 | |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 461 | // Reusing bitmap. Clear it. |
| 462 | c.setBitmap(preview); |
| 463 | c.drawColor(0, PorterDuff.Mode.CLEAR); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 464 | } |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 465 | Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); |
| 466 | RectF boxRect = drawBoxWithShadow(c, p, size, size); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 467 | |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 468 | Bitmap icon = LauncherIcons.createScaledBitmapWithoutShadow( |
| 469 | mutateOnMainThread(info.getFullResIcon(mIconCache)), mContext); |
| 470 | Rect src = new Rect(0, 0, icon.getWidth(), icon.getHeight()); |
Sunny Goyal | 4cad753 | 2015-03-18 15:56:30 -0700 | [diff] [blame] | 471 | |
Sunny Goyal | 7d87cb1 | 2017-02-13 10:44:43 -0800 | [diff] [blame] | 472 | boxRect.set(0, 0, iconSize, iconSize); |
| 473 | boxRect.offset(padding, padding); |
| 474 | c.drawBitmap(icon, src, boxRect, p); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 475 | c.setBitmap(null); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 476 | return preview; |
| 477 | } |
| 478 | |
Adrian Roos | 65d60e2 | 2014-04-15 21:07:49 +0200 | [diff] [blame] | 479 | private Drawable mutateOnMainThread(final Drawable drawable) { |
| 480 | try { |
| 481 | return mMainThreadExecutor.submit(new Callable<Drawable>() { |
| 482 | @Override |
| 483 | public Drawable call() throws Exception { |
| 484 | return drawable.mutate(); |
| 485 | } |
| 486 | }).get(); |
| 487 | } catch (InterruptedException e) { |
| 488 | Thread.currentThread().interrupt(); |
| 489 | throw new RuntimeException(e); |
| 490 | } catch (ExecutionException e) { |
| 491 | throw new RuntimeException(e); |
| 492 | } |
| 493 | } |
Adrian Roos | 1f375ab | 2014-04-28 18:26:38 +0200 | [diff] [blame] | 494 | |
Adrian Roos | 1f375ab | 2014-04-28 18:26:38 +0200 | [diff] [blame] | 495 | /** |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 496 | * @return an array of containing versionCode and lastUpdatedTime for the package. |
Adrian Roos | 1f375ab | 2014-04-28 18:26:38 +0200 | [diff] [blame] | 497 | */ |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 498 | @Thunk long[] getPackageVersion(String packageName) { |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 499 | synchronized (mPackageVersions) { |
| 500 | long[] versions = mPackageVersions.get(packageName); |
| 501 | if (versions == null) { |
| 502 | versions = new long[2]; |
Adrian Roos | 1f375ab | 2014-04-28 18:26:38 +0200 | [diff] [blame] | 503 | try { |
Sunny Goyal | 4ddc401 | 2016-03-10 12:02:29 -0800 | [diff] [blame] | 504 | PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, |
| 505 | PackageManager.GET_UNINSTALLED_PACKAGES); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 506 | versions[0] = info.versionCode; |
| 507 | versions[1] = info.lastUpdateTime; |
| 508 | } catch (NameNotFoundException e) { |
| 509 | Log.e(TAG, "PackageInfo not found", e); |
| 510 | } |
| 511 | mPackageVersions.put(packageName, versions); |
| 512 | } |
| 513 | return versions; |
| 514 | } |
| 515 | } |
| 516 | |
Sunny Goyal | c65a008 | 2017-03-08 14:49:55 -0800 | [diff] [blame^] | 517 | public class PreviewLoadTask extends AsyncTask<Void, Void, Bitmap> |
| 518 | implements CancellationSignal.OnCancelListener { |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 519 | @Thunk final WidgetCacheKey mKey; |
Sunny Goyal | 4ddc401 | 2016-03-10 12:02:29 -0800 | [diff] [blame] | 520 | private final WidgetItem mInfo; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 521 | private final int mPreviewHeight; |
| 522 | private final int mPreviewWidth; |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 523 | private final WidgetCell mCaller; |
Sunny Goyal | a2441e8 | 2017-01-14 13:40:15 -0800 | [diff] [blame] | 524 | private final BaseActivity mActivity; |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 525 | @Thunk long[] mVersions; |
| 526 | @Thunk Bitmap mBitmapToRecycle; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 527 | |
Sunny Goyal | 4ddc401 | 2016-03-10 12:02:29 -0800 | [diff] [blame] | 528 | PreviewLoadTask(WidgetCacheKey key, WidgetItem info, int previewWidth, |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 529 | int previewHeight, WidgetCell caller) { |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 530 | mKey = key; |
| 531 | mInfo = info; |
| 532 | mPreviewHeight = previewHeight; |
| 533 | mPreviewWidth = previewWidth; |
| 534 | mCaller = caller; |
Sunny Goyal | a2441e8 | 2017-01-14 13:40:15 -0800 | [diff] [blame] | 535 | mActivity = BaseActivity.fromContext(mCaller.getContext()); |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 536 | if (DEBUG) { |
| 537 | Log.d(TAG, String.format("%s, %s, %d, %d", |
| 538 | mKey, mInfo, mPreviewHeight, mPreviewWidth)); |
| 539 | } |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 542 | @Override |
| 543 | protected Bitmap doInBackground(Void... params) { |
| 544 | Bitmap unusedBitmap = null; |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 545 | |
Hyunyoung Song | f00d02b | 2015-06-05 13:30:19 -0700 | [diff] [blame] | 546 | // If already cancelled before this gets to run in the background, then return early |
| 547 | if (isCancelled()) { |
| 548 | return null; |
| 549 | } |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 550 | synchronized (mUnusedBitmaps) { |
Hyunyoung Song | f00d02b | 2015-06-05 13:30:19 -0700 | [diff] [blame] | 551 | // Check if we can re-use a bitmap |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 552 | for (Bitmap candidate : mUnusedBitmaps) { |
| 553 | if (candidate != null && candidate.isMutable() && |
| 554 | candidate.getWidth() == mPreviewWidth && |
| 555 | candidate.getHeight() == mPreviewHeight) { |
| 556 | unusedBitmap = candidate; |
Hyunyoung Song | f00d02b | 2015-06-05 13:30:19 -0700 | [diff] [blame] | 557 | mUnusedBitmaps.remove(unusedBitmap); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 558 | break; |
| 559 | } |
| 560 | } |
Hyunyoung Song | f00d02b | 2015-06-05 13:30:19 -0700 | [diff] [blame] | 561 | } |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 562 | |
Hyunyoung Song | f00d02b | 2015-06-05 13:30:19 -0700 | [diff] [blame] | 563 | // creating a bitmap is expensive. Do not do this inside synchronized block. |
| 564 | if (unusedBitmap == null) { |
| 565 | unusedBitmap = Bitmap.createBitmap(mPreviewWidth, mPreviewHeight, Config.ARGB_8888); |
Adrian Roos | 1f375ab | 2014-04-28 18:26:38 +0200 | [diff] [blame] | 566 | } |
Winson Chung | 05304db | 2015-05-18 16:53:20 -0700 | [diff] [blame] | 567 | // If cancelled now, don't bother reading the preview from the DB |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 568 | if (isCancelled()) { |
Winson Chung | 05304db | 2015-05-18 16:53:20 -0700 | [diff] [blame] | 569 | return unusedBitmap; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 570 | } |
Winson Chung | 05304db | 2015-05-18 16:53:20 -0700 | [diff] [blame] | 571 | Bitmap preview = readFromDb(mKey, unusedBitmap, this); |
| 572 | // Only consider generating the preview if we have not cancelled the task already |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 573 | if (!isCancelled() && preview == null) { |
| 574 | // Fetch the version info before we generate the preview, so that, in-case the |
| 575 | // app was updated while we are generating the preview, we use the old version info, |
| 576 | // which would gets re-written next time. |
Sunny Goyal | 65513ba | 2017-01-20 11:54:25 -0800 | [diff] [blame] | 577 | boolean persistable = mInfo.activityInfo == null |
| 578 | || mInfo.activityInfo.isPersistable(); |
| 579 | mVersions = persistable ? getPackageVersion(mKey.componentName.getPackageName()) |
| 580 | : null; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 581 | |
| 582 | // it's not in the db... we need to generate it |
Sunny Goyal | a2441e8 | 2017-01-14 13:40:15 -0800 | [diff] [blame] | 583 | preview = generatePreview(mActivity, mInfo, unusedBitmap, mPreviewWidth, mPreviewHeight); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 584 | } |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 585 | return preview; |
| 586 | } |
| 587 | |
| 588 | @Override |
Winson Chung | 05304db | 2015-05-18 16:53:20 -0700 | [diff] [blame] | 589 | protected void onPostExecute(final Bitmap preview) { |
| 590 | mCaller.applyPreview(preview); |
| 591 | |
| 592 | // Write the generated preview to the DB in the worker thread |
| 593 | if (mVersions != null) { |
| 594 | mWorkerHandler.post(new Runnable() { |
| 595 | @Override |
| 596 | public void run() { |
| 597 | if (!isCancelled()) { |
| 598 | // If we are still using this preview, then write it to the DB and then |
| 599 | // let the normal clear mechanism recycle the bitmap |
| 600 | writeToDb(mKey, mVersions, preview); |
| 601 | mBitmapToRecycle = preview; |
| 602 | } else { |
| 603 | // If we've already cancelled, then skip writing the bitmap to the DB |
| 604 | // and manually add the bitmap back to the recycled set |
| 605 | synchronized (mUnusedBitmaps) { |
| 606 | mUnusedBitmaps.add(preview); |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | }); |
| 611 | } else { |
| 612 | // If we don't need to write to disk, then ensure the preview gets recycled by |
| 613 | // the normal clear mechanism |
| 614 | mBitmapToRecycle = preview; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | @Override |
Hyunyoung Song | e98f4a4 | 2015-06-16 10:45:24 -0700 | [diff] [blame] | 619 | protected void onCancelled(final Bitmap preview) { |
Winson Chung | 05304db | 2015-05-18 16:53:20 -0700 | [diff] [blame] | 620 | // If we've cancelled while the task is running, then can return the bitmap to the |
| 621 | // recycled set immediately. Otherwise, it will be recycled after the preview is written |
| 622 | // to disk. |
| 623 | if (preview != null) { |
Hyunyoung Song | e98f4a4 | 2015-06-16 10:45:24 -0700 | [diff] [blame] | 624 | mWorkerHandler.post(new Runnable() { |
| 625 | @Override |
| 626 | public void run() { |
| 627 | synchronized (mUnusedBitmaps) { |
| 628 | mUnusedBitmaps.add(preview); |
| 629 | } |
| 630 | } |
| 631 | }); |
Winson Chung | 05304db | 2015-05-18 16:53:20 -0700 | [diff] [blame] | 632 | } |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 633 | } |
Sunny Goyal | c65a008 | 2017-03-08 14:49:55 -0800 | [diff] [blame^] | 634 | |
| 635 | @Override |
| 636 | public void onCancel() { |
| 637 | cancel(true); |
| 638 | |
| 639 | // This only handles the case where the PreviewLoadTask is cancelled after the task has |
| 640 | // successfully completed (including having written to disk when necessary). In the |
| 641 | // other cases where it is cancelled while the task is running, it will be cleaned up |
| 642 | // in the tasks's onCancelled() call, and if cancelled while the task is writing to |
| 643 | // disk, it will be cancelled in the task's onPostExecute() call. |
| 644 | if (mBitmapToRecycle != null) { |
| 645 | mWorkerHandler.post(new Runnable() { |
| 646 | @Override |
| 647 | public void run() { |
| 648 | synchronized (mUnusedBitmaps) { |
| 649 | mUnusedBitmaps.add(mBitmapToRecycle); |
| 650 | } |
| 651 | mBitmapToRecycle = null; |
| 652 | } |
| 653 | }); |
| 654 | } |
| 655 | } |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | private static final class WidgetCacheKey extends ComponentKey { |
| 659 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 660 | @Thunk final String size; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 661 | |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 662 | public WidgetCacheKey(ComponentName componentName, UserHandle user, String size) { |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 663 | super(componentName, user); |
| 664 | this.size = size; |
| 665 | } |
| 666 | |
| 667 | @Override |
| 668 | public int hashCode() { |
| 669 | return super.hashCode() ^ size.hashCode(); |
| 670 | } |
| 671 | |
| 672 | @Override |
| 673 | public boolean equals(Object o) { |
| 674 | return super.equals(o) && ((WidgetCacheKey) o).size.equals(size); |
Adrian Roos | 1f375ab | 2014-04-28 18:26:38 +0200 | [diff] [blame] | 675 | } |
| 676 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 677 | } |