Wear CWF Allow custom fonts (up to 4) within zip file

This commit is contained in:
Philoul 2023-09-03 17:38:08 +02:00
parent 90cb0fd097
commit 170b2ecec0
2 changed files with 66 additions and 16 deletions

View file

@ -2,6 +2,7 @@ package info.nightscout.rx.weardata
import android.content.res.Resources import android.content.res.Resources
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.Typeface
import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.graphics.drawable.PictureDrawable import android.graphics.drawable.PictureDrawable
@ -49,7 +50,11 @@ enum class ResFileMap(val fileName: String) {
ARROW_FLAT("ArrowFlat"), ARROW_FLAT("ArrowFlat"),
ARROW_FORTY_FIVE_DOWN("Arrow45Down"), ARROW_FORTY_FIVE_DOWN("Arrow45Down"),
ARROW_SINGLE_DOWN("ArrowSingleDown"), ARROW_SINGLE_DOWN("ArrowSingleDown"),
ARROW_DOUBLE_DOWN("ArrowDoubleDown"); ARROW_DOUBLE_DOWN("ArrowDoubleDown"),
FONT1("Font1"),
FONT2("Font2"),
FONT3("Font3"),
FONT4("Font4");
companion object { companion object {
@ -61,7 +66,8 @@ enum class ResFormat(val extension: String) {
UNKNOWN(""), UNKNOWN(""),
SVG("svg"), SVG("svg"),
JPG("jpg"), JPG("jpg"),
PNG("png"); PNG("png"),
TTF("ttf");
companion object { companion object {
@ -96,6 +102,31 @@ data class ResData(val value: ByteArray, val format: ResFormat) {
return null return null
} }
} }
fun toTypeface(): Typeface? {
try {
return when (format) {
ResFormat.TTF -> {
// Workaround with temporary File, Typeface.createFromFileDescriptor(null, value, 0, value.size) more simple not available
val tempFile = File.createTempFile("temp", ".ttf")
val fileOutputStream = FileOutputStream(tempFile)
fileOutputStream.write(value)
fileOutputStream.close()
val typeface = Typeface.createFromFile(tempFile)
tempFile.delete() // delete tempfile after usage
typeface
}
else -> {
null
}
}
} catch (e: Exception) {
return null
}
}
} }
typealias CwfResDataMap = MutableMap<ResFileMap, ResData> typealias CwfResDataMap = MutableMap<ResFileMap, ResData>
@ -224,7 +255,10 @@ enum class JsonKeyValues(val key: String, val jsonKey: JsonKeys) {
BOLD_ITALIC("bold_italic", JsonKeys.FONTSTYLE), BOLD_ITALIC("bold_italic", JsonKeys.FONTSTYLE),
ITALIC("italic", JsonKeys.FONTSTYLE), ITALIC("italic", JsonKeys.FONTSTYLE),
BGCOLOR("bgColor", JsonKeys.COLOR), BGCOLOR("bgColor", JsonKeys.COLOR),
BGCOLOR1("bgColor", JsonKeys.FONTCOLOR) FONT1("font1", JsonKeys.FONTCOLOR),
FONT2("font2", JsonKeys.FONTCOLOR),
FONT3("font3", JsonKeys.FONTCOLOR),
FONT4("font4", JsonKeys.FONTCOLOR)
} }
enum class ViewType(@StringRes val comment: Int?) { enum class ViewType(@StringRes val comment: Int?) {

View file

@ -67,7 +67,7 @@ class CustomWatchface : BaseWatchFace() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
FontMap.init(context) FontMap.init(context, resDataMap)
} }
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
@ -149,6 +149,7 @@ class CustomWatchface : BaseWatchFace() {
try { try {
val json = JSONObject(it.customWatchfaceData.json) val json = JSONObject(it.customWatchfaceData.json)
resDataMap = it.customWatchfaceData.resDatas resDataMap = it.customWatchfaceData.resDatas
FontMap.init(context, resDataMap)
enableSecond = json.optBoolean(ENABLESECOND.key) && sp.getBoolean(R.string.key_show_seconds, true) enableSecond = json.optBoolean(ENABLESECOND.key) && sp.getBoolean(R.string.key_show_seconds, true)
highColor = getColor(json.optString(HIGHCOLOR.key), ContextCompat.getColor(this, R.color.dark_highColor)) highColor = getColor(json.optString(HIGHCOLOR.key), ContextCompat.getColor(this, R.color.dark_highColor))
midColor = getColor(json.optString(MIDCOLOR.key), ContextCompat.getColor(this, R.color.inrange)) midColor = getColor(json.optString(MIDCOLOR.key), ContextCompat.getColor(this, R.color.inrange))
@ -529,20 +530,35 @@ private enum class GravityMap(val key: String, val gravity: Int) {
} }
} }
private enum class FontMap(val key: String, var font: Typeface, @FontRes val fontRessources: Int?) { private enum class FontMap(val key: String, var font: Typeface, @FontRes val fontRessources: Int?, val customFont: ResFileMap?) {
SANS_SERIF(JsonKeyValues.SANS_SERIF.key, Typeface.SANS_SERIF, null), SANS_SERIF(JsonKeyValues.SANS_SERIF.key, Typeface.SANS_SERIF, null, null),
DEFAULT(JsonKeyValues.DEFAULT.key, Typeface.DEFAULT, null), DEFAULT(JsonKeyValues.DEFAULT.key, Typeface.DEFAULT, null, null),
DEFAULT_BOLD(JsonKeyValues.DEFAULT_BOLD.key, Typeface.DEFAULT_BOLD, null), DEFAULT_BOLD(JsonKeyValues.DEFAULT_BOLD.key, Typeface.DEFAULT_BOLD, null, null),
MONOSPACE(JsonKeyValues.MONOSPACE.key, Typeface.MONOSPACE, null), MONOSPACE(JsonKeyValues.MONOSPACE.key, Typeface.MONOSPACE, null, null),
SERIF(JsonKeyValues.SERIF.key, Typeface.SERIF, null), SERIF(JsonKeyValues.SERIF.key, Typeface.SERIF, null, null),
ROBOTO_CONDENSED_BOLD(JsonKeyValues.ROBOTO_CONDENSED_BOLD.key, Typeface.DEFAULT, R.font.roboto_condensed_bold), ROBOTO_CONDENSED_BOLD(JsonKeyValues.ROBOTO_CONDENSED_BOLD.key, Typeface.DEFAULT, R.font.roboto_condensed_bold, null),
ROBOTO_CONDENSED_LIGHT(JsonKeyValues.ROBOTO_CONDENSED_LIGHT.key, Typeface.DEFAULT, R.font.roboto_condensed_light), ROBOTO_CONDENSED_LIGHT(JsonKeyValues.ROBOTO_CONDENSED_LIGHT.key, Typeface.DEFAULT, R.font.roboto_condensed_light, null),
ROBOTO_CONDENSED_REGULAR(JsonKeyValues.ROBOTO_CONDENSED_REGULAR.key, Typeface.DEFAULT, R.font.roboto_condensed_regular), ROBOTO_CONDENSED_REGULAR(JsonKeyValues.ROBOTO_CONDENSED_REGULAR.key, Typeface.DEFAULT, R.font.roboto_condensed_regular, null),
ROBOTO_SLAB_LIGHT(JsonKeyValues.ROBOTO_SLAB_LIGHT.key, Typeface.DEFAULT, R.font.roboto_slab_light); ROBOTO_SLAB_LIGHT(JsonKeyValues.ROBOTO_SLAB_LIGHT.key, Typeface.DEFAULT, R.font.roboto_slab_light, null),
FONT1(JsonKeyValues.FONT1.key, Typeface.DEFAULT, null, ResFileMap.FONT1),
FONT2(JsonKeyValues.FONT2.key, Typeface.DEFAULT, null, ResFileMap.FONT2),
FONT3(JsonKeyValues.FONT3.key, Typeface.DEFAULT, null, ResFileMap.FONT3),
FONT4(JsonKeyValues.FONT4.key, Typeface.DEFAULT, null, ResFileMap.FONT4);
companion object { companion object {
fun init(context: Context) = values().forEach { it.font = it.fontRessources?.let { font -> ResourcesCompat.getFont(context, font) } ?: it.font } fun init(context: Context, resDataMap: CwfResDataMap) = values().forEach { fontMap ->
fontMap.customFont?.let { customFont ->
fontMap.font = Typeface.DEFAULT
resDataMap[customFont]?.toTypeface()?.let { resData ->
fontMap.font = resData
}
} ?: run {
fontMap.font = fontMap.fontRessources?.let { fontResource ->
ResourcesCompat.getFont(context, fontResource)
} ?: fontMap.font
}
}
fun font(key: String) = values().firstOrNull { it.key == key }?.font ?: DEFAULT.font fun font(key: String) = values().firstOrNull { it.key == key }?.font ?: DEFAULT.font
fun key() = DEFAULT.key fun key() = DEFAULT.key
} }