Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bca4e694 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Revert "Revert "Revert "Revert "Changing GridMigrationTask to use ModelDbController directly""""

This reverts commit 3772b246.

Reason for revert: Fixed the test failure

Change-Id: Ibdc9e184fcb32c7caa4ab25d8753a46fa322b703
parent fb085e63
Loading
Loading
Loading
Loading
+4 −4
Original line number Original line Diff line number Diff line
@@ -16,8 +16,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import androidx.annotation.WorkerThread;


import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.model.DatabaseHelper;
import com.android.launcher3.model.LoaderTask;
import com.android.launcher3.model.LoaderTask;
import com.android.launcher3.model.ModelDbController;
import com.android.launcher3.model.WidgetsModel;
import com.android.launcher3.model.WidgetsModel;
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
import com.android.launcher3.pm.UserCache;
import com.android.launcher3.pm.UserCache;
@@ -52,7 +52,7 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
     * Updates the app widgets whose id has changed during the restore process.
     * Updates the app widgets whose id has changed during the restore process.
     */
     */
    @WorkerThread
    @WorkerThread
    public static void restoreAppWidgetIds(Context context, DatabaseHelper helper,
    public static void restoreAppWidgetIds(Context context, ModelDbController controller,
            int[] oldWidgetIds, int[] newWidgetIds, @NonNull AppWidgetHost host) {
            int[] oldWidgetIds, int[] newWidgetIds, @NonNull AppWidgetHost host) {
        if (WidgetsModel.GO_DISABLE_WIDGETS) {
        if (WidgetsModel.GO_DISABLE_WIDGETS) {
            Log.e(TAG, "Skipping widget ID remap as widgets not supported");
            Log.e(TAG, "Skipping widget ID remap as widgets not supported");
@@ -92,12 +92,12 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
            final String where = "appWidgetId=? and (restored & 1) = 1 and profileId=?";
            final String where = "appWidgetId=? and (restored & 1) = 1 and profileId=?";
            final String[] args = new String[] { oldWidgetId, Long.toString(mainProfileId) };
            final String[] args = new String[] { oldWidgetId, Long.toString(mainProfileId) };
            int result = new ContentWriter(context,
            int result = new ContentWriter(context,
                            new ContentWriter.CommitParams(helper, where, args))
                            new ContentWriter.CommitParams(controller, where, args))
                    .put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i])
                    .put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i])
                    .put(LauncherSettings.Favorites.RESTORED, state)
                    .put(LauncherSettings.Favorites.RESTORED, state)
                    .commit();
                    .commit();
            if (result == 0) {
            if (result == 0) {
                Cursor cursor = helper.getWritableDatabase().query(
                Cursor cursor = controller.getDb().query(
                        Favorites.TABLE_NAME,
                        Favorites.TABLE_NAME,
                        new String[] {Favorites.APPWIDGET_ID},
                        new String[] {Favorites.APPWIDGET_ID},
                        "appWidgetId=?", new String[] { oldWidgetId }, null, null, null);
                        "appWidgetId=?", new String[] { oldWidgetId }, null, null, null);
+0 −12
Original line number Original line Diff line number Diff line
@@ -263,18 +263,6 @@ public class LauncherProvider extends ContentProvider {
                getModelDbController().refreshHotseatRestoreTable();
                getModelDbController().refreshHotseatRestoreTable();
                return null;
                return null;
            }
            }
            case LauncherSettings.Settings.METHOD_UPDATE_CURRENT_OPEN_HELPER: {
                Bundle result = new Bundle();
                result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE,
                        getModelDbController().updateCurrentOpenHelper(arg /* dbFile */));
                return result;
            }
            case LauncherSettings.Settings.METHOD_PREP_FOR_PREVIEW: {
                Bundle result = new Bundle();
                result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE,
                        getModelDbController().prepareForPreview(arg /* dbFile */));
                return result;
            }
        }
        }
        return null;
        return null;
    }
    }
+1 −25
Original line number Original line Diff line number Diff line
@@ -147,11 +147,6 @@ public class LauncherSettings {
         */
         */
        public static final String HYBRID_HOTSEAT_BACKUP_TABLE = "hotseat_restore_backup";
        public static final String HYBRID_HOTSEAT_BACKUP_TABLE = "hotseat_restore_backup";


        /**
         * Temporary table used specifically for grid migrations during wallpaper preview
         */
        public static final String PREVIEW_TABLE_NAME = "favorites_preview";

        /**
        /**
         * Temporary table used specifically for multi-db grid migrations
         * Temporary table used specifically for multi-db grid migrations
         */
         */
@@ -163,18 +158,6 @@ public class LauncherSettings {
        public static final Uri CONTENT_URI = Uri.parse("content://"
        public static final Uri CONTENT_URI = Uri.parse("content://"
                + LauncherProvider.AUTHORITY + "/" + TABLE_NAME);
                + LauncherProvider.AUTHORITY + "/" + TABLE_NAME);


        /**
         * The content:// style URL for "favorites_preview" table
         */
        public static final Uri PREVIEW_CONTENT_URI = Uri.parse("content://"
                + LauncherProvider.AUTHORITY + "/" + PREVIEW_TABLE_NAME);

        /**
         * The content:// style URL for "favorites_tmp" table
         */
        public static final Uri TMP_CONTENT_URI = Uri.parse("content://"
                + LauncherProvider.AUTHORITY + "/" + TMP_TABLE);

        /**
        /**
         * The content:// style URL for a given row, identified by its id.
         * The content:// style URL for a given row, identified by its id.
         *
         *
@@ -376,10 +359,6 @@ public class LauncherSettings {


        public static final String METHOD_REFRESH_HOTSEAT_RESTORE_TABLE = "restore_hotseat_table";
        public static final String METHOD_REFRESH_HOTSEAT_RESTORE_TABLE = "restore_hotseat_table";


        public static final String METHOD_UPDATE_CURRENT_OPEN_HELPER = "update_current_open_helper";

        public static final String METHOD_PREP_FOR_PREVIEW = "prep_for_preview";

        public static final String EXTRA_VALUE = "value";
        public static final String EXTRA_VALUE = "value";


        public static final String EXTRA_DB_NAME = "db_name";
        public static final String EXTRA_DB_NAME = "db_name";
@@ -393,11 +372,8 @@ public class LauncherSettings {
        }
        }


        public static Bundle call(ContentResolver cr, String method, String arg) {
        public static Bundle call(ContentResolver cr, String method, String arg) {
            return call(cr, method, arg, null /* extras */);
            return cr.call(CONTENT_URI, method, arg, null);
        }
        }


        public static Bundle call(ContentResolver cr, String method, String arg, Bundle extras) {
            return cr.call(CONTENT_URI, method, arg, extras);
        }
    }
    }
}
}
+20 −14
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package com.android.launcher3.graphics;
package com.android.launcher3.graphics;


import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME;
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;


@@ -52,6 +53,8 @@ import com.android.launcher3.graphics.LauncherPreviewRenderer.PreviewContext;
import com.android.launcher3.model.BgDataModel;
import com.android.launcher3.model.BgDataModel;
import com.android.launcher3.model.GridSizeMigrationUtil;
import com.android.launcher3.model.GridSizeMigrationUtil;
import com.android.launcher3.model.LoaderTask;
import com.android.launcher3.model.LoaderTask;
import com.android.launcher3.model.ModelDbController;
import com.android.launcher3.provider.LauncherDbUtils;
import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.RunnableList;
import com.android.launcher3.util.RunnableList;
import com.android.launcher3.util.Themes;
import com.android.launcher3.util.Themes;
@@ -145,7 +148,9 @@ public class PreviewSurfaceRenderer {
        final String query = LauncherSettings.Favorites.ITEM_TYPE + " = "
        final String query = LauncherSettings.Favorites.ITEM_TYPE + " = "
                + LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
                + LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;


        try (Cursor c = context.getContentResolver().query(LauncherSettings.Favorites.CONTENT_URI,
        ModelDbController mainController =
                LauncherAppState.getInstance(mContext).getModel().getModelDbController();
        try (Cursor c = mainController.query(TABLE_NAME,
                new String[] {
                new String[] {
                        LauncherSettings.Favorites.APPWIDGET_ID,
                        LauncherSettings.Favorites.APPWIDGET_ID,
                        LauncherSettings.Favorites.SPANX,
                        LauncherSettings.Favorites.SPANX,
@@ -190,8 +195,6 @@ public class PreviewSurfaceRenderer {


    @WorkerThread
    @WorkerThread
    private void loadModelData() {
    private void loadModelData() {
        final boolean migrated = doGridMigrationIfNecessary();

        final Context inflationContext;
        final Context inflationContext;
        if (mWallpaperColors != null) {
        if (mWallpaperColors != null) {
            // Create a themed context, without affecting the main application context
            // Create a themed context, without affecting the main application context
@@ -209,8 +212,20 @@ public class PreviewSurfaceRenderer {
                    Themes.getActivityThemeRes(mContext));
                    Themes.getActivityThemeRes(mContext));
        }
        }


        if (migrated) {
        if (GridSizeMigrationUtil.needsToMigrate(inflationContext, mIdp)) {
            // Start the migration
            PreviewContext previewContext = new PreviewContext(inflationContext, mIdp);
            PreviewContext previewContext = new PreviewContext(inflationContext, mIdp);
            // Copy existing data to preview DB
            LauncherDbUtils.copyTable(LauncherAppState.getInstance(mContext)
                    .getModel().getModelDbController().getDb(),
                    TABLE_NAME,
                    LauncherAppState.getInstance(previewContext)
                            .getModel().getModelDbController().getDb(),
                    TABLE_NAME,
                    mContext);
            LauncherAppState.getInstance(previewContext)
                    .getModel().getModelDbController().clearEmptyDbFlag();

            new LoaderTask(
            new LoaderTask(
                    LauncherAppState.getInstance(previewContext),
                    LauncherAppState.getInstance(previewContext),
                    /* bgAllAppsList= */ null,
                    /* bgAllAppsList= */ null,
@@ -229,8 +244,7 @@ public class PreviewSurfaceRenderer {
                        query += " or " + LauncherSettings.Favorites.SCREEN + " = "
                        query += " or " + LauncherSettings.Favorites.SCREEN + " = "
                                + Workspace.SECOND_SCREEN_ID;
                                + Workspace.SECOND_SCREEN_ID;
                    }
                    }
                    loadWorkspaceForPreviewSurfaceRenderer(new ArrayList<>(),
                    loadWorkspace(new ArrayList<>(), query, null);
                            LauncherSettings.Favorites.PREVIEW_CONTENT_URI, query);


                    final SparseArray<Size> spanInfo =
                    final SparseArray<Size> spanInfo =
                            getLoadedLauncherWidgetInfo(previewContext.getBaseContext());
                            getLoadedLauncherWidgetInfo(previewContext.getBaseContext());
@@ -253,14 +267,6 @@ public class PreviewSurfaceRenderer {
        }
        }
    }
    }


    @WorkerThread
    private boolean doGridMigrationIfNecessary() {
        if (!GridSizeMigrationUtil.needsToMigrate(mContext, mIdp)) {
            return false;
        }
        return GridSizeMigrationUtil.migrateGridIfNeeded(mContext, mIdp);
    }

    @UiThread
    @UiThread
    private void renderView(Context inflationContext, BgDataModel dataModel,
    private void renderView(Context inflationContext, BgDataModel dataModel,
            Map<ComponentKey, AppWidgetProviderInfo> widgetProviderInfoMap,
            Map<ComponentKey, AppWidgetProviderInfo> widgetProviderInfoMap,
+13 −69
Original line number Original line Diff line number Diff line
@@ -15,8 +15,8 @@
 */
 */
package com.android.launcher3.model;
package com.android.launcher3.model;


import static com.android.launcher3.LauncherSettings.Favorites.addTableToDb;
import static com.android.launcher3.provider.LauncherDbUtils.dropTable;
import static com.android.launcher3.provider.LauncherDbUtils.dropTable;
import static com.android.launcher3.provider.LauncherDbUtils.tableExists;


import android.content.ContentValues;
import android.content.ContentValues;
import android.content.Context;
import android.content.Context;
@@ -36,9 +36,6 @@ import androidx.annotation.NonNull;


import com.android.launcher3.AutoInstallsLayout;
import com.android.launcher3.AutoInstallsLayout;
import com.android.launcher3.AutoInstallsLayout.LayoutParserCallback;
import com.android.launcher3.AutoInstallsLayout.LayoutParserCallback;
import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.LauncherFiles;
import com.android.launcher3.LauncherPrefs;
import com.android.launcher3.LauncherSettings;
import com.android.launcher3.LauncherSettings;
import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.Utilities;
import com.android.launcher3.Utilities;
@@ -58,6 +55,7 @@ import java.io.File;
import java.net.URISyntaxException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Locale;
import java.util.Locale;
import java.util.function.ToLongFunction;
import java.util.stream.Collectors;
import java.util.stream.Collectors;


/**
/**
@@ -76,45 +74,23 @@ public class DatabaseHelper extends NoLocaleSQLiteHelper implements
    private static final boolean LOGD = false;
    private static final boolean LOGD = false;


    private static final String DOWNGRADE_SCHEMA_FILE = "downgrade_schema.json";
    private static final String DOWNGRADE_SCHEMA_FILE = "downgrade_schema.json";
    public static final String EMPTY_DATABASE_CREATED = "EMPTY_DATABASE_CREATED";


    private final Context mContext;
    private final Context mContext;
    private final boolean mForMigration;
    private final ToLongFunction<UserHandle> mUserSerialProvider;
    private final Runnable mOnEmptyDbCreateCallback;

    private int mMaxItemId = -1;
    private int mMaxItemId = -1;
    public boolean mHotseatRestoreTableExists;
    public boolean mHotseatRestoreTableExists;


    public static DatabaseHelper createDatabaseHelper(Context context, boolean forMigration) {
        return createDatabaseHelper(context, null, forMigration);
    }

    public static DatabaseHelper createDatabaseHelper(Context context, String dbName,
            boolean forMigration) {
        if (dbName == null) {
            dbName = InvariantDeviceProfile.INSTANCE.get(context).dbFile;
        }
        DatabaseHelper databaseHelper = new DatabaseHelper(context, dbName, forMigration);
        // Table creation sometimes fails silently, which leads to a crash loop.
        // This way, we will try to create a table every time after crash, so the device
        // would eventually be able to recover.
        if (!tableExists(databaseHelper.getReadableDatabase(), Favorites.TABLE_NAME)) {
            Log.e(TAG, "Tables are missing after onCreate has been called. Trying to recreate");
            // This operation is a no-op if the table already exists.
            databaseHelper.addFavoritesTable(databaseHelper.getWritableDatabase(), true);
        }
        databaseHelper.mHotseatRestoreTableExists = tableExists(
                databaseHelper.getReadableDatabase(), Favorites.HYBRID_HOTSEAT_BACKUP_TABLE);

        databaseHelper.initIds();
        return databaseHelper;
    }

    /**
    /**
     * Constructor used in tests and for restore.
     * Constructor used in tests and for restore.
     */
     */
    public DatabaseHelper(Context context, String dbName, boolean forMigration) {
    public DatabaseHelper(Context context, String dbName,
            ToLongFunction<UserHandle> userSerialProvider, Runnable onEmptyDbCreateCallback) {
        super(context, dbName, SCHEMA_VERSION);
        super(context, dbName, SCHEMA_VERSION);
        mContext = context;
        mContext = context;
        mForMigration = forMigration;
        mUserSerialProvider = userSerialProvider;
        mOnEmptyDbCreateCallback = onEmptyDbCreateCallback;
    }
    }


    protected void initIds() {
    protected void initIds() {
@@ -131,13 +107,11 @@ public class DatabaseHelper extends NoLocaleSQLiteHelper implements


        mMaxItemId = 1;
        mMaxItemId = 1;


        addFavoritesTable(db, false);
        addTableToDb(db, getDefaultUserSerial(), false /* optional */);


        // Fresh and clean launcher DB.
        // Fresh and clean launcher DB.
        mMaxItemId = initializeMaxItemId(db);
        mMaxItemId = initializeMaxItemId(db);
        if (!mForMigration) {
        mOnEmptyDbCreateCallback.run();
            onEmptyDbCreated();
        }
    }
    }


    public void onAddOrDeleteOp(SQLiteDatabase db) {
    public void onAddOrDeleteOp(SQLiteDatabase db) {
@@ -147,38 +121,8 @@ public class DatabaseHelper extends NoLocaleSQLiteHelper implements
        }
        }
    }
    }


    /**
    private long getDefaultUserSerial() {
     * Re-composite given key in respect to database. If the current db is
        return mUserSerialProvider.applyAsLong(Process.myUserHandle());
     * {@link LauncherFiles#LAUNCHER_DB}, return the key as-is. Otherwise append the db name to
     * given key. e.g. consider key="EMPTY_DATABASE_CREATED", dbName="minimal.db", the returning
     * string will be "EMPTY_DATABASE_CREATED@minimal.db".
     */
    public String getKey(final String key) {
        if (TextUtils.equals(getDatabaseName(), LauncherFiles.LAUNCHER_DB)) {
            return key;
        }
        return key + "@" + getDatabaseName();
    }

    /**
     * Overridden in tests.
     */
    protected void onEmptyDbCreated() {
        // Set the flag for empty DB
        LauncherPrefs.getPrefs(mContext).edit().putBoolean(getKey(EMPTY_DATABASE_CREATED), true)
                .commit();
    }

    public long getSerialNumberForUser(UserHandle user) {
        return UserCache.INSTANCE.get(mContext).getSerialNumberForUser(user);
    }

    public long getDefaultUserSerial() {
        return getSerialNumberForUser(Process.myUserHandle());
    }

    private void addFavoritesTable(SQLiteDatabase db, boolean optional) {
        Favorites.addTableToDb(db, getDefaultUserSerial(), optional);
    }
    }


    @Override
    @Override
Loading