From fd5760d27aa45d3e8063a46bb1f0874e7ab4e10c Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Sat, 12 Jul 2014 12:41:37 +0200 Subject: made conversation list thread safe --- src/eu/siacs/conversations/persistance/DatabaseBackend.java | 4 ++-- .../siacs/conversations/services/XmppConnectionService.java | 13 ++++++++++--- src/eu/siacs/conversations/ui/ConversationActivity.java | 3 +-- src/eu/siacs/conversations/ui/ShareWithActivity.java | 3 ++- 4 files changed, 15 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/eu/siacs/conversations/persistance/DatabaseBackend.java index fbf45d25..7643076a 100644 --- a/src/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -116,8 +116,8 @@ public class DatabaseBackend extends SQLiteOpenHelper { return cursor.getInt(0); } - public List getConversations(int status) { - List list = new ArrayList(); + public CopyOnWriteArrayList getConversations(int status) { + CopyOnWriteArrayList list = new CopyOnWriteArrayList(); SQLiteDatabase db = this.getReadableDatabase(); String[] selectionArgs = { "" + status }; Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index fc19b3c1..f8a5d974 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -10,6 +10,7 @@ import java.util.Hashtable; import java.util.List; import java.util.Locale; import java.util.TimeZone; +import java.util.concurrent.CopyOnWriteArrayList; import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpServiceConnection; @@ -98,7 +99,7 @@ public class XmppConnectionService extends Service { private PresenceGenerator mPresenceGenerator = new PresenceGenerator(); private List accounts; - private List conversations = null; + private CopyOnWriteArrayList conversations = null; private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager( this); @@ -708,7 +709,14 @@ public class XmppConnectionService extends Service { conv.setMessages(databaseBackend.getMessages(conv, 50)); } } - Collections.sort(this.conversations, new Comparator() { + + return this.conversations; + } + + public void populateWithOrderedConversations(List list) { + list.clear(); + list.addAll(getConversations()); + Collections.sort(list, new Comparator() { @Override public int compare(Conversation lhs, Conversation rhs) { Message left = lhs.getLatestMessage(); @@ -722,7 +730,6 @@ public class XmppConnectionService extends Service { } } }); - return this.conversations; } public List getMoreMessages(Conversation conversation, diff --git a/src/eu/siacs/conversations/ui/ConversationActivity.java b/src/eu/siacs/conversations/ui/ConversationActivity.java index aea35d3c..209ff3b8 100644 --- a/src/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/eu/siacs/conversations/ui/ConversationActivity.java @@ -805,8 +805,7 @@ public class ConversationActivity extends XmppActivity { } public void updateConversationList() { - conversationList.clear(); - conversationList.addAll(xmppConnectionService.getConversations()); + xmppConnectionService.populateWithOrderedConversations(conversationList); listView.invalidateViews(); } diff --git a/src/eu/siacs/conversations/ui/ShareWithActivity.java b/src/eu/siacs/conversations/ui/ShareWithActivity.java index 601caa29..461aaec4 100644 --- a/src/eu/siacs/conversations/ui/ShareWithActivity.java +++ b/src/eu/siacs/conversations/ui/ShareWithActivity.java @@ -89,7 +89,8 @@ public class ShareWithActivity extends XmppActivity { Set displayedContacts = new HashSet(); conversations.removeAllViews(); - List convList = xmppConnectionService.getConversations(); + List convList = new ArrayList(); + xmppConnectionService.populateWithOrderedConversations(convList); Collections.sort(convList, new Comparator() { @Override public int compare(Conversation lhs, Conversation rhs) { -- cgit v1.2.3