Search in sources :

Example 1 with ContactDataMapper

use of com.googlecode.asmack.contacts.ContactDataMapper 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)

Example 2 with ContactDataMapper

use of com.googlecode.asmack.contacts.ContactDataMapper in project AsmackService by rtreffer.

the class XmppTransportService method onCreate.

/**
     * Initialize the xmpp service, binding all required receivers.
     */
@Override
public void onCreate() {
    super.onCreate();
    accountManager = AccountManager.get(this);
    ContentProviderClient provider = getContentResolver().acquireContentProviderClient(ContactsContract.AUTHORITY_URI);
    ContactDataMapper mapper = new ContactDataMapper(provider);
    BroadcastReceiver receiver = new PresenceBroadcastReceiver(mapper);
    registerReceiver(receiver, new IntentFilter(XmppTransportService.XMPP_STANZA_INTENT));
    receiver = new DiscoReceiver();
    registerReceiver(receiver, new IntentFilter(XmppTransportService.XMPP_STANZA_INTENT));
    receiver = new KeepaliveActionIntentReceiver(this);
    ;
    registerReceiver(receiver, new IntentFilter(Intent.ACTION_TIME_TICK));
    receiver = new SendStanzaReceiver(this);
    registerReceiver(receiver, new IntentFilter(XMPP_STANZA_SEND_INTENT));
    receiver = new ConnectivityReceiver(this);
    registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    accountManager.addOnAccountsUpdatedListener(this, null, true);
}
Also used : ContactDataMapper(com.googlecode.asmack.contacts.ContactDataMapper) IntentFilter(android.content.IntentFilter) DiscoReceiver(com.googlecode.asmack.disco.DiscoReceiver) PresenceBroadcastReceiver(com.googlecode.asmack.contacts.PresenceBroadcastReceiver) PresenceBroadcastReceiver(com.googlecode.asmack.contacts.PresenceBroadcastReceiver) BroadcastReceiver(android.content.BroadcastReceiver) ContentProviderClient(android.content.ContentProviderClient)

Aggregations

ContactDataMapper (com.googlecode.asmack.contacts.ContactDataMapper)2 BroadcastReceiver (android.content.BroadcastReceiver)1 ContentProviderClient (android.content.ContentProviderClient)1 ContentProviderOperation (android.content.ContentProviderOperation)1 IntentFilter (android.content.IntentFilter)1 ImMetadata (com.googlecode.asmack.contacts.ImMetadata)1 NicknameMetadata (com.googlecode.asmack.contacts.NicknameMetadata)1 PresenceBroadcastReceiver (com.googlecode.asmack.contacts.PresenceBroadcastReceiver)1 RawContact (com.googlecode.asmack.contacts.RawContact)1 XmppMetadata (com.googlecode.asmack.contacts.XmppMetadata)1 DiscoReceiver (com.googlecode.asmack.disco.DiscoReceiver)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1