Skip to content

Commit

Permalink
error notification
Browse files Browse the repository at this point in the history
  • Loading branch information
lhns committed Aug 2, 2021
1 parent 9e7c67c commit 4e6b421
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ android {

signingConfigs {
release {
storeFile file(System.env.RELEASE_STORE_FILE)
storeFile file(System.env.RELEASE_STORE_FILE ?: "null")
storePassword System.env.RELEASE_STORE_PASSWORD
keyAlias System.env.RELEASE_KEY_ALIAS
keyPassword System.env.RELEASE_KEY_PASSWORD
Expand Down
69 changes: 51 additions & 18 deletions app/src/main/java/de/lolhens/resticui/BackupManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.observe
import de.lolhens.resticui.config.*
import de.lolhens.resticui.restic.*
import de.lolhens.resticui.restic.Restic
import de.lolhens.resticui.restic.ResticException
import de.lolhens.resticui.restic.ResticRepo
import de.lolhens.resticui.restic.ResticStorage
import java.time.Duration
import java.time.ZonedDateTime
import java.util.concurrent.CompletableFuture
Expand Down Expand Up @@ -48,31 +51,62 @@ class BackupManager private constructor(context: Context) {

val notificationChannelId = "RESTIC_BACKUP_PROGRESS"

private fun backupProgressNotification(
private fun updateNotification(
context: Context,
activeBackup: ActiveBackup,
progress: ResticBackupProgress?,
doneNotification: Boolean = false
doneNotification: Boolean = true,
errorNotification: Boolean = true
) {
when {
progress != null -> {
activeBackup.inProgress -> {
// TODO: use somewhere
val details = if (activeBackup.progress == null) null else {
"""
${activeBackup.progress.files_done}${if (activeBackup.progress.total_files != null) " / ${activeBackup.progress.total_files}" else ""} Files
${activeBackup.progress.bytesDoneString()}${if (activeBackup.progress.total_bytes != null) " / ${activeBackup.progress.totalBytesString()}" else ""}
""".trimIndent()
}

val progress = activeBackup.progress?.percentDoneString() ?: "0%"

notificationManager(context).notify(
activeBackup.notificationId,
NotificationCompat.Builder(context, notificationChannelId)
.setContentTitle(context.resources.getString(R.string.notification_backup_title))
.setContentTitle("${context.resources.getString(R.string.notification_backup_progress_message)} ${progress.percentDoneString()}")
.setSubText(progress)
.setContentTitle("${context.resources.getString(R.string.notification_backup_progress_message)} $progress")
.setContentText(
if (activeBackup.progress == null) null
else "${activeBackup.progress.timeElapsedString()} elapsed"
)
.setSmallIcon(R.drawable.outline_cloud_24)
.setProgress(100, progress.percentDone100().roundToInt(), false)
.setProgress(
100,
activeBackup.progress?.percentDone100()?.roundToInt() ?: 0,
activeBackup.isStarting()
)
.setOngoing(true)
.build()
)
}
doneNotification -> {
activeBackup.error != null && errorNotification -> {
notificationManager(context).notify(
activeBackup.notificationId,
NotificationCompat.Builder(context, notificationChannelId)
.setContentTitle(context.resources.getString(R.string.notification_backup_title))
.setContentTitle("${context.resources.getString(R.string.notification_backup_failed_message)}\n${activeBackup.error}")
.setSmallIcon(R.drawable.outline_cloud_error_24)
.build()
)
}
activeBackup.summary != null && doneNotification -> {
notificationManager(context).notify(
activeBackup.notificationId,
NotificationCompat.Builder(context, notificationChannelId)
.setSubText("100%")
.setContentTitle(context.resources.getString(R.string.notification_backup_done_message))
.setContentText(
if (activeBackup.progress == null) null
else "${activeBackup.progress.timeElapsedString()} elapsed"
)
.setSmallIcon(R.drawable.outline_cloud_done_24)
.build()
)
Expand Down Expand Up @@ -143,14 +177,15 @@ class BackupManager private constructor(context: Context) {
val activeBackup = ActiveBackup.create()
activeBackupLiveData.postValue(activeBackup)

backupProgressNotification(context, activeBackup, ResticBackupProgress.zero())
updateNotification(context, activeBackup)

resticRepo.backup(
ResticRepo.hostname,
folder.path,
{ progress ->
activeBackupLiveData.postValue(activeBackupLiveData.value!!.progress(progress))
backupProgressNotification(context, activeBackup, progress)
val activeBackupProgress = activeBackupLiveData.value!!.progress(progress)
activeBackupLiveData.postValue(activeBackupProgress)
updateNotification(context, activeBackupProgress)
},
activeBackup.cancelFuture
).handle { summary, throwable ->
Expand Down Expand Up @@ -184,11 +219,9 @@ class BackupManager private constructor(context: Context) {
})
}

activeBackupLiveData.postValue(
activeBackupLiveData.value!!.finish(summary, errorMessage)
)

backupProgressNotification(context, activeBackup, null, doneNotification = true)
val finishedActiveBackup = activeBackupLiveData.value!!.finish(summary, errorMessage)
activeBackupLiveData.postValue(finishedActiveBackup)
updateNotification(context, finishedActiveBackup)

fun removeOldBackups(callback: () -> Unit) {
if (removeOld && throwable == null && (folder.keepLast != null || folder.keepWithin != null)) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/de/lolhens/resticui/BackupService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class BackupService : JobService() {
override fun onStopJob(params: JobParameters?): Boolean {
val backupManager = BackupManager.instance(applicationContext)
backupManager.currentlyActiveBackups().forEach { it.cancel() }

// Wait for all backups to be cancelled to make sure the notification is dismissed
while (backupManager.currentlyActiveBackups().isNotEmpty()) {
Thread.sleep(100)
}
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,18 @@ class FolderFragment : Fragment() {
if (backup.error != null) {
System.err.println(backup.error)
binding.textBackupError.text = backup.error
} else if (backup.summary == null) {

} else {
val details = """
Backup completed in ${backup.progress!!.timeElapsedString()}!
val details = if (backup.progress == null) null else {
"""
Backup finished after ${backup.progress.timeElapsedString()}!
${backup.progress.files_done}${if (backup.progress.total_files != null) " / ${backup.progress.total_files}" else ""} Files
${backup.progress.bytesDoneString()}${if (backup.progress.total_bytes != null) " / ${backup.progress.totalBytesString()}" else ""}
""".trimIndent()
""".trimIndent()
}

binding.textBackupDetails.text = details
binding.textBackupDetails.text = details ?: "Backup finished!"
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/outline_cloud_error_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M12,6c2.62,0 4.88,1.86 5.39,4.43l0.3,1.5 1.53,0.11c1.56,0.1 2.78,1.41 2.78,2.96 0,1.65 -1.35,3 -3,3H6c-2.21,0 -4,-1.79 -4,-4 0,-2.05 1.53,-3.76 3.56,-3.97l1.07,-0.11 0.5,-0.95C8.08,7.14 9.94,6 12,6m0,-2C9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96C18.67,6.59 15.64,4 12,4z M 9.91 9.5 L 8.5 10.91 L 10.59 13 L 8.5 15.09 l 1.41 1.41 L 12 14.42 l 2.09 2.08 l 1.41 -1.41 L 13.42 13 l 2.08 -2.09 L 14.09 9.5 L 12 11.59 L 9.91 9.5 z" />
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
<string name="notification_backup_title">Restic Backup</string>
<string name="notification_backup_progress_message">Backup in Progress…</string>
<string name="notification_backup_done_message">Backup Finished!</string>
<string name="notification_backup_failed_message">Backup Failed!</string>
</resources>

0 comments on commit 4e6b421

Please sign in to comment.