Commit 2ed64d4b authored by Yusuke Iwaki's avatar Yusuke Iwaki

show attachments in message.

parent fef9ccd9
......@@ -57,7 +57,7 @@ public class MessageViewHolder extends RealmModelViewHolder<PairedMessage> {
.timestampInto(timestamp)
.bodyInto(body)
.urlsInto(urls)
.attachmentsInto(attachments);
.attachmentsInto(attachments, hostname);
if (pairedMessage.target != null) {
int syncstate = pairedMessage.target.getSyncstate();
......
......@@ -104,7 +104,8 @@ public class MessageRenderer extends AbstractRenderer<Message> {
/**
* show urls in RocketChatMessageUrlsLayout.
*/
public MessageRenderer attachmentsInto(RocketChatMessageAttachmentsLayout attachmentsLayout) {
public MessageRenderer attachmentsInto(RocketChatMessageAttachmentsLayout attachmentsLayout,
String hostname) {
if (!shouldHandle(attachmentsLayout)) {
return this;
}
......@@ -114,6 +115,7 @@ public class MessageRenderer extends AbstractRenderer<Message> {
attachmentsLayout.setVisibility(View.GONE);
} else {
attachmentsLayout.setVisibility(View.VISIBLE);
attachmentsLayout.setHostname(hostname);
attachmentsLayout.setAttachments(attachments);
}
......
package chat.rocket.android.widget.helper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*/
public interface ImageFormat {
String PNG = "image/png";
String JPG = "image/jpg";
String JPEG = "image/jpeg";
String WEBP = "image/webp";
List<String> SUPPORTED_LIST = Collections.unmodifiableList(new ArrayList<String>() {
{
add(PNG);
add(JPG);
add(JPEG);
add(WEBP);
}
});
}
......@@ -2,10 +2,19 @@ package chat.rocket.android.widget.message;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import chat.rocket.android.widget.R;
import chat.rocket.android.widget.helper.ImageFormat;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -14,6 +23,7 @@ import org.json.JSONObject;
*/
public class RocketChatMessageAttachmentsLayout extends LinearLayout {
private LayoutInflater inflater;
private String hostname;
public RocketChatMessageAttachmentsLayout(Context context) {
super(context);
......@@ -42,6 +52,10 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
setOrientation(VERTICAL);
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public void setAttachments(String attachmentsString) {
removeAllViews();
......@@ -57,6 +71,55 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
}
private void appendAttachmentView(JSONObject attachmentObj) throws JSONException {
if (attachmentObj.isNull("image_url")) {
return;
}
String imageURL = attachmentObj.getString("image_url");
String imageType = attachmentObj.getString("image_type");
if (TextUtils.isEmpty(imageURL)
|| !imageType.startsWith("image/")
|| !ImageFormat.SUPPORTED_LIST.contains(imageType)) {
return;
}
View attachmentView = inflater.inflate(R.layout.message_inline_attachment, this, false);
imageURL = absolutize(imageURL);
Picasso.with(getContext())
.load(imageURL)
.placeholder(R.drawable.image_dummy)
.error(R.drawable.image_error)
.into((ImageView) attachmentView.findViewById(R.id.image));
TextView titleView = (TextView) attachmentView.findViewById(R.id.title);
if (attachmentObj.isNull("title")) {
titleView.setVisibility(View.GONE);
} else {
titleView.setVisibility(View.VISIBLE);
titleView.setText(attachmentObj.getString("title"));
if (attachmentObj.isNull("title_link")) {
titleView.setOnClickListener(null);
titleView.setClickable(false);
} else {
final String link = absolutize(attachmentObj.getString("title_link"));
titleView.setOnClickListener(new OnClickListener() {
@Override public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(intent);
}
});
}
}
addView(attachmentView);
}
private String absolutize(String url) {
return url.startsWith("/") ? "https://" + hostname + url : url;
}
}
......@@ -13,8 +13,8 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import chat.rocket.android.widget.R;
import chat.rocket.android.widget.helper.ImageFormat;
import com.squareup.picasso.Picasso;
import java.util.HashSet;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -22,15 +22,6 @@ import org.json.JSONObject;
/**
*/
public class RocketChatMessageUrlsLayout extends LinearLayout {
private static final HashSet<String> SUPPORTED_IMAGE_FORMATS = new HashSet<String>() {
{
add("image/png");
add("image/jpg");
add("image/jpeg");
add("image/webp");
}
};
private LayoutInflater inflater;
public RocketChatMessageUrlsLayout(Context context) {
......@@ -78,7 +69,7 @@ public class RocketChatMessageUrlsLayout extends LinearLayout {
final String url = urlObj.getString("url");
String contentType = urlObj.getJSONObject("headers").getString("contentType");
if (contentType.startsWith("image/") && SUPPORTED_IMAGE_FORMATS.contains(contentType)) {
if (contentType.startsWith("image/") && ImageFormat.SUPPORTED_LIST.contains(contentType)) {
View inlineImage = inflater.inflate(R.layout.message_inline_image, this, false);
Picasso.with(getContext())
.load(url)
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="4dp"
android:padding="4dp">
<View
android:layout_width="3dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:background="@color/inline_attachment_quote_line"/>
<LinearLayout
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:textAppearance="@style/TextAppearance.RocketChat.MessageAttachment.Title"
android:background="?attr/selectableItemBackground"/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="200dp"
android:layout_marginTop="4dp"
android:layout_marginRight="8dp"
android:adjustViewBounds="true"
android:scaleType="fitStart"/>
</LinearLayout>
</LinearLayout>
......@@ -10,7 +10,8 @@
android:layout_width="3dp"
android:layout_height="match_parent"
android:layout_marginEnd="5dp"
android:background="#FFCCCCCC"/>
android:background="@color/inline_attachment_quote_line"/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
......@@ -41,7 +42,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:textAppearance="@style/TextAppearance.RocketChat.MessageUrl.Title"/>
android:textAppearance="@style/TextAppearance.RocketChat.MessageAttachment.Title"/>
<TextView
android:id="@+id/description"
......@@ -49,7 +50,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:textAppearance="@style/TextAppearance.RocketChat.MessageUrl.Description"/>
android:textAppearance="@style/TextAppearance.RocketChat.MessageAttachment.Description"/>
</LinearLayout>
......
......@@ -10,15 +10,15 @@
<item name="android:textStyle">bold</item>
</style>
<style name="TextAppearance.RocketChat.MessageUrl" parent="TextAppearance.AppCompat.Body1"/>
<style name="TextAppearance.RocketChat.MessageUrl.Title" parent="TextAppearance.AppCompat.Title">
<style name="TextAppearance.RocketChat.MessageAttachment" parent="TextAppearance.AppCompat.Body1"/>
<style name="TextAppearance.RocketChat.MessageAttachment.Title" parent="TextAppearance.AppCompat.Title">
<item name="android:textSize">14sp</item>
<item name="android:textColor">?android:attr/textColorLink</item>
</style>
<style name="TextAppearance.RocketChat.MessageUrl.Description">
<style name="TextAppearance.RocketChat.MessageAttachment.Description">
<item name="android:textSize">12sp</item>
</style>
<style name="TextAppearance.RocketChat.MessageUrl.Hostname"
<style name="TextAppearance.RocketChat.MessageAttachment.Hostname"
parent="TextAppearance.AppCompat.Caption">
<item name="android:textSize">8sp</item>
</style>
......@@ -27,6 +27,7 @@
<color name="highlight_text_background_color">#f8f8f8</color>
<color name="highlight_text_border_color">#ccc</color>
<color name="inline_attachment_quote_line">#FFCCCCCC</color>
<color name="inline_attachment_box_outline">#FFF0F0F0</color>
<color name="inline_attachment_box_background">@android:color/white</color>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment