From c288760047f7b2fc37dd13e06bfab075ed604219 Mon Sep 17 00:00:00 2001 From: Mady Mellor Date: Wed, 17 Jun 2020 16:47:19 -0700 Subject: [PATCH] Introduce versions to XML for bubbles persistence Let's just drop previous versions... we could check and update but this is very safe and technically version 1. Test: atest BubbleXmlHelperTest Fixes: 159243080 Change-Id: Ia8a136c737bac638ab70398212e6387c3736eead --- .../systemui/bubbles/storage/BubbleXmlHelper.kt | 14 +++++++++++--- .../bubbles/storage/BubbleXmlHelperTest.kt | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/bubbles/storage/BubbleXmlHelper.kt b/packages/SystemUI/src/com/android/systemui/bubbles/storage/BubbleXmlHelper.kt index 66fff3386ae14..bf163a230affc 100644 --- a/packages/SystemUI/src/com/android/systemui/bubbles/storage/BubbleXmlHelper.kt +++ b/packages/SystemUI/src/com/android/systemui/bubbles/storage/BubbleXmlHelper.kt @@ -25,7 +25,11 @@ import java.io.InputStream import java.io.OutputStream 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 ATTR_VERSION = "v" private const val TAG_BUBBLE = "bb" private const val ATTR_USER_ID = "uid" private const val ATTR_PACKAGE = "pkg" @@ -44,6 +48,7 @@ fun writeXml(stream: OutputStream, bubbles: List) { serializer.setOutput(stream, StandardCharsets.UTF_8.name()) serializer.startDocument(null, true) serializer.startTag(null, TAG_BUBBLES) + serializer.attribute(null, ATTR_VERSION, CURRENT_VERSION.toString()) bubbles.forEach { b -> writeXmlEntry(serializer, b) } serializer.endTag(null, TAG_BUBBLES) serializer.endDocument() @@ -79,9 +84,12 @@ fun readXml(stream: InputStream): List { val parser: XmlPullParser = Xml.newPullParser() parser.setInput(stream, StandardCharsets.UTF_8.name()) XmlUtils.beginDocument(parser, TAG_BUBBLES) - val outerDepth = parser.depth - while (XmlUtils.nextElementWithin(parser, outerDepth)) { - bubbles.add(readXmlEntry(parser) ?: continue) + val version = parser.getAttributeWithName(ATTR_VERSION)?.toInt() + if (version != null && version == CURRENT_VERSION) { + val outerDepth = parser.depth + while (XmlUtils.nextElementWithin(parser, outerDepth)) { + bubbles.add(readXmlEntry(parser) ?: continue) + } } return bubbles } diff --git a/packages/SystemUI/tests/src/com/android/systemui/bubbles/storage/BubbleXmlHelperTest.kt b/packages/SystemUI/tests/src/com/android/systemui/bubbles/storage/BubbleXmlHelperTest.kt index 81687c7fbe1a7..8cf4534ecdce2 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/bubbles/storage/BubbleXmlHelperTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/bubbles/storage/BubbleXmlHelperTest.kt @@ -55,7 +55,7 @@ class BubbleXmlHelperTest : SysuiTestCase() { fun testReadXml() { val src = """ - + @@ -64,4 +64,19 @@ class BubbleXmlHelperTest : SysuiTestCase() { val actual = readXml(ByteArrayInputStream(src.toByteArray(Charsets.UTF_8))) 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 = """ + + + + + + + """.trimIndent() + val actual = readXml(ByteArrayInputStream(src.toByteArray(Charsets.UTF_8))) + assertEquals("failed parsing bubbles from xml\n$src", emptyList(), actual) + } } \ No newline at end of file