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

Commit b4df2351 authored by Abdullah Tabassum's avatar Abdullah Tabassum Committed by Android (Google) Code Review
Browse files

Merge "Implementing smart device config backup" into main

parents 14bf1787 3fbdf467
Loading
Loading
Loading
Loading
+80 −36
Original line number Diff line number Diff line
@@ -120,6 +120,14 @@ public class WallpaperBackupAgent extends BackupAgent {
    static final String SYSTEM_GENERATION = "system_gen";
    static final String LOCK_GENERATION = "lock_gen";

    static final String DEVICE_CONFIG_WIDTH = "device_config_width";

    static final String DEVICE_CONFIG_HEIGHT = "device_config_height";

    static final String DEVICE_CONFIG_SECONDARY_WIDTH = "device_config_secondary_width";

    static final String DEVICE_CONFIG_SECONDARY_HEIGHT = "device_config_secondary_height";

    static final float DEFAULT_ACCEPTABLE_PARALLAX = 0.2f;

    // If this file exists, it means we exceeded our quota last time
@@ -175,6 +183,16 @@ public class WallpaperBackupAgent extends BackupAgent {
            // disk churn.
            final int lastSysGeneration = sharedPrefs.getInt(SYSTEM_GENERATION, /* defValue= */ -1);
            final int lastLockGeneration = sharedPrefs.getInt(LOCK_GENERATION, /* defValue= */ -1);

            final int deviceConfigWidth = sharedPrefs.getInt(
                    DEVICE_CONFIG_WIDTH, /* defValue= */ -1);
            final int deviceConfigHeight = sharedPrefs.getInt(
                    DEVICE_CONFIG_HEIGHT, /* defValue= */ -1);
            final int deviceConfigSecondaryWidth = sharedPrefs.getInt(
                    DEVICE_CONFIG_SECONDARY_WIDTH, /* defValue= */ -1);
            final int deviceConfigSecondaryHeight = sharedPrefs.getInt(
                    DEVICE_CONFIG_SECONDARY_HEIGHT, /* defValue= */ -1);

            final int sysGeneration = mWallpaperManager.getWallpaperId(FLAG_SYSTEM);
            final int lockGeneration = mWallpaperManager.getWallpaperId(FLAG_LOCK);
            final boolean sysChanged = (sysGeneration != lastSysGeneration);
@@ -195,7 +213,11 @@ public class WallpaperBackupAgent extends BackupAgent {
            backupWallpaperInfoFile(/* sysOrLockChanged= */ sysChanged || lockChanged, data);
            backupSystemWallpaperFile(sharedPrefs, sysChanged, sysGeneration, data);
            backupLockWallpaperFileIfItExists(sharedPrefs, lockChanged, lockGeneration, data);
            backupDeviceInfoFile(data);

            final boolean isDeviceConfigChanged = isDeviceConfigChanged(deviceConfigWidth,
                    deviceConfigHeight, deviceConfigSecondaryWidth, deviceConfigSecondaryHeight);

            backupDeviceInfoFile(sharedPrefs, isDeviceConfigChanged, data);
        } catch (Exception e) {
            Slog.e(TAG, "Unable to back up wallpaper", e);
            mEventLogger.onBackupException(e);
@@ -209,14 +231,29 @@ public class WallpaperBackupAgent extends BackupAgent {
        }
    }

    private boolean isDeviceConfigChanged(int width, int height, int secondaryWidth,
            int secondaryHeight) {
        Point currentDimensions = getScreenDimensions();
        Display smallerDisplay = getSmallerDisplayIfExists();
        Point currentSecondaryDimensions = smallerDisplay != null ? getRealSize(smallerDisplay) :
                new Point(0, 0);

        return (currentDimensions.x != width
                || currentDimensions.y != height
                || currentSecondaryDimensions.x != secondaryWidth
                || currentSecondaryDimensions.y != secondaryHeight);
    }

    /**
     * This method backs up the device dimension information. The device data will always get
     * overwritten when triggering a backup
     */
    private void backupDeviceInfoFile(FullBackupDataOutput data)
    private void backupDeviceInfoFile(SharedPreferences sharedPrefs, boolean isDeviceConfigChanged,
            FullBackupDataOutput data)
            throws IOException {
        final File deviceInfoStage = new File(getFilesDir(), WALLPAPER_BACKUP_DEVICE_INFO_STAGE);

        if (isDeviceConfigChanged) {
            // save the dimensions of the device with xml formatting
            Point dimensions = getScreenDimensions();
            Display smallerDisplay = getSmallerDisplayIfExists();
@@ -253,6 +290,13 @@ public class WallpaperBackupAgent extends BackupAgent {
            FileUtils.sync(fstream);
            fstream.close();

            SharedPreferences.Editor editor = sharedPrefs.edit();
            editor.putInt(DEVICE_CONFIG_WIDTH, dimensions.x);
            editor.putInt(DEVICE_CONFIG_HEIGHT, dimensions.y);
            editor.putInt(DEVICE_CONFIG_SECONDARY_WIDTH, secondaryDimensions.x);
            editor.putInt(DEVICE_CONFIG_SECONDARY_HEIGHT, secondaryDimensions.y);
            editor.apply();
        }
        if (DEBUG) Slog.v(TAG, "Storing device dimension data");
        backupFile(deviceInfoStage, data);
    }