Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion app/src/main/java/com/bnyro/wallpaper/util/WallpaperHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,17 +68,35 @@ object WallpaperHelper {
}
}

/** The display size in its natural orientation, which is how a wallpaper is stored. */
private fun getMetrics(context: Context): Pair<Int, Int> {
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()
@Suppress("DEPRECATION")
windowManager.defaultDisplay.getMetrics(metrics)
metrics.widthPixels to metrics.heightPixels
}

return when (getDisplayRotation(context)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return when (getDisplayRotation(context)) {
// `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)) {

Please add a short comment here to explain what we're doing, e.g. as the one proposed here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added~

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the DisplayManager way works for all Android versions, so why do we fall back to the WindowManager on old Android versions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, fixed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager)
.getDisplay(Display.DEFAULT_DISPLAY)
context.getSystemService<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 {
Expand Down
Loading