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;
|
|
|
|
|
2021-03-09 07:26:29 +01:00
|
|
|
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;
|
2021-05-21 06:55:06 +02:00
|
|
|
import org.session.libsignal.utilities.Log;
|
2021-02-01 02:10:48 +01:00
|
|
|
import org.session.libsignal.utilities.ThreadUtils;
|
2021-03-09 07:26:29 +01:00
|
|
|
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;
|
2021-05-21 06:55:06 +02:00
|
|
|
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;
|
2020-07-13 03:52:32 +02:00
|
|
|
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) {
|
2020-07-10 07:19:12 +02:00
|
|
|
performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context));
|
2020-06-26 08:17:53 +02:00
|
|
|
} else {
|
2020-07-10 07:19:12 +02:00
|
|
|
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;
|
2020-07-13 03:52:32 +02:00
|
|
|
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();
|
2021-05-21 06:55:06 +02:00
|
|
|
|
|
|
|
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) {
|
2020-07-10 07:19:12 +02:00
|
|
|
performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId));
|
2020-06-26 08:17:53 +02:00
|
|
|
} else {
|
2020-07-10 07:19:12 +02:00
|
|
|
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;
|
2020-07-13 03:52:32 +02:00
|
|
|
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) {
|
2020-07-10 07:19:12 +02:00
|
|
|
performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId, signal));
|
2020-06-26 08:17:53 +02:00
|
|
|
} else {
|
2020-07-10 07:19:12 +02:00
|
|
|
debouncer.publish(() -> performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, threadId, signal)));
|
2020-06-26 08:17:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-08-19 02:06:26 +02:00
|
|
|
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;
|
2020-07-13 03:52:32 +02:00
|
|
|
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) {
|
2020-07-10 07:19:12 +02:00
|
|
|
performOnBackgroundThreadIfNeeded(() -> wrapped.updateNotification(context, signal, reminderCount));
|
2020-06-26 08:17:53 +02:00
|
|
|
} else {
|
2020-07-10 07:19:12 +02:00
|
|
|
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); }
|
2020-07-10 07:19:12 +02:00
|
|
|
|
|
|
|
private void performOnBackgroundThreadIfNeeded(Runnable r) {
|
|
|
|
if (Looper.myLooper() == Looper.getMainLooper()) {
|
2021-01-31 23:39:14 +01:00
|
|
|
ThreadUtils.queue(r);
|
2020-07-10 07:19:12 +02:00
|
|
|
} else {
|
|
|
|
r.run();
|
|
|
|
}
|
|
|
|
}
|
2020-06-26 08:17:53 +02:00
|
|
|
}
|