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

Commit 9e28237a authored by Sunny Goyal's avatar Sunny Goyal Committed by Automerger Merge Worker
Browse files

Revert "Revert "Revert "Revert "Revert "Revert "Changing GridMigrationTask to...

Revert "Revert "Revert "Revert "Revert "Revert "Changing GridMigrationTask to use ModelDbController directly"""""" am: 7bc6cdee am: e4e7a650

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/23219852



Change-Id: I183c8bb6dd35414a9ed32ec2705cba59e479ed9f
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents e2c113a7 e4e7a650
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -15,8 +15,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;

import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.model.DatabaseHelper;
import com.android.launcher3.model.LoaderTask;
import com.android.launcher3.model.ModelDbController;
import com.android.launcher3.model.WidgetsModel;
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
import com.android.launcher3.pm.UserCache;
@@ -53,7 +53,7 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
     * Updates the app widgets whose id has changed during the restore process.
     */
    @WorkerThread
    public static void restoreAppWidgetIds(Context context, DatabaseHelper helper,
    public static void restoreAppWidgetIds(Context context, ModelDbController controller,
            int[] oldWidgetIds, int[] newWidgetIds, @NonNull AppWidgetHost host) {
        if (WidgetsModel.GO_DISABLE_WIDGETS) {
            Log.e(TAG, "Skipping widget ID remap as widgets not supported");
@@ -78,7 +78,7 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
                + ", newWidgetIds=" + IntArray.wrap(newWidgetIds).toConcatString());

        try {
            IntArray result = LauncherDbUtils.queryIntArray(false, helper.getReadableDatabase(),
            IntArray result = LauncherDbUtils.queryIntArray(false, controller.getDb(),
                    Favorites.TABLE_NAME, Favorites.APPWIDGET_ID,
                    Favorites.APPWIDGET_ID + "!=" + LauncherAppWidgetInfo.NO_ID, null, null);
            // TODO(b/234700507): Remove the logs after the bug is fixed
@@ -108,7 +108,7 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
            final String where = "appWidgetId=? and (restored & 1) = 1 and profileId=?";
            final String[] args = new String[] { oldWidgetId, Long.toString(mainProfileId) };
            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.RESTORED, state)
                    .commit();
@@ -116,7 +116,7 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
                // TODO(b/234700507): Remove the logs after the bug is fixed
                Log.e(TAG, "restoreAppWidgetIds: remapping failed since the widget is not in"
                        + " the database anymore");
                try (Cursor cursor = helper.getWritableDatabase().query(
                try (Cursor cursor = controller.getDb().query(
                        Favorites.TABLE_NAME,
                        new String[]{Favorites.APPWIDGET_ID},
                        "appWidgetId=?", new String[]{oldWidgetId}, null, null, null)) {
+0 −12
Original line number Diff line number Diff line
@@ -263,18 +263,6 @@ public class LauncherProvider extends ContentProvider {
                getModelDbController().refreshHotseatRestoreTable();
                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;
    }
+1 −25
Original line number Diff line number Diff line
@@ -147,11 +147,6 @@ public class LauncherSettings {
         */
        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
         */
@@ -163,18 +158,6 @@ public class LauncherSettings {
        public static final Uri CONTENT_URI = Uri.parse("content://"
                + 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.
         *
@@ -376,10 +359,6 @@ public class LauncherSettings {

        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_DB_NAME = "db_name";
@@ -393,11 +372,8 @@ public class LauncherSettings {
        }

        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 Diff line number Diff line
@@ -16,6 +16,7 @@

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.MODEL_EXECUTOR;

@@ -52,6 +53,8 @@ import com.android.launcher3.graphics.LauncherPreviewRenderer.PreviewContext;
import com.android.launcher3.model.BgDataModel;
import com.android.launcher3.model.GridSizeMigrationUtil;
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.RunnableList;
import com.android.launcher3.util.Themes;
@@ -145,7 +148,9 @@ public class PreviewSurfaceRenderer {
        final String query = LauncherSettings.Favorites.ITEM_TYPE + " = "
                + 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[] {
                        LauncherSettings.Favorites.APPWIDGET_ID,
                        LauncherSettings.Favorites.SPANX,
@@ -190,8 +195,6 @@ public class PreviewSurfaceRenderer {

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

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

        if (migrated) {
        if (GridSizeMigrationUtil.needsToMigrate(inflationContext, mIdp)) {
            // Start the migration
            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(
                    LauncherAppState.getInstance(previewContext),
                    /* bgAllAppsList= */ null,
@@ -229,8 +244,7 @@ public class PreviewSurfaceRenderer {
                        query += " or " + LauncherSettings.Favorites.SCREEN + " = "
                                + Workspace.SECOND_SCREEN_ID;
                    }
                    loadWorkspaceForPreviewSurfaceRenderer(new ArrayList<>(),
                            LauncherSettings.Favorites.PREVIEW_CONTENT_URI, query);
                    loadWorkspace(new ArrayList<>(), query, null);

                    final SparseArray<Size> spanInfo =
                            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
    private void renderView(Context inflationContext, BgDataModel dataModel,
            Map<ComponentKey, AppWidgetProviderInfo> widgetProviderInfoMap,
+13 −69
Original line number Diff line number Diff line
@@ -15,8 +15,8 @@
 */
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.tableExists;

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

import com.android.launcher3.AutoInstallsLayout;
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.Favorites;
import com.android.launcher3.Utilities;
@@ -58,6 +55,7 @@ import java.io.File;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Locale;
import java.util.function.ToLongFunction;
import java.util.stream.Collectors;

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

    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 boolean mForMigration;
    private final ToLongFunction<UserHandle> mUserSerialProvider;
    private final Runnable mOnEmptyDbCreateCallback;

    private int mMaxItemId = -1;
    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.
     */
    public DatabaseHelper(Context context, String dbName, boolean forMigration) {
    public DatabaseHelper(Context context, String dbName,
            ToLongFunction<UserHandle> userSerialProvider, Runnable onEmptyDbCreateCallback) {
        super(context, dbName, SCHEMA_VERSION);
        mContext = context;
        mForMigration = forMigration;
        mUserSerialProvider = userSerialProvider;
        mOnEmptyDbCreateCallback = onEmptyDbCreateCallback;
    }

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

        mMaxItemId = 1;

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

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

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

    /**
     * Re-composite given key in respect to database. If the current db is
     * {@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);
    private long getDefaultUserSerial() {
        return mUserSerialProvider.applyAsLong(Process.myUserHandle());
    }

    @Override
Loading