From 377bd49ee807b9a06089ad5612ca15b9b00f2e36 Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Sun, 9 Nov 2014 16:21:13 +0100 Subject: [PATCH] refactored code to always contain full jid for counterpart --- .../conversations/entities/Conversation.java | 10 +-- .../siacs/conversations/entities/Message.java | 17 ----- .../conversations/parser/MessageParser.java | 6 +- .../services/XmppConnectionService.java | 66 ++++++++++++------- .../ui/ConversationFragment.java | 26 ++++---- .../siacs/conversations/ui/XmppActivity.java | 19 ++++-- .../ui/adapter/MessageAdapter.java | 24 +++---- .../xmpp/jingle/JingleConnection.java | 2 +- 8 files changed, 84 insertions(+), 86 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index fdffeccba..c8dedd7b3 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -55,7 +55,7 @@ public class Conversation extends AbstractEntity { private JSONObject attributes = new JSONObject(); - private String nextPresence; + private Jid nextCounterpart; protected ArrayList messages = new ArrayList<>(); protected Account account = null; @@ -345,12 +345,12 @@ public class Conversation extends AbstractEntity { this.contactJid = jid; } - public void setNextPresence(String presence) { - this.nextPresence = presence; + public void setNextCounterpart(Jid jid) { + this.nextCounterpart = jid; } - public String getNextPresence() { - return this.nextPresence; + public Jid getNextCounterpart() { + return this.nextCounterpart; } public int getLatestEncryption() { diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index f15b75277..550474bbc 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -232,27 +232,10 @@ public class Message extends AbstractEntity { return this.type; } - public void setPresence(String presence) { - if (presence == null) { - this.counterpart = this.counterpart.toBareJid(); - } else { - try { - this.counterpart = Jid.fromString(this.counterpart.toBareJid() + "/" + presence); - } catch (final InvalidJidException ignored) { - // TODO: Handle this? - } - } - } - public void setTrueCounterpart(String trueCounterpart) { this.trueCounterpart = trueCounterpart; } - public Jid getPresence() { - // TODO: This is now the same as getCounterpart()... find usages in code and remove one? - return counterpart; - } - public void setDownloadable(Downloadable downloadable) { this.downloadable = downloadable; } diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index 9796814bb..70d5b12b8 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -42,7 +42,7 @@ public class MessageParser extends AbstractParser implements if (conversation.getMode() == Conversation.MODE_MULTI && !jid.getResourcepart().isEmpty()) { finishedMessage.setType(Message.TYPE_PRIVATE); - finishedMessage.setPresence(jid.getResourcepart()); + finishedMessage.setCounterpart(packet.getFrom()); finishedMessage.setTrueCounterpart(conversation.getMucOptions() .getTrueCounterpart(jid.getResourcepart())); if (conversation.hasDuplicateMessage(finishedMessage)) { @@ -249,7 +249,7 @@ public class MessageParser extends AbstractParser implements if (conversation.getMode() == Conversation.MODE_MULTI && !fullJid.isBareJid()) { finishedMessage.setType(Message.TYPE_PRIVATE); - finishedMessage.setPresence(fullJid.getResourcepart()); + finishedMessage.setCounterpart(fullJid); finishedMessage.setTrueCounterpart(conversation.getMucOptions() .getTrueCounterpart(fullJid.getResourcepart())); if (conversation.hasDuplicateMessage(finishedMessage)) { @@ -478,7 +478,7 @@ public class MessageParser extends AbstractParser implements if (message.getStatus() == Message.STATUS_RECEIVED && conversation.getOtrSession() != null && !conversation.getOtrSession().getSessionID().getUserID() - .equals(message.getPresence())) { + .equals(message.getCounterpart().getResourcepart())) { conversation.endOtrIfNeeded(); } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index f2f9becdd..6d5006503 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -19,6 +19,7 @@ import de.duenndns.ssl.MemorizingTrustManager; import net.java.otr4j.OtrException; import net.java.otr4j.session.Session; +import net.java.otr4j.session.SessionID; import net.java.otr4j.session.SessionStatus; import eu.siacs.conversations.Config; import eu.siacs.conversations.R; @@ -308,7 +309,7 @@ public class XmppConnectionService extends Service { message = new Message(conversation, "", conversation.getNextEncryption(forceEncryption())); } - message.setPresence(conversation.getNextPresence()); + message.setCounterpart(conversation.getNextCounterpart()); message.setType(Message.TYPE_IMAGE); message.setStatus(Message.STATUS_OFFERED); new Thread(new Runnable() { @@ -559,11 +560,10 @@ public class XmppConnectionService extends Service { if (account.getStatus() == Account.STATUS_ONLINE && account.getXmppConnection() != null) { if (message.getType() == Message.TYPE_IMAGE) { - if (message.getPresence() != null) { + if (message.getCounterpart() != null) { if (message.getEncryption() == Message.ENCRYPTION_OTR) { - if (!conv.hasValidOtrSession() - && (message.getPresence() != null)) { - conv.startOtrSession(this, message.getPresence().toString(), + if (!conv.hasValidOtrSession()) { + conv.startOtrSession(this, message.getCounterpart().getResourcepart(), true); message.setStatus(Message.STATUS_WAITING); } else if (conv.hasValidOtrSession() @@ -582,18 +582,21 @@ public class XmppConnectionService extends Service { } } else { if (message.getEncryption() == Message.ENCRYPTION_OTR) { - if (!conv.hasValidOtrSession() - && (message.getPresence() != null)) { - conv.startOtrSession(this, message.getPresence().toString(), true); + if (!conv.hasValidOtrSession()&& (message.getCounterpart() != null)) { + conv.startOtrSession(this, message.getCounterpart().getResourcepart(), true); message.setStatus(Message.STATUS_WAITING); } else if (conv.hasValidOtrSession() && conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) { - message.setPresence(conv.getOtrSession().getSessionID() - .getUserID()); + SessionID id = conv.getOtrSession().getSessionID(); + try { + message.setCounterpart(Jid.fromString(id.getAccountID() + "/" + id.getUserID())); + } catch (final InvalidJidException e) { + message.setCounterpart(null); + } packet = mMessageGenerator.generateOtrChat(message); send = true; - } else if (message.getPresence() == null) { + } else if (message.getCounterpart() == null) { conv.startOtrIfNeeded(); message.setStatus(Message.STATUS_WAITING); } @@ -627,11 +630,15 @@ public class XmppConnectionService extends Service { message.setEncryption(Message.ENCRYPTION_DECRYPTED); } else if (message.getEncryption() == Message.ENCRYPTION_OTR) { if (conv.hasValidOtrSession()) { - message.setPresence(conv.getOtrSession().getSessionID() - .getUserID()); + SessionID id = conv.getOtrSession().getSessionID(); + try { + message.setCounterpart(Jid.fromString(id.getAccountID() + "/" + id.getUserID())); + } catch (final InvalidJidException e) { + message.setCounterpart(null); + } } else if (!conv.hasValidOtrSession() - && message.getPresence() != null) { - conv.startOtrSession(this, message.getPresence().toString(), false); + && message.getCounterpart() != null) { + conv.startOtrSession(this, message.getCounterpart().getResourcepart(), false); } } } @@ -666,10 +673,10 @@ public class XmppConnectionService extends Service { Presences presences = message.getConversation().getContact() .getPresences(); if (!message.getConversation().hasValidOtrSession()) { - if ((message.getPresence() != null) - && (presences.has(message.getPresence().toString()))) { + if ((message.getCounterpart() != null) + && (presences.has(message.getCounterpart().getResourcepart()))) { message.getConversation().startOtrSession(this, - message.getPresence().toString(), true); + message.getCounterpart().getResourcepart(), true); } else { if (presences.size() == 1) { String presence = presences.asStringArray()[0]; @@ -696,16 +703,20 @@ public class XmppConnectionService extends Service { packet = mMessageGenerator.generatePgpChat(message, true); } } else if (message.getType() == Message.TYPE_IMAGE) { - Presences presences = message.getConversation().getContact() - .getPresences(); - if ((message.getPresence() != null) - && (presences.has(message.getPresence().toString()))) { + Contact contact = message.getConversation().getContact(); + Presences presences = contact.getPresences(); + if ((message.getCounterpart() != null) + && (presences.has(message.getCounterpart().getResourcepart()))) { markMessage(message, Message.STATUS_OFFERED); mJingleConnectionManager.createNewConnection(message); } else { if (presences.size() == 1) { String presence = presences.asStringArray()[0]; - message.setPresence(presence); + try { + message.setCounterpart(Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), presence)); + } catch (InvalidJidException e) { + return; + } markMessage(message, Message.STATUS_OFFERED); mJingleConnectionManager.createNewConnection(message); } @@ -1371,7 +1382,12 @@ public class XmppConnectionService extends Service { for (Message msg : messages) { if ((msg.getStatus() == Message.STATUS_UNSEND || msg.getStatus() == Message.STATUS_WAITING) && (msg.getEncryption() == Message.ENCRYPTION_OTR)) { - msg.setPresence(otrSession.getSessionID().getUserID()); + SessionID id = otrSession.getSessionID(); + try { + msg.setCounterpart(Jid.fromString(id.getAccountID()+"/"+id.getUserID())); + } catch (InvalidJidException e) { + break; + } if (msg.getType() == Message.TYPE_TEXT) { MessagePacket outPacket = mMessageGenerator .generateOtrChat(msg, true); @@ -1400,7 +1416,7 @@ public class XmppConnectionService extends Service { packet.addChild("private", "urn:xmpp:carbons:2"); packet.addChild("no-copy", "urn:xmpp:hints"); packet.setAttribute("to", otrSession.getSessionID().getAccountID() + "/" - + otrSession.getSessionID().getUserID()); + + otrSession.getSessionID().getUserID()); try { packet.setBody(otrSession .transformSending(CryptoHelper.FILETRANSFER diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 58e29d300..cc0227287 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -190,7 +190,7 @@ public class ConversationFragment extends Fragment { } if (mEditMessage.getText().length() < 1) { if (this.conversation.getMode() == Conversation.MODE_MULTI) { - conversation.setNextPresence(null); + conversation.setNextCounterpart(null); updateChatMsgHint(); } return; @@ -199,10 +199,10 @@ public class ConversationFragment extends Fragment { .toString(), conversation.getNextEncryption(activity .forceEncryption())); if (conversation.getMode() == Conversation.MODE_MULTI) { - if (conversation.getNextPresence() != null) { - message.setPresence(conversation.getNextPresence()); + if (conversation.getNextCounterpart() != null) { + message.setCounterpart(conversation.getNextCounterpart()); message.setType(Message.TYPE_PRIVATE); - conversation.setNextPresence(null); + conversation.setNextCounterpart(null); } } if (conversation.getNextEncryption(activity.forceEncryption()) == Message.ENCRYPTION_OTR) { @@ -216,10 +216,10 @@ public class ConversationFragment extends Fragment { public void updateChatMsgHint() { if (conversation.getMode() == Conversation.MODE_MULTI - && conversation.getNextPresence() != null) { + && conversation.getNextCounterpart() != null) { this.mEditMessage.setHint(getString( R.string.send_private_message_to, - conversation.getNextPresence())); + conversation.getNextCounterpart().getResourcepart())); } else { switch (conversation.getNextEncryption(activity.forceEncryption())) { case Message.ENCRYPTION_NONE: @@ -279,8 +279,8 @@ public class ConversationFragment extends Fragment { public void onContactPictureClicked(Message message) { if (message.getStatus() <= Message.STATUS_RECEIVED) { if (message.getConversation().getMode() == Conversation.MODE_MULTI) { - if (message.getPresence() != null) { - highlightInConference(message.getPresence().getResourcepart()); + if (message.getCounterpart() != null) { + highlightInConference(message.getCounterpart().getResourcepart()); } else { highlightInConference(message .getContact().getDisplayName()); @@ -310,9 +310,7 @@ public class ConversationFragment extends Fragment { public void onContactPictureLongClicked(Message message) { if (message.getStatus() <= Message.STATUS_RECEIVED) { if (message.getConversation().getMode() == Conversation.MODE_MULTI) { - if (message.getPresence() != null) { - privateMessageWith(message.getPresence()); - } else { + if (message.getCounterpart() != null) { privateMessageWith(message.getCounterpart()); } } @@ -431,7 +429,7 @@ public class ConversationFragment extends Fragment { protected void privateMessageWith(final Jid counterpart) { this.mEditMessage.setText(""); - this.conversation.setNextPresence(counterpart.toString()); + this.conversation.setNextCounterpart(counterpart); updateChatMsgHint(); } @@ -465,7 +463,7 @@ public class ConversationFragment extends Fragment { this.activity = (ConversationActivity) getActivity(); this.conversation = conversation; if (this.conversation.getMode() == Conversation.MODE_MULTI) { - this.conversation.setNextPresence(null); + this.conversation.setNextCounterpart(null); } this.mEditMessage.setText(""); this.mEditMessage.append(this.conversation.getNextMessage()); @@ -835,7 +833,7 @@ public class ConversationFragment extends Fragment { @Override public void onPresenceSelected() { - message.setPresence(conversation.getNextPresence()); + message.setCounterpart(conversation.getNextCounterpart()); xmppService.sendMessage(message); messageSent(); } diff --git a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java index d2cf582b7..4bd64411d 100644 --- a/src/main/java/eu/siacs/conversations/ui/XmppActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/XmppActivity.java @@ -65,6 +65,7 @@ import eu.siacs.conversations.services.AvatarService; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder; import eu.siacs.conversations.utils.ExceptionHelper; +import eu.siacs.conversations.xmpp.jid.InvalidJidException; import eu.siacs.conversations.xmpp.jid.Jid; public abstract class XmppActivity extends Activity { @@ -399,7 +400,7 @@ public abstract class XmppActivity extends Activity { @Override public void onClick(DialogInterface dialog, int which) { - conversation.setNextPresence(null); + conversation.setNextCounterpart(null); if (listener != null) { listener.onPresenceSelected(); } @@ -450,7 +451,7 @@ public abstract class XmppActivity extends Activity { public void selectPresence(final Conversation conversation, final OnPresenceSelected listener) { - Contact contact = conversation.getContact(); + final Contact contact = conversation.getContact(); if (!contact.showInRoster()) { showAddToRosterDialog(conversation); } else { @@ -464,12 +465,16 @@ public abstract class XmppActivity extends Activity { || !contact.getOption(Contact.Options.FROM)) { warnMutalPresenceSubscription(conversation, listener); } else { - conversation.setNextPresence(null); + conversation.setNextCounterpart(null); listener.onPresenceSelected(); } } else if (presences.size() == 1) { String presence = presences.asStringArray()[0]; - conversation.setNextPresence(presence); + try { + conversation.setNextCounterpart(Jid.fromParts(contact.getJid().getLocalpart(),contact.getJid().getDomainpart(),presence)); + } catch (InvalidJidException e) { + conversation.setNextCounterpart(null); + } listener.onPresenceSelected(); } else { final StringBuilder presence = new StringBuilder(); @@ -500,7 +505,11 @@ public abstract class XmppActivity extends Activity { @Override public void onClick(DialogInterface dialog, int which) { - conversation.setNextPresence(presence.toString()); + try { + conversation.setNextCounterpart(Jid.fromParts(contact.getJid().getLocalpart(),contact.getJid().getDomainpart(),presence.toString())); + } catch (InvalidJidException e) { + conversation.setNextCounterpart(null); + } listener.onPresenceSelected(); } }); diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index 465d7bc32..cefe42a77 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -135,16 +135,10 @@ public class MessageAdapter extends ArrayAdapter { Contact contact = message.getContact(); if (contact != null) { info = contact.getDisplayName(); + } else if (message.getCounterpart() != null) { + info = message.getCounterpart().getResourcepart(); } else { - if (message.getPresence() != null) { - if (message.getPresence().isBareJid()) { - info = message.getPresence().toString(); - } else { - info = message.getPresence().getResourcepart(); - } - } else { - info = message.getCounterpart().toString(); - } + info = ""; } } break; @@ -232,11 +226,11 @@ public class MessageAdapter extends ArrayAdapter { privateMarker = activity .getString(R.string.private_message); } else { - final Jid to; - if (message.getPresence() != null) { - to = message.getPresence(); + final String to; + if (message.getCounterpart() != null) { + to = message.getCounterpart().getResourcepart(); } else { - to = message.getCounterpart(); + to = ""; } privateMarker = activity.getString(R.string.private_message_to, to); } @@ -417,9 +411,7 @@ public class MessageAdapter extends ArrayAdapter { if (contact != null) { viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(contact, activity.getPixel(48))); } else if (item.getConversation().getMode() == Conversation.MODE_MULTI) { - final Jid name = item.getPresence() != null ? item.getPresence() : item.getCounterpart(); - viewHolder.contact_picture.setImageBitmap(activity.avatarService().get( - name.isBareJid() ? name.toString() : name.getResourcepart(), + viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(item.getCounterpart().getResourcepart(), activity.getPixel(48))); } } else if (type == SENT) { diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java index a863775d7..6647c5ba4 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java @@ -262,7 +262,7 @@ public class JingleConnection implements Downloadable { this.mStatus = Downloadable.STATUS_OFFER; this.message.setDownloadable(this); final Jid from = packet.getFrom(); - this.message.setPresence(from.isBareJid() ? "" : from.getResourcepart()); + this.message.setCounterpart(from); this.account = account; this.initiator = packet.getFrom(); this.responder = this.account.getFullJid();