Search in sources :

Example 11 with Stanza

use of com.googlecode.asmack.Stanza in project AsmackService by rtreffer.

the class PresenceBroadcastReceiver method onReceive.

/**
     * Receive a single stanza intent, check for xmpp <presence/> and
     * store it to the database.
     * @param Context The current application context.
     * @param intent The event Intent.
     */
@Override
public void onReceive(Context context, Intent intent) {
    Stanza stanza = intent.getParcelableExtra("stanza");
    if (!"presence".equals(stanza.getName())) {
        return;
    }
    String accountJid = XMPPUtils.getBareJid(stanza.getVia());
    String jid = XMPPUtils.getBareJid(stanza.getAttribute("from").getValue());
    StatusUpdate update = null;
    if (stanza.getAttribute("type") != null) {
        if ("unavailable".equals(stanza.getAttribute("type").getValue())) {
            update = mapper.getStatusUpdate(accountJid, jid);
            update.setPresence(Presence.OFFLINE);
            mapper.persist(update);
        }
    }
    try {
        update = mapper.getStatusUpdate(accountJid, jid);
        update.setPresence(Presence.AVAILABLE);
        Node node = stanza.getDocumentNode();
        Node show = XMLUtils.getFirstChild(node, null, "show");
        if (show != null) {
            String presence = show.getTextContent();
            if ("away".equals(presence)) {
                update.setPresence(Presence.AWAY);
            }
            if ("dnd".equals(presence)) {
                update.setPresence(Presence.DO_NOT_DISTURB);
            }
        }
        Node status = XMLUtils.getFirstChild(node, null, "status");
        if (status != null) {
            update.setStatus(status.getTextContent());
        }
        mapper.persist(update);
    } catch (XmppMalformedException e) {
        e.printStackTrace();
    }
}
Also used : XmppMalformedException(com.googlecode.asmack.XmppMalformedException) Stanza(com.googlecode.asmack.Stanza) Node(org.w3c.dom.Node)

Aggregations

Stanza (com.googlecode.asmack.Stanza)11 XmppMalformedException (com.googlecode.asmack.XmppMalformedException)5 Node (org.w3c.dom.Node)5 XmppException (com.googlecode.asmack.XmppException)3 Attribute (com.googlecode.asmack.Attribute)2 BroadcastReceiver (android.content.BroadcastReceiver)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 RemoteException (android.os.RemoteException)1 XmppIdentity (com.googlecode.asmack.XmppIdentity)1 XmppTransportException (com.googlecode.asmack.connection.XmppTransportException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 SAXException (org.xml.sax.SAXException)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1