1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-26 00:02:06 +01:00

Fix 260c5539b ("Add support for XEP-0118: UserTune")

Fix a bug in the EqualsBuilder usage in UserTuneElement. Properly
synchronize listeners using CopyOnWriteSet. Make methods and fields
static where sensible and possible. Make
disableUserTuneNotifications() public (why was it private?). And a few
other minor fixes.
This commit is contained in:
Florian Schmaus 2019-10-17 12:25:09 +02:00
parent 260c5539b5
commit 926c5892ad
3 changed files with 23 additions and 24 deletions

View file

@ -16,10 +16,11 @@
*/ */
package org.jivesoftware.smackx.usertune; package org.jivesoftware.smackx.usertune;
import java.util.HashSet; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import org.jivesoftware.smack.AsyncButOrdered; import org.jivesoftware.smack.AsyncButOrdered;
import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.Manager;
@ -28,6 +29,7 @@ import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.SmackException.NotLoggedInException; import org.jivesoftware.smack.SmackException.NotLoggedInException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.pep.PepListener; import org.jivesoftware.smackx.pep.PepListener;
@ -64,10 +66,12 @@ public final class UserTuneManager extends Manager {
private static final Map<XMPPConnection, UserTuneManager> INSTANCES = new WeakHashMap<>(); private static final Map<XMPPConnection, UserTuneManager> INSTANCES = new WeakHashMap<>();
private final Set<UserTuneListener> userTuneListeners = new HashSet<>(); private static boolean ENABLE_USER_TUNE_NOTIFICATIONS_BY_DEFAULT = true;
private final Set<UserTuneListener> userTuneListeners = new CopyOnWriteArraySet<>();
private final AsyncButOrdered<BareJid> asyncButOrdered = new AsyncButOrdered<>(); private final AsyncButOrdered<BareJid> asyncButOrdered = new AsyncButOrdered<>();
private final ServiceDiscoveryManager serviceDiscoveryManager;
private final PepManager pepManager; private final PepManager pepManager;
private boolean ENABLE_USER_TUNE_NOTIFICATIONS_BY_DEFAULT = true;
public static synchronized UserTuneManager getInstanceFor(XMPPConnection connection) throws NotLoggedInException { public static synchronized UserTuneManager getInstanceFor(XMPPConnection connection) throws NotLoggedInException {
UserTuneManager manager = INSTANCES.get(connection); UserTuneManager manager = INSTANCES.get(connection);
@ -90,34 +94,34 @@ public final class UserTuneManager extends Manager {
final BareJid contact = from.asBareJid(); final BareJid contact = from.asBareJid();
asyncButOrdered.performAsyncButOrdered(contact, () -> { asyncButOrdered.performAsyncButOrdered(contact, () -> {
ItemsExtension items = (ItemsExtension) event.getExtensions().get(0); ItemsExtension itemsExtension = (ItemsExtension) event.getEvent();
PayloadItem<?> payload = (PayloadItem) items.getItems().get(0); List<ExtensionElement> items = itemsExtension.getExtensions();
UserTuneElement tune = (UserTuneElement) payload.getPayload(); @SuppressWarnings("unchecked")
PayloadItem<UserTuneElement> payload = (PayloadItem<UserTuneElement>) items.get(0);
UserTuneElement tune = payload.getPayload();
for (UserTuneListener listener : userTuneListeners) { for (UserTuneListener listener : userTuneListeners) {
synchronized (userTuneListeners) {
listener.onUserTuneUpdated(contact, message, tune); listener.onUserTuneUpdated(contact, message, tune);
} }
}
}); });
} }
}); });
serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
if (ENABLE_USER_TUNE_NOTIFICATIONS_BY_DEFAULT) { if (ENABLE_USER_TUNE_NOTIFICATIONS_BY_DEFAULT) {
enableUserTuneNotifications(); enableUserTuneNotifications();
} }
} }
public void setUserTuneNotificationsEnabledByDefault(boolean bool) { public static void setUserTuneNotificationsEnabledByDefault(boolean bool) {
ENABLE_USER_TUNE_NOTIFICATIONS_BY_DEFAULT = bool; ENABLE_USER_TUNE_NOTIFICATIONS_BY_DEFAULT = bool;
} }
public void enableUserTuneNotifications() { public void enableUserTuneNotifications() {
ServiceDiscoveryManager.getInstanceFor(connection()).addFeature(USERTUNE_NOTIFY); serviceDiscoveryManager.addFeature(USERTUNE_NOTIFY);
} }
@SuppressWarnings("unused") public void disableUserTuneNotifications() {
private void disableUserTuneNotifications() { serviceDiscoveryManager.removeFeature(USERTUNE_NOTIFY);
ServiceDiscoveryManager.getInstanceFor(connection()).removeFeature(USERTUNE_NOTIFY);
} }
public void clearUserTune() throws NotLoggedInException, NotALeafNodeException, NoResponseException, NotConnectedException, XMPPErrorException, InterruptedException { public void clearUserTune() throws NotLoggedInException, NotALeafNodeException, NoResponseException, NotConnectedException, XMPPErrorException, InterruptedException {
@ -129,15 +133,11 @@ public final class UserTuneManager extends Manager {
pepManager.publish(USERTUNE_NODE, new PayloadItem<>(userTuneElement)); pepManager.publish(USERTUNE_NODE, new PayloadItem<>(userTuneElement));
} }
public void addUserTuneListener(UserTuneListener listener) { public boolean addUserTuneListener(UserTuneListener listener) {
synchronized (userTuneListeners) { return userTuneListeners.add(listener);
userTuneListeners.add(listener);
}
} }
public void removeUserTuneListener(UserTuneListener listener) { public boolean removeUserTuneListener(UserTuneListener listener) {
synchronized (userTuneListeners) { return userTuneListeners.remove(listener);
userTuneListeners.remove(listener);
}
} }
} }

View file

@ -143,7 +143,6 @@ public final class UserTuneElement implements ExtensionElement {
.append(length, otherTune.length) .append(length, otherTune.length)
.append(rating, otherTune.rating) .append(rating, otherTune.rating)
.append(source, otherTune.source) .append(source, otherTune.source)
.append(title, otherTune.source)
.append(title, otherTune.title) .append(title, otherTune.title)
.append(track, otherTune.track) .append(track, otherTune.track)
.append(uri, otherTune.uri)); .append(uri, otherTune.uri));