fix: stabilize synchronized changes and foldable rendering
This commit is contained in:
1 parent
3793aaae65
commit
bc513d666e
8 files changed
+213
-73
No files matched your search
@@ -119,9 +119,11 @@ dependencies {
|
||||
testImplementation(libs.junit)
|
||||
testImplementation(libs.mockk)
|
||||
testImplementation(libs.kotlinx.coroutines.test)
|
||||
androidTestImplementation(platform(libs.androidx.compose.bom))
|
||||
androidTestImplementation(libs.androidx.junit)
|
||||
androidTestImplementation(libs.androidx.espresso.core)
|
||||
androidTestImplementation(libs.androidx.ui.test.junit4)
|
||||
debugImplementation(platform(libs.androidx.compose.bom))
|
||||
debugImplementation(libs.androidx.ui.tooling)
|
||||
debugImplementation(libs.androidx.ui.test.manifest)
|
||||
implementation(libs.hilt.android)
|
||||
@@ -133,4 +135,4 @@ dependencies {
|
||||
implementation(libs.dfc)
|
||||
implementation (libs.kotlinx.serialization.json)
|
||||
implementation(libs.toolbar.compose)
|
||||
}
|
||||
}
|
||||
@@ -21,11 +21,13 @@ import android.graphics.RadialGradient
|
||||
import android.graphics.RenderEffect
|
||||
import android.graphics.RenderNode
|
||||
import android.graphics.Shader
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.hardware.HardwareBuffer
|
||||
import android.media.ImageReader
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import android.util.Size
|
||||
import android.view.Display
|
||||
import android.view.WindowManager
|
||||
import android.view.WindowMetrics
|
||||
import androidx.compose.ui.util.fastRoundToInt
|
||||
@@ -126,10 +128,50 @@ object ScreenMetricsCompat {
|
||||
fun getScreenSize(context: Context): Size {
|
||||
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
|
||||
val metrics: WindowMetrics = windowManager.currentWindowMetrics
|
||||
return Size(metrics.bounds.width(), metrics.bounds.height())
|
||||
val candidates = buildList {
|
||||
add(metrics.bounds.width() to metrics.bounds.height())
|
||||
|
||||
// Foldables may expose the cover and inner panels as separate internal displays.
|
||||
// Include every supported internal-display mode so a wallpaper changed while folded
|
||||
// is still rendered sharply enough for the larger unfolded panel. External displays
|
||||
// are deliberately excluded so a connected monitor cannot inflate wallpaper memory.
|
||||
val displayManager = context.getSystemService(DisplayManager::class.java)
|
||||
displayManager?.displays
|
||||
?.asSequence()
|
||||
?.filter { display ->
|
||||
display.flags and Display.FLAG_PRESENTATION == 0 &&
|
||||
display.flags and Display.FLAG_PRIVATE == 0
|
||||
}
|
||||
?.flatMap { display ->
|
||||
display.supportedModes.asSequence().map { mode ->
|
||||
mode.physicalWidth to mode.physicalHeight
|
||||
}
|
||||
}
|
||||
?.forEach(::add)
|
||||
}
|
||||
val largest = selectLargestDisplayDimensions(candidates)
|
||||
return largest?.let { Size(it.first, it.second) }
|
||||
?: Size(metrics.bounds.width(), metrics.bounds.height())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Select the highest-resolution display dimensions.
|
||||
*
|
||||
* Kept platform-independent so foldable sizing behavior is covered by local unit tests.
|
||||
*/
|
||||
internal fun selectLargestDisplayDimensions(
|
||||
candidates: Iterable<Pair<Int, Int>>
|
||||
): Pair<Int, Int>? = candidates
|
||||
.filter { (width, height) -> width > 0 && height > 0 }
|
||||
.maxWithOrNull(
|
||||
compareBy<Pair<Int, Int>>(
|
||||
{ (width, height) -> width.toLong() * height.toLong() },
|
||||
{ (width, height) -> maxOf(width, height) },
|
||||
{ (width, height) -> minOf(width, height) }
|
||||
)
|
||||
)
|
||||
|
||||
/**
|
||||
* Get device screen size with orientation
|
||||
*/
|
||||
@@ -169,8 +211,12 @@ fun getWallpaperRenderSize(context: Context, screenType: com.anthonyla.paperize.
|
||||
val desiredW = wm.desiredMinimumWidth
|
||||
val desiredH = wm.desiredMinimumHeight
|
||||
// desiredMinimumWidth/Height return 0 when the launcher hasn't set a preference yet.
|
||||
// Fall back to physical screen size in that case.
|
||||
if (desiredW > 0 && desiredH > 0) Size(desiredW, desiredH) else screen
|
||||
// Never render below the largest internal panel's resolution on a foldable.
|
||||
if (desiredW > 0 && desiredH > 0) {
|
||||
Size(maxOf(desiredW, screen.width), maxOf(desiredH, screen.height))
|
||||
} else {
|
||||
screen
|
||||
}
|
||||
}
|
||||
else -> screen
|
||||
}
|
||||
|
||||
+43
-26
@@ -157,14 +157,31 @@ class WallpaperChangeService : Service() {
|
||||
throw IllegalStateException("Bitmap has been recycled")
|
||||
}
|
||||
|
||||
// Set for home screen (rendered at parallax canvas size)
|
||||
wallpaperManager.setBitmap(
|
||||
bitmap,
|
||||
null,
|
||||
true,
|
||||
WallpaperManager.FLAG_SYSTEM
|
||||
)
|
||||
Log.d(TAG, "Home wallpaper set in BOTH mode")
|
||||
val canSetAtomically =
|
||||
settings.homeEffects == settings.lockEffects &&
|
||||
settings.homeScalingType == settings.lockScalingType
|
||||
|
||||
if (canSetAtomically) {
|
||||
// A single platform transaction prevents home and lock
|
||||
// from diverging (or one side reverting to the stock
|
||||
// wallpaper) if a second setBitmap call fails.
|
||||
wallpaperManager.setBitmap(
|
||||
bitmap,
|
||||
null,
|
||||
true,
|
||||
WallpaperManager.FLAG_SYSTEM or WallpaperManager.FLAG_LOCK
|
||||
)
|
||||
Log.d(TAG, "Home and lock wallpaper set atomically")
|
||||
} else {
|
||||
// Different effects/scaling require distinct render sizes.
|
||||
wallpaperManager.setBitmap(
|
||||
bitmap,
|
||||
null,
|
||||
true,
|
||||
WallpaperManager.FLAG_SYSTEM
|
||||
)
|
||||
Log.d(TAG, "Home wallpaper set in BOTH mode")
|
||||
}
|
||||
|
||||
// Keep LOCK queue in sync with HOME so that if the user later
|
||||
// switches to separate schedules, both screens continue from
|
||||
@@ -196,25 +213,25 @@ class WallpaperChangeService : Service() {
|
||||
bitmap.recycle()
|
||||
}
|
||||
|
||||
// Render a separate bitmap for the lock screen at physical
|
||||
// screen dimensions. The HOME bitmap was sized for the
|
||||
// launcher's parallax canvas which is typically wider than
|
||||
// the screen; reusing it for LOCK would let Android
|
||||
// center-crop it, defeating FIT/NONE scaling modes.
|
||||
val lockResult = reapplyEffectsUseCase(homeAlbumId, ScreenType.LOCK)
|
||||
lockResult.onSuccess { lockBitmap ->
|
||||
try {
|
||||
wallpaperManager.setBitmap(
|
||||
lockBitmap, null, true, WallpaperManager.FLAG_LOCK
|
||||
)
|
||||
Log.d(TAG, "Lock wallpaper set separately in BOTH mode")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error setting lock wallpaper in BOTH mode", e)
|
||||
} finally {
|
||||
lockBitmap.recycle()
|
||||
if (settings.homeEffects != settings.lockEffects ||
|
||||
settings.homeScalingType != settings.lockScalingType) {
|
||||
// Render a separate bitmap only when the user deliberately
|
||||
// configured different lock-screen presentation settings.
|
||||
val lockResult = reapplyEffectsUseCase(homeAlbumId, ScreenType.LOCK)
|
||||
lockResult.onSuccess { lockBitmap ->
|
||||
try {
|
||||
wallpaperManager.setBitmap(
|
||||
lockBitmap, null, true, WallpaperManager.FLAG_LOCK
|
||||
)
|
||||
Log.d(TAG, "Lock wallpaper set separately in BOTH mode")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error setting lock wallpaper in BOTH mode", e)
|
||||
} finally {
|
||||
lockBitmap.recycle()
|
||||
}
|
||||
}.onError { error ->
|
||||
Log.w(TAG, "Lock rerender failed in BOTH mode: ${error.message}")
|
||||
}
|
||||
}.onError { error ->
|
||||
Log.w(TAG, "Lock rerender failed in BOTH mode: ${error.message}")
|
||||
}
|
||||
}.onError { error ->
|
||||
if (error is EmptyAlbumException) {
|
||||
|
||||
@@ -113,14 +113,27 @@ class WallpaperChangeWorker @AssistedInject constructor(
|
||||
|
||||
Log.d(TAG, "Setting both screens - size: ${bitmap.width}x${bitmap.height}, config: ${bitmap.config}")
|
||||
|
||||
// Set HOME (rendered at parallax canvas size)
|
||||
wallpaperManager.setBitmap(
|
||||
bitmap,
|
||||
null,
|
||||
true,
|
||||
WallpaperManager.FLAG_SYSTEM
|
||||
)
|
||||
Log.d(TAG, "Home wallpaper set in BOTH mode")
|
||||
val canSetAtomically =
|
||||
settings.homeEffects == settings.lockEffects &&
|
||||
settings.homeScalingType == settings.lockScalingType
|
||||
|
||||
if (canSetAtomically) {
|
||||
wallpaperManager.setBitmap(
|
||||
bitmap,
|
||||
null,
|
||||
true,
|
||||
WallpaperManager.FLAG_SYSTEM or WallpaperManager.FLAG_LOCK
|
||||
)
|
||||
Log.d(TAG, "Home and lock wallpaper set atomically")
|
||||
} else {
|
||||
wallpaperManager.setBitmap(
|
||||
bitmap,
|
||||
null,
|
||||
true,
|
||||
WallpaperManager.FLAG_SYSTEM
|
||||
)
|
||||
Log.d(TAG, "Home wallpaper set in BOTH mode")
|
||||
}
|
||||
|
||||
// Keep LOCK queue in sync with HOME so that if the user later
|
||||
// switches to separate schedules, both screens continue from
|
||||
@@ -151,22 +164,23 @@ class WallpaperChangeWorker @AssistedInject constructor(
|
||||
bitmap.recycle()
|
||||
}
|
||||
|
||||
// Render a separate bitmap for the lock screen at physical
|
||||
// screen dimensions (no parallax on lock screen).
|
||||
val lockResult = reapplyEffectsUseCase(homeAlbumId, ScreenType.LOCK)
|
||||
lockResult.onSuccess { lockBitmap ->
|
||||
try {
|
||||
wallpaperManager.setBitmap(
|
||||
lockBitmap, null, true, WallpaperManager.FLAG_LOCK
|
||||
)
|
||||
Log.d(TAG, "Lock wallpaper set separately in BOTH mode")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error setting lock wallpaper in BOTH mode", e)
|
||||
} finally {
|
||||
lockBitmap.recycle()
|
||||
if (settings.homeEffects != settings.lockEffects ||
|
||||
settings.homeScalingType != settings.lockScalingType) {
|
||||
val lockResult = reapplyEffectsUseCase(homeAlbumId, ScreenType.LOCK)
|
||||
lockResult.onSuccess { lockBitmap ->
|
||||
try {
|
||||
wallpaperManager.setBitmap(
|
||||
lockBitmap, null, true, WallpaperManager.FLAG_LOCK
|
||||
)
|
||||
Log.d(TAG, "Lock wallpaper set separately in BOTH mode")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error setting lock wallpaper in BOTH mode", e)
|
||||
} finally {
|
||||
lockBitmap.recycle()
|
||||
}
|
||||
}.onError { error ->
|
||||
Log.w(TAG, "Lock rerender failed in BOTH mode: ${error.message}")
|
||||
}
|
||||
}.onError { error ->
|
||||
Log.w(TAG, "Lock rerender failed in BOTH mode: ${error.message}")
|
||||
}
|
||||
}.onError { error ->
|
||||
if (error is EmptyAlbumException) {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.anthonyla.paperize.core.util
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
|
||||
class ScreenMetricsCompatTest {
|
||||
|
||||
@Test
|
||||
fun `largest internal display wins even when current window is folded`() {
|
||||
val result = selectLargestDisplayDimensions(
|
||||
listOf(
|
||||
1080 to 2092,
|
||||
1840 to 2208,
|
||||
920 to 1104
|
||||
)
|
||||
)
|
||||
|
||||
assertEquals(1840 to 2208, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `selection uses pixel area instead of only one dimension`() {
|
||||
val result = selectLargestDisplayDimensions(
|
||||
listOf(
|
||||
2400 to 800,
|
||||
1800 to 1400
|
||||
)
|
||||
)
|
||||
|
||||
assertEquals(1800 to 1400, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `invalid dimensions are ignored`() {
|
||||
val result = selectLargestDisplayDimensions(
|
||||
listOf(
|
||||
0 to 2208,
|
||||
-1 to 1080
|
||||
)
|
||||
)
|
||||
|
||||
assertNull(result)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user