made conversation list thread safe

This commit is contained in:
iNPUTmice 2014-07-12 12:41:37 +02:00
parent c512d98b74
commit fd5760d27a
4 changed files with 15 additions and 8 deletions

View File

@ -116,8 +116,8 @@ public class DatabaseBackend extends SQLiteOpenHelper {
return cursor.getInt(0); return cursor.getInt(0);
} }
public List<Conversation> getConversations(int status) { public CopyOnWriteArrayList<Conversation> getConversations(int status) {
List<Conversation> list = new ArrayList<Conversation>(); CopyOnWriteArrayList<Conversation> list = new CopyOnWriteArrayList<Conversation>();
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = { "" + status }; String[] selectionArgs = { "" + status };
Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME

View File

@ -10,6 +10,7 @@ import java.util.Hashtable;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.CopyOnWriteArrayList;
import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection; import org.openintents.openpgp.util.OpenPgpServiceConnection;
@ -98,7 +99,7 @@ public class XmppConnectionService extends Service {
private PresenceGenerator mPresenceGenerator = new PresenceGenerator(); private PresenceGenerator mPresenceGenerator = new PresenceGenerator();
private List<Account> accounts; private List<Account> accounts;
private List<Conversation> conversations = null; private CopyOnWriteArrayList<Conversation> conversations = null;
private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager( private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager(
this); this);
@ -708,7 +709,14 @@ public class XmppConnectionService extends Service {
conv.setMessages(databaseBackend.getMessages(conv, 50)); conv.setMessages(databaseBackend.getMessages(conv, 50));
} }
} }
Collections.sort(this.conversations, new Comparator<Conversation>() {
return this.conversations;
}
public void populateWithOrderedConversations(List<Conversation> list) {
list.clear();
list.addAll(getConversations());
Collections.sort(list, new Comparator<Conversation>() {
@Override @Override
public int compare(Conversation lhs, Conversation rhs) { public int compare(Conversation lhs, Conversation rhs) {
Message left = lhs.getLatestMessage(); Message left = lhs.getLatestMessage();
@ -722,7 +730,6 @@ public class XmppConnectionService extends Service {
} }
} }
}); });
return this.conversations;
} }
public List<Message> getMoreMessages(Conversation conversation, public List<Message> getMoreMessages(Conversation conversation,

View File

@ -805,8 +805,7 @@ public class ConversationActivity extends XmppActivity {
} }
public void updateConversationList() { public void updateConversationList() {
conversationList.clear(); xmppConnectionService.populateWithOrderedConversations(conversationList);
conversationList.addAll(xmppConnectionService.getConversations());
listView.invalidateViews(); listView.invalidateViews();
} }

View File

@ -89,7 +89,8 @@ public class ShareWithActivity extends XmppActivity {
Set<Contact> displayedContacts = new HashSet<Contact>(); Set<Contact> displayedContacts = new HashSet<Contact>();
conversations.removeAllViews(); conversations.removeAllViews();
List<Conversation> convList = xmppConnectionService.getConversations(); List<Conversation> convList = new ArrayList<Conversation>();
xmppConnectionService.populateWithOrderedConversations(convList);
Collections.sort(convList, new Comparator<Conversation>() { Collections.sort(convList, new Comparator<Conversation>() {
@Override @Override
public int compare(Conversation lhs, Conversation rhs) { public int compare(Conversation lhs, Conversation rhs) {