Improve preview

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2018-12-29 01:42:24 +01:00
parent 5cbb8c2e31
commit 1b20b63d8c
6 changed files with 94 additions and 36 deletions

View File

@ -199,6 +199,7 @@ dependencies {
implementation 'uk.co.chrisjenx:calligraphy:2.3.0' implementation 'uk.co.chrisjenx:calligraphy:2.3.0'
implementation group: 'eu.medsea.mimeutil', name: 'mime-util', version: '2.1.3'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation ('androidx.test.espresso:espresso-core:3.1.0-alpha4', { androidTestImplementation ('androidx.test.espresso:espresso-core:3.1.0-alpha4', {
exclude group: 'com.android.support', module: 'support-annotations' exclude group: 'com.android.support', module: 'support-annotations'

View File

@ -40,6 +40,7 @@ import com.bumptech.glide.request.RequestOptions;
import com.nextcloud.talk.R; import com.nextcloud.talk.R;
import com.nextcloud.talk.application.NextcloudTalkApplication; import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.models.database.UserEntity; import com.nextcloud.talk.models.database.UserEntity;
import com.nextcloud.talk.models.json.chat.ChatMessage;
import com.nextcloud.talk.models.json.rooms.Conversation; import com.nextcloud.talk.models.json.rooms.Conversation;
import com.nextcloud.talk.utils.ApiUtils; import com.nextcloud.talk.utils.ApiUtils;
import com.nextcloud.talk.utils.TextMatchers; import com.nextcloud.talk.utils.TextMatchers;
@ -126,7 +127,7 @@ public class ConversationItem extends AbstractFlexibleItem<ConversationItem.Conv
holder.dialogUnreadBubble.setVisibility(View.GONE); holder.dialogUnreadBubble.setVisibility(View.GONE);
} }
String authorDisplayName = ""; String authorDisplayName;
if (conversation.isHasPassword()) { if (conversation.isHasPassword()) {
holder.passwordProtectedRoomImageView.setVisibility(View.VISIBLE); holder.passwordProtectedRoomImageView.setVisibility(View.VISIBLE);
@ -159,7 +160,7 @@ public class ConversationItem extends AbstractFlexibleItem<ConversationItem.Conv
} }
} }
if (conversation.getLastMessage().getSpecialURLType().equals(TextMatchers.SpecialURLType.NONE)) { if (conversation.getLastMessage().getMessageType().equals(ChatMessage.MessageType.REGULAR_TEXT_MESSAGE)) {
authorDisplayName += ": "; authorDisplayName += ": ";
} else { } else {
authorDisplayName += " "; authorDisplayName += " ";

View File

@ -57,7 +57,7 @@ public class MagicPreviewMessageViewHolder extends MessageHolders.IncomingImageM
} }
} }
if (message.getSpecialURLType() == TextMatchers.SpecialURLType.NONE) { if (message.getMessageType() == ChatMessage.MessageType.SINGLE_NC_ATTACHMENT_MESSAGE) {
// it's a preview for a Nextcloud share // it's a preview for a Nextcloud share
messageText.setText(message.getSelectedIndividualHashMap().get("name")); messageText.setText(message.getSelectedIndividualHashMap().get("name"));
DisplayUtils.setClickableString(message.getSelectedIndividualHashMap().get("name"), message.getSelectedIndividualHashMap().get("link"), messageText); DisplayUtils.setClickableString(message.getSelectedIndividualHashMap().get("name"), message.getSelectedIndividualHashMap().get("link"), messageText);
@ -66,12 +66,14 @@ public class MagicPreviewMessageViewHolder extends MessageHolders.IncomingImageM
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
NextcloudTalkApplication.getSharedApplication().getApplicationContext().startActivity(browserIntent); NextcloudTalkApplication.getSharedApplication().getApplicationContext().startActivity(browserIntent);
}); });
} else if (message.getSpecialURLType() == TextMatchers.SpecialURLType.GIPHY){ } else if (message.getMessageType() == ChatMessage.MessageType.SINGLE_LINK_GIPHY_MESSAGE){
messageText.setText("GIPHY"); messageText.setText("GIPHY");
DisplayUtils.setClickableString("GIPHY", "https://giphy.com", messageText); DisplayUtils.setClickableString("GIPHY", "https://giphy.com", messageText);
} else if (message.getSpecialURLType() == TextMatchers.SpecialURLType.TENOR) { } else if (message.getMessageType() == ChatMessage.MessageType.SINGLE_LINK_TENOR_MESSAGE) {
messageText.setText("Tenor"); messageText.setText("Tenor");
DisplayUtils.setClickableString("Tenor", "https://tenor.com", messageText); DisplayUtils.setClickableString("Tenor", "https://tenor.com", messageText);
} else {
messageText.setText("");
} }
} }
} }

View File

