diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/NotificationsSettingsViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/NotificationsSettingsViewModel.kt index ee34d9a764..eab696ef93 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/NotificationsSettingsViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/NotificationsSettingsViewModel.kt @@ -16,19 +16,21 @@ import org.thoughtcrime.securesms.util.livedata.Store class NotificationsSettingsViewModel(private val sharedPreferences: SharedPreferences) : ViewModel() { + private val store = Store(getState()) + + val state: LiveData = store.stateLiveData + init { if (NotificationChannels.supported()) { SignalStore.settings().messageNotificationSound = NotificationChannels.getInstance().messageRingtone SignalStore.settings().isMessageVibrateEnabled = NotificationChannels.getInstance().messageVibrate } + + store.update { getState(calculateSlowNotifications = true) } } - private val store = Store(getState()) - - val state: LiveData = store.stateLiveData - fun refresh() { - store.update { getState() } + store.update { getState(currentState = it) } } fun setMessageNotificationsEnabled(enabled: Boolean) { @@ -100,7 +102,12 @@ class NotificationsSettingsViewModel(private val sharedPreferences: SharedPrefer refresh() } - private fun getState(): NotificationsSettingsState = NotificationsSettingsState( + /** + * @param currentState If provided and [calculateSlowNotifications] = false, then we will copy the slow notification state from it + * @param calculateSlowNotifications If true, calculate the true slow notification state (this is not main-thread safe). Otherwise, it will copy from + * [currentState] or default to false. + */ + private fun getState(currentState: NotificationsSettingsState? = null, calculateSlowNotifications: Boolean = false): NotificationsSettingsState = NotificationsSettingsState( messageNotificationsState = MessageNotificationsState( notificationsEnabled = SignalStore.settings().isMessageNotificationsEnabled && canEnableNotifications(), canEnableNotifications = canEnableNotifications(), @@ -112,7 +119,13 @@ class NotificationsSettingsViewModel(private val sharedPreferences: SharedPrefer repeatAlerts = SignalStore.settings().messageNotificationsRepeatAlerts, messagePrivacy = SignalStore.settings().messageNotificationsPrivacy.toString(), priority = TextSecurePreferences.getNotificationPriority(ApplicationDependencies.getApplication()), - troubleshootNotifications = SlowNotificationHeuristics.isPotentiallyCausedByBatteryOptimizations() && SlowNotificationHeuristics.isHavingDelayedNotifications() + troubleshootNotifications = if (calculateSlowNotifications) { + SlowNotificationHeuristics.isPotentiallyCausedByBatteryOptimizations() && SlowNotificationHeuristics.isHavingDelayedNotifications() + } else if (currentState != null) { + currentState.messageNotificationsState.troubleshootNotifications + } else { + false + } ), callNotificationsState = CallNotificationsState( notificationsEnabled = SignalStore.settings().isCallNotificationsEnabled && canEnableNotifications(), diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/SlowNotificationHeuristics.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/SlowNotificationHeuristics.kt index 6e92f516b3..034746643c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/SlowNotificationHeuristics.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/SlowNotificationHeuristics.kt @@ -7,6 +7,7 @@ package org.thoughtcrime.securesms.notifications import android.os.Build import android.text.TextUtils +import androidx.annotation.WorkerThread import org.signal.core.util.logging.Log import org.thoughtcrime.securesms.database.LocalMetricsDatabase import org.thoughtcrime.securesms.dependencies.ApplicationDependencies @@ -85,6 +86,7 @@ object SlowNotificationHeuristics { return true } + @WorkerThread @JvmStatic fun isHavingDelayedNotifications(): Boolean { if (!SignalStore.settings().isMessageNotificationsEnabled || diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/SlowNotificationsViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/SlowNotificationsViewModel.kt index 37f931928a..4433e38df1 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/SlowNotificationsViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/SlowNotificationsViewModel.kt @@ -6,6 +6,7 @@ package org.thoughtcrime.securesms.notifications import android.os.Build +import androidx.annotation.WorkerThread import androidx.lifecycle.ViewModel import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.core.Observable @@ -43,6 +44,7 @@ class SlowNotificationsViewModel : ViewModel() { checkSubject.onNext(Unit) } + @WorkerThread private fun checkHeuristics(): Single { return Single.fromCallable { var state = State.NONE