Search in sources :

Example 6 with SQLiteTransaction

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

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.numHotseatIcons);
        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.numHotseatIcons && new GridSizeMigrationTask(context, transaction.getDb(), validPackages, migrateForPreview, srcHotseatCount, idp.numHotseatIcons).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.numHotseatIcons).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 7 with SQLiteTransaction

use of com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction in project Neo-Launcher by NeoApplications.

the class LauncherProvider method applyBatch.

@TargetApi(Build.VERSION_CODES.M)
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
    createDbIfNotExists();
    try (SQLiteTransaction t = new SQLiteTransaction(mOpenHelper.getWritableDatabase())) {
        boolean isAddOrDelete = false;
        final int numOperations = operations.size();
        final ContentProviderResult[] results = new ContentProviderResult[numOperations];
        for (int i = 0; i < numOperations; i++) {
            ContentProviderOperation op = operations.get(i);
            results[i] = op.apply(this, results, i);
            isAddOrDelete |= (op.isInsert() || op.isDelete()) && results[i].count != null && results[i].count > 0;
        }
        if (isAddOrDelete) {
            mOpenHelper.onAddOrDeleteOp(t.getDb());
        }
        t.commit();
        reloadLauncherIfExternal();
        return results;
    }
}
Also used : ContentProviderResult(android.content.ContentProviderResult) ContentProviderOperation(android.content.ContentProviderOperation) SQLiteTransaction(com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction) TargetApi(android.annotation.TargetApi)

Example 8 with SQLiteTransaction

use of com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction in project Neo-Launcher by NeoApplications.

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

use of com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction in project Neo-Launcher by NeoApplications.

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)

Example 10 with SQLiteTransaction

use of com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction in project Neo-Launcher by NeoApplications.

the class GridSizeMigrationTask method migrateGridIfNeeded.

/**
 * Migrates the workspace and hotseat in case their sizes changed.
 *
 * @return false if the migration failed.
 */
public static boolean migrateGridIfNeeded(Context context) {
    SharedPreferences prefs = Utilities.getPrefs(context);
    InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
    String gridSizeString = getPointString(idp.numColumns, idp.numRows);
    if (gridSizeString.equals(prefs.getString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, "")) && idp.numHotseatIcons == prefs.getInt(KEY_MIGRATION_SRC_HOTSEAT_COUNT, idp.numHotseatIcons)) {
        // Skip if workspace and hotseat sizes have not changed.
        return true;
    }
    long migrationStartTime = System.currentTimeMillis();
    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.numHotseatIcons);
        Point sourceSize = parsePoint(prefs.getString(KEY_MIGRATION_SRC_WORKSPACE_SIZE, gridSizeString));
        boolean dbChanged = false;
        GridBackupTable backupTable = new GridBackupTable(context, transaction.getDb(), srcHotseatCount, sourceSize.x, sourceSize.y);
        if (backupTable.backupOrRestoreAsNeeded()) {
            dbChanged = true;
            srcHotseatCount = backupTable.getRestoreHotseatAndGridSize(sourceSize);
        }
        HashSet<String> validPackages = getValidPackages(context);
        // Hotseat
        if (srcHotseatCount != idp.numHotseatIcons) {
            // Migrate hotseat.
            dbChanged = new GridSizeMigrationTask(context, transaction.getDb(), validPackages, srcHotseatCount, idp.numHotseatIcons).migrateHotseat();
        }
        // Grid size
        Point targetSize = new Point(idp.numColumns, idp.numRows);
        if (new MultiStepMigrationTask(validPackages, context, transaction.getDb()).migrate(sourceSize, targetSize)) {
            dbChanged = true;
        }
        if (dbChanged) {
            // Make sure we haven't removed everything.
            final Cursor c = context.getContentResolver().query(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();
        Settings.call(context.getContentResolver(), Settings.METHOD_REFRESH_BACKUP_TABLE);
        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));
        // 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.numHotseatIcons).apply();
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) InvariantDeviceProfile(com.android.launcher3.InvariantDeviceProfile) 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)

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