|
1 | 1 | package com.anthonyla.paperize.core.util |
2 | 2 |
|
| 3 | +import android.app.WallpaperManager |
| 4 | +import android.content.res.Configuration |
3 | 5 | import android.graphics.Bitmap |
4 | 6 | import android.graphics.Color |
5 | 7 | import android.hardware.display.DisplayManager |
6 | 8 | import android.net.Uri |
7 | 9 | import android.os.Build |
8 | | -import android.content.res.Configuration |
| 10 | +import android.os.SystemClock |
9 | 11 | import androidx.exifinterface.media.ExifInterface |
10 | 12 | import androidx.test.core.app.ApplicationProvider |
11 | 13 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
@@ -225,6 +227,55 @@ class WallpaperUtilInstrumentedTest { |
225 | 227 | result.recycle() |
226 | 228 | } |
227 | 229 |
|
| 230 | + @Test |
| 231 | + fun staticWallpaperChangesRefreshAospWallpaperColors() { |
| 232 | + assumeTrue( |
| 233 | + "Wallpaper color extraction test is destructive and must run only on an emulator", |
| 234 | + Build.FINGERPRINT.contains("/emu") || |
| 235 | + Build.FINGERPRINT.contains("generic") || |
| 236 | + Build.MODEL.contains("Emulator", ignoreCase = true) || |
| 237 | + Build.MODEL.contains("sdk", ignoreCase = true) |
| 238 | + ) |
| 239 | + val wallpaperManager = WallpaperManager.getInstance(context) |
| 240 | + val red = mutableBitmap(256, 256, Color.rgb(230, 20, 20)) |
| 241 | + val blue = mutableBitmap(256, 256, Color.rgb(20, 20, 230)) |
| 242 | + |
| 243 | + try { |
| 244 | + wallpaperManager.setBitmapChecked(red, WallpaperManager.FLAG_SYSTEM) |
| 245 | + val redPrimary = awaitPrimaryWallpaperColor(wallpaperManager) { color -> |
| 246 | + Color.red(color) > Color.blue(color) * 2 |
| 247 | + } |
| 248 | + |
| 249 | + wallpaperManager.setBitmapChecked(blue, WallpaperManager.FLAG_SYSTEM) |
| 250 | + val bluePrimary = awaitPrimaryWallpaperColor(wallpaperManager) { color -> |
| 251 | + Color.blue(color) > Color.red(color) * 2 |
| 252 | + } |
| 253 | + |
| 254 | + assertTrue(Color.red(redPrimary) > Color.blue(redPrimary)) |
| 255 | + assertTrue(Color.blue(bluePrimary) > Color.red(bluePrimary)) |
| 256 | + } finally { |
| 257 | + red.recycle() |
| 258 | + blue.recycle() |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + private fun awaitPrimaryWallpaperColor( |
| 263 | + wallpaperManager: WallpaperManager, |
| 264 | + predicate: (Int) -> Boolean |
| 265 | + ): Int { |
| 266 | + val deadline = SystemClock.uptimeMillis() + 10_000L |
| 267 | + var lastColor: Int? = null |
| 268 | + while (SystemClock.uptimeMillis() < deadline) { |
| 269 | + lastColor = wallpaperManager |
| 270 | + .getWallpaperColors(WallpaperManager.FLAG_SYSTEM) |
| 271 | + ?.primaryColor |
| 272 | + ?.toArgb() |
| 273 | + if (lastColor != null && predicate(lastColor)) return lastColor |
| 274 | + SystemClock.sleep(100L) |
| 275 | + } |
| 276 | + throw AssertionError("Wallpaper colors did not refresh; last primary color was $lastColor") |
| 277 | + } |
| 278 | + |
228 | 279 | private fun mutableBitmap(width: Int, height: Int, color: Int): Bitmap = |
229 | 280 | Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).apply { |
230 | 281 | eraseColor(color) |
|
0 commit comments