better usage of synchronized in some places

This commit is contained in:
iNPUTmice 2014-11-18 03:10:59 +01:00
parent 090e6ecf09
commit 018a0c7978
3 changed files with 82 additions and 84 deletions

View File

@ -57,7 +57,7 @@ public class Conversation extends AbstractEntity {
private Jid nextCounterpart; private Jid nextCounterpart;
protected ArrayList<Message> messages = new ArrayList<>(); protected final ArrayList<Message> messages = new ArrayList<>();
protected Account account = null; protected Account account = null;
private transient SessionImpl otrSession; private transient SessionImpl otrSession;
@ -148,10 +148,6 @@ public class Conversation extends AbstractEntity {
} }
} }
public void setMessages(ArrayList<Message> msgs) {
this.messages = msgs;
}
public String getName() { public String getName() {
if (getMode() == MODE_MULTI && getMucOptions().getSubject() != null) { if (getMode() == MODE_MULTI && getMucOptions().getSubject() != null) {
return getMucOptions().getSubject(); return getMucOptions().getSubject();

View File

@ -139,7 +139,7 @@ public class XmppConnectionService extends Service {
private MessageGenerator mMessageGenerator = new MessageGenerator(this); private MessageGenerator mMessageGenerator = new MessageGenerator(this);
private PresenceGenerator mPresenceGenerator = new PresenceGenerator(this); private PresenceGenerator mPresenceGenerator = new PresenceGenerator(this);
private List<Account> accounts; private List<Account> accounts;
private CopyOnWriteArrayList<Conversation> conversations = null; private final CopyOnWriteArrayList<Conversation> conversations = new CopyOnWriteArrayList<Conversation>();
private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager( private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager(
this); this);
private HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager( private HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager(
@ -486,27 +486,23 @@ public class XmppConnectionService extends Service {
} }
}; };
this.databaseBackend = DatabaseBackend this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
.getInstance(getApplicationContext());
this.accounts = databaseBackend.getAccounts(); this.accounts = databaseBackend.getAccounts();
for (Account account : this.accounts) { for (Account account : this.accounts) {
account.initOtrEngine(this); account.initOtrEngine(this);
this.databaseBackend.readRoster(account.getRoster()); this.databaseBackend.readRoster(account.getRoster());
} }
initConversations();
this.mergePhoneContactsWithRoster(); this.mergePhoneContactsWithRoster();
this.getConversations();
getContentResolver().registerContentObserver( getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
this.fileObserver.startWatching(); this.fileObserver.startWatching();
this.pgpServiceConnection = new OpenPgpServiceConnection( this.pgpServiceConnection = new OpenPgpServiceConnection(getApplicationContext(), "org.sufficientlysecure.keychain");
getApplicationContext(), "org.sufficientlysecure.keychain");
this.pgpServiceConnection.bindToService(); this.pgpServiceConnection.bindToService();
this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE); this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"XmppConnectionService");
"XmppConnectionService");
toggleForegroundService(); toggleForegroundService();
} }
@ -878,21 +874,23 @@ public class XmppConnectionService extends Service {
}); });
} }
public List<Conversation> getConversations() { private void initConversations() {
if (this.conversations == null) { synchronized (this.conversations) {
Hashtable<String, Account> accountLookupTable = new Hashtable<>(); Hashtable<String, Account> accountLookupTable = new Hashtable<>();
for (Account account : this.accounts) { for (Account account : this.accounts) {
accountLookupTable.put(account.getUuid(), account); accountLookupTable.put(account.getUuid(), account);
} }
this.conversations = databaseBackend this.conversations.addAll(databaseBackend.getConversations(Conversation.STATUS_AVAILABLE));
.getConversations(Conversation.STATUS_AVAILABLE);
for (Conversation conv : this.conversations) { for (Conversation conv : this.conversations) {
Account account = accountLookupTable.get(conv.getAccountUuid()); Account account = accountLookupTable.get(conv.getAccountUuid());
conv.setAccount(account); conv.setAccount(account);
conv.setMessages(databaseBackend.getMessages(conv, 50)); conv.addAll(0, databaseBackend.getMessages(conv, 50));
checkDeletedFiles(conv); checkDeletedFiles(conv);
} }
} }
}
public List<Conversation> getConversations() {
return this.conversations; return this.conversations;
} }
@ -923,12 +921,11 @@ public class XmppConnectionService extends Service {
} }
} }
public void populateWithOrderedConversations(List<Conversation> list) { public void populateWithOrderedConversations(final List<Conversation> list) {
populateWithOrderedConversations(list, true); populateWithOrderedConversations(list, true);
} }
public void populateWithOrderedConversations(List<Conversation> list, public void populateWithOrderedConversations(final List<Conversation> list, boolean includeConferences) {
boolean includeConferences) {
list.clear(); list.clear();
if (includeConferences) { if (includeConferences) {
list.addAll(getConversations()); list.addAll(getConversations());
@ -992,6 +989,7 @@ public class XmppConnectionService extends Service {
public Conversation findOrCreateConversation(final Account account, final Jid jid, public Conversation findOrCreateConversation(final Account account, final Jid jid,
final boolean muc) { final boolean muc) {
synchronized (this.conversations) {
Conversation conversation = find(account, jid); Conversation conversation = find(account, jid);
if (conversation != null) { if (conversation != null) {
return conversation; return conversation;
@ -1005,8 +1003,7 @@ public class XmppConnectionService extends Service {
} else { } else {
conversation.setMode(Conversation.MODE_SINGLE); conversation.setMode(Conversation.MODE_SINGLE);
} }
conversation.setMessages(databaseBackend.getMessages(conversation, conversation.addAll(0, databaseBackend.getMessages(conversation, 50));
50));
this.databaseBackend.updateConversation(conversation); this.databaseBackend.updateConversation(conversation);
} else { } else {
String conversationName; String conversationName;
@ -1029,8 +1026,10 @@ public class XmppConnectionService extends Service {
updateConversationUi(); updateConversationUi();
return conversation; return conversation;
} }
}
public void archiveConversation(Conversation conversation) { public void archiveConversation(Conversation conversation) {
synchronized (this.conversations) {
if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getMode() == Conversation.MODE_MULTI) {
if (conversation.getAccount().getStatus() == Account.State.ONLINE) { if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
Bookmark bookmark = conversation.getBookmark(); Bookmark bookmark = conversation.getBookmark();
@ -1047,6 +1046,7 @@ public class XmppConnectionService extends Service {
this.conversations.remove(conversation); this.conversations.remove(conversation);
updateConversationUi(); updateConversationUi();
} }
}
public void clearConversationHistory(Conversation conversation) { public void clearConversationHistory(Conversation conversation) {
this.databaseBackend.deleteMessagesInConversation(conversation); this.databaseBackend.deleteMessagesInConversation(conversation);
@ -1075,6 +1075,7 @@ public class XmppConnectionService extends Service {
} }
public void deleteAccount(Account account) { public void deleteAccount(Account account) {
synchronized (this.conversations) {
for (Conversation conversation : conversations) { for (Conversation conversation : conversations) {
if (conversation.getAccount() == account) { if (conversation.getAccount() == account) {
if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getMode() == Conversation.MODE_MULTI) {
@ -1093,6 +1094,7 @@ public class XmppConnectionService extends Service {
updateAccountUi(); updateAccountUi();
UIHelper.showErrorNotification(getApplicationContext(), getAccounts()); UIHelper.showErrorNotification(getApplicationContext(), getAccounts());
} }
}
private void removeStaleListeners() { private void removeStaleListeners() {
boolean removedListener = false; boolean removedListener = false;

View File

@ -94,7 +94,7 @@ public class ConversationFragment extends Fragment {
} }
}; };
protected ListView messagesView; protected ListView messagesView;
protected List<Message> messageList = new ArrayList<>(); final protected List<Message> messageList = new ArrayList<>();
protected MessageAdapter messageListAdapter; protected MessageAdapter messageListAdapter;
protected Contact contact; protected Contact contact;
private EditMessage mEditMessage; private EditMessage mEditMessage;