use of com.googlecode.asmack.Stanza in project AsmackService by rtreffer.
the class RosterResultReceiver method onReceive.
/**
* Receive a stanza intent, check for roster entries and write results to
* the result queue.
*/
public void onReceive(Context context, Intent intent) {
Stanza stanza = intent.getParcelableExtra("stanza");
if (stanza.getName() == null || stanza.getVia() == null || stanza.getAttribute("type") == null) {
return;
}
if (!"iq".equals(stanza.getName())) {
return;
}
if (!stanza.getVia().startsWith(account.name + "/")) {
return;
}
if (!"result".equals(stanza.getAttribute("type").getValue())) {
return;
}
try {
Node node = stanza.getDocumentNode();
Node roster = XMLUtils.getFirstChild(node, "jabber:iq:roster", "query");
if (roster == null) {
return;
}
rosterQueue.put(roster);
} catch (XmppMalformedException e) {
Log.w(TAG, "PLEASE REPORT", e);
} catch (InterruptedException e) {
Log.w(TAG, "PLEASE REPORT", e);
}
}
use of com.googlecode.asmack.Stanza in project AsmackService by rtreffer.
the class SyncAdapter method onPerformSync.
/**
* Perform a roster sync on a given account and a given content provider.
* @param account The xmpp account to be synced.
* @param extras SyncAdapter-specific parameters
* @param authority The authority of this sync request.
* @param provider A authority based ContentProvider for this sync.
* @param syncResult Sync error and result counters.
*/
@Override
public void onPerformSync(final Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
Log.d(TAG, "Start Roster Sync");
final ArrayBlockingQueue<Node> rosterQueue = new ArrayBlockingQueue<Node>(1);
BroadcastReceiver receiver = new RosterResultReceiver(account, rosterQueue);
applicationContext.registerReceiver(receiver, new IntentFilter(XmppTransportService.XMPP_STANZA_INTENT));
try {
bindService();
if (!waitForService()) {
return;
}
if (!waitForServiceBind(account.name)) {
return;
}
Stanza stanza = getRosterRequest(account);
if (!sendWithRetry(stanza)) {
syncResult.stats.numIoExceptions++;
return;
}
Node roster = rosterQueue.poll(300, TimeUnit.SECONDS);
if (roster == null) {
return;
}
handleRosterResult(account, roster, provider);
} catch (InterruptedException e) {
Log.e(TAG, "Sync interrupted", e);
} finally {
applicationContext.unregisterReceiver(receiver);
unbindService();
}
}
use of com.googlecode.asmack.Stanza in project AsmackService by rtreffer.
the class SyncAdapter method getRosterRequest.
/**
* Create a stanza to retrieve the roster of a xmpp account.
* @param account The xmpp account.
* @return A roster iq stanza.
*/
private Stanza getRosterRequest(final Account account) {
long syncCount = getAndIncrementSyncCount(account);
List<Attribute> attributes = new ArrayList<Attribute>(3);
attributes.add(new Attribute("type", null, "get"));
attributes.add(new Attribute("id", null, "rostersync-" + Long.toHexString(syncCount)));
Stanza stanza = new Stanza("iq", "", account.name, "<iq><query xmlns='jabber:iq:roster'/></iq>", attributes);
String fullJid = null;
try {
fullJid = service.getFullJidByBare(account.name);
} catch (RemoteException e1) {
e1.printStackTrace();
}
stanza.addAttribute(new Attribute("from", null, fullJid));
return stanza;
}
use of com.googlecode.asmack.Stanza in project AsmackService by rtreffer.
the class DiscoReceiver method onReceive.
/**
* Called on incoming stanzas and replies on service discovery requests.
* @param context The current context.
* @param intent The stanza intent.
*/
@Override
public void onReceive(Context context, Intent intent) {
Stanza stanza = intent.getParcelableExtra("stanza");
// only accept IQ stanzas
if (!"iq".equals(stanza.getName())) {
return;
}
// of type="get"
if (!"get".equals(stanza.getAttributeValue("type"))) {
return;
}
// from / to / id are mandatory
Attribute from = stanza.getAttribute("from");
Attribute to = stanza.getAttribute("to");
Attribute id = stanza.getAttribute("id");
if (id == null || from == null || to == null) {
return;
}
try {
Node node = stanza.getDocumentNode();
Node query = XMLUtils.getFirstChild(node, "http://jabber.org/protocol/disco#info", "query");
if (query == null || !query.hasAttributes()) {
return;
}
Node discoAttributeNode = query.getAttributes().getNamedItem("node");
String discoNode = null;
if (discoAttributeNode != null) {
discoNode = discoAttributeNode.getTextContent();
}
// we got a disco, reply
StringBuilder payload = new StringBuilder("<iq type='result'");
payload.append(" from='");
payload.append(XMLUtils.xmlEscape(to.getValue()));
payload.append("' to='");
payload.append(XMLUtils.xmlEscape(from.getValue()));
payload.append("'>");
payload.append("<query xmlns='");
payload.append("http://jabber.org/protocol/disco#info");
if (discoNode != null) {
payload.append("' node='");
payload.append(XMLUtils.xmlEscape(discoNode));
}
payload.append("'>");
String myJid = XMPPUtils.getBareJid(to.getValue());
for (XmppIdentity identity : Database.getIdentities(context, myJid, null)) {
payload.append("<identity");
if (identity.getCategory().length() > 0) {
payload.append(" category='");
payload.append(XMLUtils.xmlEscape(identity.getCategory()));
payload.append('\'');
}
if (identity.getType().length() > 0) {
payload.append(" type='");
payload.append(XMLUtils.xmlEscape(identity.getType()));
payload.append('\'');
}
if (identity.getLang().length() > 0) {
payload.append(" lang='");
payload.append(XMLUtils.xmlEscape(identity.getLang()));
payload.append('\'');
}
if (identity.getName().length() > 0) {
payload.append(" name='");
payload.append(XMLUtils.xmlEscape(identity.getName()));
payload.append('\'');
}
payload.append("/>");
}
for (String feature : Database.getFeatures(context, myJid, null)) {
payload.append("<feature var='");
payload.append(XMLUtils.xmlEscape(feature));
payload.append("'/>");
}
payload.append("</query></iq>");
Stanza discoReply = new Stanza("iq", "", XMPPUtils.getBareJid(to.getValue()), payload.toString(), Arrays.asList(new Attribute[] { id }));
intent = new Intent();
intent.setAction(XmppTransportService.XMPP_STANZA_SEND_INTENT);
intent.putExtra("stanza", discoReply);
intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
context.sendBroadcast(intent, XmppTransportService.XMPP_STANZA_SEND_INTENT);
} catch (XmppMalformedException e) {
// Impossible
Log.e("AsmackService", "Please report (impossible condition)", e);
}
}
use of com.googlecode.asmack.Stanza in project AsmackService by rtreffer.
the class SendStanzaReceiver method onReceive.
/**
* Called on new intents.
* @param context The current context, ignored.
* @param intent The new intent.
*/
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.hasExtra("stanza")) {
return;
}
Stanza stanza = intent.getParcelableExtra("stanza");
service.send(stanza);
}
Aggregations