show language in message bubble if multiple language variants were received

Daniel Gultsch created

XML and by inheritence XMPP has the feature of transmitting multiple language
variants for the same content. This can be really useful if, for example, you
are talking to an automated system. A chat bot could greet you in your own
language.

On the wire this will usually look like this:

```xml
<message to="you">
  <body>Good morning</body>
  <body xml:lang="de">Guten Morgen</body>
</message>
```

However receiving such a message in a group chat can be very confusing and
potentially dangerous if the sender puts conflicting information in there and
different people get shown different strings.

Disabeling support for localization entirely isn’t an ideal solution as on
principle it is still a good feature; and other clients might still show a
localization even if Conversations would always show the default language.

So instead Conversations now shows the displayed language in a corner of the
message bubble if more than one translation has been received.

If multiple languages are received Conversations will attempt to find one in
the language the operating system is set to. If no such translation can be
found it will attempt to display the English string.

If English can not be found either (for example a message that only has ru and
fr on a phone that is set to de) it will display what ever language came first.

Furthermore Conversations will discard (not show at all) messages with with
multiple bodies of the same language. (This is considered an invalid message)

The lanuage tag will not be shown if Conversations received a single body in
a language not understood by the user. (For example operating system set to
'de' and message received with one body in 'ru' will just display that body as
usual.)

As a guide line to the user: If you are reading a message where it is important
that this message is not interpreted differently by different people (like a
vote (+1 / -1) in a chat room) make sure it has *no* language tag.

Change summary

src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java |  2 
src/main/java/eu/siacs/conversations/entities/IndividualMessage.java        |  8 
src/main/java/eu/siacs/conversations/entities/Message.java                  | 21 
src/main/java/eu/siacs/conversations/parser/MessageParser.java              | 29 
src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java       |  7 
src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java         | 23 
src/main/java/eu/siacs/conversations/xml/Element.java                       | 31 
src/main/java/eu/siacs/conversations/xml/LocalizedContent.java              | 57 
src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java               |  3 
src/main/java/eu/siacs/conversations/xmpp/stanzas/MessagePacket.java        |  5 
10 files changed, 125 insertions(+), 61 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java πŸ”—

@@ -135,7 +135,7 @@ public class XmppAxolotlMessage {
 					break;
 			}
 		}
-		Element payloadElement = axolotlMessage.findChild(PAYLOAD);
+		Element payloadElement = axolotlMessage.findChild(PAYLOAD); //TODO make sure we only have _one_ paypload
 		if (payloadElement != null) {
 			ciphertext = Base64.decode(payloadElement.getContent().trim(), Base64.DEFAULT);
 		}

src/main/java/eu/siacs/conversations/entities/IndividualMessage.java πŸ”—

@@ -43,8 +43,8 @@ public class IndividualMessage extends Message {
 		super(conversation);
 	}
 
-	private IndividualMessage(Conversational conversation, String uuid, String conversationUUid, Jid counterpart, Jid trueCounterpart, String body, long timeSent, int encryption, int status, int type, boolean carbon, String remoteMsgId, String relativeFilePath, String serverMsgId, String fingerprint, boolean read, String edited, boolean oob, String errorMessage, Set<ReadByMarker> readByMarkers, boolean markable, boolean deleted) {
-		super(conversation, uuid, conversationUUid, counterpart, trueCounterpart, body, timeSent, encryption, status, type, carbon, remoteMsgId, relativeFilePath, serverMsgId, fingerprint, read, edited, oob, errorMessage, readByMarkers, markable, deleted);
+	private IndividualMessage(Conversational conversation, String uuid, String conversationUUid, Jid counterpart, Jid trueCounterpart, String body, long timeSent, int encryption, int status, int type, boolean carbon, String remoteMsgId, String relativeFilePath, String serverMsgId, String fingerprint, boolean read, String edited, boolean oob, String errorMessage, Set<ReadByMarker> readByMarkers, boolean markable, boolean deleted, String bodyLanguage) {
+		super(conversation, uuid, conversationUUid, counterpart, trueCounterpart, body, timeSent, encryption, status, type, carbon, remoteMsgId, relativeFilePath, serverMsgId, fingerprint, read, edited, oob, errorMessage, readByMarkers, markable, deleted, bodyLanguage);
 	}
 
 	@Override
@@ -116,6 +116,8 @@ public class IndividualMessage extends Message {
 				cursor.getString(cursor.getColumnIndex(ERROR_MESSAGE)),
 				ReadByMarker.fromJsonString(cursor.getString(cursor.getColumnIndex(READ_BY_MARKERS))),
 				cursor.getInt(cursor.getColumnIndex(MARKABLE)) > 0,
-				cursor.getInt(cursor.getColumnIndex(DELETED)) > 0);
+				cursor.getInt(cursor.getColumnIndex(DELETED)) > 0,
+				cursor.getString(cursor.getColumnIndex(BODY_LANGUAGE))
+		);
 	}
 }

