diff --git a/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt b/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt index e11244d5..4229e481 100644 --- a/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/repository/BackupRepositoryImpl.kt @@ -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 = 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 } } \ No newline at end of file diff --git a/domain/src/main/java/com/moez/QKSMS/repository/BackupRepository.kt b/domain/src/main/java/com/moez/QKSMS/repository/BackupRepository.kt index 7bf83a57..04bf6a87 100644 --- a/domain/src/main/java/com/moez/QKSMS/repository/BackupRepository.kt +++ b/domain/src/main/java/com/moez/QKSMS/repository/BackupRepository.kt @@ -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() diff --git a/presentation/src/main/AndroidManifest.xml b/presentation/src/main/AndroidManifest.xml index e0c0405b..f14a4029 100644 --- a/presentation/src/main/AndroidManifest.xml +++ b/presentation/src/main/AndroidManifest.xml @@ -153,6 +153,7 @@ + @@ -170,7 +171,6 @@ - diff --git a/presentation/src/main/java/com/moez/QKSMS/common/util/extensions/ProgressExtensions.kt b/presentation/src/main/java/com/moez/QKSMS/common/util/extensions/ProgressExtensions.kt new file mode 100644 index 00000000..7af20f2d --- /dev/null +++ b/presentation/src/main/java/com/moez/QKSMS/common/util/extensions/ProgressExtensions.kt @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * 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 . + */ +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 + } +} \ No newline at end of file diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupController.kt b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupController.kt index d43ff362..582f0dc7 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupController.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupController.kt @@ -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( 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 } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupPresenter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupPresenter.kt index bd70e1b5..f5b69019 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupPresenter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/backup/BackupPresenter.kt @@ -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() } } diff --git a/data/src/main/java/com/moez/QKSMS/service/RestoreBackupService.kt b/presentation/src/main/java/com/moez/QKSMS/feature/backup/RestoreBackupService.kt similarity index 85% rename from data/src/main/java/com/moez/QKSMS/service/RestoreBackupService.kt rename to presentation/src/main/java/com/moez/QKSMS/feature/backup/RestoreBackupService.kt index efcd3dad..9b30e160 100644 --- a/data/src/main/java/com/moez/QKSMS/service/RestoreBackupService.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/backup/RestoreBackupService.kt @@ -16,7 +16,7 @@ * You should have received a copy of the GNU General Public License * along with QKSMS. If not, see . */ -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()) + } } } diff --git a/presentation/src/main/java/com/moez/QKSMS/injection/android/ServiceBuilderModule.kt b/presentation/src/main/java/com/moez/QKSMS/injection/android/ServiceBuilderModule.kt index 142d1207..c14aa210 100644 --- a/presentation/src/main/java/com/moez/QKSMS/injection/android/ServiceBuilderModule.kt +++ b/presentation/src/main/java/com/moez/QKSMS/injection/android/ServiceBuilderModule.kt @@ -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 diff --git a/presentation/src/main/res/values/strings.xml b/presentation/src/main/res/values/strings.xml index 2f1dc6e8..9b514b90 100644 --- a/presentation/src/main/res/values/strings.xml +++ b/presentation/src/main/res/values/strings.xml @@ -166,6 +166,11 @@ Currently, only SMS is supported by Backup and Restore. MMS support and scheduled backups will be coming soon! Backup now + Parsing backup… + %d/%d messages + Saving backup… + Syncing messages… + Finished! Backup and restore Scheduled