Using transaction when dropping tables, so that the DB never enters

an inconsistant state

Bug: 34720697
Change-Id: I55a26d63be6c06622da6ee3395bf1990f1a58a11
This commit is contained in:
Sunny Goyal
2017-02-23 18:30:22 -08:00
parent ea53ca3a17
commit e05b08f705
3 changed files with 13 additions and 20 deletions
+1 -1
View File
@@ -815,7 +815,7 @@ public class LauncherModel extends BroadcastReceiver
if (clearDb) {
Log.d(TAG, "loadWorkspace: resetting launcher database");
LauncherSettings.Settings.call(contentResolver,
LauncherSettings.Settings.METHOD_DELETE_DB);
LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
}
Log.d(TAG, "loadWorkspace: loading default favorites");
+12 -18
View File
@@ -406,18 +406,13 @@ public class LauncherProvider extends ContentProvider {
return result;
}
case LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB: {
createEmptyDB();
mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
return null;
}
case LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES: {
loadDefaultFavoritesIfNecessary();
return null;
}
case LauncherSettings.Settings.METHOD_DELETE_DB: {
// Are you sure? (y/n)
mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
return null;
}
}
return null;
}
@@ -469,13 +464,6 @@ public class LauncherProvider extends ContentProvider {
values.put(LauncherSettings.ChangeLogColumns.MODIFIED, System.currentTimeMillis());
}
/**
* Clears all the data for a fresh start.
*/
synchronized private void createEmptyDB() {
mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
}
private void clearFlagEmptyDbCreated() {
Utilities.getPrefs(getContext()).edit().remove(EMPTY_DATABASE_CREATED).commit();
}
@@ -518,12 +506,12 @@ public class LauncherProvider extends ContentProvider {
// There might be some partially restored DB items, due to buggy restore logic in
// previous versions of launcher.
createEmptyDB();
mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
// Populate favorites table with initial favorites
if ((mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), loader) <= 0)
&& usingExternallyProvidedLayout) {
// Unable to load external layout. Cleanup and load the internal layout.
createEmptyDB();
mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(),
getDefaultLayoutParser(widgetHost));
}
@@ -825,9 +813,15 @@ public class LauncherProvider extends ContentProvider {
* Clears all the data for a fresh start.
*/
public void createEmptyDB(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + Favorites.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + WorkspaceScreens.TABLE_NAME);
onCreate(db);
db.beginTransaction();
try {
db.execSQL("DROP TABLE IF EXISTS " + Favorites.TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + WorkspaceScreens.TABLE_NAME);
onCreate(db);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
/**
@@ -291,7 +291,6 @@ public class LauncherSettings {
public static final String METHOD_NEW_SCREEN_ID = "generate_new_screen_id";
public static final String METHOD_CREATE_EMPTY_DB = "create_empty_db";
public static final String METHOD_DELETE_DB = "delete_db";
public static final String METHOD_LOAD_DEFAULT_FAVORITES = "load_default_favorites";