From af2bf4f60a4bf152c217d8c27283f510ec051d5a Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Mon, 5 Oct 2020 16:40:47 -0300 Subject: [PATCH] Support APK splits in metadata --- .../stevesoltys/seedvault/metadata/Metadata.kt | 9 +++++++++ .../seedvault/metadata/MetadataManager.kt | 1 + .../seedvault/metadata/MetadataReader.kt | 15 +++++++++++++++ .../seedvault/metadata/MetadataWriter.kt | 8 ++++++++ .../metadata/MetadataWriterDecoderTest.kt | 5 +++++ 5 files changed, 38 insertions(+) diff --git a/app/src/main/java/com/stevesoltys/seedvault/metadata/Metadata.kt b/app/src/main/java/com/stevesoltys/seedvault/metadata/Metadata.kt index 50385c7f..8400cfc5 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/metadata/Metadata.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/metadata/Metadata.kt @@ -70,6 +70,7 @@ data class PackageMetadata( internal val system: Boolean = false, internal val version: Long? = null, internal val installer: String? = null, + internal val splits: List? = null, internal val sha256: String? = null, internal val signatures: List? = null ) { @@ -78,11 +79,19 @@ data class PackageMetadata( } } +data class ApkSplit( + val name: String, + val sha256: String + // There's also a revisionCode, but it doesn't seem to be used just yet +) + internal const val JSON_PACKAGE_TIME = "time" internal const val JSON_PACKAGE_STATE = "state" internal const val JSON_PACKAGE_SYSTEM = "system" internal const val JSON_PACKAGE_VERSION = "version" internal const val JSON_PACKAGE_INSTALLER = "installer" +internal const val JSON_PACKAGE_SPLITS = "splits" +internal const val JSON_PACKAGE_SPLIT_NAME = "name" internal const val JSON_PACKAGE_SHA256 = "sha256" internal const val JSON_PACKAGE_SIGNATURES = "signatures" diff --git a/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataManager.kt b/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataManager.kt index 83d9d4ba..938cfc0b 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataManager.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataManager.kt @@ -103,6 +103,7 @@ class MetadataManager( system = packageInfo.isSystemApp(), version = packageMetadata.version, installer = packageMetadata.installer, + splits = packageMetadata.splits, sha256 = packageMetadata.sha256, signatures = packageMetadata.signatures ) diff --git a/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataReader.kt b/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataReader.kt index 860d99e4..a6e2dc7b 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataReader.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataReader.kt @@ -112,6 +112,7 @@ internal class MetadataReaderImpl(private val crypto: Crypto) : MetadataReader { system = pSystem, version = if (pVersion == 0L) null else pVersion, installer = if (pInstaller == "") null else pInstaller, + splits = getSplits(p), sha256 = if (pSha256 == "") null else pSha256, signatures = signatures ) @@ -130,4 +131,18 @@ internal class MetadataReaderImpl(private val crypto: Crypto) : MetadataReader { } } + private fun getSplits(p: JSONObject): List? { + val jsonSplits = p.optJSONArray(JSON_PACKAGE_SPLITS) ?: return null + val splits = ArrayList(jsonSplits.length()) + for (i in 0 until jsonSplits.length()) { + val jsonApkSplit = jsonSplits.getJSONObject(i) + val apkSplit = ApkSplit( + name = jsonApkSplit.getString(JSON_PACKAGE_SPLIT_NAME), + sha256 = jsonApkSplit.getString(JSON_PACKAGE_SHA256) + ) + splits.add(apkSplit) + } + return splits + } + } diff --git a/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataWriter.kt b/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataWriter.kt index b285d9b4..7afcd5b5 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataWriter.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataWriter.kt @@ -45,6 +45,14 @@ internal class MetadataWriterImpl(private val crypto: Crypto) : MetadataWriter { } packageMetadata.version?.let { put(JSON_PACKAGE_VERSION, it) } packageMetadata.installer?.let { put(JSON_PACKAGE_INSTALLER, it) } + packageMetadata.splits?.let { splits -> + put(JSON_PACKAGE_SPLITS, JSONArray().apply { + for (split in splits) put(JSONObject().apply { + put(JSON_PACKAGE_SPLIT_NAME, split.name) + put(JSON_PACKAGE_SHA256, split.sha256) + }) + }) + } packageMetadata.sha256?.let { put(JSON_PACKAGE_SHA256, it) } packageMetadata.signatures?.let { put(JSON_PACKAGE_SIGNATURES, JSONArray(it)) } }) diff --git a/app/src/test/java/com/stevesoltys/seedvault/metadata/MetadataWriterDecoderTest.kt b/app/src/test/java/com/stevesoltys/seedvault/metadata/MetadataWriterDecoderTest.kt index 865249e7..a85bbe6e 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/metadata/MetadataWriterDecoderTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/metadata/MetadataWriterDecoderTest.kt @@ -54,6 +54,11 @@ internal class MetadataWriterDecoderTest { state = APK_AND_DATA, version = Random.nextLong(), installer = getRandomString(), + splits = listOf( + ApkSplit(getRandomString(), getRandomString()), + ApkSplit(getRandomString(), getRandomString()), + ApkSplit(getRandomString(), getRandomString()) + ), sha256 = getRandomString(), signatures = listOf(getRandomString(), getRandomString()) )