aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart
diff options
context:
space:
mode:
authorChristian Schneppe <christian@pix-art.de>2018-07-09 22:11:11 +0200
committerChristian Schneppe <christian@pix-art.de>2018-07-09 22:11:11 +0200
commit83f53eb8e93212313aa8003d1e478d7eb9d7986c (patch)
treef01fe9af12e22e7e856747bca19003e0c9887db7 /src/main/java/de/pixart
parentda802b4248998584c9233a1b268020f8df2696af (diff)
use sets instead of list for listeners
Diffstat (limited to 'src/main/java/de/pixart')
-rw-r--r--src/main/java/de/pixart/messenger/services/XmppConnectionService.java147
-rw-r--r--src/main/java/de/pixart/messenger/ui/XmppActivity.java14
2 files changed, 93 insertions, 68 deletions
diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java
index bf760949a..e809b1269 100644
--- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java
+++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java
@@ -276,14 +276,16 @@ public class XmppConnectionService extends Service {
private int unreadCount = -1;
//Ui callback listeners
- private final List<OnConversationUpdate> mOnConversationUpdates = new ArrayList<>();
- private final List<OnShowErrorToast> mOnShowErrorToasts = new ArrayList<>();
- private final List<OnAccountUpdate> mOnAccountUpdates = new ArrayList<>();
- private final List<OnCaptchaRequested> mOnCaptchaRequested = new ArrayList<>();
- private final List<OnRosterUpdate> mOnRosterUpdates = new ArrayList<>();
- private final List<OnUpdateBlocklist> mOnUpdateBlocklist = new ArrayList<>();
- private final List<OnMucRosterUpdate> mOnMucRosterUpdate = new ArrayList<>();
- private final List<OnKeyStatusUpdated> mOnKeyStatusUpdated = new ArrayList<>();
+ private final Set<OnConversationUpdate> mOnConversationUpdates = Collections.newSetFromMap(new WeakHashMap<OnConversationUpdate, Boolean>());
+ private final Set<OnShowErrorToast> mOnShowErrorToasts = Collections.newSetFromMap(new WeakHashMap<OnShowErrorToast, Boolean>());
+ private final Set<OnAccountUpdate> mOnAccountUpdates = Collections.newSetFromMap(new WeakHashMap<OnAccountUpdate, Boolean>());
+ private final Set<OnCaptchaRequested> mOnCaptchaRequested = Collections.newSetFromMap(new WeakHashMap<OnCaptchaRequested, Boolean>());
+ private final Set<OnRosterUpdate> mOnRosterUpdates = Collections.newSetFromMap(new WeakHashMap<OnRosterUpdate, Boolean>());
+ private final Set<OnUpdateBlocklist> mOnUpdateBlocklist = Collections.newSetFromMap(new WeakHashMap<OnUpdateBlocklist, Boolean>());
+ private final Set<OnMucRosterUpdate> mOnMucRosterUpdate = Collections.newSetFromMap(new WeakHashMap<OnMucRosterUpdate, Boolean>());
+ private final Set<OnKeyStatusUpdated> mOnKeyStatusUpdated = Collections.newSetFromMap(new WeakHashMap<OnKeyStatusUpdated, Boolean>());
+
+ private final Object LISTENER_LOCK = new Object();
private final OnBindListener mOnBindListener = new OnBindListener() {
@@ -2133,18 +2135,19 @@ public class XmppConnectionService extends Service {
}
public void setOnConversationListChangedListener(OnConversationUpdate listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
if (checkListeners()) {
switchToForeground();
}
- this.mOnConversationUpdates.add(listener);
- Log.d(Config.LOGTAG, "XmppConnectionService setOnConversationListChangedListener(): setIsInForeground = " + (this.mOnConversationUpdates.size() > 0));
+ if (!this.mOnConversationUpdates.add(listener)) {
+ Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as ConversationListChangedListener");
+ }
this.mNotificationService.setIsInForeground(this.mOnConversationUpdates.size() > 0);
}
}
public void removeOnConversationListChangedListener(OnConversationUpdate listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
this.mOnConversationUpdates.remove(listener);
Log.d(Config.LOGTAG, "XmppConnectionService removeOnConversationListChangedListener(): setIsInForeground = " + (this.mOnConversationUpdates.size() > 0));
this.mNotificationService.setIsInForeground(this.mOnConversationUpdates.size() > 0);
@@ -2154,17 +2157,19 @@ public class XmppConnectionService extends Service {
}
}
- public void setOnShowErrorToastListener(OnShowErrorToast onShowErrorToast) {
- synchronized (this) {
+ public void setOnShowErrorToastListener(OnShowErrorToast listener) {
+ synchronized (LISTENER_LOCK) {
if (checkListeners()) {
switchToForeground();
}
- this.mOnShowErrorToasts.add(onShowErrorToast);
+ if (!this.mOnShowErrorToasts.add(listener)) {
+ Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnShowErrorToastListener");
+ }
}
}
public void removeOnShowErrorToastListener(OnShowErrorToast onShowErrorToast) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
this.mOnShowErrorToasts.remove(onShowErrorToast);
if (checkListeners()) {
switchToBackground();
@@ -2173,16 +2178,18 @@ public class XmppConnectionService extends Service {
}
public void setOnAccountListChangedListener(OnAccountUpdate listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
if (checkListeners()) {
switchToForeground();
}
- this.mOnAccountUpdates.add(listener);
+ if (!this.mOnAccountUpdates.add(listener)) {
+ Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnAccountListChangedtListener");
+ }
}
}
public void removeOnAccountListChangedListener(OnAccountUpdate listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
this.mOnAccountUpdates.remove(listener);
if (checkListeners()) {
switchToBackground();
@@ -2191,16 +2198,18 @@ public class XmppConnectionService extends Service {
}
public void setOnCaptchaRequestedListener(OnCaptchaRequested listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
if (checkListeners()) {
switchToForeground();
}
- this.mOnCaptchaRequested.add(listener);
+ if (!this.mOnCaptchaRequested.add(listener)) {
+ Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnCaptchaRequestListener");
+ }
}
}
public void removeOnCaptchaRequestedListener(OnCaptchaRequested listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
this.mOnCaptchaRequested.remove(listener);
if (checkListeners()) {
switchToBackground();
@@ -2209,16 +2218,18 @@ public class XmppConnectionService extends Service {
}
public void setOnRosterUpdateListener(final OnRosterUpdate listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
if (checkListeners()) {
switchToForeground();
}
- this.mOnRosterUpdates.add(listener);
+ if (!this.mOnRosterUpdates.add(listener)) {
+ Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnRosterUpdateListener");
+ }
}
}
public void removeOnRosterUpdateListener(final OnRosterUpdate listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
this.mOnRosterUpdates.remove(listener);
if (checkListeners()) {
switchToBackground();
@@ -2227,16 +2238,18 @@ public class XmppConnectionService extends Service {
}
public void setOnUpdateBlocklistListener(final OnUpdateBlocklist listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
if (checkListeners()) {
switchToForeground();
}
- this.mOnUpdateBlocklist.add(listener);
+ if (!this.mOnUpdateBlocklist.add(listener)) {
+ Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnUpdateBlocklistListener");
+ }
}
}
public void removeOnUpdateBlocklistListener(final OnUpdateBlocklist listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
this.mOnUpdateBlocklist.remove(listener);
if (checkListeners()) {
switchToBackground();
@@ -2245,16 +2258,18 @@ public class XmppConnectionService extends Service {
}
public void setOnKeyStatusUpdatedListener(final OnKeyStatusUpdated listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
if (checkListeners()) {
switchToForeground();
}
- this.mOnKeyStatusUpdated.add(listener);
+ if (!this.mOnKeyStatusUpdated.add(listener)) {
+ Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnKeyStatusUpdateListener");
+ }
}
}
public void removeOnNewKeysAvailableListener(final OnKeyStatusUpdated listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
this.mOnKeyStatusUpdated.remove(listener);
if (checkListeners()) {
switchToBackground();
@@ -2263,16 +2278,18 @@ public class XmppConnectionService extends Service {
}
public void setOnMucRosterUpdateListener(OnMucRosterUpdate listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
if (checkListeners()) {
switchToForeground();
}
- this.mOnMucRosterUpdate.add(listener);
+ if (!this.mOnMucRosterUpdate.add(listener)) {
+ Log.w(Config.LOGTAG, listener.getClass().getName() + " is already registered as OnMucRosterListener");
+ }
}
}
public void removeOnMucRosterUpdateListener(final OnMucRosterUpdate listener) {
- synchronized (this) {
+ synchronized (LISTENER_LOCK) {
this.mOnMucRosterUpdate.remove(listener);
if (checkListeners()) {
switchToBackground();
@@ -2285,6 +2302,7 @@ public class XmppConnectionService extends Service {
&& this.mOnConversationUpdates.size() == 0
&& this.mOnRosterUpdates.size() == 0
&& this.mOnCaptchaRequested.size() == 0
+ && this.mOnMucRosterUpdate.size() == 0
&& this.mOnUpdateBlocklist.size() == 0
&& this.mOnShowErrorToasts.size() == 0
&& this.mOnKeyStatusUpdated.size() == 0);
@@ -3553,58 +3571,73 @@ public class XmppConnectionService extends Service {
}
public void showErrorToastInUi(int resId) {
- for(OnShowErrorToast listener : this.mOnShowErrorToasts) {
- listener.onShowErrorToast(resId);
+ synchronized (LISTENER_LOCK) {
+ for (OnShowErrorToast listener : this.mOnShowErrorToasts) {
+ listener.onShowErrorToast(resId);
+ }
}
}
public void updateConversationUi() {
- for(OnConversationUpdate listener : this.mOnConversationUpdates) {
- listener.onConversationUpdate();
+ synchronized (LISTENER_LOCK) {
+ for (OnConversationUpdate listener : this.mOnConversationUpdates) {
+ listener.onConversationUpdate();
+ }
}
}
public void updateAccountUi() {
- for(OnAccountUpdate listener : this.mOnAccountUpdates) {
- listener.onAccountUpdate();
+ synchronized (LISTENER_LOCK) {
+ for (OnAccountUpdate listener : this.mOnAccountUpdates) {
+ listener.onAccountUpdate();
+ }
}
}
public void updateRosterUi() {
- for(OnRosterUpdate listener : this.mOnRosterUpdates) {
- listener.onRosterUpdate();
+ synchronized (LISTENER_LOCK) {
+ for (OnRosterUpdate listener : this.mOnRosterUpdates) {
+ listener.onRosterUpdate();
+ }
}
}
public boolean displayCaptchaRequest(Account account, String id, Data data, Bitmap captcha) {
- if (mOnCaptchaRequested.size() > 0) {
- DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
- Bitmap scaled = Bitmap.createScaledBitmap(captcha, (int) (captcha.getWidth() * metrics.scaledDensity),
- (int) (captcha.getHeight() * metrics.scaledDensity), false);
-
- for(OnCaptchaRequested listener : this.mOnCaptchaRequested) {
- listener.onCaptchaRequested(account, id, data, scaled);
+ synchronized (LISTENER_LOCK) {
+ if (mOnCaptchaRequested.size() > 0) {
+ DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
+ Bitmap scaled = Bitmap.createScaledBitmap(captcha, (int) (captcha.getWidth() * metrics.scaledDensity),
+ (int) (captcha.getHeight() * metrics.scaledDensity), false);
+ for (OnCaptchaRequested listener : this.mOnCaptchaRequested) {
+ listener.onCaptchaRequested(account, id, data, scaled);
+ }
+ return true;
}
- return true;
+ return false;
}
- return false;
}
public void updateBlocklistUi(final OnUpdateBlocklist.Status status) {
- for(OnUpdateBlocklist listener : this.mOnUpdateBlocklist) {
- listener.OnUpdateBlocklist(status);
+ synchronized (LISTENER_LOCK) {
+ for (OnUpdateBlocklist listener : this.mOnUpdateBlocklist) {
+ listener.OnUpdateBlocklist(status);
+ }
}
}
public void updateMucRosterUi() {
- for(OnMucRosterUpdate listener : this.mOnMucRosterUpdate) {
- listener.onMucRosterUpdate();
+ synchronized (LISTENER_LOCK) {
+ for (OnMucRosterUpdate listener : this.mOnMucRosterUpdate) {
+ listener.onMucRosterUpdate();
+ }
}
}
public void keyStatusUpdated(AxolotlService.FetchStatus report) {
- for(OnKeyStatusUpdated listener : this.mOnKeyStatusUpdated) {
- listener.onKeyStatusUpdated(report);
+ synchronized (LISTENER_LOCK) {
+ for (OnKeyStatusUpdated listener : this.mOnKeyStatusUpdated) {
+ listener.onKeyStatusUpdated(report);
+ }
}
}
diff --git a/src/main/java/de/pixart/messenger/ui/XmppActivity.java b/src/main/java/de/pixart/messenger/ui/XmppActivity.java
index e52e3dbaf..f52b2d72c 100644
--- a/src/main/java/de/pixart/messenger/ui/XmppActivity.java
+++ b/src/main/java/de/pixart/messenger/ui/XmppActivity.java
@@ -61,7 +61,6 @@ 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;
@@ -99,7 +98,6 @@ public abstract class XmppActivity extends ActionBarActivity {
public XmppConnectionService xmppConnectionService;
public boolean xmppConnectionServiceBound = false;
- protected final AtomicBoolean registeredListeners = new AtomicBoolean(false);
protected int mColorRed;
protected int mColorWarningButton;
@@ -122,9 +120,7 @@ public abstract class XmppActivity extends ActionBarActivity {
XmppConnectionBinder binder = (XmppConnectionBinder) service;
xmppConnectionService = binder.getService();
xmppConnectionServiceBound = true;
- if (registeredListeners.compareAndSet(false, true)) {
- registerListeners();
- }
+ registerListeners();
invalidateOptionsMenu();
onBackendConnected();
}
@@ -228,9 +224,7 @@ public abstract class XmppActivity extends ActionBarActivity {
connectToBackend();
}
} else {
- if (registeredListeners.compareAndSet(false, true)) {
- this.registerListeners();
- }
+ this.registerListeners();
this.onBackendConnected();
}
}
@@ -246,9 +240,7 @@ public abstract class XmppActivity extends ActionBarActivity {
protected void onStop() {
super.onStop();
if (xmppConnectionServiceBound) {
- if (registeredListeners.compareAndSet(true, false)) {
- this.unregisterListeners();
- }
+ this.unregisterListeners();
unbindService(mConnection);
xmppConnectionServiceBound = false;
}