Search in sources :

Example 26 with Contact

use of org.awesomeapp.messenger.model.Contact in project Zom-Android by zom.

the class XmppConnection method makeContact.

private Contact makeContact(String address) {
    Contact contact = null;
    // load from roster if we don't have the contact
    RosterEntry rEntry = null;
    try {
        if (mConnection != null)
            rEntry = mRoster.getEntry(JidCreate.bareFrom(address));
        if (rEntry != null) {
            XmppAddress xAddress = new XmppAddress(address);
            String name = rEntry.getName();
            if (name == null)
                name = xAddress.getUser();
            // TODO we should check the type from here
            contact = new Contact(xAddress, name, Imps.Contacts.TYPE_NORMAL);
        } else {
            XmppAddress xAddress = new XmppAddress(address);
            contact = new Contact(xAddress, xAddress.getUser(), Imps.Contacts.TYPE_NORMAL);
        }
    } catch (XmppStringprepException xe) {
    // nothing return null;
    }
    return contact;
}
Also used : RosterEntry(org.jivesoftware.smack.roster.RosterEntry) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) Contact(org.awesomeapp.messenger.model.Contact)

Example 27 with Contact

use of org.awesomeapp.messenger.model.Contact in project Zom-Android by zom.

the class XmppConnection method handleSubscribeRequest.

private void handleSubscribeRequest(Jid jid) throws ImException, RemoteException {
    Contact contact = mContactListManager.getContact(jid.asBareJid().toString());
    if (contact == null) {
        XmppAddress xAddr = new XmppAddress(jid.toString());
        contact = new Contact(xAddr, xAddr.getUser(), Imps.Contacts.TYPE_NORMAL);
    }
    if (contact.getSubscriptionType() == Imps.Contacts.SUBSCRIPTION_TYPE_BOTH || contact.getSubscriptionType() == Imps.Contacts.SUBSCRIPTION_TYPE_TO) {
        mContactListManager.approveSubscriptionRequest(contact);
    } else {
        ContactList cList = getContactListManager().getDefaultContactList();
        contact.setSubscriptionStatus(Imps.Contacts.SUBSCRIPTION_STATUS_SUBSCRIBE_PENDING);
        contact.setSubscriptionType(Imps.Contacts.SUBSCRIPTION_TYPE_FROM);
        mContactListManager.doAddContactToListAsync(contact, cList, false);
        mContactListManager.getSubscriptionRequestListener().onSubScriptionRequest(contact, mProviderId, mAccountId);
    }
    ChatSession session = findOrCreateSession(jid.toString(), false);
}
Also used : ContactList(org.awesomeapp.messenger.model.ContactList) ChatSession(org.awesomeapp.messenger.model.ChatSession) IChatSession(org.awesomeapp.messenger.service.IChatSession) Contact(org.awesomeapp.messenger.model.Contact)

Example 28 with Contact

use of org.awesomeapp.messenger.model.Contact in project Zom-Android by zom.

the class XmppConnection method handlePresenceChanged.

