diff options
author | Christian Schneppe <christian@pix-art.de> | 2019-09-12 19:44:01 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2019-09-12 19:44:01 +0200 |
commit | 85b8ec7702f5423c82d601c9c265415de344e26e (patch) | |
tree | 2b62a61dc8fb9540781b44daf781e5fad8a61d40 /src/main | |
parent | 0abffde5ef442e8bff1ce7cbfe84bf7e750b3136 (diff) |
show language in message bubble if multiple language variants were received
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.
Disabling 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 we now show the displayed language in a corner of the
message bubble if more than one translation has been received.
If multiple languages are received we 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 we will discard (not show at all) messages with with
multiple bodies of the same language. (This is considered an invalid message)
The language tag will not be shown if we receive 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.
Diffstat (limited to 'src/main')
10 files changed, 130 insertions, 59 deletions
diff --git a/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlMessage.java b/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlMessage.java index c89ad2172..5d6b9529b 100644 --- a/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlMessage.java +++ b/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlMessage.java @@ -134,7 +134,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); } diff --git a/src/main/java/de/pixart/messenger/entities/IndividualMessage.java b/src/main/java/de/pixart/messenger/entities/IndividualMessage.java index 195ea82c1..3daa27964 100644 --- a/src/main/java/de/pixart/messenger/entities/IndividualMessage.java +++ b/src/main/java/de/pixart/messenger/entities/IndividualMessage.java @@ -41,8 +41,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, boolean deleted, String edited, boolean oob, String errorMessage, Set<ReadByMarker> readByMarkers, boolean markable, boolean file_deleted) { - super(conversation, uuid, conversationUUid, counterpart, trueCounterpart, body, timeSent, encryption, status, type, carbon, remoteMsgId, relativeFilePath, serverMsgId, fingerprint, read, deleted, edited, oob, errorMessage, readByMarkers, markable, file_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, boolean deleted, String edited, boolean oob, String errorMessage, Set<ReadByMarker> readByMarkers, boolean markable, boolean file_deleted, String bodyLanguage) { + super(conversation, uuid, conversationUUid, counterpart, trueCounterpart, body, timeSent, encryption, status, type, carbon, remoteMsgId, relativeFilePath, serverMsgId, fingerprint, read, deleted, edited, oob, errorMessage, readByMarkers, markable, file_deleted, bodyLanguage); } public static Message createDateSeparator(Message message) { @@ -100,7 +100,9 @@ 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(FILE_DELETED)) > 0); + cursor.getInt(cursor.getColumnIndex(FILE_DELETED)) > 0, + cursor.getString(cursor.getColumnIndex(BODY_LANGUAGE)) + ); } @Override diff --git a/src/main/java/de/pixart/messenger/entities/Message.java b/src/main/java/de/pixart/messenger/entities/Message.java index e67519180..0010222d8 100644 --- a/src/main/java/de/pixart/messenger/entities/Message.java +++ b/src/main/java/de/pixart/messenger/entities/Message.java @@ -64,6 +64,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"; @@ -102,6 +103,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable protected boolean read = true; protected boolean deleted = false; protected String remoteMsgId = null; + private String bodyLanguage = null; protected String serverMsgId = null; private final Conversational conversation; protected Transferable transferable = null; @@ -150,7 +152,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, @@ -159,7 +162,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 boolean deleted, final String edited, final boolean oob, final String errorMessage, final Set<ReadByMarker> readByMarkers, - final boolean markable, final boolean file_deleted) { + final boolean markable, final boolean file_deleted, final String bodyLanguage) { this.conversation = conversation; this.uuid = uuid; this.conversationUuid = conversationUUid; @@ -183,6 +186,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable this.readByMarkers = readByMarkers == null ? new HashSet<ReadByMarker>() : readByMarkers; this.markable = markable; this.file_deleted = file_deleted; + this.bodyLanguage = bodyLanguage; } public static Message fromCursor(Cursor cursor, Conversation conversation) { @@ -208,7 +212,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(FILE_DELETED)) > 0); + cursor.getInt(cursor.getColumnIndex(FILE_DELETED)) > 0, + cursor.getString(cursor.getColumnIndex(BODY_LANGUAGE)) + ); } private static Jid fromString(String value) { @@ -270,6 +276,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(FILE_DELETED, file_deleted ? 1 : 0); + values.put(BODY_LANGUAGE, bodyLanguage); return values; } @@ -440,6 +447,14 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable this.edited = edited; } + public String getBodyLanguage() { + return this.bodyLanguage; + } + + public void setBodyLanguage(String language) { + this.bodyLanguage = language; + } + public boolean edited() { return this.edited != null; } diff --git a/src/main/java/de/pixart/messenger/parser/MessageParser.java b/src/main/java/de/pixart/messenger/parser/MessageParser.java index e07ac2615..601bd1e56 100644 --- a/src/main/java/de/pixart/messenger/parser/MessageParser.java +++ b/src/main/java/de/pixart/messenger/parser/MessageParser.java @@ -43,6 +43,7 @@ import de.pixart.messenger.services.XmppConnectionService; import de.pixart.messenger.utils.CryptoHelper; import de.pixart.messenger.utils.Namespace; import de.pixart.messenger.xml.Element; +import de.pixart.messenger.xml.LocalizedContent; import de.pixart.messenger.xmpp.InvalidJid; import de.pixart.messenger.xmpp.OnMessagePacketReceived; import de.pixart.messenger.xmpp.chatstate.ChatState; @@ -430,7 +431,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"); @@ -439,7 +440,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(); @@ -508,10 +509,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 { @@ -519,15 +523,18 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece } } final Message message; - if (body != null && body.startsWith("?OTR") && Config.supportOtr()) { + if (body.content != null && body.content.startsWith("?OTR") && Config.supportOtr()) { if (!isForwarded && !isTypeGroupChat && isProperlyAddressed && !conversationMultiMode) { - message = parseOtrChat(body, from, remoteMsgId, conversation); + message = parseOtrChat(body.content, from, remoteMsgId, conversation); if (message == null) { return; } } else { Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring OTR message from " + from + " isForwarded=" + Boolean.toString(isForwarded) + ", isProperlyAddressed=" + Boolean.valueOf(isProperlyAddressed)); - 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); + } } } else if (xP1S3url != null) { message = new Message(conversation, xP1S3url.toString(), Message.ENCRYPTION_NONE, status); @@ -599,7 +606,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); @@ -607,7 +617,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); @@ -814,11 +824,11 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece mXmppConnectionService.updateConversationUi(); } 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(); diff --git a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java index e9f7c0576..549771a5f 100644 --- a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java @@ -63,7 +63,7 @@ import rocks.xmpp.addr.Jid; public class DatabaseBackend extends SQLiteOpenHelper { public static final String DATABASE_NAME = "history"; - public static final int DATABASE_VERSION = 48; // = Conversations DATABASE_VERSION + 4 + public static final int DATABASE_VERSION = 49; // = Conversations DATABASE_VERSION + 4 private static DatabaseBackend instance = null; private static String CREATE_CONTATCS_STATEMENT = "create table " @@ -241,6 +241,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { + Message.READ_BY_MARKERS + " TEXT," + Message.MARKABLE + " NUMBER DEFAULT 0," + Message.FILE_DELETED + " NUMBER DEFAULT 0," + + Message.BODY_LANGUAGE + " TEXT," + Message.REMOTE_MSG_ID + " TEXT, FOREIGN KEY(" + Message.CONVERSATION + ") REFERENCES " + Conversation.TABLENAME + "(" + Conversation.UUID @@ -569,6 +570,10 @@ public class DatabaseBackend extends SQLiteOpenHelper { //ignore } } + + if (oldVersion < 49 && newVersion >= 49) { + db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.BODY_LANGUAGE); + } } private boolean isColumnExisting(SQLiteDatabase db, String TableName, String ColumnName) { diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java index ed8541273..02f8945de 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -351,30 +351,33 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie viewHolder.indicator.setVisibility(View.VISIBLE); } - String formatedTime = UIHelper.readableTimeDifferenceFull(getContext(), message.getMergedTimeSent()); + 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 + bodyLanguage); } 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); } } } diff --git a/src/main/java/de/pixart/messenger/xml/Element.java b/src/main/java/de/pixart/messenger/xml/Element.java index d1d3c50e9..c833575fa 100644 --- a/src/main/java/de/pixart/messenger/xml/Element.java +++ b/src/main/java/de/pixart/messenger/xml/Element.java @@ -69,31 +69,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) { diff --git a/src/main/java/de/pixart/messenger/xml/LocalizedContent.java b/src/main/java/de/pixart/messenger/xml/LocalizedContent.java new file mode 100644 index 000000000..be7c2e77e --- /dev/null +++ b/src/main/java/de/pixart/messenger/xml/LocalizedContent.java @@ -0,0 +1,57 @@ +package de.pixart.messenger.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()); + } +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java index 54f72754c..571297851 100644 --- a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java +++ b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java @@ -82,6 +82,7 @@ import de.pixart.messenger.utils.SSLSocketHelper; import de.pixart.messenger.utils.SocksSocketFactory; import de.pixart.messenger.utils.XmlHelper; import de.pixart.messenger.xml.Element; +import de.pixart.messenger.xml.LocalizedContent; import de.pixart.messenger.xml.Tag; import de.pixart.messenger.xml.TagWriter; import de.pixart.messenger.xml.XmlReader; @@ -1454,7 +1455,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); diff --git a/src/main/java/de/pixart/messenger/xmpp/stanzas/MessagePacket.java b/src/main/java/de/pixart/messenger/xmpp/stanzas/MessagePacket.java index 2f7d7e071..8b63527c4 100644 --- a/src/main/java/de/pixart/messenger/xmpp/stanzas/MessagePacket.java +++ b/src/main/java/de/pixart/messenger/xmpp/stanzas/MessagePacket.java @@ -4,6 +4,7 @@ import android.util.Pair; import de.pixart.messenger.parser.AbstractParser; import de.pixart.messenger.xml.Element; +import de.pixart.messenger.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) { |