summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author TreeHugger Robot <treehugger-gerrit@google.com> 2022-11-07 18:13:56 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2022-11-07 18:13:56 +0000
commitb6adbfc68e698130e7abe02d843ef85f1e4deb1c (patch)
tree864289d9eba65fd351ea0488146006bcc29f8798
parent61d495962f8982fadb05432239f1bec4d6f05286 (diff)
parent88e4f7a80d2af82b6792e84e4c73af72a1dc1979 (diff)
Merge "Remove gson depdency from SystemUISharedLib" into tm-qpr-dev
-rw-r--r--packages/SystemUI/shared/Android.bp1
-rw-r--r--packages/SystemUI/shared/src/com/android/systemui/shared/clocks/ClockRegistry.kt30
-rw-r--r--packages/SystemUI/tests/src/com/android/systemui/shared/clocks/ClockRegistryTest.kt49
3 files changed, 73 insertions, 7 deletions
diff --git a/packages/SystemUI/shared/Android.bp b/packages/SystemUI/shared/Android.bp
index ebc4186187bf..485a0d320bb9 100644
--- a/packages/SystemUI/shared/Android.bp
+++ b/packages/SystemUI/shared/Android.bp
@@ -57,7 +57,6 @@ android_library {
"androidx.recyclerview_recyclerview",
"kotlinx_coroutines_android",
"kotlinx_coroutines",
- "gson-prebuilt-jar",
"dagger2",
"jsr330",
],
diff --git a/packages/SystemUI/shared/src/com/android/systemui/shared/clocks/ClockRegistry.kt b/packages/SystemUI/shared/src/com/android/systemui/shared/clocks/ClockRegistry.kt
index 48821e8d0bd3..601cb66d99c2 100644
--- a/packages/SystemUI/shared/src/com/android/systemui/shared/clocks/ClockRegistry.kt
+++ b/packages/SystemUI/shared/src/com/android/systemui/shared/clocks/ClockRegistry.kt
@@ -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)
+ }
+ }
+ }
}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/shared/clocks/ClockRegistryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/shared/clocks/ClockRegistryTest.kt
index 70cbc64c79f1..28bd26a9094b 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/shared/clocks/ClockRegistryTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/shared/clocks/ClockRegistryTest.kt
@@ -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)
+ }
}