Make app blacklist accessible by multiple threads

Might fix #83
This commit is contained in:
Torsten Grote 2020-08-13 15:43:43 -03:00 committed by Chirayu Desai
parent 0b6742df44
commit 77ce3f6fe8
2 changed files with 76 additions and 64 deletions

View file

@ -6,6 +6,7 @@ import android.net.Uri
import androidx.annotation.UiThread
import androidx.documentfile.provider.DocumentFile
import androidx.preference.PreferenceManager
import java.util.concurrent.ConcurrentSkipListSet
import java.util.concurrent.atomic.AtomicBoolean
internal const val PREF_KEY_BACKUP_APK = "backup_apk"
@ -27,8 +28,13 @@ class SettingsManager(context: Context) {
private var isStorageChanging: AtomicBoolean = AtomicBoolean(false)
private val blacklistedApps: HashSet<String> by lazy {
prefs.getStringSet(PREF_KEY_BACKUP_APP_BLACKLIST, emptySet()).toHashSet()
/**
* This gets accessed by non-UI threads when saving with [PreferenceManager]
* and when [isBackupEnabled] is called during a backup run.
* Therefore, it is implemented with a thread-safe [ConcurrentSkipListSet].
*/
private val blacklistedApps: MutableSet<String> by lazy {
ConcurrentSkipListSet(prefs.getStringSet(PREF_KEY_BACKUP_APP_BLACKLIST, emptySet()))
}
// FIXME Storage is currently plugin specific and not generic
@ -44,7 +50,8 @@ class SettingsManager(context: Context) {
fun getStorage(): Storage? {
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("no storage name")
val name = prefs.getString(PREF_KEY_STORAGE_NAME, null)
?: throw IllegalStateException("no storage name")
val isUsb = prefs.getBoolean(PREF_KEY_STORAGE_IS_USB, false)
return Storage(uri, name, isUsb)
}
@ -98,7 +105,8 @@ class SettingsManager(context: Context) {
data class Storage(
val uri: Uri,
val name: String,
val isUsb: Boolean) {
val isUsb: Boolean
) {
fun getDocumentFile(context: Context) = DocumentFile.fromTreeUri(context, uri)
?: throw AssertionError("Should only happen on API < 21.")
}
@ -107,7 +115,8 @@ data class FlashDrive(
val name: String,
val serialNumber: String?,
val vendorId: Int,
val productId: Int) {
val productId: Int
) {
companion object {
fun from(device: UsbDevice) = FlashDrive(
name = "${device.manufacturerName} ${device.productName}",

View file

@ -46,7 +46,10 @@ class SettingsViewModel(
internal val lastBackupTime = metadataManager.lastBackupTime
private val mAppStatusList = switchMap(lastBackupTime) { getAppStatusResult() }
private val mAppStatusList = switchMap(lastBackupTime) {
// updates app list when lastBackupTime changes
getAppStatusResult()
}
internal val appStatusList: LiveData<AppStatusResult> = mAppStatusList
private val mAppEditMode = MutableLiveData<Boolean>()
@ -63,7 +66,7 @@ class SettingsViewModel(
Thread { requestBackup(app) }.start()
}
private fun getAppStatusResult(): LiveData<AppStatusResult> = liveData(Dispatchers.Main) {
private fun getAppStatusResult(): LiveData<AppStatusResult> = liveData {
val pm = app.packageManager
val locale = Locale.getDefault()
val list = pm.getInstalledPackages(0)