synchronization for listeners

This commit is contained in:
iNPUTmice 2014-10-15 14:41:27 +02:00
parent f063b1c063
commit 9d960f2741

View file

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