cache some information generated from body like isEmojiOnly, fileParams, isGeoUri and isXmppUri

This commit is contained in:
Christian Schneppe 2017-08-13 22:40:00 +02:00
parent 1fd688b9d8
commit 04db5052cc
6 changed files with 111 additions and 137 deletions

View file

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

View file

@ -541,7 +541,7 @@ public class NotificationService {
private Message getFirstLocationMessage(final Iterable<Message> messages) { private Message getFirstLocationMessage(final Iterable<Message> messages) {
for (final Message message : messages) { for (final Message message : messages) {
if (GeoHelper.isGeoUri(message.getBody())) { if (message.isGeoUri()) {
return message; return message;
} }
} }

View file

@ -73,10 +73,8 @@ import de.pixart.messenger.ui.adapter.MessageAdapter;
import de.pixart.messenger.ui.adapter.MessageAdapter.OnContactPictureClicked; import de.pixart.messenger.ui.adapter.MessageAdapter.OnContactPictureClicked;
import de.pixart.messenger.ui.adapter.MessageAdapter.OnContactPictureLongClicked; import de.pixart.messenger.ui.adapter.MessageAdapter.OnContactPictureLongClicked;
import de.pixart.messenger.ui.widget.ListSelectionManager; import de.pixart.messenger.ui.widget.ListSelectionManager;
import de.pixart.messenger.utils.GeoHelper;
import de.pixart.messenger.utils.NickValidityChecker; import de.pixart.messenger.utils.NickValidityChecker;
import de.pixart.messenger.utils.UIHelper; import de.pixart.messenger.utils.UIHelper;
import de.pixart.messenger.utils.XmppUri;
import de.pixart.messenger.xmpp.XmppConnection; import de.pixart.messenger.xmpp.XmppConnection;
import de.pixart.messenger.xmpp.chatstate.ChatState; import de.pixart.messenger.xmpp.chatstate.ChatState;
import de.pixart.messenger.xmpp.jid.Jid; import de.pixart.messenger.xmpp.jid.Jid;
@ -636,20 +634,15 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
MenuItem downloadFile = menu.findItem(R.id.download_file); MenuItem downloadFile = menu.findItem(R.id.download_file);
MenuItem deleteFile = menu.findItem(R.id.delete_file); MenuItem deleteFile = menu.findItem(R.id.delete_file);
MenuItem showErrorMessage = menu.findItem(R.id.show_error_message); MenuItem showErrorMessage = menu.findItem(R.id.show_error_message);
if (!treatAsFile && !GeoHelper.isGeoUri(m.getBody()) && !m.treatAsDownloadable()) { if (!treatAsFile && !m.isGeoUri() && !m.isXmppUri() && !m.treatAsDownloadable()) {
selectText.setVisible(ListSelectionManager.isSupported()); selectText.setVisible(ListSelectionManager.isSupported());
} }
if (m.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) { if (m.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
retryDecryption.setVisible(true); retryDecryption.setVisible(true);
} }
if (((relevantForCorrection.getType() == Message.TYPE_TEXT if (relevantForCorrection.getType() == Message.TYPE_TEXT
&& relevantForCorrection.isLastCorrectableMessage() && relevantForCorrection.isLastCorrectableMessage()
&& conversation.getMode() == Conversation.MODE_SINGLE) && (m.getConversation().getMucOptions().nonanonymous() || m.getConversation().getMode() == Conversation.MODE_SINGLE)) {
|| (relevantForCorrection.getType() == Message.TYPE_TEXT
&& relevantForCorrection.isLastCorrectableMessage()
&& conversation.getMode() == Conversation.MODE_MULTI
&& m.getConversation().getMucOptions().nonanonymous()))
&& !(GeoHelper.isGeoUri(m.getBody()) || XmppUri.isXmppUri(m.getBody()))) {
correctMessage.setVisible(true); correctMessage.setVisible(true);
} }
if (treatAsFile || (m.getType() == Message.TYPE_TEXT && !m.treatAsDownloadable())) { if (treatAsFile || (m.getType() == Message.TYPE_TEXT && !m.treatAsDownloadable())) {
@ -660,8 +653,8 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
sendAgain.setVisible(true); sendAgain.setVisible(true);
} }
if (m.hasFileOnRemoteHost() if (m.hasFileOnRemoteHost()
|| GeoHelper.isGeoUri(m.getBody()) || m.isGeoUri()
|| XmppUri.isXmppUri(m.getBody()) || m.isXmppUri()
|| m.treatAsDownloadable() || m.treatAsDownloadable()
|| (t != null && t instanceof HttpDownloadConnection)) { || (t != null && t instanceof HttpDownloadConnection)) {
copyUrl.setVisible(true); copyUrl.setVisible(true);
@ -739,7 +732,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
private void shareWith(Message message) { private void shareWith(Message message) {
Intent shareIntent = new Intent(); Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setAction(Intent.ACTION_SEND);
if (GeoHelper.isGeoUri(message.getBody())) { if (message.isGeoUri() || message.isXmppUri()) {
shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody()); shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody());
shareIntent.setType("text/plain"); shareIntent.setType("text/plain");
} else if (!message.isFileOrImage()) { } else if (!message.isFileOrImage()) {
@ -811,10 +804,10 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
private void copyUrl(Message message) { private void copyUrl(Message message) {
final String url; final String url;
final int resId; final int resId;
if (GeoHelper.isGeoUri(message.getBody())) { if (message.isGeoUri()) {
resId = R.string.location; resId = R.string.location;
url = message.getBody(); url = message.getBody();
} else if (XmppUri.isXmppUri(message.getBody())) { } else if (message.isXmppUri()) {
resId = R.string.contact; resId = R.string.contact;
url = message.getBody(); url = message.getBody();
} else if (message.hasFileOnRemoteHost()) { } else if (message.hasFileOnRemoteHost()) {

View file

@ -961,11 +961,11 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) { } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
displayDecryptionFailed(viewHolder, darkBackground); displayDecryptionFailed(viewHolder, darkBackground);
} else { } else {
if (GeoHelper.isGeoUri(message.getBody())) { if (message.isGeoUri()) {
displayLocationMessage(viewHolder, message); displayLocationMessage(viewHolder, message);
} else if (message.bodyIsOnlyEmojis()) { } else if (message.bodyIsOnlyEmojis()) {
displayEmojiMessage(viewHolder, message.getBody().replaceAll("\\s", "")); displayEmojiMessage(viewHolder, message.getBody().replaceAll("\\s", ""));
} else if (message.bodyIsXmpp()) { } else if (message.isXmppUri()) {
displayXmppMessage(viewHolder, message.getBody().trim()); displayXmppMessage(viewHolder, message.getBody().trim());
} else if (message.treatAsDownloadable()) { } else if (message.treatAsDownloadable()) {
try { try {

View file

@ -17,10 +17,6 @@ import de.pixart.messenger.entities.Message;
public class GeoHelper { public class GeoHelper {
public static Pattern GEO_URI = Pattern.compile("geo:([\\-0-9.]+),([\\-0-9.]+)(?:,([\\-0-9.]+))?(?:\\?(.*))?", Pattern.CASE_INSENSITIVE); public static Pattern GEO_URI = Pattern.compile("geo:([\\-0-9.]+),([\\-0-9.]+)(?:,([\\-0-9.]+))?(?:\\?(.*))?", Pattern.CASE_INSENSITIVE);
public static boolean isGeoUri(String body) {
return body != null && GEO_URI.matcher(body).matches();
}
public static String MapPreviewUri(Message message) { public static String MapPreviewUri(Message message) {
Matcher matcher = GEO_URI.matcher(message.getBody()); Matcher matcher = GEO_URI.matcher(message.getBody());
if (!matcher.matches()) { if (!matcher.matches()) {

View file

@ -205,13 +205,13 @@ public class UIHelper {
if (body.startsWith(Message.ME_COMMAND)) { if (body.startsWith(Message.ME_COMMAND)) {
return new Pair<>(body.replaceAll("^" + Message.ME_COMMAND, return new Pair<>(body.replaceAll("^" + Message.ME_COMMAND,
UIHelper.getMessageDisplayName(message) + " "), false); UIHelper.getMessageDisplayName(message) + " "), false);
} else if (GeoHelper.isGeoUri(message.getBody())) { } else if (message.isGeoUri()) {
if (message.getStatus() == Message.STATUS_RECEIVED) { if (message.getStatus() == Message.STATUS_RECEIVED) {
return new Pair<>(context.getString(R.string.received_location), true); return new Pair<>(context.getString(R.string.received_location), true);
} else { } else {
return new Pair<>(context.getString(R.string.location), true); return new Pair<>(context.getString(R.string.location), true);
} }
} else if (message.bodyIsXmpp()) { } else if (message.isXmppUri()) {
if (message.getStatus() == Message.STATUS_RECEIVED) { if (message.getStatus() == Message.STATUS_RECEIVED) {
return new Pair<>(context.getString(R.string.received_contact), true); return new Pair<>(context.getString(R.string.received_contact), true);
} else { } else {