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