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()) {
@ -783,7 +783,7 @@ public class XmppConnectionService extends Service {
@Override @Override
public void onIqPacketReceived(final Account account, public void onIqPacketReceived(final Account account,
IqPacket packet) { IqPacket packet) {
Element query = packet.findChild("query"); Element query = packet.findChild("query");
if (query != null) { if (query != null) {
account.getRoster().markAllAsNotInRoster(); account.getRoster().markAllAsNotInRoster();
@ -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);
this.convChangedListenerCount++; if (this.convChangedListenerCount < 2) {
} 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;
this.accountChangedListenerCount++; if (this.accountChangedListenerCount < 2) {
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;
this.rosterChangedListenerCount++; if (this.rosterChangedListenerCount < 2) {
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;
this.mucRosterChangedListenerCount++; if (this.mucRosterChangedListenerCount < 2) {
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) {
@ -1306,7 +1247,7 @@ public class XmppConnectionService extends Service {
if (joinJid == null) { if (joinJid == null) {
return; //safety net return; //safety net
} }
Log.d(Config.LOGTAG,account.getJid().toBareJid().toString()+": joining conversation " + joinJid.toString()); Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": joining conversation " + joinJid.toString());
PresencePacket packet = new PresencePacket(); PresencePacket packet = new PresencePacket();
packet.setFrom(conversation.getAccount().getJid()); packet.setFrom(conversation.getAccount().getJid());
packet.setTo(joinJid); packet.setTo(joinJid);
@ -1738,7 +1679,7 @@ public class XmppConnectionService extends Service {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket result) { public void onIqPacketReceived(Account account, IqPacket result) {
final String ERROR = account.getJid().toBareJid() final String ERROR = account.getJid().toBareJid()
+ ": fetching avatar for " + avatar.owner + " failed "; + ": fetching avatar for " + avatar.owner + " failed ";
if (result.getType() == IqPacket.TYPE_RESULT) { if (result.getType() == IqPacket.TYPE_RESULT) {
avatar.image = mIqParser.avatarData(result); avatar.image = mIqParser.avatarData(result);
if (avatar.image != null) { if (avatar.image != null) {
@ -1752,7 +1693,7 @@ public class XmppConnectionService extends Service {
updateAccountUi(); updateAccountUi();
} else { } else {
Contact contact = account.getRoster() Contact contact = account.getRoster()
.getContact(avatar.owner); .getContact(avatar.owner);
contact.setAvatar(avatar.getFilename()); contact.setAvatar(avatar.getFilename());
getAvatarService().clear(contact); getAvatarService().clear(contact);
updateConversationUi(); updateConversationUi();