Fix SQLite crashes in Launcher

We were using multiple SQLiteOpenHelpers, which
causes problems; this change switches us to using
only one

Bug: 8346109

Change-Id: If7d4dff3d34edb38d9586d3b0f4e8e1747a71a75
This commit is contained in:
Michael Jurka
2013-03-19 12:01:06 +01:00
parent 5cb3ac0af7
commit d9cb4a124a
4 changed files with 38 additions and 24 deletions
@@ -31,8 +31,9 @@ import com.android.launcher.R;
import java.lang.ref.WeakReference;
public class LauncherApplication extends Application {
public LauncherModel mModel;
public IconCache mIconCache;
private LauncherModel mModel;
private IconCache mIconCache;
private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
private static boolean sIsScreenLarge;
private static float sScreenDensity;
private static int sLongPressTimeout = 300;
@@ -47,6 +48,7 @@ public class LauncherApplication extends Application {
sIsScreenLarge = getResources().getBoolean(R.bool.is_large_screen);
sScreenDensity = getResources().getDisplayMetrics().density;
mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(this);
mIconCache = new IconCache(this);
mModel = new LauncherModel(this, mIconCache);
@@ -114,7 +116,11 @@ public class LauncherApplication extends Application {
return mModel;
}
void setLauncherProvider(LauncherProvider provider) {
WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
return mWidgetPreviewCacheDb;
}
void setLauncherProvider(LauncherProvider provider) {
mLauncherProvider = new WeakReference<LauncherProvider>(provider);
}
+8 -2
View File
@@ -1988,7 +1988,10 @@ public class LauncherModel extends BroadcastReceiver {
for (int i=0; i<N; i++) {
if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
mBgAllAppsList.updatePackage(context, packages[i]);
WidgetPreviewLoader.removeFromDb(context, packages[i]);
LauncherApplication app =
(LauncherApplication) context.getApplicationContext();
WidgetPreviewLoader.removeFromDb(
app.getWidgetPreviewCacheDb(), packages[i]);
}
break;
case OP_REMOVE:
@@ -1996,7 +1999,10 @@ public class LauncherModel extends BroadcastReceiver {
for (int i=0; i<N; i++) {
if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
mBgAllAppsList.removePackage(packages[i]);
WidgetPreviewLoader.removeFromDb(context, packages[i]);
LauncherApplication app =
(LauncherApplication) context.getApplicationContext();
WidgetPreviewLoader.removeFromDb(
app.getWidgetPreviewCacheDb(), packages[i]);
}
break;
}
@@ -13,6 +13,7 @@ public class PackageChangedReceiver extends BroadcastReceiver {
// they sent us a bad intent
return;
}
WidgetPreviewLoader.removeFromDb(context, packageName);
LauncherApplication app = (LauncherApplication) context.getApplicationContext();
WidgetPreviewLoader.removeFromDb(app.getWidgetPreviewCacheDb(), packageName);
}
}
@@ -129,7 +129,7 @@ public class WidgetPreviewLoader {
private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
private WidgetPreviewCacheDb mDb;
private CacheDb mDb;
private HashMap<String, WeakReference<Bitmap>> mLoadedPreviews;
private ArrayList<SoftReference<Bitmap>> mUnusedBitmaps;
@@ -143,8 +143,9 @@ public class WidgetPreviewLoader {
mContext = mLauncher = launcher;
mPackageManager = mContext.getPackageManager();
mAppIconSize = mContext.getResources().getDimensionPixelSize(R.dimen.app_icon_size);
mIconCache = ((LauncherApplication) launcher.getApplicationContext()).getIconCache();
mDb = new WidgetPreviewCacheDb(mContext);
LauncherApplication app = (LauncherApplication) launcher.getApplicationContext();
mIconCache = app.getIconCache();
mDb = app.getWidgetPreviewCacheDb();
mLoadedPreviews = new HashMap<String, WeakReference<Bitmap>>();
mUnusedBitmaps = new ArrayList<SoftReference<Bitmap>>();
}
@@ -253,7 +254,7 @@ public class WidgetPreviewLoader {
}
}
static class WidgetPreviewCacheDb extends SQLiteOpenHelper {
static class CacheDb extends SQLiteOpenHelper {
final static int DB_VERSION = 2;
final static String DB_NAME = "widgetpreviews.db";
final static String TABLE_NAME = "shortcut_and_widget_previews";
@@ -262,7 +263,7 @@ public class WidgetPreviewLoader {
final static String COLUMN_PREVIEW_BITMAP = "preview_bitmap";
Context mContext;
public WidgetPreviewCacheDb(Context context) {
public CacheDb(Context context) {
super(context, new File(context.getCacheDir(), DB_NAME).getPath(), null, DB_VERSION);
// Store the context for later use
mContext = context;
@@ -325,24 +326,24 @@ public class WidgetPreviewLoader {
SQLiteDatabase db = mDb.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(WidgetPreviewCacheDb.COLUMN_NAME, name);
values.put(CacheDb.COLUMN_NAME, name);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
preview.compress(Bitmap.CompressFormat.PNG, 100, stream);
values.put(WidgetPreviewCacheDb.COLUMN_PREVIEW_BITMAP, stream.toByteArray());
values.put(WidgetPreviewCacheDb.COLUMN_SIZE, mSize);
db.insert(WidgetPreviewCacheDb.TABLE_NAME, null, values);
values.put(CacheDb.COLUMN_PREVIEW_BITMAP, stream.toByteArray());
values.put(CacheDb.COLUMN_SIZE, mSize);
db.insert(CacheDb.TABLE_NAME, null, values);
}
public static void removeFromDb(final Context mContext, final String packageName) {
public static void removeFromDb(final CacheDb cacheDb, final String packageName) {
synchronized(sInvalidPackages) {
sInvalidPackages.add(packageName);
}
new AsyncTask<Void, Void, Void>() {
public Void doInBackground(Void ... args) {
SQLiteDatabase db = new WidgetPreviewCacheDb(mContext).getWritableDatabase();
db.delete(WidgetPreviewCacheDb.TABLE_NAME,
WidgetPreviewCacheDb.COLUMN_NAME + " LIKE ? OR " +
WidgetPreviewCacheDb.COLUMN_NAME + " LIKE ?", // SELECT query
SQLiteDatabase db = cacheDb.getWritableDatabase();
db.delete(CacheDb.TABLE_NAME,
CacheDb.COLUMN_NAME + " LIKE ? OR " +
CacheDb.COLUMN_NAME + " LIKE ?", // SELECT query
new String[] {
WIDGET_PREFIX + packageName + "/%",
SHORTCUT_PREFIX + packageName + "/%"} // args to SELECT query
@@ -364,12 +365,12 @@ public class WidgetPreviewLoader {
private Bitmap readFromDb(String name, Bitmap b) {
if (mCachedSelectQuery == null) {
mCachedSelectQuery = WidgetPreviewCacheDb.COLUMN_NAME + " = ? AND " +
WidgetPreviewCacheDb.COLUMN_SIZE + " = ?";
mCachedSelectQuery = CacheDb.COLUMN_NAME + " = ? AND " +
CacheDb.COLUMN_SIZE + " = ?";
}
SQLiteDatabase db = mDb.getReadableDatabase();
Cursor result = db.query(WidgetPreviewCacheDb.TABLE_NAME,
new String[] { WidgetPreviewCacheDb.COLUMN_PREVIEW_BITMAP }, // cols to return
Cursor result = db.query(CacheDb.TABLE_NAME,
new String[] { CacheDb.COLUMN_PREVIEW_BITMAP }, // cols to return
mCachedSelectQuery, // select query
new String[] { name, mSize }, // args to select query
null,