forked from upstream-mirrors/quik
Migrate backup & restore feature to use SAF (#1900)
* Migrate backup & restore feature to use SAF * Remove unused imports * Remove unused rx subject * Remove unused dateFormatter * Open picker to current dir * Improve exception messages when handling DocumentFile
This commit is contained in:
@@ -4,6 +4,7 @@ buildscript {
|
|||||||
ext.androidx_appcompat_version = '1.3.1'
|
ext.androidx_appcompat_version = '1.3.1'
|
||||||
ext.androidx_constraintlayout_version = '1.1.3'
|
ext.androidx_constraintlayout_version = '1.1.3'
|
||||||
ext.androidx_core_version = '1.1.0'
|
ext.androidx_core_version = '1.1.0'
|
||||||
|
ext.androidx_documentfile_version = '1.0.1'
|
||||||
ext.androidx_emoji_version = '1.0.0'
|
ext.androidx_emoji_version = '1.0.0'
|
||||||
ext.androidx_exifinterface_version = '1.0.0'
|
ext.androidx_exifinterface_version = '1.0.0'
|
||||||
ext.androidx_testrunner_version = '1.1.0-alpha3'
|
ext.androidx_testrunner_version = '1.1.0-alpha3'
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ dependencies {
|
|||||||
implementation "androidx.appcompat:appcompat:$androidx_appcompat_version"
|
implementation "androidx.appcompat:appcompat:$androidx_appcompat_version"
|
||||||
implementation "androidx.core:core-ktx:$androidx_core_version"
|
implementation "androidx.core:core-ktx:$androidx_core_version"
|
||||||
implementation "androidx.exifinterface:exifinterface:$androidx_exifinterface_version"
|
implementation "androidx.exifinterface:exifinterface:$androidx_exifinterface_version"
|
||||||
|
implementation "androidx.documentfile:documentfile:$androidx_documentfile_version"
|
||||||
|
|
||||||
// glide
|
// glide
|
||||||
implementation "com.github.bumptech.glide:glide:$glide_version"
|
implementation "com.github.bumptech.glide:glide:$glide_version"
|
||||||
|
|||||||
@@ -18,26 +18,27 @@
|
|||||||
*/
|
*/
|
||||||
package com.moez.QKSMS.repository
|
package com.moez.QKSMS.repository
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
import androidx.core.content.contentValuesOf
|
import androidx.core.content.contentValuesOf
|
||||||
|
import androidx.core.net.toUri
|
||||||
|
import androidx.documentfile.provider.DocumentFile
|
||||||
|
import com.moez.QKSMS.common.util.extensions.now
|
||||||
import com.moez.QKSMS.model.BackupFile
|
import com.moez.QKSMS.model.BackupFile
|
||||||
import com.moez.QKSMS.model.Message
|
import com.moez.QKSMS.model.Message
|
||||||
import com.moez.QKSMS.util.Preferences
|
import com.moez.QKSMS.util.Preferences
|
||||||
import com.moez.QKSMS.util.QkFileObserver
|
|
||||||
import com.moez.QKSMS.util.tryOrNull
|
|
||||||
import com.squareup.moshi.Moshi
|
import com.squareup.moshi.Moshi
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
import io.reactivex.schedulers.Schedulers
|
|
||||||
import io.reactivex.subjects.BehaviorSubject
|
import io.reactivex.subjects.BehaviorSubject
|
||||||
import io.reactivex.subjects.Subject
|
import io.reactivex.subjects.Subject
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import okio.buffer
|
import okio.buffer
|
||||||
import okio.source
|
import okio.source
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.File
|
|
||||||
import java.io.FileOutputStream
|
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -52,10 +53,6 @@ class BackupRepositoryImpl @Inject constructor(
|
|||||||
private val syncRepo: SyncRepository
|
private val syncRepo: SyncRepository
|
||||||
) : BackupRepository {
|
) : BackupRepository {
|
||||||
|
|
||||||
companion object {
|
|
||||||
private val BACKUP_DIRECTORY = Environment.getExternalStorageDirectory().toString() + "/QKSMS/Backups"
|
|
||||||
}
|
|
||||||
|
|
||||||
data class Backup(
|
data class Backup(
|
||||||
val messageCount: Int = 0,
|
val messageCount: Int = 0,
|
||||||
val messages: List<BackupMessage> = listOf()
|
val messages: List<BackupMessage> = listOf()
|
||||||
@@ -91,11 +88,35 @@ class BackupRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
@Volatile private var stopFlag: Boolean = false
|
@Volatile private var stopFlag: Boolean = false
|
||||||
|
|
||||||
|
override fun getDefaultBackupPath(): String {
|
||||||
|
return "${Environment.getExternalStorageDirectory()}/QKSMS/Backups"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getBackupDocumentTree(): DocumentFile? {
|
||||||
|
return prefs.backupDirectory.get()
|
||||||
|
.takeIf { uri -> uri != Uri.EMPTY }
|
||||||
|
?.let { uri -> DocumentFile.fromTreeUri(context, uri) }
|
||||||
|
?.takeIf { dir -> dir.exists() && dir.canRead() && dir.canWrite() }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getBackupPathUriForPicker(): Uri {
|
||||||
|
return prefs.backupDirectory.get().takeIf { uri -> uri != Uri.EMPTY }
|
||||||
|
?: getDefaultBackupPath().toUri()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun persistBackupDirectory(directory: Uri) {
|
||||||
|
context.contentResolver.takePersistableUriPermission(directory,
|
||||||
|
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
|
||||||
|
prefs.backupDirectory.set(directory)
|
||||||
|
|
||||||
|
Timber.v("Updated backup directory: $directory")
|
||||||
|
}
|
||||||
|
|
||||||
override fun performBackup() {
|
override fun performBackup() {
|
||||||
// If a backup or restore is already running, don't do anything
|
// If a backup or restore is already running, don't do anything
|
||||||
if (isBackupOrRestoreRunning()) return
|
if (isBackupOrRestoreRunning()) return
|
||||||
|
|
||||||
var messageCount = 0
|
var messageCount: Int
|
||||||
|
|
||||||
// Map all the messages into our object we'll use for the Json mapping
|
// Map all the messages into our object we'll use for the Json mapping
|
||||||
val backupMessages = Realm.getDefaultInstance().use { realm ->
|
val backupMessages = Realm.getDefaultInstance().use { realm ->
|
||||||
@@ -119,13 +140,15 @@ class BackupRepositoryImpl @Inject constructor(
|
|||||||
val json = adapter.toJson(Backup(messageCount, backupMessages)).toByteArray()
|
val json = adapter.toJson(Backup(messageCount, backupMessages)).toByteArray()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Create the directory and file
|
val timestamp = SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(now())
|
||||||
val dir = File(BACKUP_DIRECTORY).apply { mkdirs() }
|
val inputStream = getBackupDocumentTree()
|
||||||
val timestamp = SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(System.currentTimeMillis())
|
?.createFile("application/json", "backup-$timestamp.json")
|
||||||
val file = File(dir, "backup-$timestamp.json")
|
?.let { file -> context.contentResolver.openOutputStream(file.uri) }
|
||||||
|
?: throw Exception("Failed to open output stream")
|
||||||
|
|
||||||
// Write the log to the file
|
inputStream.use { stream ->
|
||||||
FileOutputStream(file, true).use { fileOutputStream -> fileOutputStream.write(json) }
|
stream.write(json)
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.w(e)
|
Timber.w(e)
|
||||||
}
|
}
|
||||||
@@ -151,36 +174,34 @@ class BackupRepositoryImpl @Inject constructor(
|
|||||||
|
|
||||||
override fun getBackupProgress(): Observable<BackupRepository.Progress> = backupProgress
|
override fun getBackupProgress(): Observable<BackupRepository.Progress> = backupProgress
|
||||||
|
|
||||||
override fun getBackups(): Observable<List<BackupFile>> = QkFileObserver(BACKUP_DIRECTORY).observable
|
@SuppressLint("Recycle") // InputStream is closed by Okio BufferedSource
|
||||||
.map { File(BACKUP_DIRECTORY).listFiles() ?: arrayOf() }
|
override fun parseBackup(uri: Uri): BackupFile {
|
||||||
.subscribeOn(Schedulers.io())
|
val adapter = moshi.adapter(BackupMetadata::class.java)
|
||||||
.observeOn(Schedulers.computation())
|
|
||||||
.map { files ->
|
|
||||||
files.mapNotNull { file ->
|
|
||||||
val adapter = moshi.adapter(BackupMetadata::class.java)
|
|
||||||
val backup = tryOrNull(false) {
|
|
||||||
file.source().buffer().use(adapter::fromJson)
|
|
||||||
} ?: return@mapNotNull null
|
|
||||||
|
|
||||||
val path = file.path
|
val file = DocumentFile.fromSingleUri(context, uri)
|
||||||
val date = file.lastModified()
|
?: throw IllegalArgumentException("Couldn't open backup file")
|
||||||
val messages = backup.messageCount
|
|
||||||
val size = file.length()
|
|
||||||
BackupFile(path, date, messages, size)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.map { files -> files.sortedByDescending { file -> file.date } }
|
|
||||||
|
|
||||||
override fun performRestore(filePath: String) {
|
val metadata = context.contentResolver.openInputStream(file.uri)
|
||||||
|
?.source()
|
||||||
|
?.buffer()
|
||||||
|
?.use(adapter::fromJson)
|
||||||
|
?: throw IllegalArgumentException("Couldn't parse backup file")
|
||||||
|
|
||||||
|
return BackupFile(file.lastModified(), metadata.messageCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun performRestore(uri: Uri) {
|
||||||
// If a backupFile or restore is already running, don't do anything
|
// If a backupFile or restore is already running, don't do anything
|
||||||
if (isBackupOrRestoreRunning()) return
|
if (isBackupOrRestoreRunning()) return
|
||||||
|
|
||||||
restoreProgress.onNext(BackupRepository.Progress.Parsing())
|
restoreProgress.onNext(BackupRepository.Progress.Parsing())
|
||||||
|
|
||||||
val file = File(filePath)
|
val adapter = moshi.adapter(Backup::class.java)
|
||||||
val backup = file.source().buffer().use { source ->
|
val backup = DocumentFile.fromSingleUri(context, uri)
|
||||||
moshi.adapter(Backup::class.java).fromJson(source)
|
?.let { file -> context.contentResolver.openInputStream(file.uri) }
|
||||||
}
|
?.source()
|
||||||
|
?.buffer()
|
||||||
|
?.use(adapter::fromJson)
|
||||||
|
|
||||||
val messageCount = backup?.messages?.size ?: 0
|
val messageCount = backup?.messages?.size ?: 0
|
||||||
var errorCount = 0
|
var errorCount = 0
|
||||||
@@ -234,12 +255,12 @@ class BackupRepositoryImpl @Inject constructor(
|
|||||||
Timer().schedule(1000) { restoreProgress.onNext(BackupRepository.Progress.Idle()) }
|
Timer().schedule(1000) { restoreProgress.onNext(BackupRepository.Progress.Idle()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getRestoreProgress(): Observable<BackupRepository.Progress> = restoreProgress
|
||||||
|
|
||||||
override fun stopRestore() {
|
override fun stopRestore() {
|
||||||
stopFlag = true
|
stopFlag = true
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getRestoreProgress(): Observable<BackupRepository.Progress> = restoreProgress
|
|
||||||
|
|
||||||
private fun isBackupOrRestoreRunning(): Boolean {
|
private fun isBackupOrRestoreRunning(): Boolean {
|
||||||
return backupProgress.blockingFirst().running || restoreProgress.blockingFirst().running
|
return backupProgress.blockingFirst().running || restoreProgress.blockingFirst().running
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
// androidx
|
||||||
|
implementation "androidx.documentfile:documentfile:$androidx_documentfile_version"
|
||||||
|
|
||||||
// dagger
|
// dagger
|
||||||
implementation "com.google.dagger:dagger:$dagger_version"
|
implementation "com.google.dagger:dagger:$dagger_version"
|
||||||
|
|||||||
@@ -18,17 +18,18 @@
|
|||||||
*/
|
*/
|
||||||
package com.moez.QKSMS.interactor
|
package com.moez.QKSMS.interactor
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
import com.moez.QKSMS.repository.BackupRepository
|
import com.moez.QKSMS.repository.BackupRepository
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class PerformRestore @Inject constructor(
|
class PerformRestore @Inject constructor(
|
||||||
private val backupRepo: BackupRepository
|
private val backupRepo: BackupRepository
|
||||||
) : Interactor<String>() {
|
) : Interactor<Uri>() {
|
||||||
|
|
||||||
override fun buildObservable(params: String): Flowable<*> {
|
override fun buildObservable(params: Uri): Flowable<*> {
|
||||||
return Flowable.just(params)
|
return Flowable.just(params)
|
||||||
.doOnNext(backupRepo::performRestore)
|
.doOnNext(backupRepo::performRestore)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,8 +19,6 @@
|
|||||||
package com.moez.QKSMS.model
|
package com.moez.QKSMS.model
|
||||||
|
|
||||||
data class BackupFile(
|
data class BackupFile(
|
||||||
val path: String,
|
|
||||||
val date: Long,
|
val date: Long,
|
||||||
val messages: Int,
|
val messages: Int
|
||||||
val size: Long
|
)
|
||||||
)
|
|
||||||
|
|||||||
@@ -18,6 +18,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.moez.QKSMS.repository
|
package com.moez.QKSMS.repository
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import androidx.documentfile.provider.DocumentFile
|
||||||
import com.moez.QKSMS.model.BackupFile
|
import com.moez.QKSMS.model.BackupFile
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
|
|
||||||
@@ -32,19 +34,24 @@ interface BackupRepository {
|
|||||||
class Finished : Progress(true, false)
|
class Finished : Progress(true, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getDefaultBackupPath(): String
|
||||||
|
|
||||||
|
fun getBackupDocumentTree(): DocumentFile?
|
||||||
|
|
||||||
|
fun getBackupPathUriForPicker(): Uri
|
||||||
|
|
||||||
|
fun persistBackupDirectory(directory: Uri)
|
||||||
|
|
||||||
fun performBackup()
|
fun performBackup()
|
||||||
|
|
||||||
fun getBackupProgress(): Observable<Progress>
|
fun getBackupProgress(): Observable<Progress>
|
||||||
|
|
||||||
/**
|
fun parseBackup(uri: Uri): BackupFile
|
||||||
* Returns a list of all local backups
|
|
||||||
*/
|
|
||||||
fun getBackups(): Observable<List<BackupFile>>
|
|
||||||
|
|
||||||
fun performRestore(filePath: String)
|
fun performRestore(uri: Uri)
|
||||||
|
|
||||||
fun stopRestore()
|
|
||||||
|
|
||||||
fun getRestoreProgress(): Observable<Progress>
|
fun getRestoreProgress(): Observable<Progress>
|
||||||
|
|
||||||
}
|
fun stopRestore()
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ package com.moez.QKSMS.util
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
import com.f2prateek.rx.preferences2.Preference
|
import com.f2prateek.rx.preferences2.Preference
|
||||||
@@ -85,6 +86,7 @@ class Preferences @Inject constructor(
|
|||||||
val version = rxPrefs.getInteger("version", context.versionCode)
|
val version = rxPrefs.getInteger("version", context.versionCode)
|
||||||
val changelogVersion = rxPrefs.getInteger("changelogVersion", context.versionCode)
|
val changelogVersion = rxPrefs.getInteger("changelogVersion", context.versionCode)
|
||||||
val hasAskedForNotificationPermission = rxPrefs.getBoolean("hasAskedForNotificationPermission", false)
|
val hasAskedForNotificationPermission = rxPrefs.getBoolean("hasAskedForNotificationPermission", false)
|
||||||
|
val backupDirectory = rxPrefs.getObject("backupDirectory", Uri.EMPTY, UriPreferenceConverter())
|
||||||
@Deprecated("This should only be accessed when migrating to @blockingManager")
|
@Deprecated("This should only be accessed when migrating to @blockingManager")
|
||||||
val sia = rxPrefs.getBoolean("sia", false)
|
val sia = rxPrefs.getBoolean("sia", false)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.moez.QKSMS.util
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import com.f2prateek.rx.preferences2.Preference.Converter
|
||||||
|
|
||||||
|
class UriPreferenceConverter : Converter<Uri> {
|
||||||
|
|
||||||
|
override fun deserialize(serialized: String): Uri {
|
||||||
|
return Uri.parse(serialized)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun serialize(value: Uri): String {
|
||||||
|
return value.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.moez.QKSMS.common.util
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.Build
|
||||||
|
import android.provider.DocumentsContract
|
||||||
|
import androidx.activity.result.contract.ActivityResultContract
|
||||||
|
|
||||||
|
class QkActivityResultContracts {
|
||||||
|
|
||||||
|
data class OpenDocumentParams(
|
||||||
|
val mimeTypes: List<String>,
|
||||||
|
val initialUri: Uri? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
class OpenDocument : ActivityResultContract<OpenDocumentParams, Uri>() {
|
||||||
|
override fun createIntent(context: Context, input: OpenDocumentParams): Intent {
|
||||||
|
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
|
||||||
|
.putExtra(Intent.EXTRA_MIME_TYPES, input.mimeTypes.toTypedArray())
|
||||||
|
.setType("*/*")
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, input.initialUri)
|
||||||
|
}
|
||||||
|
|
||||||
|
return intent
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
|
||||||
|
if (intent == null || resultCode != Activity.RESULT_OK) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return intent.data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+9
-1
@@ -32,4 +32,12 @@ fun AlertDialog.Builder.setNegativeButton(@StringRes textId: Int, subject: Subje
|
|||||||
|
|
||||||
fun AlertDialog.Builder.setNeutralButton(@StringRes textId: Int, subject: Subject<Unit>): AlertDialog.Builder {
|
fun AlertDialog.Builder.setNeutralButton(@StringRes textId: Int, subject: Subject<Unit>): AlertDialog.Builder {
|
||||||
return setNeutralButton(textId) { _, _ -> subject.onNext(Unit) }
|
return setNeutralButton(textId) { _, _ -> subject.onNext(Unit) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun AlertDialog.setShowing(show: Boolean) {
|
||||||
|
if (isShowing && !show) {
|
||||||
|
dismiss()
|
||||||
|
} else if (!isShowing && show) {
|
||||||
|
show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.feature.backup
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.text.format.Formatter
|
|
||||||
import android.view.LayoutInflater
|
|
||||||
import android.view.ViewGroup
|
|
||||||
import com.moez.QKSMS.R
|
|
||||||
import com.moez.QKSMS.common.base.FlowableAdapter
|
|
||||||
import com.moez.QKSMS.common.base.QkViewHolder
|
|
||||||
import com.moez.QKSMS.common.util.DateFormatter
|
|
||||||
import com.moez.QKSMS.model.BackupFile
|
|
||||||
import io.reactivex.subjects.PublishSubject
|
|
||||||
import io.reactivex.subjects.Subject
|
|
||||||
import kotlinx.android.synthetic.main.backup_list_item.*
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
class BackupAdapter @Inject constructor(
|
|
||||||
private val context: Context,
|
|
||||||
private val dateFormatter: DateFormatter
|
|
||||||
) : FlowableAdapter<BackupFile>() {
|
|
||||||
|
|
||||||
val backupSelected: Subject<BackupFile> = PublishSubject.create()
|
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
|
|
||||||
val layoutInflater = LayoutInflater.from(parent.context)
|
|
||||||
val view = layoutInflater.inflate(R.layout.backup_list_item, parent, false)
|
|
||||||
|
|
||||||
return QkViewHolder(view).apply {
|
|
||||||
view.setOnClickListener { backupSelected.onNext(getItem(adapterPosition)) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
|
||||||
val backup = getItem(position)
|
|
||||||
|
|
||||||
val count = backup.messages
|
|
||||||
|
|
||||||
holder.title.text = dateFormatter.getDetailedTimestamp(backup.date)
|
|
||||||
holder.messages.text = context.resources.getQuantityString(R.plurals.backup_message_count, count, count)
|
|
||||||
holder.size.text = Formatter.formatFileSize(context, backup.size)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -18,78 +18,112 @@
|
|||||||
*/
|
*/
|
||||||
package com.moez.QKSMS.feature.backup
|
package com.moez.QKSMS.feature.backup
|
||||||
|
|
||||||
import android.Manifest
|
import android.content.Context
|
||||||
import android.app.Activity
|
|
||||||
import android.content.res.ColorStateList
|
import android.content.res.ColorStateList
|
||||||
import android.graphics.Typeface
|
import android.graphics.Typeface
|
||||||
|
import android.net.Uri
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.core.app.ActivityCompat
|
|
||||||
import androidx.core.view.children
|
import androidx.core.view.children
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import com.jakewharton.rxbinding2.view.clicks
|
import com.jakewharton.rxbinding2.view.clicks
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import com.moez.QKSMS.common.base.QkController
|
import com.moez.QKSMS.common.base.QkController
|
||||||
import com.moez.QKSMS.common.util.DateFormatter
|
import com.moez.QKSMS.common.util.DateFormatter
|
||||||
|
import com.moez.QKSMS.common.util.QkActivityResultContracts
|
||||||
import com.moez.QKSMS.common.util.extensions.getLabel
|
import com.moez.QKSMS.common.util.extensions.getLabel
|
||||||
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
|
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
|
||||||
|
import com.moez.QKSMS.common.util.extensions.setNegativeButton
|
||||||
import com.moez.QKSMS.common.util.extensions.setPositiveButton
|
import com.moez.QKSMS.common.util.extensions.setPositiveButton
|
||||||
|
import com.moez.QKSMS.common.util.extensions.setShowing
|
||||||
import com.moez.QKSMS.common.util.extensions.setTint
|
import com.moez.QKSMS.common.util.extensions.setTint
|
||||||
import com.moez.QKSMS.common.widget.PreferenceView
|
import com.moez.QKSMS.common.widget.PreferenceView
|
||||||
import com.moez.QKSMS.injection.appComponent
|
import com.moez.QKSMS.injection.appComponent
|
||||||
import com.moez.QKSMS.model.BackupFile
|
|
||||||
import com.moez.QKSMS.repository.BackupRepository
|
import com.moez.QKSMS.repository.BackupRepository
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
import io.reactivex.subjects.PublishSubject
|
import io.reactivex.subjects.PublishSubject
|
||||||
import io.reactivex.subjects.Subject
|
import io.reactivex.subjects.Subject
|
||||||
import kotlinx.android.synthetic.main.backup_controller.*
|
import kotlinx.android.synthetic.main.backup_controller.*
|
||||||
import kotlinx.android.synthetic.main.backup_list_dialog.view.*
|
|
||||||
import kotlinx.android.synthetic.main.preference_view.view.*
|
import kotlinx.android.synthetic.main.preference_view.view.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class BackupController : QkController<BackupView, BackupState, BackupPresenter>(), BackupView {
|
class BackupController : QkController<BackupView, BackupState, BackupPresenter>(), BackupView {
|
||||||
|
|
||||||
@Inject lateinit var adapter: BackupAdapter
|
|
||||||
@Inject lateinit var dateFormatter: DateFormatter
|
|
||||||
@Inject override lateinit var presenter: BackupPresenter
|
@Inject override lateinit var presenter: BackupPresenter
|
||||||
|
|
||||||
private val activityVisibleSubject: Subject<Unit> = PublishSubject.create()
|
private val selectFolderCancelSubject: Subject<Unit> = PublishSubject.create()
|
||||||
private val confirmRestoreSubject: Subject<Unit> = PublishSubject.create()
|
private val selectFolderConfirmSubject: Subject<Unit> = PublishSubject.create()
|
||||||
private val stopRestoreSubject: Subject<Unit> = PublishSubject.create()
|
|
||||||
|
|
||||||
private val backupFilesDialog by lazy {
|
private val restoreErrorConfirmSubject: Subject<Unit> = PublishSubject.create()
|
||||||
val view = View.inflate(activity, R.layout.backup_list_dialog, null)
|
|
||||||
.apply { files.adapter = adapter.apply { emptyView = empty } }
|
|
||||||
|
|
||||||
AlertDialog.Builder(activity!!)
|
private val confirmRestoreCancelSubject: Subject<Unit> = PublishSubject.create()
|
||||||
.setView(view)
|
private val confirmRestoreConfirmSubject: Subject<Unit> = PublishSubject.create()
|
||||||
.setCancelable(true)
|
|
||||||
.create()
|
|
||||||
}
|
|
||||||
|
|
||||||
private val confirmRestoreDialog by lazy {
|
private val stopRestoreConfirmSubject: Subject<Unit> = PublishSubject.create()
|
||||||
AlertDialog.Builder(activity!!)
|
private val stopRestoreCancelSubject: Subject<Unit> = PublishSubject.create()
|
||||||
.setTitle(R.string.backup_restore_confirm_title)
|
|
||||||
.setMessage(R.string.backup_restore_confirm_message)
|
private val documentTreeSelectedSubject: Subject<Uri> = PublishSubject.create()
|
||||||
.setPositiveButton(R.string.backup_restore_title, confirmRestoreSubject)
|
private val documentSelectedSubject: Subject<Uri> = PublishSubject.create()
|
||||||
.setNegativeButton(R.string.button_cancel, null)
|
|
||||||
.create()
|
|
||||||
}
|
|
||||||
|
|
||||||
private val stopRestoreDialog by lazy {
|
private val stopRestoreDialog by lazy {
|
||||||
AlertDialog.Builder(activity!!)
|
AlertDialog.Builder(activity!!)
|
||||||
.setTitle(R.string.backup_restore_stop_title)
|
.setTitle(R.string.backup_restore_stop_title)
|
||||||
.setMessage(R.string.backup_restore_stop_message)
|
.setMessage(R.string.backup_restore_stop_message)
|
||||||
.setPositiveButton(R.string.button_stop, stopRestoreSubject)
|
.setPositiveButton(R.string.button_stop, stopRestoreConfirmSubject)
|
||||||
.setNegativeButton(R.string.button_cancel, null)
|
.setNegativeButton(R.string.button_cancel, stopRestoreCancelSubject)
|
||||||
|
.setCancelable(false)
|
||||||
.create()
|
.create()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val selectLocationRationaleDialog by lazy {
|
||||||
|
AlertDialog.Builder(activity!!)
|
||||||
|
.setTitle(R.string.backup_select_location_rationale_title)
|
||||||
|
.setMessage(R.string.backup_select_location_rationale_message)
|
||||||
|
.setPositiveButton(R.string.button_continue, selectFolderConfirmSubject)
|
||||||
|
.setNegativeButton(R.string.button_cancel, selectFolderCancelSubject)
|
||||||
|
.setCancelable(false)
|
||||||
|
.create()
|
||||||
|
}
|
||||||
|
|
||||||
|
private val selectedBackupErrorDialog by lazy {
|
||||||
|
AlertDialog.Builder(activity!!)
|
||||||
|
.setTitle(R.string.backup_selected_backup_error_title)
|
||||||
|
.setMessage(R.string.backup_selected_backup_error_message)
|
||||||
|
.setPositiveButton(R.string.button_continue, restoreErrorConfirmSubject)
|
||||||
|
.setCancelable(false)
|
||||||
|
.create()
|
||||||
|
}
|
||||||
|
|
||||||
|
private val selectedBackupDetailsDialog by lazy {
|
||||||
|
AlertDialog.Builder(activity!!)
|
||||||
|
.setTitle(R.string.backup_selected_backup_details_title)
|
||||||
|
.setPositiveButton(R.string.backup_restore_title, confirmRestoreConfirmSubject)
|
||||||
|
.setNegativeButton(R.string.button_cancel, confirmRestoreCancelSubject)
|
||||||
|
.setCancelable(false)
|
||||||
|
.create()
|
||||||
|
}
|
||||||
|
|
||||||
|
private lateinit var openDirectory: ActivityResultLauncher<Uri>
|
||||||
|
private lateinit var openDocument: ActivityResultLauncher<QkActivityResultContracts.OpenDocumentParams>
|
||||||
|
|
||||||
init {
|
init {
|
||||||
appComponent.inject(this)
|
appComponent.inject(this)
|
||||||
layoutRes = R.layout.backup_controller
|
layoutRes = R.layout.backup_controller
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onContextAvailable(context: Context) {
|
||||||
|
// Init activity result contracts
|
||||||
|
openDirectory = themedActivity!!.registerForActivityResult(
|
||||||
|
ActivityResultContracts.OpenDocumentTree(),
|
||||||
|
documentTreeSelectedSubject::onNext)
|
||||||
|
|
||||||
|
openDocument = themedActivity!!.registerForActivityResult(
|
||||||
|
QkActivityResultContracts.OpenDocument(),
|
||||||
|
documentSelectedSubject::onNext)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onAttach(view: View) {
|
override fun onAttach(view: View) {
|
||||||
super.onAttach(view)
|
super.onAttach(view)
|
||||||
presenter.bindIntents(this)
|
presenter.bindIntents(this)
|
||||||
@@ -115,11 +149,6 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
|
|||||||
.forEach { it.setTypeface(it.typeface, Typeface.BOLD) }
|
.forEach { it.setTypeface(it.typeface, Typeface.BOLD) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onActivityResumed(activity: Activity) {
|
|
||||||
super.onActivityResumed(activity)
|
|
||||||
activityVisibleSubject.onNext(Unit)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun render(state: BackupState) {
|
override fun render(state: BackupState) {
|
||||||
when {
|
when {
|
||||||
state.backupProgress.running -> {
|
state.backupProgress.running -> {
|
||||||
@@ -158,9 +187,14 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
backup.summary = state.lastBackup
|
selectLocationRationaleDialog.setShowing(state.showLocationRationale)
|
||||||
|
|
||||||
adapter.data = state.backups
|
selectedBackupErrorDialog.setShowing(state.showSelectedBackupError)
|
||||||
|
|
||||||
|
selectedBackupDetailsDialog.setMessage(state.selectedBackupDetails)
|
||||||
|
selectedBackupDetailsDialog.setShowing(state.selectedBackupDetails != null)
|
||||||
|
|
||||||
|
stopRestoreDialog.setShowing(state.showStopRestoreDialog)
|
||||||
|
|
||||||
fabIcon.setImageResource(when (state.upgraded) {
|
fabIcon.setImageResource(when (state.upgraded) {
|
||||||
true -> R.drawable.ic_file_upload_black_24dp
|
true -> R.drawable.ic_file_upload_black_24dp
|
||||||
@@ -173,29 +207,40 @@ class BackupController : QkController<BackupView, BackupState, BackupPresenter>(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun activityVisible(): Observable<*> = activityVisibleSubject
|
override fun setBackupLocationClicks(): Observable<*> = location.clicks()
|
||||||
|
|
||||||
override fun restoreClicks(): Observable<*> = restore.clicks()
|
override fun restoreClicks(): Observable<*> = restore.clicks()
|
||||||
|
|
||||||
override fun restoreFileSelected(): Observable<BackupFile> = adapter.backupSelected
|
override fun locationRationaleConfirmClicks(): Observable<*> = selectFolderConfirmSubject
|
||||||
.doOnNext { backupFilesDialog.dismiss() }
|
|
||||||
|
|
||||||
override fun restoreConfirmed(): Observable<*> = confirmRestoreSubject
|
override fun locationRationaleCancelClicks(): Observable<*> = selectFolderCancelSubject
|
||||||
|
|
||||||
|
override fun selectedBackupErrorClicks(): Observable<*> = restoreErrorConfirmSubject
|
||||||
|
|
||||||
|
override fun confirmRestoreBackupConfirmClicks(): Observable<*> = confirmRestoreConfirmSubject
|
||||||
|
|
||||||
|
override fun confirmRestoreBackupCancelClicks(): Observable<*> = confirmRestoreCancelSubject
|
||||||
|
|
||||||
override fun stopRestoreClicks(): Observable<*> = progressCancel.clicks()
|
override fun stopRestoreClicks(): Observable<*> = progressCancel.clicks()
|
||||||
|
|
||||||
override fun stopRestoreConfirmed(): Observable<*> = stopRestoreSubject
|
override fun stopRestoreConfirmed(): Observable<*> = stopRestoreConfirmSubject
|
||||||
|
|
||||||
override fun fabClicks(): Observable<*> = fab.clicks()
|
override fun stopRestoreCancel(): Observable<*> = stopRestoreCancelSubject
|
||||||
|
|
||||||
override fun requestStoragePermission() {
|
override fun backupClicks(): Observable<*> = fab.clicks()
|
||||||
ActivityCompat.requestPermissions(activity!!, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 0)
|
|
||||||
|
override fun documentTreeSelected(): Observable<Uri> = documentTreeSelectedSubject
|
||||||
|
|
||||||
|
override fun documentSelected(): Observable<Uri> = documentSelectedSubject
|
||||||
|
|
||||||
|
override fun selectFolder(initialUri: Uri) {
|
||||||
|
openDirectory.launch(initialUri)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun selectFile() = backupFilesDialog.show()
|
override fun selectFile(initialUri: Uri) {
|
||||||
|
openDocument.launch(QkActivityResultContracts.OpenDocumentParams(
|
||||||
|
mimeTypes = listOf("application/json"),
|
||||||
|
initialUri = initialUri))
|
||||||
|
}
|
||||||
|
|
||||||
override fun confirmRestore() = confirmRestoreDialog.show()
|
}
|
||||||
|
|
||||||
override fun stopRestore() = stopRestoreDialog.show()
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
package com.moez.QKSMS.feature.backup
|
package com.moez.QKSMS.feature.backup
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import androidx.core.net.toUri
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import com.moez.QKSMS.common.Navigator
|
import com.moez.QKSMS.common.Navigator
|
||||||
import com.moez.QKSMS.common.base.QkPresenter
|
import com.moez.QKSMS.common.base.QkPresenter
|
||||||
@@ -26,14 +27,14 @@ import com.moez.QKSMS.common.util.DateFormatter
|
|||||||
import com.moez.QKSMS.common.util.extensions.makeToast
|
import com.moez.QKSMS.common.util.extensions.makeToast
|
||||||
import com.moez.QKSMS.interactor.PerformBackup
|
import com.moez.QKSMS.interactor.PerformBackup
|
||||||
import com.moez.QKSMS.manager.BillingManager
|
import com.moez.QKSMS.manager.BillingManager
|
||||||
import com.moez.QKSMS.manager.PermissionManager
|
|
||||||
import com.moez.QKSMS.repository.BackupRepository
|
import com.moez.QKSMS.repository.BackupRepository
|
||||||
import com.uber.autodispose.android.lifecycle.scope
|
import com.uber.autodispose.android.lifecycle.scope
|
||||||
import com.uber.autodispose.autoDisposable
|
import com.uber.autodispose.autoDisposable
|
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
import io.reactivex.rxkotlin.withLatestFrom
|
import io.reactivex.rxkotlin.withLatestFrom
|
||||||
import io.reactivex.subjects.BehaviorSubject
|
import io.reactivex.schedulers.Schedulers
|
||||||
import io.reactivex.subjects.Subject
|
import timber.log.Timber
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -43,12 +44,9 @@ class BackupPresenter @Inject constructor(
|
|||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val dateFormatter: DateFormatter,
|
private val dateFormatter: DateFormatter,
|
||||||
private val navigator: Navigator,
|
private val navigator: Navigator,
|
||||||
private val performBackup: PerformBackup,
|
private val performBackup: PerformBackup
|
||||||
private val permissionManager: PermissionManager
|
|
||||||
) : QkPresenter<BackupView, BackupState>(BackupState()) {
|
) : QkPresenter<BackupView, BackupState>(BackupState()) {
|
||||||
|
|
||||||
private val storagePermissionSubject: Subject<Boolean> = BehaviorSubject.createDefault(permissionManager.hasStorage())
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
disposables += backupRepo.getBackupProgress()
|
disposables += backupRepo.getBackupProgress()
|
||||||
.sample(16, TimeUnit.MILLISECONDS)
|
.sample(16, TimeUnit.MILLISECONDS)
|
||||||
@@ -60,20 +58,6 @@ class BackupPresenter @Inject constructor(
|
|||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
.subscribe { progress -> newState { copy(restoreProgress = progress) } }
|
.subscribe { progress -> newState { copy(restoreProgress = progress) } }
|
||||||
|
|
||||||
disposables += storagePermissionSubject
|
|
||||||
.distinctUntilChanged()
|
|
||||||
.switchMap { backupRepo.getBackups() }
|
|
||||||
.doOnNext { backups -> newState { copy(backups = backups) } }
|
|
||||||
.map { backups -> backups.maxOfOrNull { it.date } ?: 0L }
|
|
||||||
.map { lastBackup ->
|
|
||||||
when (lastBackup) {
|
|
||||||
0L -> context.getString(R.string.backup_never)
|
|
||||||
else -> dateFormatter.getDetailedTimestamp(lastBackup)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.startWith(context.getString(R.string.backup_loading))
|
|
||||||
.subscribe { lastBackup -> newState { copy(lastBackup = lastBackup) } }
|
|
||||||
|
|
||||||
disposables += billingManager.upgradeStatus
|
disposables += billingManager.upgradeStatus
|
||||||
.subscribe { upgraded -> newState { copy(upgraded = upgraded) } }
|
.subscribe { upgraded -> newState { copy(upgraded = upgraded) } }
|
||||||
}
|
}
|
||||||
@@ -81,10 +65,10 @@ class BackupPresenter @Inject constructor(
|
|||||||
override fun bindIntents(view: BackupView) {
|
override fun bindIntents(view: BackupView) {
|
||||||
super.bindIntents(view)
|
super.bindIntents(view)
|
||||||
|
|
||||||
view.activityVisible()
|
view.setBackupLocationClicks()
|
||||||
.map { permissionManager.hasStorage() }
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe(storagePermissionSubject)
|
.subscribe { view.selectFolder(backupRepo.getBackupPathUriForPicker()) }
|
||||||
|
|
||||||
view.restoreClicks()
|
view.restoreClicks()
|
||||||
.withLatestFrom(
|
.withLatestFrom(
|
||||||
@@ -96,40 +80,80 @@ class BackupPresenter @Inject constructor(
|
|||||||
!upgraded -> context.makeToast(R.string.backup_restore_error_plus)
|
!upgraded -> context.makeToast(R.string.backup_restore_error_plus)
|
||||||
backupProgress.running -> context.makeToast(R.string.backup_restore_error_backup)
|
backupProgress.running -> context.makeToast(R.string.backup_restore_error_backup)
|
||||||
restoreProgress.running -> context.makeToast(R.string.backup_restore_error_restore)
|
restoreProgress.running -> context.makeToast(R.string.backup_restore_error_restore)
|
||||||
!permissionManager.hasStorage() -> view.requestStoragePermission()
|
else -> view.selectFile(backupRepo.getBackupPathUriForPicker())
|
||||||
else -> view.selectFile()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe()
|
.subscribe()
|
||||||
|
|
||||||
view.restoreFileSelected()
|
view.backupClicks()
|
||||||
.autoDisposable(view.scope())
|
|
||||||
.subscribe { view.confirmRestore() }
|
|
||||||
|
|
||||||
view.restoreConfirmed()
|
|
||||||
.withLatestFrom(view.restoreFileSelected()) { _, backup -> backup }
|
|
||||||
.autoDisposable(view.scope())
|
|
||||||
.subscribe { backup -> RestoreBackupService.start(context, backup.path) }
|
|
||||||
|
|
||||||
view.stopRestoreClicks()
|
|
||||||
.autoDisposable(view.scope())
|
|
||||||
.subscribe { view.stopRestore() }
|
|
||||||
|
|
||||||
view.stopRestoreConfirmed()
|
|
||||||
.autoDisposable(view.scope())
|
|
||||||
.subscribe { backupRepo.stopRestore() }
|
|
||||||
|
|
||||||
view.fabClicks()
|
|
||||||
.withLatestFrom(billingManager.upgradeStatus) { _, upgraded -> upgraded }
|
.withLatestFrom(billingManager.upgradeStatus) { _, upgraded -> upgraded }
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { upgraded ->
|
.subscribe { upgraded ->
|
||||||
when {
|
when {
|
||||||
|
backupRepo.getBackupDocumentTree() == null -> {
|
||||||
|
newState { copy(showLocationRationale = true) }
|
||||||
|
}
|
||||||
!upgraded -> navigator.showQksmsPlusActivity("backup_fab")
|
!upgraded -> navigator.showQksmsPlusActivity("backup_fab")
|
||||||
!permissionManager.hasStorage() -> view.requestStoragePermission()
|
|
||||||
upgraded -> performBackup.execute(Unit)
|
upgraded -> performBackup.execute(Unit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
view.locationRationaleConfirmClicks()
|
||||||
|
.doOnNext { newState { copy(showLocationRationale = false) } }
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe { view.selectFolder(backupRepo.getBackupPathUriForPicker()) }
|
||||||
|
|
||||||
|
view.locationRationaleCancelClicks()
|
||||||
|
.doOnNext { newState { copy(showLocationRationale = false) } }
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe()
|
||||||
|
|
||||||
|
view.selectedBackupErrorClicks()
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe { newState { copy(showSelectedBackupError = false) } }
|
||||||
|
|
||||||
|
view.confirmRestoreBackupConfirmClicks()
|
||||||
|
.doOnNext { newState { copy(selectedBackupDetails = null) } }
|
||||||
|
.withLatestFrom(view.documentSelected()) { _, backup -> backup }
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe { backup -> RestoreBackupService.start(context, backup) }
|
||||||
|
|
||||||
|
view.confirmRestoreBackupCancelClicks()
|
||||||
|
.doOnNext { newState { copy(selectedBackupDetails = null) } }
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe()
|
||||||
|
|
||||||
|
view.stopRestoreClicks()
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe { newState { copy(showStopRestoreDialog = true) } }
|
||||||
|
|
||||||
|
view.stopRestoreConfirmed()
|
||||||
|
.doOnNext { newState { copy(showStopRestoreDialog = false) } }
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe { backupRepo.stopRestore() }
|
||||||
|
|
||||||
|
view.stopRestoreCancel()
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe { newState { copy(showStopRestoreDialog = false) } }
|
||||||
|
|
||||||
|
view.documentTreeSelected()
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe { uri -> backupRepo.persistBackupDirectory(uri) }
|
||||||
|
|
||||||
|
view.documentSelected()
|
||||||
|
.observeOn(Schedulers.io())
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe { uri ->
|
||||||
|
try {
|
||||||
|
val backupFile = backupRepo.parseBackup(uri)
|
||||||
|
val date = dateFormatter.getDetailedTimestamp(backupFile.date)
|
||||||
|
val details = context.getString(R.string.backup_details, date, backupFile.messages)
|
||||||
|
newState { copy(selectedBackupDetails = details) }
|
||||||
|
} catch (e: Exception) {
|
||||||
|
newState { copy(showSelectedBackupError = true) }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -18,13 +18,16 @@
|
|||||||
*/
|
*/
|
||||||
package com.moez.QKSMS.feature.backup
|
package com.moez.QKSMS.feature.backup
|
||||||
|
|
||||||
import com.moez.QKSMS.model.BackupFile
|
|
||||||
import com.moez.QKSMS.repository.BackupRepository
|
import com.moez.QKSMS.repository.BackupRepository
|
||||||
|
|
||||||
data class BackupState(
|
data class BackupState(
|
||||||
val backupProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(),
|
val backupProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(),
|
||||||
val restoreProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(),
|
val restoreProgress: BackupRepository.Progress = BackupRepository.Progress.Idle(),
|
||||||
val lastBackup: String = "",
|
|
||||||
val backups: List<BackupFile> = listOf(),
|
val showLocationRationale: Boolean = false,
|
||||||
|
val showSelectedBackupError: Boolean = false,
|
||||||
|
val selectedBackupDetails: String? = null,
|
||||||
|
val showStopRestoreDialog: Boolean = false,
|
||||||
|
|
||||||
val upgraded: Boolean = false
|
val upgraded: Boolean = false
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -18,23 +18,31 @@
|
|||||||
*/
|
*/
|
||||||
package com.moez.QKSMS.feature.backup
|
package com.moez.QKSMS.feature.backup
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
import com.moez.QKSMS.common.base.QkViewContract
|
import com.moez.QKSMS.common.base.QkViewContract
|
||||||
import com.moez.QKSMS.model.BackupFile
|
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
|
|
||||||
interface BackupView : QkViewContract<BackupState> {
|
interface BackupView : QkViewContract<BackupState> {
|
||||||
|
fun setBackupLocationClicks(): Observable<*>
|
||||||
fun activityVisible(): Observable<*>
|
|
||||||
fun restoreClicks(): Observable<*>
|
fun restoreClicks(): Observable<*>
|
||||||
fun restoreFileSelected(): Observable<BackupFile>
|
|
||||||
fun restoreConfirmed(): Observable<*>
|
fun backupClicks(): Observable<*>
|
||||||
|
|
||||||
|
fun locationRationaleConfirmClicks(): Observable<*>
|
||||||
|
fun locationRationaleCancelClicks(): Observable<*>
|
||||||
|
|
||||||
|
fun selectedBackupErrorClicks(): Observable<*>
|
||||||
|
|
||||||
|
fun confirmRestoreBackupConfirmClicks(): Observable<*>
|
||||||
|
fun confirmRestoreBackupCancelClicks(): Observable<*>
|
||||||
|
|
||||||
fun stopRestoreClicks(): Observable<*>
|
fun stopRestoreClicks(): Observable<*>
|
||||||
fun stopRestoreConfirmed(): Observable<*>
|
fun stopRestoreConfirmed(): Observable<*>
|
||||||
fun fabClicks(): Observable<*>
|
fun stopRestoreCancel(): Observable<*>
|
||||||
|
|
||||||
fun requestStoragePermission()
|
fun documentTreeSelected(): Observable<Uri>
|
||||||
fun selectFile()
|
fun documentSelected(): Observable<Uri>
|
||||||
fun confirmRestore()
|
|
||||||
fun stopRestore()
|
|
||||||
|
|
||||||
}
|
fun selectFolder(initialUri: Uri)
|
||||||
|
fun selectFile(initialUri: Uri)
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import android.annotation.SuppressLint
|
|||||||
import android.app.Service
|
import android.app.Service
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import androidx.core.app.NotificationManagerCompat
|
import androidx.core.app.NotificationManagerCompat
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
@@ -43,12 +44,12 @@ class RestoreBackupService : Service() {
|
|||||||
|
|
||||||
private const val ACTION_START = "com.moez.QKSMS.ACTION_START"
|
private const val ACTION_START = "com.moez.QKSMS.ACTION_START"
|
||||||
private const val ACTION_STOP = "com.moez.QKSMS.ACTION_STOP"
|
private const val ACTION_STOP = "com.moez.QKSMS.ACTION_STOP"
|
||||||
private const val EXTRA_FILE_PATH = "com.moez.QKSMS.EXTRA_FILE_PATH"
|
private const val EXTRA_FILE_URI = "com.moez.QKSMS.EXTRA_FILE_URI"
|
||||||
|
|
||||||
fun start(context: Context, filePath: String) {
|
fun start(context: Context, backupFile: Uri) {
|
||||||
val intent = Intent(context, RestoreBackupService::class.java)
|
val intent = Intent(context, RestoreBackupService::class.java)
|
||||||
.setAction(ACTION_START)
|
.setAction(ACTION_START)
|
||||||
.putExtra(EXTRA_FILE_PATH, filePath)
|
.putExtra(EXTRA_FILE_URI, backupFile.toString())
|
||||||
|
|
||||||
ContextCompat.startForegroundService(context, intent)
|
ContextCompat.startForegroundService(context, intent)
|
||||||
}
|
}
|
||||||
@@ -100,7 +101,7 @@ class RestoreBackupService : Service() {
|
|||||||
|
|
||||||
// Start the restore
|
// Start the restore
|
||||||
Observable.just(intent)
|
Observable.just(intent)
|
||||||
.map { it.getStringExtra(EXTRA_FILE_PATH) }
|
.map { Uri.parse(it.getStringExtra(EXTRA_FILE_URI)) }
|
||||||
.map(backupRepo::performRestore)
|
.map(backupRepo::performRestore)
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.subscribe({}, Timber::w)
|
.subscribe({}, Timber::w)
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<vector android:height="24dp" android:tint="#000000"
|
||||||
|
android:viewportHeight="24" android:viewportWidth="24"
|
||||||
|
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<path android:fillColor="@android:color/white" android:pathData="M10,4H4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2V8c0,-1.1 -0.9,-2 -2,-2h-8l-2,-2z"/>
|
||||||
|
</vector>
|
||||||
@@ -119,12 +119,12 @@
|
|||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
|
|
||||||
<com.moez.QKSMS.common.widget.PreferenceView
|
<com.moez.QKSMS.common.widget.PreferenceView
|
||||||
android:id="@+id/backup"
|
android:id="@+id/location"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
app:icon="@drawable/ic_history_black_24dp"
|
app:icon="@drawable/ic_baseline_folder_24"
|
||||||
app:title="@string/backup_last"
|
app:title="@string/backup_location_title"
|
||||||
tools:summary="@string/backup_never" />
|
app:summary="@string/backup_location_summary" />
|
||||||
|
|
||||||
<com.moez.QKSMS.common.widget.PreferenceView
|
<com.moez.QKSMS.common.widget.PreferenceView
|
||||||
android:id="@+id/restore"
|
android:id="@+id/restore"
|
||||||
|
|||||||
@@ -164,26 +164,27 @@
|
|||||||
<string name="backup_title">Backup and restore</string>
|
<string name="backup_title">Backup and restore</string>
|
||||||
<string name="backup_backing_up">Backing up messages</string>
|
<string name="backup_backing_up">Backing up messages</string>
|
||||||
<string name="backup_restoring">Restoring from backup</string>
|
<string name="backup_restoring">Restoring from backup</string>
|
||||||
<string name="backup_last">Last backup</string>
|
|
||||||
<string name="backup_loading">Loading…</string>
|
<string name="backup_loading">Loading…</string>
|
||||||
<string name="backup_never">Never</string>
|
<string name="backup_never">Never</string>
|
||||||
|
<string name="backup_location_title">Backup location</string>
|
||||||
|
<string name="backup_location_summary">Set folder</string>
|
||||||
<string name="backup_restore_title">Restore</string>
|
<string name="backup_restore_title">Restore</string>
|
||||||
<string name="backup_restore_summary">Select a backup</string>
|
<string name="backup_restore_summary">Select a backup</string>
|
||||||
<string name="backup_restore_error_plus">Please unlock QKSMS+ to use backup and restore</string>
|
<string name="backup_restore_error_plus">Please unlock QKSMS+ to use backup and restore</string>
|
||||||
<string name="backup_restore_error_backup">Backup in progress…</string>
|
<string name="backup_restore_error_backup">Backup in progress…</string>
|
||||||
<string name="backup_restore_error_restore">Restore in progress…</string>
|
<string name="backup_restore_error_restore">Restore in progress…</string>
|
||||||
<string name="backup_restore_confirm_title">Restore from backup</string>
|
<string name="backup_restore_confirm_title">Restore from backup</string>
|
||||||
<string name="backup_restore_confirm_message">Are you sure you would like to restore your messages from this backup?</string>
|
|
||||||
<string name="backup_restore_stop_title">Stop restore</string>
|
<string name="backup_restore_stop_title">Stop restore</string>
|
||||||
<string name="backup_restore_stop_message">Messages that have already been restored will remain on your device</string>
|
<string name="backup_restore_stop_message">Messages that have already been restored will remain on your device</string>
|
||||||
|
<string name="backup_select_location_rationale_title">Select backup location</string>
|
||||||
|
<string name="backup_select_location_rationale_message">Please select a folder to save your backups to</string>
|
||||||
|
<string name="backup_details">%1$s\n%2$d messages</string>
|
||||||
|
<string name="backup_selected_backup_error_title">Error</string>
|
||||||
|
<string name="backup_selected_backup_error_message">Couldn\'t read backup file</string>
|
||||||
|
<string name="backup_selected_backup_details_title">Backup details</string>
|
||||||
<string name="backup_restore_dialog_title">Backups</string>
|
<string name="backup_restore_dialog_title">Backups</string>
|
||||||
<string name="backup_restore_dialog_summary" translatable="false">/QKSMS/Backups</string>
|
<string name="backup_restore_dialog_summary" translatable="false">/QKSMS/Backups</string>
|
||||||
<string name="backup_restore_dialog_empty">No backups found</string>
|
<string name="backup_restore_dialog_empty">No backups found</string>
|
||||||
<plurals name="backup_message_count">
|
|
||||||
<item quantity="zero">No messages</item>
|
|
||||||
<item quantity="one">%d message</item>
|
|
||||||
<item quantity="other">%d messages</item>
|
|
||||||
</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_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_now">Backup now</string>
|
||||||
<string name="backup_progress_parsing">Parsing backup…</string>
|
<string name="backup_progress_parsing">Parsing backup…</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user