private Contact handlePresenceChanged(org.jivesoftware.smack.packet.Presence presence) {
    if (// our presence isn't really valid
    presence == null)
        return null;
    if (TextUtils.isEmpty(presence.getFrom()))
        return null;
    if (presence.getType() == org.jivesoftware.smack.packet.Presence.Type.error)
        return null;
    if (// ignore presence from yourself
    presence.getFrom().toString().startsWith(mUser.getAddress().getBareAddress()))
        return null;
    XmppAddress xaddress = new XmppAddress(presence.getFrom().toString());
    Presence p = new Presence(parsePresence(presence), presence.getStatus(), null, null, Presence.CLIENT_TYPE_MOBILE, null, xaddress.getResource());
    // this is only persisted in memory
    p.setPriority(presence.getPriority());
    if (mContactListManager == null)
        // we may have logged out
        return null;
    Contact contact = mContactListManager.getContact(xaddress.getBareAddress());
    BareJid jid = presence.getFrom().asBareJid();
    if (presence.getFrom().hasResource())
        p.setResource(presence.getFrom().getResourceOrEmpty().toString());
    if (presence.getType() == org.jivesoftware.smack.packet.Presence.Type.subscribe) {
        debug(TAG, "got subscribe request: " + presence.getFrom());
        try {
            handleSubscribeRequest(presence.getFrom());
        } catch (Exception e) {
            Log.e(TAG, "remote exception on subscription handling", e);
        }
    } else if (presence.getType() == org.jivesoftware.smack.packet.Presence.Type.subscribed) {
        debug(TAG, "got subscribed confirmation: " + presence.getFrom());
        try {
            if (contact == null) {
                XmppAddress xAddr = new XmppAddress(presence.getFrom().toString());
                contact = new Contact(xAddr, xAddr.getUser(), Imps.Contacts.TYPE_NORMAL);
                mContactListManager.doAddContactToListAsync(contact, getContactListManager().getDefaultContactList(), false);
            }
            if (contact.getSubscriptionStatus() == Imps.Contacts.SUBSCRIPTION_STATUS_SUBSCRIBE_PENDING) {
                mContactListManager.approveSubscriptionRequest(contact);
                mContactListManager.getSubscriptionRequestListener().onSubscriptionApproved(contact, mProviderId, mAccountId);
            }
            if (getOmemo().getManager().contactSupportsOmemo(jid)) {
                getOmemo().getManager().requestDeviceListUpdateFor(jid);
                getOmemo().getManager().buildSessionsWith(jid);
            }
        } catch (Exception e) {
            Log.e(TAG, "remote exception on subscription handling", e);
        }
    } else if (presence.getType() == org.jivesoftware.smack.packet.Presence.Type.unsubscribe) {
        debug(TAG, "got unsubscribe request: " + presence.getFrom());
    // TBD how to handle this
    // mContactListManager.getSubscriptionRequestListener().onUnSubScriptionRequest(contact);
    } else if (presence.getType() == org.jivesoftware.smack.packet.Presence.Type.unsubscribed) {
        debug(TAG, "got unsubscribe request: " + presence.getFrom());
        try {
            mContactListManager.getSubscriptionRequestListener().onSubscriptionDeclined(contact, mProviderId, mAccountId);
        } catch (RemoteException e) {
            Log.e(TAG, "remote exception on subscription handling", e);
        }
    }
    // this is typical presence, let's get the latest/highest priority
    debug(TAG, "got presence: " + presence.getFrom() + "=" + presence.getType());
    if (contact != null) {
        if (contact.getPresence() != null) {
            Presence pOld = contact.getPresence();
            if (pOld != null && pOld.getLastSeen() != null && p.getLastSeen() == null)
                p.setLastSeen(pOld.getLastSeen());
            if (pOld == null || pOld.getResource() == null) {
                contact.setPresence(p);
            } else if (// if the same resource as the existing one, then update it
            pOld.getResource() != null && pOld.getResource().equals(p.getResource())) {
                contact.setPresence(p);
            } else if (// if priority is higher, then override
            p.getPriority() >= pOld.getPriority()) {
                contact.setPresence(p);
            }
        } else
            contact.setPresence(p);
        if (contact.getPresence().getLastSeen() == null) {
            getLastSeen(contact);
        }
        ExtensionElement packetExtension = presence.getExtension("x", "vcard-temp:x:update");
        if (packetExtension != null) {
            StandardExtensionElement o = (StandardExtensionElement) packetExtension;
            String hash = o.getAttributeValue("photo");
            if (hash != null) {
                boolean hasMatches = DatabaseUtils.doesAvatarHashExist(mContext.getContentResolver(), Imps.Avatars.CONTENT_URI, contact.getAddress().getBareAddress(), hash);
                if (// we must reload
                !hasMatches)
                    qAvatar.put(contact.getAddress().getAddress(), hash);
            } else {
                for (StandardExtensionElement element : o.getElements()) {
                    if (element.getElementName().equals("photo")) {
                        hash = element.getText();
                        if (hash != null) {
                            boolean hasMatches = DatabaseUtils.doesAvatarHashExist(mContext.getContentResolver(), Imps.Avatars.CONTENT_URI, contact.getAddress().getBareAddress(), hash);
                            if (// we must reload
                            !hasMatches)
                                qAvatar.put(contact.getAddress().getAddress(), hash);
                        }
                        break;
                    }
                }
            }
        } else {
            boolean hasAvatar = DatabaseUtils.hasAvatarContact(mContext.getContentResolver(), Imps.Avatars.CONTENT_URI, contact.getAddress().getBareAddress());
            if (!hasAvatar) {
                qAvatar.put(contact.getAddress().getAddress(), "");
            }
        }
    }
    return contact;
}
Also used : EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid) DomainBareJid(org.jxmpp.jid.DomainBareJid) StandardExtensionElement(org.jivesoftware.smack.packet.StandardExtensionElement) StandardExtensionElement(org.jivesoftware.smack.packet.StandardExtensionElement) DefaultExtensionElement(org.jivesoftware.smack.packet.DefaultExtensionElement) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) Presence(org.awesomeapp.messenger.model.Presence) RemoteException(android.os.RemoteException) 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 29 with Contact

