From e555fe4b033d2ad342d757217bb59704ad8207f9 Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Tue, 11 Nov 2014 17:39:28 +0100 Subject: auto register/unregister listeners --- .../eu/siacs/conversations/ui/XmppActivity.java | 50 ++++++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'src/main/java/eu/siacs/conversations/ui/XmppActivity.java') diff --git a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java index 2dafbd26d..9d78e2224 100644 --- a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java @@ -62,6 +62,7 @@ import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; +import eu.siacs.conversations.entities.MucOptions; import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.services.AvatarService; import eu.siacs.conversations.services.XmppConnectionService; @@ -77,6 +78,7 @@ public abstract class XmppActivity extends Activity { public XmppConnectionService xmppConnectionService; public boolean xmppConnectionServiceBound = false; + protected boolean registeredListeners = false; protected int mPrimaryTextColor; protected int mSecondaryTextColor; @@ -105,11 +107,11 @@ public abstract class XmppActivity extends Activity { XmppConnectionBinder binder = (XmppConnectionBinder) service; xmppConnectionService = binder.getService(); xmppConnectionServiceBound = true; - if (!isFinishing() && !isDestroyed()) { - onBackendConnected(); - } else { - Log.d(Config.LOGTAG,"omitting call to onBackendConnected()"); + if (!registeredListeners) { + registerListeners(); + registeredListeners = true; } + onBackendConnected(); } @Override @@ -123,6 +125,12 @@ public abstract class XmppActivity extends Activity { super.onStart(); if (!xmppConnectionServiceBound) { connectToBackend(); + } else { + if (!registeredListeners) { + this.registerListeners(); + this.registeredListeners = true; + } + this.onBackendConnected(); } } @@ -137,6 +145,10 @@ public abstract class XmppActivity extends Activity { protected void onStop() { super.onStop(); if (xmppConnectionServiceBound) { + if (registeredListeners) { + this.unregisterListeners(); + this.registeredListeners = false; + } unbindService(mConnection); xmppConnectionServiceBound = false; } @@ -207,6 +219,36 @@ public abstract class XmppActivity extends Activity { abstract void onBackendConnected(); + protected void registerListeners() { + if (this instanceof XmppConnectionService.OnConversationUpdate) { + this.xmppConnectionService.setOnConversationListChangedListener((XmppConnectionService.OnConversationUpdate) this); + } + if (this instanceof XmppConnectionService.OnAccountUpdate) { + this.xmppConnectionService.setOnAccountListChangedListener((XmppConnectionService.OnAccountUpdate) this); + } + if (this instanceof XmppConnectionService.OnRosterUpdate) { + this.xmppConnectionService.setOnRosterUpdateListener((XmppConnectionService.OnRosterUpdate) this); + } + if (this instanceof MucOptions.OnRenameListener) { + this.xmppConnectionService.setOnRenameListener((MucOptions.OnRenameListener) this); + } + } + + protected void unregisterListeners() { + if (this instanceof XmppConnectionService.OnConversationUpdate) { + this.xmppConnectionService.removeOnConversationListChangedListener(); + } + if (this instanceof XmppConnectionService.OnAccountUpdate) { + this.xmppConnectionService.removeOnAccountListChangedListener(); + } + if (this instanceof XmppConnectionService.OnRosterUpdate) { + this.xmppConnectionService.removeOnRosterUpdateListener(); + } + if (this instanceof MucOptions.OnRenameListener) { + this.xmppConnectionService.setOnRenameListener(null); + } + } + public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_settings: -- cgit v1.2.3