Search in sources :

Example 11 with SQLiteTransaction

use of com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction in project android_packages_apps_Launcher3 by crdroidandroid.

the class GridSizeMigrationTask method removeBrokenHotseatItems.

/**
 * Removes any broken item from the hotseat.
 *
 * @return a map with occupied hotseat position set to non-null value.
 */
public static IntSparseArrayMap<Object> removeBrokenHotseatItems(Context context) throws Exception {
    try (SQLiteTransaction transaction = (SQLiteTransaction) Settings.call(context.getContentResolver(), Settings.METHOD_NEW_TRANSACTION).getBinder(Settings.EXTRA_VALUE)) {
        GridSizeMigrationTask task = new GridSizeMigrationTask(context, transaction.getDb(), getValidPackages(context), false, /* usePreviewTable */
        Integer.MAX_VALUE, Integer.MAX_VALUE);
        // Load all the valid entries
        ArrayList<DbEntry> items = task.loadHotseatEntries();
        // Delete any entry marked for deletion by above load.
        task.applyOperations();
        IntSparseArrayMap<Object> positions = new IntSparseArrayMap<>();
        for (DbEntry item : items) {
            positions.put(item.screenId, item);
        }
        transaction.commit();
        return positions;
    }
}
Also used : IntSparseArrayMap(com.android.launcher3.util.IntSparseArrayMap) SQLiteTransaction(com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction)

Example 12 with SQLiteTransaction

use of com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction in project android_packages_apps_Launcher3 by crdroidandroid.

the class GridSizeMigrationTaskV2 method migrateGridIfNeeded.

/**
 * When migrating the grid for preview, we copy the table
 * {@link LauncherSettings.Favorites.TABLE_NAME} into
 * {@link LauncherSettings.Favorites.PREVIEW_TABLE_NAME}, run grid size migration from the
 * former to the later, then use the later table for preview.
 *
 * Similarly when doing the actual grid migration, the former grid option's table
 * {@link LauncherSettings.Favorites.TABLE_NAME} is copied into the new grid option's
 * {@link LauncherSettings.Favorites.TMP_TABLE}, we then run the grid size migration algorithm
 * to migrate the later to the former, and load the workspace from the default
 * {@link LauncherSettings.Favorites.TABLE_NAME}.
 *
 * @return false if the migration failed.
 */
public static boolean migrateGridIfNeeded(Context context, InvariantDeviceProfile idp) {
    boolean migrateForPreview = idp != null;
    if (!migrateForPreview) {
        idp = LauncherAppState.getIDP(context);
    }
    if (!needsToMigrate(context, idp)) {
        return true;
    }
    SharedPreferences prefs = Utilities.getPrefs(context);
    String gridSizeString = getPointString(idp.numColumns, idp.numRows);
    HashSet<String> validPackages = getValidPackages(context);
    int srcHotseatCount = prefs.getInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, idp.numDatabaseHotseatIcons);
    if (migrateForPreview) {
        if (!LauncherSettings.Settings.call(context.getContentResolver(), LauncherSettings.Settings.METHOD_PREP_FOR_PREVIEW, idp.dbFile).getBoolean(LauncherSettings.Settings.EXTRA_VALUE)) {
            return false;
        }
    } else if (!LauncherSettings.Settings.call(context.getContentResolver(), LauncherSettings.Settings.METHOD_UPDATE_CURRENT_OPEN_HELPER).getBoolean(LauncherSettings.Settings.EXTRA_VALUE)) {
        return false;
    }
    long migrationStartTime = System.currentTimeMillis();
    try (SQLiteTransaction t = (SQLiteTransaction) LauncherSettings.Settings.call(context.getContentResolver(), LauncherSettings.Settings.METHOD_NEW_TRANSACTION).getBinder(LauncherSettings.Settings.EXTRA_VALUE)) {
        DbReader srcReader = new DbReader(t.getDb(), migrateForPreview ? LauncherSettings.Favorites.TABLE_NAME : LauncherSettings.Favorites.TMP_TABLE, context, validPackages, srcHotseatCount);
        DbReader destReader = new DbReader(t.getDb(), migrateForPreview ? LauncherSettings.Favorites.PREVIEW_TABLE_NAME : LauncherSettings.Favorites.TABLE_NAME, context, validPackages, idp.numDatabaseHotseatIcons);
        Point targetSize = new Point(idp.numColumns, idp.numRows);
        GridSizeMigrationTaskV2 task = new GridSizeMigrationTaskV2(context, t.getDb(), srcReader, destReader, idp.numDatabaseHotseatIcons, targetSize);
        task.migrate();
        if (!migrateForPreview) {
            dropTable(t.getDb(), LauncherSettings.Favorites.TMP_TABLE);
        }
        t.commit();
        return true;
    } catch (Exception e) {
        Log.e(TAG, "Error during grid migration", e);
        return false;
    } finally {
        Log.v(TAG, "Workspace migration completed in " + (System.currentTimeMillis() - migrationStartTime));
        if (!migrateForPreview) {
            // Save current configuration, so that the migration does not run again.
            prefs.edit().putString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, gridSizeString).putInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, idp.numDatabaseHotseatIcons).apply();
        }
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) SQLiteTransaction(com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction) Utilities.getPointString(com.android.launcher3.Utilities.getPointString) Point(android.graphics.Point) Point(android.graphics.Point)

