session-android/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java

125 lines
4.3 KiB
Java
Raw Normal View History

2020-06-26 08:17:53 +02:00
package org.thoughtcrime.securesms.notifications;
import android.content.Context;
2020-07-10 03:43:45 +02:00
import android.os.Looper;
2020-06-26 08:17:53 +02:00
2020-07-08 03:30:00 +02:00
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import org.session.libsession.messaging.sending_receiving.notifications.MessageNotifier;
import org.session.libsession.messaging.sending_receiving.pollers.Poller;
2021-05-18 08:03:47 +02:00
import org.session.libsession.utilities.recipients.Recipient;
2021-02-01 05:01:06 +01:00
import org.session.libsession.utilities.Debouncer;
import org.session.libsignal.utilities.Log;
2021-02-01 02:10:48 +01:00
import org.session.libsignal.utilities.ThreadUtils;
import org.thoughtcrime.securesms.ApplicationContext;
2021-05-19 09:01:53 +02:00
import org.thoughtcrime.securesms.loki.api.OpenGroupManager;
2021-01-13 07:11:30 +01:00
2020-06-26 08:17:53 +02:00
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(2));
2020-06-26 08:17:53 +02:00
}
@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) {
2021-05-19 03:12:29 +02:00
Poller poller = ApplicationContext.getInstance(context).poller;
boolean isCaughtUp = true;
2021-05-19 03:12:29 +02:00
if (poller != null) {
isCaughtUp = isCaughtUp && poller.isCaughtUp();
2020-07-10 07:36:23 +02:00
}
2021-05-19 09:01:53 +02:00
isCaughtUp = isCaughtUp && OpenGroupManager.INSTANCE.isAllCaughtUp();
2020-06-26 08:17:53 +02:00
2020-07-08 03:30:00 +02:00
if (isCaughtUp) {
performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context));
2020-06-26 08:17:53 +02:00
} else {
debouncer.publish(() -> performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context)));
2020-06-26 08:17:53 +02:00
}
}
@Override
public void updateNotification(@NonNull Context context, long threadId) {
2020-07-16 00:55:29 +02:00
Poller lokiPoller = ApplicationContext.getInstance(context).poller;
boolean isCaughtUp = true;
2020-07-10 07:36:23 +02:00
if (lokiPoller != null) {
isCaughtUp = isCaughtUp && lokiPoller.isCaughtUp();
2020-06-26 08:17:53 +02:00
}
2021-05-19 09:01:53 +02:00
isCaughtUp = isCaughtUp && OpenGroupManager.INSTANCE.isAllCaughtUp();
Log.d("Ryan", "Is caught up? " + isCaughtUp);
2020-07-10 07:36:23 +02:00
2020-07-08 03:30:00 +02:00
if (isCaughtUp) {
performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId));
2020-06-26 08:17:53 +02:00
} else {
debouncer.publish(() -> performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId)));
2020-06-26 08:17:53 +02:00
}
}
@Override
public void updateNotification(@NonNull Context context, long threadId, boolean signal) {
2020-07-16 00:55:29 +02:00
Poller lokiPoller = ApplicationContext.getInstance(context).poller;
boolean isCaughtUp = true;
2020-07-10 07:36:23 +02:00
if (lokiPoller != null) {
isCaughtUp = isCaughtUp && lokiPoller.isCaughtUp();
}
2021-05-19 09:01:53 +02:00
isCaughtUp = isCaughtUp && OpenGroupManager.INSTANCE.isAllCaughtUp();
2020-06-26 08:17:53 +02:00
2020-07-08 03:30:00 +02:00
if (isCaughtUp) {
performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId, signal));
2020-06-26 08:17:53 +02:00
} else {
debouncer.publish(() -> performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId, signal)));
2020-06-26 08:17:53 +02:00
}
}
@Override
public void updateNotification(@androidx.annotation.NonNull Context context, boolean signal, int reminderCount) {
2020-07-16 00:55:29 +02:00
Poller lokiPoller = ApplicationContext.getInstance(context).poller;
boolean isCaughtUp = true;
2020-07-10 07:36:23 +02:00
if (lokiPoller != null) {
isCaughtUp = isCaughtUp && lokiPoller.isCaughtUp();
}
2021-05-19 09:01:53 +02:00
isCaughtUp = isCaughtUp && OpenGroupManager.INSTANCE.isAllCaughtUp();
2020-06-26 08:17:53 +02:00
2020-07-08 03:30:00 +02:00
if (isCaughtUp) {
performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, signal, reminderCount));
2020-06-26 08:17:53 +02:00
} else {
debouncer.publish(() -> performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, signal, reminderCount)));
2020-06-26 08:17:53 +02:00
}
}
@Override
public void clearReminder(@NonNull Context context) { wrapped.clearReminder(context); }
private void performOnBackgroundThreadIfNeeded(Runnable r) {
if (Looper.myLooper() == Looper.getMainLooper()) {
2021-01-31 23:39:14 +01:00
ThreadUtils.queue(r);
} else {
r.run();
}
}
2020-06-26 08:17:53 +02:00
}