Search in sources :

Example 1 with PhoneNumberContact

use of eu.siacs.conversations.android.PhoneNumberContact 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)

Example 2 with PhoneNumberContact

use of eu.siacs.conversations.android.PhoneNumberContact in project Conversations by siacs.

the class QuickConversationsService method refresh.

private void refresh(Account account, Collection<PhoneNumberContact> contacts) {
    for (Contact contact : account.getRoster().getWithSystemAccounts(PhoneNumberContact.class)) {
        final Uri uri = contact.getSystemAccount();
        if (uri == null) {
            continue;
        }
        final String number = getNumber(contact);
        final PhoneNumberContact phoneNumberContact = PhoneNumberContact.findByUriOrNumber(contacts, uri, number);
        final boolean needsCacheClean;
        if (phoneNumberContact != null) {
            if (!uri.equals(phoneNumberContact.getLookupUri())) {
                Log.d(Config.LOGTAG, "lookupUri has changed from " + uri + " to " + phoneNumberContact.getLookupUri());
            }
            needsCacheClean = contact.setPhoneContact(phoneNumberContact);
        } else {
            needsCacheClean = contact.unsetPhoneContact(PhoneNumberContact.class);
            Log.d(Config.LOGTAG, uri.toString() + " vanished from address book");
        }
        if (needsCacheClean) {
            service.getAvatarService().clear(contact);
        }
    }
}
Also used : PhoneNumberContact(eu.siacs.conversations.android.PhoneNumberContact) Uri(android.net.Uri) PhoneNumberContact(eu.siacs.conversations.android.PhoneNumberContact) Contact(eu.siacs.conversations.entities.Contact)

Example 3 with PhoneNumberContact

use of eu.siacs.conversations.android.PhoneNumberContact in project Conversations by siacs.

the class Entry method ofPhoneNumberContactsAndContacts.

private static List<Entry> ofPhoneNumberContactsAndContacts(final Collection<PhoneNumberContact> phoneNumberContacts, Collection<Contact> systemContacts) {
    final ArrayList<Entry> entries = new ArrayList<>();
    for (Contact contact : systemContacts) {
        final PhoneNumberContact phoneNumberContact = PhoneNumberContact.findByUri(phoneNumberContacts, contact.getSystemAccount());
        if (phoneNumberContact != null && phoneNumberContact.getPhoneNumber() != null) {
            Entry entry = findOrCreateByPhoneNumber(entries, phoneNumberContact.getPhoneNumber());
            entry.jids.add(contact.getJid().asBareJid());
        }
    }
    return entries;
}
Also used : PhoneNumberContact(eu.siacs.conversations.android.PhoneNumberContact) ArrayList(java.util.ArrayList) PhoneNumberContact(eu.siacs.conversations.android.PhoneNumberContact)

Example 4 with PhoneNumberContact

use of eu.siacs.conversations.android.PhoneNumberContact in project Conversations by siacs.

the class QuickConversationsService method considerSync.

private void considerSync(boolean forced) {
    final ImmutableMap<String, PhoneNumberContact> allContacts = PhoneNumberContact.load(service);
    for (final Account account : service.getAccounts()) {
        final Map<String, PhoneNumberContact> contacts = filtered(allContacts, account.getJid().getLocal());
        if (contacts.size() < allContacts.size()) {
            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": found own phone number in address book. ignoring...");
        }
        refresh(account, contacts.values());
        if (!considerSync(account, contacts, forced)) {
            service.syncRoster(account);
        }
    }
}
Also used : Account(eu.siacs.conversations.entities.Account) PhoneNumberContact(eu.siacs.conversations.android.PhoneNumberContact)

Aggregations

PhoneNumberContact (eu.siacs.conversations.android.PhoneNumberContact)4 Contact (eu.siacs.conversations.entities.Contact)2 ArrayList (java.util.ArrayList)2 Uri (android.net.Uri)1 Account (eu.siacs.conversations.entities.Account)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