Search in sources :

Example 1 with SQLiteTransaction

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

the class GridSizeMigrationTask method migrateGridIfNeeded.

/**
 * Run the migration algorithm if needed. For preview, we provide the intended idp because it
 * has not been changed. If idp is null, we read it from the context, for actual grid migration.
 *
 * @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);
    long migrationStartTime = SystemClock.elapsedRealtime();
    try (SQLiteTransaction transaction = (SQLiteTransaction) Settings.call(context.getContentResolver(), Settings.METHOD_NEW_TRANSACTION).getBinder(Settings.EXTRA_VALUE)) {
        int srcHotseatCount = prefs.getInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, idp.numDatabaseHotseatIcons);
        Point sourceSize = parsePoint(prefs.getString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, gridSizeString));
        boolean dbChanged = false;
        if (migrateForPreview) {
            copyTable(transaction.getDb(), Favorites.TABLE_NAME, transaction.getDb(), Favorites.PREVIEW_TABLE_NAME, context);
        }
        GridBackupTable backupTable = new GridBackupTable(context, transaction.getDb(), srcHotseatCount, sourceSize.x, sourceSize.y);
        if (migrateForPreview ? backupTable.restoreToPreviewIfBackupExists() : backupTable.backupOrRestoreAsNeeded()) {
            dbChanged = true;
            srcHotseatCount = backupTable.getRestoreHotseatAndGridSize(sourceSize);
        }
        HashSet<String> validPackages = getValidPackages(context);
        // Hotseat.
        if (srcHotseatCount != idp.numDatabaseHotseatIcons && new GridSizeMigrationTask(context, transaction.getDb(), validPackages, migrateForPreview, srcHotseatCount, idp.numDatabaseHotseatIcons).migrateHotseat()) {
            dbChanged = true;
        }
        // Grid size
        Point targetSize = new Point(idp.numColumns, idp.numRows);
        if (new MultiStepMigrationTask(validPackages, context, transaction.getDb(), migrateForPreview).migrate(sourceSize, targetSize)) {
            dbChanged = true;
        }
        if (dbChanged) {
            // Make sure we haven't removed everything.
            final Cursor c = context.getContentResolver().query(migrateForPreview ? Favorites.PREVIEW_CONTENT_URI : Favorites.CONTENT_URI, null, null, null, null);
            boolean hasData = c.moveToNext();
            c.close();
            if (!hasData) {
                throw new Exception("Removed every thing during grid resize");
            }
        }
        transaction.commit();
        if (!migrateForPreview) {
            Settings.call(context.getContentResolver(), Settings.METHOD_REFRESH_BACKUP_TABLE);
        }
        return true;
    } catch (Exception e) {
        Log.e(TAG, "Error during preview grid migration", e);
        return false;
    } finally {
        Log.v(TAG, "Preview workspace migration completed in " + (SystemClock.elapsedRealtime() - 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) Utilities.parsePoint(com.android.launcher3.Utilities.parsePoint) Point(android.graphics.Point) Cursor(android.database.Cursor) Utilities.parsePoint(com.android.launcher3.Utilities.parsePoint) Point(android.graphics.Point)

Example 2 with SQLiteTransaction

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

the class LauncherProvider method bulkInsert.

@Override
public int bulkInsert(Uri uri, ContentValues[] values) {
    createDbIfNotExists();
    SqlArguments args = new SqlArguments(uri);
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    try (SQLiteTransaction t = new SQLiteTransaction(db)) {
        int numValues = values.length;
        for (int i = 0; i < numValues; i++) {
            addModifiedTime(values[i]);
            if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) {
                return 0;
            }
        }
        onAddOrDeleteOp(db);
        t.commit();
    }
    reloadLauncherIfExternal();
    return values.length;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteTransaction(com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction)

Example 3 with SQLiteTransaction

use of com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction in project android_packages_apps_Trebuchet by LineageOS.

the class LauncherProvider method deleteEmptyFolders.

/**
 * Deletes any empty folder from the DB.
 * @return Ids of deleted folders.
 */
private IntArray deleteEmptyFolders() {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    try (SQLiteTransaction t = new SQLiteTransaction(db)) {
        // Select folders whose id do not match any container value.
        String selection = LauncherSettings.Favorites.ITEM_TYPE + " = " + LauncherSettings.Favorites.ITEM_TYPE_FOLDER + " AND " + LauncherSettings.Favorites._ID + " NOT IN (SELECT " + LauncherSettings.Favorites.CONTAINER + " FROM " + Favorites.TABLE_NAME + ")";
        IntArray folderIds = LauncherDbUtils.queryIntArray(db, Favorites.TABLE_NAME, Favorites._ID, selection, null, null);
        if (!folderIds.isEmpty()) {
            db.delete(Favorites.TABLE_NAME, Utilities.createDbSelectionQuery(LauncherSettings.Favorites._ID, folderIds), null);
        }
        t.commit();
        return folderIds;
    } catch (SQLException ex) {
        Log.e(TAG, ex.getMessage(), ex);
        return new IntArray();
    }
}
Also used : IntArray(com.android.launcher3.util.IntArray) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLException(android.database.SQLException) SQLiteTransaction(com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction)

Example 4 with SQLiteTransaction

use of com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction in project android_packages_apps_Trebuchet by LineageOS.

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 5 with SQLiteTransaction

use of com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction in project android_packages_apps_Trebuchet by LineageOS.

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