Merge "Remove gson depdency from SystemUISharedLib" into tm-qpr-dev

This commit is contained in:
TreeHugger Robot
2022-11-07 18:13:56 +00:00
committed by Android (Google) Code Review
3 changed files with 73 additions and 7 deletions

View File

@@ -57,7 +57,6 @@ android_library {
"androidx.recyclerview_recyclerview", "androidx.recyclerview_recyclerview",
"kotlinx_coroutines_android", "kotlinx_coroutines_android",
"kotlinx_coroutines", "kotlinx_coroutines",
"gson-prebuilt-jar",
"dagger2", "dagger2",
"jsr330", "jsr330",
], ],

View File

@@ -28,7 +28,7 @@ import com.android.systemui.plugins.ClockProvider
import com.android.systemui.plugins.ClockProviderPlugin import com.android.systemui.plugins.ClockProviderPlugin
import com.android.systemui.plugins.PluginListener import com.android.systemui.plugins.PluginListener
import com.android.systemui.shared.plugins.PluginManager import com.android.systemui.shared.plugins.PluginManager
import com.google.gson.Gson import org.json.JSONObject
private val TAG = ClockRegistry::class.simpleName private val TAG = ClockRegistry::class.simpleName
private const val DEBUG = true private const val DEBUG = true
@@ -47,7 +47,6 @@ open class ClockRegistry(
fun onClockChanged() fun onClockChanged()
} }
private val gson = Gson()
private val availableClocks = mutableMapOf<ClockId, ClockInfo>() private val availableClocks = mutableMapOf<ClockId, ClockInfo>()
private val clockChangeListeners = mutableListOf<ClockChangeListener>() private val clockChangeListeners = mutableListOf<ClockChangeListener>()
private val settingObserver = object : ContentObserver(handler) { private val settingObserver = object : ContentObserver(handler) {
@@ -70,7 +69,7 @@ open class ClockRegistry(
context.contentResolver, context.contentResolver,
Settings.Secure.LOCK_SCREEN_CUSTOM_CLOCK_FACE 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) { } catch (ex: Exception) {
Log.e(TAG, "Failed to parse clock setting", ex) Log.e(TAG, "Failed to parse clock setting", ex)
DEFAULT_CLOCK_ID DEFAULT_CLOCK_ID
@@ -78,7 +77,7 @@ open class ClockRegistry(
} }
set(value) { set(value) {
try { try {
val json = gson.toJson(ClockSetting(value, System.currentTimeMillis())) val json = ClockSetting.serialize(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
@@ -198,8 +197,27 @@ open class ClockRegistry(
) )
@Keep @Keep
private data class ClockSetting( data class ClockSetting(
val clockId: ClockId, val clockId: ClockId,
val _applied_timestamp: Long? 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 com.android.systemui.util.mockito.eq
import junit.framework.Assert.assertEquals import junit.framework.Assert.assertEquals
import junit.framework.Assert.fail import junit.framework.Assert.fail
import org.json.JSONException
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
@@ -238,4 +239,52 @@ class ClockRegistryTest : SysuiTestCase() {
pluginListener.onPluginDisconnected(plugin2) pluginListener.onPluginDisconnected(plugin2)
assertEquals(1, changeCallCount) 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)
}
} }