Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b01edb92 authored by Hawkwood Glazier's avatar Hawkwood Glazier
Browse files

Expose Metadata Json Object for WPP settings

Bug: 270206382
Bug: 270388304
Test: atest ClockRegistryTest
Change-Id: Idd2668e3fa49af8039f48636c10aea8630d18581
parent 178a0bf1
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import kotlinx.coroutines.launch

private val TAG = ClockRegistry::class.simpleName!!
private const val DEBUG = true
private val KEY_TIMESTAMP = "appliedTimestamp"

/** ClockRegistry aggregates providers and plugins */
open class ClockRegistry(
@@ -134,9 +135,9 @@ open class ClockRegistry(
        assertNotMainThread()

        try {
            value?._applied_timestamp = System.currentTimeMillis()
            val json = ClockSettings.serialize(value)
            value?.metadata?.put(KEY_TIMESTAMP, System.currentTimeMillis())

            val json = ClockSettings.serialize(value)
            if (handleAllUsers) {
                Settings.Secure.putStringForUser(
                    context.contentResolver,
@@ -172,7 +173,7 @@ open class ClockRegistry(
        clockChangeListeners.forEach(func)
    }

    private fun mutateSetting(mutator: (ClockSettings) -> ClockSettings) {
    public fun mutateSetting(mutator: (ClockSettings) -> ClockSettings) {
        scope.launch(bgDispatcher) { applySettings(mutator(settings ?: ClockSettings())) }
    }

+7 −6
Original line number Diff line number Diff line
@@ -189,12 +189,13 @@ data class ClockSettings(
    val clockId: ClockId? = null,
    val seedColor: Int? = null,
) {
    var _applied_timestamp: Long? = null
    // Exclude metadata from equality checks
    var metadata: JSONObject = JSONObject()

    companion object {
        private val KEY_CLOCK_ID = "clockId"
        private val KEY_SEED_COLOR = "seedColor"
        private val KEY_TIMESTAMP = "_applied_timestamp"
        private val KEY_METADATA = "metadata"

        fun serialize(setting: ClockSettings?): String {
            if (setting == null) {
@@ -204,7 +205,7 @@ data class ClockSettings(
            return JSONObject()
                .put(KEY_CLOCK_ID, setting.clockId)
                .put(KEY_SEED_COLOR, setting.seedColor)
                .put(KEY_TIMESTAMP, setting._applied_timestamp)
                .put(KEY_METADATA, setting.metadata)
                .toString()
        }

@@ -216,11 +217,11 @@ data class ClockSettings(
            val json = JSONObject(jsonStr)
            val result =
                ClockSettings(
                    json.getString(KEY_CLOCK_ID),
                    if (!json.isNull(KEY_CLOCK_ID)) json.getString(KEY_CLOCK_ID) else null,
                    if (!json.isNull(KEY_SEED_COLOR)) json.getInt(KEY_SEED_COLOR) else null
                )
            if (!json.isNull(KEY_TIMESTAMP)) {
                result._applied_timestamp = json.getLong(KEY_TIMESTAMP)
            if (!json.isNull(KEY_METADATA)) {
                result.metadata = json.getJSONObject(KEY_METADATA)
            }
            return result
        }
+18 −12
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import junit.framework.Assert.fail
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import org.json.JSONException
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@@ -271,10 +270,14 @@ class ClockRegistryTest : SysuiTestCase() {

    @Test
    fun jsonDeserialization_gotExpectedObject() {
        val expected = ClockSettings("ID", null).apply { _applied_timestamp = 500 }
        val expected = ClockSettings("ID", null).apply {
            metadata.put("appliedTimestamp", 500)
        }
        val actual = ClockSettings.deserialize("""{
            "clockId":"ID",
            "_applied_timestamp":500
            "metadata": {
                "appliedTimestamp":500
            }
        }""")
        assertEquals(expected, actual)
    }
@@ -291,29 +294,32 @@ class ClockRegistryTest : SysuiTestCase() {
        val expected = ClockSettings("ID", null)
        val actual = ClockSettings.deserialize("""{
            "clockId":"ID",
            "_applied_timestamp":null
            "metadata":null
        }""")
        assertEquals(expected, actual)
    }

    @Test(expected = JSONException::class)
    fun jsonDeserialization_noId_threwException() {
        val expected = ClockSettings(null, null).apply { _applied_timestamp = 500 }
        val actual = ClockSettings.deserialize("{\"_applied_timestamp\":500}")
    @Test
    fun jsonDeserialization_noId_deserializedEmpty() {
        val expected = ClockSettings(null, null).apply {
            metadata.put("appliedTimestamp", 500)
        }
        val actual = ClockSettings.deserialize("{\"metadata\":{\"appliedTimestamp\":500}}")
        assertEquals(expected, actual)
    }

    @Test
    fun jsonSerialization_gotExpectedString() {
        val expected = "{\"clockId\":\"ID\",\"_applied_timestamp\":500}"
        val actual = ClockSettings.serialize(ClockSettings("ID", null)
            .apply { _applied_timestamp = 500 })
        val expected = "{\"clockId\":\"ID\",\"metadata\":{\"appliedTimestamp\":500}}"
        val actual = ClockSettings.serialize(ClockSettings("ID", null).apply {
            metadata.put("appliedTimestamp", 500)
        })
        assertEquals(expected, actual)
    }

    @Test
    fun jsonSerialization_noTimestamp_gotExpectedString() {
        val expected = "{\"clockId\":\"ID\"}"
        val expected = "{\"clockId\":\"ID\",\"metadata\":{}}"
        val actual = ClockSettings.serialize(ClockSettings("ID", null))
        assertEquals(expected, actual)
    }