From f3241622d54a853f6e451badb62d1ca169c29036 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 8 Mar 2022 23:51:45 +0100 Subject: [PATCH] Avoid race condition when selecting a notification sound --- .../k9/ui/settings/account/AccountSettingsFragment.kt | 11 ++++++++++- .../settings/account/NotificationSoundPreference.kt | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsFragment.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsFragment.kt index 8aceb476e..fc57d2873 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsFragment.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/account/AccountSettingsFragment.kt @@ -101,7 +101,16 @@ class AccountSettingsFragment : PreferenceFragmentCompat(), ConfirmationDialogFr val account = getAccount() initializeCryptoSettings(account) - maybeUpdateNotificationPreferences(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) { diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/account/NotificationSoundPreference.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/account/NotificationSoundPreference.kt index 7546f862a..44050ee66 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/account/NotificationSoundPreference.kt +++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/settings/account/NotificationSoundPreference.kt @@ -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(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) if (callChangeListener(uri?.toString().orEmpty())) { + receivedActivityResultJustNow = true persistRingtone(uri) } }