diff --git a/app/src/main/java/org/awesomeapp/messenger/crypto/otr/OtrChatManager.java b/app/src/main/java/org/awesomeapp/messenger/crypto/otr/OtrChatManager.java index 0c36cb6d3..ae175ea7d 100644 --- a/app/src/main/java/org/awesomeapp/messenger/crypto/otr/OtrChatManager.java +++ b/app/src/main/java/org/awesomeapp/messenger/crypto/otr/OtrChatManager.java @@ -106,7 +106,7 @@ public static String processResource(String userId) { public synchronized SessionID getSessionId(String localUserId, String remoteUserId) { - SessionID sessionId = new SessionID( XmppAddress.stripResource(localUserId), XmppAddress.stripResource(remoteUserId), SESSION_TYPE_XMPP); + SessionID sessionId = new SessionID( XmppAddress.stripResource(localUserId), remoteUserId, SESSION_TYPE_XMPP); Session session = mSessions.get(sessionId.toString()); diff --git a/app/src/main/java/org/awesomeapp/messenger/model/ChatSession.java b/app/src/main/java/org/awesomeapp/messenger/model/ChatSession.java index 8cfbc4c38..816bb5d33 100644 --- a/app/src/main/java/org/awesomeapp/messenger/model/ChatSession.java +++ b/app/src/main/java/org/awesomeapp/messenger/model/ChatSession.java @@ -70,6 +70,14 @@ private void initJid () { try { mJid = JidCreate.from(mParticipant.getAddress().getAddress()); + + if (mJid.hasNoResource() && mParticipant instanceof Contact) + { + Contact contact = (Contact)mParticipant; + if (contact.getPresence() != null && contact.getPresence().getResource() != null) + mJid = JidCreate.from(mParticipant.getAddress().getBareAddress() + '/' + contact.getPresence().getResource()); + } + mXa = new XmppAddress(mJid.toString()); if (mJid.hasNoResource()) { @@ -383,5 +391,8 @@ public void setOmemoGroupEnabled (boolean omemoGroups) mEnableOmemoGroups = omemoGroups; } - + public void updateParticipant (ImEntity participant) + { + mParticipant = participant; + } } diff --git a/app/src/main/java/org/awesomeapp/messenger/plugin/xmpp/XmppConnection.java b/app/src/main/java/org/awesomeapp/messenger/plugin/xmpp/XmppConnection.java index c07b63a0a..5005fb257 100644 --- a/app/src/main/java/org/awesomeapp/messenger/plugin/xmpp/XmppConnection.java +++ b/app/src/main/java/org/awesomeapp/messenger/plugin/xmpp/XmppConnection.java @@ -2482,6 +2482,15 @@ else if (rec.getFrom().getResource().equals(rec.getTo().getUser())) { } } + else + { + Contact contact = (Contact)session.getParticipant(); + if (contact.getPresence() == null || contact.getPresence().getResource() == null) + { + session.updateParticipant(handlePresenceChanged(mRoster.getPresence(smackMessage.getFrom().asBareJid()))); + + } + } boolean good = session.onReceiveMessage(rec, notifyUser); @@ -2925,7 +2934,7 @@ Collection getSessions () { @Override public ChatSession createChatSession(ImEntity participant, boolean isNewSession) { ChatSession session = super.createChatSession(participant,isNewSession); - mSessions.put(participant.getAddress().getAddress(),session); + mSessions.put(participant.getAddress().getBareAddress(),session); if (participant instanceof Contact) getLastSeen ((Contact)participant); @@ -4351,6 +4360,8 @@ private void handleChatState (String from, String chatStateXml) throws RemoteExc if (contact == null) return; + p = contact.getPresence(); + boolean isTyping = false; //handle is-typing, probably some indication on screen @@ -4358,23 +4369,27 @@ private void handleChatState (String from, String chatStateXml) throws RemoteExc //do nothing } else if (chatStateXml.contains(ChatState.active.toString())) { - p = new Presence(Presence.AVAILABLE, "", null, null, - Presence.CLIENT_TYPE_MOBILE); + if (p == null) + 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, + if (p == null) + 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, + if (p == null) + 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())) { @@ -4445,8 +4460,11 @@ private void setPresence (Jid from, int presenceType) { if (contact == null) return; - p = new Presence(presenceType, "", null, null, - Presence.CLIENT_TYPE_MOBILE); + p = contact.getPresence(); + + if (p == null) + p = new Presence(presenceType, "", null, null, + Presence.CLIENT_TYPE_MOBILE); if (from.hasResource()) p.setResource(from.getResourceOrEmpty().toString()); @@ -4472,7 +4490,10 @@ private void getLastSeen (Contact contact) LastActivity activity = mLastActivityManager.getLastActivity(JidCreate.bareFrom(contact.getAddress().getBareAddress())); if (activity != null) { - Presence presence = new Presence(); + Presence presence = contact.getPresence(); + if (presence == null) + presence = new Presence(); + Date now = new Date(); presence.setLastSeen(new Date(now.getTime() - (activity.getIdleTime() * 1000))); contact.setPresence(presence); diff --git a/app/src/main/java/org/awesomeapp/messenger/provider/Imps.java b/app/src/main/java/org/awesomeapp/messenger/provider/Imps.java index 261e8e245..cb2fa7bc1 100644 --- a/app/src/main/java/org/awesomeapp/messenger/provider/Imps.java +++ b/app/src/main/java/org/awesomeapp/messenger/provider/Imps.java @@ -2556,6 +2556,26 @@ public static boolean messageExists (ContentResolver resolver, String id) return result; } + public static boolean messageExists (ContentResolver resolver, String id, int messageType) + { + boolean result = false; + + Uri.Builder builder = Messages.OTR_MESSAGES_CONTENT_URI_BY_PACKET_ID.buildUpon(); + builder.appendPath(id); + + String[] args = {messageType+""}; + + Cursor c = resolver.query(builder.build(),null, Imps.Messages.TYPE + "=?", args, null); + if (c != null) + { + if (c.getCount() > 0) + result = true; + + c.close(); + } + + return result; + } } diff --git a/app/src/main/java/org/awesomeapp/messenger/service/adapters/ChatSessionAdapter.java b/app/src/main/java/org/awesomeapp/messenger/service/adapters/ChatSessionAdapter.java index ec3b9ceea..e0815ed78 100644 --- a/app/src/main/java/org/awesomeapp/messenger/service/adapters/ChatSessionAdapter.java +++ b/app/src/main/java/org/awesomeapp/messenger/service/adapters/ChatSessionAdapter.java @@ -1126,8 +1126,7 @@ public boolean onIncomingMessage(ChatSession ses, final org.awesomeapp.messenger && msg.getType() == Imps.MessageType.INCOMING) return false; - - if (Imps.messageExists(mContentResolver,msg.getID())) + if (Imps.messageExists(mContentResolver,msg.getID(),msg.getType())) return false; String body = msg.getBody(); @@ -1140,6 +1139,7 @@ public boolean onIncomingMessage(ChatSession ses, final org.awesomeapp.messenger contact = mConnection.getContactListManager().getContactByAddress(bareUsername); nickname = contact.getName(); } catch (Exception e) { + return false; } long time = msg.getDateTime().getTime(); diff --git a/app/src/main/java/org/awesomeapp/messenger/ui/ConversationView.java b/app/src/main/java/org/awesomeapp/messenger/ui/ConversationView.java index 636e796c5..45e12c2ef 100644 --- a/app/src/main/java/org/awesomeapp/messenger/ui/ConversationView.java +++ b/app/src/main/java/org/awesomeapp/messenger/ui/ConversationView.java @@ -2057,7 +2057,7 @@ private IChatSession createChatSession() { } else { - remoteAddress = Address.stripResource(mRemoteAddress); + //remoteAddress = Address.stripResource(mRemoteAddress); session = sessionMgr.createChatSession(remoteAddress,false); }