Search in sources :

Example 11 with Presence

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

the class XmppConnection method setPresence.

private void setPresence(Jid from, int presenceType) {
    Presence p = null;
    Contact contact = mContactListManager.getContact(from.asBareJid().toString());
    if (contact == null)
        return;
    p = new Presence(presenceType, "", null, null, Presence.CLIENT_TYPE_MOBILE);
    if (from.hasResource())
        p.setResource(from.getResourceOrEmpty().toString());
    if (presenceType == Presence.AVAILABLE)
        p.setLastSeen(new Date());
    else if (contact.getPresence() != null)
        p.setLastSeen(contact.getPresence().getLastSeen());
    contact.setPresence(p);
    Collection<Contact> contactsUpdate = new ArrayList<Contact>();
    contactsUpdate.add(contact);
    mContactListManager.notifyContactsPresenceUpdated(contactsUpdate.toArray(new Contact[contactsUpdate.size()]));
}
Also used : ArrayList(java.util.ArrayList) Presence(org.awesomeapp.messenger.model.Presence) Date(java.util.Date) Contact(org.awesomeapp.messenger.model.Contact)

Example 12 with Presence

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

the class XmppConnection method handleChatState.

private void handleChatState(String from, String chatStateXml) throws RemoteException {
    Presence p = null;
    Contact contact = mContactListManager.getContact(from);
    if (contact == null)
        return;
    boolean isTyping = false;
    // handle is-typing, probably some indication on screen
    if (chatStateXml.contains("'error'") || chatStateXml.contains("'cancel'")) {
    // do nothing
    } else if (chatStateXml.contains(ChatState.active.toString())) {
        p = new Presence(Presence.AVAILABLE, "", null, null, Presence.CLIENT_TYPE_MOBILE);
        p.setLastSeen(new Date());
    } else if (chatStateXml.contains(ChatState.inactive.toString())) {
        p = new Presence(Presence.AWAY, "", null, null, Presence.CLIENT_TYPE_MOBILE);
        p.setLastSeen(new Date());
    } else if (chatStateXml.contains(ChatState.composing.toString())) {
        p = new Presence(Presence.AVAILABLE, "", null, null, Presence.CLIENT_TYPE_MOBILE);
        isTyping = true;
        p.setLastSeen(new Date());
    } else if (chatStateXml.contains(ChatState.inactive.toString()) || chatStateXml.contains(ChatState.paused.toString())) {
    } else if (chatStateXml.contains(ChatState.gone.toString())) {
    }
    IChatSession csa = mSessionManager.getAdapter().getChatSession(from);
    if (csa != null)
        csa.setContactTyping(contact, isTyping);
    if (p != null) {
        String[] presenceParts = from.split("/");
        if (presenceParts.length > 1)
            p.setResource(presenceParts[1]);
        contact.setPresence(p);
        Collection<Contact> contactsUpdate = new ArrayList<Contact>();
        contactsUpdate.add(contact);
        mContactListManager.notifyContactsPresenceUpdated(contactsUpdate.toArray(new Contact[contactsUpdate.size()]));
    }
}
Also used : ArrayList(java.util.ArrayList) Presence(org.awesomeapp.messenger.model.Presence) IChatSession(org.awesomeapp.messenger.service.IChatSession) Date(java.util.Date) Contact(org.awesomeapp.messenger.model.Contact)

Example 13 with Presence

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

the class XmppConnection method makePresencePacket.

private org.jivesoftware.smack.packet.Presence makePresencePacket(Presence presence) {
    String statusText = presence.getStatusText();
    org.jivesoftware.smack.packet.Presence.Type type = org.jivesoftware.smack.packet.Presence.Type.available;
    org.jivesoftware.smack.packet.Presence.Mode mode = org.jivesoftware.smack.packet.Presence.Mode.available;
    int priority = mPriority;
    final int status = presence.getStatus();
    if (status == Presence.AWAY) {
        priority = 10;
        mode = org.jivesoftware.smack.packet.Presence.Mode.away;
    } else if (status == Presence.IDLE) {
        priority = 15;
        mode = org.jivesoftware.smack.packet.Presence.Mode.away;
    } else if (status == Presence.DO_NOT_DISTURB) {
        priority = 5;
        mode = org.jivesoftware.smack.packet.Presence.Mode.dnd;
    } else if (status == Presence.OFFLINE) {
        priority = 0;
        type = org.jivesoftware.smack.packet.Presence.Type.unavailable;
        statusText = "Offline";
    }
    // The user set priority is the maximum allowed
    if (priority > mPriority)
        priority = mPriority;
    org.jivesoftware.smack.packet.Presence packet = new org.jivesoftware.smack.packet.Presence(type, statusText, priority, mode);
    try {
        byte[] avatar = DatabaseUtils.getAvatarBytesFromAddress(mContext.getContentResolver(), mUser.getAddress().getBareAddress());
        if (avatar != null) {
            VCardTempXUpdatePresenceExtension vcardExt = new VCardTempXUpdatePresenceExtension(avatar);
            packet.addExtension(vcardExt);
        }
    } catch (Exception e) {
        debug(TAG, "error upading presence with avatar hash", e);
    }
    return packet;
}
Also used : 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) Presence(org.awesomeapp.messenger.model.Presence)

Example 14 with Presence

use of org.awesomeapp.messenger.model.Presence 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 15 with Presence

use of org.awesomeapp.messenger.model.Presence 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)

Aggregations

Presence (org.awesomeapp.messenger.model.Presence)16 ImException (org.awesomeapp.messenger.model.ImException)7 RemoteException (android.os.RemoteException)6 IOException (java.io.IOException)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 KeyManagementException (java.security.KeyManagementException)6 KeyStoreException (java.security.KeyStoreException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 CertificateException (java.security.cert.CertificateException)6 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)6 Contact (org.awesomeapp.messenger.model.Contact)6 SmackException (org.jivesoftware.smack.SmackException)6 XMPPException (org.jivesoftware.smack.XMPPException)6 MultiUserChatException (org.jivesoftware.smackx.muc.MultiUserChatException)6 CryptoFailedException (org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException)6 UndecidedOmemoIdentityException (org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException)6 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)6 ContentResolver (android.content.ContentResolver)4 ArrayList (java.util.ArrayList)4 ContentValues (android.content.ContentValues)3