src/main/java/eu/siacs/conversations/entities/Message.java πŸ”—

@@ -62,6 +62,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
 	public static final String COUNTERPART = "counterpart";
 	public static final String TRUE_COUNTERPART = "trueCounterpart";
 	public static final String BODY = "body";
+	public static final String BODY_LANGUAGE = "bodyLanguage";
 	public static final String TIME_SENT = "timeSent";
 	public static final String ENCRYPTION = "encryption";
 	public static final String STATUS = "status";
@@ -100,6 +101,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
 	protected String relativeFilePath;
 	protected boolean read = true;
 	protected String remoteMsgId = null;
+	private String bodyLanguage = null;
 	protected String serverMsgId = null;
 	private final Conversational conversation;
 	protected Transferable transferable = null;
@@ -145,7 +147,8 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
 				null,
 				null,
 				false,
-				false);
+				false,
+				null);
 	}
 
 	protected Message(final Conversational conversation, final String uuid, final String conversationUUid, final Jid counterpart,
@@ -154,7 +157,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
 	                final String remoteMsgId, final String relativeFilePath,
 	                final String serverMsgId, final String fingerprint, final boolean read,
 	                final String edited, final boolean oob, final String errorMessage, final Set<ReadByMarker> readByMarkers,
-	                final boolean markable, final boolean deleted) {
+	                final boolean markable, final boolean deleted, final String bodyLanguage) {
 		this.conversation = conversation;
 		this.uuid = uuid;
 		this.conversationUuid = conversationUUid;
@@ -177,6 +180,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
 		this.readByMarkers = readByMarkers == null ? new HashSet<>() : readByMarkers;
 		this.markable = markable;
 		this.deleted = deleted;
+		this.bodyLanguage = bodyLanguage;
 	}
 
 	public static Message fromCursor(Cursor cursor, Conversation conversation) {
@@ -201,7 +205,9 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
 				cursor.getString(cursor.getColumnIndex(ERROR_MESSAGE)),
 				ReadByMarker.fromJsonString(cursor.getString(cursor.getColumnIndex(READ_BY_MARKERS))),
 				cursor.getInt(cursor.getColumnIndex(MARKABLE)) > 0,
-				cursor.getInt(cursor.getColumnIndex(DELETED)) > 0);
+				cursor.getInt(cursor.getColumnIndex(DELETED)) > 0,
+				cursor.getString(cursor.getColumnIndex(BODY_LANGUAGE))
+		);
 	}
 
 	private static Jid fromString(String value) {
@@ -266,6 +272,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
 		values.put(READ_BY_MARKERS, ReadByMarker.toJson(readByMarkers).toString());
 		values.put(MARKABLE, markable ? 1 : 0);
 		values.put(DELETED, deleted ? 1 : 0);
+		values.put(BODY_LANGUAGE, bodyLanguage);
 		return values;
 	}
 
@@ -430,6 +437,14 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
 		this.edits.add(new Edited(edited, serverMsgId));
 	}
 
+	public String getBodyLanguage() {
+		return this.bodyLanguage;
+	}
+
+	public void setBodyLanguage(String language) {
+		this.bodyLanguage = language;
+	}
+
 	public boolean edited() {
 		return this.edits.size() > 0;
 	}

src/main/java/eu/siacs/conversations/parser/MessageParser.java πŸ”—

@@ -33,6 +33,7 @@ import eu.siacs.conversations.services.MessageArchiveService;
 import eu.siacs.conversations.services.QuickConversationsService;
 import eu.siacs.conversations.services.XmppConnectionService;
 import eu.siacs.conversations.utils.CryptoHelper;
+import eu.siacs.conversations.xml.LocalizedContent;
 import eu.siacs.conversations.xml.Namespace;
 import eu.siacs.conversations.xml.Element;
 import eu.siacs.conversations.xmpp.InvalidJid;
