From 90d5e9dccdb5735f09bbf2870cd0d20e94dd0abb Mon Sep 17 00:00:00 2001
From: Moxie Marlinspike <moxie@thoughtcrime.org>
Date: Mon, 20 Jul 2015 18:00:48 -0700
Subject: [PATCH] Fix for double notification when locked.

Fixes #3702
// FREEBIE
---
 .../securesms/jobs/PushDecryptJob.java        | 10 ++-----
 .../notifications/MessageNotifier.java        | 30 ++++++++++++-------
 2 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java
index 4a4fdc7c51..10b3183925 100644
--- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java
+++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java
@@ -34,8 +34,6 @@ import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
 import org.thoughtcrime.securesms.util.Base64;
 import org.thoughtcrime.securesms.util.GroupUtil;
 import org.thoughtcrime.securesms.util.TextSecurePreferences;
-import org.thoughtcrime.securesms.util.Util;
-import org.thoughtcrime.securesms.util.VersionTracker;
 import org.whispersystems.jobqueue.JobParameters;
 import org.whispersystems.libaxolotl.DuplicateMessageException;
 import org.whispersystems.libaxolotl.IdentityKey;
@@ -88,11 +86,7 @@ public class PushDecryptJob extends ContextJob {
   }
 
   @Override
-  public void onAdded() {
-    if (KeyCachingService.getMasterSecret(context) == null) {
-      MessageNotifier.updateNotification(context, null, -2);
-    }
-  }
+  public void onAdded() {}
 
   @Override
   public void onRun() throws NoSuchMessageException {
@@ -100,7 +94,7 @@ public class PushDecryptJob extends ContextJob {
       Log.w(TAG, "Skipping job, waiting for migration...");
 
       if (KeyCachingService.getMasterSecret(context) != null) {
-        MessageNotifier.updateNotification(context, null, -2);
+        MessageNotifier.updateNotification(context, null, true, -2);
       }
 
       return;
diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java
index 84c8a5f7f3..4d3c509859 100644
--- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java
+++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java
@@ -124,12 +124,20 @@ public class MessageNotifier {
       return;
     }
 
-    updateNotification(context, masterSecret, false, 0);
+    updateNotification(context, masterSecret, false, false, 0);
   }
 
   public static void updateNotification(@NonNull  Context context,
                                         @Nullable MasterSecret masterSecret,
                                         long threadId)
+  {
+    updateNotification(context, masterSecret, false, threadId);
+  }
+
+  public static void updateNotification(@NonNull  Context context,
+                                        @Nullable MasterSecret masterSecret,
+                                        boolean   includePushDatabase,
+                                        long      threadId)
   {
     Recipients recipients = DatabaseFactory.getThreadDatabase(context)
                                            .getRecipientsForThreadId(threadId);
@@ -140,19 +148,20 @@ public class MessageNotifier {
       return;
     }
 
-
     if (visibleThread == threadId) {
       ThreadDatabase threads = DatabaseFactory.getThreadDatabase(context);
       threads.setRead(threadId);
       sendInThreadNotification(context, threads.getRecipientsForThreadId(threadId));
     } else {
-      updateNotification(context, masterSecret, true, 0);
+      updateNotification(context, masterSecret, true, includePushDatabase, 0);
     }
   }
 
   private static void updateNotification(@NonNull  Context context,
                                          @Nullable MasterSecret masterSecret,
-                                         boolean signal, int reminderCount)
+                                         boolean signal,
+                                         boolean includePushDatabase,
+                                         int     reminderCount)
   {
     Cursor telcoCursor = null;
     Cursor pushCursor  = null;
@@ -173,7 +182,9 @@ public class MessageNotifier {
 
       NotificationState notificationState = constructNotificationState(context, masterSecret, telcoCursor);
 
-      appendPushNotificationState(context, masterSecret, notificationState, pushCursor);
+      if (includePushDatabase) {
+        appendPushNotificationState(context, notificationState, pushCursor);
+      }
 
       if (notificationState.hasMultipleThreads()) {
         sendMultipleThreadNotification(context, masterSecret, notificationState, signal);
@@ -382,13 +393,10 @@ public class MessageNotifier {
     }
   }
 
-  private static void appendPushNotificationState(@NonNull  Context context,
-                                                  @Nullable MasterSecret masterSecret,
-                                                  @NonNull  NotificationState notificationState,
-                                                  @NonNull  Cursor cursor)
+  private static void appendPushNotificationState(@NonNull Context context,
+                                                  @NonNull NotificationState notificationState,
+                                                  @NonNull Cursor cursor)
   {
-    if (masterSecret != null) return;
-
     PushDatabase.Reader reader = null;
     TextSecureEnvelope envelope;