Search in sources :

Example 1 with RawContact

use of com.googlecode.asmack.contacts.RawContact 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);
    }
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) XmppMetadata(com.googlecode.asmack.contacts.XmppMetadata) ImMetadata(com.googlecode.asmack.contacts.ImMetadata) ContactDataMapper(com.googlecode.asmack.contacts.ContactDataMapper) RawContact(com.googlecode.asmack.contacts.RawContact) NicknameMetadata(com.googlecode.asmack.contacts.NicknameMetadata)

Aggregations

ContentProviderOperation (android.content.ContentProviderOperation)1 ContactDataMapper (com.googlecode.asmack.contacts.ContactDataMapper)1 ImMetadata (com.googlecode.asmack.contacts.ImMetadata)1 NicknameMetadata (com.googlecode.asmack.contacts.NicknameMetadata)1 RawContact (com.googlecode.asmack.contacts.RawContact)1 XmppMetadata (com.googlecode.asmack.contacts.XmppMetadata)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1