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
|
|
|
|
import androidx.lifecycle.AndroidViewModel
|
2019-07-08 16:02:53 +02:00
|
|
|
import com.stevesoltys.backup.LiveEvent
|
|
|
|
import com.stevesoltys.backup.MutableLiveEvent
|
2019-07-08 12:32:47 +02:00
|
|
|
import com.stevesoltys.backup.security.KeyManager
|
2019-07-03 19:44:37 +02:00
|
|
|
|
|
|
|
class SettingsViewModel(application: Application) : AndroidViewModel(application) {
|
|
|
|
|
|
|
|
private val app = application
|
|
|
|
|
2019-07-08 16:02:53 +02:00
|
|
|
private val mLocationWasSet = MutableLiveEvent<Boolean>()
|
|
|
|
/**
|
|
|
|
* 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-08 12:32:47 +02:00
|
|
|
fun recoveryCodeIsSet() = 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
|
|
|
|
mLocationWasSet.setEvent(wasEmptyBefore)
|
2019-07-03 19:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|