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