From 7aa49998425b768244715b19c9779202c0145230 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Sat, 9 Feb 2019 14:34:49 +0100 Subject: show web link previews in chat fixes #113 --- .../pixart/messenger/ui/widget/RichLinkView.java | 184 +++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 src/main/java/de/pixart/messenger/ui/widget/RichLinkView.java (limited to 'src/main/java/de/pixart/messenger/ui/widget') diff --git a/src/main/java/de/pixart/messenger/ui/widget/RichLinkView.java b/src/main/java/de/pixart/messenger/ui/widget/RichLinkView.java new file mode 100644 index 000000000..29f04970d --- /dev/null +++ b/src/main/java/de/pixart/messenger/ui/widget/RichLinkView.java @@ -0,0 +1,184 @@ +package de.pixart.messenger.ui.widget; + +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.Build; +import android.support.annotation.RequiresApi; +import android.util.AttributeSet; +import android.util.Log; +import android.view.View; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.RelativeLayout; +import android.widget.TextView; + +import com.squareup.picasso.Picasso; + +import de.pixart.messenger.R; +import io.github.ponnamkarthik.richlinkpreview.MetaData; +import io.github.ponnamkarthik.richlinkpreview.ResponseListener; +import io.github.ponnamkarthik.richlinkpreview.RichLinkListener; +import io.github.ponnamkarthik.richlinkpreview.RichPreview; +import io.github.ponnamkarthik.richlinkpreview.ViewListener; + +/** + * Created by ponna on 16-01-2018. + */ + +public class RichLinkView extends RelativeLayout { + + private View view; + Context context; + private MetaData meta; + + LinearLayout linearLayout; + ImageView imageView; + TextView textViewTitle; + TextView textViewDesp; + TextView textViewUrl; + + private String main_url; + + private boolean isDefaultClick = true; + + private RichLinkListener richLinkListener; + + + public RichLinkView(Context context) { + super(context); + this.context = context; + } + + public RichLinkView(Context context, AttributeSet attrs) { + super(context, attrs); + this.context = context; + } + + public RichLinkView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + this.context = context; + } + + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + public RichLinkView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + this.context = context; + } + + @Override + protected void onFinishInflate() { + super.onFinishInflate(); + } + + + public void initView() { + if (findLinearLayoutChild() != null) { + this.view = findLinearLayoutChild(); + } else { + this.view = this; + inflate(context, R.layout.link_layout, this); + } + linearLayout = findViewById(R.id.rich_link_card); + imageView = findViewById(R.id.rich_link_image); + textViewTitle = findViewById(R.id.rich_link_title); + textViewDesp = findViewById(R.id.rich_link_desp); + textViewUrl = findViewById(R.id.rich_link_url); + + if (!meta.getImageurl().equals("") || !meta.getImageurl().isEmpty() + && !meta.getTitle().isEmpty() || !meta.getTitle().equals("") + && !meta.getUrl().isEmpty() || !meta.getUrl().equals("") + && !meta.getDescription().isEmpty() || !meta.getDescription().equals("")) { + linearLayout.setVisibility(VISIBLE); + } else { + linearLayout.setVisibility(GONE); + } + if (meta.getImageurl().equals("") || meta.getImageurl().isEmpty()) { + imageView.setVisibility(GONE); + } else { + imageView.setVisibility(VISIBLE); + Picasso.get() + .load(meta.getImageurl()) + .into(imageView); + } + if (meta.getTitle().isEmpty() || meta.getTitle().equals("")) { + textViewTitle.setVisibility(GONE); + } else { + textViewTitle.setVisibility(VISIBLE); + textViewTitle.setText(meta.getTitle()); + } + if (meta.getUrl().isEmpty() || meta.getUrl().equals("")) { + textViewUrl.setVisibility(GONE); + } else { + textViewUrl.setVisibility(GONE); + textViewUrl.setText(meta.getUrl()); + } + if (meta.getDescription().isEmpty() || meta.getDescription().equals("")) { + textViewDesp.setVisibility(GONE); + } else { + textViewDesp.setVisibility(VISIBLE); + textViewDesp.setText(meta.getDescription()); + } + + linearLayout.setOnClickListener(view -> { + if (isDefaultClick) { + richLinkClicked(); + } else { + if (richLinkListener != null) { + richLinkListener.onClicked(view, meta); + } else { + richLinkClicked(); + } + } + }); + } + + private void richLinkClicked() { + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(main_url)); + context.startActivity(intent); + } + + public void setDefaultClickListener(boolean isDefault) { + isDefaultClick = isDefault; + } + + public void setClickListener(RichLinkListener richLinkListener1) { + richLinkListener = richLinkListener1; + } + + protected LinearLayout findLinearLayoutChild() { + if (getChildCount() > 0 && getChildAt(0) instanceof LinearLayout) { + return (LinearLayout) getChildAt(0); + } + return null; + } + + public void setLinkFromMeta(MetaData metaData) { + meta = metaData; + initView(); + } + + public MetaData getMetaData() { + return meta; + } + + public void setLink(String url, final ViewListener viewListener) { + main_url = url; + RichPreview richPreview = new RichPreview(new ResponseListener() { + @Override + public void onData(MetaData metaData) { + meta = metaData; + if (!meta.getTitle().isEmpty() || !meta.getTitle().equals("")) { + viewListener.onSuccess(true); + } + initView(); + } + + @Override + public void onError(Exception e) { + viewListener.onError(e); + } + }); + richPreview.getPreview(url); + } +} -- cgit v1.2.3