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-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-07-09 09:52:03 +02:00
|
|
|
import com.stevesoltys.backup.service.backup.requestFullBackup
|
2019-07-09 19:22:24 +02:00
|
|
|
import com.stevesoltys.backup.transport.ConfigurableBackupTransportService
|
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-07-03 19:44:37 +02:00
|
|
|
fun locationIsSet() = getBackupFolderUri(getApplication()) != null
|
|
|
|
|
|
|
|
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
|
|
|
|
val wasEmptyBefore = getBackupFolderUri(app) == null
|
|
|
|
|
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-07-09 09:52:03 +02:00
|
|
|
locationWasSet.setEvent(wasEmptyBefore)
|
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-07-09 09:52:03 +02:00
|
|
|
fun backupNow() = Thread { requestFullBackup(app) }.start()
|
|
|
|
|
2019-07-03 19:44:37 +02:00
|
|
|
}
|