From fe5fc411a701bdb1bb3e07e36c7c58fe7adddcb8 Mon Sep 17 00:00:00 2001 From: AsamK Date: Sun, 4 Jan 2015 00:25:35 +0100 Subject: [PATCH] Save drafts for recipients without an existing thread - Save drafts without thread in draft database - Clear drafts when deleting conversation(s), otherwise the drafts stay in the database and will reappear when a thread with the same threadId is created Closes #1510 --- .../securesms/ConversationActivity.java | 34 +++++++++++-------- .../securesms/database/DraftDatabase.java | 22 ++++++++++++ .../securesms/database/ThreadDatabase.java | 3 ++ 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index 06dcf48870..5fc23ad738 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -23,8 +23,6 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; -import android.graphics.Bitmap; -import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; @@ -92,7 +90,6 @@ import org.thoughtcrime.securesms.sms.OutgoingEncryptedMessage; import org.thoughtcrime.securesms.sms.OutgoingEndSessionMessage; import org.thoughtcrime.securesms.sms.OutgoingTextMessage; import org.thoughtcrime.securesms.util.BitmapDecodingException; -import org.thoughtcrime.securesms.util.BitmapUtil; import org.thoughtcrime.securesms.util.CharacterCalculator; import org.thoughtcrime.securesms.util.Dialogs; import org.thoughtcrime.securesms.util.DirectoryHelper; @@ -538,6 +535,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity public void onClick(DialogInterface dialog, int which) { if (threadId > 0) { DatabaseFactory.getThreadDatabase(ConversationActivity.this).deleteConversation(threadId); + threadId = -1; finish(); } } @@ -919,28 +917,34 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity } private void saveDraft() { - if (this.threadId <= 0 || this.recipients == null || this.recipients.isEmpty()) + if (this.recipients == null || this.recipients.isEmpty()) return; - final Drafts drafts = getDraftsForCurrentState(); - final long thisThreadId = this.threadId; - final MasterSecret thisMasterSecret = this.masterSecret.parcelClone(); + final Drafts drafts = getDraftsForCurrentState(); + final long thisThreadId = this.threadId; + final MasterSecret thisMasterSecret = this.masterSecret.parcelClone(); + final int thisDistributionType = this.distributionType; - new AsyncTask() { + new AsyncTask() { @Override - protected Void doInBackground(Void... params) { - MasterCipher masterCipher = new MasterCipher(thisMasterSecret); - DatabaseFactory.getDraftDatabase(ConversationActivity.this).insertDrafts(masterCipher, thisThreadId, drafts); + protected Void doInBackground(Long... params) { ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(ConversationActivity.this); + DraftDatabase draftDatabase = DatabaseFactory.getDraftDatabase(ConversationActivity.this); + long threadId = params[0]; + if (drafts.size() > 0) { - threadDatabase.updateSnippet(thisThreadId, drafts.getSnippet(ConversationActivity.this), Types.BASE_DRAFT_TYPE); - } else { - threadDatabase.update(thisThreadId); + if (threadId == -1) threadId = threadDatabase.getThreadIdFor(getRecipients(), thisDistributionType); + + draftDatabase.insertDrafts(new MasterCipher(thisMasterSecret), threadId, drafts); + threadDatabase.updateSnippet(threadId, drafts.getSnippet(ConversationActivity.this), Types.BASE_DRAFT_TYPE); + } else if (threadId > 0) { + threadDatabase.update(threadId); } + MemoryCleaner.clean(thisMasterSecret); return null; } - }.execute(); + }.execute(thisThreadId); } private void calculateCharactersRemaining() { diff --git a/src/org/thoughtcrime/securesms/database/DraftDatabase.java b/src/org/thoughtcrime/securesms/database/DraftDatabase.java index 6a4c5a05ad..631878f938 100644 --- a/src/org/thoughtcrime/securesms/database/DraftDatabase.java +++ b/src/org/thoughtcrime/securesms/database/DraftDatabase.java @@ -13,6 +13,7 @@ import org.thoughtcrime.securesms.crypto.MasterCipher; import java.util.LinkedList; import java.util.List; +import java.util.Set; public class DraftDatabase extends Database { @@ -51,6 +52,27 @@ public class DraftDatabase extends Database { db.delete(TABLE_NAME, THREAD_ID + " = ?", new String[] {threadId+""}); } + public void clearDrafts(Set threadIds) { + SQLiteDatabase db = databaseHelper.getWritableDatabase(); + StringBuilder where = new StringBuilder(); + List arguments = new LinkedList<>(); + + for (long threadId : threadIds) { + where.append(" OR ") + .append(THREAD_ID) + .append(" = ?"); + + arguments.add(String.valueOf(threadId)); + } + + db.delete(TABLE_NAME, where.toString().substring(4), arguments.toArray(new String[0])); + } + + public void clearAllDrafts() { + SQLiteDatabase db = databaseHelper.getWritableDatabase(); + db.delete(TABLE_NAME, null, null); + } + public List getDrafts(MasterCipher masterCipher, long threadId) { SQLiteDatabase db = databaseHelper.getReadableDatabase(); List results = new LinkedList(); diff --git a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java index 96d29b0502..f567793b1d 100644 --- a/src/org/thoughtcrime/securesms/database/ThreadDatabase.java +++ b/src/org/thoughtcrime/securesms/database/ThreadDatabase.java @@ -290,6 +290,7 @@ public class ThreadDatabase extends Database { public void deleteConversation(long threadId) { DatabaseFactory.getSmsDatabase(context).deleteThread(threadId); DatabaseFactory.getMmsDatabase(context).deleteThread(threadId); + DatabaseFactory.getDraftDatabase(context).clearDrafts(threadId); deleteThread(threadId); notifyConversationListeners(threadId); notifyConversationListListeners(); @@ -299,6 +300,7 @@ public class ThreadDatabase extends Database { public void deleteConversations(Set selectedConversations) { DatabaseFactory.getSmsDatabase(context).deleteThreads(selectedConversations); DatabaseFactory.getMmsDatabase(context).deleteThreads(selectedConversations); + DatabaseFactory.getDraftDatabase(context).clearDrafts(selectedConversations); deleteThreads(selectedConversations); notifyConversationListeners(selectedConversations); notifyConversationListListeners(); @@ -307,6 +309,7 @@ public class ThreadDatabase extends Database { public void deleteAllConversations() { DatabaseFactory.getSmsDatabase(context).deleteAllThreads(); DatabaseFactory.getMmsDatabase(context).deleteAllThreads(); + DatabaseFactory.getDraftDatabase(context).clearAllDrafts(); deleteAllThreads(); }