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
This commit is contained in:
Aurélien Pomini
2023-06-07 10:37:52 +00:00
parent 2c9783b3f0
commit 583986bb81
2 changed files with 30 additions and 13 deletions

View File

@@ -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);
}
/**

View File

@@ -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);