From 52d63e7e4c3e0db42f72f8f55453a47d62b8aa22 Mon Sep 17 00:00:00 2001 From: Ahan Wu Date: Tue, 12 Apr 2022 10:19:52 +0000 Subject: [PATCH] DO NOT MERGE: Don't do heavy operation while in the synchronized block We read the wallpaper within the synchronized block, this is a long time operation, might block the other thread who is going to obtain the lock. So we have to move the operation beyond the synchronized block. Bug: 228435216 Test: atest WallpaperManagerTest#testPeekWallpaperNotBlockingOtherThread Change-Id: Ic2a947b473710ffe68db436586769858950588a4 --- core/java/android/app/WallpaperManager.java | 38 +++++++++++---------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/core/java/android/app/WallpaperManager.java b/core/java/android/app/WallpaperManager.java index 0a18588e0131d..e022ca3066744 100644 --- a/core/java/android/app/WallpaperManager.java +++ b/core/java/android/app/WallpaperManager.java @@ -67,7 +67,6 @@ import android.os.ParcelFileDescriptor; import android.os.RemoteException; import android.os.StrictMode; import android.os.SystemProperties; -import android.service.wallpaper.WallpaperService; import android.text.TextUtils; import android.util.ArrayMap; import android.util.ArraySet; @@ -559,23 +558,26 @@ public class WallpaperManager { } mCachedWallpaper = null; mCachedWallpaperUserId = 0; - try { - mCachedWallpaper = getCurrentWallpaperLocked( - context, userId, hardware, cmProxy); - mCachedWallpaperUserId = userId; - } catch (OutOfMemoryError e) { - Log.w(TAG, "Out of memory loading the current wallpaper: " + e); - } catch (SecurityException e) { - if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O_MR1) { - Log.w(TAG, "No permission to access wallpaper, suppressing" - + " exception to avoid crashing legacy app."); - } else { - // Post-O apps really most sincerely need the permission. - throw e; + } + try { + Bitmap currentWallpaper = getCurrentWallpaperLocked( + context, userId, hardware, cmProxy); + if (currentWallpaper != null) { + synchronized (this) { + mCachedWallpaper = currentWallpaper; + mCachedWallpaperUserId = userId; + return mCachedWallpaper; } } - if (mCachedWallpaper != null) { - return mCachedWallpaper; + } catch (OutOfMemoryError e) { + Log.w(TAG, "Out of memory loading the current wallpaper: " + e); + } catch (SecurityException e) { + if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O_MR1) { + Log.w(TAG, "No permission to access wallpaper, suppressing" + + " exception to avoid crashing legacy app."); + } else { + // Post-O apps really most sincerely need the permission. + throw e; } } if (returnDefault) { @@ -2472,7 +2474,7 @@ public class WallpaperManager { * * @param colors Wallpaper color info, {@code null} when not available. * @param which A combination of {@link #FLAG_LOCK} and {@link #FLAG_SYSTEM} - * @see WallpaperService.Engine#onComputeColors() + * @see android.service.wallpaper.WallpaperService.Engine#onComputeColors() */ void onColorsChanged(@Nullable WallpaperColors colors, int which); @@ -2484,7 +2486,7 @@ public class WallpaperManager { * @param colors Wallpaper color info, {@code null} when not available. * @param which A combination of {@link #FLAG_LOCK} and {@link #FLAG_SYSTEM} * @param userId Owner of the wallpaper - * @see WallpaperService.Engine#onComputeColors() + * @see android.service.wallpaper.WallpaperService.Engine#onComputeColors() * @hide */ default void onColorsChanged(@Nullable WallpaperColors colors, int which, int userId) {