Search in sources :

Example 36 with ExtensionElement

use of org.jivesoftware.smack.packet.ExtensionElement in project xabber-android by redsolution.

the class CapabilitiesManager method isValid.

private boolean isValid(DiscoverInfo discoverInfo) {
    Set<DiscoverInfo.Identity> identities = new TreeSet<>(new Comparator<DiscoverInfo.Identity>() {

        private int compare(String string1, String string2) {
            return (string1 == null ? "" : string1).compareTo(string2 == null ? "" : string2);
        }

        @Override
        public int compare(DiscoverInfo.Identity identity1, DiscoverInfo.Identity identity2) {
            int result;
            result = compare(identity1.getCategory(), identity2.getCategory());
            if (result != 0) {
                return result;
            }
            result = compare(identity1.getType(), identity2.getType());
            if (result != 0) {
                return result;
            }
            result = compare(identity1.getLanguage(), identity2.getLanguage());
            if (result != 0) {
                return result;
            }
            result = compare(identity1.getName(), identity2.getName());
            if (result != 0) {
                return result;
            }
            return 0;
        }
    });
    for (DiscoverInfo.Identity identity : discoverInfo.getIdentities()) {
        if (!identities.add(identity)) {
            return false;
        }
    }
    Set<String> features = new HashSet<>();
    for (DiscoverInfo.Feature feature : discoverInfo.getFeatures()) {
        if (!features.add(feature.getVar())) {
            return false;
        }
    }
    Set<String> formTypes = new HashSet<>();
    for (ExtensionElement packetExtension : discoverInfo.getExtensions()) if (packetExtension instanceof DataForm) {
        DataForm dataForm = (DataForm) packetExtension;
        String formType = null;
        for (FormField formField : dataForm.getFields()) {
            if (FORM_TYPE.equals(formField.getVariable())) {
                for (String value : formField.getValues()) {
                    if (formType != null && !formType.equals(value)) {
                        return false;
                    }
                    formType = value;
                }
            }
        }
        if (!formTypes.add(formType)) {
            return false;
        }
    }
    return true;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) TreeSet(java.util.TreeSet) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) FormField(org.jivesoftware.smackx.xdata.FormField) HashSet(java.util.HashSet)

Example 37 with ExtensionElement

use of org.jivesoftware.smack.packet.ExtensionElement in project xabber-android by redsolution.

the class ChatStateManager method onPacket.

@Override
public void onPacket(ConnectionItem connection, final String bareAddress, Stanza packet) {
    if (!(connection instanceof AccountItem))
        return;
    final String resource = Jid.getResource(packet.getFrom());
    if (resource == null)
        return;
    final String account = ((AccountItem) connection).getAccount();
    if (packet instanceof Presence) {
        Presence presence = (Presence) packet;
        if (presence.getType() != Type.unavailable)
            return;
        chatStates.remove(account, bareAddress, resource);
        removeCallback(account, bareAddress, resource);
        supports.remove(account, bareAddress, resource);
    } else if (packet instanceof Message) {
        boolean support = false;
        for (ExtensionElement extension : packet.getExtensions()) if (extension instanceof ChatStateExtension) {
            removeCallback(account, bareAddress, resource);
            ChatState chatState = ((ChatStateExtension) extension).getChatState();
            chatStates.put(account, bareAddress, resource, chatState);
            if (chatState != ChatState.active) {
                Runnable runnable = new Runnable() {

                    @Override
                    public void run() {
                        if (this != stateCleaners.get(account, bareAddress, resource))
                            return;
                        chatStates.remove(account, bareAddress, resource);
                        removeCallback(account, bareAddress, resource);
                        RosterManager.getInstance().onContactChanged(account, bareAddress);
                    }
                };
                handler.postDelayed(runnable, REMOVE_STATE_DELAY);
                stateCleaners.put(account, bareAddress, resource, runnable);
            }
            RosterManager.getInstance().onContactChanged(account, bareAddress);
            support = true;
            break;
        }
        Message message = (Message) packet;
        if (message.getType() != Message.Type.chat && message.getType() != Message.Type.groupchat)
            return;
        if (support)
            supports.put(account, bareAddress, resource, true);
        else if (supports.get(account, bareAddress, resource) == null)
            // Disable only if there no information about support.
            supports.put(account, bareAddress, resource, false);
    }
}
Also used : ChatState(org.jivesoftware.smackx.chatstates.ChatState) Message(org.jivesoftware.smack.packet.Message) ChatStateExtension(org.jivesoftware.smackx.chatstates.packet.ChatStateExtension) AccountItem(com.xabber.android.data.account.AccountItem) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) Presence(org.jivesoftware.smack.packet.Presence)

Example 38 with ExtensionElement

use of org.jivesoftware.smack.packet.ExtensionElement in project xabber-android by redsolution.

the class AvatarManager method onPacket.

@Override
public void onPacket(ConnectionItem connection, String bareAddress, Stanza packet) {
    if (!(packet instanceof Presence) || bareAddress == null) {
        return;
    }
    if (!(connection instanceof AccountItem)) {
        return;
    }
    String account = ((AccountItem) connection).getAccount();
    Presence presence = (Presence) packet;
    if (presence.getType() == Presence.Type.error) {
        return;
    }
    for (ExtensionElement packetExtension : presence.getExtensions()) {
        if (packetExtension instanceof VCardUpdate) {
            VCardUpdate vCardUpdate = (VCardUpdate) packetExtension;
            if (vCardUpdate.isValid() && vCardUpdate.isPhotoReady()) {
                onPhotoReady(account, bareAddress, vCardUpdate);
            }
        }
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) Presence(org.jivesoftware.smack.packet.Presence) VCardUpdate(com.xabber.xmpp.avatar.VCardUpdate)

Aggregations

ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)38 Message (org.jivesoftware.smack.packet.Message)12 AccountItem (com.xabber.android.data.account.AccountItem)11 Test (org.junit.Test)9 XmlPullParser (org.xmlpull.v1.XmlPullParser)9 AccountJid (com.xabber.android.data.entity.AccountJid)6 UserJid (com.xabber.android.data.entity.UserJid)4 Presence (org.jivesoftware.smack.packet.Presence)4 Stanza (org.jivesoftware.smack.packet.Stanza)4 NetworkException (com.xabber.android.data.NetworkException)3 LinkedList (java.util.LinkedList)3 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)3 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)3 DataForm (org.jivesoftware.smackx.xdata.packet.DataForm)3 MucPrivateChatNotification (com.xabber.android.data.message.chat.MucPrivateChatNotification)2 Feature (com.xabber.xmpp.ssn.Feature)2 TreeSet (java.util.TreeSet)2 XMPPConnection (org.jivesoftware.smack.XMPPConnection)2 AMPExtension (org.jivesoftware.smackx.amp.packet.AMPExtension)2 AMPExtensionProvider (org.jivesoftware.smackx.amp.provider.AMPExtensionProvider)2