Show app backup check success notification

This commit is contained in:
Torsten Grote 2024-10-23 17:44:37 -03:00
parent 51a6355205
commit 96f9a8d017
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
3 changed files with 28 additions and 7 deletions

View file

@ -92,11 +92,10 @@ internal class Checker(
// only log/show notification after some time has passed (throttling) // only log/show notification after some time has passed (throttling)
if (passedTime > lastNotification.get() + 500) { if (passedTime > lastNotification.get() + 500) {
lastNotification.set(passedTime) lastNotification.set(passedTime)
val bandwidth = val bandwidth = (newSize / (passedTime.toDouble() / 1000)).roundToLong()
(newSize / 1024 / (passedTime.toDouble() / 1000)).roundToInt()
val thousandth = ((newSize.toDouble() / sampleSize) * 1000).roundToInt() val thousandth = ((newSize.toDouble() / sampleSize) * 1000).roundToInt()
log.debug { "$thousandth‰ - $bandwidth KB/sec - $newSize bytes" } log.debug { "$thousandth‰ - $bandwidth KB/sec - $newSize bytes" }
nm.showCheckNotification("$bandwidth KB/sec", thousandth) nm.showCheckNotification(bandwidth, thousandth)
} }
} }
} }
@ -104,6 +103,9 @@ internal class Checker(
if (sampleSize != size.get()) log.error { if (sampleSize != size.get()) log.error {
"Checked ${size.get()} bytes, but expected $sampleSize" "Checked ${size.get()} bytes, but expected $sampleSize"
} }
val passedTime = System.currentTimeMillis() - startTime
val bandwidth = size.get() / (passedTime.toDouble() / 1000).roundToLong()
nm.onCheckComplete(size.get(), bandwidth)
} }
private fun getBlobSample(snapshots: List<Snapshot>, percent: Int): Map<String, Blob> { private fun getBlobSample(snapshots: List<Snapshot>, percent: Int): Map<String, Blob> {

View file

@ -18,7 +18,7 @@ import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager.NameNotFoundException import android.content.pm.PackageManager.NameNotFoundException
import android.text.format.Formatter import android.text.format.Formatter.formatShortFileSize
import android.util.Log import android.util.Log
import androidx.core.app.NotificationCompat.Action import androidx.core.app.NotificationCompat.Action
import androidx.core.app.NotificationCompat.Builder import androidx.core.app.NotificationCompat.Builder
@ -51,7 +51,8 @@ internal const val NOTIFICATION_ID_RESTORE = 5
private const val NOTIFICATION_ID_RESTORE_ERROR = 6 private const val NOTIFICATION_ID_RESTORE_ERROR = 6
internal const val NOTIFICATION_ID_PRUNING = 7 internal const val NOTIFICATION_ID_PRUNING = 7
internal const val NOTIFICATION_ID_CHECKING = 8 internal const val NOTIFICATION_ID_CHECKING = 8
private const val NOTIFICATION_ID_NO_MAIN_KEY_ERROR = 9 internal const val NOTIFICATION_ID_CHECK_FINISHED = 9
private const val NOTIFICATION_ID_NO_MAIN_KEY_ERROR = 10
private val TAG = BackupNotificationManager::class.java.simpleName private val TAG = BackupNotificationManager::class.java.simpleName
@ -195,7 +196,7 @@ internal class BackupNotificationManager(private val context: Context) {
} }
fun onBackupSuccess(numBackedUp: Int, total: Int, size: Long) { fun onBackupSuccess(numBackedUp: Int, total: Int, size: Long) {
val sizeStr = Formatter.formatShortFileSize(context, size) val sizeStr = formatShortFileSize(context, size)
val contentText = val contentText =
context.getString(R.string.notification_success_text, numBackedUp, total, sizeStr) context.getString(R.string.notification_success_text, numBackedUp, total, sizeStr)
val intent = Intent(context, SettingsActivity::class.java).apply { val intent = Intent(context, SettingsActivity::class.java).apply {
@ -340,7 +341,8 @@ internal class BackupNotificationManager(private val context: Context) {
foregroundServiceBehavior = FOREGROUND_SERVICE_IMMEDIATE foregroundServiceBehavior = FOREGROUND_SERVICE_IMMEDIATE
} }
fun showCheckNotification(text: String, thousandth: Int) { fun showCheckNotification(speed: Long, thousandth: Int) {
val text = "${formatShortFileSize(context, speed)}/s"
val notification = getCheckNotification() val notification = getCheckNotification()
.setContentText(text) .setContentText(text)
.setProgress(1000, thousandth, false) .setProgress(1000, thousandth, false)
@ -348,6 +350,21 @@ internal class BackupNotificationManager(private val context: Context) {
nm.notify(NOTIFICATION_ID_CHECKING, notification) nm.notify(NOTIFICATION_ID_CHECKING, notification)
} }
fun onCheckComplete(size: Long, speed: Long) {
val text = context.getString(
R.string.notification_checking_finished_text,
formatShortFileSize(context, size),
"${formatShortFileSize(context, speed)}/s",
)
val notification = Builder(context, CHANNEL_ID_CHECKING)
.setContentTitle(context.getString(R.string.notification_checking_finished_title))
.setContentText(text)
.setSmallIcon(R.drawable.ic_cloud_done)
.build()
nm.cancel(NOTIFICATION_ID_CHECKING)
nm.notify(NOTIFICATION_ID_CHECK_FINISHED, notification)
}
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
fun onNoMainKeyError() { fun onNoMainKeyError() {
val intent = Intent(context, SettingsActivity::class.java) val intent = Intent(context, SettingsActivity::class.java)

View file

@ -189,6 +189,8 @@
<string name="notification_checking_channel_title">App backup integrity check</string> <string name="notification_checking_channel_title">App backup integrity check</string>
<string name="notification_checking_title">Checking app backups…</string> <string name="notification_checking_title">Checking app backups…</string>
<string name="notification_checking_finished_title">App backup integrity confirmed</string>
<string name="notification_checking_finished_text">Successfully checked %1$s at an average speed of %2$s.</string>
<!-- App Backup and Restore State --> <!-- App Backup and Restore State -->