From cdddfd37d2292b4bce20206f6061ae8a6dc91369 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Wed, 14 Apr 2021 18:49:31 -0400 Subject: [PATCH] Dynamically respond to notificationsV2 feature flag. We were only reading it once, possibly before the flags were initialized. This lets us be more responsive to the flag changing within an application cycle. --- .../securesms/ApplicationContext.java | 2 +- .../ApplicationDependencyProvider.java | 9 +--- .../OptimizedMessageNotifier.java | 45 ++++++++++++------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index 1f98cfd55..ceb34cac4 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -142,6 +142,7 @@ public class ApplicationContext extends MultiDexApplication implements AppForegr } }) .addBlocking("blob-provider", this::initializeBlobProvider) + .addBlocking("feature-flags", FeatureFlags::init) .addNonBlocking(this::initializeRevealableMessageManager) .addNonBlocking(this::initializeGcmCheck) .addNonBlocking(this::initializeSignedPreKeyCheck) @@ -150,7 +151,6 @@ public class ApplicationContext extends MultiDexApplication implements AppForegr .addNonBlocking(this::initializePendingMessages) .addNonBlocking(this::initializeCleanup) .addNonBlocking(this::initializeGlideCodecs) - .addNonBlocking(FeatureFlags::init) .addNonBlocking(RefreshPreKeysJob::scheduleIfNecessary) .addNonBlocking(StorageSyncHelper::scheduleRoutineSync) .addNonBlocking(() -> ApplicationDependencies.getJobManager().beginJobLoop()) diff --git a/app/src/main/java/org/thoughtcrime/securesms/dependencies/ApplicationDependencyProvider.java b/app/src/main/java/org/thoughtcrime/securesms/dependencies/ApplicationDependencyProvider.java index d0cafbc77..ffa7f01fb 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/dependencies/ApplicationDependencyProvider.java +++ b/app/src/main/java/org/thoughtcrime/securesms/dependencies/ApplicationDependencyProvider.java @@ -186,14 +186,7 @@ public class ApplicationDependencyProvider implements ApplicationDependencies.Pr @Override public @NonNull MessageNotifier provideMessageNotifier() { - MessageNotifier inner; - if (FeatureFlags.useNewNotificationSystem()) { - inner = new MessageNotifierV2(); - } else { - inner = new DefaultMessageNotifier(); - } - - return new OptimizedMessageNotifier(inner); + return new OptimizedMessageNotifier(); } @Override diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java b/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java index 6a3453114..f152806fd 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/OptimizedMessageNotifier.java @@ -7,8 +7,10 @@ import androidx.annotation.MainThread; import androidx.annotation.NonNull; import org.signal.core.util.concurrent.SignalExecutors; +import org.thoughtcrime.securesms.notifications.v2.MessageNotifierV2; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.util.BubbleUtil; +import org.thoughtcrime.securesms.util.FeatureFlags; import org.thoughtcrime.securesms.util.LeakyBucketLimiter; import org.thoughtcrime.securesms.util.Util; @@ -17,73 +19,76 @@ import org.thoughtcrime.securesms.util.Util; */ public class OptimizedMessageNotifier implements MessageNotifier { - private final MessageNotifier wrapped; private final LeakyBucketLimiter limiter; + private final DefaultMessageNotifier messageNotifierV1; + private final MessageNotifierV2 messageNotifierV2; + @MainThread - public OptimizedMessageNotifier(@NonNull MessageNotifier wrapped) { - this.wrapped = wrapped; - this.limiter = new LeakyBucketLimiter(5, 1000, new Handler(SignalExecutors.getAndStartHandlerThread("signal-notifier").getLooper())); + public OptimizedMessageNotifier() { + this.limiter = new LeakyBucketLimiter(5, 1000, new Handler(SignalExecutors.getAndStartHandlerThread("signal-notifier").getLooper())); + this.messageNotifierV1 = new DefaultMessageNotifier(); + this.messageNotifierV2 = new MessageNotifierV2(); } @Override public void setVisibleThread(long threadId) { - wrapped.setVisibleThread(threadId); + getNotifier().setVisibleThread(threadId); } @Override public long getVisibleThread() { - return wrapped.getVisibleThread(); + return getNotifier().getVisibleThread(); } @Override public void clearVisibleThread() { - wrapped.clearVisibleThread(); + getNotifier().clearVisibleThread(); } @Override public void setLastDesktopActivityTimestamp(long timestamp) { - wrapped.setLastDesktopActivityTimestamp(timestamp); + getNotifier().setLastDesktopActivityTimestamp(timestamp); } @Override public void notifyMessageDeliveryFailed(Context context, Recipient recipient, long threadId) { - wrapped.notifyMessageDeliveryFailed(context, recipient, threadId); + getNotifier().notifyMessageDeliveryFailed(context, recipient, threadId); } @Override public void cancelDelayedNotifications() { - wrapped.cancelDelayedNotifications(); + getNotifier().cancelDelayedNotifications(); } @Override public void updateNotification(@NonNull Context context) { - runOnLimiter(() -> wrapped.updateNotification(context)); + runOnLimiter(() -> getNotifier().updateNotification(context)); } @Override public void updateNotification(@NonNull Context context, long threadId) { - runOnLimiter(() -> wrapped.updateNotification(context, threadId)); + runOnLimiter(() -> getNotifier().updateNotification(context, threadId)); } @Override public void updateNotification(@NonNull Context context, long threadId, @NonNull BubbleUtil.BubbleState defaultBubbleState) { - runOnLimiter(() -> wrapped.updateNotification(context, threadId, defaultBubbleState)); + runOnLimiter(() -> getNotifier().updateNotification(context, threadId, defaultBubbleState)); } @Override public void updateNotification(@NonNull Context context, long threadId, boolean signal) { - runOnLimiter(() -> wrapped.updateNotification(context, threadId, signal)); + runOnLimiter(() -> getNotifier().updateNotification(context, threadId, signal)); } @Override public void updateNotification(@NonNull Context context, long threadId, boolean signal, int reminderCount, @NonNull BubbleUtil.BubbleState defaultBubbleState) { - runOnLimiter(() -> wrapped.updateNotification(context, threadId, signal, reminderCount, defaultBubbleState)); + runOnLimiter(() -> getNotifier().updateNotification(context, threadId, signal, reminderCount, defaultBubbleState)); } @Override public void clearReminder(@NonNull Context context) { - wrapped.clearReminder(context); + getNotifier().clearReminder(context); } private void runOnLimiter(@NonNull Runnable runnable) { @@ -96,4 +101,12 @@ public class OptimizedMessageNotifier implements MessageNotifier { } }); } + + private MessageNotifier getNotifier() { + if (FeatureFlags.useNewNotificationSystem()) { + return messageNotifierV2; + } else { + return messageNotifierV1; + } + } }