Do live-counting of backed up apps for success notification

Previously, we asked the MetadataManager which also includes historic data and may provide misleading totals.
This commit is contained in:
Torsten Grote 2024-03-19 11:07:55 -03:00
parent f7730d3034
commit baef15b2bc
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
2 changed files with 23 additions and 24 deletions

View file

@ -14,7 +14,6 @@ import com.stevesoltys.seedvault.crypto.Crypto
import com.stevesoltys.seedvault.encodeBase64
import com.stevesoltys.seedvault.header.VERSION
import com.stevesoltys.seedvault.metadata.PackageState.APK_AND_DATA
import com.stevesoltys.seedvault.metadata.PackageState.NO_DATA
import com.stevesoltys.seedvault.settings.SettingsManager
import com.stevesoltys.seedvault.transport.backup.isSystemApp
import java.io.FileNotFoundException
@ -268,18 +267,6 @@ internal class MetadataManager(
return metadata.packageMetadataMap[packageName]?.copy()
}
@Synchronized
fun getPackagesNumBackedUp(): Int {
// FIXME we are under-reporting packages here,
// because we have no way to also include upgraded system apps
return metadata.packageMetadataMap.filter { (_, packageMetadata) ->
!packageMetadata.system && ( // ignore system apps
packageMetadata.state == APK_AND_DATA || // either full success
packageMetadata.state == NO_DATA // or apps that simply had no data
)
}.count()
}
@Synchronized
fun getPackagesBackupSize(): Long {
return metadata.packageMetadataMap.values.sumOf { it.size ?: 0L }

View file

@ -3,6 +3,7 @@ package com.stevesoltys.seedvault.ui.notification
import android.app.backup.BackupProgress
import android.app.backup.IBackupObserver
import android.content.Context
import android.content.pm.ApplicationInfo.FLAG_SYSTEM
import android.content.pm.PackageManager.NameNotFoundException
import android.util.Log
import android.util.Log.INFO
@ -28,6 +29,7 @@ internal class NotificationBackupObserver(
private val packageService: PackageService by inject()
private var currentPackage: String? = null
private var numPackages: Int = 0
private var numPackagesToReport: Int = 0
private var pmCounted: Boolean = false
init {
@ -64,6 +66,26 @@ internal class NotificationBackupObserver(
if (isLoggable(TAG, INFO)) {
Log.i(TAG, "Completed. Target: $target, status: $status")
}
// prevent double counting of @pm@ which gets backed up with each requested chunk
if (target == MAGIC_PACKAGE_MANAGER) {
if (!pmCounted) {
numPackages += 1
pmCounted = true
}
} else {
numPackages += 1
}
// count package if success and not a system app
if (status == 0 && target != null && target != MAGIC_PACKAGE_MANAGER) try {
val appInfo = context.packageManager.getApplicationInfo(target, 0)
// exclude system apps from final count for now
if (appInfo.flags and FLAG_SYSTEM == 0) {
numPackagesToReport += 1
}
} catch (e: Exception) {
// should only happen for MAGIC_PACKAGE_MANAGER, but better save than sorry
Log.e(TAG, "Error getting ApplicationInfo: ", e)
}
// often [onResult] gets called right away without any [onUpdate] call
showProgressNotification(target)
}
@ -81,7 +103,6 @@ internal class NotificationBackupObserver(
Log.i(TAG, "Backup finished $numPackages/$requestedPackages. Status: $status")
}
val success = status == 0
val numBackedUp = if (success) metadataManager.getPackagesNumBackedUp() else null
val size = if (success) metadataManager.getPackagesBackupSize() else 0L
val total = try {
packageService.allUserPackages.size
@ -89,7 +110,7 @@ internal class NotificationBackupObserver(
Log.e(TAG, "Error getting number of all user packages: ", e)
requestedPackages
}
nm.onBackupFinished(success, numBackedUp, total, size)
nm.onBackupFinished(success, numPackagesToReport, total, size)
}
}
@ -107,15 +128,6 @@ internal class NotificationBackupObserver(
} else {
context.getString(R.string.backup_section_system)
}
// prevent double counting of @pm@ which gets backed up with each requested chunk
if (packageName == MAGIC_PACKAGE_MANAGER) {
if (!pmCounted) {
numPackages += 1
pmCounted = true
}
} else {
numPackages += 1
}
Log.i(TAG, "$numPackages/$requestedPackages - $appName ($packageName)")
nm.onBackupUpdate(name, numPackages, requestedPackages)
}