Search in sources :

Example 1 with Entry

use of eu.siacs.conversations.entities.Entry in project Conversations by siacs.

the class QuickConversationsService method considerSync.

private boolean considerSync(final Account account, final Map<String, PhoneNumberContact> contacts, final boolean forced) {
    final int hash = contacts.keySet().hashCode();
    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": consider sync of " + hash);
    if (!mLastSyncAttempt.retry(hash) && !forced) {
        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": do not attempt sync");
        return false;
    }
    mRunningSyncJobs.incrementAndGet();
    final Jid syncServer = Jid.of(API_DOMAIN);
    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending phone list to " + syncServer);
    final List<Element> entries = new ArrayList<>();
    for (final PhoneNumberContact c : contacts.values()) {
        entries.add(new Element("entry").setAttribute("number", c.getPhoneNumber()));
    }
    final IqPacket query = new IqPacket(IqPacket.TYPE.GET);
    query.setTo(syncServer);
    final Element book = new Element("phone-book", Namespace.SYNCHRONIZATION).setChildren(entries);
    final String statusQuo = Entry.statusQuo(contacts.values(), account.getRoster().getWithSystemAccounts(PhoneNumberContact.class));
    book.setAttribute("ver", statusQuo);
    query.addChild(book);
    mLastSyncAttempt = Attempt.create(hash);
    service.sendIqPacket(account, query, (a, response) -> {
        if (response.getType() == IqPacket.TYPE.RESULT) {
            final Element phoneBook = response.findChild("phone-book", Namespace.SYNCHRONIZATION);
            if (phoneBook != null) {
                final List<Contact> withSystemAccounts = account.getRoster().getWithSystemAccounts(PhoneNumberContact.class);
                for (Entry entry : Entry.ofPhoneBook(phoneBook)) {
                    final PhoneNumberContact phoneContact = contacts.get(entry.getNumber());
                    if (phoneContact == null) {
                        continue;
                    }
                    for (final Jid jid : entry.getJids()) {
                        final Contact contact = account.getRoster().getContact(jid);
                        final boolean needsCacheClean = contact.setPhoneContact(phoneContact);
                        if (needsCacheClean) {
                            service.getAvatarService().clear(contact);
                        }
                        withSystemAccounts.remove(contact);
                    }
                }
                for (final Contact contact : withSystemAccounts) {
                    final boolean needsCacheClean = contact.unsetPhoneContact(PhoneNumberContact.class);
                    if (needsCacheClean) {
                        service.getAvatarService().clear(contact);
                    }
                }
            } else {
                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": phone number contact list remains unchanged");
            }
        } else if (response.getType() == IqPacket.TYPE.TIMEOUT) {
            mLastSyncAttempt = Attempt.NULL;
        } else {
            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": failed to sync contact list with api server");
        }
        mRunningSyncJobs.decrementAndGet();
        service.syncRoster(account);
        service.updateRosterUi();
    });
    return true;
}
Also used : Entry(eu.siacs.conversations.entities.Entry) Jid(eu.siacs.conversations.xmpp.Jid) PhoneNumberContact(eu.siacs.conversations.android.PhoneNumberContact) Element(eu.siacs.conversations.xml.Element) ArrayList(java.util.ArrayList) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket) PhoneNumberContact(eu.siacs.conversations.android.PhoneNumberContact) Contact(eu.siacs.conversations.entities.Contact)

Aggregations

PhoneNumberContact (eu.siacs.conversations.android.PhoneNumberContact)1 Contact (eu.siacs.conversations.entities.Contact)1 Entry (eu.siacs.conversations.entities.Entry)1 Element (eu.siacs.conversations.xml.Element)1 Jid (eu.siacs.conversations.xmpp.Jid)1 IqPacket (eu.siacs.conversations.xmpp.stanzas.IqPacket)1 ArrayList (java.util.ArrayList)1