@@ -8,8 +8,9 @@ package com.stevesoltys.seedvault.settings
8
8
import android.content.Context
9
9
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
10
10
import android.hardware.usb.UsbDevice
11
- import android.net.Uri
12
11
import androidx.annotation.UiThread
12
+ import androidx.core.content.edit
13
+ import androidx.core.net.toUri
13
14
import androidx.lifecycle.LiveData
14
15
import androidx.lifecycle.MutableLiveData
15
16
import androidx.preference.PreferenceManager
@@ -81,13 +82,13 @@ class SettingsManager(private val context: Context) {
81
82
var token: Long? = null
82
83
private set(newToken) {
83
84
if (newToken == null ) {
84
- prefs.edit()
85
- . remove(PREF_KEY_TOKEN )
86
- . apply ()
85
+ prefs.edit {
86
+ remove(PREF_KEY_TOKEN )
87
+ }
87
88
} else {
88
- prefs.edit()
89
- . putLong(PREF_KEY_TOKEN , newToken)
90
- . apply ()
89
+ prefs.edit {
90
+ putLong(PREF_KEY_TOKEN , newToken)
91
+ }
91
92
}
92
93
field = newToken
93
94
}
@@ -105,9 +106,9 @@ class SettingsManager(private val context: Context) {
105
106
// check if this is an existing user that needs to be migrated
106
107
// this check could be removed after a reasonable migration time (added 2024)
107
108
if (prefs.getString(PREF_KEY_STORAGE_URI , null ) != null ) {
108
- prefs.edit()
109
- . putString(PREF_KEY_STORAGE_PLUGIN , StoragePluginType .SAF .name)
110
- . apply ()
109
+ prefs.edit {
110
+ putString(PREF_KEY_STORAGE_PLUGIN , StoragePluginType .SAF .name)
111
+ }
111
112
StoragePluginType .SAF
112
113
} else null
113
114
} else savedType.let {
@@ -130,7 +131,7 @@ class SettingsManager(private val context: Context) {
130
131
fun onSuccessfulBackupCompleted (token : Long ) {
131
132
this .token = token
132
133
val now = System .currentTimeMillis()
133
- prefs.edit(). putLong(PREF_KEY_LAST_BACKUP , now). apply ()
134
+ prefs.edit { putLong(PREF_KEY_LAST_BACKUP , now) }
134
135
mLastBackupTime.postValue(now)
135
136
}
136
137
@@ -140,24 +141,30 @@ class SettingsManager(private val context: Context) {
140
141
BackendId .WEBDAV -> StoragePluginType .WEB_DAV
141
142
else -> error(" Unsupported plugin: ${plugin::class .java.simpleName} " )
142
143
}.name
143
- prefs.edit()
144
- .putString(PREF_KEY_STORAGE_PLUGIN , value)
145
- .apply ()
144
+ prefs.edit {
145
+ putString(PREF_KEY_STORAGE_PLUGIN , value)
146
+ }
147
+ }
148
+
149
+ fun clearStorageBackend () {
150
+ prefs.edit {
151
+ putString(PREF_KEY_STORAGE_PLUGIN , null )
152
+ }
146
153
}
147
154
148
155
fun setSafProperties (safProperties : SafProperties ) {
149
- prefs.edit()
150
- . putString(PREF_KEY_STORAGE_URI , safProperties.uri.toString())
151
- . putString(PREF_KEY_STORAGE_ROOT_ID , safProperties.rootId)
152
- . putString(PREF_KEY_STORAGE_NAME , safProperties.name)
153
- . putBoolean(PREF_KEY_STORAGE_IS_USB , safProperties.isUsb)
154
- . putBoolean(PREF_KEY_STORAGE_REQUIRES_NETWORK , safProperties.requiresNetwork)
155
- . apply ()
156
+ prefs.edit {
157
+ putString(PREF_KEY_STORAGE_URI , safProperties.uri.toString())
158
+ putString(PREF_KEY_STORAGE_ROOT_ID , safProperties.rootId)
159
+ putString(PREF_KEY_STORAGE_NAME , safProperties.name)
160
+ putBoolean(PREF_KEY_STORAGE_IS_USB , safProperties.isUsb)
161
+ putBoolean(PREF_KEY_STORAGE_REQUIRES_NETWORK , safProperties.requiresNetwork)
162
+ }
156
163
}
157
164
158
165
fun getSafProperties (): SafProperties ? {
159
166
val uriStr = prefs.getString(PREF_KEY_STORAGE_URI , null ) ? : return null
160
- val uri = Uri .parse(uriStr )
167
+ val uri = uriStr.toUri( )
161
168
val name = prefs.getString(PREF_KEY_STORAGE_NAME , null )
162
169
? : throw IllegalStateException (" no storage name" )
163
170
val isUsb = prefs.getBoolean(PREF_KEY_STORAGE_IS_USB , false )
@@ -168,19 +175,19 @@ class SettingsManager(private val context: Context) {
168
175
169
176
fun setFlashDrive (usb : FlashDrive ? ) {
170
177
if (usb == null ) {
171
- prefs.edit()
172
- . remove(PREF_KEY_FLASH_DRIVE_NAME )
173
- . remove(PREF_KEY_FLASH_DRIVE_SERIAL_NUMBER )
174
- . remove(PREF_KEY_FLASH_DRIVE_VENDOR_ID )
175
- . remove(PREF_KEY_FLASH_DRIVE_PRODUCT_ID )
176
- . apply ()
178
+ prefs.edit {
179
+ remove(PREF_KEY_FLASH_DRIVE_NAME )
180
+ remove(PREF_KEY_FLASH_DRIVE_SERIAL_NUMBER )
181
+ remove(PREF_KEY_FLASH_DRIVE_VENDOR_ID )
182
+ remove(PREF_KEY_FLASH_DRIVE_PRODUCT_ID )
183
+ }
177
184
} else {
178
- prefs.edit()
179
- . putString(PREF_KEY_FLASH_DRIVE_NAME , usb.name)
180
- . putString(PREF_KEY_FLASH_DRIVE_SERIAL_NUMBER , usb.serialNumber)
181
- . putInt(PREF_KEY_FLASH_DRIVE_VENDOR_ID , usb.vendorId)
182
- . putInt(PREF_KEY_FLASH_DRIVE_PRODUCT_ID , usb.productId)
183
- . apply ()
185
+ prefs.edit {
186
+ putString(PREF_KEY_FLASH_DRIVE_NAME , usb.name)
187
+ putString(PREF_KEY_FLASH_DRIVE_SERIAL_NUMBER , usb.serialNumber)
188
+ putInt(PREF_KEY_FLASH_DRIVE_VENDOR_ID , usb.vendorId)
189
+ putInt(PREF_KEY_FLASH_DRIVE_PRODUCT_ID , usb.productId)
190
+ }
184
191
}
185
192
}
186
193
@@ -203,11 +210,11 @@ class SettingsManager(private val context: Context) {
203
210
}
204
211
205
212
fun saveWebDavConfig (config : WebDavConfig ) {
206
- prefs.edit()
207
- . putString(PREF_KEY_WEBDAV_URL , config.url)
208
- . putString(PREF_KEY_WEBDAV_USER , config.username)
209
- . putString(PREF_KEY_WEBDAV_PASS , config.password)
210
- . apply ()
213
+ prefs.edit {
214
+ putString(PREF_KEY_WEBDAV_URL , config.url)
215
+ putString(PREF_KEY_WEBDAV_USER , config.username)
216
+ putString(PREF_KEY_WEBDAV_PASS , config.password)
217
+ }
211
218
}
212
219
213
220
fun backupApks (): Boolean {
@@ -231,7 +238,7 @@ class SettingsManager(private val context: Context) {
231
238
*/
232
239
fun disableBackup (packageName : String ) {
233
240
if (blacklistedApps.add(packageName)) {
234
- prefs.edit(). putStringSet(PREF_KEY_BACKUP_APP_BLACKLIST , blacklistedApps). apply ()
241
+ prefs.edit { putStringSet(PREF_KEY_BACKUP_APP_BLACKLIST , blacklistedApps) }
235
242
}
236
243
}
237
244
@@ -241,7 +248,7 @@ class SettingsManager(private val context: Context) {
241
248
fun onAppBackupStatusChanged (status : AppStatus ) {
242
249
if (status.enabled) blacklistedApps.remove(status.packageName)
243
250
else blacklistedApps.add(status.packageName)
244
- prefs.edit(). putStringSet(PREF_KEY_BACKUP_APP_BLACKLIST , blacklistedApps). apply ()
251
+ prefs.edit { putStringSet(PREF_KEY_BACKUP_APP_BLACKLIST , blacklistedApps) }
245
252
}
246
253
247
254
val quota: Long = 3L * 1024 * 1024 * 1024 // 3 GiB for now
0 commit comments