From 52b55652c873f7066af2e05148952e26e8c66302 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 12 Sep 2019 09:59:15 +1000 Subject: [PATCH] Clean --- .../securesms/ApplicationContext.java | 45 +++++++++---------- .../conversation/ConversationActivity.java | 6 +-- .../securesms/database/ThreadDatabase.java | 9 ++-- .../securesms/groups/GroupManager.java | 4 +- .../securesms/jobs/PushDecryptJob.java | 15 +++---- .../notifications/MessageNotifier.java | 6 +-- 6 files changed, 37 insertions(+), 48 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ApplicationContext.java b/src/org/thoughtcrime/securesms/ApplicationContext.java index d7888360f4..3570503791 100644 --- a/src/org/thoughtcrime/securesms/ApplicationContext.java +++ b/src/org/thoughtcrime/securesms/ApplicationContext.java @@ -478,16 +478,14 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc } } - private void createGroupChatPollersIfNeeded() { - // Only add the public chat poller if we have the thread + // Only create the group chat pollers if their threads aren't deleted LokiGroupChat publicChat = lokiPublicChat(); - long threadId = GroupManager.getThreadId(publicChat.getId(), this); - if (threadId >= 0 && lokiPublicChatPoller == null) { + long threadID = GroupManager.getThreadId(publicChat.getId(), this); + if (threadID >= 0 && lokiPublicChatPoller == null) { lokiPublicChatPoller = new LokiGroupChatPoller(this, publicChat); - - // Attach a deletion listener to the thread if we have it - setupThreadDeletionListeners(threadId, () -> { + // Set up deletion listeners if needed + setUpThreadDeletionListeners(threadID, () -> { if (lokiPublicChatPoller != null) lokiPublicChatPoller.stop(); lokiPublicChatPoller = null; }); @@ -495,43 +493,42 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc } private void createRSSFeedPollersIfNeeded() { - // Only add the feed poller if we have the thread + // Only create the RSS feed pollers if their threads aren't deleted LokiRSSFeed lokiNewsFeed = lokiNewsFeed(); - long lokiNewsFeedThreadId = GroupManager.getThreadId(lokiNewsFeed.getId(), this); - if (lokiNewsFeedThreadId >= 0 && lokiNewsFeedPoller == null) { + long lokiNewsFeedThreadID = GroupManager.getThreadId(lokiNewsFeed.getId(), this); + if (lokiNewsFeedThreadID >= 0 && lokiNewsFeedPoller == null) { lokiNewsFeedPoller = new LokiRSSFeedPoller(this, lokiNewsFeed); - - // Attach a deletion listener to the thread if we have it - setupThreadDeletionListeners(lokiNewsFeedThreadId, () -> { + // Set up deletion listeners if needed + setUpThreadDeletionListeners(lokiNewsFeedThreadID, () -> { if (lokiNewsFeedPoller != null) lokiNewsFeedPoller.stop(); lokiNewsFeedPoller = null; }); } - - // This one is not stoppable - if (lokiMessengerUpdatesFeedPoller == null) { lokiMessengerUpdatesFeedPoller = new LokiRSSFeedPoller(this, lokiMessengerUpdatesFeed()); } + // The user can't delete the Loki Messenger Updates RSS feed + if (lokiMessengerUpdatesFeedPoller == null) { + lokiMessengerUpdatesFeedPoller = new LokiRSSFeedPoller(this, lokiMessengerUpdatesFeed()); + } } - private void setupThreadDeletionListeners(long threadId, Runnable onDelete) { - if (threadId < 0) { return; } - + private void setUpThreadDeletionListeners(long threadID, Runnable onDelete) { + if (threadID < 0) { return; } ContentObserver observer = new ContentObserver(null) { + @Override public void onChange(boolean selfChange) { super.onChange(selfChange); - - // Stop the poller if thread doesn't exist + // Stop the poller if thread is deleted try { - if (!DatabaseFactory.getThreadDatabase(getApplicationContext()).hasThread(threadId)) { + if (!DatabaseFactory.getThreadDatabase(getApplicationContext()).hasThread(threadID)) { onDelete.run(); getContentResolver().unregisterContentObserver(this); } } catch (Exception e) { - // Failed to call delete + // TODO: Handle } } }; - this.getContentResolver().registerContentObserver(DatabaseContentProviders.Conversation.getUriForThread(threadId), true, observer); + this.getContentResolver().registerContentObserver(DatabaseContentProviders.Conversation.getUriForThread(threadID), true, observer); } public void startGroupChatPollersIfNeeded() { diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java index 79b6003c5c..88c5404192 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java @@ -2101,10 +2101,8 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity Context context = ConversationActivity.this; List messageIds = DatabaseFactory.getThreadDatabase(context).setRead(params[0], false); - // Only notify on private chats - if (!getRecipient().isGroupRecipient()) { - MessageNotifier.updateNotification(context); - } + // Only send notifications for private chats + if (!getRecipient().isGroupRecipient()) { MessageNotifier.updateNotification(context); } MarkReadReceiver.process(context, messageIds); diff --git a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java index b6598860a8..a4d1917174 100644 --- a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java +++ b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java @@ -473,14 +473,11 @@ public class ThreadDatabase extends Database { } public boolean hasThread(long threadId) { - SQLiteDatabase db = databaseHelper.getReadableDatabase(); - Cursor cursor = db.query(TABLE_NAME, new String[]{ID}, ID_WHERE, new String[]{String.valueOf(threadId)}, null, null, null); + SQLiteDatabase db = databaseHelper.getReadableDatabase(); + Cursor cursor = db.query(TABLE_NAME, new String[]{ ID }, ID_WHERE, new String[]{ String.valueOf(threadId) }, null, null, null); try { - if (cursor != null && cursor.moveToFirst()) { - return true; - } - + if (cursor != null && cursor.moveToFirst()) { return true; } return false; } finally { if (cursor != null) cursor.close(); diff --git a/src/org/thoughtcrime/securesms/groups/GroupManager.java b/src/org/thoughtcrime/securesms/groups/GroupManager.java index 5fae394cf7..e17b964d93 100644 --- a/src/org/thoughtcrime/securesms/groups/GroupManager.java +++ b/src/org/thoughtcrime/securesms/groups/GroupManager.java @@ -37,12 +37,12 @@ import java.util.Set; public class GroupManager { public static long getThreadId(String id, @NonNull Context context) { - final String groupId = GroupUtil.getEncodedId(id.getBytes(), false); + final String groupId = GroupUtil.getEncodedId(id.getBytes(), false); return getThreadIdFromGroupId(groupId, context); } public static long getThreadIdFromGroupId(String groupId, @NonNull Context context) { - final Recipient groupRecipient = Recipient.from(context, Address.fromSerialized(groupId), false); + final Recipient groupRecipient = Recipient.from(context, Address.fromSerialized(groupId), false); return DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(groupRecipient); } diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 25be4d87d9..3554c88309 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -812,8 +812,8 @@ public class PushDecryptJob extends BaseJob implements InjectableType { database.endTransaction(); } - // Loki - Map message id to server id - updatePublicChatMessageWithServerID(messageServerIDOrNull, insertResult); + // Loki - Store message server ID + updateGroupChatMessageServerID(messageServerIDOrNull, insertResult); if (insertResult.isPresent()) { MessageNotifier.updateNotification(context, insertResult.get().getThreadId()); @@ -982,8 +982,8 @@ public class PushDecryptJob extends BaseJob implements InjectableType { if (smsMessageId.isPresent()) database.deleteMessage(smsMessageId.get()); - // Loki - Map message id to server id - updatePublicChatMessageWithServerID(messageServerIDOrNull, insertResult); + // Loki - Store message server ID + updateGroupChatMessageServerID(messageServerIDOrNull, insertResult); boolean isGroupMessage = message.getGroupInfo().isPresent(); if (threadId != null && !isGroupMessage) { @@ -993,10 +993,9 @@ public class PushDecryptJob extends BaseJob implements InjectableType { } } - private void updatePublicChatMessageWithServerID(Optional messageServerIDOrNull, Optional databaseInsert) { - if (messageServerIDOrNull == null) { return; } - if (databaseInsert.isPresent() && messageServerIDOrNull.isPresent()) { - long messageID = databaseInsert.get().getMessageId(); + private void updateGroupChatMessageServerID(Optional messageServerIDOrNull, Optional insertResult) { + if (insertResult.isPresent() && messageServerIDOrNull.isPresent()) { + long messageID = insertResult.get().getMessageId(); long messageServerID = messageServerIDOrNull.get(); DatabaseFactory.getLokiMessageDatabase(context).setServerID(messageID, messageServerID); } diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index 418afa0d5b..e7e6cc73ba 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -388,10 +388,8 @@ public class MessageNotifier { } private static void sendInThreadNotification(Context context, Recipient recipient) { - // Keep group messages muted! - if (recipient.isGroupRecipient()) { - return; - } + // Mute group chats + if (recipient.isGroupRecipient()) { return; } if (!TextSecurePreferences.isInThreadNotifications(context) || ServiceUtil.getAudioManager(context).getRingerMode() != AudioManager.RINGER_MODE_NORMAL)