Avoid race condition when selecting a notification sound

This commit is contained in:
cketti 2022-03-08 23:51:45 +01:00
parent aa90f4369d
commit f3241622d5
2 changed files with 12 additions and 1 deletions

View file

@ -101,8 +101,17 @@ class AccountSettingsFragment : PreferenceFragmentCompat(), ConfirmationDialogFr
val account = getAccount()
initializeCryptoSettings(account)
// Don't update the notification preferences when resuming after the user has selected a new notification sound
// via NotificationSoundPreference. Otherwise we race the background thread and might read data from the old
// NotificationChannel, overwriting the notification sound with the previous value.
notificationSoundPreference?.let { notificationSoundPreference ->
if (notificationSoundPreference.receivedActivityResultJustNow) {
notificationSoundPreference.receivedActivityResultJustNow = false
} else {
maybeUpdateNotificationPreferences(account)
}
}
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)

View file

@ -27,6 +27,7 @@ constructor(
),
defStyleRes: Int = 0
) : Preference(context, attrs, defStyleAttr, defStyleRes), PreferenceActivityResultListener {
var receivedActivityResultJustNow = false
fun setNotificationSound(sound: Uri?) {
persistRingtone(sound)
@ -42,6 +43,7 @@ constructor(
val uri = data?.getParcelableExtra<Uri>(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)
if (callChangeListener(uri?.toString().orEmpty())) {
receivedActivityResultJustNow = true
persistRingtone(uri)
}
}