more safety checks for listener counts

This commit is contained in:
iNPUTmice 2014-12-03 14:55:09 +01:00
parent 71905ef2b4
commit 46e319b241
1 changed files with 29 additions and 88 deletions

View File

@ -209,11 +209,11 @@ public class XmppConnectionService extends Service {
getNotificationService().updateErrorNotification(); getNotificationService().updateErrorNotification();
} }
}; };
private Integer accountChangedListenerCount = 0; private int accountChangedListenerCount = 0;
private OnRosterUpdate mOnRosterUpdate = null; private OnRosterUpdate mOnRosterUpdate = null;
private Integer rosterChangedListenerCount = 0; private int rosterChangedListenerCount = 0;
private OnMucRosterUpdate mOnMucRosterUpdate = null; private OnMucRosterUpdate mOnMucRosterUpdate = null;
private Integer mucRosterChangedListenerCount = 0; private int mucRosterChangedListenerCount = 0;
private SecureRandom mRandom; private SecureRandom mRandom;
private FileObserver fileObserver = new FileObserver( private FileObserver fileObserver = new FileObserver(
FileBackend.getConversationsImageDirectory()) { FileBackend.getConversationsImageDirectory()) {
@ -1095,72 +1095,21 @@ public class XmppConnectionService extends Service {
} }
} }
private void removeStaleListeners() { public void setOnConversationListChangedListener(OnConversationUpdate listener) {
boolean removedListener = false; synchronized (this) {
synchronized (this.convChangedListenerCount) {
if (this.mOnConversationUpdate != null) {
this.mOnConversationUpdate = null;
this.convChangedListenerCount = 0;
this.mNotificationService.setIsInForeground(false);
removedListener = true;
}
}
synchronized (this.accountChangedListenerCount) {
if (this.mOnAccountUpdate != null) {
this.mOnAccountUpdate = null;
this.accountChangedListenerCount = 0;
removedListener = true;
}
}
synchronized (this.rosterChangedListenerCount) {
if (this.mOnRosterUpdate != null) {
this.mOnRosterUpdate = null;
this.rosterChangedListenerCount = 0;
removedListener = true;
}
}
synchronized (this.mucRosterChangedListenerCount) {
if (this.mOnMucRosterUpdate != null) {
this.mOnMucRosterUpdate = null;
this.mucRosterChangedListenerCount = 0;
removedListener = true;
}
}
if (removedListener) {
final String msg = "removed stale listeners";
Log.d(Config.LOGTAG, msg);
checkListeners();
try {
OutputStream os = openFileOutput("stacktrace.txt", MODE_PRIVATE);
os.write(msg.getBytes());
os.flush();
os.close();
} catch (final FileNotFoundException ignored) {
} catch (final IOException ignored) {
}
}
}
public void setOnConversationListChangedListener(
OnConversationUpdate listener) {
/*if (!isScreenOn()) {
Log.d(Config.LOGTAG,
"ignoring setOnConversationListChangedListener");
return;
}*/
synchronized (this.convChangedListenerCount) {
if (checkListeners()) { if (checkListeners()) {
switchToForeground(); switchToForeground();
} }
this.mOnConversationUpdate = listener; this.mOnConversationUpdate = listener;
this.mNotificationService.setIsInForeground(true); this.mNotificationService.setIsInForeground(true);
if (this.convChangedListenerCount < 2) {
this.convChangedListenerCount++; this.convChangedListenerCount++;
} }
} }
}
public void removeOnConversationListChangedListener() { public void removeOnConversationListChangedListener() {
synchronized (this.convChangedListenerCount) { synchronized (this) {
this.convChangedListenerCount--; this.convChangedListenerCount--;
if (this.convChangedListenerCount <= 0) { if (this.convChangedListenerCount <= 0) {
this.convChangedListenerCount = 0; this.convChangedListenerCount = 0;
@ -1174,21 +1123,19 @@ public class XmppConnectionService extends Service {
} }
public void setOnAccountListChangedListener(OnAccountUpdate listener) { public void setOnAccountListChangedListener(OnAccountUpdate listener) {
/*if (!isScreenOn()) { synchronized (this) {
Log.d(Config.LOGTAG, "ignoring setOnAccountListChangedListener");
return;
}*/
synchronized (this.accountChangedListenerCount) {
if (checkListeners()) { if (checkListeners()) {
switchToForeground(); switchToForeground();
} }
this.mOnAccountUpdate = listener; this.mOnAccountUpdate = listener;
if (this.accountChangedListenerCount < 2) {
this.accountChangedListenerCount++; this.accountChangedListenerCount++;
} }
} }
}
public void removeOnAccountListChangedListener() { public void removeOnAccountListChangedListener() {
synchronized (this.accountChangedListenerCount) { synchronized (this) {
this.accountChangedListenerCount--; this.accountChangedListenerCount--;
if (this.accountChangedListenerCount <= 0) { if (this.accountChangedListenerCount <= 0) {
this.mOnAccountUpdate = null; this.mOnAccountUpdate = null;
@ -1201,21 +1148,19 @@ public class XmppConnectionService extends Service {
} }
public void setOnRosterUpdateListener(OnRosterUpdate listener) { public void setOnRosterUpdateListener(OnRosterUpdate listener) {
/*if (!isScreenOn()) { synchronized (this) {
Log.d(Config.LOGTAG, "ignoring setOnRosterUpdateListener");
return;
}*/
synchronized (this.rosterChangedListenerCount) {
if (checkListeners()) { if (checkListeners()) {
switchToForeground(); switchToForeground();
} }
this.mOnRosterUpdate = listener; this.mOnRosterUpdate = listener;
if (this.rosterChangedListenerCount < 2) {
this.rosterChangedListenerCount++; this.rosterChangedListenerCount++;
} }
} }
}
public void removeOnRosterUpdateListener() { public void removeOnRosterUpdateListener() {
synchronized (this.rosterChangedListenerCount) { synchronized (this) {
this.rosterChangedListenerCount--; this.rosterChangedListenerCount--;
if (this.rosterChangedListenerCount <= 0) { if (this.rosterChangedListenerCount <= 0) {
this.rosterChangedListenerCount = 0; this.rosterChangedListenerCount = 0;
@ -1228,17 +1173,19 @@ public class XmppConnectionService extends Service {
} }
public void setOnMucRosterUpdateListener(OnMucRosterUpdate listener) { public void setOnMucRosterUpdateListener(OnMucRosterUpdate listener) {
synchronized (this.mucRosterChangedListenerCount) { synchronized (this) {
if (checkListeners()) { if (checkListeners()) {
switchToForeground(); switchToForeground();
} }
this.mOnMucRosterUpdate = listener; this.mOnMucRosterUpdate = listener;
if (this.mucRosterChangedListenerCount < 2) {
this.mucRosterChangedListenerCount++; this.mucRosterChangedListenerCount++;
} }
} }
}
public void removeOnMucRosterUpdateListener() { public void removeOnMucRosterUpdateListener() {
synchronized (this.mucRosterChangedListenerCount) { synchronized (this) {
this.mucRosterChangedListenerCount--; this.mucRosterChangedListenerCount--;
if (this.mucRosterChangedListenerCount <= 0) { if (this.mucRosterChangedListenerCount <= 0) {
this.mucRosterChangedListenerCount = 0; this.mucRosterChangedListenerCount = 0;
@ -1280,12 +1227,6 @@ public class XmppConnectionService extends Service {
Log.d(Config.LOGTAG, "app switched into background"); Log.d(Config.LOGTAG, "app switched into background");
} }
private boolean isScreenOn() {
PowerManager pm = (PowerManager) this
.getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
}
public void connectMultiModeConversations(Account account) { public void connectMultiModeConversations(Account account) {
List<Conversation> conversations = getConversations(); List<Conversation> conversations = getConversations();
for (Conversation conversation : conversations) { for (Conversation conversation : conversations) {