@ -19,6 +19,8 @@
*/ */
package com.nextcloud.talk.models.json.chat; package com.nextcloud.talk.models.json.chat;
import android.text.TextUtils;
import com.bluelinelabs.logansquare.annotation.JsonField; import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonIgnore; import com.bluelinelabs.logansquare.annotation.JsonIgnore;
import com.bluelinelabs.logansquare.annotation.JsonObject; import com.bluelinelabs.logansquare.annotation.JsonObject;
@ -33,8 +35,10 @@ import com.stfalcon.chatkit.commons.models.MessageContentType;
import org.parceler.Parcel; import org.parceler.Parcel;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
@ -45,10 +49,9 @@ import lombok.Data;
@Data @Data
@JsonObject @JsonObject
public class ChatMessage implements IMessage, MessageContentType, MessageContentType.Image { public class ChatMessage implements IMessage, MessageContentType, MessageContentType.Image {
List<MessageType> messageTypesToIgnore = Arrays.asList(MessageType.REGULAR_TEXT_MESSAGE,
public TextMatchers.SpecialURLType getSpecialURLType() { MessageType.SYSTEM_MESSAGE, MessageType.SINGLE_LINK_VIDEO_MESSAGE,
return TextMatchers.getSpecialUrlTypeMessage(getMessage()); MessageType.SINGLE_LINK_AUDIO_MESSAGE, MessageType.SINGLE_LINK_MESSAGE);
}
private boolean hasFileAttachment() { private boolean hasFileAttachment() {
if (messageParameters != null && messageParameters.size() > 0) { if (messageParameters != null && messageParameters.size() > 0) {
@ -78,14 +81,38 @@ public class ChatMessage implements IMessage, MessageContentType, MessageContent
} }
} }
if (!getSpecialURLType().equals(TextMatchers.SpecialURLType.NONE) && if (!messageTypesToIgnore.contains(getMessageType())) {
!getSpecialURLType().equals(TextMatchers.SpecialURLType.REGULAR)) {
return getMessage().trim(); return getMessage().trim();
} }
return null; return null;
} }
public MessageType getMessageType() {
if (!TextUtils.isEmpty(getSystemMessage())) {
return MessageType.SYSTEM_MESSAGE;
}
if (hasFileAttachment()) {
return MessageType.SINGLE_NC_ATTACHMENT_MESSAGE;
}
return TextMatchers.getMessageTypeFromString(getText());
}
public enum MessageType {
REGULAR_TEXT_MESSAGE,
SYSTEM_MESSAGE,
SINGLE_LINK_GIPHY_MESSAGE,
SINGLE_LINK_TENOR_MESSAGE,
SINGLE_LINK_GIF_MESSAGE,
SINGLE_LINK_MESSAGE,
SINGLE_LINK_VIDEO_MESSAGE,
SINGLE_LINK_IMAGE_MESSAGE,
SINGLE_LINK_AUDIO_MESSAGE,
SINGLE_NC_ATTACHMENT_MESSAGE,
}
public enum SystemMessageType { public enum SystemMessageType {
DUMMY, DUMMY,
CONVERSATION_CREATED, CONVERSATION_CREATED,
@ -164,18 +191,23 @@ public class ChatMessage implements IMessage, MessageContentType, MessageContent
} }
public String getLastMessageDisplayText() { public String getLastMessageDisplayText() {
if (getSpecialURLType().equals(TextMatchers.SpecialURLType.NONE)) { if (getMessageType().equals(MessageType.REGULAR_TEXT_MESSAGE) || getMessageType().equals(MessageType.SYSTEM_MESSAGE)) {
return getText(); return getText();
} else { } else {
if (getSpecialURLType().equals(TextMatchers.SpecialURLType.GIPHY) if (getMessageType().equals(MessageType.SINGLE_LINK_GIPHY_MESSAGE)
|| getSpecialURLType().equals(TextMatchers.SpecialURLType.TENOR)) { || getMessageType().equals(MessageType.SINGLE_LINK_TENOR_MESSAGE)
|| getMessageType().equals(MessageType.SINGLE_LINK_GIF_MESSAGE)) {
return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_a_gif)); return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_a_gif));
} else if (getSpecialURLType().equals(TextMatchers.SpecialURLType.REGULAR)) { } else if (getMessageType().equals(MessageType.SINGLE_NC_ATTACHMENT_MESSAGE)) {
if (hasFileAttachment()) { return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_an_attachment));
return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_an_attachment)); } else if (getMessageType().equals(MessageType.SINGLE_LINK_MESSAGE)) {
} else { return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_a_link));
return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_a_link)); } else if (getMessageType().equals(MessageType.SINGLE_LINK_AUDIO_MESSAGE)) {
} return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_an_audio));
} else if (getMessageType().equals(MessageType.SINGLE_LINK_VIDEO_MESSAGE)) {
return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_a_video));
} else if (getMessageType().equals(MessageType.SINGLE_LINK_IMAGE_MESSAGE)) {
return (NextcloudTalkApplication.getSharedApplication().getString(R.string.nc_sent_an_image));
} }
} }

View File

