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 | |
| 3 | import android.appwidget.AppWidgetProviderInfo; |
| 4 | import android.content.ComponentName; |
| 5 | import android.content.ContentValues; |
| 6 | import android.content.Context; |
Michael Jurka | 8ff02ca | 2013-11-01 14:19:27 +0100 | [diff] [blame] | 7 | import android.content.SharedPreferences; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 8 | import android.content.pm.PackageManager; |
| 9 | import android.content.pm.ResolveInfo; |
| 10 | import android.content.res.Resources; |
| 11 | import android.database.Cursor; |
| 12 | import android.database.sqlite.SQLiteDatabase; |
Michael Jurka | 6e27f64 | 2013-12-10 13:40:30 +0100 | [diff] [blame] | 13 | import android.database.sqlite.SQLiteDiskIOException; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 14 | import android.database.sqlite.SQLiteOpenHelper; |
| 15 | import android.graphics.Bitmap; |
| 16 | import android.graphics.Bitmap.Config; |
| 17 | import android.graphics.BitmapFactory; |
Adrian Roos | fa4c799 | 2014-03-19 15:58:14 +0100 | [diff] [blame] | 18 | import android.graphics.BitmapShader; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 19 | import android.graphics.Canvas; |
| 20 | import android.graphics.ColorMatrix; |
| 21 | import android.graphics.ColorMatrixColorFilter; |
| 22 | import android.graphics.Paint; |
| 23 | import android.graphics.PorterDuff; |
| 24 | import android.graphics.Rect; |
| 25 | import android.graphics.Shader; |
| 26 | import android.graphics.drawable.BitmapDrawable; |
| 27 | import android.graphics.drawable.Drawable; |
| 28 | import android.os.AsyncTask; |
| 29 | import android.util.Log; |
| 30 | |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 31 | import java.io.ByteArrayOutputStream; |
| 32 | import java.io.File; |
| 33 | import java.lang.ref.SoftReference; |
| 34 | import java.lang.ref.WeakReference; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.HashMap; |
| 37 | import java.util.HashSet; |
Adrian Roos | 65d60e2 | 2014-04-15 21:07:49 +0200 | [diff] [blame^] | 38 | import java.util.concurrent.Callable; |
| 39 | import java.util.concurrent.ExecutionException; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 40 | |
| 41 | abstract class SoftReferenceThreadLocal<T> { |
| 42 | private ThreadLocal<SoftReference<T>> mThreadLocal; |
| 43 | public SoftReferenceThreadLocal() { |
| 44 | mThreadLocal = new ThreadLocal<SoftReference<T>>(); |
| 45 | } |
| 46 | |
| 47 | abstract T initialValue(); |
| 48 | |
| 49 | public void set(T t) { |
| 50 | mThreadLocal.set(new SoftReference<T>(t)); |
| 51 | } |
| 52 | |
| 53 | public T get() { |
| 54 | SoftReference<T> reference = mThreadLocal.get(); |
| 55 | T obj; |
| 56 | if (reference == null) { |
| 57 | obj = initialValue(); |
| 58 | mThreadLocal.set(new SoftReference<T>(obj)); |
| 59 | return obj; |
| 60 | } else { |
| 61 | obj = reference.get(); |
| 62 | if (obj == null) { |
| 63 | obj = initialValue(); |
| 64 | mThreadLocal.set(new SoftReference<T>(obj)); |
| 65 | } |
| 66 | return obj; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | class CanvasCache extends SoftReferenceThreadLocal<Canvas> { |
| 72 | @Override |
| 73 | protected Canvas initialValue() { |
| 74 | return new Canvas(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | class PaintCache extends SoftReferenceThreadLocal<Paint> { |
| 79 | @Override |
| 80 | protected Paint initialValue() { |
| 81 | return null; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | class BitmapCache extends SoftReferenceThreadLocal<Bitmap> { |
| 86 | @Override |
| 87 | protected Bitmap initialValue() { |
| 88 | return null; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | class RectCache extends SoftReferenceThreadLocal<Rect> { |
| 93 | @Override |
| 94 | protected Rect initialValue() { |
| 95 | return new Rect(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | class BitmapFactoryOptionsCache extends SoftReferenceThreadLocal<BitmapFactory.Options> { |
| 100 | @Override |
| 101 | protected BitmapFactory.Options initialValue() { |
| 102 | return new BitmapFactory.Options(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | public class WidgetPreviewLoader { |
| 107 | static final String TAG = "WidgetPreviewLoader"; |
Michael Jurka | 8ff02ca | 2013-11-01 14:19:27 +0100 | [diff] [blame] | 108 | static final String ANDROID_INCREMENTAL_VERSION_NAME_KEY = "android.incremental.version"; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 109 | |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 110 | private int mPreviewBitmapWidth; |
| 111 | private int mPreviewBitmapHeight; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 112 | private String mSize; |
| 113 | private Context mContext; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 114 | private PackageManager mPackageManager; |
| 115 | private PagedViewCellLayout mWidgetSpacingLayout; |
| 116 | |
| 117 | // Used for drawing shortcut previews |
| 118 | private BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache(); |
| 119 | private PaintCache mCachedShortcutPreviewPaint = new PaintCache(); |
| 120 | private CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache(); |
| 121 | |
| 122 | // Used for drawing widget previews |
| 123 | private CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache(); |
| 124 | private RectCache mCachedAppWidgetPreviewSrcRect = new RectCache(); |
| 125 | private RectCache mCachedAppWidgetPreviewDestRect = new RectCache(); |
| 126 | private PaintCache mCachedAppWidgetPreviewPaint = new PaintCache(); |
Adrian Roos | fa4c799 | 2014-03-19 15:58:14 +0100 | [diff] [blame] | 127 | private PaintCache mDefaultAppWidgetPreviewPaint = new PaintCache(); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 128 | private String mCachedSelectQuery; |
| 129 | private BitmapFactoryOptionsCache mCachedBitmapFactoryOptions = new BitmapFactoryOptionsCache(); |
| 130 | |
| 131 | private int mAppIconSize; |
| 132 | private IconCache mIconCache; |
| 133 | |
Adrian Roos | 5d2704f | 2014-03-18 23:09:12 +0100 | [diff] [blame] | 134 | private static final float sWidgetPreviewIconPaddingPercentage = 0.25f; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 135 | |
Michael Jurka | d9cb4a1 | 2013-03-19 12:01:06 +0100 | [diff] [blame] | 136 | private CacheDb mDb; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 137 | |
Adrian Roos | 5d2704f | 2014-03-18 23:09:12 +0100 | [diff] [blame] | 138 | private final HashMap<String, WeakReference<Bitmap>> mLoadedPreviews; |
| 139 | private final ArrayList<SoftReference<Bitmap>> mUnusedBitmaps; |
| 140 | private final static HashSet<String> sInvalidPackages; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 141 | |
Adrian Roos | 65d60e2 | 2014-04-15 21:07:49 +0200 | [diff] [blame^] | 142 | private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor(); |
| 143 | |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 144 | static { |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 145 | sInvalidPackages = new HashSet<String>(); |
| 146 | } |
| 147 | |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 148 | public WidgetPreviewLoader(Context context) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 149 | LauncherAppState app = LauncherAppState.getInstance(); |
| 150 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 151 | |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 152 | mContext = context; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 153 | mPackageManager = mContext.getPackageManager(); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 154 | mAppIconSize = grid.iconSizePx; |
Michael Jurka | d9cb4a1 | 2013-03-19 12:01:06 +0100 | [diff] [blame] | 155 | mIconCache = app.getIconCache(); |
| 156 | mDb = app.getWidgetPreviewCacheDb(); |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 157 | mLoadedPreviews = new HashMap<String, WeakReference<Bitmap>>(); |
| 158 | mUnusedBitmaps = new ArrayList<SoftReference<Bitmap>>(); |
Michael Jurka | 8ff02ca | 2013-11-01 14:19:27 +0100 | [diff] [blame] | 159 | |
| 160 | SharedPreferences sp = context.getSharedPreferences( |
| 161 | LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE); |
| 162 | final String lastVersionName = sp.getString(ANDROID_INCREMENTAL_VERSION_NAME_KEY, null); |
| 163 | final String versionName = android.os.Build.VERSION.INCREMENTAL; |
| 164 | if (!versionName.equals(lastVersionName)) { |
| 165 | // clear all the previews whenever the system version changes, to ensure that previews |
| 166 | // are up-to-date for any apps that might have been updated with the system |
| 167 | clearDb(); |
| 168 | |
| 169 | SharedPreferences.Editor editor = sp.edit(); |
| 170 | editor.putString(ANDROID_INCREMENTAL_VERSION_NAME_KEY, versionName); |
| 171 | editor.commit(); |
| 172 | } |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 173 | } |
Michael Jurka | 6e27f64 | 2013-12-10 13:40:30 +0100 | [diff] [blame] | 174 | |
| 175 | public void recreateDb() { |
| 176 | LauncherAppState app = LauncherAppState.getInstance(); |
| 177 | app.recreateWidgetPreviewDb(); |
| 178 | mDb = app.getWidgetPreviewCacheDb(); |
| 179 | } |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 180 | |
| 181 | public void setPreviewSize(int previewWidth, int previewHeight, |
| 182 | PagedViewCellLayout widgetSpacingLayout) { |
| 183 | mPreviewBitmapWidth = previewWidth; |
| 184 | mPreviewBitmapHeight = previewHeight; |
| 185 | mSize = previewWidth + "x" + previewHeight; |
| 186 | mWidgetSpacingLayout = widgetSpacingLayout; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | public Bitmap getPreview(final Object o) { |
Michael Jurka | eb1bb92 | 2013-09-26 11:29:01 -0700 | [diff] [blame] | 190 | final String name = getObjectName(o); |
| 191 | final String packageName = getObjectPackage(o); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 192 | // check if the package is valid |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 193 | synchronized(sInvalidPackages) { |
Adrian Roos | 5d2704f | 2014-03-18 23:09:12 +0100 | [diff] [blame] | 194 | boolean packageValid = !sInvalidPackages.contains(packageName); |
| 195 | if (!packageValid) { |
| 196 | return null; |
| 197 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 198 | } |
Adrian Roos | 5d2704f | 2014-03-18 23:09:12 +0100 | [diff] [blame] | 199 | synchronized(mLoadedPreviews) { |
| 200 | // check if it exists in our existing cache |
| 201 | if (mLoadedPreviews.containsKey(name)) { |
| 202 | WeakReference<Bitmap> bitmapReference = mLoadedPreviews.get(name); |
| 203 | Bitmap bitmap = bitmapReference.get(); |
| 204 | if (bitmap != null) { |
| 205 | return bitmap; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | Bitmap unusedBitmap = null; |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 211 | synchronized(mUnusedBitmaps) { |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 212 | // not in cache; we need to load it from the db |
Adrian Roos | 5d2704f | 2014-03-18 23:09:12 +0100 | [diff] [blame] | 213 | while (unusedBitmap == null && mUnusedBitmaps.size() > 0) { |
| 214 | Bitmap candidate = mUnusedBitmaps.remove(0).get(); |
| 215 | if (candidate != null && candidate.isMutable() && |
| 216 | candidate.getWidth() == mPreviewBitmapWidth && |
| 217 | candidate.getHeight() == mPreviewBitmapHeight) { |
| 218 | unusedBitmap = candidate; |
| 219 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 220 | } |
| 221 | if (unusedBitmap != null) { |
| 222 | final Canvas c = mCachedAppWidgetPreviewCanvas.get(); |
| 223 | c.setBitmap(unusedBitmap); |
| 224 | c.drawColor(0, PorterDuff.Mode.CLEAR); |
| 225 | c.setBitmap(null); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (unusedBitmap == null) { |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 230 | unusedBitmap = Bitmap.createBitmap(mPreviewBitmapWidth, mPreviewBitmapHeight, |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 231 | Bitmap.Config.ARGB_8888); |
| 232 | } |
Adrian Roos | 5d2704f | 2014-03-18 23:09:12 +0100 | [diff] [blame] | 233 | Bitmap preview = readFromDb(name, unusedBitmap); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 234 | |
| 235 | if (preview != null) { |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 236 | synchronized(mLoadedPreviews) { |
| 237 | mLoadedPreviews.put(name, new WeakReference<Bitmap>(preview)); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 238 | } |
| 239 | return preview; |
| 240 | } else { |
| 241 | // it's not in the db... we need to generate it |
| 242 | final Bitmap generatedPreview = generatePreview(o, unusedBitmap); |
| 243 | preview = generatedPreview; |
| 244 | if (preview != unusedBitmap) { |
| 245 | throw new RuntimeException("generatePreview is not recycling the bitmap " + o); |
| 246 | } |
| 247 | |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 248 | synchronized(mLoadedPreviews) { |
| 249 | mLoadedPreviews.put(name, new WeakReference<Bitmap>(preview)); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // write to db on a thread pool... this can be done lazily and improves the performance |
| 253 | // of the first time widget previews are loaded |
| 254 | new AsyncTask<Void, Void, Void>() { |
| 255 | public Void doInBackground(Void ... args) { |
| 256 | writeToDb(o, generatedPreview); |
| 257 | return null; |
| 258 | } |
| 259 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null); |
| 260 | |
| 261 | return preview; |
| 262 | } |
| 263 | } |
| 264 | |
Michael Jurka | ee8e99f | 2013-02-07 13:27:06 +0100 | [diff] [blame] | 265 | public void recycleBitmap(Object o, Bitmap bitmapToRecycle) { |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 266 | String name = getObjectName(o); |
Michael Jurka | 5140cfa | 2013-02-15 14:50:15 +0100 | [diff] [blame] | 267 | synchronized (mLoadedPreviews) { |
| 268 | if (mLoadedPreviews.containsKey(name)) { |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 269 | Bitmap b = mLoadedPreviews.get(name).get(); |
Michael Jurka | ee8e99f | 2013-02-07 13:27:06 +0100 | [diff] [blame] | 270 | if (b == bitmapToRecycle) { |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 271 | mLoadedPreviews.remove(name); |
Michael Jurka | ee8e99f | 2013-02-07 13:27:06 +0100 | [diff] [blame] | 272 | if (bitmapToRecycle.isMutable()) { |
Michael Jurka | 5140cfa | 2013-02-15 14:50:15 +0100 | [diff] [blame] | 273 | synchronized (mUnusedBitmaps) { |
| 274 | mUnusedBitmaps.add(new SoftReference<Bitmap>(b)); |
| 275 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 276 | } |
| 277 | } else { |
| 278 | throw new RuntimeException("Bitmap passed in doesn't match up"); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
Michael Jurka | d9cb4a1 | 2013-03-19 12:01:06 +0100 | [diff] [blame] | 284 | static class CacheDb extends SQLiteOpenHelper { |
Michael Jurka | e5919c5 | 2013-03-06 17:30:10 +0100 | [diff] [blame] | 285 | final static int DB_VERSION = 2; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 286 | final static String DB_NAME = "widgetpreviews.db"; |
| 287 | final static String TABLE_NAME = "shortcut_and_widget_previews"; |
| 288 | final static String COLUMN_NAME = "name"; |
| 289 | final static String COLUMN_SIZE = "size"; |
| 290 | final static String COLUMN_PREVIEW_BITMAP = "preview_bitmap"; |
| 291 | Context mContext; |
| 292 | |
Michael Jurka | d9cb4a1 | 2013-03-19 12:01:06 +0100 | [diff] [blame] | 293 | public CacheDb(Context context) { |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 294 | super(context, new File(context.getCacheDir(), DB_NAME).getPath(), null, DB_VERSION); |
| 295 | // Store the context for later use |
| 296 | mContext = context; |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public void onCreate(SQLiteDatabase database) { |
Michael Jurka | 32b7a09 | 2013-02-07 20:06:49 +0100 | [diff] [blame] | 301 | database.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 302 | COLUMN_NAME + " TEXT NOT NULL, " + |
| 303 | COLUMN_SIZE + " TEXT NOT NULL, " + |
| 304 | COLUMN_PREVIEW_BITMAP + " BLOB NOT NULL, " + |
| 305 | "PRIMARY KEY (" + COLUMN_NAME + ", " + COLUMN_SIZE + ") " + |
Michael Jurka | 32b7a09 | 2013-02-07 20:06:49 +0100 | [diff] [blame] | 306 | ");"); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
Michael Jurka | e5919c5 | 2013-03-06 17:30:10 +0100 | [diff] [blame] | 311 | if (oldVersion != newVersion) { |
| 312 | // Delete all the records; they'll be repopulated as this is a cache |
| 313 | db.execSQL("DELETE FROM " + TABLE_NAME); |
| 314 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | private static final String WIDGET_PREFIX = "Widget:"; |
| 319 | private static final String SHORTCUT_PREFIX = "Shortcut:"; |
| 320 | |
| 321 | private static String getObjectName(Object o) { |
| 322 | // should cache the string builder |
| 323 | StringBuilder sb = new StringBuilder(); |
| 324 | String output; |
| 325 | if (o instanceof AppWidgetProviderInfo) { |
| 326 | sb.append(WIDGET_PREFIX); |
| 327 | sb.append(((AppWidgetProviderInfo) o).provider.flattenToString()); |
| 328 | output = sb.toString(); |
| 329 | sb.setLength(0); |
| 330 | } else { |
| 331 | sb.append(SHORTCUT_PREFIX); |
| 332 | |
| 333 | ResolveInfo info = (ResolveInfo) o; |
| 334 | sb.append(new ComponentName(info.activityInfo.packageName, |
| 335 | info.activityInfo.name).flattenToString()); |
| 336 | output = sb.toString(); |
| 337 | sb.setLength(0); |
| 338 | } |
| 339 | return output; |
| 340 | } |
| 341 | |
| 342 | private String getObjectPackage(Object o) { |
| 343 | if (o instanceof AppWidgetProviderInfo) { |
| 344 | return ((AppWidgetProviderInfo) o).provider.getPackageName(); |
| 345 | } else { |
| 346 | ResolveInfo info = (ResolveInfo) o; |
| 347 | return info.activityInfo.packageName; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | private void writeToDb(Object o, Bitmap preview) { |
| 352 | String name = getObjectName(o); |
| 353 | SQLiteDatabase db = mDb.getWritableDatabase(); |
| 354 | ContentValues values = new ContentValues(); |
| 355 | |
Michael Jurka | d9cb4a1 | 2013-03-19 12:01:06 +0100 | [diff] [blame] | 356 | values.put(CacheDb.COLUMN_NAME, name); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 357 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 358 | preview.compress(Bitmap.CompressFormat.PNG, 100, stream); |
Michael Jurka | d9cb4a1 | 2013-03-19 12:01:06 +0100 | [diff] [blame] | 359 | values.put(CacheDb.COLUMN_PREVIEW_BITMAP, stream.toByteArray()); |
| 360 | values.put(CacheDb.COLUMN_SIZE, mSize); |
Michael Jurka | 6e27f64 | 2013-12-10 13:40:30 +0100 | [diff] [blame] | 361 | try { |
| 362 | db.insert(CacheDb.TABLE_NAME, null, values); |
| 363 | } catch (SQLiteDiskIOException e) { |
| 364 | recreateDb(); |
| 365 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Michael Jurka | 8ff02ca | 2013-11-01 14:19:27 +0100 | [diff] [blame] | 368 | private void clearDb() { |
| 369 | SQLiteDatabase db = mDb.getWritableDatabase(); |
| 370 | // Delete everything |
Michael Jurka | 6e27f64 | 2013-12-10 13:40:30 +0100 | [diff] [blame] | 371 | try { |
| 372 | db.delete(CacheDb.TABLE_NAME, null, null); |
| 373 | } catch (SQLiteDiskIOException e) { |
| 374 | } |
Michael Jurka | 8ff02ca | 2013-11-01 14:19:27 +0100 | [diff] [blame] | 375 | } |
| 376 | |
Michael Jurka | eb1bb92 | 2013-09-26 11:29:01 -0700 | [diff] [blame] | 377 | public static void removePackageFromDb(final CacheDb cacheDb, final String packageName) { |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 378 | synchronized(sInvalidPackages) { |
| 379 | sInvalidPackages.add(packageName); |
| 380 | } |
| 381 | new AsyncTask<Void, Void, Void>() { |
| 382 | public Void doInBackground(Void ... args) { |
Michael Jurka | d9cb4a1 | 2013-03-19 12:01:06 +0100 | [diff] [blame] | 383 | SQLiteDatabase db = cacheDb.getWritableDatabase(); |
Michael Jurka | 6e27f64 | 2013-12-10 13:40:30 +0100 | [diff] [blame] | 384 | try { |
| 385 | db.delete(CacheDb.TABLE_NAME, |
| 386 | CacheDb.COLUMN_NAME + " LIKE ? OR " + |
| 387 | CacheDb.COLUMN_NAME + " LIKE ?", // SELECT query |
| 388 | new String[] { |
| 389 | WIDGET_PREFIX + packageName + "/%", |
| 390 | SHORTCUT_PREFIX + packageName + "/%" |
| 391 | } // args to SELECT query |
| 392 | ); |
| 393 | } catch (SQLiteDiskIOException e) { |
| 394 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 395 | synchronized(sInvalidPackages) { |
| 396 | sInvalidPackages.remove(packageName); |
| 397 | } |
| 398 | return null; |
| 399 | } |
| 400 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null); |
| 401 | } |
| 402 | |
Michael Jurka | eb1bb92 | 2013-09-26 11:29:01 -0700 | [diff] [blame] | 403 | public static void removeItemFromDb(final CacheDb cacheDb, final String objectName) { |
| 404 | new AsyncTask<Void, Void, Void>() { |
| 405 | public Void doInBackground(Void ... args) { |
| 406 | SQLiteDatabase db = cacheDb.getWritableDatabase(); |
Michael Jurka | 6e27f64 | 2013-12-10 13:40:30 +0100 | [diff] [blame] | 407 | try { |
| 408 | db.delete(CacheDb.TABLE_NAME, |
| 409 | CacheDb.COLUMN_NAME + " = ? ", // SELECT query |
| 410 | new String[] { objectName }); // args to SELECT query |
| 411 | } catch (SQLiteDiskIOException e) { |
| 412 | } |
Michael Jurka | eb1bb92 | 2013-09-26 11:29:01 -0700 | [diff] [blame] | 413 | return null; |
| 414 | } |
| 415 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null); |
| 416 | } |
| 417 | |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 418 | private Bitmap readFromDb(String name, Bitmap b) { |
| 419 | if (mCachedSelectQuery == null) { |
Michael Jurka | d9cb4a1 | 2013-03-19 12:01:06 +0100 | [diff] [blame] | 420 | mCachedSelectQuery = CacheDb.COLUMN_NAME + " = ? AND " + |
| 421 | CacheDb.COLUMN_SIZE + " = ?"; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 422 | } |
| 423 | SQLiteDatabase db = mDb.getReadableDatabase(); |
Michael Jurka | 6e27f64 | 2013-12-10 13:40:30 +0100 | [diff] [blame] | 424 | Cursor result; |
| 425 | try { |
| 426 | result = db.query(CacheDb.TABLE_NAME, |
| 427 | new String[] { CacheDb.COLUMN_PREVIEW_BITMAP }, // cols to return |
| 428 | mCachedSelectQuery, // select query |
| 429 | new String[] { name, mSize }, // args to select query |
| 430 | null, |
| 431 | null, |
| 432 | null, |
| 433 | null); |
| 434 | } catch (SQLiteDiskIOException e) { |
| 435 | recreateDb(); |
| 436 | return null; |
| 437 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 438 | if (result.getCount() > 0) { |
| 439 | result.moveToFirst(); |
| 440 | byte[] blob = result.getBlob(0); |
| 441 | result.close(); |
| 442 | final BitmapFactory.Options opts = mCachedBitmapFactoryOptions.get(); |
| 443 | opts.inBitmap = b; |
| 444 | opts.inSampleSize = 1; |
Michael Jurka | eb1bb92 | 2013-09-26 11:29:01 -0700 | [diff] [blame] | 445 | try { |
| 446 | return BitmapFactory.decodeByteArray(blob, 0, blob.length, opts); |
| 447 | } catch (IllegalArgumentException e) { |
| 448 | removeItemFromDb(mDb, name); |
| 449 | return null; |
| 450 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 451 | } else { |
| 452 | result.close(); |
| 453 | return null; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | public Bitmap generatePreview(Object info, Bitmap preview) { |
| 458 | if (preview != null && |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 459 | (preview.getWidth() != mPreviewBitmapWidth || |
| 460 | preview.getHeight() != mPreviewBitmapHeight)) { |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 461 | throw new RuntimeException("Improperly sized bitmap passed as argument"); |
| 462 | } |
| 463 | if (info instanceof AppWidgetProviderInfo) { |
| 464 | return generateWidgetPreview((AppWidgetProviderInfo) info, preview); |
| 465 | } else { |
| 466 | return generateShortcutPreview( |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 467 | (ResolveInfo) info, mPreviewBitmapWidth, mPreviewBitmapHeight, preview); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
| 471 | public Bitmap generateWidgetPreview(AppWidgetProviderInfo info, Bitmap preview) { |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 472 | int[] cellSpans = Launcher.getSpanForWidget(mContext, info); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 473 | int maxWidth = maxWidthForWidgetPreview(cellSpans[0]); |
| 474 | int maxHeight = maxHeightForWidgetPreview(cellSpans[1]); |
| 475 | return generateWidgetPreview(info.provider, info.previewImage, info.icon, |
| 476 | cellSpans[0], cellSpans[1], maxWidth, maxHeight, preview, null); |
| 477 | } |
| 478 | |
| 479 | public int maxWidthForWidgetPreview(int spanX) { |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 480 | return Math.min(mPreviewBitmapWidth, |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 481 | mWidgetSpacingLayout.estimateCellWidth(spanX)); |
| 482 | } |
| 483 | |
| 484 | public int maxHeightForWidgetPreview(int spanY) { |
Michael Jurka | 3f4e070 | 2013-02-05 11:21:28 +0100 | [diff] [blame] | 485 | return Math.min(mPreviewBitmapHeight, |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 486 | mWidgetSpacingLayout.estimateCellHeight(spanY)); |
| 487 | } |
| 488 | |
| 489 | public Bitmap generateWidgetPreview(ComponentName provider, int previewImage, |
| 490 | int iconId, int cellHSpan, int cellVSpan, int maxPreviewWidth, int maxPreviewHeight, |
| 491 | Bitmap preview, int[] preScaledWidthOut) { |
| 492 | // Load the preview image if possible |
| 493 | String packageName = provider.getPackageName(); |
| 494 | if (maxPreviewWidth < 0) maxPreviewWidth = Integer.MAX_VALUE; |
| 495 | if (maxPreviewHeight < 0) maxPreviewHeight = Integer.MAX_VALUE; |
| 496 | |
| 497 | Drawable drawable = null; |
| 498 | if (previewImage != 0) { |
Adrian Roos | 65d60e2 | 2014-04-15 21:07:49 +0200 | [diff] [blame^] | 499 | drawable = mutateOnMainThread( |
| 500 | mPackageManager.getDrawable(packageName, previewImage, null)); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 501 | if (drawable == null) { |
| 502 | Log.w(TAG, "Can't load widget preview drawable 0x" + |
| 503 | Integer.toHexString(previewImage) + " for provider: " + provider); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | int previewWidth; |
| 508 | int previewHeight; |
| 509 | Bitmap defaultPreview = null; |
| 510 | boolean widgetPreviewExists = (drawable != null); |
| 511 | if (widgetPreviewExists) { |
| 512 | previewWidth = drawable.getIntrinsicWidth(); |
| 513 | previewHeight = drawable.getIntrinsicHeight(); |
| 514 | } else { |
| 515 | // Generate a preview image if we couldn't load one |
| 516 | if (cellHSpan < 1) cellHSpan = 1; |
| 517 | if (cellVSpan < 1) cellVSpan = 1; |
| 518 | |
Adrian Roos | 65d60e2 | 2014-04-15 21:07:49 +0200 | [diff] [blame^] | 519 | // This Drawable is not directly drawn, so there's no need to mutate it. |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 520 | BitmapDrawable previewDrawable = (BitmapDrawable) mContext.getResources() |
Winson Chung | 6706ed8 | 2013-10-02 11:00:15 -0700 | [diff] [blame] | 521 | .getDrawable(R.drawable.widget_tile); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 522 | final int previewDrawableWidth = previewDrawable |
| 523 | .getIntrinsicWidth(); |
| 524 | final int previewDrawableHeight = previewDrawable |
| 525 | .getIntrinsicHeight(); |
Winson Chung | 45cab39 | 2013-10-02 17:45:32 -0700 | [diff] [blame] | 526 | previewWidth = previewDrawableWidth * cellHSpan; |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 527 | previewHeight = previewDrawableHeight * cellVSpan; |
| 528 | |
Adrian Roos | 5d2704f | 2014-03-18 23:09:12 +0100 | [diff] [blame] | 529 | defaultPreview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 530 | final Canvas c = mCachedAppWidgetPreviewCanvas.get(); |
| 531 | c.setBitmap(defaultPreview); |
Adrian Roos | fa4c799 | 2014-03-19 15:58:14 +0100 | [diff] [blame] | 532 | Paint p = mDefaultAppWidgetPreviewPaint.get(); |
| 533 | if (p == null) { |
| 534 | p = new Paint(); |
| 535 | p.setShader(new BitmapShader(previewDrawable.getBitmap(), |
| 536 | Shader.TileMode.REPEAT, Shader.TileMode.REPEAT)); |
| 537 | mDefaultAppWidgetPreviewPaint.set(p); |
| 538 | } |
| 539 | final Rect dest = mCachedAppWidgetPreviewDestRect.get(); |
| 540 | dest.set(0, 0, previewWidth, previewHeight); |
| 541 | c.drawRect(dest, p); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 542 | c.setBitmap(null); |
| 543 | |
| 544 | // Draw the icon in the top left corner |
| 545 | int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage); |
| 546 | int smallestSide = Math.min(previewWidth, previewHeight); |
| 547 | float iconScale = Math.min((float) smallestSide |
| 548 | / (mAppIconSize + 2 * minOffset), 1f); |
| 549 | |
| 550 | try { |
| 551 | Drawable icon = null; |
| 552 | int hoffset = |
| 553 | (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2); |
| 554 | int yoffset = |
| 555 | (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2); |
| 556 | if (iconId > 0) |
Adrian Roos | 65d60e2 | 2014-04-15 21:07:49 +0200 | [diff] [blame^] | 557 | icon = mutateOnMainThread(mIconCache.getFullResIcon(packageName, iconId)); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 558 | if (icon != null) { |
| 559 | renderDrawableToBitmap(icon, defaultPreview, hoffset, |
| 560 | yoffset, (int) (mAppIconSize * iconScale), |
| 561 | (int) (mAppIconSize * iconScale)); |
| 562 | } |
| 563 | } catch (Resources.NotFoundException e) { |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | // Scale to fit width only - let the widget preview be clipped in the |
| 568 | // vertical dimension |
| 569 | float scale = 1f; |
| 570 | if (preScaledWidthOut != null) { |
| 571 | preScaledWidthOut[0] = previewWidth; |
| 572 | } |
| 573 | if (previewWidth > maxPreviewWidth) { |
| 574 | scale = maxPreviewWidth / (float) previewWidth; |
| 575 | } |
| 576 | if (scale != 1f) { |
| 577 | previewWidth = (int) (scale * previewWidth); |
| 578 | previewHeight = (int) (scale * previewHeight); |
| 579 | } |
| 580 | |
| 581 | // If a bitmap is passed in, we use it; otherwise, we create a bitmap of the right size |
| 582 | if (preview == null) { |
| 583 | preview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888); |
| 584 | } |
| 585 | |
| 586 | // Draw the scaled preview into the final bitmap |
| 587 | int x = (preview.getWidth() - previewWidth) / 2; |
| 588 | if (widgetPreviewExists) { |
| 589 | renderDrawableToBitmap(drawable, preview, x, 0, previewWidth, |
| 590 | previewHeight); |
| 591 | } else { |
| 592 | final Canvas c = mCachedAppWidgetPreviewCanvas.get(); |
| 593 | final Rect src = mCachedAppWidgetPreviewSrcRect.get(); |
| 594 | final Rect dest = mCachedAppWidgetPreviewDestRect.get(); |
| 595 | c.setBitmap(preview); |
| 596 | src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight()); |
Michael Jurka | e5919c5 | 2013-03-06 17:30:10 +0100 | [diff] [blame] | 597 | dest.set(x, 0, x + previewWidth, previewHeight); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 598 | |
| 599 | Paint p = mCachedAppWidgetPreviewPaint.get(); |
| 600 | if (p == null) { |
| 601 | p = new Paint(); |
| 602 | p.setFilterBitmap(true); |
| 603 | mCachedAppWidgetPreviewPaint.set(p); |
| 604 | } |
| 605 | c.drawBitmap(defaultPreview, src, dest, p); |
| 606 | c.setBitmap(null); |
| 607 | } |
| 608 | return preview; |
| 609 | } |
| 610 | |
| 611 | private Bitmap generateShortcutPreview( |
| 612 | ResolveInfo info, int maxWidth, int maxHeight, Bitmap preview) { |
| 613 | Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get(); |
| 614 | final Canvas c = mCachedShortcutPreviewCanvas.get(); |
| 615 | if (tempBitmap == null || |
| 616 | tempBitmap.getWidth() != maxWidth || |
| 617 | tempBitmap.getHeight() != maxHeight) { |
| 618 | tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888); |
| 619 | mCachedShortcutPreviewBitmap.set(tempBitmap); |
| 620 | } else { |
| 621 | c.setBitmap(tempBitmap); |
| 622 | c.drawColor(0, PorterDuff.Mode.CLEAR); |
| 623 | c.setBitmap(null); |
| 624 | } |
| 625 | // Render the icon |
Adrian Roos | 65d60e2 | 2014-04-15 21:07:49 +0200 | [diff] [blame^] | 626 | Drawable icon = mutateOnMainThread(mIconCache.getFullResIcon(info)); |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 627 | |
| 628 | int paddingTop = mContext. |
| 629 | getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top); |
| 630 | int paddingLeft = mContext. |
| 631 | getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left); |
| 632 | int paddingRight = mContext. |
| 633 | getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right); |
| 634 | |
| 635 | int scaledIconWidth = (maxWidth - paddingLeft - paddingRight); |
| 636 | |
| 637 | renderDrawableToBitmap( |
| 638 | icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth); |
| 639 | |
| 640 | if (preview != null && |
| 641 | (preview.getWidth() != maxWidth || preview.getHeight() != maxHeight)) { |
| 642 | throw new RuntimeException("Improperly sized bitmap passed as argument"); |
| 643 | } else if (preview == null) { |
| 644 | preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888); |
| 645 | } |
| 646 | |
| 647 | c.setBitmap(preview); |
| 648 | // Draw a desaturated/scaled version of the icon in the background as a watermark |
| 649 | Paint p = mCachedShortcutPreviewPaint.get(); |
| 650 | if (p == null) { |
| 651 | p = new Paint(); |
| 652 | ColorMatrix colorMatrix = new ColorMatrix(); |
| 653 | colorMatrix.setSaturation(0); |
| 654 | p.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); |
| 655 | p.setAlpha((int) (255 * 0.06f)); |
| 656 | mCachedShortcutPreviewPaint.set(p); |
| 657 | } |
| 658 | c.drawBitmap(tempBitmap, 0, 0, p); |
| 659 | c.setBitmap(null); |
| 660 | |
| 661 | renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize); |
| 662 | |
| 663 | return preview; |
| 664 | } |
| 665 | |
| 666 | |
| 667 | public static void renderDrawableToBitmap( |
| 668 | Drawable d, Bitmap bitmap, int x, int y, int w, int h) { |
| 669 | renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f); |
| 670 | } |
| 671 | |
| 672 | private static void renderDrawableToBitmap( |
| 673 | Drawable d, Bitmap bitmap, int x, int y, int w, int h, |
| 674 | float scale) { |
| 675 | if (bitmap != null) { |
| 676 | Canvas c = new Canvas(bitmap); |
| 677 | c.scale(scale, scale); |
| 678 | Rect oldBounds = d.copyBounds(); |
| 679 | d.setBounds(x, y, x + w, y + h); |
| 680 | d.draw(c); |
| 681 | d.setBounds(oldBounds); // Restore the bounds |
| 682 | c.setBitmap(null); |
| 683 | } |
| 684 | } |
| 685 | |
Adrian Roos | 65d60e2 | 2014-04-15 21:07:49 +0200 | [diff] [blame^] | 686 | private Drawable mutateOnMainThread(final Drawable drawable) { |
| 687 | try { |
| 688 | return mMainThreadExecutor.submit(new Callable<Drawable>() { |
| 689 | @Override |
| 690 | public Drawable call() throws Exception { |
| 691 | return drawable.mutate(); |
| 692 | } |
| 693 | }).get(); |
| 694 | } catch (InterruptedException e) { |
| 695 | Thread.currentThread().interrupt(); |
| 696 | throw new RuntimeException(e); |
| 697 | } catch (ExecutionException e) { |
| 698 | throw new RuntimeException(e); |
| 699 | } |
| 700 | } |
Michael Jurka | 05713af | 2013-01-23 12:39:24 +0100 | [diff] [blame] | 701 | } |