2019-07-04 08:25:37 +02:00
|
|
|
package com.stevesoltys.backup.settings
|
|
|
|
|
|
|
|
import android.content.Context
|
2019-09-18 09:23:46 -03:00
|
|
|
import android.hardware.usb.UsbDevice
|
2019-07-04 08:25:37 +02:00
|
|
|
import android.net.Uri
|
2019-09-17 15:45:14 -03:00
|
|
|
import androidx.documentfile.provider.DocumentFile
|
2019-09-18 09:23:46 -03:00
|
|
|
import androidx.preference.PreferenceManager
|
2019-09-11 15:26:10 -03:00
|
|
|
import java.util.*
|
2019-07-04 08:25:37 +02:00
|
|
|
|
2019-09-13 14:37:32 -03:00
|
|
|
private const val PREF_KEY_STORAGE_URI = "storageUri"
|
|
|
|
private const val PREF_KEY_STORAGE_NAME = "storageName"
|
|
|
|
private const val PREF_KEY_STORAGE_EJECTABLE = "storageEjectable"
|
2019-09-18 09:23:46 -03:00
|
|
|
|
|
|
|
private const val PREF_KEY_FLASH_DRIVE_NAME = "flashDriveName"
|
|
|
|
private const val PREF_KEY_FLASH_DRIVE_SERIAL_NUMBER = "flashSerialNumber"
|
|
|
|
private const val PREF_KEY_FLASH_DRIVE_VENDOR_ID = "flashDriveVendorId"
|
|
|
|
private const val PREF_KEY_FLASH_DRIVE_PRODUCT_ID = "flashDriveProductId"
|
|
|
|
|
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"
|
|
|
|
|
2019-09-13 14:37:32 -03:00
|
|
|
data class Storage(
|
|
|
|
val uri: Uri,
|
|
|
|
val name: String,
|
2019-09-17 15:45:14 -03:00
|
|
|
val ejectable: Boolean) {
|
|
|
|
fun getDocumentFile(context: Context) = DocumentFile.fromTreeUri(context, uri)
|
|
|
|
?: throw AssertionError("Should only happen on API < 21.")
|
|
|
|
}
|
2019-09-13 14:37:32 -03:00
|
|
|
|
|
|
|
fun setStorage(context: Context, storage: Storage) {
|
2019-09-18 09:23:46 -03:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(context)
|
2019-07-04 08:25:37 +02:00
|
|
|
.edit()
|
2019-09-13 14:37:32 -03:00
|
|
|
.putString(PREF_KEY_STORAGE_URI, storage.uri.toString())
|
|
|
|
.putString(PREF_KEY_STORAGE_NAME, storage.name)
|
|
|
|
.putBoolean(PREF_KEY_STORAGE_EJECTABLE, storage.ejectable)
|
2019-07-04 08:25:37 +02:00
|
|
|
.apply()
|
|
|
|
}
|
|
|
|
|
2019-09-13 14:37:32 -03:00
|
|
|
fun getStorage(context: Context): Storage? {
|
2019-09-18 09:23:46 -03:00
|
|
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
2019-09-13 14:37:32 -03:00
|
|
|
val uriStr = prefs.getString(PREF_KEY_STORAGE_URI, null) ?: return null
|
|
|
|
val uri = Uri.parse(uriStr)
|
|
|
|
val name = prefs.getString(PREF_KEY_STORAGE_NAME, null) ?: throw IllegalStateException()
|
|
|
|
val ejectable = prefs.getBoolean(PREF_KEY_STORAGE_EJECTABLE, false)
|
|
|
|
return Storage(uri, name, ejectable)
|
2019-07-04 08:25:37 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 09:23:46 -03:00
|
|
|
data class FlashDrive(
|
|
|
|
val name: String,
|
|
|
|
val serialNumber: String?,
|
|
|
|
val vendorId: Int,
|
|
|
|
val productId: Int) {
|
|
|
|
companion object {
|
|
|
|
fun from(device: UsbDevice) = FlashDrive(
|
|
|
|
name = "${device.manufacturerName} ${device.productName}",
|
|
|
|
serialNumber = "", // device.serialNumber requires a permission since API 29
|
|
|
|
vendorId = device.vendorId,
|
|
|
|
productId = device.productId
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setFlashDrive(context: Context, usb: FlashDrive?) {
|
|
|
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
if (usb == null) {
|
|
|
|
prefs.edit()
|
|
|
|
.remove(PREF_KEY_FLASH_DRIVE_NAME)
|
|
|
|
.remove(PREF_KEY_FLASH_DRIVE_SERIAL_NUMBER)
|
|
|
|
.remove(PREF_KEY_FLASH_DRIVE_VENDOR_ID)
|
|
|
|
.remove(PREF_KEY_FLASH_DRIVE_PRODUCT_ID)
|
|
|
|
.apply()
|
|
|
|
} else {
|
|
|
|
prefs.edit()
|
|
|
|
.putString(PREF_KEY_FLASH_DRIVE_NAME, usb.name)
|
|
|
|
.putString(PREF_KEY_FLASH_DRIVE_SERIAL_NUMBER, usb.serialNumber)
|
|
|
|
.putInt(PREF_KEY_FLASH_DRIVE_VENDOR_ID, usb.vendorId)
|
|
|
|
.putInt(PREF_KEY_FLASH_DRIVE_PRODUCT_ID, usb.productId)
|
|
|
|
.apply()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getFlashDrive(context: Context): FlashDrive? {
|
|
|
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
val name = prefs.getString(PREF_KEY_FLASH_DRIVE_NAME, null) ?: return null
|
|
|
|
val serialNumber = prefs.getString(PREF_KEY_FLASH_DRIVE_SERIAL_NUMBER, null)
|
|
|
|
val vendorId = prefs.getInt(PREF_KEY_FLASH_DRIVE_VENDOR_ID, -1)
|
|
|
|
val productId = prefs.getInt(PREF_KEY_FLASH_DRIVE_PRODUCT_ID, -1)
|
|
|
|
return FlashDrive(name, serialNumber, vendorId, productId)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 {
|
2019-09-18 09:23:46 -03:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(context)
|
2019-09-11 15:26:10 -03:00
|
|
|
.edit()
|
|
|
|
.putLong(PREF_KEY_BACKUP_TOKEN, this)
|
|
|
|
.apply()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current backup token or 0 if none exists.
|
|
|
|
*/
|
|
|
|
fun getBackupToken(context: Context): Long {
|
2019-09-18 09:23:46 -03:00
|
|
|
return PreferenceManager.getDefaultSharedPreferences(context).getLong(PREF_KEY_BACKUP_TOKEN, 0L)
|
2019-09-11 15:26:10 -03:00
|
|
|
}
|
|
|
|
|
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? {
|
2019-09-18 09:23:46 -03:00
|
|
|
return PreferenceManager.getDefaultSharedPreferences(context).getString(PREF_KEY_BACKUP_PASSWORD, null)
|
2019-07-04 08:25:37 +02:00
|
|
|
}
|