From 44d4e58016a9322f2cdb60d9a5996be0ac9e359e Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Mon, 30 Sep 2024 15:24:08 -0300 Subject: [PATCH] Improve RestoreSet display when user is asked to choose a backup to restore --- .../stevesoltys/seedvault/proto/SnapshotKt.kt | 17 +++++++ .../seedvault/repo/SnapshotCreator.kt | 1 + .../seedvault/restore/RestoreSetAdapter.kt | 40 +++++++++------- .../transport/restore/RestorableBackup.kt | 25 ++++++++-- app/src/main/proto/snapshot.proto | 1 + .../main/res/layout/list_item_restore_set.xml | 46 +++++++++++++------ app/src/main/res/values/strings.xml | 6 ++- .../seedvault/repo/SnapshotCreatorTest.kt | 1 + 8 files changed, 100 insertions(+), 37 deletions(-) diff --git a/app/build/generated/source/proto/debug/kotlin/com/stevesoltys/seedvault/proto/SnapshotKt.kt b/app/build/generated/source/proto/debug/kotlin/com/stevesoltys/seedvault/proto/SnapshotKt.kt index f5141adca..1a4bc94c5 100644 --- a/app/build/generated/source/proto/debug/kotlin/com/stevesoltys/seedvault/proto/SnapshotKt.kt +++ b/app/build/generated/source/proto/debug/kotlin/com/stevesoltys/seedvault/proto/SnapshotKt.kt @@ -537,6 +537,23 @@ public object SnapshotKt { public fun hasApk(): kotlin.Boolean { return _builder.hasApk() } + + /** + * uint64 size = 8; + */ + public var size: kotlin.Long + @JvmName("getSize") + get() = _builder.getSize() + @JvmName("setSize") + set(value) { + _builder.setSize(value) + } + /** + * uint64 size = 8; + */ + public fun clearSize() { + _builder.clearSize() + } } } @kotlin.jvm.JvmName("-initializeapk") diff --git a/app/src/main/java/com/stevesoltys/seedvault/repo/SnapshotCreator.kt b/app/src/main/java/com/stevesoltys/seedvault/repo/SnapshotCreator.kt index eda04826f..37d615a8b 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/repo/SnapshotCreator.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/repo/SnapshotCreator.kt @@ -97,6 +97,7 @@ internal class SnapshotCreator( system = isSystemApp launchableSystemApp = isSystemApp && launchableSystemApps.contains(packageName) addAllChunkIds(chunkIds) + size = backupData.size } blobsMap.putAll(backupData.blobMap) metadataManager.onPackageBackedUp(packageInfo, backupType, backupData.size) diff --git a/app/src/main/java/com/stevesoltys/seedvault/restore/RestoreSetAdapter.kt b/app/src/main/java/com/stevesoltys/seedvault/restore/RestoreSetAdapter.kt index cdf15cc04..bf89c92ea 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/restore/RestoreSetAdapter.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/restore/RestoreSetAdapter.kt @@ -6,9 +6,9 @@ package com.stevesoltys.seedvault.restore import android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE -import android.text.format.DateUtils.HOUR_IN_MILLIS +import android.text.format.DateUtils.MINUTE_IN_MILLIS import android.text.format.DateUtils.getRelativeTimeSpanString -import android.text.format.Formatter +import android.text.format.Formatter.formatShortFileSize import android.view.LayoutInflater import android.view.View import android.view.View.GONE @@ -41,32 +41,40 @@ internal class RestoreSetAdapter( inner class RestoreSetViewHolder(private val v: View) : ViewHolder(v) { private val titleView = v.requireViewById(R.id.titleView) - private val subtitleView = v.requireViewById(R.id.subtitleView) - private val sizeView = v.requireViewById(R.id.sizeView) + private val appView = v.requireViewById(R.id.appView) + private val apkView = v.requireViewById(R.id.apkView) + private val timeView = v.requireViewById(R.id.timeView) internal fun bind(item: RestorableBackup) { v.setOnClickListener { listener.onRestorableBackupClicked(item) } titleView.text = item.name - val lastBackup = getRelativeTime(item.time) - val setup = getRelativeTime(item.token) - subtitleView.text = - v.context.getString(R.string.restore_restore_set_times, lastBackup, setup) - val size = item.size - if (size == null) { - sizeView.visibility = GONE + appView.text = if (item.sizeAppData > 0) { + v.context.getString( + R.string.restore_restore_set_apps, + item.numAppData, + formatShortFileSize(v.context, item.sizeAppData), + ) } else { - sizeView.text = v.context.getString( - R.string.restore_restore_set_size, - Formatter.formatShortFileSize(v.context, size), + v.context.getString(R.string.restore_restore_set_apps_no_size, item.numAppData) + } + appView.visibility = if (item.numAppData > 0) VISIBLE else GONE + apkView.text = if (item.sizeApks > 0) { + v.context.getString( + R.string.restore_restore_set_apks, + item.numApks, + formatShortFileSize(v.context, item.sizeApks), ) - sizeView.visibility = VISIBLE + } else { + v.context.getString(R.string.restore_restore_set_apks_no_size, item.numApks) } + apkView.visibility = if (item.numApks > 0) VISIBLE else GONE + timeView.text = getRelativeTime(item.time) } private fun getRelativeTime(time: Long): CharSequence { val now = System.currentTimeMillis() - return getRelativeTimeSpanString(time, now, HOUR_IN_MILLIS, FORMAT_ABBREV_RELATIVE) + return getRelativeTimeSpanString(time, now, MINUTE_IN_MILLIS, FORMAT_ABBREV_RELATIVE) } } diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestorableBackup.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestorableBackup.kt index cbadca531..2dc7de45a 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestorableBackup.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/transport/restore/RestorableBackup.kt @@ -7,6 +7,7 @@ package com.stevesoltys.seedvault.transport.restore import com.stevesoltys.seedvault.metadata.BackupMetadata import com.stevesoltys.seedvault.metadata.PackageMetadataMap +import com.stevesoltys.seedvault.metadata.PackageState.APK_AND_DATA import com.stevesoltys.seedvault.proto.Snapshot sealed class RestorableBackupResult { @@ -20,7 +21,6 @@ data class RestorableBackup( val snapshot: Snapshot? = null, ) { - // FIXME creating this mapping is expensive, a single call can take several seconds to complete constructor(repoId: String, snapshot: Snapshot) : this( backupMetadata = BackupMetadata.fromSnapshot(snapshot), repoId = repoId, @@ -40,18 +40,33 @@ data class RestorableBackup( get() = backupMetadata.salt val time: Long - get() = backupMetadata.time + get() = snapshot?.token ?: backupMetadata.time - val size: Long - get() = snapshot?.blobsMap?.values?.sumOf { it.uncompressedLength.toLong() } - ?: backupMetadata.size + val size: Long = snapshot?.blobsMap?.values?.sumOf { it.uncompressedLength.toLong() } + ?: backupMetadata.size val deviceName: String get() = backupMetadata.deviceName + val user: String? + get() = snapshot?.user?.takeIf { it.isNotBlank() } + val d2dBackup: Boolean get() = backupMetadata.d2dBackup + val numAppData: Int = snapshot?.appsMap?.values?.count { it.chunkIdsCount > 0 } + ?: packageMetadataMap.values.count { packageMetadata -> + packageMetadata.backupType != null && packageMetadata.state == APK_AND_DATA + } + + val sizeAppData: Long = snapshot?.appsMap?.values?.sumOf { it.size } + ?: packageMetadataMap.values.sumOf { it.size ?: 0L } + + val numApks: Int = snapshot?.appsMap?.values?.count { it.apk.splitsCount > 0 } + ?: packageMetadataMap.values.count { it.hasApk() } + + val sizeApks: Long = size - sizeAppData + val packageMetadataMap: PackageMetadataMap get() = backupMetadata.packageMetadataMap diff --git a/app/src/main/proto/snapshot.proto b/app/src/main/proto/snapshot.proto index 142a3ea70..80ac4b4fb 100644 --- a/app/src/main/proto/snapshot.proto +++ b/app/src/main/proto/snapshot.proto @@ -25,6 +25,7 @@ message Snapshot { bool launchableSystemApp = 5; repeated bytes chunkIds = 6; Apk apk = 7; + uint64 size = 8; } enum BackupType { diff --git a/app/src/main/res/layout/list_item_restore_set.xml b/app/src/main/res/layout/list_item_restore_set.xml index df9c65c0c..8ea4b8278 100644 --- a/app/src/main/res/layout/list_item_restore_set.xml +++ b/app/src/main/res/layout/list_item_restore_set.xml @@ -1,5 +1,4 @@ - - @@ -25,37 +24,56 @@ + tools:text="@string/restore_restore_set_apps" /> + + + app:layout_constraintTop_toBottomOf="@+id/apkView" + tools:text="5 days ago" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5bf7309e8..610858d2f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -205,8 +205,10 @@ Restore from backup Choose a backup to restore - Last backup %1$s ยท First %2$s. - Size: %1$s + Has user data for %1$d apps (%2$s) + Has user data for %1$d apps + Contains %1$d apps (%2$s) + Contains %1$d apps Don\'t restore Skip restoring apps No backups found diff --git a/app/src/test/java/com/stevesoltys/seedvault/repo/SnapshotCreatorTest.kt b/app/src/test/java/com/stevesoltys/seedvault/repo/SnapshotCreatorTest.kt index d4e4005a2..52f1f8765 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/repo/SnapshotCreatorTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/repo/SnapshotCreatorTest.kt @@ -77,6 +77,7 @@ internal class SnapshotCreatorTest : TransportTest() { assertEquals(name, s.appsMap[packageName]?.name) assertEquals(token, s.appsMap[packageName]?.time) assertEquals(Snapshot.BackupType.FULL, s.appsMap[packageName]?.type) + assertEquals(size, s.appsMap[packageName]?.size) assertEquals(isSystem, s.appsMap[packageName]?.system) assertEquals(isSystem, s.appsMap[packageName]?.launchableSystemApp) assertEquals(apkBackupData.chunkIds.forProto(), s.appsMap[packageName]?.chunkIdsList)