@@ -328,7 +329,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
         if (timestamp == null) {
             timestamp = AbstractParser.parseTimestamp(original, AbstractParser.parseTimestamp(packet));
         }
-        final String body = packet.getBody();
+        final LocalizedContent body = packet.getBody();
         final Element mucUserElement = packet.findChild("x", "http://jabber.org/protocol/muc#user");
         final String pgpEncrypted = packet.findChildContent("x", "jabber:x:encrypted");
         final Element replaceElement = packet.findChild("replace", "urn:xmpp:message-correct:0");
@@ -337,7 +338,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
         final URL xP1S3url = xP1S3 == null ? null : P1S3UrlStreamHandler.of(xP1S3);
         final String oobUrl = oob != null ? oob.findChildContent("url") : null;
         final String replacementId = replaceElement == null ? null : replaceElement.getAttribute("id");
-        final Element axolotlEncrypted = packet.findChild(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX);
+        final Element axolotlEncrypted = packet.findChild(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX); //TODO make sure we only have _one_ axolotl element!
         int status;
         final Jid counterpart;
         final Jid to = packet.getTo();
@@ -409,10 +410,13 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
                     if (mXmppConnectionService.markMessage(conversation, remoteMsgId, status, serverMsgId)) {
                         return;
                     } else if (remoteMsgId == null || Config.IGNORE_ID_REWRITE_IN_MUC) {
-                        Message message = conversation.findSentMessageWithBody(packet.getBody());
-                        if (message != null) {
-                            mXmppConnectionService.markMessage(message, status);
-                            return;
+                        LocalizedContent localizedBody = packet.getBody();
+                        if (localizedBody != null) {
+                            Message message = conversation.findSentMessageWithBody(localizedBody.content);
+                            if (message != null) {
+                                mXmppConnectionService.markMessage(message, status);
+                                return;
+                            }
                         }
                     }
                 } else {
@@ -491,7 +495,10 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
                     message.setEncryption(Message.ENCRYPTION_DECRYPTED);
                 }
             } else {
-                message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
+                message = new Message(conversation, body.content, Message.ENCRYPTION_NONE, status);
+                if (body.count > 1) {
+                    message.setBodyLanguage(body.language);
+                }
             }
 
             message.setCounterpart(counterpart);
@@ -499,7 +506,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
             message.setServerMsgId(serverMsgId);
             message.setCarbon(isCarbon);
             message.setTime(timestamp);
