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