Merge "Replace WallpaperData File fields with methods" into udc-qpr-dev

This commit is contained in:
Aurélien Pomini
2023-06-22 23:09:27 +00:00
committed by Android (Google) Code Review
5 changed files with 68 additions and 54 deletions

View File

@@ -81,7 +81,7 @@ class WallpaperCropper {
if (DEBUG) { if (DEBUG) {
Slog.v(TAG, "Generating crop for new wallpaper(s): 0x" Slog.v(TAG, "Generating crop for new wallpaper(s): 0x"
+ Integer.toHexString(wallpaper.mWhich) + Integer.toHexString(wallpaper.mWhich)
+ " to " + wallpaper.cropFile.getName() + " to " + wallpaper.getCropFile().getName()
+ " crop=(" + cropHint.width() + 'x' + cropHint.height() + " crop=(" + cropHint.width() + 'x' + cropHint.height()
+ ") dim=(" + wpData.mWidth + 'x' + wpData.mHeight + ')'); + ") dim=(" + wpData.mWidth + 'x' + wpData.mHeight + ')');
} }
@@ -89,7 +89,7 @@ class WallpaperCropper {
// Analyse the source; needed in multiple cases // Analyse the source; needed in multiple cases
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(wallpaper.wallpaperFile.getAbsolutePath(), options); BitmapFactory.decodeFile(wallpaper.getWallpaperFile().getAbsolutePath(), options);
if (options.outWidth <= 0 || options.outHeight <= 0) { if (options.outWidth <= 0 || options.outHeight <= 0) {
Slog.w(TAG, "Invalid wallpaper data"); Slog.w(TAG, "Invalid wallpaper data");
success = false; success = false;
@@ -154,11 +154,10 @@ class WallpaperCropper {
// may be we can try to remove this optimized way in the future, // may be we can try to remove this optimized way in the future,
// that means, we will always go into the 'else' block. // that means, we will always go into the 'else' block.
success = FileUtils.copyFile(wallpaper.wallpaperFile, wallpaper.cropFile); success = FileUtils.copyFile(wallpaper.getWallpaperFile(), wallpaper.getCropFile());
if (!success) { if (!success) {
wallpaper.cropFile.delete(); wallpaper.getCropFile().delete();
// TODO: fall back to default wallpaper in this case
} }
if (DEBUG) { if (DEBUG) {
@@ -226,7 +225,7 @@ class WallpaperCropper {
//Create a record file and will delete if ImageDecoder work well. //Create a record file and will delete if ImageDecoder work well.
final String recordName = final String recordName =
(wallpaper.wallpaperFile.getName().equals(WALLPAPER) (wallpaper.getWallpaperFile().getName().equals(WALLPAPER)
? RECORD_FILE : RECORD_LOCK_FILE); ? RECORD_FILE : RECORD_LOCK_FILE);
final File record = new File(getWallpaperDir(wallpaper.userId), recordName); final File record = new File(getWallpaperDir(wallpaper.userId), recordName);
record.createNewFile(); record.createNewFile();
@@ -234,7 +233,7 @@ class WallpaperCropper {
+ ", record name =" + record.getName()); + ", record name =" + record.getName());
final ImageDecoder.Source srcData = final ImageDecoder.Source srcData =
ImageDecoder.createSource(wallpaper.wallpaperFile); ImageDecoder.createSource(wallpaper.getWallpaperFile());
final int sampleSize = scale; final int sampleSize = scale;
Bitmap cropped = ImageDecoder.decodeBitmap(srcData, (decoder, info, src) -> { Bitmap cropped = ImageDecoder.decodeBitmap(srcData, (decoder, info, src) -> {
decoder.setTargetSampleSize(sampleSize); decoder.setTargetSampleSize(sampleSize);
@@ -257,7 +256,7 @@ class WallpaperCropper {
+ " h=" + finalCrop.getHeight()); + " h=" + finalCrop.getHeight());
} }
f = new FileOutputStream(wallpaper.cropFile); f = new FileOutputStream(wallpaper.getCropFile());
bos = new BufferedOutputStream(f, 32 * 1024); bos = new BufferedOutputStream(f, 32 * 1024);
finalCrop.compress(Bitmap.CompressFormat.PNG, 100, bos); finalCrop.compress(Bitmap.CompressFormat.PNG, 100, bos);
// don't rely on the implicit flush-at-close when noting success // don't rely on the implicit flush-at-close when noting success
@@ -277,11 +276,11 @@ class WallpaperCropper {
if (!success) { if (!success) {
Slog.e(TAG, "Unable to apply new wallpaper"); Slog.e(TAG, "Unable to apply new wallpaper");
wallpaper.cropFile.delete(); wallpaper.getCropFile().delete();
} }
if (wallpaper.cropFile.exists()) { if (wallpaper.getCropFile().exists()) {
boolean didRestorecon = SELinux.restorecon(wallpaper.cropFile.getAbsoluteFile()); boolean didRestorecon = SELinux.restorecon(wallpaper.getCropFile().getAbsoluteFile());
if (DEBUG) { if (DEBUG) {
Slog.v(TAG, "restorecon() of crop file returned " + didRestorecon); Slog.v(TAG, "restorecon() of crop file returned " + didRestorecon);
} }

View File

@@ -40,10 +40,7 @@ import java.io.File;
*/ */
class WallpaperData { class WallpaperData {
int userId; final int userId;
final File wallpaperFile; // source image
final File cropFile; // eventual destination
/** /**
* True while the client is writing a new wallpaper * True while the client is writing a new wallpaper
@@ -133,14 +130,13 @@ class WallpaperData {
*/ */
final Rect cropHint = new Rect(0, 0, 0, 0); final Rect cropHint = new Rect(0, 0, 0, 0);
// map of which -> File
private final SparseArray<File> mWallpaperFiles = new SparseArray<>();
private final SparseArray<File> mCropFiles = new SparseArray<>();
WallpaperData(int userId, @SetWallpaperFlags int wallpaperType) { WallpaperData(int userId, @SetWallpaperFlags int wallpaperType) {
this.userId = userId; this.userId = userId;
this.mWhich = wallpaperType; 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);
} }
/** /**
@@ -154,8 +150,6 @@ class WallpaperData {
*/ */
WallpaperData(WallpaperData source) { WallpaperData(WallpaperData source) {
this.userId = source.userId; this.userId = source.userId;
this.wallpaperFile = source.wallpaperFile;
this.cropFile = source.cropFile;
this.wallpaperComponent = source.wallpaperComponent; this.wallpaperComponent = source.wallpaperComponent;
this.mWhich = source.mWhich; this.mWhich = source.mWhich;
this.wallpaperId = source.wallpaperId; this.wallpaperId = source.wallpaperId;
@@ -169,6 +163,25 @@ class WallpaperData {
} }
} }
File getWallpaperFile() {
String fileName = mWhich == FLAG_LOCK ? WALLPAPER_LOCK_ORIG : WALLPAPER;
return getFile(mWallpaperFiles, fileName);
}
File getCropFile() {
String fileName = mWhich == FLAG_LOCK ? WALLPAPER_LOCK_CROP : WALLPAPER_CROP;
return getFile(mCropFiles, fileName);
}
private File getFile(SparseArray<File> map, String fileName) {
File result = map.get(mWhich);
if (result == null) {
result = new File(getWallpaperDir(userId), fileName);
map.put(userId, result);
}
return result;
}
@Override @Override
public String toString() { public String toString() {
StringBuilder out = new StringBuilder(defaultString(this)); StringBuilder out = new StringBuilder(defaultString(this));
@@ -177,7 +190,7 @@ class WallpaperData {
out.append(", which: "); out.append(", which: ");
out.append(mWhich); out.append(mWhich);
out.append(", file mod: "); out.append(", file mod: ");
out.append(wallpaperFile != null ? wallpaperFile.lastModified() : "null"); out.append(getWallpaperFile() != null ? getWallpaperFile().lastModified() : "null");
if (connection == null) { if (connection == null) {
out.append(", no connection"); out.append(", no connection");
} else { } else {
@@ -202,10 +215,10 @@ class WallpaperData {
// Called during initialization of a given user's wallpaper bookkeeping // Called during initialization of a given user's wallpaper bookkeeping
boolean cropExists() { boolean cropExists() {
return cropFile.exists(); return getCropFile().exists();
} }
boolean sourceExists() { boolean sourceExists() {
return wallpaperFile.exists(); return getWallpaperFile().exists();
} }
} }

View File

@@ -542,12 +542,12 @@ class WallpaperDataParser {
} }
res = r.openRawResource(resId); res = r.openRawResource(resId);
if (wallpaper.wallpaperFile.exists()) { if (wallpaper.getWallpaperFile().exists()) {
wallpaper.wallpaperFile.delete(); wallpaper.getWallpaperFile().delete();
wallpaper.cropFile.delete(); wallpaper.getCropFile().delete();
} }
fos = new FileOutputStream(wallpaper.wallpaperFile); fos = new FileOutputStream(wallpaper.getWallpaperFile());
cos = new FileOutputStream(wallpaper.cropFile); cos = new FileOutputStream(wallpaper.getCropFile());
byte[] buffer = new byte[32768]; byte[] buffer = new byte[32768];
int amt; int amt;

View File

@@ -670,8 +670,8 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
// Not having a wallpaperComponent means it's a lock screen wallpaper. // Not having a wallpaperComponent means it's a lock screen wallpaper.
final boolean imageWallpaper = mImageWallpaper.equals(wallpaper.wallpaperComponent) final boolean imageWallpaper = mImageWallpaper.equals(wallpaper.wallpaperComponent)
|| wallpaper.wallpaperComponent == null; || wallpaper.wallpaperComponent == null;
if (imageWallpaper && wallpaper.cropFile != null && wallpaper.cropFile.exists()) { if (imageWallpaper && wallpaper.getCropFile().exists()) {
cropFile = wallpaper.cropFile.getAbsolutePath(); cropFile = wallpaper.getCropFile().getAbsolutePath();
} else if (imageWallpaper && !wallpaper.cropExists() && !wallpaper.sourceExists()) { } else if (imageWallpaper && !wallpaper.cropExists() && !wallpaper.sourceExists()) {
defaultImageWallpaper = true; defaultImageWallpaper = true;
} }
@@ -1794,8 +1794,8 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
private boolean clearWallpaperBitmaps(WallpaperData wallpaper) { private boolean clearWallpaperBitmaps(WallpaperData wallpaper) {
boolean sourceExists = wallpaper.sourceExists(); boolean sourceExists = wallpaper.sourceExists();
boolean cropExists = wallpaper.cropExists(); boolean cropExists = wallpaper.cropExists();
if (sourceExists) wallpaper.wallpaperFile.delete(); if (sourceExists) wallpaper.getWallpaperFile().delete();
if (cropExists) wallpaper.cropFile.delete(); if (cropExists) wallpaper.getCropFile().delete();
return sourceExists || cropExists; return sourceExists || cropExists;
} }
@@ -2394,13 +2394,13 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
wallpaper.callbacks.register(cb); wallpaper.callbacks.register(cb);
} }
File fileToReturn = getCropped ? wallpaper.cropFile : wallpaper.wallpaperFile; File result = getCropped ? wallpaper.getCropFile() : wallpaper.getWallpaperFile();
if (!fileToReturn.exists()) { if (!result.exists()) {
return null; return null;
} }
return ParcelFileDescriptor.open(fileToReturn, MODE_READ_ONLY); return ParcelFileDescriptor.open(result, MODE_READ_ONLY);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
/* Shouldn't happen as we check to see if the file exists */ /* Shouldn't happen as we check to see if the file exists */
Slog.w(TAG, "Error getting wallpaper", e); Slog.w(TAG, "Error getting wallpaper", e);
@@ -3161,16 +3161,17 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
// Migrate the bitmap files outright; no need to copy // Migrate the bitmap files outright; no need to copy
try { try {
if (!mIsLockscreenLiveWallpaperEnabled || sysWP.wallpaperFile.exists()) { if (!mIsLockscreenLiveWallpaperEnabled || sysWP.getWallpaperFile().exists()) {
Os.rename(sysWP.wallpaperFile.getAbsolutePath(), Os.rename(sysWP.getWallpaperFile().getAbsolutePath(),
lockWP.wallpaperFile.getAbsolutePath()); lockWP.getWallpaperFile().getAbsolutePath());
} }
if (!mIsLockscreenLiveWallpaperEnabled || sysWP.cropFile.exists()) { if (!mIsLockscreenLiveWallpaperEnabled || sysWP.getCropFile().exists()) {
Os.rename(sysWP.cropFile.getAbsolutePath(), lockWP.cropFile.getAbsolutePath()); Os.rename(sysWP.getCropFile().getAbsolutePath(),
lockWP.getCropFile().getAbsolutePath());
} }
mLockWallpaperMap.put(userId, lockWP); mLockWallpaperMap.put(userId, lockWP);
if (mIsLockscreenLiveWallpaperEnabled) { if (mIsLockscreenLiveWallpaperEnabled) {
SELinux.restorecon(lockWP.wallpaperFile); SELinux.restorecon(lockWP.getWallpaperFile());
mLastLockWallpaper = lockWP; mLastLockWallpaper = lockWP;
} }
} catch (ErrnoException e) { } catch (ErrnoException e) {
@@ -3191,11 +3192,11 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
-1, -1); -1, -1);
} }
ParcelFileDescriptor fd = ParcelFileDescriptor.open(wallpaper.wallpaperFile, ParcelFileDescriptor fd = ParcelFileDescriptor.open(wallpaper.getWallpaperFile(),
MODE_CREATE|MODE_READ_WRITE|MODE_TRUNCATE); MODE_CREATE|MODE_READ_WRITE|MODE_TRUNCATE);
if (!SELinux.restorecon(wallpaper.wallpaperFile)) { if (!SELinux.restorecon(wallpaper.getWallpaperFile())) {
Slog.w(TAG, "restorecon failed for wallpaper file: " + Slog.w(TAG, "restorecon failed for wallpaper file: " +
wallpaper.wallpaperFile.getPath()); wallpaper.getWallpaperFile().getPath());
return null; return null;
} }
wallpaper.name = name; wallpaper.name = name;
@@ -3206,7 +3207,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub
// Nullify field to require new computation // Nullify field to require new computation
wallpaper.primaryColors = null; wallpaper.primaryColors = null;
Slog.v(TAG, "updateWallpaperBitmapLocked() : id=" + wallpaper.wallpaperId Slog.v(TAG, "updateWallpaperBitmapLocked() : id=" + wallpaper.wallpaperId
+ " name=" + name + " file=" + wallpaper.wallpaperFile.getName()); + " name=" + name + " file=" + wallpaper.getWallpaperFile().getName());
return fd; return fd;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Slog.w(TAG, "Error setting wallpaper", e); Slog.w(TAG, "Error setting wallpaper", e);

View File

@@ -31,7 +31,6 @@ import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn; import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
import static com.android.server.wallpaper.WallpaperUtils.WALLPAPER; import static com.android.server.wallpaper.WallpaperUtils.WALLPAPER;
import static com.android.server.wallpaper.WallpaperUtils.WALLPAPER_CROP;
import static org.hamcrest.core.IsNot.not; import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@@ -275,10 +274,10 @@ public class WallpaperManagerServiceTests {
assertEquals(testUserId, newWallpaperData.userId); assertEquals(testUserId, newWallpaperData.userId);
WallpaperData wallpaperData = mService.getWallpaperSafeLocked(testUserId, which); WallpaperData wallpaperData = mService.getWallpaperSafeLocked(testUserId, which);
assertEquals(wallpaperData.cropFile.getAbsolutePath(), assertEquals(wallpaperData.getCropFile().getAbsolutePath(),
newWallpaperData.cropFile.getAbsolutePath()); newWallpaperData.getCropFile().getAbsolutePath());
assertEquals(wallpaperData.wallpaperFile.getAbsolutePath(), assertEquals(wallpaperData.getWallpaperFile().getAbsolutePath(),
newWallpaperData.wallpaperFile.getAbsolutePath()); newWallpaperData.getWallpaperFile().getAbsolutePath());
} }
} }
@@ -525,7 +524,8 @@ public class WallpaperManagerServiceTests {
@Test @Test
public void getWallpaperWithFeature_getCropped_returnsCropFile() throws Exception { public void getWallpaperWithFeature_getCropped_returnsCropFile() throws Exception {
File cropSystemWallpaperFile = File cropSystemWallpaperFile =
new File(WallpaperUtils.getWallpaperDir(USER_SYSTEM), WALLPAPER_CROP); new WallpaperData(USER_SYSTEM, FLAG_SYSTEM).getCropFile();
cropSystemWallpaperFile.getParentFile().mkdirs();
cropSystemWallpaperFile.createNewFile(); cropSystemWallpaperFile.createNewFile();
try (FileOutputStream outputStream = new FileOutputStream(cropSystemWallpaperFile)) { try (FileOutputStream outputStream = new FileOutputStream(cropSystemWallpaperFile)) {
outputStream.write("Crop system wallpaper".getBytes()); outputStream.write("Crop system wallpaper".getBytes());
@@ -547,7 +547,8 @@ public class WallpaperManagerServiceTests {
@Test @Test
public void getWallpaperWithFeature_notGetCropped_returnsOriginalFile() throws Exception { public void getWallpaperWithFeature_notGetCropped_returnsOriginalFile() throws Exception {
File originalSystemWallpaperFile = File originalSystemWallpaperFile =
new File(WallpaperUtils.getWallpaperDir(USER_SYSTEM), WALLPAPER); new WallpaperData(USER_SYSTEM, FLAG_SYSTEM).getWallpaperFile();
originalSystemWallpaperFile.getParentFile().mkdirs();
originalSystemWallpaperFile.createNewFile(); originalSystemWallpaperFile.createNewFile();
try (FileOutputStream outputStream = new FileOutputStream(originalSystemWallpaperFile)) { try (FileOutputStream outputStream = new FileOutputStream(originalSystemWallpaperFile)) {
outputStream.write("Original system wallpaper".getBytes()); outputStream.write("Original system wallpaper".getBytes());