Skip to content
Merged
Changes from all commits
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
21 changes: 20 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,10 +3,14 @@ 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 androidx.core.content.getSystemService
import com.bnyro.wallpaper.enums.ResizeMethod
import com.bnyro.wallpaper.enums.WallpaperTarget
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -65,17 +69,32 @@ 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
}

// `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)) {

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 = context.getSystemService<DisplayManager>()
?.getDisplay(Display.DEFAULT_DISPLAY)

return display?.rotation ?: Surface.ROTATION_0
}

private fun resizeBitmapByPreference(context: Context, bitmap: Bitmap): Bitmap {
Expand Down
Loading