use of org.awesomeapp.messenger.model.Contact in project Zom-Android by zom.

the class XmppConnection method initPresenceProcessor.

private void initPresenceProcessor() {
    mTimerPresence = new Timer();
    mTimerPresence.schedule(new TimerTask() {

        public void run() {
            if (qPresence.size() > 0) {
                try {
                    ContactList cList = getContactListManager().getDefaultContactList();
                    if (cList == null)
                        // not ready yet
                        return;
                } catch (Exception e) {
                    // not ready yet
                    return;
                }
                Map<String, Contact> alUpdate = new HashMap<String, Contact>();
                org.jivesoftware.smack.packet.Presence p = null;
                Contact contact = null;
                final int maxBatch = 20;
                while (qPresence.peek() != null && alUpdate.size() < maxBatch) {
                    p = qPresence.poll();
                    contact = handlePresenceChanged(p);
                    if (contact != null) {
                        alUpdate.put(contact.getAddress().getBareAddress(), contact);
                    }
                }
                if (alUpdate.size() > 0) {
                    loadVCardsAsync();
                    // Log.d(TAG,"XMPP processed presence q=" + alUpdate.size());
                    Collection<Contact> contactsUpdate = alUpdate.values();
                    if (mContactListManager != null)
                        mContactListManager.notifyContactsPresenceUpdated(contactsUpdate.toArray(new Contact[contactsUpdate.size()]));
                }
            }
        }
    }, 500, 500);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) Presence(org.awesomeapp.messenger.model.Presence) Collection(java.util.Collection) ContactList(org.awesomeapp.messenger.model.ContactList) Map(java.util.Map) HashMap(java.util.HashMap) 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 30 with Contact

use of org.awesomeapp.messenger.model.Contact in project Zom-Android by zom.

the class ChatSessionAdapter method initOtrChatSession.

private void initOtrChatSession(ImEntity participant) {
    try {
        if (mConnection != null) {
            mDataHandler = new OtrDataHandler(mChatSession);
            mDataHandler.setDataListener(mDataHandlerListener);
            OtrChatManager cm = service.getOtrChatManager();
            cm.addOtrEngineListener(mListenerAdapter);
            mChatSession.setMessageListener(new OtrChatListener(cm, mListenerAdapter));
            if (participant instanceof Contact) {
                String key = participant.getAddress().getAddress();
                if (!mOtrChatSessions.containsKey(key)) {
                    OtrChatSessionAdapter adapter = new OtrChatSessionAdapter(mConnection.getLoginUser().getAddress().getAddress(), participant, cm);
                    mOtrChatSessions.put(key, adapter);
                }
            } else if (participant instanceof ChatGroup) {
            }
            mDataHandler.setChatId(getId());
        }
    } catch (NullPointerException npe) {
        Log.e(ImApp.LOG_TAG, "error init OTR session", npe);
    }
}
Also used : OtrChatSessionAdapter(org.awesomeapp.messenger.crypto.otr.OtrChatSessionAdapter) OtrDataHandler(org.awesomeapp.messenger.crypto.otr.OtrDataHandler) ChatGroup(org.awesomeapp.messenger.model.ChatGroup) OtrChatListener(org.awesomeapp.messenger.crypto.otr.OtrChatListener) OtrChatManager(org.awesomeapp.messenger.crypto.otr.OtrChatManager) Contact(org.awesomeapp.messenger.model.Contact)

Aggregations

Contact (org.awesomeapp.messenger.model.Contact)33 RemoteException (android.os.RemoteException)11 XmppAddress (org.awesomeapp.messenger.plugin.xmpp.XmppAddress)10 Presence (org.awesomeapp.messenger.model.Presence)6 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)6 Intent (android.content.Intent)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 ImException (org.awesomeapp.messenger.model.ImException)5 IContactListManager (org.awesomeapp.messenger.service.IContactListManager)5 ContentValues (android.content.ContentValues)4 Uri (android.net.Uri)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 KeyManagementException (java.security.KeyManagementException)4 KeyStoreException (java.security.KeyStoreException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 CertificateException (java.security.cert.CertificateException)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 ContactList (org.awesomeapp.messenger.model.ContactList)4 IChatSession (org.awesomeapp.messenger.service.IChatSession)4