Avoid crash of Watchface (even on wrong type of data within json file)
This commit is contained in:
parent
f8dbcf2edd
commit
af2f18acd9
3 changed files with 87 additions and 84 deletions
|
@ -17,6 +17,7 @@ import info.nightscout.rx.events.EventLoopUpdateGui
|
|||
import info.nightscout.rx.events.EventMobileToWear
|
||||
import info.nightscout.rx.events.EventOverviewBolusProgress
|
||||
import info.nightscout.rx.events.EventPreferenceChange
|
||||
import info.nightscout.rx.events.EventWearUpdateGui
|
||||
import info.nightscout.rx.logging.AAPSLogger
|
||||
import info.nightscout.rx.weardata.CustomWatchfaceData
|
||||
import info.nightscout.rx.weardata.EventData
|
||||
|
@ -91,6 +92,10 @@ class WearPlugin @Inject constructor(
|
|||
.toObservable(EventLoopUpdateGui::class.java)
|
||||
.observeOn(aapsSchedulers.io)
|
||||
.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() {
|
||||
|
|
|
@ -106,7 +106,7 @@
|
|||
<androidx.gridlayout.widget.GridLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="0dp"
|
||||
android:layout_marginTop="20dp"
|
||||
android:padding="10dip"
|
||||
app:columnCount="2">
|
||||
|
||||
|
|
|
@ -116,13 +116,14 @@ class CustomWatchface : BaseWatchFace() {
|
|||
}
|
||||
|
||||
override fun updateSecondVisibility() {
|
||||
binding.second.visibility = (enableSecond && showSecond).toVisibility()
|
||||
binding.secondHand.visibility = (enableSecond && showSecond).toVisibility()
|
||||
binding.second.visibility = showSecond.toVisibility()
|
||||
binding.secondHand.visibility = showSecond.toVisibility()
|
||||
}
|
||||
|
||||
private fun setWatchfaceStyle() {
|
||||
val customWatchface = persistence.readCustomWatchface() ?: persistence.readCustomWatchface(true)
|
||||
customWatchface?.let {
|
||||
try {
|
||||
val json = JSONObject(it.customWatchfaceData.json)
|
||||
val drawableDataMap = it.customWatchfaceData.drawableDatas
|
||||
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.setTypeface(
|
||||
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"))
|
||||
view.setTextColor(getColor(viewjson.getString("fontColor")))
|
||||
|
@ -185,6 +186,9 @@ class CustomWatchface : BaseWatchFace() {
|
|||
}
|
||||
}
|
||||
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("gravity", getGravity(view.gravity))
|
||||
.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))
|
||||
)
|
||||
}
|
||||
|
@ -321,32 +325,22 @@ class CustomWatchface : BaseWatchFace() {
|
|||
else -> "default"
|
||||
}
|
||||
|
||||
enum class Style(val key: String, val typeface: Int) {
|
||||
NORMAL("normal", Typeface.NORMAL),
|
||||
BOLD("bold", Typeface.BOLD),
|
||||
BOLD_ITALIC("bold-italic", Typeface.BOLD_ITALIC),
|
||||
ITALIC("italic", Typeface.ITALIC);
|
||||
companion object {
|
||||
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 setStyle(style: String): Int = when (style) {
|
||||
"normal" -> Typeface.NORMAL
|
||||
"bold" -> Typeface.BOLD
|
||||
"bold-italic" -> Typeface.BOLD_ITALIC
|
||||
"italic" -> Typeface.ITALIC
|
||||
else -> Typeface.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? {
|
||||
val inputStream = resources.openRawResource(resourceId)
|
||||
val byteArrayOutputStream = ByteArrayOutputStream()
|
||||
|
@ -387,7 +381,11 @@ class CustomWatchface : BaseWatchFace() {
|
|||
if (color == "bgColor")
|
||||
return bgColor
|
||||
else
|
||||
return Color.parseColor(color)
|
||||
return try {
|
||||
Color.parseColor(color)
|
||||
} catch (e: Exception) {
|
||||
Color.GRAY
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue