session-android/src/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java
2020-07-08 17:05:26 +10:00

107 lines
4.0 KiB
Java

package org.thoughtcrime.securesms.notifications;
import android.content.Context;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.loki.api.LokiPublicChatManager;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.Debouncer;
import org.whispersystems.signalservice.loki.api.Poller;
import java.util.concurrent.TimeUnit;
public class OptimizedMessageNotifier implements MessageNotifier {
private final MessageNotifier wrapped;
private final Debouncer debouncer;
@MainThread
public OptimizedMessageNotifier(@NonNull MessageNotifier wrapped) {
this.wrapped = wrapped;
this.debouncer = new Debouncer(TimeUnit.SECONDS.toMillis(1));
}
@Override
public void setVisibleThread(long threadId) { wrapped.setVisibleThread(threadId); }
@Override
public void setLastDesktopActivityTimestamp(long timestamp) { wrapped.setLastDesktopActivityTimestamp(timestamp);}
@Override
public void notifyMessageDeliveryFailed(Context context, Recipient recipient, long threadId) {
wrapped.notifyMessageDeliveryFailed(context, recipient, threadId);
}
@Override
public void cancelDelayedNotifications() { wrapped.cancelDelayedNotifications(); }
@Override
public void updateNotification(@NonNull Context context) {
Poller lokiPoller = ApplicationContext.getInstance(context).lokiPoller;
LokiPublicChatManager lokiPublicChatManager = ApplicationContext.getInstance(context).lokiPublicChatManager;
Boolean isCaughtUp = false;
if (lokiPoller != null && lokiPublicChatManager != null) {
isCaughtUp = lokiPoller.isCaughtUp() && lokiPublicChatManager.areAllCaughtUp();
}
if (isCaughtUp) {
wrapped.updateNotification(context);
} else {
debouncer.publish(() -> wrapped.updateNotification(context));
}
}
@Override
public void updateNotification(@NonNull Context context, long threadId) {
Poller lokiPoller = ApplicationContext.getInstance(context).lokiPoller;
LokiPublicChatManager lokiPublicChatManager = ApplicationContext.getInstance(context).lokiPublicChatManager;
Boolean isCaughtUp = false;
if (lokiPoller != null && lokiPublicChatManager != null) {
isCaughtUp = lokiPoller.isCaughtUp() && lokiPublicChatManager.areAllCaughtUp();
}
if (isCaughtUp) {
wrapped.updateNotification(context, threadId);
} else {
debouncer.publish(() -> wrapped.updateNotification(context, threadId));
}
}
@Override
public void updateNotification(@NonNull Context context, long threadId, boolean signal) {
Poller lokiPoller = ApplicationContext.getInstance(context).lokiPoller;
LokiPublicChatManager lokiPublicChatManager = ApplicationContext.getInstance(context).lokiPublicChatManager;
Boolean isCaughtUp = false;
if (lokiPoller != null && lokiPublicChatManager != null) {
isCaughtUp = lokiPoller.isCaughtUp() && lokiPublicChatManager.areAllCaughtUp();
}
if (isCaughtUp) {
wrapped.updateNotification(context, threadId, signal);
} else {
debouncer.publish(() -> wrapped.updateNotification(context, threadId, signal));
}
}
@Override
public void updateNotification(@android.support.annotation.NonNull Context context, boolean signal, int reminderCount) {
Poller lokiPoller = ApplicationContext.getInstance(context).lokiPoller;
LokiPublicChatManager lokiPublicChatManager = ApplicationContext.getInstance(context).lokiPublicChatManager;
Boolean isCaughtUp = false;
if (lokiPoller != null && lokiPublicChatManager != null) {
isCaughtUp = lokiPoller.isCaughtUp() && lokiPublicChatManager.areAllCaughtUp();
}
if (isCaughtUp) {
wrapped.updateNotification(context, signal, reminderCount);
} else {
debouncer.publish(() -> wrapped.updateNotification(context, signal, reminderCount));
}
}
@Override
public void clearReminder(@NonNull Context context) { wrapped.clearReminder(context); }
}