Commit de3b81ed authored by Leonardo Aramaki's avatar Leonardo Aramaki

Add chrome custom tabs dependency

parent 9959d848
...@@ -12,7 +12,8 @@ ext { ...@@ -12,7 +12,8 @@ ext {
constraintLayout : "com.android.support.constraint:constraint-layout:${constraintLayoutVersion}", constraintLayout : "com.android.support.constraint:constraint-layout:${constraintLayoutVersion}",
cardView : "com.android.support:cardview-v7:${supportLibraryVersion}", cardView : "com.android.support:cardview-v7:${supportLibraryVersion}",
supportV13 : "com.android.support:support-v13:${supportLibraryVersion}", supportV13 : "com.android.support:support-v13:${supportLibraryVersion}",
multidex : "com.android.support:multidex:1.0.2" multidex : "com.android.support:multidex:1.0.2",
customTabs : "com.android.support:customtabs:${supportLibraryVersion}"
] ]
extraDependencies = [ extraDependencies = [
okHTTP : "com.squareup.okhttp3:okhttp:${okHttpVersion}", okHTTP : "com.squareup.okhttp3:okhttp:${okHttpVersion}",
......
...@@ -39,6 +39,7 @@ dependencies { ...@@ -39,6 +39,7 @@ dependencies {
compile supportDependencies.designSupportLibrary compile supportDependencies.designSupportLibrary
compile supportDependencies.constraintLayout compile supportDependencies.constraintLayout
compile supportDependencies.supportV13 compile supportDependencies.supportV13
compile supportDependencies.customTabs
compile rxbindingDependencies.rxBinding compile rxbindingDependencies.rxBinding
compile rxbindingDependencies.rxBindingSupport compile rxbindingDependencies.rxBindingSupport
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$rootProject.ext.kotlinVersion" compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$rootProject.ext.kotlinVersion"
......
...@@ -7,6 +7,7 @@ import android.graphics.Color; ...@@ -7,6 +7,7 @@ import android.graphics.Color;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.support.customtabs.CustomTabsIntent;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v4.widget.TextViewCompat; import android.support.v4.widget.TextViewCompat;
import android.text.TextUtils; import android.text.TextUtils;
...@@ -37,265 +38,262 @@ import chat.rocket.core.models.AttachmentTitle; ...@@ -37,265 +38,262 @@ import chat.rocket.core.models.AttachmentTitle;
/** /**
*/ */
public class RocketChatMessageAttachmentsLayout extends LinearLayout { public class RocketChatMessageAttachmentsLayout extends LinearLayout {
private LayoutInflater inflater; private LayoutInflater inflater;
private AbsoluteUrl absoluteUrl; private AbsoluteUrl absoluteUrl;
private List<Attachment> attachments; private List<Attachment> attachments;
public RocketChatMessageAttachmentsLayout(Context context) { public RocketChatMessageAttachmentsLayout(Context context) {
super(context); super(context);
initialize(context, null); initialize(context, null);
} }
public RocketChatMessageAttachmentsLayout(Context context, AttributeSet attrs) { public RocketChatMessageAttachmentsLayout(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
initialize(context, attrs); initialize(context, attrs);
}
public RocketChatMessageAttachmentsLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RocketChatMessageAttachmentsLayout(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize(context, attrs);
}
private void initialize(Context context, AttributeSet attrs) {
inflater = LayoutInflater.from(context);
setOrientation(VERTICAL);
}
public void setAbsoluteUrl(AbsoluteUrl absoluteUrl) {
this.absoluteUrl = absoluteUrl;
}
public void setAttachments(List<Attachment> attachments, boolean autoloadImages) {
if (this.attachments != null && this.attachments.equals(attachments)) {
return;
} }
this.attachments = attachments;
for (int i = 0, size = attachments.size(); i < size; i++) { public RocketChatMessageAttachmentsLayout(Context context, AttributeSet attrs, int defStyleAttr) {
appendAttachmentView(attachments.get(i), autoloadImages, true); super(context, attrs, defStyleAttr);
initialize(context, attrs);
} }
}
public void appendAttachmentView(Attachment attachment, boolean autoloadImages, boolean showAttachmentStrip) { @TargetApi(Build.VERSION_CODES.LOLLIPOP)
if (attachment == null) { public RocketChatMessageAttachmentsLayout(Context context, AttributeSet attrs, int defStyleAttr,
return; int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize(context, attrs);
} }
removeAllViews(); private void initialize(Context context, AttributeSet attrs) {
View attachmentView = inflater.inflate(R.layout.message_inline_attachment, this, false); inflater = LayoutInflater.from(context);
setOrientation(VERTICAL);
colorizeAttachmentBar(attachment, attachmentView, showAttachmentStrip);
showAuthorAttachment(attachment, attachmentView);
showTitleAttachment(attachment, attachmentView);
showReferenceAttachment(attachment, attachmentView);
showImageAttachment(attachment, attachmentView, autoloadImages);
// audio
// video
showFieldsAttachment(attachment, attachmentView);
addView(attachmentView);
}
private void colorizeAttachmentBar(Attachment attachment, View attachmentView, boolean showAttachmentStrip) {
final View attachmentStrip = attachmentView.findViewById(R.id.attachment_strip);
if (showAttachmentStrip) {
final String colorString = attachment.getColor();
if (TextUtils.isEmpty(colorString)) {
attachmentStrip.setBackgroundResource(R.color.inline_attachment_quote_line);
return;
}
try {
attachmentStrip.setBackgroundColor(Color.parseColor(colorString));
} catch (Exception e) {
attachmentStrip.setBackgroundResource(R.color.inline_attachment_quote_line);
}
} else {
attachmentStrip.setVisibility(GONE);
} }
}
public void setAbsoluteUrl(AbsoluteUrl absoluteUrl) {
private void showAuthorAttachment(Attachment attachment, View attachmentView) { this.absoluteUrl = absoluteUrl;
final View authorBox = attachmentView.findViewById(R.id.author_box);
AttachmentAuthor author = attachment.getAttachmentAuthor();
if (author == null) {
authorBox.setVisibility(GONE);
return;
} }
authorBox.setVisibility(VISIBLE); public void setAttachments(List<Attachment> attachments, boolean autoloadImages) {
if (this.attachments != null && this.attachments.equals(attachments)) {
return;
}
this.attachments = attachments;
FrescoHelper.INSTANCE.loadImageWithCustomization((SimpleDraweeView) attachmentView.findViewById(R.id.author_icon), absolutize(author.getIconUrl())); for (int i = 0, size = attachments.size(); i < size; i++) {
appendAttachmentView(attachments.get(i), autoloadImages, true);
}
}
final TextView authorName = attachmentView.findViewById(R.id.author_name); public void appendAttachmentView(Attachment attachment, boolean autoloadImages, boolean showAttachmentStrip) {
authorName.setText(author.getName()); if (attachment == null) {
return;
}
final String link = absolutize(author.getLink()); removeAllViews();
authorName.setOnClickListener(new OnClickListener() { View attachmentView = inflater.inflate(R.layout.message_inline_attachment, this, false);
@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);
}
});
// timestamp and link - need to format time colorizeAttachmentBar(attachment, attachmentView, showAttachmentStrip);
} showAuthorAttachment(attachment, attachmentView);
showTitleAttachment(attachment, attachmentView);
showReferenceAttachment(attachment, attachmentView);
showImageAttachment(attachment, attachmentView, autoloadImages);
// audio
// video
showFieldsAttachment(attachment, attachmentView);
private void showTitleAttachment(Attachment attachment, View attachmentView) { addView(attachmentView);
TextView titleView = attachmentView.findViewById(R.id.title);
AttachmentTitle title = attachment.getAttachmentTitle();
if (title == null || title.getTitle() == null) {
titleView.setVisibility(View.GONE);
return;
} }
titleView.setVisibility(View.VISIBLE); private void colorizeAttachmentBar(Attachment attachment, View attachmentView, boolean showAttachmentStrip) {
titleView.setText(title.getTitle()); final View attachmentStrip = attachmentView.findViewById(R.id.attachment_strip);
if (title.getLink() == null) { if (showAttachmentStrip) {
titleView.setOnClickListener(null); final String colorString = attachment.getColor();
titleView.setClickable(false); if (TextUtils.isEmpty(colorString)) {
} else { attachmentStrip.setBackgroundResource(R.color.inline_attachment_quote_line);
final String link = absolutize(title.getLink()); return;
titleView.setOnClickListener(new OnClickListener() { }
@Override
public void onClick(View view) { try {
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link)); attachmentStrip.setBackgroundColor(Color.parseColor(colorString));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } catch (Exception e) {
attachmentStrip.setBackgroundResource(R.color.inline_attachment_quote_line);
final Context context = view.getContext(); }
if (intent.resolveActivity(context.getPackageManager()) != null) { } else {
context.startActivity(intent); attachmentStrip.setVisibility(GONE);
}
} }
});
TextViewCompat.setTextAppearance(titleView,
R.style.TextAppearance_RocketChat_MessageAttachment_Title_Link);
} }
}
private void showReferenceAttachment(Attachment attachment, View attachmentView) { private void showAuthorAttachment(Attachment attachment, View attachmentView) {
final View refBox = attachmentView.findViewById(R.id.ref_box); final View authorBox = attachmentView.findViewById(R.id.author_box);
if (attachment.getThumbUrl() == null && attachment.getText() == null) { AttachmentAuthor author = attachment.getAttachmentAuthor();
refBox.setVisibility(GONE); if (author == null) {
return; authorBox.setVisibility(GONE);
} return;
}
authorBox.setVisibility(VISIBLE);
FrescoHelper.INSTANCE.loadImageWithCustomization((SimpleDraweeView) attachmentView.findViewById(R.id.author_icon), absolutize(author.getIconUrl()));
refBox.setVisibility(VISIBLE); final TextView authorName = attachmentView.findViewById(R.id.author_name);
authorName.setText(author.getName());
final SimpleDraweeView thumbImage = refBox.findViewById(R.id.thumb); final String link = absolutize(author.getLink());
authorName.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);
}
});
final String thumbUrl = attachment.getThumbUrl(); // timestamp and link - need to format time
if (TextUtils.isEmpty(thumbUrl)) {
thumbImage.setVisibility(GONE);
} else {
thumbImage.setVisibility(VISIBLE);
FrescoHelper.INSTANCE.loadImageWithCustomization(thumbImage, absolutize(thumbUrl));
} }
final TextView refText = refBox.findViewById(R.id.text); private void showTitleAttachment(Attachment attachment, View attachmentView) {
TextView titleView = attachmentView.findViewById(R.id.title);
AttachmentTitle title = attachment.getAttachmentTitle();
if (title == null || title.getTitle() == null) {
titleView.setVisibility(View.GONE);
return;
}
final String refString = attachment.getText(); titleView.setVisibility(View.VISIBLE);
if (TextUtils.isEmpty(refString)) { titleView.setText(title.getTitle());
refText.setVisibility(GONE);
} else { if (title.getLink() == null) {
refText.setVisibility(VISIBLE); titleView.setOnClickListener(null);
refText.setText(refString); titleView.setClickable(false);
} else {
final String link = absolutize(title.getLink());
titleView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
new CustomTabsIntent.Builder()
.setToolbarColor(ContextCompat.getColor(getContext(), R.color.colorPrimary))
.build()
.launchUrl(getContext(), Uri.parse(link));
}
});
TextViewCompat.setTextAppearance(titleView,
R.style.TextAppearance_RocketChat_MessageAttachment_Title_Link);
}
} }
}
private void showImageAttachment(Attachment attachment, View attachmentView, boolean autoloadImages) { private void showReferenceAttachment(Attachment attachment, View attachmentView) {
final View imageContainer = attachmentView.findViewById(R.id.image_container); final View refBox = attachmentView.findViewById(R.id.ref_box);
if (attachment.getImageUrl() == null) { if (attachment.getThumbUrl() == null && attachment.getText() == null) {
imageContainer.setVisibility(GONE); refBox.setVisibility(GONE);
return; return;
}
refBox.setVisibility(VISIBLE);
final SimpleDraweeView thumbImage = refBox.findViewById(R.id.thumb);
final String thumbUrl = attachment.getThumbUrl();
if (TextUtils.isEmpty(thumbUrl)) {
thumbImage.setVisibility(GONE);
} else {
thumbImage.setVisibility(VISIBLE);
FrescoHelper.INSTANCE.loadImageWithCustomization(thumbImage, absolutize(thumbUrl));
}
final TextView refText = refBox.findViewById(R.id.text);
final String refString = attachment.getText();
if (TextUtils.isEmpty(refString)) {
refText.setVisibility(GONE);
} else {
refText.setVisibility(VISIBLE);
refText.setText(refString);
}
} }
imageContainer.setVisibility(VISIBLE); private void showImageAttachment(Attachment attachment, View attachmentView, boolean autoloadImages) {
final View imageContainer = attachmentView.findViewById(R.id.image_container);
if (attachment.getImageUrl() == null) {
imageContainer.setVisibility(GONE);
return;
}
final SimpleDraweeView attachedImage = attachmentView.findViewById(R.id.image); imageContainer.setVisibility(VISIBLE);
final View load = attachmentView.findViewById(R.id.image_load);
// Fix for https://fabric.io/rocketchat3/android/apps/chat.rocket.android/issues/59982403be077a4dcc4d7dc3/sessions/599F217000CF00015C771EEF2021AA0F_f9320e3f88fd11e7935256847afe9799_0_v2? final SimpleDraweeView attachedImage = attachmentView.findViewById(R.id.image);
// From: https://github.com/facebook/fresco/issues/1176#issuecomment-216830098 final View load = attachmentView.findViewById(R.id.image_load);
// android.support.v4.content.ContextCompat creates your vector drawable
Drawable placeholderDrawable = ContextCompat.getDrawable(getContext(), R.drawable.image_dummy);
// Set the placeholder image to the placeholder vector drawable // Fix for https://fabric.io/rocketchat3/android/apps/chat.rocket.android/issues/59982403be077a4dcc4d7dc3/sessions/599F217000CF00015C771EEF2021AA0F_f9320e3f88fd11e7935256847afe9799_0_v2?
attachedImage.setHierarchy( // From: https://github.com/facebook/fresco/issues/1176#issuecomment-216830098
GenericDraweeHierarchyBuilder.newInstance(getResources()) // android.support.v4.content.ContextCompat creates your vector drawable
.setPlaceholderImage(placeholderDrawable) Drawable placeholderDrawable = ContextCompat.getDrawable(getContext(), R.drawable.image_dummy);
.build());
loadImage(absolutize(attachment.getImageUrl()), attachedImage, load, autoloadImages); // Set the placeholder image to the placeholder vector drawable
} attachedImage.setHierarchy(
GenericDraweeHierarchyBuilder.newInstance(getResources())
.setPlaceholderImage(placeholderDrawable)
.build());
private void showFieldsAttachment(Attachment attachment, View attachmentView) { loadImage(absolutize(attachment.getImageUrl()), attachedImage, load, autoloadImages);
List<AttachmentField> fields = attachment.getAttachmentFields();
if (fields == null || fields.size() == 0) {
return;
} }
final ViewGroup attachmentContent = attachmentView.findViewById(R.id.attachment_content); private void showFieldsAttachment(Attachment attachment, View attachmentView) {
List<AttachmentField> fields = attachment.getAttachmentFields();
if (fields == null || fields.size() == 0) {
return;
}
for (int i = 0, size = fields.size(); i < size; i++) { final ViewGroup attachmentContent = attachmentView.findViewById(R.id.attachment_content);
final AttachmentField attachmentField = fields.get(i);
if (attachmentField.getTitle() == null
|| attachmentField.getText() == null) {
return;
}
MessageAttachmentFieldLayout fieldLayout = new MessageAttachmentFieldLayout(getContext());
fieldLayout.setTitle(attachmentField.getTitle());
fieldLayout.setValue(attachmentField.getText());
attachmentContent.addView(fieldLayout); for (int i = 0, size = fields.size(); i < size; i++) {
final AttachmentField attachmentField = fields.get(i);
if (attachmentField.getTitle() == null
|| attachmentField.getText() == null) {
return;
}
MessageAttachmentFieldLayout fieldLayout = new MessageAttachmentFieldLayout(getContext());
fieldLayout.setTitle(attachmentField.getTitle());
fieldLayout.setValue(attachmentField.getText());
attachmentContent.addView(fieldLayout);
}
} }
}
private String absolutize(String url) { private String absolutize(String url) {
if (absoluteUrl == null) { if (absoluteUrl == null) {
return url; return url;
}
return absoluteUrl.from(url);
} }
return absoluteUrl.from(url);
} private void loadImage(final String url, final SimpleDraweeView drawee, final View load,
boolean autoloadImage) {
private void loadImage(final String url, final SimpleDraweeView drawee, final View load, if (autoloadImage || isCached(Uri.parse(url))) {
boolean autoloadImage) { load.setVisibility(GONE);
if (autoloadImage || isCached(Uri.parse(url))) { FrescoHelper.INSTANCE.loadImageWithCustomization(drawee, url);
load.setVisibility(GONE); return;
FrescoHelper.INSTANCE.loadImageWithCustomization(drawee, url); }
return;
load.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
load.setVisibility(GONE);
load.setOnClickListener(null);
FrescoHelper.INSTANCE.loadImageWithCustomization(drawee, url);
}
});
} }
load.setOnClickListener(new OnClickListener() { private boolean isCached(Uri loadUri) {
@Override if (loadUri == null) {
public void onClick(View v) { return false;
load.setVisibility(GONE); }
load.setOnClickListener(null); ImageRequest imageRequest = ImageRequest.fromUri(loadUri);
FrescoHelper.INSTANCE.loadImageWithCustomization(drawee, url); CacheKey cacheKey = DefaultCacheKeyFactory.getInstance()
} .getEncodedCacheKey(imageRequest, null);
}); return ImagePipelineFactory.getInstance()
} .getMainFileCache().hasKey(cacheKey);
private boolean isCached(Uri loadUri) {
if (loadUri == null) {
return false;
} }
ImageRequest imageRequest = ImageRequest.fromUri(loadUri);
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance()
.getEncodedCacheKey(imageRequest, null);
return ImagePipelineFactory.getInstance()
.getMainFileCache().hasKey(cacheKey);
}
} }
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<color name="color_accent">#FF2D91FA</color> <color name="color_accent">#FF2D91FA</color>
<color name="colorPrimary">#044b76</color>
<color name="colorPrimaryDark">#FFF</color>
<color name="color_shadow">#FFE6E6E7</color> <color name="color_shadow">#FFE6E6E7</color>
<color name="color_icon_composer">#FFA8A8A8</color> <color name="color_icon_composer">#FFA8A8A8</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