widget landscape sizing
This commit is contained in:
parent
59473dab64
commit
25ae1a6c1e
1 changed files with 18 additions and 7 deletions
|
@ -5,6 +5,8 @@ import android.appwidget.AppWidgetManager
|
||||||
import android.appwidget.AppWidgetProvider
|
import android.appwidget.AppWidgetProvider
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.res.Configuration
|
||||||
|
import android.content.res.Resources
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
@ -62,6 +64,18 @@ class HomeWidgetProvider : AppWidgetProvider() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getDevicePixelRatio(): Float = Resources.getSystem().displayMetrics.density
|
||||||
|
|
||||||
|
private fun getWidgetSizePx(context: Context, widgetInfo: Bundle): Pair<Int, Int> {
|
||||||
|
val devicePixelRatio = getDevicePixelRatio()
|
||||||
|
val isPortrait = context.resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT
|
||||||
|
val widthKey = if (isPortrait) AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH else AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH
|
||||||
|
val heightKey = if (isPortrait) AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT else AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT
|
||||||
|
val widthPx = (widgetInfo.getInt(widthKey) * devicePixelRatio).roundToInt()
|
||||||
|
val heightPx = (widgetInfo.getInt(heightKey) * devicePixelRatio).roundToInt()
|
||||||
|
return Pair(widthPx, heightPx)
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun getBytes(
|
private suspend fun getBytes(
|
||||||
context: Context,
|
context: Context,
|
||||||
widgetId: Int,
|
widgetId: Int,
|
||||||
|
@ -69,9 +83,7 @@ class HomeWidgetProvider : AppWidgetProvider() {
|
||||||
drawEntryImage: Boolean,
|
drawEntryImage: Boolean,
|
||||||
reuseEntry: Boolean = false,
|
reuseEntry: Boolean = false,
|
||||||
): ByteArray? {
|
): ByteArray? {
|
||||||
val devicePixelRatio = context.resources.displayMetrics.density
|
val (widthPx, heightPx) = getWidgetSizePx(context, widgetInfo)
|
||||||
val widthPx = (widgetInfo.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) * devicePixelRatio).roundToInt()
|
|
||||||
val heightPx = (widgetInfo.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT) * devicePixelRatio).roundToInt()
|
|
||||||
if (widthPx == 0 || heightPx == 0) return null
|
if (widthPx == 0 || heightPx == 0) return null
|
||||||
|
|
||||||
initFlutterEngine(context)
|
initFlutterEngine(context)
|
||||||
|
@ -85,7 +97,7 @@ class HomeWidgetProvider : AppWidgetProvider() {
|
||||||
"widgetId" to widgetId,
|
"widgetId" to widgetId,
|
||||||
"widthPx" to widthPx,
|
"widthPx" to widthPx,
|
||||||
"heightPx" to heightPx,
|
"heightPx" to heightPx,
|
||||||
"devicePixelRatio" to devicePixelRatio,
|
"devicePixelRatio" to getDevicePixelRatio(),
|
||||||
"drawEntryImage" to drawEntryImage,
|
"drawEntryImage" to drawEntryImage,
|
||||||
"reuseEntry" to reuseEntry,
|
"reuseEntry" to reuseEntry,
|
||||||
), object : MethodChannel.Result {
|
), object : MethodChannel.Result {
|
||||||
|
@ -120,9 +132,8 @@ class HomeWidgetProvider : AppWidgetProvider() {
|
||||||
) {
|
) {
|
||||||
bytes ?: return
|
bytes ?: return
|
||||||
|
|
||||||
val devicePixelRatio = context.resources.displayMetrics.density
|
val (widthPx, heightPx) = getWidgetSizePx(context, widgetInfo)
|
||||||
val widthPx = (widgetInfo.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH) * devicePixelRatio).roundToInt()
|
if (widthPx == 0 || heightPx == 0) return
|
||||||
val heightPx = (widgetInfo.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT) * devicePixelRatio).roundToInt()
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val bitmap = Bitmap.createBitmap(widthPx, heightPx, Bitmap.Config.ARGB_8888)
|
val bitmap = Bitmap.createBitmap(widthPx, heightPx, Bitmap.Config.ARGB_8888)
|
||||||
|
|
Loading…
Reference in a new issue