-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throttle notifications when doing the intial message fetch.
- Loading branch information
1 parent
51c8270
commit 3a06412
Showing
4 changed files
with
107 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |