From 9ba19df2af48d06780f8defe3ae91bfa5443d30b Mon Sep 17 00:00:00 2001 From: Rhodey Orbits Date: Tue, 31 Mar 2015 13:36:04 -0700 Subject: [PATCH] Eliminate ghost thread when all messages deleted in a thread. Fixes #2262 Closes #2842 // FREEBIE --- .../securesms/ConversationActivity.java | 5 +++++ .../securesms/ConversationFragment.java | 13 +++++++++++-- .../securesms/database/MmsDatabase.java | 5 +++-- .../securesms/database/SmsDatabase.java | 5 +++-- .../securesms/database/ThreadDatabase.java | 10 ++++++---- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index 733519f659..691792a747 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -1168,6 +1168,11 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity this.composeText.setText(text); } + @Override + public void setThreadId(long threadId) { + this.threadId = threadId; + } + @Override public void onAttachmentChanged() { initializeSecurity(); diff --git a/src/org/thoughtcrime/securesms/ConversationFragment.java b/src/org/thoughtcrime/securesms/ConversationFragment.java index 1660b9b113..4fe62fe872 100644 --- a/src/org/thoughtcrime/securesms/ConversationFragment.java +++ b/src/org/thoughtcrime/securesms/ConversationFragment.java @@ -212,10 +212,17 @@ public class ConversationFragment extends ListFragment @Override protected Void doInBackground(MessageRecord... messageRecords) { for (MessageRecord messageRecord : messageRecords) { + boolean threadDeleted; + if (messageRecord.isMms()) { - DatabaseFactory.getMmsDatabase(getActivity()).delete(messageRecord.getId()); + threadDeleted = DatabaseFactory.getMmsDatabase(getActivity()).delete(messageRecord.getId()); } else { - DatabaseFactory.getSmsDatabase(getActivity()).deleteMessage(messageRecord.getId()); + threadDeleted = DatabaseFactory.getSmsDatabase(getActivity()).deleteMessage(messageRecord.getId()); + } + + if (threadDeleted) { + threadId = -1; + listener.setThreadId(threadId); } } @@ -297,6 +304,8 @@ public class ConversationFragment extends ListFragment public interface ConversationFragmentListener { public void setComposeText(String text); + + public void setThreadId(long threadId); } public interface SelectionClickListener extends diff --git a/src/org/thoughtcrime/securesms/database/MmsDatabase.java b/src/org/thoughtcrime/securesms/database/MmsDatabase.java index f791d4d9ac..26192f1c00 100644 --- a/src/org/thoughtcrime/securesms/database/MmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/MmsDatabase.java @@ -760,7 +760,7 @@ public class MmsDatabase extends MessagingDatabase { return messageId; } - public void delete(long messageId) { + public boolean delete(long messageId) { long threadId = getThreadIdForMessage(messageId); MmsAddressDatabase addrDatabase = DatabaseFactory.getMmsAddressDatabase(context); PartDatabase partDatabase = DatabaseFactory.getPartDatabase(context); @@ -769,8 +769,9 @@ public class MmsDatabase extends MessagingDatabase { SQLiteDatabase database = databaseHelper.getWritableDatabase(); database.delete(TABLE_NAME, ID_WHERE, new String[] {messageId+""}); - DatabaseFactory.getThreadDatabase(context).update(threadId); + boolean threadDeleted = DatabaseFactory.getThreadDatabase(context).update(threadId); notifyConversationListeners(threadId); + return threadDeleted; } public void deleteThread(long threadId) { diff --git a/src/org/thoughtcrime/securesms/database/SmsDatabase.java b/src/org/thoughtcrime/securesms/database/SmsDatabase.java index 5f2c782f8a..f25096c2eb 100644 --- a/src/org/thoughtcrime/securesms/database/SmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/SmsDatabase.java @@ -482,13 +482,14 @@ public class SmsDatabase extends MessagingDatabase { return cursor; } - public void deleteMessage(long messageId) { + public boolean deleteMessage(long messageId) { Log.w("MessageDatabase", "Deleting: " + messageId); SQLiteDatabase db = databaseHelper.getWritableDatabase(); long threadId = getThreadIdForMessage(messageId); db.delete(TABLE_NAME, ID_WHERE, new String[] {messageId+""}); - DatabaseFactory.getThreadDatabase(context).update(threadId); + boolean threadDeleted = DatabaseFactory.getThreadDatabase(context).update(threadId); notifyConversationListeners(threadId); + return threadDeleted; } /*package */void deleteThread(long threadId) { diff --git a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java index 632543d338..db9bd21b7d 100644 --- a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java +++ b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java @@ -391,14 +391,14 @@ public class ThreadDatabase extends Database { return null; } - public void update(long threadId) { + public boolean update(long threadId) { MmsSmsDatabase mmsSmsDatabase = DatabaseFactory.getMmsSmsDatabase(context); long count = mmsSmsDatabase.getConversationCount(threadId); if (count == 0) { deleteThread(threadId); notifyConversationListListeners(); - return; + return true; } MmsSmsDatabase.Reader reader = null; @@ -414,15 +414,17 @@ public class ThreadDatabase extends Database { else timestamp = record.getDateReceived(); updateThread(threadId, count, record.getBody().getBody(), timestamp, record.getType()); + notifyConversationListListeners(); + return false; } else { deleteThread(threadId); + notifyConversationListListeners(); + return true; } } finally { if (reader != null) reader.close(); } - - notifyConversationListListeners(); } public static interface ProgressListener {