better aproach on thread saftey

This commit is contained in:
iNPUTmice 2014-10-22 00:25:28 +02:00
parent 9f9c3d3c2f
commit 45bdadd915
1 changed files with 7 additions and 13 deletions

View File

@ -1,9 +1,7 @@
package eu.siacs.conversations.services; package eu.siacs.conversations.services;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -58,24 +56,25 @@ public class NotificationService {
mList.add(message); mList.add(message);
notifications.put(conversationUuid, mList); notifications.put(conversationUuid, mList);
} }
Account account = message.getConversation().getAccount();
updateNotification((!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn)
&& !account.inGracePeriod());
} }
Account account = message.getConversation().getAccount();
updateNotification((!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn)
&& !account.inGracePeriod());
} }
public void clear() { public void clear() {
synchronized (notifications) { synchronized (notifications) {
notifications.clear(); notifications.clear();
updateNotification(false);
} }
updateNotification(false);
} }
public void clear(Conversation conversation) { public void clear(Conversation conversation) {
synchronized (notifications) { synchronized (notifications) {
notifications.remove(conversation.getUuid()); notifications.remove(conversation.getUuid());
updateNotification(false);
} }
updateNotification(false);
} }
private void updateNotification(boolean notify) { private void updateNotification(boolean notify) {
@ -132,11 +131,7 @@ public class NotificationService {
.getString(R.string.unread_conversations)); .getString(R.string.unread_conversations));
StringBuilder names = new StringBuilder(); StringBuilder names = new StringBuilder();
Conversation conversation = null; Conversation conversation = null;
Iterator<Entry<String, ArrayList<Message>>> it = notifications for (ArrayList<Message> messages : notifications.values()) {
.entrySet().iterator();
while (it.hasNext()) {
Entry<String, ArrayList<Message>> entry = it.next();
ArrayList<Message> messages = entry.getValue();
if (messages.size() > 0) { if (messages.size() > 0) {
conversation = messages.get(0).getConversation(); conversation = messages.get(0).getConversation();
String name = conversation.getName(); String name = conversation.getName();
@ -148,7 +143,6 @@ public class NotificationService {
names.append(name); names.append(name);
names.append(", "); names.append(", ");
} }
it.remove();
} }
if (names.length() >= 2) { if (names.length() >= 2) {
names.delete(names.length() - 2, names.length()); names.delete(names.length() - 2, names.length());