2019-07-03 19:44:37 +02:00
|
|
|
package com.stevesoltys.backup.settings
|
|
|
|
|
|
|
|
import android.app.Application
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION
|
|
|
|
import android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
2019-07-09 19:22:24 +02:00
|
|
|
import android.util.Log
|
2019-09-02 17:01:12 -03:00
|
|
|
import androidx.documentfile.provider.DocumentFile
|
2019-07-03 19:44:37 +02:00
|
|
|
import androidx.lifecycle.AndroidViewModel
|
2019-07-09 19:22:24 +02:00
|
|
|
import com.stevesoltys.backup.Backup
|
2019-07-08 16:02:53 +02:00
|
|
|
import com.stevesoltys.backup.LiveEvent
|
|
|
|
import com.stevesoltys.backup.MutableLiveEvent
|
2019-09-02 17:01:12 -03:00
|
|
|
import com.stevesoltys.backup.isOnExternalStorage
|
2019-07-09 19:22:24 +02:00
|
|
|
import com.stevesoltys.backup.transport.ConfigurableBackupTransportService
|
2019-08-01 12:00:06 +02:00
|
|
|
import com.stevesoltys.backup.transport.requestBackup
|
2019-07-09 09:52:03 +02:00
|
|
|
|
2019-07-09 19:22:24 +02:00
|
|
|
private val TAG = SettingsViewModel::class.java.simpleName
|
2019-07-03 19:44:37 +02:00
|
|
|
|
|
|
|
class SettingsViewModel(application: Application) : AndroidViewModel(application) {
|
|
|
|
|
|
|
|
private val app = application
|
|
|
|
|
2019-07-09 09:52:03 +02:00
|
|
|
private val locationWasSet = MutableLiveEvent<Boolean>()
|
2019-07-08 16:02:53 +02:00
|
|
|
/**
|
|
|
|
* Will be set to true if this is the initial location.
|
|
|
|
* It will be false if an existing location was changed.
|
|
|
|
*/
|
|
|
|
internal val onLocationSet: LiveEvent<Boolean> = locationWasSet
|
|
|
|
|
|
|
|
private val mChooseBackupLocation = MutableLiveEvent<Boolean>()
|
|
|
|
internal val chooseBackupLocation: LiveEvent<Boolean> = mChooseBackupLocation
|
|
|
|
internal fun chooseBackupLocation() = mChooseBackupLocation.setEvent(true)
|
|
|
|
|
2019-07-09 19:22:24 +02:00
|
|
|
fun recoveryCodeIsSet() = Backup.keyManager.hasBackupKey()
|
2019-09-02 17:01:12 -03:00
|
|
|
|
|
|
|
fun validLocationIsSet(): Boolean {
|
|
|
|
val uri = getBackupFolderUri(app) ?: return false
|
|
|
|
if (uri.isOnExternalStorage()) return true // might be a temporary failure
|
|
|
|
val file = DocumentFile.fromTreeUri(app, uri) ?: return false
|
|
|
|
return file.isDirectory
|
|
|
|
}
|
2019-07-03 19:44:37 +02:00
|
|
|
|
|
|
|
fun handleChooseFolderResult(result: Intent?) {
|
|
|
|
val folderUri = result?.data ?: return
|
|
|
|
|
|
|
|
// persist permission to access backup folder across reboots
|
|
|
|
val takeFlags = result.flags and (FLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION)
|
|
|
|
app.contentResolver.takePersistableUriPermission(folderUri, takeFlags)
|
|
|
|
|
2019-07-08 16:02:53 +02:00
|
|
|
// check if this is initial set-up or a later change
|
2019-09-02 17:01:12 -03:00
|
|
|
val initialSetUp = !validLocationIsSet()
|
2019-07-08 16:02:53 +02:00
|
|
|
|
2019-07-03 19:44:37 +02:00
|
|
|
// store backup folder location in settings
|
|
|
|
setBackupFolderUri(app, folderUri)
|
2019-07-08 16:02:53 +02:00
|
|
|
|
|
|
|
// notify the UI that the location has been set
|
2019-09-02 17:01:12 -03:00
|
|
|
locationWasSet.setEvent(initialSetUp)
|
2019-07-09 19:22:24 +02:00
|
|
|
|
|
|
|
// stop backup service to be sure the old location will get updated
|
|
|
|
app.stopService(Intent(app, ConfigurableBackupTransportService::class.java))
|
|
|
|
|
|
|
|
Log.d(TAG, "New storage location chosen: $folderUri")
|
2019-07-03 19:44:37 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 12:00:06 +02:00
|
|
|
fun backupNow() = Thread { requestBackup(app) }.start()
|
2019-07-09 09:52:03 +02:00
|
|
|
|
2019-07-03 19:44:37 +02:00
|
|
|
}
|