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();
}
}
Aggregations