-
Notifications
You must be signed in to change notification settings - Fork 1
/
TodayWidget.kt
66 lines (62 loc) · 2.81 KB
/
TodayWidget.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ir.mahdiparastesh.fortuna.misc
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.icu.util.Calendar
import android.widget.RemoteViews
import androidx.core.graphics.drawable.toBitmap
import com.google.android.material.shape.CornerFamily
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import ir.mahdiparastesh.fortuna.Kit
import ir.mahdiparastesh.fortuna.Kit.create
import ir.mahdiparastesh.fortuna.Kit.isLandscape
import ir.mahdiparastesh.fortuna.Kit.sp
import ir.mahdiparastesh.fortuna.R
import ir.mahdiparastesh.fortuna.misc.Numerals.write
/**
* An app widget for notifying the user of the current date in the current calendar with the chosen
* type of numerals.
*
* AppWidgetProvider sucks when using ADB; it doesn't update properly!
*/
class TodayWidget : AppWidgetProvider() {
override fun onUpdate(c: Context, manager: AppWidgetManager, ids: IntArray) {
super.onUpdate(c, manager, ids)
ids.forEach { id -> manager.updateAppWidget(id, update(c)) }
}
companion object {
fun update(c: Context) = RemoteViews(c.packageName, R.layout.today_widget).apply {
val cal = Kit.calType.create()
val den = c.resources.displayMetrics.density
setImageViewBitmap(
R.id.bg, MaterialShapeDrawable(
ShapeAppearanceModel.Builder().setAllCorners(CornerFamily.CUT, den * 8f).build()
).let {
it.fillColor = c.resources.getColorStateList(R.color.todayWidget, null)
val size = if (!c.isLandscape()) arrayOf(70, 100) else arrayOf(140, 50)
it.toBitmap((den * size[0]).toInt(), (den * size[1]).toInt())
})
setOnClickPendingIntent(R.id.root, Kit.openInDate(c, cal, 1))
setTextViewText(
R.id.dies, Numerals.build(c.sp().getString(Kit.SP_NUMERAL_TYPE, Kit.defNumType)
.let { if (it == Kit.defNumType) null else it })
.write(cal[Calendar.DAY_OF_MONTH])
)
val month = c.resources.getStringArray(R.array.luna)[cal[Calendar.MONTH]]
if (c.isLandscape()) {
setTextViewText(R.id.luna, "$month\n${cal[Calendar.YEAR]}")
setTextViewText(R.id.year, "")
} else {
setTextViewText(R.id.luna, month)
setTextViewText(R.id.year, "${cal[Calendar.YEAR]}")
}
} // RemoteViews has no getters.
fun externalUpdate(c: Context) {
AppWidgetManager.getInstance(c).updateAppWidget(
ComponentName(c, TodayWidget::class.java.name), update(c)
)
}
}
}