Remove gson depdency from SystemUISharedLib

Bug: 257473381
Test: Manually checked clock setting parsing
Change-Id: I70ecae6f229567c6031d7dc16181c8ec5fbe60e4
This commit is contained in:
Hawkwood Glazier
2022-11-04 17:18:26 +00:00
parent 0457d5d2c8
commit 6b7e252895
3 changed files with 73 additions and 7 deletions

View File

@@ -52,7 +52,6 @@ android_library {
"SystemUIUnfoldLib",
"androidx.dynamicanimation_dynamicanimation",
"androidx.concurrent_concurrent-futures",
"gson",
"androidx.lifecycle_lifecycle-runtime-ktx",
"androidx.lifecycle_lifecycle-viewmodel-ktx",
"androidx.recyclerview_recyclerview",

View File

@@ -28,7 +28,7 @@ import com.android.systemui.plugins.ClockProvider
import com.android.systemui.plugins.ClockProviderPlugin
import com.android.systemui.plugins.PluginListener
import com.android.systemui.shared.plugins.PluginManager
import com.google.gson.Gson
import org.json.JSONObject
private val TAG = ClockRegistry::class.simpleName
private const val DEBUG = true
@@ -47,7 +47,6 @@ open class ClockRegistry(
fun onClockChanged()
}
private val gson = Gson()
private val availableClocks = mutableMapOf<ClockId, ClockInfo>()
private val clockChangeListeners = mutableListOf<ClockChangeListener>()
private val settingObserver = object : ContentObserver(handler) {
@@ -70,7 +69,7 @@ open class ClockRegistry(
context.contentResolver,
Settings.Secure.LOCK_SCREEN_CUSTOM_CLOCK_FACE
)
gson.fromJson(json, ClockSetting::class.java)?.clockId ?: DEFAULT_CLOCK_ID
ClockSetting.deserialize(json)?.clockId ?: DEFAULT_CLOCK_ID
} catch (ex: Exception) {
Log.e(TAG, "Failed to parse clock setting", ex)
DEFAULT_CLOCK_ID
@@ -78,7 +77,7 @@ open class ClockRegistry(
}
set(value) {
try {
val json = gson.toJson(ClockSetting(value, System.currentTimeMillis()))
val json = ClockSetting.serialize(ClockSetting(value, System.currentTimeMillis()))
Settings.Secure.putString(
context.contentResolver,
Settings.Secure.LOCK_SCREEN_CUSTOM_CLOCK_FACE, json
@@ -198,8 +197,27 @@ open class ClockRegistry(
)
@Keep
private data class ClockSetting(
data class ClockSetting(
val clockId: ClockId,
val _applied_timestamp: Long?
)
) {
companion object {
private val KEY_CLOCK_ID = "clockId"
private val KEY_TIMESTAMP = "_applied_timestamp"
fun serialize(setting: ClockSetting): String {
return JSONObject()
.put(KEY_CLOCK_ID, setting.clockId)
.put(KEY_TIMESTAMP, setting._applied_timestamp)
.toString()
}
fun deserialize(jsonStr: String): ClockSetting {
val json = JSONObject(jsonStr)
return ClockSetting(
json.getString(KEY_CLOCK_ID),
if (!json.isNull(KEY_TIMESTAMP)) json.getLong(KEY_TIMESTAMP) else null)
}
}
}
}

View File

@@ -33,6 +33,7 @@ import com.android.systemui.util.mockito.argumentCaptor
import com.android.systemui.util.mockito.eq
import junit.framework.Assert.assertEquals
import junit.framework.Assert.fail
import org.json.JSONException
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@@ -238,4 +239,52 @@ class ClockRegistryTest : SysuiTestCase() {
pluginListener.onPluginDisconnected(plugin2)
assertEquals(1, changeCallCount)
}
@Test
fun jsonDeserialization_gotExpectedObject() {
val expected = ClockRegistry.ClockSetting("ID", 500)
val actual = ClockRegistry.ClockSetting.deserialize("""{
"clockId":"ID",
"_applied_timestamp":500
}""")
assertEquals(expected, actual)
}
@Test
fun jsonDeserialization_noTimestamp_gotExpectedObject() {
val expected = ClockRegistry.ClockSetting("ID", null)
val actual = ClockRegistry.ClockSetting.deserialize("{\"clockId\":\"ID\"}")
assertEquals(expected, actual)
}
@Test
fun jsonDeserialization_nullTimestamp_gotExpectedObject() {
val expected = ClockRegistry.ClockSetting("ID", null)
val actual = ClockRegistry.ClockSetting.deserialize("""{
"clockId":"ID",
"_applied_timestamp":null
}""")
assertEquals(expected, actual)
}
@Test(expected = JSONException::class)
fun jsonDeserialization_noId_threwException() {
val expected = ClockRegistry.ClockSetting("ID", 500)
val actual = ClockRegistry.ClockSetting.deserialize("{\"_applied_timestamp\":500}")
assertEquals(expected, actual)
}
@Test
fun jsonSerialization_gotExpectedString() {
val expected = "{\"clockId\":\"ID\",\"_applied_timestamp\":500}"
val actual = ClockRegistry.ClockSetting.serialize( ClockRegistry.ClockSetting("ID", 500))
assertEquals(expected, actual)
}
@Test
fun jsonSerialization_noTimestamp_gotExpectedString() {
val expected = "{\"clockId\":\"ID\"}"
val actual = ClockRegistry.ClockSetting.serialize( ClockRegistry.ClockSetting("ID", null))
assertEquals(expected, actual)
}
}