Reduce notification update interval.

This commit is contained in:
Greyson Parrelli
2020-06-25 07:18:26 -07:00
parent 87a59b6a9b
commit 75c8c59d78
3 changed files with 127 additions and 33 deletions

View File

@@ -1,32 +1,27 @@
package org.thoughtcrime.securesms.notifications;
import android.content.Context;
import android.os.Handler;
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;
import org.thoughtcrime.securesms.util.LeakyBucketLimiter;
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
/**
* Wraps another {@link MessageNotifier} and throttles it while {@link InitialMessageRetriever} is
* running.
* Uses a leaky-bucket strategy to limiting notification updates.
*/
public class OptimizedMessageNotifier implements MessageNotifier {
private final MessageNotifier wrapped;
private final Throttler throttler;
private final InitialMessageRetriever retriever;
private final MessageNotifier wrapped;
private final LeakyBucketLimiter limiter;
@MainThread
public OptimizedMessageNotifier(@NonNull MessageNotifier wrapped) {
this.wrapped = wrapped;
this.throttler = new Throttler(TimeUnit.SECONDS.toMillis(5));
this.retriever = ApplicationDependencies.getInitialMessageRetriever();
this.wrapped = wrapped;
this.limiter = new LeakyBucketLimiter(5, 1000, new Handler(SignalExecutors.getAndStartHandlerThread("signal-notifier").getLooper()));
}
@Override
@@ -56,38 +51,22 @@ public class OptimizedMessageNotifier implements MessageNotifier {
@Override
public void updateNotification(@NonNull Context context) {
if (retriever.isCaughtUp()) {
wrapped.updateNotification(context);
} else {
throttler.publish(() -> wrapped.updateNotification(context));
}
limiter.run(() -> 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));
}
limiter.run(() -> wrapped.updateNotification(context, threadId));
}
@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));
}
limiter.run(() -> wrapped.updateNotification(context, threadId, signal));
}
@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));
}
limiter.run(() -> wrapped.updateNotification(context, threadId, signal, reminderCount));
}
@Override