From e52b3d8942ec9843533c0a08040e8b8436aa4454 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Thu, 3 May 2018 21:50:16 +0200 Subject: organize listeners in lists --- .../java/de/pixart/messenger/ui/XmppActivity.java | 37 ++++++++-------------- 1 file changed, 13 insertions(+), 24 deletions(-) (limited to 'src/main/java/de/pixart/messenger/ui') diff --git a/src/main/java/de/pixart/messenger/ui/XmppActivity.java b/src/main/java/de/pixart/messenger/ui/XmppActivity.java index c77545be6..c7dba686d 100644 --- a/src/main/java/de/pixart/messenger/ui/XmppActivity.java +++ b/src/main/java/de/pixart/messenger/ui/XmppActivity.java @@ -59,6 +59,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import de.pixart.messenger.Config; @@ -94,7 +95,7 @@ public abstract class XmppActivity extends ActionBarActivity { public XmppConnectionService xmppConnectionService; public boolean xmppConnectionServiceBound = false; - protected boolean registeredListeners = false; + protected final AtomicBoolean registeredListeners = new AtomicBoolean(false); protected int mColorRed; protected int mColorWarningButton; @@ -119,9 +120,8 @@ public abstract class XmppActivity extends ActionBarActivity { XmppConnectionBinder binder = (XmppConnectionBinder) service; xmppConnectionService = binder.getService(); xmppConnectionServiceBound = true; - if (!registeredListeners && shouldRegisterListeners()) { + if (registeredListeners.compareAndSet(false, true)) { registerListeners(); - registeredListeners = true; } invalidateOptionsMenu(); onBackendConnected(); @@ -226,23 +226,13 @@ public abstract class XmppActivity extends ActionBarActivity { connectToBackend(); } } else { - if (!registeredListeners) { + if (registeredListeners.compareAndSet(false, true)) { this.registerListeners(); - this.registeredListeners = true; } this.onBackendConnected(); } } - @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) - protected boolean shouldRegisterListeners() { - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - return !isDestroyed() && !isFinishing(); - } else { - return !isFinishing(); - } - } - public void connectToBackend() { Intent intent = new Intent(this, XmppConnectionService.class); intent.setAction("ui"); @@ -254,9 +244,8 @@ public abstract class XmppActivity extends ActionBarActivity { protected void onStop() { super.onStop(); if (xmppConnectionServiceBound) { - if (registeredListeners) { + if (registeredListeners.compareAndSet(true, false)) { this.unregisterListeners(); - this.registeredListeners = false; } unbindService(mConnection); xmppConnectionServiceBound = false; @@ -337,28 +326,28 @@ public abstract class XmppActivity extends ActionBarActivity { protected void unregisterListeners() { if (this instanceof XmppConnectionService.OnConversationUpdate) { - this.xmppConnectionService.removeOnConversationListChangedListener(); + this.xmppConnectionService.removeOnConversationListChangedListener((XmppConnectionService.OnConversationUpdate) this); } if (this instanceof XmppConnectionService.OnAccountUpdate) { - this.xmppConnectionService.removeOnAccountListChangedListener(); + this.xmppConnectionService.removeOnAccountListChangedListener((XmppConnectionService.OnAccountUpdate) this); } if (this instanceof XmppConnectionService.OnCaptchaRequested) { - this.xmppConnectionService.removeOnCaptchaRequestedListener(); + this.xmppConnectionService.removeOnCaptchaRequestedListener((XmppConnectionService.OnCaptchaRequested) this); } if (this instanceof XmppConnectionService.OnRosterUpdate) { - this.xmppConnectionService.removeOnRosterUpdateListener(); + this.xmppConnectionService.removeOnRosterUpdateListener((XmppConnectionService.OnRosterUpdate) this); } if (this instanceof XmppConnectionService.OnMucRosterUpdate) { - this.xmppConnectionService.removeOnMucRosterUpdateListener(); + this.xmppConnectionService.removeOnMucRosterUpdateListener((XmppConnectionService.OnMucRosterUpdate) this); } if (this instanceof OnUpdateBlocklist) { - this.xmppConnectionService.removeOnUpdateBlocklistListener(); + this.xmppConnectionService.removeOnUpdateBlocklistListener((OnUpdateBlocklist) this); } if (this instanceof XmppConnectionService.OnShowErrorToast) { - this.xmppConnectionService.removeOnShowErrorToastListener(); + this.xmppConnectionService.removeOnShowErrorToastListener((XmppConnectionService.OnShowErrorToast) this); } if (this instanceof OnKeyStatusUpdated) { - this.xmppConnectionService.removeOnNewKeysAvailableListener(); + this.xmppConnectionService.removeOnNewKeysAvailableListener((OnKeyStatusUpdated) this); } } -- cgit v1.2.3