@ -28,6 +28,7 @@ import android.util.Log;
import android.util.Patterns; import android.util.Patterns;
import com.nextcloud.talk.R; import com.nextcloud.talk.R;
import com.nextcloud.talk.models.json.chat.ChatMessage;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
@ -40,6 +41,10 @@ import java.util.regex.Pattern;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import eu.medsea.mimeutil.MimeUtil;
import eu.medsea.mimeutil.detector.ExtensionMimeDetector;
import eu.medsea.mimeutil.detector.MagicMimeMimeDetector;
import eu.medsea.mimeutil.detector.OpendesktopMimeDetector;
public final class TextMatchers { public final class TextMatchers {
@ -47,14 +52,7 @@ public final class TextMatchers {
private static Pattern regexPattern; private static Pattern regexPattern;
public enum SpecialURLType { public static ChatMessage.MessageType getMessageTypeFromString(@NonNull final String text) {
NONE,
REGULAR,
GIPHY,
TENOR,
}
public static SpecialURLType getSpecialUrlTypeMessage(@NonNull final String text) {
List<String> links = new ArrayList<>(); List<String> links = new ArrayList<>();
Matcher m = Patterns.WEB_URL.matcher(text); Matcher m = Patterns.WEB_URL.matcher(text);
while (m.find()) { while (m.find()) {
@ -65,18 +63,38 @@ public final class TextMatchers {
if (links.size() == 1 && text.trim().length() == links.get(0).length()) { if (links.size() == 1 && text.trim().length() == links.get(0).length()) {
String specialLink = links.get(0); String specialLink = links.get(0);
if (specialLink.startsWith("https://media.giphy.com/") && specialLink.endsWith(".gif")) { if (specialLink.startsWith("https://media.giphy.com/") && specialLink.endsWith(".gif")) {
return SpecialURLType.GIPHY; return ChatMessage.MessageType.SINGLE_LINK_GIPHY_MESSAGE;
} else if (specialLink.contains("tenor.com/")) { } else if (specialLink.contains("tenor.com/") &&
Pattern pattern = Pattern.compile("https://media.*\\.tenor\\.com.*\\.gif.*", Pattern.CASE_INSENSITIVE); Pattern.compile("https://media.*\\.tenor\\.com.*\\.gif.*",
if (pattern.matcher(specialLink).matches()) { Pattern.CASE_INSENSITIVE).matcher(specialLink).matches()) {
return SpecialURLType.TENOR; return ChatMessage.MessageType.SINGLE_LINK_TENOR_MESSAGE;
}
} else { } else {
return SpecialURLType.REGULAR; if (specialLink.contains("?")) {
specialLink = specialLink.substring(0, specialLink.indexOf("?"));
}
MimeUtil.registerMimeDetector(MagicMimeMimeDetector.class.getName());
MimeUtil.registerMimeDetector(ExtensionMimeDetector.class.getName());
MimeUtil.registerMimeDetector(OpendesktopMimeDetector.class.getName());
String mimeType = MimeUtil.getMostSpecificMimeType(MimeUtil.getMimeTypes(specialLink)).toString();
if (mimeType.startsWith("image/")) {
if (mimeType.equalsIgnoreCase("image/gif")) {
return ChatMessage.MessageType.SINGLE_LINK_GIF_MESSAGE;
} else {
return ChatMessage.MessageType.SINGLE_LINK_IMAGE_MESSAGE;
}
} else if (mimeType.startsWith("video/")) {
return ChatMessage.MessageType.SINGLE_LINK_VIDEO_MESSAGE;
} else if (mimeType.startsWith("audio/")) {
return ChatMessage.MessageType.SINGLE_LINK_AUDIO_MESSAGE;
}
return ChatMessage.MessageType.SINGLE_LINK_MESSAGE;
} }
} }
return SpecialURLType.NONE; // if we have 0 or more than 1 link, we're a regular message
return ChatMessage.MessageType.REGULAR_TEXT_MESSAGE;
} }
public static boolean isMessageWithSingleEmoticonOnly(@NonNull final Context context, public static boolean isMessageWithSingleEmoticonOnly(@NonNull final Context context,

View File

@ -183,6 +183,10 @@
<string name="nc_sent_a_link">sent a link.</string> <string name="nc_sent_a_link">sent a link.</string>
<string name="nc_sent_a_gif">sent a GIF.</string> <string name="nc_sent_a_gif">sent a GIF.</string>
<string name="nc_sent_an_attachment">sent an attachment.</string> <string name="nc_sent_an_attachment">sent an attachment.</string>
<string name="nc_sent_an_audio">sent an audio.</string>
<string name="nc_sent_a_video">sent a video.</string>
<string name="nc_sent_an_image">sent an image.</string>
<!-- Contacts endless loading --> <!-- Contacts endless loading -->
<string name="nc_no_more_load_retry">No more items to load. Refresh to retry.</string> <string name="nc_no_more_load_retry">No more items to load. Refresh to retry.</string>