feat: rounded corners compat

This commit is contained in:
rebelonion 2024-03-25 23:07:45 -05:00
parent f177e2cf7c
commit 954fdde1c4
3 changed files with 47 additions and 5 deletions

View file

@ -22,8 +22,8 @@ class BitmapUtil {
return output return output
} }
fun convertDrawableToBitmap(drawable: Drawable): Bitmap { fun convertDrawableToBitmap(drawable: Drawable, width: Int, height: Int): Bitmap {
val bitmap = Bitmap.createBitmap(100, 300, Bitmap.Config.ARGB_8888) val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap) val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height) drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas) drawable.draw(canvas)

View file

@ -0,0 +1,40 @@
package ani.dantotsu.widgets
import android.appwidget.AppWidgetManager
import android.content.Context
class WidgetSizeProvider(
private val context: Context // Do not pass Application context
) {
private val appWidgetManager = AppWidgetManager.getInstance(context)
fun getWidgetsSize(widgetId: Int): Pair<Int, Int> {
val isPortrait = context.resources.configuration.orientation == 1
val width = getWidgetWidth(isPortrait, widgetId)
val height = getWidgetHeight(isPortrait, widgetId)
val widthInPx = context.dip(width)
val heightInPx = context.dip(height)
return widthInPx to heightInPx
}
private fun getWidgetWidth(isPortrait: Boolean, widgetId: Int): Int =
if (isPortrait) {
getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
} else {
getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
}
private fun getWidgetHeight(isPortrait: Boolean, widgetId: Int): Int =
if (isPortrait) {
getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)
} else {
getWidgetSizeInDp(widgetId, AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
}
private fun getWidgetSizeInDp(widgetId: Int, key: String): Int =
appWidgetManager.getAppWidgetOptions(widgetId).getInt(key, 0)
private fun Context.dip(value: Int): Int = (value * resources.displayMetrics.density).toInt()
}

View file

@ -14,6 +14,7 @@ import androidx.core.content.res.ResourcesCompat
import ani.dantotsu.MainActivity import ani.dantotsu.MainActivity
import ani.dantotsu.R import ani.dantotsu.R
import ani.dantotsu.util.BitmapUtil.Companion.convertDrawableToBitmap import ani.dantotsu.util.BitmapUtil.Companion.convertDrawableToBitmap
import ani.dantotsu.widgets.WidgetSizeProvider
/** /**
* Implementation of App Widget functionality. * Implementation of App Widget functionality.
@ -70,13 +71,16 @@ class UpcomingWidget : AppWidgetProvider() {
null null
) as GradientDrawable ) as GradientDrawable
gradientDrawable.colors = intArrayOf(backgroundColor, backgroundFade) gradientDrawable.colors = intArrayOf(backgroundColor, backgroundFade)
val widgetSizeProvider = WidgetSizeProvider(context)
val (width, height) = widgetSizeProvider.getWidgetsSize(appWidgetId)
gradientDrawable.cornerRadius = 50f
val intentTemplate = Intent(context, MainActivity::class.java) val intentTemplate = Intent(context, MainActivity::class.java)
intentTemplate.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK intentTemplate.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
intentTemplate.putExtra("fromWidget", true) intentTemplate.putExtra("fromWidget", true)
val views = RemoteViews(context.packageName, R.layout.upcoming_widget).apply { val views = RemoteViews(context.packageName, R.layout.upcoming_widget).apply {
setImageViewBitmap(R.id.backgroundView, convertDrawableToBitmap(gradientDrawable)) setImageViewBitmap(R.id.backgroundView, convertDrawableToBitmap(gradientDrawable, width, height))
setTextColor(R.id.text_show_title, titleTextColor) setTextColor(R.id.text_show_title, titleTextColor)
setTextColor(R.id.text_show_countdown, countdownTextColor) setTextColor(R.id.text_show_countdown, countdownTextColor)
setTextColor(R.id.widgetTitle, titleTextColor) setTextColor(R.id.widgetTitle, titleTextColor)
@ -103,9 +107,7 @@ class UpcomingWidget : AppWidgetProvider() {
) )
) )
} }
return views return views
} }
const val PREFS_NAME = "ani.dantotsu.widgets.UpcomingWidget" const val PREFS_NAME = "ani.dantotsu.widgets.UpcomingWidget"