diff --git a/src/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java index 46e718359a..eada9a49e8 100644 --- a/src/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java @@ -1,6 +1,7 @@ package org.thoughtcrime.securesms.notifications; import android.content.Context; +import android.os.Looper; import androidx.annotation.MainThread; import androidx.annotation.NonNull; @@ -47,9 +48,9 @@ public class OptimizedMessageNotifier implements MessageNotifier { } if (isCaughtUp) { - wrapped.updateNotification(context); + performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context)); } else { - debouncer.publish(() -> wrapped.updateNotification(context)); + debouncer.publish(() -> performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context))); } } @@ -63,9 +64,9 @@ public class OptimizedMessageNotifier implements MessageNotifier { } if (isCaughtUp) { - wrapped.updateNotification(context, threadId); + performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId)); } else { - debouncer.publish(() -> wrapped.updateNotification(context, threadId)); + debouncer.publish(() -> performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId))); } } @@ -79,9 +80,9 @@ public class OptimizedMessageNotifier implements MessageNotifier { } if (isCaughtUp) { - wrapped.updateNotification(context, threadId, signal); + performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId, signal)); } else { - debouncer.publish(() -> wrapped.updateNotification(context, threadId, signal)); + debouncer.publish(() -> performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId, signal))); } } @@ -95,12 +96,20 @@ public class OptimizedMessageNotifier implements MessageNotifier { } if (isCaughtUp) { - wrapped.updateNotification(context, signal, reminderCount); + performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, signal, reminderCount)); } else { - debouncer.publish(() -> wrapped.updateNotification(context, signal, reminderCount)); + debouncer.publish(() -> performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, signal, reminderCount))); } } @Override public void clearReminder(@NonNull Context context) { wrapped.clearReminder(context); } + + private void performOnBackgroundThreadIfNeeded(Runnable r) { + if (Looper.myLooper() == Looper.getMainLooper()) { + new Thread(r).start(); + } else { + r.run(); + } + } }