Browse Source

Improve preview

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 6 năm trước cách đây
mục cha
commit
1b20b63d8c

+ 1 - 0
app/build.gradle

@@ -199,6 +199,7 @@ dependencies {
 
     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'
     androidTestImplementation ('androidx.test.espresso:espresso-core:3.1.0-alpha4', {
         exclude group: 'com.android.support', module: 'support-annotations'

+ 3 - 2
app/src/main/java/com/nextcloud/talk/adapters/items/ConversationItem.java

@@ -40,6 +40,7 @@ import com.bumptech.glide.request.RequestOptions;
 import com.nextcloud.talk.R;
 import com.nextcloud.talk.application.NextcloudTalkApplication;
 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.utils.ApiUtils;
 import com.nextcloud.talk.utils.TextMatchers;
@@ -126,7 +127,7 @@ public class ConversationItem extends AbstractFlexibleItem<ConversationItem.Conv
             holder.dialogUnreadBubble.setVisibility(View.GONE);
         }
 
-        String authorDisplayName = "";
+        String authorDisplayName;
 
         if (conversation.isHasPassword()) {
             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 += ": ";
                 } else {
                     authorDisplayName += " ";

+ 5 - 3
app/src/main/java/com/nextcloud/talk/adapters/messages/MagicPreviewMessageViewHolder.java

@@ -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
             messageText.setText(message.getSelectedIndividualHashMap().get("name"));
             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);
                 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");
             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");
             DisplayUtils.setClickableString("Tenor", "https://tenor.com", messageText);
+        } else {
+            messageText.setText("");
         }
     }
 }

+ 47 - 15
app/src/main/java/com/nextcloud/talk/models/json/chat/ChatMessage.java

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

+ 34 - 16
app/src/main/java/com/nextcloud/talk/utils/TextMatchers.java

@@ -28,6 +28,7 @@ import android.util.Log;
 import android.util.Patterns;
 
 import com.nextcloud.talk.R;
+import com.nextcloud.talk.models.json.chat.ChatMessage;
 
 import java.io.BufferedReader;
 import java.io.IOException;
@@ -40,6 +41,10 @@ import java.util.regex.Pattern;
 
 import androidx.annotation.NonNull;
 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 {
 
@@ -47,14 +52,7 @@ public final class TextMatchers {
 
     private static Pattern regexPattern;
 
-    public enum SpecialURLType {
-        NONE,
-        REGULAR,
-        GIPHY,
-        TENOR,
-    }
-
-    public static SpecialURLType getSpecialUrlTypeMessage(@NonNull final String text) {
+    public static ChatMessage.MessageType getMessageTypeFromString(@NonNull final String text) {
         List<String> links = new ArrayList<>();
         Matcher m = Patterns.WEB_URL.matcher(text);
         while (m.find()) {
@@ -65,18 +63,38 @@ public final class TextMatchers {
         if (links.size() == 1 && text.trim().length() == links.get(0).length()) {
             String specialLink = links.get(0);
             if (specialLink.startsWith("https://media.giphy.com/") && specialLink.endsWith(".gif")) {
-                return SpecialURLType.GIPHY;
-            } else if (specialLink.contains("tenor.com/")) {
-                Pattern pattern = Pattern.compile("https://media.*\\.tenor\\.com.*\\.gif.*", Pattern.CASE_INSENSITIVE);
-                if (pattern.matcher(specialLink).matches()) {
-                    return SpecialURLType.TENOR;
-                }
+                return ChatMessage.MessageType.SINGLE_LINK_GIPHY_MESSAGE;
+            } else if (specialLink.contains("tenor.com/") &&
+                    Pattern.compile("https://media.*\\.tenor\\.com.*\\.gif.*",
+                            Pattern.CASE_INSENSITIVE).matcher(specialLink).matches()) {
+                return ChatMessage.MessageType.SINGLE_LINK_TENOR_MESSAGE;
             } 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,

+ 4 - 0
app/src/main/res/values/strings.xml

@@ -183,6 +183,10 @@
     <string name="nc_sent_a_link">sent a link.</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_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 -->
     <string name="nc_no_more_load_retry">No more items to load. Refresh to retry.</string>