use of com.googlecode.asmack.contacts.ImMetadata in project AsmackService by rtreffer.
the class SyncAdapter method handleRosterResult.
/**
* Retrieve and handle a roster result.
* @param account The xmpp account.
* @param roster The user roster xml node.
* @param provider The content provider used to store the results.
*/
private void handleRosterResult(Account account, Node roster, ContentProviderClient provider) {
long syncCount = getAndIncrementSyncCount(account);
NodeList rosterItems = roster.getChildNodes();
ContactDataMapper mapper = new ContactDataMapper(provider);
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(60);
HashMap<String, RawContact> oldContacts = new HashMap<String, RawContact>();
for (RawContact contact : mapper.getRawContacts(account.name, true)) {
oldContacts.put(contact.getJid(), contact);
}
for (int i = 0; i < rosterItems.getLength(); i++) {
Node item = rosterItems.item(i);
if (!"item".equals(item.getLocalName())) {
continue;
}
if (!"both".equals(item.getAttributes().getNamedItem("subscription").getTextContent())) {
continue;
}
String jid = item.getAttributes().getNamedItem("jid").getTextContent();
String name = null;
Node nameAttribute = item.getAttributes().getNamedItem("name");
if (nameAttribute != null) {
name = nameAttribute.getTextContent();
}
if (TextUtils.isEmpty(name)) {
name = jid;
}
RawContact contact = oldContacts.remove(jid);
if (contact == null) {
contact = new RawContact();
contact.setAccountName(account.name);
contact.setJid(jid);
contact.setSyncIndex(Long.toString(syncCount));
}
XmppMetadata xmpp = new XmppMetadata();
xmpp.setJid(jid);
contact.setMetadata(xmpp);
if (!TextUtils.isEmpty(name)) {
NicknameMetadata nick = new NicknameMetadata();
nick.setNickname(name);
contact.setMetadata(nick);
} else {
contact.removeMetadata(NicknameMetadata.MIMETYPE);
}
ImMetadata im = new ImMetadata();
im.setType(ImMetadata.Type.OTHER);
im.setAccountJid(account.name);
im.setJid(jid);
im.setProtocol(ImMetadata.Protocol.JABBER);
contact.setMetadata(im);
mapper.persist(contact, operations);
Log.d(TAG, "Persisted " + jid);
if (operations.size() > 100) {
mapper.perform(operations);
operations.clear();
}
}
if (operations.size() > 0) {
mapper.perform(operations);
}
}
Aggregations