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

Commit 94936edf authored by Aurélien Pomini's avatar Aurélien Pomini Committed by Automerger Merge Worker
Browse files

Merge changes I98f847c9,Ia19fcefc into udc-qpr-dev am: 66f3f48e

parents 9130cd51 66f3f48e
Loading
Loading
Loading
Loading
+66 −21
Original line number Diff line number Diff line
@@ -359,11 +359,7 @@ public class WallpaperBackupAgent extends BackupAgent {
        final File infoStage = new File(filesDir, WALLPAPER_INFO_STAGE);
        final File imageStage = new File(filesDir, SYSTEM_WALLPAPER_STAGE);
        final File lockImageStage = new File(filesDir, LOCK_WALLPAPER_STAGE);

        // If we restored separate lock imagery, the system wallpaper should be
        // applied as system-only; but if there's no separate lock image, make
        // sure to apply the restored system wallpaper as both.
        final int sysWhich = FLAG_SYSTEM | (lockImageStage.exists() ? 0 : FLAG_LOCK);
        boolean lockImageStageExists = lockImageStage.exists();

        try {
            // First parse the live component name so that we know for logging if we care about
@@ -371,14 +367,31 @@ public class WallpaperBackupAgent extends BackupAgent {
            ComponentName wpService = parseWallpaperComponent(infoStage, "wp");
            mSystemHasLiveComponent = wpService != null;

            ComponentName kwpService = null;
            boolean lockscreenLiveWallpaper = mWallpaperManager.isLockscreenLiveWallpaperEnabled();
            if (lockscreenLiveWallpaper) {
                kwpService = parseWallpaperComponent(infoStage, "kwp");
            }
            mLockHasLiveComponent = kwpService != null;
            boolean separateLockWallpaper = mLockHasLiveComponent || lockImageStage.exists();

            // if there's no separate lock wallpaper, apply the system wallpaper to both screens.
            final int sysWhich = separateLockWallpaper ? FLAG_SYSTEM : FLAG_SYSTEM | FLAG_LOCK;

            // It is valid for the imagery to be absent; it means that we were not permitted
            // to back up the original image on the source device, or there was no user-supplied
            // wallpaper image present.
            restoreFromStage(imageStage, infoStage, "wp", sysWhich);
            if (!lockscreenLiveWallpaper) restoreFromStage(imageStage, infoStage, "wp", sysWhich);
            if (lockImageStageExists) {
                restoreFromStage(lockImageStage, infoStage, "kwp", FLAG_LOCK);
            }
            if (lockscreenLiveWallpaper) restoreFromStage(imageStage, infoStage, "wp", sysWhich);

            // And reset to the wallpaper service we should be using
            updateWallpaperComponent(wpService, !lockImageStage.exists());
            if (lockscreenLiveWallpaper && mLockHasLiveComponent) {
                updateWallpaperComponent(kwpService, false, FLAG_LOCK);
            }
            updateWallpaperComponent(wpService, !lockImageStageExists, sysWhich);
        } catch (Exception e) {
            Slog.e(TAG, "Unable to restore wallpaper: " + e.getMessage());
            mEventLogger.onRestoreException(e);
@@ -397,9 +410,21 @@ public class WallpaperBackupAgent extends BackupAgent {
    }

    @VisibleForTesting
    void updateWallpaperComponent(ComponentName wpService, boolean applyToLock) throws IOException {
    void updateWallpaperComponent(ComponentName wpService, boolean applyToLock, int which)
            throws IOException {
        boolean lockscreenLiveWallpaper = mWallpaperManager.isLockscreenLiveWallpaperEnabled();
        if (servicePackageExists(wpService)) {
            Slog.i(TAG, "Using wallpaper service " + wpService);
            if (lockscreenLiveWallpaper) {
                mWallpaperManager.setWallpaperComponentWithFlags(wpService, which);
                if ((which & FLAG_LOCK) != 0) {
                    mEventLogger.onLockLiveWallpaperRestored(wpService);
                }
                if ((which & FLAG_SYSTEM) != 0) {
                    mEventLogger.onSystemLiveWallpaperRestored(wpService);
                }
                return;
            }
            mWallpaperManager.setWallpaperComponent(wpService);
            if (applyToLock) {
                // We have a live wallpaper and no static lock image,
@@ -414,7 +439,7 @@ public class WallpaperBackupAgent extends BackupAgent {
            // in reports from users
            if (wpService != null) {
                // TODO(b/268471749): Handle delayed case
                applyComponentAtInstall(wpService, applyToLock);
                applyComponentAtInstall(wpService, applyToLock, which);
                Slog.w(TAG, "Wallpaper service " + wpService + " isn't available. "
                        + " Will try to apply later");
            }
@@ -437,7 +462,8 @@ public class WallpaperBackupAgent extends BackupAgent {
                    // And log the success
                    if ((which & FLAG_SYSTEM) > 0) {
                        mEventLogger.onSystemImageWallpaperRestored();
                    } else {
                    }
                    if ((which & FLAG_LOCK) > 0) {
                        mEventLogger.onLockImageWallpaperRestored();
                    }
                }
@@ -460,7 +486,8 @@ public class WallpaperBackupAgent extends BackupAgent {
    private void logRestoreError(int which, String error) {
        if ((which & FLAG_SYSTEM) == FLAG_SYSTEM) {
            mEventLogger.onSystemImageWallpaperRestoreFailed(error);
        } else if ((which & FLAG_LOCK) == FLAG_LOCK) {
        }
        if ((which & FLAG_LOCK) == FLAG_LOCK) {
            mEventLogger.onLockImageWallpaperRestoreFailed(error);
        }
    }
@@ -552,16 +579,21 @@ public class WallpaperBackupAgent extends BackupAgent {
        // Intentionally blank
    }

    private void applyComponentAtInstall(ComponentName componentName, boolean applyToLock) {
        PackageMonitor packageMonitor = getWallpaperPackageMonitor(componentName, applyToLock);
    private void applyComponentAtInstall(ComponentName componentName, boolean applyToLock,
            int which) {
        PackageMonitor packageMonitor = getWallpaperPackageMonitor(
                componentName, applyToLock, which);
        packageMonitor.register(getBaseContext(), null, UserHandle.ALL, true);
    }

    @VisibleForTesting
    PackageMonitor getWallpaperPackageMonitor(ComponentName componentName, boolean applyToLock) {
    PackageMonitor getWallpaperPackageMonitor(ComponentName componentName, boolean applyToLock,
            int which) {
        return new PackageMonitor() {
            @Override
            public void onPackageAdded(String packageName, int uid) {
                boolean lockscreenLiveWallpaper =
                        mWallpaperManager.isLockscreenLiveWallpaperEnabled();
                if (!isDeviceInRestore()) {
                    // We don't want to reapply the wallpaper outside a restore.
                    unregister();
@@ -582,16 +614,29 @@ public class WallpaperBackupAgent extends BackupAgent {

                if (componentName.getPackageName().equals(packageName)) {
                    Slog.d(TAG, "Applying component " + componentName);
                    boolean sysResult = mWallpaperManager.setWallpaperComponent(componentName);
                    boolean success = lockscreenLiveWallpaper
                            ? mWallpaperManager.setWallpaperComponentWithFlags(componentName, which)
                            : mWallpaperManager.setWallpaperComponent(componentName);
                    WallpaperEventLogger logger = new WallpaperEventLogger(
                            mBackupManager.getDelayedRestoreLogger());
                    if (sysResult) {
                    if (success) {
                        if (!lockscreenLiveWallpaper || (which & FLAG_SYSTEM) != 0) {
                            logger.onSystemLiveWallpaperRestored(componentName);
                        }
                        if (lockscreenLiveWallpaper && (which & FLAG_LOCK) != 0) {
                            logger.onLockLiveWallpaperRestored(componentName);
                        }
                    } else {
                        if (!lockscreenLiveWallpaper || (which & FLAG_SYSTEM) != 0) {
                            logger.onSystemLiveWallpaperRestoreFailed(
                                    WallpaperEventLogger.ERROR_SET_COMPONENT_EXCEPTION);
                        }
                    if (applyToLock) {
                        if (lockscreenLiveWallpaper && (which & FLAG_LOCK) != 0) {
                            logger.onLockLiveWallpaperRestoreFailed(
                                    WallpaperEventLogger.ERROR_SET_COMPONENT_EXCEPTION);
                        }
                    }
                    if (applyToLock && !lockscreenLiveWallpaper) {
                        try {
                            mWallpaperManager.clear(FLAG_LOCK);
                            logger.onLockLiveWallpaperRestored(componentName);
+52 −21
Original line number Diff line number Diff line
@@ -117,6 +117,7 @@ public class WallpaperBackupAgentTest {
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        when(mWallpaperManager.isLockscreenLiveWallpaperEnabled()).thenReturn(true);
        when(mWallpaperManager.isWallpaperBackupEligible(eq(FLAG_SYSTEM))).thenReturn(true);
        when(mWallpaperManager.isWallpaperBackupEligible(eq(FLAG_LOCK))).thenReturn(true);

@@ -364,15 +365,24 @@ public class WallpaperBackupAgentTest {
        mWallpaperBackupAgent.mIsDeviceInRestore = true;

        mWallpaperBackupAgent.updateWallpaperComponent(mWallpaperComponent,
                /* applyToLock */ true);
                /* applyToLock */ true, FLAG_LOCK | FLAG_SYSTEM);

        // Imitate wallpaper component installation.
        mWallpaperBackupAgent.mWallpaperPackageMonitor.onPackageAdded(TEST_WALLPAPER_PACKAGE,
                /* uid */0);

        if (mWallpaperManager.isLockscreenLiveWallpaperEnabled()) {
            verify(mWallpaperManager, times(1))
                    .setWallpaperComponentWithFlags(mWallpaperComponent, FLAG_LOCK | FLAG_SYSTEM);
            verify(mWallpaperManager, never())
                    .setWallpaperComponentWithFlags(mWallpaperComponent, FLAG_SYSTEM);
            verify(mWallpaperManager, never())
                    .setWallpaperComponentWithFlags(mWallpaperComponent, FLAG_LOCK);
            verify(mWallpaperManager, never()).clear(anyInt());
        } else {
            verify(mWallpaperManager, times(1)).setWallpaperComponent(mWallpaperComponent);
            verify(mWallpaperManager, times(1)).clear(eq(FLAG_LOCK));
        }
    }

    @Test
    public void testUpdateWallpaperComponent_applyToLockFalse_doesApplyLaterOnlyToMainScreen()
@@ -380,15 +390,25 @@ public class WallpaperBackupAgentTest {
        mWallpaperBackupAgent.mIsDeviceInRestore = true;

        mWallpaperBackupAgent.updateWallpaperComponent(mWallpaperComponent,
                /* applyToLock */ false);
                /* applyToLock */ false, FLAG_SYSTEM);

        // Imitate wallpaper component installation.
        mWallpaperBackupAgent.mWallpaperPackageMonitor.onPackageAdded(TEST_WALLPAPER_PACKAGE,
                /* uid */0);

        if (mWallpaperManager.isLockscreenLiveWallpaperEnabled()) {
            verify(mWallpaperManager, times(1))
                    .setWallpaperComponentWithFlags(mWallpaperComponent, FLAG_SYSTEM);
            verify(mWallpaperManager, never())
                    .setWallpaperComponentWithFlags(mWallpaperComponent, FLAG_LOCK);
            verify(mWallpaperManager, never())
                    .setWallpaperComponentWithFlags(mWallpaperComponent, FLAG_LOCK | FLAG_SYSTEM);
            verify(mWallpaperManager, never()).clear(anyInt());
        } else {
            verify(mWallpaperManager, times(1)).setWallpaperComponent(mWallpaperComponent);
            verify(mWallpaperManager, never()).clear(eq(FLAG_LOCK));
        }
    }

    @Test
    public void testUpdateWallpaperComponent_deviceNotInRestore_doesNotApply()
@@ -396,7 +416,7 @@ public class WallpaperBackupAgentTest {
        mWallpaperBackupAgent.mIsDeviceInRestore = false;

        mWallpaperBackupAgent.updateWallpaperComponent(mWallpaperComponent,
                /* applyToLock */ true);
                /* applyToLock */ true, FLAG_LOCK | FLAG_SYSTEM);

        // Imitate wallpaper component installation.
        mWallpaperBackupAgent.mWallpaperPackageMonitor.onPackageAdded(TEST_WALLPAPER_PACKAGE,
@@ -412,7 +432,7 @@ public class WallpaperBackupAgentTest {
        mWallpaperBackupAgent.mIsDeviceInRestore = false;

        mWallpaperBackupAgent.updateWallpaperComponent(mWallpaperComponent,
                /* applyToLock */ true);
                /* applyToLock */ true, FLAG_LOCK | FLAG_SYSTEM);

        // Imitate "wrong" wallpaper component installation.
        mWallpaperBackupAgent.mWallpaperPackageMonitor.onPackageAdded(/* packageName */"",
@@ -614,6 +634,13 @@ public class WallpaperBackupAgentTest {
                mWallpaperBackupAgent.getBackupRestoreEventLogger().getLoggingResults());
        assertThat(result).isNotNull();
        assertThat(result.getSuccessCount()).isEqualTo(1);

        if (mWallpaperManager.isLockscreenLiveWallpaperEnabled()) {
            result = getLoggingResult(WALLPAPER_IMG_LOCK,
                    mWallpaperBackupAgent.getBackupRestoreEventLogger().getLoggingResults());
            assertThat(result).isNotNull();
            assertThat(result.getSuccessCount()).isEqualTo(1);
        }
    }

    @Test
@@ -649,20 +676,21 @@ public class WallpaperBackupAgentTest {
    }

    @Test
    public void testOnRestore_lockWallpaperImgMissingAndNoLive_logsFailure() throws Exception {
    public void testOnRestore_wallpaperImgMissingAndNoLive_logsFailure() throws Exception {
        mockStagedWallpaperFile(WALLPAPER_INFO_STAGE);
        mockStagedWallpaperFile(SYSTEM_WALLPAPER_STAGE);
        mWallpaperBackupAgent.onCreate(USER_HANDLE, BackupAnnotations.BackupDestination.CLOUD,
                BackupAnnotations.OperationType.RESTORE);

        mWallpaperBackupAgent.onRestoreFinished();

        DataTypeResult result = getLoggingResult(WALLPAPER_IMG_LOCK,
        for (String wallpaper: List.of(WALLPAPER_IMG_LOCK, WALLPAPER_IMG_SYSTEM)) {
            DataTypeResult result = getLoggingResult(wallpaper,
                    mWallpaperBackupAgent.getBackupRestoreEventLogger().getLoggingResults());
            assertThat(result).isNotNull();
            assertThat(result.getFailCount()).isEqualTo(1);
            assertThat(result.getErrors()).containsKey(ERROR_NO_WALLPAPER);
        }
    }

    @Test
    public void testOnRestore_wallpaperInfoMissing_logsFailure() throws Exception {
@@ -722,13 +750,15 @@ public class WallpaperBackupAgentTest {
    public void testUpdateWallpaperComponent_delayedRestore_logsSuccess() throws Exception {
        mWallpaperBackupAgent.mIsDeviceInRestore = true;
        when(mWallpaperManager.setWallpaperComponent(any())).thenReturn(true);
        when(mWallpaperManager.setWallpaperComponentWithFlags(any(), eq(FLAG_LOCK | FLAG_SYSTEM)))
                .thenReturn(true);
        BackupRestoreEventLogger logger = new BackupRestoreEventLogger(
                BackupAnnotations.OperationType.RESTORE);
        when(mBackupManager.getDelayedRestoreLogger()).thenReturn(logger);
        mWallpaperBackupAgent.setBackupManagerForTesting(mBackupManager);

        mWallpaperBackupAgent.updateWallpaperComponent(mWallpaperComponent,
                /* applyToLock */ true);
                /* applyToLock */ true, FLAG_LOCK | FLAG_SYSTEM);
        // Imitate wallpaper component installation.
        mWallpaperBackupAgent.mWallpaperPackageMonitor.onPackageAdded(TEST_WALLPAPER_PACKAGE,
                /* uid */0);
@@ -752,7 +782,7 @@ public class WallpaperBackupAgentTest {
        mWallpaperBackupAgent.setBackupManagerForTesting(mBackupManager);

        mWallpaperBackupAgent.updateWallpaperComponent(mWallpaperComponent,
                /* applyToLock */ true);
                /* applyToLock */ true, FLAG_LOCK | FLAG_SYSTEM);
        // Imitate wallpaper component installation.
        mWallpaperBackupAgent.mWallpaperPackageMonitor.onPackageAdded(TEST_WALLPAPER_PACKAGE,
                /* uid */0);
@@ -774,7 +804,7 @@ public class WallpaperBackupAgentTest {
        mWallpaperBackupAgent.setBackupManagerForTesting(mBackupManager);

        mWallpaperBackupAgent.updateWallpaperComponent(mWallpaperComponent,
                /* applyToLock */ true);
                /* applyToLock */ true, FLAG_LOCK | FLAG_SYSTEM);

        // Imitate wallpaper component installation.
        mWallpaperBackupAgent.mWallpaperPackageMonitor.onPackageAdded(TEST_WALLPAPER_PACKAGE,
@@ -909,8 +939,9 @@ public class WallpaperBackupAgentTest {

        @Override
        PackageMonitor getWallpaperPackageMonitor(ComponentName componentName,
                boolean applyToLock) {
            mWallpaperPackageMonitor = super.getWallpaperPackageMonitor(componentName, applyToLock);
                boolean applyToLock, int which) {
            mWallpaperPackageMonitor = super.getWallpaperPackageMonitor(
                    componentName, applyToLock, which);
            return mWallpaperPackageMonitor;
        }