diff options
Diffstat (limited to 'src/main/java/de/pixart/messenger/entities')
-rw-r--r-- | src/main/java/de/pixart/messenger/entities/Message.java | 209 |
1 files changed, 97 insertions, 112 deletions
diff --git a/src/main/java/de/pixart/messenger/entities/Message.java b/src/main/java/de/pixart/messenger/entities/Message.java index e0b7029a5..6f516f45e 100644 --- a/src/main/java/de/pixart/messenger/entities/Message.java +++ b/src/main/java/de/pixart/messenger/entities/Message.java @@ -91,6 +91,12 @@ public class Message extends AbstractEntity { private String axolotlFingerprint = null; private String errorMessage = null; + private Boolean isGeoUri = null; + private Boolean isXmppUri = null; + private Boolean isEmojisOnly = null; + private Boolean treatAsDownloadable = null; + private FileParams fileParams = null; + private Message(Conversation conversation) { this.conversation = conversation; } @@ -196,21 +202,21 @@ public class Message extends AbstractEntity { public static Message createStatusMessage(Conversation conversation, String body) { final Message message = new Message(conversation); message.setType(Message.TYPE_STATUS); - message.setBody(body); + message.body = body; return message; } public static Message createLoadMoreMessage(Conversation conversation) { final Message message = new Message(conversation); message.setType(Message.TYPE_STATUS); - message.setBody("LOAD_MORE"); + message.body = "LOAD_MORE"; return message; } public static Message createDateSeparator(Message message) { final Message separator = new Message(message.getConversation()); separator.setType(Message.TYPE_STATUS); - separator.setBody(MessageAdapter.DATE_SEPARATOR_BODY); + separator.body = MessageAdapter.DATE_SEPARATOR_BODY; separator.setTime(message.getTimeSent()); return separator; } @@ -280,11 +286,16 @@ public class Message extends AbstractEntity { return body; } - public void setBody(String body) { + public synchronized void setBody(String body) { if (body == null) { throw new Error("You should not set the message body to null"); } this.body = body; + this.isGeoUri = null; + this.isXmppUri = null; + this.isEmojisOnly = null; + this.treatAsDownloadable = null; + this.fileParams = null; } public String getErrorMessage() { @@ -402,7 +413,8 @@ public class Message extends AbstractEntity { return this.transferable; } - public void setTransferable(Transferable transferable) { + public synchronized void setTransferable(Transferable transferable) { + this.fileParams = null; this.transferable = transferable; } @@ -497,16 +509,16 @@ public class Message extends AbstractEntity { this.edited() == message.edited() && (message.getTimeSent() - this.getTimeSent()) <= (Config.MESSAGE_MERGE_WINDOW * 1000) && this.getBody().length() + message.getBody().length() <= Config.MAX_DISPLAY_MESSAGE_CHARS && - !GeoHelper.isGeoUri(message.getBody()) && - !GeoHelper.isGeoUri(this.body) && + !message.isGeoUri()&& + !this.isGeoUri() && !message.treatAsDownloadable() && !this.treatAsDownloadable() && !message.getBody().startsWith(ME_COMMAND) && !this.getBody().startsWith(ME_COMMAND) && !this.bodyIsOnlyEmojis() && !message.bodyIsOnlyEmojis() && - !this.bodyIsXmpp() && - !message.bodyIsXmpp() && + !this.isXmppUri() && + !message.isXmppUri() && ((this.axolotlFingerprint == null && message.axolotlFingerprint == null) || this.axolotlFingerprint.equals(message.getFingerprint())) && UIHelper.sameDay(message.getTimeSent(), this.getTimeSent()) ); @@ -654,124 +666,97 @@ public class Message extends AbstractEntity { } } - public boolean treatAsDownloadable() { - if (body.trim().contains(" ")) { - return false; + public synchronized boolean treatAsDownloadable() { + if (treatAsDownloadable == null) { + if (body.trim().contains(" ")) { + treatAsDownloadable = false; + } + try { + final URL url = new URL(body); + final String ref = url.getRef(); + final String protocol = url.getProtocol(); + final boolean encrypted = ref != null && ref.matches("([A-Fa-f0-9]{2}){48}"); + treatAsDownloadable = (AesGcmURLStreamHandler.PROTOCOL_NAME.equalsIgnoreCase(protocol) && encrypted) + || (("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) && (oob || encrypted)); + } catch (MalformedURLException e) { + treatAsDownloadable = false; + } } - try { - final URL url = new URL(body); - final String ref = url.getRef(); - final String protocol = url.getProtocol(); - final boolean encrypted = ref != null && ref.matches("([A-Fa-f0-9]{2}){48}"); - return (AesGcmURLStreamHandler.PROTOCOL_NAME.equalsIgnoreCase(protocol) && encrypted) - || (("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) && (oob || encrypted)); + return treatAsDownloadable; + } - } catch (MalformedURLException e) { - return false; + public synchronized boolean bodyIsOnlyEmojis() { + if (isEmojisOnly == null) { + isEmojisOnly = EmojiManager.isOnlyEmojis(body.replaceAll("\\s", "")); } + return isEmojisOnly; } - public boolean bodyIsOnlyEmojis() { - return EmojiManager.isOnlyEmojis(body.replaceAll("\\s", "")); + public synchronized boolean isXmppUri() { + if (isXmppUri == null) { + isXmppUri = XmppUri.isXmppUri(body.trim()); + } + return isXmppUri; } - public boolean bodyIsXmpp() { - return body != null && XmppUri.isXmppUri(body.trim()); + public synchronized boolean isGeoUri() { + if (isGeoUri == null) { + isGeoUri = GeoHelper.GEO_URI.matcher(body).matches(); + } + return isGeoUri; } - public FileParams getFileParams() { - FileParams params = getLegacyFileParams(); - if (params != null) { - return params; - } - params = new FileParams(); - if (this.transferable != null) { - params.size = this.transferable.getFileSize(); - } - if (body == null) { - return params; - } - String parts[] = body.split("\\|"); - switch (parts.length) { - case 1: - try { - params.size = Long.parseLong(parts[0]); - } catch (NumberFormatException e) { + public synchronized FileParams getFileParams() { + if (fileParams == null) { + fileParams = new FileParams(); + if (this.transferable != null) { + fileParams.size = this.transferable.getFileSize(); + } + String parts[] = body == null ? new String[0] : body.split("\\|"); + switch (parts.length) { + case 1: try { - params.url = new URL(parts[0]); - } catch (MalformedURLException e1) { - params.url = null; + fileParams.size = Long.parseLong(parts[0]); + } catch (NumberFormatException e) { + fileParams.url = parseUrl(parts[0]); } - } - break; - case 2: - case 4: - try { - params.url = new URL(parts[0]); - } catch (MalformedURLException e1) { - params.url = null; - } - try { - params.size = Long.parseLong(parts[1]); - } catch (NumberFormatException e) { - params.size = 0; - } - try { - params.width = Integer.parseInt(parts[2]); - } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { - params.width = 0; - } - try { - params.height = Integer.parseInt(parts[3]); - } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { - params.height = 0; - } - break; - case 3: - try { - params.size = Long.parseLong(parts[0]); - } catch (NumberFormatException e) { - params.size = 0; - } - try { - params.width = Integer.parseInt(parts[1]); - } catch (NumberFormatException e) { - params.width = 0; - } - try { - params.height = Integer.parseInt(parts[2]); - } catch (NumberFormatException e) { - params.height = 0; - } - break; + case 4: + fileParams.width = parseInt(parts[2]); + fileParams.height = parseInt(parts[3]); + case 2: + fileParams.url = parseUrl(parts[0]); + fileParams.size = parseLong(parts[1]); + break; + case 3: + fileParams.size = parseLong(parts[0]); + fileParams.width = parseInt(parts[1]); + fileParams.height = parseInt(parts[2]); + break; + } } - return params; + return fileParams; } - public FileParams getLegacyFileParams() { - FileParams params = new FileParams(); - if (body == null) { - return params; + private static long parseLong(String value) { + try { + return Long.parseLong(value); + } catch (NumberFormatException e) { + return 0; } - String parts[] = body.split(","); - if (parts.length == 3) { - try { - params.size = Long.parseLong(parts[0]); - } catch (NumberFormatException e) { - return null; - } - try { - params.width = Integer.parseInt(parts[1]); - } catch (NumberFormatException e) { - return null; - } - try { - params.height = Integer.parseInt(parts[2]); - } catch (NumberFormatException e) { - return null; - } - return params; - } else { + } + + private static int parseInt(String value) { + try { + return Integer.parseInt(value); + } catch (NumberFormatException e) { + return 0; + } + } + + private static URL parseUrl(String value) { + try { + return new URL(value); + } catch (MalformedURLException e) { return null; } } |