Merge "Expose Metadata Json Object for WPP settings" into tm-qpr-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
c3835e6b4c
@@ -38,6 +38,7 @@ import kotlinx.coroutines.launch
|
|||||||
|
|
||||||
private val TAG = ClockRegistry::class.simpleName!!
|
private val TAG = ClockRegistry::class.simpleName!!
|
||||||
private const val DEBUG = true
|
private const val DEBUG = true
|
||||||
|
private val KEY_TIMESTAMP = "appliedTimestamp"
|
||||||
|
|
||||||
/** ClockRegistry aggregates providers and plugins */
|
/** ClockRegistry aggregates providers and plugins */
|
||||||
open class ClockRegistry(
|
open class ClockRegistry(
|
||||||
@@ -134,9 +135,9 @@ open class ClockRegistry(
|
|||||||
assertNotMainThread()
|
assertNotMainThread()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
value?._applied_timestamp = System.currentTimeMillis()
|
value?.metadata?.put(KEY_TIMESTAMP, System.currentTimeMillis())
|
||||||
val json = ClockSettings.serialize(value)
|
|
||||||
|
|
||||||
|
val json = ClockSettings.serialize(value)
|
||||||
if (handleAllUsers) {
|
if (handleAllUsers) {
|
||||||
Settings.Secure.putStringForUser(
|
Settings.Secure.putStringForUser(
|
||||||
context.contentResolver,
|
context.contentResolver,
|
||||||
@@ -172,7 +173,7 @@ open class ClockRegistry(
|
|||||||
clockChangeListeners.forEach(func)
|
clockChangeListeners.forEach(func)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun mutateSetting(mutator: (ClockSettings) -> ClockSettings) {
|
public fun mutateSetting(mutator: (ClockSettings) -> ClockSettings) {
|
||||||
scope.launch(bgDispatcher) { applySettings(mutator(settings ?: ClockSettings())) }
|
scope.launch(bgDispatcher) { applySettings(mutator(settings ?: ClockSettings())) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -192,12 +192,13 @@ data class ClockSettings(
|
|||||||
val clockId: ClockId? = null,
|
val clockId: ClockId? = null,
|
||||||
val seedColor: Int? = null,
|
val seedColor: Int? = null,
|
||||||
) {
|
) {
|
||||||
var _applied_timestamp: Long? = null
|
// Exclude metadata from equality checks
|
||||||
|
var metadata: JSONObject = JSONObject()
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val KEY_CLOCK_ID = "clockId"
|
private val KEY_CLOCK_ID = "clockId"
|
||||||
private val KEY_SEED_COLOR = "seedColor"
|
private val KEY_SEED_COLOR = "seedColor"
|
||||||
private val KEY_TIMESTAMP = "_applied_timestamp"
|
private val KEY_METADATA = "metadata"
|
||||||
|
|
||||||
fun serialize(setting: ClockSettings?): String {
|
fun serialize(setting: ClockSettings?): String {
|
||||||
if (setting == null) {
|
if (setting == null) {
|
||||||
@@ -207,7 +208,7 @@ data class ClockSettings(
|
|||||||
return JSONObject()
|
return JSONObject()
|
||||||
.put(KEY_CLOCK_ID, setting.clockId)
|
.put(KEY_CLOCK_ID, setting.clockId)
|
||||||
.put(KEY_SEED_COLOR, setting.seedColor)
|
.put(KEY_SEED_COLOR, setting.seedColor)
|
||||||
.put(KEY_TIMESTAMP, setting._applied_timestamp)
|
.put(KEY_METADATA, setting.metadata)
|
||||||
.toString()
|
.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,11 +220,11 @@ data class ClockSettings(
|
|||||||
val json = JSONObject(jsonStr)
|
val json = JSONObject(jsonStr)
|
||||||
val result =
|
val result =
|
||||||
ClockSettings(
|
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_SEED_COLOR)) json.getInt(KEY_SEED_COLOR) else null
|
||||||
)
|
)
|
||||||
if (!json.isNull(KEY_TIMESTAMP)) {
|
if (!json.isNull(KEY_METADATA)) {
|
||||||
result._applied_timestamp = json.getLong(KEY_TIMESTAMP)
|
result.metadata = json.getJSONObject(KEY_METADATA)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ import junit.framework.Assert.fail
|
|||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.test.StandardTestDispatcher
|
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||||
import kotlinx.coroutines.test.TestScope
|
import kotlinx.coroutines.test.TestScope
|
||||||
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
|
||||||
@@ -271,10 +270,14 @@ class ClockRegistryTest : SysuiTestCase() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun jsonDeserialization_gotExpectedObject() {
|
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("""{
|
val actual = ClockSettings.deserialize("""{
|
||||||
"clockId":"ID",
|
"clockId":"ID",
|
||||||
"_applied_timestamp":500
|
"metadata": {
|
||||||
|
"appliedTimestamp":500
|
||||||
|
}
|
||||||
}""")
|
}""")
|
||||||
assertEquals(expected, actual)
|
assertEquals(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -291,29 +294,32 @@ class ClockRegistryTest : SysuiTestCase() {
|
|||||||
val expected = ClockSettings("ID", null)
|
val expected = ClockSettings("ID", null)
|
||||||
val actual = ClockSettings.deserialize("""{
|
val actual = ClockSettings.deserialize("""{
|
||||||
"clockId":"ID",
|
"clockId":"ID",
|
||||||
"_applied_timestamp":null
|
"metadata":null
|
||||||
}""")
|
}""")
|
||||||
assertEquals(expected, actual)
|
assertEquals(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = JSONException::class)
|
@Test
|
||||||
fun jsonDeserialization_noId_threwException() {
|
fun jsonDeserialization_noId_deserializedEmpty() {
|
||||||
val expected = ClockSettings(null, null).apply { _applied_timestamp = 500 }
|
val expected = ClockSettings(null, null).apply {
|
||||||
val actual = ClockSettings.deserialize("{\"_applied_timestamp\":500}")
|
metadata.put("appliedTimestamp", 500)
|
||||||
|
}
|
||||||
|
val actual = ClockSettings.deserialize("{\"metadata\":{\"appliedTimestamp\":500}}")
|
||||||
assertEquals(expected, actual)
|
assertEquals(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun jsonSerialization_gotExpectedString() {
|
fun jsonSerialization_gotExpectedString() {
|
||||||
val expected = "{\"clockId\":\"ID\",\"_applied_timestamp\":500}"
|
val expected = "{\"clockId\":\"ID\",\"metadata\":{\"appliedTimestamp\":500}}"
|
||||||
val actual = ClockSettings.serialize(ClockSettings("ID", null)
|
val actual = ClockSettings.serialize(ClockSettings("ID", null).apply {
|
||||||
.apply { _applied_timestamp = 500 })
|
metadata.put("appliedTimestamp", 500)
|
||||||
|
})
|
||||||
assertEquals(expected, actual)
|
assertEquals(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun jsonSerialization_noTimestamp_gotExpectedString() {
|
fun jsonSerialization_noTimestamp_gotExpectedString() {
|
||||||
val expected = "{\"clockId\":\"ID\"}"
|
val expected = "{\"clockId\":\"ID\",\"metadata\":{}}"
|
||||||
val actual = ClockSettings.serialize(ClockSettings("ID", null))
|
val actual = ClockSettings.serialize(ClockSettings("ID", null))
|
||||||
assertEquals(expected, actual)
|
assertEquals(expected, actual)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user