-            if (body != null && body.equals(oobUrl)) {
+            if (body != null && body.content != null && body.content.equals(oobUrl)) {
                 message.setOob(true);
                 if (CryptoHelper.isPgpEncryptedUrl(oobUrl)) {
                     message.setEncryption(Message.ENCRYPTION_DECRYPTED);
@@ -702,11 +709,11 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
             }
 
             if (isTypeGroupChat) {
-                if (packet.hasChild("subject")) {
+                if (packet.hasChild("subject")) { //TODO usually we would want to check for lack of body; however some servers do set a body :(
                     if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
                         conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0);
-                        String subject = packet.findInternationalizedChildContent("subject");
-                        if (conversation.getMucOptions().setSubject(subject)) {
+                        final LocalizedContent subject = packet.findInternationalizedChildContentInDefaultNamespace("subject");
+                        if (subject != null && conversation.getMucOptions().setSubject(subject.content)) {
                             mXmppConnectionService.updateConversation(conversation);
                         }
                         mXmppConnectionService.updateConversationUi();

src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java πŸ”—

@@ -62,7 +62,7 @@ import rocks.xmpp.addr.Jid;
 public class DatabaseBackend extends SQLiteOpenHelper {
 
     private static final String DATABASE_NAME = "history";
-    private static final int DATABASE_VERSION = 44;
+    private static final int DATABASE_VERSION = 45;
     private static DatabaseBackend instance = null;
     private static String CREATE_CONTATCS_STATEMENT = "create table "
             + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
@@ -239,6 +239,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
                 + Message.READ_BY_MARKERS + " TEXT,"
                 + Message.MARKABLE + " NUMBER DEFAULT 0,"
                 + Message.DELETED + " NUMBER DEFAULT 0,"
+                + Message.BODY_LANGUAGE + " TEXT,"
                 + Message.REMOTE_MSG_ID + " TEXT, FOREIGN KEY("
                 + Message.CONVERSATION + ") REFERENCES "
                 + Conversation.TABLENAME + "(" + Conversation.UUID
@@ -540,6 +541,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
             db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX);
             db.execSQL(CREATE_MESSAGE_TYPE_INDEX);
         }
+
+        if (oldVersion < 45 && newVersion >= 45) {
+            db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.BODY_LANGUAGE);
+        }
     }
 
     private void canonicalizeJids(SQLiteDatabase db) {

src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java πŸ”—

@@ -36,6 +36,7 @@ import com.google.common.base.Strings;
 
 import java.net.URL;
 import java.util.List;
+import java.util.Locale;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -283,30 +284,32 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
 			viewHolder.indicator.setVisibility(View.VISIBLE);
 		}
 
-		String formatedTime = UIHelper.readableTimeDifferenceFull(getContext(), message.getMergedTimeSent());
-		if (message.getStatus() <= Message.STATUS_RECEIVED) {
+		final String formattedTime = UIHelper.readableTimeDifferenceFull(getContext(), message.getMergedTimeSent());
+		final String bodyLanguage = message.getBodyLanguage();
+		final String bodyLanguageInfo = bodyLanguage == null ? "" : String.format(" \u00B7 %s", bodyLanguage.toUpperCase(Locale.US));
+		if (message.getStatus() <= Message.STATUS_RECEIVED) { ;
 			if ((filesize != null) && (info != null)) {
-				viewHolder.time.setText(formatedTime + " \u00B7 " + filesize + " \u00B7 " + info);
+				viewHolder.time.setText(formattedTime + " \u00B7 " + filesize + " \u00B7 " + info + bodyLanguageInfo);
 			} else if ((filesize == null) && (info != null)) {
-				viewHolder.time.setText(formatedTime + " \u00B7 " + info);
+				viewHolder.time.setText(formattedTime + " \u00B7 " + info + bodyLanguageInfo);
 			} else if ((filesize != null) && (info == null)) {
-				viewHolder.time.setText(formatedTime + " \u00B7 " + filesize);
+				viewHolder.time.setText(formattedTime + " \u00B7 " + filesize + bodyLanguageInfo);
 			} else {
-				viewHolder.time.setText(formatedTime);
+				viewHolder.time.setText(formattedTime+bodyLanguageInfo);
 			}
 		} else {
 			if ((filesize != null) && (info != null)) {
-				viewHolder.time.setText(filesize + " \u00B7 " + info);
+				viewHolder.time.setText(filesize + " \u00B7 " + info + bodyLanguageInfo);
 			} else if ((filesize == null) && (info != null)) {
 				if (error) {
-					viewHolder.time.setText(info + " \u00B7 " + formatedTime);
+					viewHolder.time.setText(info + " \u00B7 " + formattedTime + bodyLanguageInfo);
 				} else {
 					viewHolder.time.setText(info);
 				}
 			} else if ((filesize != null) && (info == null)) {
-				viewHolder.time.setText(filesize + " \u00B7 " + formatedTime);
+				viewHolder.time.setText(filesize + " \u00B7 " + formattedTime + bodyLanguageInfo);
 			} else {
-				viewHolder.time.setText(formatedTime);
+				viewHolder.time.setText(formattedTime+bodyLanguageInfo);
 			}
 		}
 	}

src/main/java/eu/siacs/conversations/xml/Element.java πŸ”—

@@ -1,15 +1,11 @@
 package eu.siacs.conversations.xml;
 
-import android.support.annotation.NonNull;
-import android.util.Log;
-
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Locale;
 
-import eu.siacs.conversations.Config;
 import eu.siacs.conversations.utils.XmlHelper;
 import eu.siacs.conversations.xmpp.InvalidJid;
 import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
@@ -71,31 +67,8 @@ public class Element {
 		return element == null ? null : element.getContent();
 	}
 
-	public String findInternationalizedChildContent(String name) {
-		return findInternationalizedChildContent(name, Locale.getDefault().getLanguage());
-	}
-
-	private String findInternationalizedChildContent(String name, @NonNull String language) {
-		final HashMap<String,String> contents = new HashMap<>();
-		for(Element child : this.children) {
-			if (name.equals(child.getName())) {
-				String lang = child.getAttribute("xml:lang");
-				String content = child.getContent();
-				if (content != null) {
-					if (language.equals(lang)) {
-						return content;
-					} else {
-						contents.put(lang, content);
-					}
-				}
-			}
-		}
-		final String value = contents.get(null);
-		if (value != null) {
-			return value;
-		}
-		final String[] values = contents.values().toArray(new String[0]);
-		return values.length == 0 ? null : values[0];
+	public LocalizedContent findInternationalizedChildContentInDefaultNamespace(String name) {
+		return LocalizedContent.get(this, name);
 	}
 
 	public Element findChild(String name, String xmlns) {

src/main/java/eu/siacs/conversations/xml/LocalizedContent.java πŸ”—

@@ -0,0 +1,57 @@
+package eu.siacs.conversations.xml;
+
+import com.google.common.collect.Iterables;
+
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+public class LocalizedContent {
+
+    public static final String STREAM_LANGUAGE = "en";
+
+    public final String content;
+    public final String language;
+    public final int count;
+
+    private LocalizedContent(String content, String language, int count) {
+        this.content = content;
+        this.language = language;
+        this.count = count;
+    }
+
+    public static LocalizedContent get(final Element element, String name) {
+        final HashMap<String, String> contents = new HashMap<>();
+        for(Element child : element.children) {
+            if (name.equals(child.getName())) {
+                final String namespace = child.getNamespace();
+                final String lang = child.getAttribute("xml:lang");
+                final String content = child.getContent();
+                if (content != null && (namespace == null || "jabber:client".equals(namespace))) {
+                    if (contents.put(lang, content) != null) {
+                        //anything that has multiple contents for the same language is invalid
+                        return null;
+                    }
+                }
+            }
+        }
+        if (contents.size() == 0) {
+            return null;
+        }
+        final String userLanguage = Locale.getDefault().getLanguage();
+        final String localized = contents.get(userLanguage);
+        if (localized != null) {
+            return new LocalizedContent(localized, userLanguage, contents.size());
+        }
+        final String defaultLanguageContent = contents.get(null);
+        if (defaultLanguageContent != null) {
+            return new LocalizedContent(defaultLanguageContent, STREAM_LANGUAGE, contents.size());
+        }
+        final String streamLanguageContent = contents.get(STREAM_LANGUAGE);
+        if (streamLanguageContent != null) {
+            return new LocalizedContent(streamLanguageContent, STREAM_LANGUAGE, contents.size());
+        }
+        final Map.Entry<String, String> first = Iterables.get(contents.entrySet(), 0);
+        return new LocalizedContent(first.getValue(), first.getKey(), contents.size());
+    }
+}

src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java πŸ”—

@@ -80,6 +80,7 @@ import eu.siacs.conversations.utils.SSLSocketHelper;
 import eu.siacs.conversations.utils.SocksSocketFactory;
 import eu.siacs.conversations.utils.XmlHelper;
 import eu.siacs.conversations.xml.Element;
+import eu.siacs.conversations.xml.LocalizedContent;
 import eu.siacs.conversations.xml.Namespace;
 import eu.siacs.conversations.xml.Tag;
 import eu.siacs.conversations.xml.TagWriter;
@@ -1334,7 +1335,7 @@ public class XmppConnection implements Runnable {
         final Tag stream = Tag.start("stream:stream");
         stream.setAttribute("to", account.getServer());
         stream.setAttribute("version", "1.0");
-        stream.setAttribute("xml:lang", "en");
+        stream.setAttribute("xml:lang", LocalizedContent.STREAM_LANGUAGE);
         stream.setAttribute("xmlns", "jabber:client");
         stream.setAttribute("xmlns:stream", "http://etherx.jabber.org/streams");
         tagWriter.writeTag(stream);

src/main/java/eu/siacs/conversations/xmpp/stanzas/MessagePacket.java πŸ”—

@@ -4,6 +4,7 @@ import android.util.Pair;
 
 import eu.siacs.conversations.parser.AbstractParser;
 import eu.siacs.conversations.xml.Element;
+import eu.siacs.conversations.xml.LocalizedContent;
 
 public class MessagePacket extends AbstractAcknowledgeableStanza {
 	public static final int TYPE_CHAT = 0;
@@ -16,8 +17,8 @@ public class MessagePacket extends AbstractAcknowledgeableStanza {
 		super("message");
 	}
 
-	public String getBody() {
-		return findChildContent("body");
+	public LocalizedContent getBody() {
+		return findInternationalizedChildContentInDefaultNamespace("body");
 	}
 
 	public void setBody(String text) {