Java Code Examples for org.jivesoftware.smack.packet.Presence#Type

The following examples show how to use org.jivesoftware.smack.packet.Presence#Type . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RosterHandler.java    From desktopclient-java with GNU General Public License v3.0 6 votes vote down vote up
public void onPresenceUpdate(JID jid, Presence.Type type, Optional<String> optStatus) {
    JID myJID = mClient.getOwnJID().orElse(null);
    if (myJID != null && myJID.equals(jid))
        // don't wanna see myself
        return;

    Contact contact = mModel.contacts().get(jid).orElse(null);
    if (contact == null) {
        LOGGER.info("can't find contact with jid: "+jid);
        return;
    }

    if (type == Presence.Type.available) {
        contact.setOnlineStatus(Contact.Online.YES);
    } else if (type == Presence.Type.unavailable) {
        contact.setOnlineStatus(Contact.Online.NO);
    }

    if (optStatus.isPresent())
        contact.setStatusText(optStatus.get());
}
 
Example 2
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 6 votes vote down vote up
public Presence.Type getStatusFromContact(String name) {
    try {
        HashMap<Jid, Presence.Type> buddies = getContacts();

        for (Map.Entry<Jid, Presence.Type> pair : buddies.entrySet()) {
            if (XMPPUtils.fromJIDToUserName(pair.getKey().toString()).equals(name)) {
                return pair.getValue();
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return Presence.Type.unavailable;
}
 
Example 3
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
public boolean hasContactSubscriptionPending(Jid jid)
        throws SmackException.NotLoggedInException, InterruptedException,
        SmackException.NotConnectedException {
    HashMap<Jid, Presence.Type> buddies = getContactsWithSubscriptionPending();
    return buddies.containsKey(jid);

}
 
Example 4
Source File: Client.java    From desktopclient-java with GNU General Public License v3.0 5 votes vote down vote up
public void sendPresenceSubscription(JID jid, PresenceCommand command) {
    LOGGER.info("to: "+jid+ ", command: "+command);
    Presence.Type type = null;
    switch(command) {
        case REQUEST: type = Presence.Type.subscribe; break;
        case GRANT: type = Presence.Type.subscribed; break;
        case DENY: type = Presence.Type.unsubscribed; break;
    }
    Presence presence = new Presence(type);
    presence.setTo(jid.toBareSmack());
    this.sendPacket(presence);
}
 
Example 5
Source File: ClientManagerImpl.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * helper method to trigger a presence update even if the server does not send a presence packet itself (e.g. entering a test but no other buddies are online)
 * 
 * @param username
 */
@Override
public void sendPresenceEvent(final Presence.Type type, final String username) {
    final Presence presence = new Presence(type);
    presence.setTo(username);
    final GenericEventListener listener = listeners.get(username);
    if (listener != null) {
        listener.event(new InstantMessagingEvent(presence, "presence"));
    }
}
 
Example 6
Source File: ClientManagerImpl.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * helper method to trigger a presence update even if the server does not send a presence packet itself (e.g. entering a test but no other buddies are online)
 * 
 * @param username
 */
@Override
public void sendPresenceEvent(final Presence.Type type, final String username) {
    final Presence presence = new Presence(type);
    presence.setTo(username);
    final GenericEventListener listener = listeners.get(username);
    if (listener != null) {
        listener.event(new InstantMessagingEvent(presence, "presence"));
    }
}
 
Example 7
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
public HashMap<Jid, Presence.Type> getContactsWithSubscriptionPending()
        throws SmackException.NotLoggedInException, InterruptedException,
        SmackException.NotConnectedException {
    Roster roster = Roster.getInstanceFor(XMPPSession.getInstance().getXMPPConnection());
    if (!roster.isLoaded()) {
        roster.reloadAndWait();
    }

    String groupName = "Buddies";

    RosterGroup group = roster.getGroup(groupName);

    if (group == null) {
        roster.createGroup(groupName);
        group = roster.getGroup(groupName);
    }

    HashMap<Jid, Presence.Type> buddiesPending = new HashMap<>();

    List<RosterEntry> entries = group.getEntries();
    for (RosterEntry entry : entries) {
        if (entry.isSubscriptionPending()) {
            BareJid jid = entry.getJid();
            Presence.Type status = roster.getPresence(jid).getType();
            buddiesPending.put(jid, status);
        }
    }

    return buddiesPending;
}
 
Example 8
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
public HashMap<Jid, Presence.Type> getContacts()
        throws SmackException.NotLoggedInException, InterruptedException,
        SmackException.NotConnectedException {
    Roster roster = Roster.getInstanceFor(XMPPSession.getInstance().getXMPPConnection());
    if (!roster.isLoaded()) {
        roster.reloadAndWait();
    }

    String groupName = "Buddies";

    RosterGroup group = roster.getGroup(groupName);

    if (group == null) {
        roster.createGroup(groupName);
        group = roster.getGroup(groupName);
    }

    HashMap<Jid, Presence.Type> buddies = new HashMap<>();

    List<RosterEntry> entries = group.getEntries();
    for (RosterEntry entry : entries) {
        BareJid jid = entry.getJid();
        Presence.Type status = roster.getPresence(jid).getType();
        buddies.put(jid, status);
    }

    return buddies;
}
 
Example 9
Source File: RoomManager.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
public void loadRosterContactsChats() throws SmackException.NotLoggedInException, InterruptedException, SmackException.NotConnectedException {
    try {
        HashMap<Jid, Presence.Type> buddies = RosterManager.getInstance().getContacts();
        for (Map.Entry pair : buddies.entrySet()) {
            String userJid = pair.getKey().toString();
            RoomsListManager.getInstance().createChatIfNotExists(userJid, true);
        }
    } finally {
        mListener.onRoomsLoaded();
    }
}
 
Example 10
Source File: ChatActivity.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
private boolean isChatWithContact() {
    try {
        HashMap<Jid, Presence.Type> buddies = RosterManager.getInstance().getContacts();
        for (Map.Entry pair : buddies.entrySet()) {
            if (mChat.getJid().equals(pair.getKey().toString())) {
                return true;
            }
        }
    } catch (SmackException.NotLoggedInException | InterruptedException | SmackException.NotConnectedException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 11
Source File: PresenceTypeFilter.java    From Smack with Apache License 2.0 4 votes vote down vote up
private PresenceTypeFilter(Presence.Type type) {
    super(Presence.class);
    this.type = Objects.requireNonNull(type, "type must not be null");
}
 
Example 12
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 4 votes vote down vote up
public boolean isContact(Jid jid)
        throws SmackException.NotLoggedInException, InterruptedException,
        SmackException.NotConnectedException {
    HashMap<Jid, Presence.Type> buddies = getContacts();
    return buddies.containsKey(jid);
}
 
Example 13
Source File: PacketParserUtils.java    From Smack with Apache License 2.0 4 votes vote down vote up
/**
 * Parses a presence packet.
 *
 * @param parser the XML parser, positioned at the start of a presence packet.
 * @param outerXmlEnvironment the outer XML environment (optional).
 * @return a Presence packet.
 * @throws IOException if an I/O error occurred.
 * @throws XmlPullParserException if an error in the XML parser occurred.
 * @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
 */
public static Presence parsePresence(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    ParserUtils.assertAtStartTag(parser);
    final int initialDepth = parser.getDepth();
    XmlEnvironment presenceXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);

    PresenceBuilder presence = parseCommonStanzaAttributes(
                    stanzaId -> StanzaBuilder.buildPresence(stanzaId), parser, outerXmlEnvironment);

    Presence.Type type = Presence.Type.available;
    String typeString = parser.getAttributeValue("", "type");
    if (typeString != null && !typeString.equals("")) {
        type = Presence.Type.fromString(typeString);
    }

    presence.ofType(type);

    // Parse sub-elements
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch (eventType) {
        case START_ELEMENT:
            String elementName = parser.getName();
            String namespace = parser.getNamespace();
            switch (elementName) {
            case "status":
                presence.setStatus(parser.nextText());
                break;
            case "priority":
                Byte priority = ParserUtils.getByteAttributeFromNextText(parser);
                presence.setPriority(priority);
                break;
            case "show":
                String modeText = parser.nextText();
                if (StringUtils.isNotEmpty(modeText)) {
                    presence.setMode(Presence.Mode.fromString(modeText));
                } else {
                    // Some implementations send presence stanzas with a
                    // '<show />' element, which is a invalid XMPP presence
                    // stanza according to RFC 6121 4.7.2.1
                    LOGGER.warning("Empty or null mode text in presence show element form "
                                    + presence
                                    + "' which is invalid according to RFC6121 4.7.2.1");
                }
                break;
            case "error":
                presence.setError(parseError(parser, presenceXmlEnvironment));
                break;
            default:
            // Otherwise, it must be a packet extension.
                // Be extra robust: Skip PacketExtensions that cause Exceptions, instead of
                // failing completely here. See SMACK-390 for more information.
                try {
                    ExtensionElement extensionElement = parseExtensionElement(elementName, namespace, parser, presenceXmlEnvironment);
                    presence.addExtension(extensionElement);
                } catch (Exception e) {
                    LOGGER.log(Level.WARNING, "Failed to parse extension element in Presence stanza: " + presence, e);
                }
                break;
            }
            break;
        case END_ELEMENT:
            if (parser.getDepth() == initialDepth) {
                break outerloop;
            }
            break;
        default:
            // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
            break;
        }
    }

    return presence.build();
}
 
Example 14
Source File: XMPPManager.java    From Yahala-Messenger with MIT License 4 votes vote down vote up
public PresenceTypeFilter(Presence.Type type) {
    this.type = type;
}
 
Example 15
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * change jabber status. Example: sendPresencePacket(Presence.Type.AVAILABLE, "at meeting...", 1, Presence.Mode.AWAY);
 * 
 * @param type
 * @param status
 * @param priority
 * @param mode
 */
public void sendPresence(final Presence.Type type, String status, final int priority, final Presence.Mode mode) {
    // get rid of "&" because they break xml packages!
    if (status != null) {
        status = status.replaceAll("&", "&amp;");
    }
    if (connection == null || !connection.isConnected()) {
        return;
    }
    if (collaborationDisabled) {
        return;
    }

    setStatus(mode);
    final Presence presence = new Presence(type);
    if (status == null) {
        if (mode == Presence.Mode.available) {
            status = InstantMessagingConstants.PRESENCE_MODE_AVAILABLE;
        } else if (mode == Presence.Mode.away) {
            status = InstantMessagingConstants.PRESENCE_MODE_AWAY;
        } else if (mode == Presence.Mode.chat) {
            status = InstantMessagingConstants.PRESENCE_MODE_CHAT;
        } else if (mode == Presence.Mode.dnd) {
            status = InstantMessagingConstants.PRESENCE_MODE_DND;
        } else if (mode == Presence.Mode.xa) {
            status = InstantMessagingConstants.PRESENCE_MODE_XAWAY;
        }
        presence.setStatus(status);
    } else {
        presence.setStatus(status);
    }
    setStatusMsg(presence.getStatus());
    // setting prio when type == unavailable causes error on IM server
    if (presence.getType() == Presence.Type.available) {
        presence.setPriority(priority);
    }
    if (mode != null) {
        presence.setMode(mode);
    }
    try {
        connection.sendPacket(presence);
    } catch (final RuntimeException ex) {
        log.warn("Error while trying to send Instant Messaging packet for user: " + userInfo.getUsername() + " .Errormessage: ", ex);
    }
}
 
Example 16
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * change jabber status. Example: sendPresencePacket(Presence.Type.AVAILABLE, "at meeting...", 1, Presence.Mode.AWAY);
 * 
 * @param type
 * @param status
 * @param priority
 * @param mode
 */
public void sendPresence(final Presence.Type type, String status, final int priority, final Presence.Mode mode) {
    // get rid of "&" because they break xml packages!
    if (status != null) {
        status = status.replaceAll("&", "&amp;");
    }
    if (connection == null || !connection.isConnected()) {
        return;
    }
    if (collaborationDisabled) {
        return;
    }

    setStatus(mode);
    final Presence presence = new Presence(type);
    if (status == null) {
        if (mode == Presence.Mode.available) {
            status = InstantMessagingConstants.PRESENCE_MODE_AVAILABLE;
        } else if (mode == Presence.Mode.away) {
            status = InstantMessagingConstants.PRESENCE_MODE_AWAY;
        } else if (mode == Presence.Mode.chat) {
            status = InstantMessagingConstants.PRESENCE_MODE_CHAT;
        } else if (mode == Presence.Mode.dnd) {
            status = InstantMessagingConstants.PRESENCE_MODE_DND;
        } else if (mode == Presence.Mode.xa) {
            status = InstantMessagingConstants.PRESENCE_MODE_XAWAY;
        }
        presence.setStatus(status);
    } else {
        presence.setStatus(status);
    }
    setStatusMsg(presence.getStatus());
    // setting prio when type == unavailable causes error on IM server
    if (presence.getType() == Presence.Type.available) {
        presence.setPriority(priority);
    }
    if (mode != null) {
        presence.setMode(mode);
    }
    try {
        connection.sendPacket(presence);
    } catch (final RuntimeException ex) {
        log.warn("Error while trying to send Instant Messaging packet for user: " + userInfo.getUsername() + " .Errormessage: ", ex);
    }
}
 
Example 17
Source File: User.java    From mangosta-android with Apache License 2.0 4 votes vote down vote up
public void setConnectionStatus(Presence.Type connectionStatus) {
    this.connectionStatus = connectionStatus;
}
 
Example 18
Source File: SubscriptionHandler.java    From saros with GNU General Public License v2.0 4 votes vote down vote up
private void sendPresence(Presence.Type type, String to) {
  Presence presence = new Presence(type);
  presence.setTo(to);
  presence.setFrom(connection.getUser());
  connection.sendPacket(presence);
}
 
Example 19
Source File: ClientManager.java    From olat with Apache License 2.0 2 votes vote down vote up
/**
 * helper method to trigger a presence update even if the server does not send a presence packet itself (e.g. entering a test but no other buddies are online)
 * 
 * @param username
 */
public void sendPresenceEvent(Presence.Type type, String username);
 
Example 20
Source File: ClientManager.java    From olat with Apache License 2.0 2 votes vote down vote up
/**
 * helper method to trigger a presence update even if the server does not send a presence packet itself (e.g. entering a test but no other buddies are online)
 * 
 * @param username
 */
public void sendPresenceEvent(Presence.Type type, String username);