fix #20, make widget text scaling more dynamic

This commit is contained in:
tibbi 2018-09-18 22:53:15 +02:00
parent 07894ef11b
commit fbb3fc6b7b
3 changed files with 88 additions and 122 deletions

View File

@ -18,6 +18,7 @@ import java.util.*
class MyWidgetDateTimeProvider : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
performUpdate(context)
context.scheduleNextWidgetUpdate()
}
@ -30,13 +31,9 @@ class MyWidgetDateTimeProvider : AppWidgetProvider() {
private fun performUpdate(context: Context) {
val appWidgetManager = AppWidgetManager.getInstance(context)
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
val bundle = appWidgetManager.getAppWidgetOptions(it)
val minHeight = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
val isSmallLayout = getCellsForSize(minHeight) == 1
val layout = if (context.config.useTextShadow) R.layout.widget_date_time_with_shadow else R.layout.widget_date_time
RemoteViews(context.packageName, layout).apply {
updateTexts(context, this, isSmallLayout)
updateTexts(context, this)
updateColors(context, this)
setupAppOpenIntent(context, this)
appWidgetManager.updateAppWidget(it, this)
@ -44,33 +41,14 @@ class MyWidgetDateTimeProvider : AppWidgetProvider() {
}
}
private fun updateTexts(context: Context, views: RemoteViews, isSmallLayout: Boolean) {
val calendar = Calendar.getInstance()
val use24HourFormat = context.config.use24HourFormat
private fun updateTexts(context: Context, views: RemoteViews) {
val timeText = context.getFormattedTime(getPassedSeconds(), false, false).toString()
val dimenResource = if (isSmallLayout) R.dimen.widget_time_text_size_small else R.dimen.widget_time_text_size_big
val timeTextSize = context.resources.getDimension(dimenResource) / context.resources.displayMetrics.density
val topPadding = if (isSmallLayout) 0 else context.resources.getDimension(R.dimen.tiny_margin).toInt()
val bottomPadding = context.resources.getDimension(if (isSmallLayout) R.dimen.tiny_margin else R.dimen.medium_margin).toInt()
val nextAlarm = getFormattedNextAlarm(context)
views.apply {
setViewPadding(R.id.widget_date_time_holder, 0, topPadding, 0, bottomPadding)
if (use24HourFormat) {
setText(R.id.widget_time, timeText)
} else {
val timeParts = timeText.split(" ")
setText(R.id.widget_time, timeParts[0])
setText(R.id.widget_time_am_pm, " ${timeParts[1]}")
}
setText(R.id.widget_date, context.getFormattedDate(calendar))
setTextSize(R.id.widget_time, timeTextSize)
setVisibleIf(R.id.widget_time_am_pm, !use24HourFormat)
val nextAlarm = getFormattedNextAlarm(context)
setVisibleIf(R.id.widget_alarm_holder, nextAlarm.isNotEmpty())
setText(R.id.widget_time, timeText)
setText(R.id.widget_date, context.getFormattedDate(Calendar.getInstance()))
setText(R.id.widget_next_alarm, nextAlarm)
setVisibleIf(R.id.widget_alarm_holder, nextAlarm.isNotEmpty())
}
}
@ -82,7 +60,6 @@ class MyWidgetDateTimeProvider : AppWidgetProvider() {
views.apply {
setBackgroundColor(R.id.widget_date_time_holder, widgetBgColor)
setTextColor(R.id.widget_time, widgetTextColor)
setTextColor(R.id.widget_time_am_pm, widgetTextColor)
setTextColor(R.id.widget_date, widgetTextColor)
setTextColor(R.id.widget_next_alarm, widgetTextColor)
@ -140,8 +117,8 @@ class MyWidgetDateTimeProvider : AppWidgetProvider() {
}
override fun onAppWidgetOptionsChanged(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int, newOptions: Bundle?) {
performUpdate(context)
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions)
performUpdate(context)
}
private fun getMultiplyColoredBitmap(resourceId: Int, newColor: Int, context: Context): Bitmap {
@ -155,12 +132,4 @@ class MyWidgetDateTimeProvider : AppWidgetProvider() {
canvas.drawBitmap(bmp, 0f, 0f, paint)
return bmp
}
private fun getCellsForSize(size: Int): Int {
var n = 2
while (70 * n - 30 < size) {
++n
}
return n - 1
}
}

View File

@ -1,70 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/widget_date_time_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/small_margin"
android:paddingLeft="@dimen/small_margin"
android:paddingRight="@dimen/small_margin">
<RelativeLayout
android:id="@+id/widget_time_holder"
<TextView
android:id="@+id/widget_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="@+id/widget_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:textSize="@dimen/widget_time_text_size_small"
tools:text="00:00"/>
<TextView
android:id="@+id/widget_time_am_pm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/widget_time"
android:layout_toRightOf="@+id/widget_time"
android:includeFontPadding="false"
android:textSize="@dimen/widget_details_text_size"
tools:text=" a.m."/>
</RelativeLayout>
android:layout_height="0dp"
android:layout_weight="4"
android:autoSizeMaxTextSize="300sp"
android:autoSizeMinTextSize="2sp"
android:autoSizeStepGranularity="1sp"
android:autoSizeTextType="uniform"
android:gravity="center"
android:includeFontPadding="false"
android:maxLines="1"
android:textSize="@dimen/normal_text_size"
tools:text="00:00"/>
<TextView
android:id="@+id/widget_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/widget_time_holder"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:autoSizeMinTextSize="2sp"
android:autoSizeStepGranularity="1sp"
android:autoSizeTextType="uniform"
android:gravity="center"
android:includeFontPadding="false"
android:textSize="@dimen/widget_details_text_size"
android:maxLines="1"
android:textSize="@dimen/normal_text_size"
tools:text="Mon, 1 January"/>
<RelativeLayout
android:id="@+id/widget_alarm_holder"
android:layout_width="match_parent"
android:layout_height="@dimen/widget_alarm_icon_size"
android:layout_below="@+id/widget_date"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/widget_next_alarm_image"
android:layout_width="@dimen/widget_alarm_icon_size"
android:layout_height="@dimen/widget_alarm_icon_size"
android:layout_alignBottom="@+id/widget_next_alarm"
android:layout_alignTop="@+id/widget_next_alarm"
android:src="@drawable/ic_clock"/>
<TextView
android:id="@+id/widget_next_alarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/widget_next_alarm_image"
android:gravity="center"
android:includeFontPadding="false"
android:maxLines="1"
android:paddingLeft="@dimen/small_margin"
android:textSize="@dimen/widget_details_text_size"
android:textSize="@dimen/normal_text_size"
tools:text="Tue, 18:30"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>

View File

@ -1,82 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/widget_date_time_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/widget_time_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="@+id/widget_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:shadowColor="@android:color/black"
android:shadowDy="1"
android:shadowRadius="1"
android:textSize="@dimen/widget_time_text_size_small"
tools:text="00:00"/>
<TextView
android:id="@+id/widget_time_am_pm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/widget_time"
android:layout_toRightOf="@+id/widget_time"
android:includeFontPadding="false"
android:shadowColor="@android:color/black"
android:shadowDy="1"
android:shadowRadius="1"
android:textSize="@dimen/widget_details_text_size"
tools:text=" a.m."/>
</RelativeLayout>
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/small_margin"
android:paddingLeft="@dimen/small_margin"
android:paddingRight="@dimen/small_margin">
<TextView
android:id="@+id/widget_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/widget_time_holder"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:id="@+id/widget_time"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:autoSizeMaxTextSize="300sp"
android:autoSizeMinTextSize="2sp"
android:autoSizeStepGranularity="1sp"
android:autoSizeTextType="uniform"
android:gravity="center"
android:includeFontPadding="false"
android:maxLines="1"
android:shadowColor="@android:color/black"
android:shadowDy="1"
android:shadowRadius="1"
android:textSize="@dimen/widget_details_text_size"
android:textSize="@dimen/normal_text_size"
tools:text="00:00"/>
<TextView
android:id="@+id/widget_date"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:autoSizeMinTextSize="2sp"
android:autoSizeStepGranularity="1sp"
android:autoSizeTextType="uniform"
android:gravity="center"
android:includeFontPadding="false"
android:maxLines="1"
android:shadowColor="@android:color/black"
android:shadowDy="1"
android:shadowRadius="1"
android:textSize="@dimen/normal_text_size"
tools:text="Mon, 1 January"/>
<RelativeLayout
android:id="@+id/widget_alarm_holder"
android:layout_width="match_parent"
android:layout_height="@dimen/widget_alarm_icon_size"
android:layout_below="@+id/widget_date"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/widget_next_alarm_image"
android:layout_width="@dimen/widget_alarm_icon_size"
android:layout_height="@dimen/widget_alarm_icon_size"
android:layout_alignBottom="@+id/widget_next_alarm"
android:layout_alignTop="@+id/widget_next_alarm"
android:src="@drawable/ic_clock"/>
<TextView
android:id="@+id/widget_next_alarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/widget_next_alarm_image"
android:gravity="center"
android:includeFontPadding="false"
android:maxLines="1"
android:paddingLeft="@dimen/small_margin"
android:shadowColor="@android:color/black"
android:shadowDy="1"
android:shadowRadius="1"
android:textSize="@dimen/widget_details_text_size"
android:textSize="@dimen/normal_text_size"
tools:text="Tue, 18:30"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>