Merge "Introduce versions to XML for bubbles persistence" into rvc-dev am: 7cc1750375
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/11914600 Change-Id: I1f2d8e7eb3ba2100b6d34a01bbfcdf5f9963dfe1
This commit is contained in:
@@ -25,7 +25,11 @@ import java.io.InputStream
|
|||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
import java.nio.charset.StandardCharsets
|
import java.nio.charset.StandardCharsets
|
||||||
|
|
||||||
|
// TODO: handle version changes gracefully
|
||||||
|
private const val CURRENT_VERSION = 1
|
||||||
|
|
||||||
private const val TAG_BUBBLES = "bs"
|
private const val TAG_BUBBLES = "bs"
|
||||||
|
private const val ATTR_VERSION = "v"
|
||||||
private const val TAG_BUBBLE = "bb"
|
private const val TAG_BUBBLE = "bb"
|
||||||
private const val ATTR_USER_ID = "uid"
|
private const val ATTR_USER_ID = "uid"
|
||||||
private const val ATTR_PACKAGE = "pkg"
|
private const val ATTR_PACKAGE = "pkg"
|
||||||
@@ -44,6 +48,7 @@ fun writeXml(stream: OutputStream, bubbles: List<BubbleEntity>) {
|
|||||||
serializer.setOutput(stream, StandardCharsets.UTF_8.name())
|
serializer.setOutput(stream, StandardCharsets.UTF_8.name())
|
||||||
serializer.startDocument(null, true)
|
serializer.startDocument(null, true)
|
||||||
serializer.startTag(null, TAG_BUBBLES)
|
serializer.startTag(null, TAG_BUBBLES)
|
||||||
|
serializer.attribute(null, ATTR_VERSION, CURRENT_VERSION.toString())
|
||||||
bubbles.forEach { b -> writeXmlEntry(serializer, b) }
|
bubbles.forEach { b -> writeXmlEntry(serializer, b) }
|
||||||
serializer.endTag(null, TAG_BUBBLES)
|
serializer.endTag(null, TAG_BUBBLES)
|
||||||
serializer.endDocument()
|
serializer.endDocument()
|
||||||
@@ -79,10 +84,13 @@ fun readXml(stream: InputStream): List<BubbleEntity> {
|
|||||||
val parser: XmlPullParser = Xml.newPullParser()
|
val parser: XmlPullParser = Xml.newPullParser()
|
||||||
parser.setInput(stream, StandardCharsets.UTF_8.name())
|
parser.setInput(stream, StandardCharsets.UTF_8.name())
|
||||||
XmlUtils.beginDocument(parser, TAG_BUBBLES)
|
XmlUtils.beginDocument(parser, TAG_BUBBLES)
|
||||||
|
val version = parser.getAttributeWithName(ATTR_VERSION)?.toInt()
|
||||||
|
if (version != null && version == CURRENT_VERSION) {
|
||||||
val outerDepth = parser.depth
|
val outerDepth = parser.depth
|
||||||
while (XmlUtils.nextElementWithin(parser, outerDepth)) {
|
while (XmlUtils.nextElementWithin(parser, outerDepth)) {
|
||||||
bubbles.add(readXmlEntry(parser) ?: continue)
|
bubbles.add(readXmlEntry(parser) ?: continue)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return bubbles
|
return bubbles
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ class BubbleXmlHelperTest : SysuiTestCase() {
|
|||||||
fun testReadXml() {
|
fun testReadXml() {
|
||||||
val src = """
|
val src = """
|
||||||
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
|
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
|
||||||
<bs>
|
<bs v="1">
|
||||||
<bb uid="0" pkg="com.example.messenger" sid="shortcut-1" key="k1" h="120" hid="0" />
|
<bb uid="0" pkg="com.example.messenger" sid="shortcut-1" key="k1" h="120" hid="0" />
|
||||||
<bb uid="10" pkg="com.example.chat" sid="alice and bob" key="k2" h="0" hid="16537428" t="title" />
|
<bb uid="10" pkg="com.example.chat" sid="alice and bob" key="k2" h="0" hid="16537428" t="title" />
|
||||||
<bb uid="0" pkg="com.example.messenger" sid="shortcut-2" key="k3" h="120" hid="0" />
|
<bb uid="0" pkg="com.example.messenger" sid="shortcut-2" key="k3" h="120" hid="0" />
|
||||||
@@ -64,4 +64,19 @@ class BubbleXmlHelperTest : SysuiTestCase() {
|
|||||||
val actual = readXml(ByteArrayInputStream(src.toByteArray(Charsets.UTF_8)))
|
val actual = readXml(ByteArrayInputStream(src.toByteArray(Charsets.UTF_8)))
|
||||||
assertEquals("failed parsing bubbles from xml\n$src", bubbles, actual)
|
assertEquals("failed parsing bubbles from xml\n$src", bubbles, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: We should handle upgrades gracefully but this is v1
|
||||||
|
@Test
|
||||||
|
fun testUpgradeDropsPreviousData() {
|
||||||
|
val src = """
|
||||||
|
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
|
||||||
|
<bs>
|
||||||
|
<bb uid="0" pkg="com.example.messenger" sid="shortcut-1" key="k1" h="120" hid="0" />
|
||||||
|
<bb uid="10" pkg="com.example.chat" sid="alice and bob" key="k2" h="0" hid="16537428" t="title" />
|
||||||
|
<bb uid="0" pkg="com.example.messenger" sid="shortcut-2" key="k3" h="120" hid="0" />
|
||||||
|
</bs>
|
||||||
|
""".trimIndent()
|
||||||
|
val actual = readXml(ByteArrayInputStream(src.toByteArray(Charsets.UTF_8)))
|
||||||
|
assertEquals("failed parsing bubbles from xml\n$src", emptyList<BubbleEntity>(), actual)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user