Search in sources :

Example 16 with SQLiteTransaction

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

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) {
            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 17 with SQLiteTransaction

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

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

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

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.numHotseatIcons);
    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.numHotseatIcons);
        Point targetSize = new Point(idp.numColumns, idp.numRows);
        GridSizeMigrationTaskV2 task = new GridSizeMigrationTaskV2(context, t.getDb(), srcReader, destReader, idp.numHotseatIcons, 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.numHotseatIcons).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 19 with SQLiteTransaction

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

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

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

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) {
            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)

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