synchronization for listeners

This commit is contained in:
iNPUTmice 2014-10-15 14:41:27 +02:00
parent f063b1c063
commit 9d960f2741
1 changed files with 60 additions and 43 deletions

View File

@ -107,14 +107,15 @@ public class XmppConnectionService extends Service {
private CopyOnWriteArrayList<Conversation> conversations = null;
private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager(
this);
private HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager(this);
private HttpConnectionManager mHttpConnectionManager = new HttpConnectionManager(
this);
private OnConversationUpdate mOnConversationUpdate = null;
private int convChangedListenerCount = 0;
private Integer convChangedListenerCount = 0;
private OnAccountUpdate mOnAccountUpdate = null;
private int accountChangedListenerCount = 0;
private Integer accountChangedListenerCount = 0;
private OnRosterUpdate mOnRosterUpdate = null;
private int rosterChangedListenerCount = 0;
private Integer rosterChangedListenerCount = 0;
public OnContactStatusChanged onContactStatusChanged = new OnContactStatusChanged() {
@Override
@ -976,9 +977,11 @@ public class XmppConnectionService extends Service {
public void setOnConversationListChangedListener(
OnConversationUpdate listener) {
if (!isScreenOn()) {
Log.d(Config.LOGTAG,"ignoring setOnConversationListChangedListener");
Log.d(Config.LOGTAG,
"ignoring setOnConversationListChangedListener");
return;
}
synchronized (this.convChangedListenerCount) {
this.mNotificationService.deactivateGracePeriod();
if (checkListeners()) {
switchToForeground();
@ -987,10 +990,13 @@ public class XmppConnectionService extends Service {
this.mNotificationService.setIsInForeground(true);
this.convChangedListenerCount++;
}
}
public void removeOnConversationListChangedListener() {
synchronized (this.convChangedListenerCount) {
this.convChangedListenerCount--;
if (this.convChangedListenerCount == 0) {
if (this.convChangedListenerCount <= 0) {
this.convChangedListenerCount = 0;
this.mOnConversationUpdate = null;
this.mNotificationService.setIsInForeground(false);
if (checkListeners()) {
@ -998,12 +1004,14 @@ public class XmppConnectionService extends Service {
}
}
}
}
public void setOnAccountListChangedListener(OnAccountUpdate listener) {
if (!isScreenOn()) {
Log.d(Config.LOGTAG,"ignoring setOnAccountListChangedListener");
Log.d(Config.LOGTAG, "ignoring setOnAccountListChangedListener");
return;
}
synchronized (this.accountChangedListenerCount) {
this.mNotificationService.deactivateGracePeriod();
if (checkListeners()) {
switchToForeground();
@ -1011,22 +1019,27 @@ public class XmppConnectionService extends Service {
this.mOnAccountUpdate = listener;
this.accountChangedListenerCount++;
}
}
public void removeOnAccountListChangedListener() {
synchronized (this.accountChangedListenerCount) {
this.accountChangedListenerCount--;
if (this.accountChangedListenerCount == 0) {
if (this.accountChangedListenerCount <= 0) {
this.mOnAccountUpdate = null;
this.accountChangedListenerCount = 0;
if (checkListeners()) {
switchToBackground();
}
}
}
}
public void setOnRosterUpdateListener(OnRosterUpdate listener) {
if (!isScreenOn()) {
Log.d(Config.LOGTAG,"ignoring setOnRosterUpdateListener");
Log.d(Config.LOGTAG, "ignoring setOnRosterUpdateListener");
return;
}
synchronized (this.rosterChangedListenerCount) {
this.mNotificationService.deactivateGracePeriod();
if (checkListeners()) {
switchToForeground();
@ -1034,16 +1047,20 @@ public class XmppConnectionService extends Service {
this.mOnRosterUpdate = listener;
this.rosterChangedListenerCount++;
}
}
public void removeOnRosterUpdateListener() {
synchronized (this.rosterChangedListenerCount) {
this.rosterChangedListenerCount--;
if (this.rosterChangedListenerCount == 0) {
if (this.rosterChangedListenerCount <= 0) {
this.rosterChangedListenerCount = 0;
this.mOnRosterUpdate = null;
if (checkListeners()) {
switchToBackground();
}
}
}
}
private boolean checkListeners() {
return (this.mOnAccountUpdate == null
@ -1077,7 +1094,8 @@ public class XmppConnectionService extends Service {
}
private boolean isScreenOn() {
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
PowerManager pm = (PowerManager) this
.getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
}
@ -1799,8 +1817,7 @@ public class XmppConnectionService extends Service {
ArrayList<Contact> contacts = new ArrayList<Contact>();
for (Account account : getAccounts()) {
if (!account.isOptionSet(Account.OPTION_DISABLED)) {
Contact contact = account.getRoster()
.getContactFromRoster(jid);
Contact contact = account.getRoster().getContactFromRoster(jid);
if (contact != null) {
contacts.add(contact);
}