Improve backup notification

This commit is contained in:
Torsten Grote 2024-02-20 15:45:58 -03:00
parent fcd4e518a5
commit 49066be31b
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
2 changed files with 19 additions and 13 deletions

View file

@ -133,7 +133,7 @@ internal class BackupNotificationManager(private val context: Context) {
*/ */
fun onBackupStarted(expectedPackages: Int) { fun onBackupStarted(expectedPackages: Int) {
updateBackupNotification( updateBackupNotification(
infoText = "", // This passes quickly, no need to show something here appName = "", // This passes quickly, no need to show something here
transferred = 0, transferred = 0,
expected = expectedPackages expected = expectedPackages
) )
@ -146,25 +146,21 @@ internal class BackupNotificationManager(private val context: Context) {
*/ */
fun onBackupUpdate(app: CharSequence, transferred: Int, total: Int) { fun onBackupUpdate(app: CharSequence, transferred: Int, total: Int) {
updateBackupNotification( updateBackupNotification(
infoText = app, appName = app,
transferred = min(transferred, total), transferred = min(transferred, total),
expected = total expected = total
) )
} }
private fun updateBackupNotification( private fun updateBackupNotification(
infoText: CharSequence, appName: CharSequence,
transferred: Int, transferred: Int,
expected: Int, expected: Int,
) { ) {
@Suppress("MagicNumber")
val percentage = (transferred.toFloat() / expected) * 100
val percentageStr = "%.0f%%".format(percentage)
Log.i(TAG, "$transferred/$expected - $percentageStr - $infoText")
val notification = Builder(context, CHANNEL_ID_OBSERVER).apply { val notification = Builder(context, CHANNEL_ID_OBSERVER).apply {
setSmallIcon(R.drawable.ic_cloud_upload) setSmallIcon(R.drawable.ic_cloud_upload)
setContentTitle(context.getString(R.string.notification_title)) setContentTitle(context.getString(R.string.notification_title))
setContentText(percentageStr) setContentText(appName)
setOngoing(true) setOngoing(true)
setShowWhen(false) setShowWhen(false)
setWhen(System.currentTimeMillis()) setWhen(System.currentTimeMillis())

View file

@ -26,6 +26,7 @@ internal class NotificationBackupObserver(
private val metadataManager: MetadataManager by inject() private val metadataManager: MetadataManager by inject()
private var currentPackage: String? = null private var currentPackage: String? = null
private var numPackages: Int = 0 private var numPackages: Int = 0
private var pmCounted: Boolean = false
init { init {
// Inform the notification manager that a backup has started // Inform the notification manager that a backup has started
@ -93,13 +94,22 @@ internal class NotificationBackupObserver(
) )
currentPackage = packageName currentPackage = packageName
val appName = getAppName(packageName) val appName = getAppName(packageName)
val app = if (appName != packageName) { val name = if (appName != packageName) {
"${getAppName(packageName)} ($packageName)" appName
} else { } else {
packageName 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 numPackages += 1
nm.onBackupUpdate(app, numPackages, requestedPackages) pmCounted = true
}
} else {
numPackages += 1
}
Log.i(TAG, "$numPackages/$requestedPackages - $appName ($packageName)")
nm.onBackupUpdate(name, numPackages, requestedPackages)
} }
private fun getAppName(packageId: String): CharSequence = getAppName(context, packageId) private fun getAppName(packageId: String): CharSequence = getAppName(context, packageId)