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

Commit acdcaa21 authored by Sebastián Franco's avatar Sebastián Franco Committed by Android (Google) Code Review
Browse files

Merge "Removing all restored backups except one so we don't have old backups" into main

parents 68c145a1 bd43b934
Loading
Loading
Loading
Loading
+40 −0
Original line number Original line Diff line number Diff line
@@ -72,6 +72,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Collections;
import java.util.Collections;
import java.util.List;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Collectors;


public class InvariantDeviceProfile {
public class InvariantDeviceProfile {
@@ -577,6 +578,45 @@ public class InvariantDeviceProfile {
        return filteredProfiles;
        return filteredProfiles;
    }
    }


    /**
     * Returns the GridOption associated to the given file name or null if the fileName is not
     * supported.
     * Ej, launcher.db -> "normal grid", launcher_4_by_4.db -> "practical grid"
     */
    public GridOption getGridOptionFromFileName(Context context, String fileName) {
        return parseAllGridOptions(context).stream()
                .filter(gridOption -> Objects.equals(gridOption.dbFile, fileName))
                .findFirst()
                .orElse(null);
    }

    /**
     * Returns the name of the given size on the current device or empty string if the size is not
     * supported. Ej. 4x4 -> normal, 5x4 -> practical, etc.
     * (Note: the name of the grid can be different for the same grid size depending of
     * the values of the InvariantDeviceProfile)
     *
     */
    public String getGridNameFromSize(Context context, Point size) {
        return parseAllGridOptions(context).stream()
                .filter(gridOption -> gridOption.numColumns == size.x
                        && gridOption.numRows == size.y)
                .map(gridOption -> gridOption.name)
                .findFirst()
                .orElse("");
    }

    /**
     * Returns the grid option for the given gridName on the current device (Note: the gridOption
     * be different for the same gridName depending on the values of the InvariantDeviceProfile).
     */
    public GridOption getGridOptionFromName(Context context, String gridName) {
        return parseAllGridOptions(context).stream()
                .filter(gridOption -> Objects.equals(gridOption.name, gridName))
                .findFirst()
                .orElse(null);
    }

    /**
    /**
     * @return all the grid options that can be shown on the device
     * @return all the grid options that can be shown on the device
     */
     */
+44 −1
Original line number Original line Diff line number Diff line
@@ -50,8 +50,10 @@ import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;
import androidx.annotation.WorkerThread;


import com.android.launcher3.Flags;
import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherFiles;
import com.android.launcher3.LauncherPrefs;
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;
@@ -121,8 +123,49 @@ public class RestoreDbTask {
        // executed again.
        // executed again.
        LauncherPrefs.get(context).removeSync(RESTORE_DEVICE);
        LauncherPrefs.get(context).removeSync(RESTORE_DEVICE);


        if (Flags.narrowGridRestore()) {
            String oldPhoneFileName = idp.dbFile;
            removeOldDBs(context, oldPhoneFileName);
            trySettingPreviousGidAsCurrent(context, idp, oldPhoneFileName);
        } else {
            idp.reinitializeAfterRestore(context);
            idp.reinitializeAfterRestore(context);
        }
        }
    }

    /**
     * Try setting the gird used in the previous phone to the new one. If the current device doesn't
     * support the previous grid option it will not be set.
     */
    private static void trySettingPreviousGidAsCurrent(Context context, InvariantDeviceProfile idp,
            String oldPhoneDbFileName) {
        InvariantDeviceProfile.GridOption gridOption = idp.getGridOptionFromFileName(context,
                oldPhoneDbFileName);
        if (gridOption != null) {
            /*
             * We do this because in some cases different devices have different names for grid
             * options, in one device the grid option "normal" can be 4x4 while in other it
             * could be "practical". Calling this changes the current device grid to the same
             * we had in the other phone, in the case the current phone doesn't support the grid
             * option we use the default and migrate the db to the default. Migration occurs on
             * {@code GridSizeMigrationUtil#migrateGridIfNeeded}
             */
            idp.setCurrentGrid(context, gridOption.name);
        }
    }

    /**
     * Only keep the last database used on the previous device.
     */
    private static void removeOldDBs(Context context, String oldPhoneDbFileName) {
        // At this point idp.dbFile contains the name of the dbFile from the previous phone
        LauncherFiles.GRID_DB_FILES.stream()
                .filter(dbName -> !dbName.equals(oldPhoneDbFileName))
                .forEach(dbName -> {
                    if (context.getDatabasePath(dbName).delete()) {
                        FileLog.d(TAG, "Removed old grid db file: " + dbName);
                    }
                });
    }


    private static boolean performRestore(Context context, ModelDbController controller) {
    private static boolean performRestore(Context context, ModelDbController controller) {
        SQLiteDatabase db = controller.getDb();
        SQLiteDatabase db = controller.getDb();
+124 KiB

File added.

No diff preview for this file type.

+40 KiB

File added.

No diff preview for this file type.

+136 KiB

File added.

No diff preview for this file type.

Loading