Use foreground service to perform restore

This commit is contained in:
Moez Bhatti
2018-08-27 20:37:16 -04:00
parent 52d73e6d57
commit 9825c6c2ee
10 changed files with 148 additions and 14 deletions
@@ -148,13 +148,13 @@ class BackupRepositoryImpl @Inject constructor(
}
.map { files -> files.sortedByDescending { file -> file.date } }
override fun performRestore(backupFile: BackupFile) {
override fun performRestore(filePath: String) {
// If a backupFile or restore is already running, don't do anything
if (isBackupOrRestoreRunning()) return
restoreProgress.onNext(BackupRepository.Progress.Running(0, "Parsing backup"))
val file = File(backupFile.path)
val file = File(filePath)
val source = Okio.buffer(Okio.source(file))
val backup = moshi.adapter(Backup::class.java).fromJson(source)
@@ -0,0 +1,105 @@
/*
* 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.service
import android.annotation.SuppressLint
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import com.moez.QKSMS.manager.NotificationManager
import com.moez.QKSMS.repository.BackupRepository
import dagger.android.AndroidInjection
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class RestoreBackupService : Service() {
companion object {
private const val NOTIFICATION_ID = -1
private const val ACTION_START = "com.moez.QKSMS.ACTION_START"
private const val ACTION_STOP = "com.moez.QKSMS.ACTION_STOP"
private const val EXTRA_FILE_PATH = "com.moez.QKSMS.EXTRA_FILE_PATH"
fun start(context: Context, filePath: String) {
val intent = Intent(context, RestoreBackupService::class.java)
.setAction(ACTION_START)
.putExtra(EXTRA_FILE_PATH, filePath)
ContextCompat.startForegroundService(context, intent)
}
}
@Inject lateinit var backupRepo: BackupRepository
@Inject lateinit var notificationManager: NotificationManager
override fun onCreate() = AndroidInjection.inject(this)
override fun onBind(intent: Intent?): IBinder? = null
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
when (intent.action) {
ACTION_START -> start(intent)
ACTION_STOP -> stop()
}
return Service.START_STICKY
}
@SuppressLint("CheckResult")
private fun start(intent: Intent) {
val notification = notificationManager.getNotificationForBackup()
val notificationManager = NotificationManagerCompat.from(this)
startForeground(NOTIFICATION_ID, notification.build())
backupRepo.getRestoreProgress()
.sample(200, TimeUnit.MILLISECONDS, true)
.subscribeOn(Schedulers.io())
.subscribe { progress ->
when (progress) {
is BackupRepository.Progress.Running -> {
notification.setProgress(100, progress.progress, false)
notificationManager.notify(NOTIFICATION_ID, notification.build())
}
is BackupRepository.Progress.Idle -> stop()
}
}
// Start the restore
Observable.just(intent)
.map { it.getStringExtra(EXTRA_FILE_PATH) }
.subscribeOn(Schedulers.io())
.subscribe(backupRepo::performRestore)
}
private fun stop() {
stopForeground(true)
stopSelf()
}
}
@@ -18,16 +18,15 @@
*/
package com.moez.QKSMS.interactor
import com.moez.QKSMS.model.BackupFile
import com.moez.QKSMS.repository.BackupRepository
import io.reactivex.Flowable
import javax.inject.Inject
class PerformRestore @Inject constructor(
private val backupRepo: BackupRepository
) : Interactor<BackupFile>() {
) : Interactor<String>() {
override fun buildObservable(params: BackupFile): Flowable<*> {
override fun buildObservable(params: String): Flowable<*> {
return Flowable.just(params)
.doOnNext(backupRepo::performRestore)
}
@@ -18,6 +18,8 @@
*/
package com.moez.QKSMS.manager
import androidx.core.app.NotificationCompat
interface NotificationManager {
fun update(threadId: Long)
@@ -28,4 +30,6 @@ interface NotificationManager {
fun buildNotificationChannelId(threadId: Long): String
fun getNotificationForBackup(): NotificationCompat.Builder
}
@@ -37,7 +37,7 @@ interface BackupRepository {
*/
fun getBackups(): Observable<List<BackupFile>>
fun performRestore(backupFile: BackupFile)
fun performRestore(filePath: String)
fun getRestoreProgress(): Observable<Progress>
+4 -3
View File
@@ -153,6 +153,9 @@
</receiver>
<service android:name="com.android.mms.transaction.TransactionService" />
<service
android:name=".feature.widget.WidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
<service
android:name=".service.HeadlessSmsSendService"
android:exported="true"
@@ -167,9 +170,7 @@
<data android:scheme="mmsto" />
</intent-filter>
</service>
<service
android:name=".feature.widget.WidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
<service android:name=".service.RestoreBackupService" />
<service android:name=".service.SendScheduledMessageService" />
<service android:name=".service.SendSmsService" />
@@ -38,7 +38,6 @@ import com.moez.QKSMS.common.util.extensions.dpToPx
import com.moez.QKSMS.extensions.isImage
import com.moez.QKSMS.feature.compose.ComposeActivity
import com.moez.QKSMS.feature.qkreply.QkReplyActivity
import com.moez.QKSMS.interactor.DeleteMessages
import com.moez.QKSMS.manager.PermissionManager
import com.moez.QKSMS.mapper.CursorToPartImpl
import com.moez.QKSMS.receiver.DeleteMessagesReceiver
@@ -65,6 +64,8 @@ class NotificationManagerImpl @Inject constructor(
companion object {
const val DEFAULT_CHANNEL_ID = "notifications_default"
const val BACKUP_RESTORE_CHANNEL_ID = "notifications_backup_restore"
val VIBRATE_PATTERN = longArrayOf(0, 200, 0, 200)
}
@@ -374,4 +375,21 @@ class NotificationManagerImpl @Inject constructor(
}
}
override fun getNotificationForBackup(): NotificationCompat.Builder {
if (Build.VERSION.SDK_INT >= 26) {
val name = context.getString(R.string.backup_notification_channel_name)
val importance = NotificationManager.IMPORTANCE_LOW
val channel = NotificationChannel(BACKUP_RESTORE_CHANNEL_ID, name, importance)
val notificationManager = context.getSystemService(NotificationManager::class.java)
notificationManager.createNotificationChannel(channel)
}
return NotificationCompat.Builder(context, BACKUP_RESTORE_CHANNEL_ID)
.setContentTitle(context.getString(R.string.backup_restoring))
.setSmallIcon(R.drawable.ic_file_download_black_24dp)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setProgress(0, 0, true)
.setOngoing(true)
}
}
@@ -18,12 +18,13 @@
*/
package com.moez.QKSMS.feature.backup
import android.content.Context
import com.moez.QKSMS.common.Navigator
import com.moez.QKSMS.common.base.QkPresenter
import com.moez.QKSMS.common.util.BillingManager
import com.moez.QKSMS.interactor.PerformBackup
import com.moez.QKSMS.interactor.PerformRestore
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
@@ -33,9 +34,9 @@ import javax.inject.Inject
class BackupPresenter @Inject constructor(
backupRepo: BackupRepository,
private val billingManager: BillingManager,
private val context: Context,
private val navigator: Navigator,
private val performBackup: PerformBackup,
private val performRestore: PerformRestore
private val performBackup: PerformBackup
) : QkPresenter<BackupView, BackupState>(BackupState()) {
init {
@@ -65,7 +66,7 @@ class BackupPresenter @Inject constructor(
view.restoreFileSelected()
.autoDisposable(view.scope())
.subscribe { backup -> performRestore.execute(backup) }
.subscribe { backup -> RestoreBackupService.start(context, backup.path) }
view.fabClicks()
.withLatestFrom(billingManager.upgradeStatus) { _, upgraded -> upgraded }
@@ -20,6 +20,7 @@ package com.moez.QKSMS.injection.android
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
@@ -32,6 +33,10 @@ abstract class ServiceBuilderModule {
@ContributesAndroidInjector()
abstract fun bindHeadlessSmsSendService(): HeadlessSmsSendService
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindRestoreBackupService(): RestoreBackupService
@ActivityScope
@ContributesAndroidInjector()
abstract fun bindSendScheduledMessageReceiver(): SendScheduledMessageService
@@ -162,6 +162,7 @@
</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_notification_channel_name">Backup and restore</string>
<string name="scheduled_title">Scheduled</string>
<string name="scheduled_empty_description">Automatically send a message, at the exact moment you\'d like</string>