From baef15b2bc6a1858f1065461b925e19321ab5138 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 19 Mar 2024 11:07:55 -0300 Subject: [PATCH] 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. --- .../seedvault/metadata/MetadataManager.kt | 13 ------- .../NotificationBackupObserver.kt | 34 +++++++++++++------ 2 files changed, 23 insertions(+), 24 deletions(-) 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 b97c85ef..6c09ac8f 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataManager.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/metadata/MetadataManager.kt @@ -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 } diff --git a/app/src/main/java/com/stevesoltys/seedvault/ui/notification/NotificationBackupObserver.kt b/app/src/main/java/com/stevesoltys/seedvault/ui/notification/NotificationBackupObserver.kt index 41498d89..30d93441 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/ui/notification/NotificationBackupObserver.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/ui/notification/NotificationBackupObserver.kt @@ -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) }