Add exception handling to ClockRegistry to prevent crashes

Test: Manual
Bug: 229771520
Change-Id: I9db17a3d945b75846ac238b59634fb595a8c8706
This commit is contained in:
Hawkwood Glazier
2022-06-24 17:13:27 +00:00
parent c5b807863a
commit 901c0a5dcb

View File

@@ -74,18 +74,27 @@ open class ClockRegistry(
open var currentClockId: ClockId open var currentClockId: ClockId
get() { get() {
return try {
val json = Settings.Secure.getString( val json = Settings.Secure.getString(
context.contentResolver, context.contentResolver,
Settings.Secure.LOCK_SCREEN_CUSTOM_CLOCK_FACE Settings.Secure.LOCK_SCREEN_CUSTOM_CLOCK_FACE
) )
return gson.fromJson(json, ClockSetting::class.java)?.clockId ?: DEFAULT_CLOCK_ID gson.fromJson(json, ClockSetting::class.java)?.clockId ?: DEFAULT_CLOCK_ID
} catch (ex: Exception) {
Log.e(TAG, "Failed to parse clock setting", ex)
DEFAULT_CLOCK_ID
}
} }
set(value) { set(value) {
try {
val json = gson.toJson(ClockSetting(value, System.currentTimeMillis())) val json = gson.toJson(ClockSetting(value, System.currentTimeMillis()))
Settings.Secure.putString( Settings.Secure.putString(
context.contentResolver, context.contentResolver,
Settings.Secure.LOCK_SCREEN_CUSTOM_CLOCK_FACE, json Settings.Secure.LOCK_SCREEN_CUSTOM_CLOCK_FACE, json
) )
} catch (ex: Exception) {
Log.e(TAG, "Failed to set clock setting", ex)
}
} }
init { init {
@@ -183,6 +192,6 @@ open class ClockRegistry(
private data class ClockSetting( private data class ClockSetting(
val clockId: ClockId, val clockId: ClockId,
val _applied_timestamp: Long val _applied_timestamp: Long?
) )
} }