Skip to content

Commit

Permalink
Throttle notifications when doing the intial message fetch.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed May 29, 2020
1 parent 51c8270 commit 3a06412
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Application;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;

import org.thoughtcrime.securesms.BuildConfig;
Expand Down Expand Up @@ -61,13 +62,15 @@ public class ApplicationDependencies {
private static InitialMessageRetriever initialMessageRetriever;
private static MessageNotifier messageNotifier;

@MainThread
public static synchronized void init(@NonNull Application application, @NonNull Provider provider) {
if (ApplicationDependencies.application != null || ApplicationDependencies.provider != null) {
throw new IllegalStateException("Already initialized!");
}

ApplicationDependencies.application = application;
ApplicationDependencies.provider = provider;
ApplicationDependencies.application = application;
ApplicationDependencies.provider = provider;
ApplicationDependencies.messageNotifier = provider.provideMessageNotifier();
}

public static @NonNull Application getApplication() {
Expand Down Expand Up @@ -251,11 +254,6 @@ public static synchronized void resetSignalServiceMessageReceiver() {

public static synchronized @NonNull MessageNotifier getMessageNotifier() {
assertInitialization();

if (messageNotifier == null) {
messageNotifier = provider.provideMessageNotifier();
}

return messageNotifier;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.thoughtcrime.securesms.messages.InitialMessageRetriever;
import org.thoughtcrime.securesms.notifications.DefaultMessageNotifier;
import org.thoughtcrime.securesms.notifications.MessageNotifier;
import org.thoughtcrime.securesms.notifications.OptimizedMessageNotifier;
import org.thoughtcrime.securesms.push.SecurityEventListener;
import org.thoughtcrime.securesms.push.SignalServiceNetworkAccess;
import org.thoughtcrime.securesms.recipients.LiveRecipientCache;
Expand Down Expand Up @@ -162,7 +163,7 @@ public ApplicationDependencyProvider(@NonNull Application context, @NonNull Sign

@Override
public @NonNull MessageNotifier provideMessageNotifier() {
return new DefaultMessageNotifier();
return new OptimizedMessageNotifier(new DefaultMessageNotifier());
}

private static class DynamicCredentialsProvider implements CredentialsProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.notifications.MessageNotifier;
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;

import java.util.List;
Expand Down Expand Up @@ -102,6 +103,8 @@ public void addListener(@NonNull Listener listener) {
listeners.clear();
}

ApplicationDependencies.getMessageNotifier().updateNotification(ApplicationDependencies.getApplication());

if (success) {
Log.i(TAG, "Successfully caught up in " + (System.currentTimeMillis() - startTime) + " ms.");
return Result.SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.thoughtcrime.securesms.notifications;

import android.content.Context;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;

import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.messages.InitialMessageRetriever;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.Throttler;

import java.util.concurrent.TimeUnit;

/**
* Wraps another {@link MessageNotifier} and throttles it while {@link InitialMessageRetriever} is
* running.
*/
public class OptimizedMessageNotifier implements MessageNotifier {

private final MessageNotifier wrapped;
private final Throttler throttler;
private final InitialMessageRetriever retriever;

@MainThread
public OptimizedMessageNotifier(@NonNull MessageNotifier wrapped) {
this.wrapped = wrapped;
this.throttler = new Throttler(TimeUnit.SECONDS.toMillis(5));
this.retriever = ApplicationDependencies.getInitialMessageRetriever();
}

@Override
public void setVisibleThread(long threadId) {
wrapped.setVisibleThread(threadId);
}

@Override
public void clearVisibleThread() {
wrapped.clearVisibleThread();
}

@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) {
if (retriever.isCaughtUp()) {
wrapped.updateNotification(context);
} else {
throttler.publish(() -> wrapped.updateNotification(context));
}
}

@Override
public void updateNotification(@NonNull Context context, long threadId) {
if (retriever.isCaughtUp()) {
wrapped.updateNotification(context, threadId);
} else {
throttler.publish(() -> wrapped.updateNotification(context));
}
}

@Override
public void updateNotification(@NonNull Context context, long threadId, boolean signal) {
if (retriever.isCaughtUp()) {
wrapped.updateNotification(context, threadId, signal);
} else {
throttler.publish(() -> wrapped.updateNotification(context));
}
}

@Override
public void updateNotification(@NonNull Context context, long threadId, boolean signal, int reminderCount) {
if (retriever.isCaughtUp()) {
wrapped.updateNotification(context, threadId, signal, reminderCount);
} else {
throttler.publish(() -> wrapped.updateNotification(context));
}
}

@Override
public void clearReminder(@NonNull Context context) {
wrapped.clearReminder(context);
}
}

0 comments on commit 3a06412

Please sign in to comment.