Merge "Remove gson depdency from SystemUISharedLib" into tm-qpr-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
b6adbfc68e
@@ -57,7 +57,6 @@ android_library {
|
||||
"androidx.recyclerview_recyclerview",
|
||||
"kotlinx_coroutines_android",
|
||||
"kotlinx_coroutines",
|
||||
"gson-prebuilt-jar",
|
||||
"dagger2",
|
||||
"jsr330",
|
||||
],
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user