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