Avoid crash of Watchface (even on wrong type of data within json file)

This commit is contained in:
Philoul 2023-08-11 22:00:19 +02:00
parent f8dbcf2edd
commit af2f18acd9
3 changed files with 87 additions and 84 deletions

View file

@ -17,6 +17,7 @@ import info.nightscout.rx.events.EventLoopUpdateGui
import info.nightscout.rx.events.EventMobileToWear import info.nightscout.rx.events.EventMobileToWear
import info.nightscout.rx.events.EventOverviewBolusProgress import info.nightscout.rx.events.EventOverviewBolusProgress
import info.nightscout.rx.events.EventPreferenceChange import info.nightscout.rx.events.EventPreferenceChange
import info.nightscout.rx.events.EventWearUpdateGui
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.weardata.CustomWatchfaceData import info.nightscout.rx.weardata.CustomWatchfaceData
import info.nightscout.rx.weardata.EventData import info.nightscout.rx.weardata.EventData
@ -91,6 +92,10 @@ class WearPlugin @Inject constructor(
.toObservable(EventLoopUpdateGui::class.java) .toObservable(EventLoopUpdateGui::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ dataHandlerMobile.resendData("EventLoopUpdateGui") }, fabricPrivacy::logException) .subscribe({ dataHandlerMobile.resendData("EventLoopUpdateGui") }, fabricPrivacy::logException)
disposable += rxBus
.toObservable(EventWearUpdateGui::class.java)
.observeOn(aapsSchedulers.main)
.subscribe({ it.customWatchfaceData?.let { cwf -> savedCustomWatchface = cwf } }, fabricPrivacy::logException)
} }
override fun onStop() { override fun onStop() {

View file

@ -106,7 +106,7 @@
<androidx.gridlayout.widget.GridLayout <androidx.gridlayout.widget.GridLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="0dp" android:layout_marginTop="20dp"
android:padding="10dip" android:padding="10dip"
app:columnCount="2"> app:columnCount="2">

View file

@ -116,13 +116,14 @@ class CustomWatchface : BaseWatchFace() {
} }
override fun updateSecondVisibility() { override fun updateSecondVisibility() {
binding.second.visibility = (enableSecond && showSecond).toVisibility() binding.second.visibility = showSecond.toVisibility()
binding.secondHand.visibility = (enableSecond && showSecond).toVisibility() binding.secondHand.visibility = showSecond.toVisibility()
} }
private fun setWatchfaceStyle() { private fun setWatchfaceStyle() {
val customWatchface = persistence.readCustomWatchface() ?: persistence.readCustomWatchface(true) val customWatchface = persistence.readCustomWatchface() ?: persistence.readCustomWatchface(true)
customWatchface?.let { customWatchface?.let {
try {
val json = JSONObject(it.customWatchfaceData.json) val json = JSONObject(it.customWatchfaceData.json)
val drawableDataMap = it.customWatchfaceData.drawableDatas val drawableDataMap = it.customWatchfaceData.drawableDatas
enableSecond = (if (json.has("enableSecond")) json.getBoolean("enableSecond") else false) && sp.getBoolean(R.string.key_show_seconds, true) enableSecond = (if (json.has("enableSecond")) json.getBoolean("enableSecond") else false) && sp.getBoolean(R.string.key_show_seconds, true)
@ -159,7 +160,7 @@ class CustomWatchface : BaseWatchFace() {
view.gravity = setGravity(if (viewjson.has("gravity")) viewjson.getString("gravity") else "center") view.gravity = setGravity(if (viewjson.has("gravity")) viewjson.getString("gravity") else "center")
view.setTypeface( view.setTypeface(
setFont(if (viewjson.has("font")) viewjson.getString("font") else "sans-serif"), setFont(if (viewjson.has("font")) viewjson.getString("font") else "sans-serif"),
Style.fromKey( viewjson.getString("fontStyle")).typeface setStyle(if (viewjson.has("fontStyle")) viewjson.getString("fontStyle") else "normal")
) )
if (viewjson.has("fontColor")) if (viewjson.has("fontColor"))
view.setTextColor(getColor(viewjson.getString("fontColor"))) view.setTextColor(getColor(viewjson.getString("fontColor")))
@ -185,6 +186,9 @@ class CustomWatchface : BaseWatchFace() {
} }
} }
updateSecondVisibility() updateSecondVisibility()
} catch (e:Exception) {
persistence.store(defaultWatchface(), false) // relaod correct values to avoid crash of watchface
}
} }
} }
@ -221,7 +225,7 @@ class CustomWatchface : BaseWatchFace() {
.put("textsize", view.textSize.toInt()) .put("textsize", view.textSize.toInt())
.put("gravity", getGravity(view.gravity)) .put("gravity", getGravity(view.gravity))
.put("font", getFont(view.typeface)) .put("font", getFont(view.typeface))
.put("fontStyle", Style.fromTypeface(view.typeface.style).key) .put("fontStyle", getStyle(view.typeface.style))
.put("fontColor", String.format("#%06X", 0xFFFFFF and view.currentTextColor)) .put("fontColor", String.format("#%06X", 0xFFFFFF and view.currentTextColor))
) )
} }
@ -321,32 +325,22 @@ class CustomWatchface : BaseWatchFace() {
else -> "default" else -> "default"
} }
enum class Style(val key: String, val typeface: Int) { private fun setStyle(style: String): Int = when (style) {
NORMAL("normal", Typeface.NORMAL), "normal" -> Typeface.NORMAL
BOLD("bold", Typeface.BOLD), "bold" -> Typeface.BOLD
BOLD_ITALIC("bold-italic", Typeface.BOLD_ITALIC), "bold-italic" -> Typeface.BOLD_ITALIC
ITALIC("italic", Typeface.ITALIC); "italic" -> Typeface.ITALIC
companion object { else -> Typeface.NORMAL
private val keyToEnumMap = HashMap<String, Style>()
private val typefaceToEnumMap = HashMap<Int, Style>()
init {
for (value in values()) keyToEnumMap[value.key] = value
for (value in values()) typefaceToEnumMap[value.typeface] = value
}
fun fromKey(key: String?): Style =
if (keyToEnumMap.containsKey(key)) {
keyToEnumMap[key] ?:NORMAL
} else {
NORMAL
}
fun fromTypeface(typeface: Int?): Style =
if (typefaceToEnumMap.containsKey(typeface)) {
typefaceToEnumMap[typeface] ?:NORMAL
} else {
NORMAL
}
} }
private fun getStyle(style: Int): String = when (style) {
Typeface.NORMAL -> "normal"
Typeface.BOLD -> "bold"
Typeface.BOLD_ITALIC -> "bold-italic"
Typeface.ITALIC -> "italic"
else -> "normal"
} }
fun getResourceByteArray(resourceId: Int): ByteArray? { fun getResourceByteArray(resourceId: Int): ByteArray? {
val inputStream = resources.openRawResource(resourceId) val inputStream = resources.openRawResource(resourceId)
val byteArrayOutputStream = ByteArrayOutputStream() val byteArrayOutputStream = ByteArrayOutputStream()
@ -387,7 +381,11 @@ class CustomWatchface : BaseWatchFace() {
if (color == "bgColor") if (color == "bgColor")
return bgColor return bgColor
else else
return Color.parseColor(color) return try {
Color.parseColor(color)
} catch (e: Exception) {
Color.GRAY
}
} }
} }