From 018b3eabf03e763e8d86454e393eb59c89317fd9 Mon Sep 17 00:00:00 2001 From: Crosswind Date: Sun, 30 Aug 2026 22:42:56 -0700 Subject: [PATCH 1/2] fix: size wallpapers against the display's natural orientation --- .../bnyro/wallpaper/util/WallpaperHelper.kt | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/bnyro/wallpaper/util/WallpaperHelper.kt b/app/src/main/java/com/bnyro/wallpaper/util/WallpaperHelper.kt index 57400194..c7a9d969 100644 --- a/app/src/main/java/com/bnyro/wallpaper/util/WallpaperHelper.kt +++ b/app/src/main/java/com/bnyro/wallpaper/util/WallpaperHelper.kt @@ -3,8 +3,11 @@ package com.bnyro.wallpaper.util import android.app.WallpaperManager import android.content.Context import android.graphics.Bitmap +import android.hardware.display.DisplayManager import android.os.Build import android.util.DisplayMetrics +import android.view.Display +import android.view.Surface import android.view.WindowManager import androidx.annotation.RequiresApi import com.bnyro.wallpaper.enums.ResizeMethod @@ -65,10 +68,11 @@ object WallpaperHelper { } } + /** The display size in its natural orientation, which is how a wallpaper is stored. */ private fun getMetrics(context: Context): Pair { val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + val (width, height) = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { windowManager.currentWindowMetrics.bounds.let { it.width() to it.height() } } else { val metrics = DisplayMetrics() @@ -76,6 +80,23 @@ object WallpaperHelper { windowManager.defaultDisplay.getMetrics(metrics) metrics.widthPixels to metrics.heightPixels } + + return when (getDisplayRotation(context)) { + Surface.ROTATION_90, Surface.ROTATION_270 -> height to width + else -> width to height + } + } + + private fun getDisplayRotation(context: Context): Int { + val display = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + (context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager) + .getDisplay(Display.DEFAULT_DISPLAY) + } else { + @Suppress("DEPRECATION") + (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay + } + + return display?.rotation ?: Surface.ROTATION_0 } private fun resizeBitmapByPreference(context: Context, bitmap: Bitmap): Bitmap { From 93265c87248ea70e722e3f99a26ca53f8c3969d7 Mon Sep 17 00:00:00 2001 From: Chunji Wang <7267426+chunjiw@users.noreply.github.com> Date: Thu, 10 Sep 2026 17:26:11 -0700 Subject: [PATCH 2/2] refactor: simplify display rotation lookup and document orientation handling --- .../java/com/bnyro/wallpaper/util/WallpaperHelper.kt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/bnyro/wallpaper/util/WallpaperHelper.kt b/app/src/main/java/com/bnyro/wallpaper/util/WallpaperHelper.kt index c7a9d969..2fc4a282 100644 --- a/app/src/main/java/com/bnyro/wallpaper/util/WallpaperHelper.kt +++ b/app/src/main/java/com/bnyro/wallpaper/util/WallpaperHelper.kt @@ -10,6 +10,7 @@ import android.view.Display import android.view.Surface import android.view.WindowManager import androidx.annotation.RequiresApi +import androidx.core.content.getSystemService import com.bnyro.wallpaper.enums.ResizeMethod import com.bnyro.wallpaper.enums.WallpaperTarget import kotlinx.coroutines.Dispatchers @@ -81,6 +82,8 @@ object WallpaperHelper { metrics.widthPixels to metrics.heightPixels } + // `width` and `height` depend on the current screen orientation. To get the real metrics, + // we additionally check the current rotation of the display. return when (getDisplayRotation(context)) { Surface.ROTATION_90, Surface.ROTATION_270 -> height to width else -> width to height @@ -88,13 +91,8 @@ object WallpaperHelper { } private fun getDisplayRotation(context: Context): Int { - val display = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - (context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager) - .getDisplay(Display.DEFAULT_DISPLAY) - } else { - @Suppress("DEPRECATION") - (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay - } + val display = context.getSystemService() + ?.getDisplay(Display.DEFAULT_DISPLAY) return display?.rotation ?: Surface.ROTATION_0 }