Search in sources :

Example 1 with VCardManager

use of org.jivesoftware.smackx.vcardtemp.VCardManager in project Zom-Android by zom.

the class XmppConnection method loadVCard.

private boolean loadVCard(ContentResolver resolver, String jid) {
    try {
        debug(TAG, "loading vcard for: " + jid);
        EntityBareJid bareJid = JidCreate.entityBareFrom(jid);
        VCardManager vCardManager = VCardManager.getInstanceFor(mConnection);
        VCard vCard = vCardManager.loadVCard(bareJid);
        Contact contact = mContactListManager.getContact(bareJid.toString());
        if (!TextUtils.isEmpty(vCard.getNickName())) {
            if (!vCard.getNickName().equals(contact.getName())) {
                contact.setName(vCard.getNickName());
                mContactListManager.doSetContactName(contact.getAddress().getBareAddress(), contact.getName());
            }
        }
        // check for a forwarding address
        if (vCard.getJabberId() != null && (!vCard.getJabberId().equals(bareJid.toString()))) {
            contact.setForwardingAddress(vCard.getJabberId());
        } else {
            contact.setForwardingAddress(null);
        }
        // If VCard is loaded, then save the avatar to the personal folder.
        String avatarHash = vCard.getAvatarHash();
        if (avatarHash != null) {
            byte[] avatarBytes = vCard.getAvatar();
            if (avatarBytes != null) {
                debug(TAG, "found avatar image in vcard for: " + bareJid.toString());
                debug(TAG, "start avatar length: " + avatarBytes.length);
                int rowsUpdated = DatabaseUtils.updateAvatarBlob(resolver, Imps.Avatars.CONTENT_URI, avatarBytes, bareJid.toString());
                if (rowsUpdated <= 0)
                    DatabaseUtils.insertAvatarBlob(resolver, Imps.Avatars.CONTENT_URI, mProviderId, mAccountId, avatarBytes, avatarHash, bareJid.toString());
                return true;
            }
        }
    } catch (Exception e) {
        debug(TAG, "err loading vcard: " + e.toString());
        if (e.getMessage() != null) {
            String streamErr = e.getMessage();
            if (streamErr != null && (streamErr.contains("404") || streamErr.contains("503"))) {
                return false;
            }
        }
    }
    return false;
}
Also used : VCardManager(org.jivesoftware.smackx.vcardtemp.VCardManager) EntityBareJid(org.jxmpp.jid.EntityBareJid) VCard(org.jivesoftware.smackx.vcardtemp.packet.VCard) OmemoFingerprint(org.jivesoftware.smackx.omemo.OmemoFingerprint) KeyStoreException(java.security.KeyStoreException) UndecidedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException) XMPPException(org.jivesoftware.smack.XMPPException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) ImException(org.awesomeapp.messenger.model.ImException) KeyManagementException(java.security.KeyManagementException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SmackException(org.jivesoftware.smack.SmackException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MultiUserChatException(org.jivesoftware.smackx.muc.MultiUserChatException) CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) CertificateException(java.security.cert.CertificateException) Contact(org.awesomeapp.messenger.model.Contact)

Example 2 with VCardManager

use of org.jivesoftware.smackx.vcardtemp.VCardManager in project Zom-Android by zom.

the class XmppConnection method sendVCard.

public void sendVCard(String migrateJabberId, boolean forceAvatarRefresh) {
    if (mConnection == null || getState() != ImConnection.LOGGED_IN)
        return;
    try {
        String jid = mUser.getAddress().getBareAddress();
        VCardManager vCardManager = VCardManager.getInstanceFor(mConnection);
        VCard vCard = null;
        try {
            vCard = vCardManager.loadVCard(JidCreate.entityBareFrom(jid));
        } catch (Exception e) {
        // debug(TAG,"error loading vcard",e);
        }
        boolean setAvatar = true;
        if (vCard == null) {
            vCard = new VCard();
            vCard.setJabberId(jid);
            setAvatar = true;
        } else if (vCard.getAvatarHash() != null) {
            setAvatar = !DatabaseUtils.doesAvatarHashExist(mContext.getContentResolver(), Imps.Avatars.CONTENT_URI, mUser.getAddress().getBareAddress(), vCard.getAvatarHash());
        }
        vCard.setNickName(mUser.getName());
        vCard.setFirstName(mUser.getName());
        // if we have moved to a new account, send it here
        if (migrateJabberId != null) {
            vCard.setJabberId(migrateJabberId);
        } else {
            vCard.setJabberId(mUser.getAddress().getBareAddress());
        }
        if (setAvatar || forceAvatarRefresh) {
            byte[] avatar = DatabaseUtils.getAvatarBytesFromAddress(mContext.getContentResolver(), mUser.getAddress().getBareAddress());
            if (avatar != null) {
                vCard.setAvatar(avatar, "image/jpeg");
            }
        }
        if (mConnection != null && mConnection.isConnected() && mConnection.isAuthenticated()) {
            debug(TAG, "Saving VCard for: " + mUser.getAddress().getAddress());
            vCardManager.saveVCard(vCard);
        }
    } catch (Exception e) {
        debug(TAG, "error saving vcard", e);
    }
}
Also used : VCardManager(org.jivesoftware.smackx.vcardtemp.VCardManager) VCard(org.jivesoftware.smackx.vcardtemp.packet.VCard) KeyStoreException(java.security.KeyStoreException) UndecidedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException) XMPPException(org.jivesoftware.smack.XMPPException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) ImException(org.awesomeapp.messenger.model.ImException) KeyManagementException(java.security.KeyManagementException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SmackException(org.jivesoftware.smack.SmackException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MultiUserChatException(org.jivesoftware.smackx.muc.MultiUserChatException) CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) CertificateException(java.security.cert.CertificateException)

Aggregations

RemoteException (android.os.RemoteException)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 KeyManagementException (java.security.KeyManagementException)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 CertificateException (java.security.cert.CertificateException)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 ImException (org.awesomeapp.messenger.model.ImException)2 SmackException (org.jivesoftware.smack.SmackException)2 XMPPException (org.jivesoftware.smack.XMPPException)2 MultiUserChatException (org.jivesoftware.smackx.muc.MultiUserChatException)2 CryptoFailedException (org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException)2 UndecidedOmemoIdentityException (org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException)2 VCardManager (org.jivesoftware.smackx.vcardtemp.VCardManager)2 VCard (org.jivesoftware.smackx.vcardtemp.packet.VCard)2 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)2 Contact (org.awesomeapp.messenger.model.Contact)1 OmemoFingerprint (org.jivesoftware.smackx.omemo.OmemoFingerprint)1 EntityBareJid (org.jxmpp.jid.EntityBareJid)1