Search in sources :

Example 1 with OnRosterChangedListener

use of com.xabber.android.data.roster.OnRosterChangedListener in project xabber-android by redsolution.

the class VCardManager method onVCardReceived.

@SuppressWarnings("WeakerAccess")
void onVCardReceived(final AccountJid account, final Jid bareAddress, final VCard vCard) {
    final StructuredName name;
    if (vCard.getType() == Type.error) {
        onVCardFailed(account, bareAddress);
        if (names.containsKey(bareAddress)) {
            return;
        }
        name = EMPTY_STRUCTURED_NAME;
    } else {
        try {
            String hash = vCard.getAvatarHash();
            byte[] avatar = vCard.getAvatar();
            AvatarManager.getInstance().onAvatarReceived(bareAddress, hash, avatar);
        // "bad base-64" error happen sometimes
        } catch (IllegalArgumentException e) {
            LogManager.exception(this, e);
        }
        name = new StructuredName(vCard.getNickName(), vCard.getField(VCardProperty.FN.name()), vCard.getFirstName(), vCard.getMiddleName(), vCard.getLastName());
        try {
            if (account.getFullJid().asBareJid().equals(bareAddress.asBareJid())) {
                PresenceManager.getInstance().resendPresence(account);
            }
        } catch (NetworkException e) {
            LogManager.exception(this, e);
        }
    }
    names.put(bareAddress, name);
    RosterContact rosterContact = RosterManager.getInstance().getRosterContact(account, bareAddress.asBareJid());
    for (OnRosterChangedListener listener : Application.getInstance().getManagers(OnRosterChangedListener.class)) {
        listener.onContactStructuredInfoChanged(rosterContact, name);
    }
    Application.getInstance().runInBackground(new Runnable() {

        @Override
        public void run() {
            VCardTable.getInstance().write(bareAddress.toString(), name);
        }
    });
    if (vCard.getFrom() == null) {
        // account it self
        AccountManager.getInstance().onAccountChanged(account);
    } else {
        try {
            RosterManager.onContactChanged(account, UserJid.from(bareAddress));
        } catch (UserJid.UserJidCreateException e) {
            LogManager.exception(this, e);
        }
    }
    for (OnVCardListener listener : Application.getInstance().getUIListeners(OnVCardListener.class)) {
        listener.onVCardReceived(account, bareAddress, vCard);
    }
}
Also used : OnRosterChangedListener(com.xabber.android.data.roster.OnRosterChangedListener) RosterContact(com.xabber.android.data.roster.RosterContact) UserJid(com.xabber.android.data.entity.UserJid) StructuredName(com.xabber.android.data.roster.StructuredName) NetworkException(com.xabber.android.data.NetworkException)

Example 2 with OnRosterChangedListener

use of com.xabber.android.data.roster.OnRosterChangedListener in project xabber-android by redsolution.

the class VCardManager method onVCardReceived.

private void onVCardReceived(final String account, final String bareAddress, final VCard vCard) {
    final StructuredName name;
    if (vCard.getType() == Type.error) {
        onVCardFailed(account, bareAddress);
        if (names.containsKey(bareAddress)) {
            return;
        }
        name = EMPTY_STRUCTURED_NAME;
    } else {
        for (OnVCardListener listener : Application.getInstance().getUIListeners(OnVCardListener.class)) {
            listener.onVCardReceived(account, bareAddress, vCard);
        }
        String hash = vCard.getAvatarHash();
        AvatarManager.getInstance().onAvatarReceived(bareAddress, hash, vCard.getAvatar());
        name = new StructuredName(vCard.getNickName(), vCard.getField(VCardProperty.FN.name()), vCard.getFirstName(), vCard.getMiddleName(), vCard.getLastName());
        Application.getInstance().runInBackground(new Runnable() {

            @Override
            public void run() {
                try {
                    if (Jid.getBareAddress(account).equals(bareAddress)) {
                        PresenceManager.getInstance().resendPresence(account);
                    }
                } catch (NetworkException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    names.put(bareAddress, name);
    for (RosterContact rosterContact : RosterManager.getInstance().getContacts()) {
        if (rosterContact.getUser().equals(bareAddress)) {
            for (OnRosterChangedListener listener : Application.getInstance().getManagers(OnRosterChangedListener.class)) {
                listener.onContactStructuredInfoChanged(rosterContact, name);
            }
        }
    }
    Application.getInstance().runInBackground(new Runnable() {

        @Override
        public void run() {
            VCardTable.getInstance().write(bareAddress, name);
        }
    });
    if (vCard.getFrom() == null) {
        // account it self
        AccountManager.getInstance().onAccountChanged(account);
    } else {
        RosterManager.getInstance().onContactChanged(account, bareAddress);
    }
}
Also used : OnRosterChangedListener(com.xabber.android.data.roster.OnRosterChangedListener) RosterContact(com.xabber.android.data.roster.RosterContact) StructuredName(com.xabber.android.data.roster.StructuredName) NetworkException(com.xabber.android.data.NetworkException)

Aggregations

NetworkException (com.xabber.android.data.NetworkException)2 OnRosterChangedListener (com.xabber.android.data.roster.OnRosterChangedListener)2 RosterContact (com.xabber.android.data.roster.RosterContact)2 StructuredName (com.xabber.android.data.roster.StructuredName)2 UserJid (com.xabber.android.data.entity.UserJid)1