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
|
2019-09-11 15:26:10 -03:00
|
|
|
import java.util.*
|
2019-07-04 08:25:37 +02:00
|
|
|
|
|
|
|
private const val PREF_KEY_BACKUP_URI = "backupUri"
|
2019-09-11 15:26:10 -03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-09-11 15:26:10 -03:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
}
|
|
|
|
|
2019-07-08 12:32:47 +02:00
|
|
|
@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)
|
|
|
|
}
|