blob: 2c45c776a1b59697d284a58dad8461994d57c585 [file] [log] [blame]
Daniel Sandler325dc232013-06-05 22:57:57 -04001package com.android.launcher3;
Michael Jurka05713af2013-01-23 12:39:24 +01002
Sunny Goyal6fe3eec2019-08-15 14:53:41 -07003import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
4import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
5
Michael Jurka05713af2013-01-23 12:39:24 +01006import android.content.ComponentName;
7import android.content.ContentValues;
8import android.content.Context;
Sunny Goyal5b0e6692015-03-19 14:31:19 -07009import android.content.pm.PackageInfo;
Sunny Goyal4ddc4012016-03-10 12:02:29 -080010import android.content.pm.PackageManager;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070011import android.content.pm.PackageManager.NameNotFoundException;
Michael Jurka05713af2013-01-23 12:39:24 +010012import android.content.res.Resources;
13import android.database.Cursor;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070014import android.database.SQLException;
Michael Jurka05713af2013-01-23 12:39:24 +010015import android.database.sqlite.SQLiteDatabase;
Michael Jurka05713af2013-01-23 12:39:24 +010016import android.graphics.Bitmap;
17import android.graphics.Bitmap.Config;
18import android.graphics.BitmapFactory;
19import android.graphics.Canvas;
Sunny Goyal7d87cb12017-02-13 10:44:43 -080020import android.graphics.Color;
Michael Jurka05713af2013-01-23 12:39:24 +010021import android.graphics.Paint;
22import android.graphics.PorterDuff;
Sunny Goyal92bbfa12017-02-07 15:06:32 -080023import android.graphics.PorterDuffXfermode;
Sunny Goyal7d87cb12017-02-13 10:44:43 -080024import android.graphics.Rect;
Sunny Goyal4cad7532015-03-18 15:56:30 -070025import android.graphics.RectF;
Sunny Goyal14168432019-10-24 15:59:49 -070026import android.graphics.drawable.BitmapDrawable;
Michael Jurka05713af2013-01-23 12:39:24 +010027import android.graphics.drawable.Drawable;
28import android.os.AsyncTask;
Sunny Goyalc65a0082017-03-08 14:49:55 -080029import android.os.CancellationSignal;
Hyunyoung Song2bf3f372019-04-02 10:45:57 -070030import android.os.Process;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080031import android.os.UserHandle;
Sunny Goyal14168432019-10-24 15:59:49 -070032import android.util.ArrayMap;
Michael Jurka05713af2013-01-23 12:39:24 +010033import android.util.Log;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070034import android.util.LongSparseArray;
Samuel Fufa15357962019-09-03 12:36:15 -070035import android.util.Pair;
Sunny Goyal383c5072015-06-12 21:18:53 -070036
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070037import androidx.annotation.Nullable;
Sunny Goyal14168432019-10-24 15:59:49 -070038import androidx.annotation.UiThread;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070039
Sunny Goyale62d2bb2018-11-06 10:28:37 -080040import com.android.launcher3.icons.GraphicsUtils;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070041import com.android.launcher3.icons.IconCache;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070042import com.android.launcher3.icons.LauncherIcons;
43import com.android.launcher3.icons.ShadowGenerator;
Sunny Goyal4ddc4012016-03-10 12:02:29 -080044import com.android.launcher3.model.WidgetItem;
Sunny Goyale7b00122019-10-02 16:13:34 -070045import com.android.launcher3.pm.ShortcutConfigActivityInfo;
Sunny Goyal337c81f2019-12-10 12:19:13 -080046import com.android.launcher3.pm.UserCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070047import com.android.launcher3.util.ComponentKey;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070048import com.android.launcher3.util.Executors;
Tony Wickham86222d22017-03-29 15:30:43 -070049import com.android.launcher3.util.PackageUserKey;
Sunny Goyal6388b2c2016-04-15 18:03:27 -070050import com.android.launcher3.util.Preconditions;
Sunny Goyal6f709362015-12-17 17:09:36 -080051import com.android.launcher3.util.SQLiteCacheHelper;
Adam Cohen091440a2015-03-18 14:16:05 -070052import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -070053import com.android.launcher3.widget.WidgetCell;
Sunny Goyal337c81f2019-12-10 12:19:13 -080054import com.android.launcher3.widget.WidgetManagerHelper;
Hyunyoung Song8821ca92015-05-04 16:28:20 -070055
56import java.util.ArrayList;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070057import java.util.Collections;
Michael Jurka05713af2013-01-23 12:39:24 +010058import java.util.HashMap;
59import java.util.HashSet;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070060import java.util.Set;
61import java.util.WeakHashMap;
Adrian Roos65d60e22014-04-15 21:07:49 +020062import java.util.concurrent.ExecutionException;
Michael Jurka05713af2013-01-23 12:39:24 +010063
Sunny Goyalffe83f12014-08-14 17:39:34 -070064public class WidgetPreviewLoader {
Michael Jurka05713af2013-01-23 12:39:24 +010065
Sunny Goyalffe83f12014-08-14 17:39:34 -070066 private static final String TAG = "WidgetPreviewLoader";
Hyunyoung Song3f471442015-04-08 19:01:34 -070067 private static final boolean DEBUG = false;
Sunny Goyalffe83f12014-08-14 17:39:34 -070068
Sunny Goyal5b0e6692015-03-19 14:31:19 -070069 private final HashMap<String, long[]> mPackageVersions = new HashMap<>();
Hyunyoung Song559d90d2015-04-28 15:06:45 -070070
71 /**
72 * Weak reference objects, do not prevent their referents from being made finalizable,
73 * finalized, and then reclaimed.
Hyunyoung Songe98f4a42015-06-16 10:45:24 -070074 * Note: synchronized block used for this variable is expensive and the block should always
75 * be posted to a background thread.
Hyunyoung Song559d90d2015-04-28 15:06:45 -070076 */
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070077 @Thunk final Set<Bitmap> mUnusedBitmaps = Collections.newSetFromMap(new WeakHashMap<>());
Sunny Goyal4cad7532015-03-18 15:56:30 -070078
79 private final Context mContext;
Sunny Goyalffe83f12014-08-14 17:39:34 -070080 private final IconCache mIconCache;
Sunny Goyal337c81f2019-12-10 12:19:13 -080081 private final UserCache mUserCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070082 private final CacheDb mDb;
Michael Jurka05713af2013-01-23 12:39:24 +010083
Sunny Goyal14168432019-10-24 15:59:49 -070084 private final UserHandle mMyUser = Process.myUserHandle();
85 private final ArrayMap<UserHandle, Bitmap> mUserBadges = new ArrayMap<>();
86
Sunny Goyal383c5072015-06-12 21:18:53 -070087 public WidgetPreviewLoader(Context context, IconCache iconCache) {
Chris Wrenfd13c712013-09-27 15:45:19 -040088 mContext = context;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070089 mIconCache = iconCache;
Sunny Goyal337c81f2019-12-10 12:19:13 -080090 mUserCache = UserCache.INSTANCE.get(context);
Sunny Goyal5b0e6692015-03-19 14:31:19 -070091 mDb = new CacheDb(context);
Michael Jurka3f4e0702013-02-05 11:21:28 +010092 }
Sunny Goyalffe83f12014-08-14 17:39:34 -070093
Sunny Goyal5b0e6692015-03-19 14:31:19 -070094 /**
Sunny Goyal14168432019-10-24 15:59:49 -070095 * Returns a drawable that can be used as a badge for the user or null.
96 */
97 @UiThread
98 public Drawable getBadgeForUser(UserHandle user, int badgeSize) {
99 if (mMyUser.equals(user)) {
100 return null;
101 }
102
103 Bitmap badgeBitmap = getUserBadge(user, badgeSize);
104 FastBitmapDrawable d = new FastBitmapDrawable(badgeBitmap);
105 d.setFilterBitmap(true);
106 d.setBounds(0, 0, badgeBitmap.getWidth(), badgeBitmap.getHeight());
107 return d;
108 }
109
110 private Bitmap getUserBadge(UserHandle user, int badgeSize) {
111 synchronized (mUserBadges) {
112 Bitmap badgeBitmap = mUserBadges.get(user);
113 if (badgeBitmap != null) {
114 return badgeBitmap;
115 }
116
117 final Resources res = mContext.getResources();
118 badgeBitmap = Bitmap.createBitmap(badgeSize, badgeSize, Bitmap.Config.ARGB_8888);
119
120 Drawable drawable = mContext.getPackageManager().getUserBadgedDrawableForDensity(
121 new BitmapDrawable(res, badgeBitmap), user,
122 new Rect(0, 0, badgeSize, badgeSize),
123 0);
124 if (drawable instanceof BitmapDrawable) {
125 badgeBitmap = ((BitmapDrawable) drawable).getBitmap();
126 } else {
127 badgeBitmap.eraseColor(Color.TRANSPARENT);
128 Canvas c = new Canvas(badgeBitmap);
129 drawable.setBounds(0, 0, badgeSize, badgeSize);
130 drawable.draw(c);
131 c.setBitmap(null);
132 }
133
134 mUserBadges.put(user, badgeBitmap);
135 return badgeBitmap;
136 }
137 }
138
139 /**
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700140 * Generates the widget preview on {@link AsyncTask#THREAD_POOL_EXECUTOR}. Must be
141 * called on UI thread
142 *
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700143 * @return a request id which can be used to cancel the request.
144 */
Sunny Goyalc65a0082017-03-08 14:49:55 -0800145 public CancellationSignal getPreview(WidgetItem item, int previewWidth,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700146 int previewHeight, WidgetCell caller) {
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700147 String size = previewWidth + "x" + previewHeight;
Sunny Goyal4ddc4012016-03-10 12:02:29 -0800148 WidgetCacheKey key = new WidgetCacheKey(item.componentName, item.user, size);
Michael Jurka3f4e0702013-02-05 11:21:28 +0100149
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700150 PreviewLoadTask task = new PreviewLoadTask(key, item, previewWidth, previewHeight, caller);
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700151 task.executeOnExecutor(Executors.THREAD_POOL_EXECUTOR);
Sunny Goyalc65a0082017-03-08 14:49:55 -0800152
153 CancellationSignal signal = new CancellationSignal();
154 signal.setOnCancelListener(task);
155 return signal;
Michael Jurka05713af2013-01-23 12:39:24 +0100156 }
157
Hyunyoung Song2bf3f372019-04-02 10:45:57 -0700158 public void refresh() {
159 mDb.clear();
Hyunyoung Song2bf3f372019-04-02 10:45:57 -0700160 }
Sunny Goyal14168432019-10-24 15:59:49 -0700161
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700162 /**
163 * The DB holds the generated previews for various components. Previews can also have different
164 * sizes (landscape vs portrait).
165 */
Sunny Goyal6f709362015-12-17 17:09:36 -0800166 private static class CacheDb extends SQLiteCacheHelper {
Jon Miranda6cdc16c2017-06-27 14:04:01 -0700167 private static final int DB_VERSION = 9;
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700168
169 private static final String TABLE_NAME = "shortcut_and_widget_previews";
170 private static final String COLUMN_COMPONENT = "componentName";
171 private static final String COLUMN_USER = "profileId";
172 private static final String COLUMN_SIZE = "size";
173 private static final String COLUMN_PACKAGE = "packageName";
174 private static final String COLUMN_LAST_UPDATED = "lastUpdated";
175 private static final String COLUMN_VERSION = "version";
176 private static final String COLUMN_PREVIEW_BITMAP = "preview_bitmap";
Michael Jurka05713af2013-01-23 12:39:24 +0100177
Michael Jurkad9cb4a12013-03-19 12:01:06 +0100178 public CacheDb(Context context) {
Sunny Goyal6f709362015-12-17 17:09:36 -0800179 super(context, LauncherFiles.WIDGET_PREVIEWS_DB, DB_VERSION, TABLE_NAME);
Michael Jurka05713af2013-01-23 12:39:24 +0100180 }
181
182 @Override
Sunny Goyal6f709362015-12-17 17:09:36 -0800183 public void onCreateTable(SQLiteDatabase database) {
Michael Jurka32b7a092013-02-07 20:06:49 +0100184 database.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700185 COLUMN_COMPONENT + " TEXT NOT NULL, " +
186 COLUMN_USER + " INTEGER NOT NULL, " +
Michael Jurka05713af2013-01-23 12:39:24 +0100187 COLUMN_SIZE + " TEXT NOT NULL, " +
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700188 COLUMN_PACKAGE + " TEXT NOT NULL, " +
189 COLUMN_LAST_UPDATED + " INTEGER NOT NULL DEFAULT 0, " +
190 COLUMN_VERSION + " INTEGER NOT NULL DEFAULT 0, " +
191 COLUMN_PREVIEW_BITMAP + " BLOB, " +
192 "PRIMARY KEY (" + COLUMN_COMPONENT + ", " + COLUMN_USER + ", " + COLUMN_SIZE + ") " +
Michael Jurka32b7a092013-02-07 20:06:49 +0100193 ");");
Michael Jurka05713af2013-01-23 12:39:24 +0100194 }
Michael Jurka05713af2013-01-23 12:39:24 +0100195 }
196
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700197 @Thunk void writeToDb(WidgetCacheKey key, long[] versions, Bitmap preview) {
Michael Jurka05713af2013-01-23 12:39:24 +0100198 ContentValues values = new ContentValues();
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700199 values.put(CacheDb.COLUMN_COMPONENT, key.componentName.flattenToShortString());
Sunny Goyal337c81f2019-12-10 12:19:13 -0800200 values.put(CacheDb.COLUMN_USER, mUserCache.getSerialNumberForUser(key.user));
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700201 values.put(CacheDb.COLUMN_SIZE, key.size);
202 values.put(CacheDb.COLUMN_PACKAGE, key.componentName.getPackageName());
203 values.put(CacheDb.COLUMN_VERSION, versions[0]);
204 values.put(CacheDb.COLUMN_LAST_UPDATED, versions[1]);
Sunny Goyale62d2bb2018-11-06 10:28:37 -0800205 values.put(CacheDb.COLUMN_PREVIEW_BITMAP, GraphicsUtils.flattenBitmap(preview));
Sunny Goyal6f709362015-12-17 17:09:36 -0800206 mDb.insertOrReplace(values);
Michael Jurka05713af2013-01-23 12:39:24 +0100207 }
208
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800209 public void removePackage(String packageName, UserHandle user) {
Sunny Goyal337c81f2019-12-10 12:19:13 -0800210 removePackage(packageName, user, mUserCache.getSerialNumberForUser(user));
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700211 }
212
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800213 private void removePackage(String packageName, UserHandle user, long userSerial) {
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700214 synchronized(mPackageVersions) {
215 mPackageVersions.remove(packageName);
216 }
217
Sunny Goyal6f709362015-12-17 17:09:36 -0800218 mDb.delete(
219 CacheDb.COLUMN_PACKAGE + " = ? AND " + CacheDb.COLUMN_USER + " = ?",
220 new String[]{packageName, Long.toString(userSerial)});
Michael Jurka8ff02ca2013-11-01 14:19:27 +0100221 }
222
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700223 /**
224 * Updates the persistent DB:
225 * 1. Any preview generated for an old package version is removed
226 * 2. Any preview for an absent package is removed
227 * This ensures that we remove entries for packages which changed while the launcher was dead.
Tony Wickham86222d22017-03-29 15:30:43 -0700228 *
229 * @param packageUser if provided, specifies that list only contains previews for the
230 * given package/user, otherwise the list contains all previews
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700231 */
Tony Wickham86222d22017-03-29 15:30:43 -0700232 public void removeObsoletePreviews(ArrayList<? extends ComponentKey> list,
233 @Nullable PackageUserKey packageUser) {
Sunny Goyal6388b2c2016-04-15 18:03:27 -0700234 Preconditions.assertWorkerThread();
Hyunyoung Song8821ca92015-05-04 16:28:20 -0700235
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700236 LongSparseArray<HashSet<String>> validPackages = new LongSparseArray<>();
237
Sunny Goyal4ddc4012016-03-10 12:02:29 -0800238 for (ComponentKey key : list) {
Sunny Goyal337c81f2019-12-10 12:19:13 -0800239 final long userId = mUserCache.getSerialNumberForUser(key.user);
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700240 HashSet<String> packages = validPackages.get(userId);
241 if (packages == null) {
242 packages = new HashSet<>();
243 validPackages.put(userId, packages);
244 }
Sunny Goyal4ddc4012016-03-10 12:02:29 -0800245 packages.add(key.componentName.getPackageName());
Michael Jurka05713af2013-01-23 12:39:24 +0100246 }
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700247
248 LongSparseArray<HashSet<String>> packagesToDelete = new LongSparseArray<>();
Tony Wickham86222d22017-03-29 15:30:43 -0700249 long passedUserId = packageUser == null ? 0
Sunny Goyal337c81f2019-12-10 12:19:13 -0800250 : mUserCache.getSerialNumberForUser(packageUser.mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700251 Cursor c = null;
252 try {
Sunny Goyal6f709362015-12-17 17:09:36 -0800253 c = mDb.query(
254 new String[]{CacheDb.COLUMN_USER, CacheDb.COLUMN_PACKAGE,
255 CacheDb.COLUMN_LAST_UPDATED, CacheDb.COLUMN_VERSION},
256 null, null);
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700257 while (c.moveToNext()) {
258 long userId = c.getLong(0);
259 String pkg = c.getString(1);
260 long lastUpdated = c.getLong(2);
261 long version = c.getLong(3);
262
Tony Wickham86222d22017-03-29 15:30:43 -0700263 if (packageUser != null && (!pkg.equals(packageUser.mPackageName)
264 || userId != passedUserId)) {
265 // This preview is associated with a different package/user, no need to remove.
266 continue;
267 }
268
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700269 HashSet<String> packages = validPackages.get(userId);
270 if (packages != null && packages.contains(pkg)) {
271 long[] versions = getPackageVersion(pkg);
272 if (versions[0] == version && versions[1] == lastUpdated) {
273 // Every thing checks out
274 continue;
275 }
276 }
277
278 // We need to delete this package.
279 packages = packagesToDelete.get(userId);
280 if (packages == null) {
281 packages = new HashSet<>();
282 packagesToDelete.put(userId, packages);
283 }
284 packages.add(pkg);
285 }
286
287 for (int i = 0; i < packagesToDelete.size(); i++) {
288 long userId = packagesToDelete.keyAt(i);
Sunny Goyal337c81f2019-12-10 12:19:13 -0800289 UserHandle user = mUserCache.getUserForSerialNumber(userId);
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700290 for (String pkg : packagesToDelete.valueAt(i)) {
291 removePackage(pkg, user, userId);
292 }
293 }
294 } catch (SQLException e) {
Sunny Goyal6f709362015-12-17 17:09:36 -0800295 Log.e(TAG, "Error updating widget previews", e);
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700296 } finally {
297 if (c != null) {
298 c.close();
299 }
300 }
301 }
302
Winson Chung05304db2015-05-18 16:53:20 -0700303 /**
304 * Reads the preview bitmap from the DB or null if the preview is not in the DB.
305 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700306 @Thunk Bitmap readFromDb(WidgetCacheKey key, Bitmap recycle, PreviewLoadTask loadTask) {
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700307 Cursor cursor = null;
308 try {
Sunny Goyal6f709362015-12-17 17:09:36 -0800309 cursor = mDb.query(
310 new String[]{CacheDb.COLUMN_PREVIEW_BITMAP},
311 CacheDb.COLUMN_COMPONENT + " = ? AND " + CacheDb.COLUMN_USER + " = ? AND "
312 + CacheDb.COLUMN_SIZE + " = ?",
313 new String[]{
Sunny Goyal3ef86912016-10-27 11:40:06 -0700314 key.componentName.flattenToShortString(),
Sunny Goyal337c81f2019-12-10 12:19:13 -0800315 Long.toString(mUserCache.getSerialNumberForUser(key.user)),
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700316 key.size
Sunny Goyal6f709362015-12-17 17:09:36 -0800317 });
Winson Chung05304db2015-05-18 16:53:20 -0700318 // If cancelled, skip getting the blob and decoding it into a bitmap
319 if (loadTask.isCancelled()) {
320 return null;
321 }
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700322 if (cursor.moveToNext()) {
323 byte[] blob = cursor.getBlob(0);
324 BitmapFactory.Options opts = new BitmapFactory.Options();
325 opts.inBitmap = recycle;
Michael Jurka6e27f642013-12-10 13:40:30 +0100326 try {
Winson Chung05304db2015-05-18 16:53:20 -0700327 if (!loadTask.isCancelled()) {
328 return BitmapFactory.decodeByteArray(blob, 0, blob.length, opts);
329 }
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700330 } catch (Exception e) {
331 return null;
Michael Jurka6e27f642013-12-10 13:40:30 +0100332 }
Michael Jurka05713af2013-01-23 12:39:24 +0100333 }
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700334 } catch (SQLException e) {
335 Log.w(TAG, "Error loading preview from DB", e);
336 } finally {
337 if (cursor != null) {
338 cursor.close();
339 }
340 }
341 return null;
Michael Jurka05713af2013-01-23 12:39:24 +0100342 }
343
Samuel Fufa15357962019-09-03 12:36:15 -0700344 /**
345 * Returns generatedPreview for a widget and if the preview should be saved in persistent
346 * storage.
347 * @param launcher
348 * @param item
349 * @param recycle
350 * @param previewWidth
351 * @param previewHeight
352 * @return Pair<Bitmap, Boolean>
353 */
354 private Pair<Bitmap, Boolean> generatePreview(BaseActivity launcher, WidgetItem item,
355 Bitmap recycle,
Adam Cohen2e6da152015-05-06 11:42:25 -0700356 int previewWidth, int previewHeight) {
Sunny Goyal4ddc4012016-03-10 12:02:29 -0800357 if (item.widgetInfo != null) {
358 return generateWidgetPreview(launcher, item.widgetInfo,
Adam Cohen2e6da152015-05-06 11:42:25 -0700359 previewWidth, recycle, null);
Michael Jurka05713af2013-01-23 12:39:24 +0100360 } else {
Samuel Fufa15357962019-09-03 12:36:15 -0700361 return new Pair<>(generateShortcutPreview(launcher, item.activityInfo,
362 previewWidth, previewHeight, recycle), false);
Michael Jurka05713af2013-01-23 12:39:24 +0100363 }
364 }
365
Hyunyoung Song3e840f42016-03-01 11:57:44 -0800366 /**
Sunny Goyal337c81f2019-12-10 12:19:13 -0800367 * Generates the widget preview from either the {@link WidgetManagerHelper} or cache
Hyunyoung Song3e840f42016-03-01 11:57:44 -0800368 * and add badge at the bottom right corner.
369 *
370 * @param launcher
371 * @param info information about the widget
372 * @param maxPreviewWidth width of the preview on either workspace or tray
373 * @param preview bitmap that can be recycled
374 * @param preScaledWidthOut return the width of the returned bitmap
Samuel Fufa15357962019-09-03 12:36:15 -0700375 * @return Pair<Bitmap (the preview) , Boolean (should be stored in db)>
Hyunyoung Song3e840f42016-03-01 11:57:44 -0800376 */
Samuel Fufa15357962019-09-03 12:36:15 -0700377 public Pair<Bitmap, Boolean> generateWidgetPreview(BaseActivity launcher,
378 LauncherAppWidgetProviderInfo info,
Sunny Goyal4cad7532015-03-18 15:56:30 -0700379 int maxPreviewWidth, Bitmap preview, int[] preScaledWidthOut) {
Michael Jurka05713af2013-01-23 12:39:24 +0100380 // Load the preview image if possible
Michael Jurka05713af2013-01-23 12:39:24 +0100381 if (maxPreviewWidth < 0) maxPreviewWidth = Integer.MAX_VALUE;
Michael Jurka05713af2013-01-23 12:39:24 +0100382
383 Drawable drawable = null;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700384 if (info.previewImage != 0) {
Sunny Goyal2570d102016-10-27 11:38:45 -0700385 try {
Sunny Goyal92bbfa12017-02-07 15:06:32 -0800386 drawable = info.loadPreviewImage(mContext, 0);
Sunny Goyal2570d102016-10-27 11:38:45 -0700387 } catch (OutOfMemoryError e) {
388 Log.w(TAG, "Error loading widget preview for: " + info.provider, e);
389 // During OutOfMemoryError, the previous heap stack is not affected. Catching
390 // an OOM error here should be safe & not affect other parts of launcher.
391 drawable = null;
392 }
Adrian Roosfa9ffc22014-05-12 15:59:59 +0200393 if (drawable != null) {
394 drawable = mutateOnMainThread(drawable);
395 } else {
Michael Jurka05713af2013-01-23 12:39:24 +0100396 Log.w(TAG, "Can't load widget preview drawable 0x" +
Sunny Goyalffe83f12014-08-14 17:39:34 -0700397 Integer.toHexString(info.previewImage) + " for provider: " + info.provider);
Michael Jurka05713af2013-01-23 12:39:24 +0100398 }
399 }
400
Sunny Goyal4cad7532015-03-18 15:56:30 -0700401 final boolean widgetPreviewExists = (drawable != null);
Sunny Goyal233ee962015-08-03 13:05:01 -0700402 final int spanX = info.spanX;
403 final int spanY = info.spanY;
Sunny Goyal4cad7532015-03-18 15:56:30 -0700404
Michael Jurka05713af2013-01-23 12:39:24 +0100405 int previewWidth;
406 int previewHeight;
Hyunyoung Song3e840f42016-03-01 11:57:44 -0800407
Samuel Fufa15357962019-09-03 12:36:15 -0700408 boolean savePreviewImage = widgetPreviewExists || info.previewImage == 0;
409
Roger Olsson4ac406d2018-04-12 10:08:01 +0200410 if (widgetPreviewExists && drawable.getIntrinsicWidth() > 0
411 && drawable.getIntrinsicHeight() > 0) {
Michael Jurka05713af2013-01-23 12:39:24 +0100412 previewWidth = drawable.getIntrinsicWidth();
413 previewHeight = drawable.getIntrinsicHeight();
414 } else {
Sunny Goyal92bbfa12017-02-07 15:06:32 -0800415 DeviceProfile dp = launcher.getDeviceProfile();
416 int tileSize = Math.min(dp.cellWidthPx, dp.cellHeightPx);
417 previewWidth = tileSize * spanX;
418 previewHeight = tileSize * spanY;
Michael Jurka05713af2013-01-23 12:39:24 +0100419 }
420
421 // Scale to fit width only - let the widget preview be clipped in the
422 // vertical dimension
423 float scale = 1f;
424 if (preScaledWidthOut != null) {
425 preScaledWidthOut[0] = previewWidth;
426 }
427 if (previewWidth > maxPreviewWidth) {
Sunny Goyala2441e82017-01-14 13:40:15 -0800428 scale = maxPreviewWidth / (float) (previewWidth);
Michael Jurka05713af2013-01-23 12:39:24 +0100429 }
430 if (scale != 1f) {
Roger Olsson4ac406d2018-04-12 10:08:01 +0200431 previewWidth = Math.max((int)(scale * previewWidth), 1);
432 previewHeight = Math.max((int)(scale * previewHeight), 1);
Michael Jurka05713af2013-01-23 12:39:24 +0100433 }
434
435 // If a bitmap is passed in, we use it; otherwise, we create a bitmap of the right size
Sunny Goyal4cad7532015-03-18 15:56:30 -0700436 final Canvas c = new Canvas();
Michael Jurka05713af2013-01-23 12:39:24 +0100437 if (preview == null) {
438 preview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
Sunny Goyal4cad7532015-03-18 15:56:30 -0700439 c.setBitmap(preview);
440 } else {
Sunny Goyala2441e82017-01-14 13:40:15 -0800441 // We use the preview bitmap height to determine where the badge will be drawn in the
442 // UI. If its larger than what we need, resize the preview bitmap so that there are
443 // no transparent pixels between the preview and the badge.
444 if (preview.getHeight() > previewHeight) {
445 preview.reconfigure(preview.getWidth(), previewHeight, preview.getConfig());
446 }
Sunny Goyal4cad7532015-03-18 15:56:30 -0700447 // Reusing bitmap. Clear it.
448 c.setBitmap(preview);
449 c.drawColor(0, PorterDuff.Mode.CLEAR);
Michael Jurka05713af2013-01-23 12:39:24 +0100450 }
451
452 // Draw the scaled preview into the final bitmap
Hyunyoung Songb9f932e2015-07-30 15:04:59 -0700453 int x = (preview.getWidth() - previewWidth) / 2;
Michael Jurka05713af2013-01-23 12:39:24 +0100454 if (widgetPreviewExists) {
Sunny Goyal4cad7532015-03-18 15:56:30 -0700455 drawable.setBounds(x, 0, x + previewWidth, previewHeight);
456 drawable.draw(c);
Michael Jurka05713af2013-01-23 12:39:24 +0100457 } else {
Sunny Goyalb3648e02017-05-02 16:17:10 -0700458 RectF boxRect = drawBoxWithShadow(c, previewWidth, previewHeight);
Sunny Goyal92bbfa12017-02-07 15:06:32 -0800459
460 // Draw horizontal and vertical lines to represent individual columns.
Sunny Goyalb3648e02017-05-02 16:17:10 -0700461 final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
Sunny Goyal92bbfa12017-02-07 15:06:32 -0800462 p.setStyle(Paint.Style.STROKE);
Sunny Goyal7d87cb12017-02-13 10:44:43 -0800463 p.setStrokeWidth(mContext.getResources()
464 .getDimension(R.dimen.widget_preview_cell_divider_width));
Sunny Goyal92bbfa12017-02-07 15:06:32 -0800465 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
466
467 float t = boxRect.left;
468 float tileSize = boxRect.width() / spanX;
469 for (int i = 1; i < spanX; i++) {
470 t += tileSize;
471 c.drawLine(t, 0, t, previewHeight, p);
Michael Jurka05713af2013-01-23 12:39:24 +0100472 }
Sunny Goyal4cad7532015-03-18 15:56:30 -0700473
Sunny Goyal92bbfa12017-02-07 15:06:32 -0800474 t = boxRect.top;
475 tileSize = boxRect.height() / spanY;
476 for (int i = 1; i < spanY; i++) {
477 t += tileSize;
478 c.drawLine(0, t, previewWidth, t, p);
479 }
Sunny Goyal4cad7532015-03-18 15:56:30 -0700480
Sunny Goyal92bbfa12017-02-07 15:06:32 -0800481 // Draw icon in the center.
Sunny Goyal4cad7532015-03-18 15:56:30 -0700482 try {
Sunny Goyal952e63d2017-08-16 04:59:08 -0700483 Drawable icon =
484 mIconCache.getFullResIcon(info.provider.getPackageName(), info.icon);
Sunny Goyal4cad7532015-03-18 15:56:30 -0700485 if (icon != null) {
Sunny Goyal92bbfa12017-02-07 15:06:32 -0800486 int appIconSize = launcher.getDeviceProfile().iconSizePx;
487 int iconSize = (int) Math.min(appIconSize * scale,
488 Math.min(boxRect.width(), boxRect.height()));
489
Sunny Goyal1ba7e362015-10-26 10:42:12 -0700490 icon = mutateOnMainThread(icon);
Sunny Goyal92bbfa12017-02-07 15:06:32 -0800491 int hoffset = (previewWidth - iconSize) / 2;
492 int yoffset = (previewHeight - iconSize) / 2;
493 icon.setBounds(hoffset, yoffset, hoffset + iconSize, yoffset + iconSize);
Sunny Goyal4cad7532015-03-18 15:56:30 -0700494 icon.draw(c);
495 }
Samuel Fufa15357962019-09-03 12:36:15 -0700496 } catch (Resources.NotFoundException e) {
497 savePreviewImage = false;
498 }
Michael Jurka05713af2013-01-23 12:39:24 +0100499 c.setBitmap(null);
500 }
Samuel Fufa15357962019-09-03 12:36:15 -0700501 return new Pair<>(preview, savePreviewImage);
Michael Jurka05713af2013-01-23 12:39:24 +0100502 }
503
Sunny Goyalb3648e02017-05-02 16:17:10 -0700504 private RectF drawBoxWithShadow(Canvas c, int width, int height) {
Sunny Goyal7d87cb12017-02-13 10:44:43 -0800505 Resources res = mContext.getResources();
Sunny Goyal7d87cb12017-02-13 10:44:43 -0800506
Sunny Goyale463c8f2017-06-20 13:34:09 -0700507 ShadowGenerator.Builder builder = new ShadowGenerator.Builder(Color.WHITE);
508 builder.shadowBlur = res.getDimension(R.dimen.widget_preview_shadow_blur);
509 builder.radius = res.getDimension(R.dimen.widget_preview_corner_radius);
510 builder.keyShadowDistance = res.getDimension(R.dimen.widget_preview_key_shadow_distance);
511
512 builder.bounds.set(builder.shadowBlur, builder.shadowBlur,
513 width - builder.shadowBlur,
514 height - builder.shadowBlur - builder.keyShadowDistance);
515 builder.drawShadow(c);
516 return builder.bounds;
Sunny Goyal7d87cb12017-02-13 10:44:43 -0800517 }
518
Sunny Goyal782f0c92017-01-19 10:27:54 -0800519 private Bitmap generateShortcutPreview(BaseActivity launcher, ShortcutConfigActivityInfo info,
520 int maxWidth, int maxHeight, Bitmap preview) {
Samuel Fufaf1424a32019-10-04 15:15:19 -0700521 int iconSize = launcher.getDeviceProfile().allAppsIconSizePx;
Sunny Goyal7d87cb12017-02-13 10:44:43 -0800522 int padding = launcher.getResources()
523 .getDimensionPixelSize(R.dimen.widget_preview_shortcut_padding);
524
525 int size = iconSize + 2 * padding;
526 if (maxHeight < size || maxWidth < size) {
527 throw new RuntimeException("Max size is too small for preview");
528 }
Sunny Goyal4cad7532015-03-18 15:56:30 -0700529 final Canvas c = new Canvas();
Sunny Goyal7d87cb12017-02-13 10:44:43 -0800530 if (preview == null || preview.getWidth() < size || preview.getHeight() < size) {
531 preview = Bitmap.createBitmap(size, size, Config.ARGB_8888);
Sunny Goyal4cad7532015-03-18 15:56:30 -0700532 c.setBitmap(preview);
Sunny Goyal4cad7532015-03-18 15:56:30 -0700533 } else {
Sunny Goyal7d87cb12017-02-13 10:44:43 -0800534 if (preview.getWidth() > size || preview.getHeight() > size) {
535 preview.reconfigure(size, size, preview.getConfig());
536 }
537
Sunny Goyal4cad7532015-03-18 15:56:30 -0700538 // Reusing bitmap. Clear it.
539 c.setBitmap(preview);
540 c.drawColor(0, PorterDuff.Mode.CLEAR);
Michael Jurka05713af2013-01-23 12:39:24 +0100541 }
Sunny Goyalb3648e02017-05-02 16:17:10 -0700542 RectF boxRect = drawBoxWithShadow(c, size, size);
Michael Jurka05713af2013-01-23 12:39:24 +0100543
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800544 LauncherIcons li = LauncherIcons.obtain(mContext);
Hyunyoung Song2bf3f372019-04-02 10:45:57 -0700545 Bitmap icon = li.createBadgedIconBitmap(
546 mutateOnMainThread(info.getFullResIcon(mIconCache)),
547 Process.myUserHandle(), 0).icon;
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800548 li.recycle();
549
Sunny Goyal7d87cb12017-02-13 10:44:43 -0800550 Rect src = new Rect(0, 0, icon.getWidth(), icon.getHeight());
Sunny Goyal4cad7532015-03-18 15:56:30 -0700551
Sunny Goyal7d87cb12017-02-13 10:44:43 -0800552 boxRect.set(0, 0, iconSize, iconSize);
553 boxRect.offset(padding, padding);
Sunny Goyalb3648e02017-05-02 16:17:10 -0700554 c.drawBitmap(icon, src, boxRect,
555 new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
Michael Jurka05713af2013-01-23 12:39:24 +0100556 c.setBitmap(null);
Michael Jurka05713af2013-01-23 12:39:24 +0100557 return preview;
558 }
559
Adrian Roos65d60e22014-04-15 21:07:49 +0200560 private Drawable mutateOnMainThread(final Drawable drawable) {
561 try {
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700562 return MAIN_EXECUTOR.submit(drawable::mutate).get();
Adrian Roos65d60e22014-04-15 21:07:49 +0200563 } catch (InterruptedException e) {
564 Thread.currentThread().interrupt();
565 throw new RuntimeException(e);
566 } catch (ExecutionException e) {
567 throw new RuntimeException(e);
568 }
569 }
Adrian Roos1f375ab2014-04-28 18:26:38 +0200570
Adrian Roos1f375ab2014-04-28 18:26:38 +0200571 /**
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700572 * @return an array of containing versionCode and lastUpdatedTime for the package.
Adrian Roos1f375ab2014-04-28 18:26:38 +0200573 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700574 @Thunk long[] getPackageVersion(String packageName) {
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700575 synchronized (mPackageVersions) {
576 long[] versions = mPackageVersions.get(packageName);
577 if (versions == null) {
578 versions = new long[2];
Adrian Roos1f375ab2014-04-28 18:26:38 +0200579 try {
Sunny Goyal4ddc4012016-03-10 12:02:29 -0800580 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName,
581 PackageManager.GET_UNINSTALLED_PACKAGES);
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700582 versions[0] = info.versionCode;
583 versions[1] = info.lastUpdateTime;
584 } catch (NameNotFoundException e) {
585 Log.e(TAG, "PackageInfo not found", e);
586 }
587 mPackageVersions.put(packageName, versions);
588 }
589 return versions;
590 }
591 }
592
Sunny Goyalc65a0082017-03-08 14:49:55 -0800593 public class PreviewLoadTask extends AsyncTask<Void, Void, Bitmap>
594 implements CancellationSignal.OnCancelListener {
Sunny Goyal316490e2015-06-02 09:38:28 -0700595 @Thunk final WidgetCacheKey mKey;
Sunny Goyal4ddc4012016-03-10 12:02:29 -0800596 private final WidgetItem mInfo;
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700597 private final int mPreviewHeight;
598 private final int mPreviewWidth;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700599 private final WidgetCell mCaller;
Sunny Goyala2441e82017-01-14 13:40:15 -0800600 private final BaseActivity mActivity;
Sunny Goyal316490e2015-06-02 09:38:28 -0700601 @Thunk long[] mVersions;
602 @Thunk Bitmap mBitmapToRecycle;
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700603
Samuel Fufa15357962019-09-03 12:36:15 -0700604 private boolean mSaveToDB = false;
605
Sunny Goyal4ddc4012016-03-10 12:02:29 -0800606 PreviewLoadTask(WidgetCacheKey key, WidgetItem info, int previewWidth,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700607 int previewHeight, WidgetCell caller) {
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700608 mKey = key;
609 mInfo = info;
610 mPreviewHeight = previewHeight;
611 mPreviewWidth = previewWidth;
612 mCaller = caller;
Sunny Goyala2441e82017-01-14 13:40:15 -0800613 mActivity = BaseActivity.fromContext(mCaller.getContext());
Hyunyoung Song3f471442015-04-08 19:01:34 -0700614 if (DEBUG) {
615 Log.d(TAG, String.format("%s, %s, %d, %d",
616 mKey, mInfo, mPreviewHeight, mPreviewWidth));
617 }
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700618 }
619
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700620 @Override
621 protected Bitmap doInBackground(Void... params) {
622 Bitmap unusedBitmap = null;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700623
Hyunyoung Songf00d02b2015-06-05 13:30:19 -0700624 // If already cancelled before this gets to run in the background, then return early
625 if (isCancelled()) {
626 return null;
627 }
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700628 synchronized (mUnusedBitmaps) {
Hyunyoung Songf00d02b2015-06-05 13:30:19 -0700629 // Check if we can re-use a bitmap
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700630 for (Bitmap candidate : mUnusedBitmaps) {
631 if (candidate != null && candidate.isMutable() &&
632 candidate.getWidth() == mPreviewWidth &&
633 candidate.getHeight() == mPreviewHeight) {
634 unusedBitmap = candidate;
Hyunyoung Songf00d02b2015-06-05 13:30:19 -0700635 mUnusedBitmaps.remove(unusedBitmap);
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700636 break;
637 }
638 }
Hyunyoung Songf00d02b2015-06-05 13:30:19 -0700639 }
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700640
Hyunyoung Songf00d02b2015-06-05 13:30:19 -0700641 // creating a bitmap is expensive. Do not do this inside synchronized block.
642 if (unusedBitmap == null) {
643 unusedBitmap = Bitmap.createBitmap(mPreviewWidth, mPreviewHeight, Config.ARGB_8888);
Adrian Roos1f375ab2014-04-28 18:26:38 +0200644 }
Winson Chung05304db2015-05-18 16:53:20 -0700645 // If cancelled now, don't bother reading the preview from the DB
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700646 if (isCancelled()) {
Winson Chung05304db2015-05-18 16:53:20 -0700647 return unusedBitmap;
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700648 }
Winson Chung05304db2015-05-18 16:53:20 -0700649 Bitmap preview = readFromDb(mKey, unusedBitmap, this);
650 // Only consider generating the preview if we have not cancelled the task already
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700651 if (!isCancelled() && preview == null) {
652 // Fetch the version info before we generate the preview, so that, in-case the
653 // app was updated while we are generating the preview, we use the old version info,
654 // which would gets re-written next time.
Sunny Goyal65513ba2017-01-20 11:54:25 -0800655 boolean persistable = mInfo.activityInfo == null
656 || mInfo.activityInfo.isPersistable();
657 mVersions = persistable ? getPackageVersion(mKey.componentName.getPackageName())
658 : null;
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700659
660 // it's not in the db... we need to generate it
Samuel Fufa15357962019-09-03 12:36:15 -0700661 Pair<Bitmap, Boolean> pair = generatePreview(mActivity, mInfo, unusedBitmap,
662 mPreviewWidth, mPreviewHeight);
663 preview = pair.first;
664 this.mSaveToDB = pair.second;
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700665 }
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700666 return preview;
667 }
668
669 @Override
Winson Chung05304db2015-05-18 16:53:20 -0700670 protected void onPostExecute(final Bitmap preview) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700671 mCaller.applyPreview(preview);
Winson Chung05304db2015-05-18 16:53:20 -0700672
673 // Write the generated preview to the DB in the worker thread
674 if (mVersions != null) {
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700675 MODEL_EXECUTOR.post(new Runnable() {
Winson Chung05304db2015-05-18 16:53:20 -0700676 @Override
677 public void run() {
Samuel Fufa15357962019-09-03 12:36:15 -0700678 if (!isCancelled() && mSaveToDB) {
Winson Chung05304db2015-05-18 16:53:20 -0700679 // If we are still using this preview, then write it to the DB and then
680 // let the normal clear mechanism recycle the bitmap
681 writeToDb(mKey, mVersions, preview);
682 mBitmapToRecycle = preview;
683 } else {
684 // If we've already cancelled, then skip writing the bitmap to the DB
685 // and manually add the bitmap back to the recycled set
686 synchronized (mUnusedBitmaps) {
687 mUnusedBitmaps.add(preview);
688 }
689 }
690 }
691 });
692 } else {
693 // If we don't need to write to disk, then ensure the preview gets recycled by
694 // the normal clear mechanism
695 mBitmapToRecycle = preview;
696 }
697 }
698
699 @Override
Hyunyoung Songe98f4a42015-06-16 10:45:24 -0700700 protected void onCancelled(final Bitmap preview) {
Winson Chung05304db2015-05-18 16:53:20 -0700701 // If we've cancelled while the task is running, then can return the bitmap to the
702 // recycled set immediately. Otherwise, it will be recycled after the preview is written
703 // to disk.
704 if (preview != null) {
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700705 MODEL_EXECUTOR.post(new Runnable() {
Hyunyoung Songe98f4a42015-06-16 10:45:24 -0700706 @Override
707 public void run() {
708 synchronized (mUnusedBitmaps) {
709 mUnusedBitmaps.add(preview);
710 }
711 }
712 });
Winson Chung05304db2015-05-18 16:53:20 -0700713 }
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700714 }
Sunny Goyalc65a0082017-03-08 14:49:55 -0800715
716 @Override
717 public void onCancel() {
718 cancel(true);
719
720 // This only handles the case where the PreviewLoadTask is cancelled after the task has
721 // successfully completed (including having written to disk when necessary). In the
722 // other cases where it is cancelled while the task is running, it will be cleaned up
723 // in the tasks's onCancelled() call, and if cancelled while the task is writing to
724 // disk, it will be cancelled in the task's onPostExecute() call.
725 if (mBitmapToRecycle != null) {
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700726 MODEL_EXECUTOR.post(new Runnable() {
Sunny Goyalc65a0082017-03-08 14:49:55 -0800727 @Override
728 public void run() {
729 synchronized (mUnusedBitmaps) {
730 mUnusedBitmaps.add(mBitmapToRecycle);
731 }
732 mBitmapToRecycle = null;
733 }
734 });
735 }
736 }
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700737 }
738
739 private static final class WidgetCacheKey extends ComponentKey {
740
Sunny Goyal316490e2015-06-02 09:38:28 -0700741 @Thunk final String size;
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700742
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800743 public WidgetCacheKey(ComponentName componentName, UserHandle user, String size) {
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700744 super(componentName, user);
745 this.size = size;
746 }
747
748 @Override
749 public int hashCode() {
750 return super.hashCode() ^ size.hashCode();
751 }
752
753 @Override
754 public boolean equals(Object o) {
755 return super.equals(o) && ((WidgetCacheKey) o).size.equals(size);
Adrian Roos1f375ab2014-04-28 18:26:38 +0200756 }
757 }
Michael Jurka05713af2013-01-23 12:39:24 +0100758}