From 583986bb817ab1c76624a30dd3c8f3e86989a663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Pomini?= Date: Wed, 7 Jun 2023 10:37:52 +0000 Subject: [PATCH] Set WallpaperData flag in constructor This mWhich field was really meant to be set in the WallpaperData constructor. Otherwise, calling bindWallpaperComponentLocked(new WallpaperData(..., FLAG_LOCK)) will actually try to set the wallpaper on system only (since the which flag will be 0), causing all sorts of problems. This happens when trying to temporarily bind a fallback wallpaper after a reboot, in case the home+lock wallpaper is not direct-boot aware. Take this opportunity to remove the old WallpaperData constructor (this does not change any logic, and the old constructor was only used in a test), and add a test for the new constructor. Bug: 286197596 Test: manually verify that the bug is fixed Test: atest WallpaperManagerTest Change-Id: Ic55a1d894134405bb325aba04eb42aaca9a1a318 --- .../server/wallpaper/WallpaperData.java | 16 +++++------ .../WallpaperManagerServiceTests.java | 27 ++++++++++++++++--- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/services/core/java/com/android/server/wallpaper/WallpaperData.java b/services/core/java/com/android/server/wallpaper/WallpaperData.java index 9ff6a0d7e6ee0..d87fca4d3c716 100644 --- a/services/core/java/com/android/server/wallpaper/WallpaperData.java +++ b/services/core/java/com/android/server/wallpaper/WallpaperData.java @@ -133,16 +133,14 @@ class WallpaperData { */ final Rect cropHint = new Rect(0, 0, 0, 0); - WallpaperData(int userId, File wallpaperDir, String inputFileName, String cropFileName) { - this.userId = userId; - wallpaperFile = new File(wallpaperDir, inputFileName); - cropFile = new File(wallpaperDir, cropFileName); - } - WallpaperData(int userId, @SetWallpaperFlags int wallpaperType) { - this(userId, getWallpaperDir(userId), - (wallpaperType == FLAG_LOCK) ? WALLPAPER_LOCK_ORIG : WALLPAPER, - (wallpaperType == FLAG_LOCK) ? WALLPAPER_LOCK_CROP : WALLPAPER_CROP); + this.userId = userId; + this.mWhich = wallpaperType; + File wallpaperDir = getWallpaperDir(userId); + String wallpaperFileName = (wallpaperType == FLAG_LOCK) ? WALLPAPER_LOCK_ORIG : WALLPAPER; + String cropFileName = (wallpaperType == FLAG_LOCK) ? WALLPAPER_LOCK_CROP : WALLPAPER_CROP; + this.wallpaperFile = new File(wallpaperDir, wallpaperFileName); + this.cropFile = new File(wallpaperDir, cropFileName); } /** diff --git a/services/tests/mockingservicestests/src/com/android/server/wallpaper/WallpaperManagerServiceTests.java b/services/tests/mockingservicestests/src/com/android/server/wallpaper/WallpaperManagerServiceTests.java index 51e521d8ffe44..f16a3a11535fd 100644 --- a/services/tests/mockingservicestests/src/com/android/server/wallpaper/WallpaperManagerServiceTests.java +++ b/services/tests/mockingservicestests/src/com/android/server/wallpaper/WallpaperManagerServiceTests.java @@ -17,8 +17,10 @@ package com.android.server.wallpaper; import static android.app.WallpaperManager.COMMAND_REAPPLY; +import static android.app.WallpaperManager.FLAG_LOCK; import static android.app.WallpaperManager.FLAG_SYSTEM; import static android.os.FileObserver.CLOSE_WRITE; +import static android.os.UserHandle.MIN_SECONDARY_USER_ID; import static android.os.UserHandle.USER_SYSTEM; import static android.view.Display.DEFAULT_DISPLAY; @@ -106,6 +108,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.List; /** * Tests for the {@link WallpaperManagerService} class. @@ -261,6 +264,25 @@ public class WallpaperManagerServiceTests { } } + /** + * Tests that the fundamental fields are set by the main WallpaperData constructor + */ + @Test + public void testWallpaperDataConstructor() { + final int testUserId = MIN_SECONDARY_USER_ID; + for (int which: List.of(FLAG_LOCK, FLAG_SYSTEM)) { + WallpaperData newWallpaperData = new WallpaperData(testUserId, which); + assertEquals(which, newWallpaperData.mWhich); + assertEquals(testUserId, newWallpaperData.userId); + + WallpaperData wallpaperData = mService.getWallpaperSafeLocked(testUserId, which); + assertEquals(wallpaperData.cropFile.getAbsolutePath(), + newWallpaperData.cropFile.getAbsolutePath()); + assertEquals(wallpaperData.wallpaperFile.getAbsolutePath(), + newWallpaperData.wallpaperFile.getAbsolutePath()); + } + } + /** * Tests that internal basic data should be correct after boot up. */ @@ -405,10 +427,7 @@ public class WallpaperManagerServiceTests { fail("exception occurred while writing system wallpaper attributes"); } - WallpaperData shouldMatchSystem = new WallpaperData(systemWallpaperData.userId, - systemWallpaperData.wallpaperFile.getParentFile(), - systemWallpaperData.wallpaperFile.getAbsolutePath(), - systemWallpaperData.cropFile.getAbsolutePath()); + WallpaperData shouldMatchSystem = new WallpaperData(0, FLAG_SYSTEM); try { TypedXmlPullParser parser = Xml.newBinaryPullParser(); mService.mWallpaperDataParser.parseWallpaperAttributes(parser, shouldMatchSystem, true);