Example 13 with SQLiteTransaction

use of com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction in project android_packages_apps_Launcher3 by crdroidandroid.

the class RestoreDbTask method restoreIfPossible.

/**
 * Restore the workspace if backup is available.
 */
public static boolean restoreIfPossible(@NonNull Context context, @NonNull DatabaseHelper helper, @NonNull BackupManager backupManager) {
    final SQLiteDatabase db = helper.getWritableDatabase();
    try (SQLiteTransaction t = new SQLiteTransaction(db)) {
        RestoreDbTask task = new RestoreDbTask();
        task.restoreWorkspace(context, db, helper, backupManager);
        t.commit();
        return true;
    } catch (Exception e) {
        FileLog.e(TAG, "Failed to restore db", e);
        return false;
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteTransaction(com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction) InvalidObjectException(java.io.InvalidObjectException)

Example 14 with SQLiteTransaction

use of com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction in project android_packages_apps_Launcher3 by crdroidandroid.

the class RestoreDbTask method performRestore.

public static boolean performRestore(Context context, DatabaseHelper helper, BackupManager backupManager) {
    SQLiteDatabase db = helper.getWritableDatabase();
    try (SQLiteTransaction t = new SQLiteTransaction(db)) {
        RestoreDbTask task = new RestoreDbTask();
        task.backupWorkspace(context, db);
        task.sanitizeDB(helper, db, backupManager);
        task.restoreAppWidgetIdsIfExists(context);
        t.commit();
        return true;
    } catch (Exception e) {
        FileLog.e(TAG, "Failed to verify db", e);
        return false;
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteTransaction(com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction) InvalidObjectException(java.io.InvalidObjectException)

Example 15 with SQLiteTransaction

use of com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction in project android_packages_apps_Launcher3 by crdroidandroid.

the class DbDowngradeHelper method onDowngrade.

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    ArrayList<String> allCommands = new ArrayList<>();
    for (int i = oldVersion - 1; i >= newVersion; i--) {
        String[] commands = mStatements.get(i);
        if (commands == null) {
            throw new SQLiteException("Downgrade path not supported to version " + i);
        }
        Collections.addAll(allCommands, commands);
    }
    try (SQLiteTransaction t = new SQLiteTransaction(db)) {
        for (String sql : allCommands) {
            db.execSQL(sql);
        }
        t.commit();
    }
}
Also used : SQLiteTransaction(com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction) ArrayList(java.util.ArrayList) SQLiteException(android.database.sqlite.SQLiteException)

Aggregations

SQLiteTransaction (com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction)26 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)11 SharedPreferences (android.content.SharedPreferences)6 Point (android.graphics.Point)6 Utilities.getPointString (com.android.launcher3.Utilities.getPointString)5 InvalidObjectException (java.io.InvalidObjectException)5 TargetApi (android.annotation.TargetApi)3 ContentProviderOperation (android.content.ContentProviderOperation)3 ContentProviderResult (android.content.ContentProviderResult)3 Cursor (android.database.Cursor)3 SQLException (android.database.SQLException)3 SQLiteException (android.database.sqlite.SQLiteException)3 Utilities.parsePoint (com.android.launcher3.Utilities.parsePoint)3 IntArray (com.android.launcher3.util.IntArray)3 IntSparseArrayMap (com.android.launcher3.util.IntSparseArrayMap)3 ArrayList (java.util.ArrayList)3 InvariantDeviceProfile (com.android.launcher3.InvariantDeviceProfile)2 SuppressLint (android.annotation.SuppressLint)1