Move backup/restore status strings to resources, show completed state

This commit is contained in:
Moez Bhatti
2018-08-31 17:31:43 -04:00
parent 680c8eb445
commit 0419f4a83a
9 changed files with 92 additions and 39 deletions
@@ -32,13 +32,14 @@ import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import io.realm.Realm
import okio.Okio
import timber.log.Timber
import java.io.File
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.concurrent.schedule
@Singleton
@@ -95,23 +96,15 @@ class BackupRepositoryImpl @Inject constructor(
messageCount = messages.size
// Map the messages to the new format
val startTime = System.currentTimeMillis()
messages.mapIndexed { index, message ->
val progress = index.toDouble() / messageCount * 100
val elapsed = System.currentTimeMillis() - startTime
val remaining = when {
index > 100 -> TimeUnit.MILLISECONDS.toSeconds((elapsed.toDouble() / index * (messageCount - index)).toLong())
else -> 0
}
// Update the progress
backupProgress.onNext(BackupRepository.Progress.Running(progress.toInt(), "$remaining seconds remaining"))
backupProgress.onNext(BackupRepository.Progress.Running(messageCount, index))
messageToBackupMessage(message)
}
}
// Update the status, and set the progress to be indeterminate since we can no longer calculate progress
backupProgress.onNext(BackupRepository.Progress.Running(0, "Saving..."))
backupProgress.onNext(BackupRepository.Progress.Saving())
// Convert the data to json
val adapter = moshi.adapter(Backup::class.java).indent("\t")
@@ -125,9 +118,12 @@ class BackupRepositoryImpl @Inject constructor(
// Write the log to the file
FileOutputStream(file, true).use { fileOutputStream -> fileOutputStream.write(json) }
} catch (e: Exception) {
Timber.w(e)
}
backupProgress.onNext(BackupRepository.Progress.Idle())
// Mark the task finished, and set it as Idle a second later
backupProgress.onNext(BackupRepository.Progress.Finished())
Timer().schedule(1000) { backupProgress.onNext(BackupRepository.Progress.Idle()) }
}
private fun messageToBackupMessage(message: Message): BackupMessage = BackupMessage(
@@ -169,7 +165,7 @@ class BackupRepositoryImpl @Inject constructor(
// If a backupFile or restore is already running, don't do anything
if (isBackupOrRestoreRunning()) return
restoreProgress.onNext(BackupRepository.Progress.Running(0, "Parsing backup"))
restoreProgress.onNext(BackupRepository.Progress.Parsing())
val file = File(filePath)
val backup = Okio.buffer(Okio.source(file)).use { source ->
@@ -180,8 +176,7 @@ class BackupRepositoryImpl @Inject constructor(
backup?.messages?.forEachIndexed { index, message ->
// Update the progress
val progress = Math.ceil(index.toDouble() / messageCount * 100).toInt()
restoreProgress.onNext(BackupRepository.Progress.Running(progress, "$index/$messageCount messages"))
restoreProgress.onNext(BackupRepository.Progress.Running(messageCount, index))
context.contentResolver.insert(Telephony.Sms.CONTENT_URI, contentValuesOf(
Telephony.Sms.TYPE to message.type,
@@ -198,18 +193,19 @@ class BackupRepositoryImpl @Inject constructor(
Telephony.Sms.SUBSCRIPTION_ID to message.subId))
}
restoreProgress.onNext(BackupRepository.Progress.Running(0, "Syncing messages"))
// Sync the messages
restoreProgress.onNext(BackupRepository.Progress.Syncing())
syncRepo.syncMessages()
restoreProgress.onNext(BackupRepository.Progress.Idle())
// Mark the task finished, and set it as Idle a second later
restoreProgress.onNext(BackupRepository.Progress.Finished())
Timer().schedule(1000) { restoreProgress.onNext(BackupRepository.Progress.Idle()) }
}
override fun getRestoreProgress(): Observable<BackupRepository.Progress> = restoreProgress
private fun isBackupOrRestoreRunning(): Boolean {
return backupProgress.blockingFirst() is BackupRepository.Progress.Running
|| restoreProgress.blockingFirst() is BackupRepository.Progress.Running
return backupProgress.blockingFirst().running || restoreProgress.blockingFirst().running
}
}
@@ -23,9 +23,13 @@ import io.reactivex.Observable
interface BackupRepository {
sealed class Progress {
sealed class Progress(val running: Boolean = false, val indeterminate: Boolean = true) {
class Idle : Progress()
class Running(val progress: Int, val status: String? = null) : Progress()
class Parsing : Progress(true)
class Running(val max: Int, val count: Int) : Progress(true, false)
class Saving : Progress(true)
class Syncing : Progress(true)
class Finished : Progress(true, false)
}
fun performBackup()
+1 -1
View File
@@ -153,6 +153,7 @@
</receiver>
<service android:name="com.android.mms.transaction.TransactionService" />
<service android:name=".feature.backup.RestoreBackupService" />
<service
android:name=".feature.widget.WidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
@@ -170,7 +171,6 @@
<data android:scheme="mmsto" />
</intent-filter>
</service>
<service android:name=".service.RestoreBackupService" />
<service android:name=".service.SendScheduledMessageService" />
<service android:name=".service.SendSmsService" />
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.common.util.extensions
import android.content.Context
import com.moez.QKSMS.R
import com.moez.QKSMS.repository.BackupRepository
fun BackupRepository.Progress.getLabel(context: Context): String? {
return when (this) {
is BackupRepository.Progress.Parsing -> context.getString(R.string.backup_progress_parsing)
is BackupRepository.Progress.Running -> context.getString(R.string.backup_progress_running, count, max)
is BackupRepository.Progress.Saving -> context.getString(R.string.backup_progress_saving)
is BackupRepository.Progress.Syncing -> context.getString(R.string.backup_progress_syncing)
is BackupRepository.Progress.Finished -> context.getString(R.string.backup_progress_finished)
else -> null
}
}
@@ -27,6 +27,7 @@ import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkController
import com.moez.QKSMS.common.util.DateFormatter
import com.moez.QKSMS.common.util.extensions.getLabel
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
import com.moez.QKSMS.common.util.extensions.setTint
import com.moez.QKSMS.common.widget.PreferenceView
@@ -85,24 +86,30 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
override fun render(state: BackupState) {
when {
state.backupProgress is BackupRepository.Progress.Running -> {
state.backupProgress.running -> {
progressIcon.setImageResource(R.drawable.ic_file_upload_black_24dp)
progressTitle.setText(R.string.backup_backing_up)
progressSummary.text = state.backupProgress.status
progressSummary.text = state.backupProgress.getLabel(activity!!)
progressSummary.isVisible = progressSummary.text.isNotEmpty()
progressBar.isIndeterminate = state.backupProgress.progress == 0
progressBar.progress = state.backupProgress.progress
val running = (state.backupProgress as? BackupRepository.Progress.Running)
progressBar.isVisible = state.backupProgress.indeterminate || running?.max ?: 0 > 0
progressBar.isIndeterminate = state.backupProgress.indeterminate
progressBar.max = running?.max ?: 0
progressBar.progress = running?.count ?: 0
progress.isVisible = true
fab.isVisible = false
}
state.restoreProgress is BackupRepository.Progress.Running -> {
state.restoreProgress.running -> {
progressIcon.setImageResource(R.drawable.ic_file_download_black_24dp)
progressTitle.setText(R.string.backup_restoring)
progressSummary.text = state.restoreProgress.status
progressSummary.text = state.restoreProgress.getLabel(activity!!)
progressSummary.isVisible = progressSummary.text.isNotEmpty()
progressBar.isIndeterminate = state.restoreProgress.progress == 0
progressBar.progress = state.restoreProgress.progress
val running = (state.restoreProgress as? BackupRepository.Progress.Running)
progressBar.isVisible = state.restoreProgress.indeterminate || running?.max ?: 0 > 0
progressBar.isIndeterminate = state.restoreProgress.indeterminate
progressBar.max = running?.max ?: 0
progressBar.progress = running?.count ?: 0
progress.isVisible = true
fab.isVisible = false
}
@@ -27,7 +27,6 @@ import com.moez.QKSMS.common.util.BillingManager
import com.moez.QKSMS.common.util.DateFormatter
import com.moez.QKSMS.interactor.PerformBackup
import com.moez.QKSMS.repository.BackupRepository
import com.moez.QKSMS.service.RestoreBackupService
import com.uber.autodispose.kotlin.autoDisposable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom
@@ -81,8 +80,8 @@ class BackupPresenter @Inject constructor(
{ _, backupProgress, restoreProgress, upgraded ->
when {
!upgraded -> context.toast(R.string.backup_restore_error_plus)
backupProgress is BackupRepository.Progress.Running -> context.toast(R.string.backup_restore_error_backup)
restoreProgress is BackupRepository.Progress.Running -> context.toast(R.string.backup_restore_error_restore)
backupProgress.running -> context.toast(R.string.backup_restore_error_backup)
restoreProgress.running -> context.toast(R.string.backup_restore_error_restore)
else -> view.selectFile()
}
}
@@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.service
package com.moez.QKSMS.feature.backup
import android.annotation.SuppressLint
import android.app.Service
@@ -25,6 +25,7 @@ import android.content.Intent
import android.os.IBinder
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import com.moez.QKSMS.common.util.extensions.getLabel
import com.moez.QKSMS.manager.NotificationManager
import com.moez.QKSMS.repository.BackupRepository
import dagger.android.AndroidInjection
@@ -83,14 +84,21 @@ class RestoreBackupService : Service() {
.subscribeOn(Schedulers.io())
.subscribe { progress ->
when (progress) {
is BackupRepository.Progress.Idle -> stop()
is BackupRepository.Progress.Running -> {
notification
.setProgress(100, progress.progress, false)
.setContentText("${progress.progress}%")
.setProgress(progress.max, progress.count, progress.indeterminate)
.setContentText(progress.getLabel(this))
notificationManager.notify(NOTIFICATION_ID, notification.build())
}
is BackupRepository.Progress.Idle -> stop()
else -> {
notification
.setProgress(0, 0, progress.indeterminate)
.setContentText(progress.getLabel(this))
notificationManager.notify(NOTIFICATION_ID, notification.build())
}
}
}
@@ -18,9 +18,9 @@
*/
package com.moez.QKSMS.injection.android
import com.moez.QKSMS.feature.backup.RestoreBackupService
import com.moez.QKSMS.injection.scope.ActivityScope
import com.moez.QKSMS.service.HeadlessSmsSendService
import com.moez.QKSMS.service.RestoreBackupService
import com.moez.QKSMS.service.SendScheduledMessageService
import com.moez.QKSMS.service.SendSmsService
import dagger.Module
@@ -166,6 +166,11 @@
</plurals>
<string name="backup_disclaimer">Currently, only SMS is supported by Backup and Restore. MMS support and scheduled backups will be coming soon!</string>
<string name="backup_now">Backup now</string>
<string name="backup_progress_parsing">Parsing backup…</string>
<string name="backup_progress_running">%d/%d messages</string>
<string name="backup_progress_saving">Saving backup…</string>
<string name="backup_progress_syncing">Syncing messages…</string>
<string name="backup_progress_finished">Finished!</string>
<string name="backup_notification_channel_name">Backup and restore</string>
<string name="scheduled_title">Scheduled</string>