seedvault/app/src/main/java/com/stevesoltys/backup/settings/SettingsManager.kt

47 lines
1.5 KiB
Kotlin
Raw Normal View History

2019-07-04 08:25:37 +02:00
package com.stevesoltys.backup.settings
import android.content.Context
import android.net.Uri
import android.preference.PreferenceManager.getDefaultSharedPreferences
import java.util.*
2019-07-04 08:25:37 +02:00
private const val PREF_KEY_BACKUP_URI = "backupUri"
private const val PREF_KEY_BACKUP_TOKEN = "backupToken"
2019-07-04 08:25:37 +02:00
private const val PREF_KEY_BACKUP_PASSWORD = "backupLegacyPassword"
fun setBackupFolderUri(context: Context, uri: Uri) {
getDefaultSharedPreferences(context)
.edit()
.putString(PREF_KEY_BACKUP_URI, uri.toString())
.apply()
}
fun getBackupFolderUri(context: Context): Uri? {
val uriStr = getDefaultSharedPreferences(context).getString(PREF_KEY_BACKUP_URI, null)
?: return null
return Uri.parse(uriStr)
}
/**
* Generates and returns a new backup token while saving it as well.
* Subsequent calls to [getBackupToken] will return this new token once saved.
*/
fun getAndSaveNewBackupToken(context: Context): Long = Date().time.apply {
getDefaultSharedPreferences(context)
.edit()
.putLong(PREF_KEY_BACKUP_TOKEN, this)
.apply()
}
/**
* Returns the current backup token or 0 if none exists.
*/
fun getBackupToken(context: Context): Long {
return getDefaultSharedPreferences(context).getLong(PREF_KEY_BACKUP_TOKEN, 0L)
}
@Deprecated("Replaced by KeyManager#getBackupKey()")
2019-07-04 08:25:37 +02:00
fun getBackupPassword(context: Context): String? {
return getDefaultSharedPreferences(context).getString(PREF_KEY_BACKUP_PASSWORD, null)
}