use of org.jivesoftware.resource.Default in project Spark by igniterealtime.
the class ContactList method addSubscriptionListener.
public void addSubscriptionListener() {
// Sometimes, presence changes happen in rapid succession (for instance, when initially connecting). To avoid
// having a lot of UI-updates (which are costly), this queue is used to create a short buffer, allowing us to
// group UI updates in batches.
final ConcurrentLinkedQueue<Presence> presenceBuffer = new ConcurrentLinkedQueue<>();
final long bufferTimeMS = 500;
final StanzaListener subscribeListener = stanza -> {
final Presence presence = (Presence) stanza;
final Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
final RosterEntry entry = roster.getEntry(presence.getFrom());
switch(presence.getType()) {
case subscribe:
// Someone else wants to subscribe to our presence. Ask user for approval
SwingUtilities.invokeLater(() -> {
try {
subscriptionRequest(presence.getFrom());
} catch (SmackException.NotConnectedException e) {
Log.warning("Unable to process subscription request from: " + presence.getFrom(), e);
}
});
break;
case unsubscribe:
// Someone else is removing their subscription to our presence (we're removed from their roster).
if (entry != null) {
try {
removeContactItem(presence.getFrom());
roster.removeEntry(entry);
} catch (XMPPException | SmackException e) {
Presence unsub = new Presence(Presence.Type.unsubscribed);
unsub.setTo(presence.getFrom());
try {
SparkManager.getConnection().sendStanza(unsub);
} catch (SmackException.NotConnectedException e1) {
Log.warning("Unable to unsubscribe from " + unsub.getTo(), e1);
}
Log.error(e);
}
}
break;
case subscribed:
// Someone else approved our request to be subscribed to their presence information.
final String jid = XmppStringUtils.parseBareJid(presence.getFrom());
final ContactItem item = getContactItemByJID(jid);
// If item is not in the Contact List, add them.
if (item == null && entry != null) {
final ContactItem newItem = UIComponentRegistry.createContactItem(entry.getName(), null, jid);
moveToOffline(newItem);
offlineGroup.fireContactGroupUpdated();
}
break;
case unsubscribed:
// Someone is telling us that we're no longer subscribed to their presence information.
SwingUtilities.invokeLater(() -> {
if (entry != null) {
try {
removeContactItem(presence.getFrom());
roster.removeEntry(entry);
} catch (XMPPException | SmackException e) {
Log.error(e);
}
}
removeContactItem(XmppStringUtils.parseBareJid(presence.getFrom()));
});
break;
default:
// Any other presence updates. These are likely regular presence changes, not subscription-state changes.
presenceBuffer.add(presence);
TaskEngine.getInstance().schedule(new TimerTask() {
@Override
public void run() {
SwingUtilities.invokeLater(() -> {
final Iterator<Presence> iterator = presenceBuffer.iterator();
while (iterator.hasNext()) {
final Presence presence = iterator.next();
try {
updateUserPresence(presence);
} catch (Exception e) {
Log.warning("Unable to process this presence update that was received: " + presence, e);
} finally {
iterator.remove();
}
}
});
}
}, bufferTimeMS);
break;
}
};
SparkManager.getConnection().addAsyncStanzaListener(subscribeListener, new StanzaTypeFilter(Presence.class));
}
Aggregations