rework webpreview for links in messages

* always use tracking filter for urls
* add quote for preview
* some small color fixes
This commit is contained in:
Christian Schneppe 2020-11-19 21:35:16 +01:00
parent acbe6a4999
commit 7f100565ba
No known key found for this signature in database
GPG key ID: F30B8D686B44D87E
7 changed files with 52 additions and 27 deletions

View file

@ -124,7 +124,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
private Boolean isGeoUri = null;
private Boolean isXmppUri = null;
private Boolean isWebUri = null;
private String WebUri = "";
private String WebUri = null;
private Boolean isEmojisOnly = null;
private Boolean treatAsDownloadable = null;
private FileParams fileParams = null;
@ -866,20 +866,22 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
public synchronized boolean isWebUri() {
if (isWebUri == null) {
Pattern pattern = Patterns.WEB_URL;
Matcher matcher = pattern.matcher(body);
isWebUri = matcher.find();
isWebUri = Patterns.WEB_URL.matcher(body).matches();
}
return isWebUri;
}
public synchronized String getWebUri() {
if (isWebUri) {
Pattern pattern = Patterns.WEB_URL;
Matcher matcher = pattern.matcher(body);
if (WebUri.equalsIgnoreCase("") && matcher.find()) {
WebUri = matcher.group(0);
};
final Pattern urlPattern = Pattern.compile(
"(?:(?:https?):\\/\\/|www\\.)(?:\\([-A-Z0-9+&@#\\/%=~_|$?!:,.]*\\)|[-A-Z0-9+&@#\\/%=~_|$?!:,.])*(?:\\([-A-Z0-9+&@#\\/%=~_|$?!:,.]*\\)|[A-Z0-9+&@#\\/%=~_|$])",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
Matcher m = urlPattern.matcher(body);
while (m.find()) {
if (WebUri == null) {
WebUri = m.group(0);
Log.d(Config.LOGTAG, "Weburi Message: " + WebUri);
return WebUri;
}
}
return WebUri;
}

View file

@ -101,6 +101,7 @@ import static eu.siacs.conversations.entities.Message.DELETED_MESSAGE_BODY_OLD;
import static eu.siacs.conversations.ui.SettingsActivity.PLAY_GIF_INSIDE;
import static eu.siacs.conversations.ui.SettingsActivity.SHOW_LINKS_INSIDE;
import static eu.siacs.conversations.ui.SettingsActivity.SHOW_MAPS_INSIDE;
import static eu.siacs.conversations.ui.util.MyLinkify.removeTrackingParameter;
import static eu.siacs.conversations.ui.util.MyLinkify.removeTrailingBracket;
import static eu.siacs.conversations.ui.util.MyLinkify.replaceYoutube;
@ -660,7 +661,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
StylingHelper.highlight(activity, body, highlightedTerm, StylingHelper.isDarkText(viewHolder.messageBody));
}
}
if (message.isWebUri() && (message.getWebUri() != null || message.getWebUri().equalsIgnoreCase(""))) {
if (message.isWebUri() || message.getWebUri() != null) {
displayRichLinkMessage(viewHolder, message, darkBackground);
}
MyLinkify.addLinks(body, true);
@ -787,10 +788,17 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
viewHolder.gifImage.setVisibility(View.GONE);
viewHolder.download_button.setVisibility(View.GONE);
viewHolder.progressBar.setVisibility(View.GONE);
final SpannableStringBuilder body = new SpannableStringBuilder(replaceYoutube(activity.getApplicationContext(), message.getWebUri()));
String url;
if (message.isWebUri()) {
url = removeTrackingParameter(Uri.parse(message.getBody().trim())).toString();
} else {
url = removeTrackingParameter(Uri.parse(message.getWebUri())).toString();
}
final SpannableStringBuilder body = new SpannableStringBuilder(replaceYoutube(activity.getApplicationContext(), url));
final boolean dataSaverDisabled = activity.xmppConnectionService.isDataSaverDisabled();
viewHolder.richlinkview.setVisibility(View.VISIBLE);
viewHolder.richlinkview.setVisibility(mShowLinksInside ? View.VISIBLE : View.GONE);
if (mShowLinksInside) {
final int color = ThemeHelper.getMessageTextColor(activity, darkBackground, false);
final double target = metrics.density * 200;
final int scaledH;
if (Math.max(100, 100) * metrics.density <= target) {
@ -803,16 +811,15 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WRAP_CONTENT, scaledH);
layoutParams.setMargins(0, (int) (metrics.density * 4), 0, (int) (metrics.density * 4));
viewHolder.richlinkview.setLayoutParams(layoutParams);
final String url = body.toString();
final String weburl;
final String lcUrl = url.trim();
final String lcUrl = body.toString();
if (lcUrl.startsWith("http://") || lcUrl.startsWith("https://")) {
weburl = removeTrailingBracket(url);
} else {
weburl = "http://" + removeTrailingBracket(url);
}
Log.d(Config.LOGTAG, "Weburi: " + weburl);
viewHolder.richlinkview.setLink(weburl, message.getUuid(), dataSaverDisabled, activity.xmppConnectionService, new RichPreview.ViewListener() {
Log.d(Config.LOGTAG, "Weburi for preview: " + weburl);
viewHolder.richlinkview.setLink(weburl, message.getUuid(), dataSaverDisabled, activity.xmppConnectionService, color, new RichPreview.ViewListener() {
@Override
public void onSuccess(boolean status) {
@ -824,8 +831,6 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
viewHolder.richlinkview.setVisibility(View.GONE);
}
});
} else {
viewHolder.richlinkview.setVisibility(View.GONE);
}
}

View file

@ -128,7 +128,7 @@ public class MyLinkify {
// https://docs.oracle.com/en/cloud/saas/marketing/eloqua-user/Help/EloquaAsynchronousTrackingScripts/EloquaTrackingParameters.htm
if (key.toLowerCase(Locale.ROOT).startsWith("utm_") ||
key.toLowerCase(Locale.ROOT).startsWith("elq") ||
TrackingHelper.TRACKIN_PARAMETER.contains(key.toLowerCase(Locale.ROOT)) ||
TrackingHelper.TRACKING_PARAMETER.contains(key.toLowerCase(Locale.ROOT)) ||
("snr".equals(key) && "store.steampowered.com".equals(uri.getHost())))
changed = true;
else if (!TextUtils.isEmpty(key))

View file

@ -38,6 +38,7 @@ public class RichLinkView extends RelativeLayout {
ImageView imageView;
TextView textViewTitle;
TextView textViewDesp;
View quoteMessage;
private String main_url;
@ -72,7 +73,7 @@ public class RichLinkView extends RelativeLayout {
}
public void initView(final boolean dataSaverDisabled) {
public void initView(final boolean dataSaverDisabled, final int color) {
if (findLinearLayoutChild() != null) {
this.view = findLinearLayoutChild();
} else {
@ -82,7 +83,11 @@ public class RichLinkView extends RelativeLayout {
linearLayout = findViewById(R.id.rich_link_card);
imageView = findViewById(R.id.rich_link_image);
textViewTitle = findViewById(R.id.rich_link_title);
textViewTitle.setTextColor(color);
textViewDesp = findViewById(R.id.rich_link_desp);
textViewDesp.setTextColor(color);
quoteMessage = findViewById(R.id.quote_message);
quoteMessage.setBackgroundColor(color);
imageView.setAdjustViewBounds(true);
if (meta.getImageurl() != null && !meta.getImageurl().equals("") && !meta.getImageurl().isEmpty()) {
if (!dataSaverDisabled) {
@ -156,16 +161,18 @@ public class RichLinkView extends RelativeLayout {
return null;
}
/*
public void setLinkFromMeta(MetaData metaData) {
meta = metaData;
initView(true);
}
*/
public MetaData getMetaData() {
return meta;
}
public void setLink(final String url, final String filename, final boolean dataSaverDisabled, final XmppConnectionService mXmppConnectionService, final RichPreview.ViewListener viewListener) {
public void setLink(final String url, final String filename, final boolean dataSaverDisabled, final XmppConnectionService mXmppConnectionService, final int color, final RichPreview.ViewListener viewListener) {
main_url = url;
RichPreview richPreview = new RichPreview(new RichPreview.ResponseListener() {
@Override
@ -174,7 +181,7 @@ public class RichLinkView extends RelativeLayout {
if (!meta.getTitle().isEmpty() || !meta.getTitle().equals("")) {
viewListener.onSuccess(true);
}
initView(dataSaverDisabled);
initView(dataSaverDisabled, color);
}
@Override

View file

@ -6,7 +6,7 @@ import java.util.List;
public class TrackingHelper {
// https://github.com/newhouse/url-tracking-stripper
public static final List<String> TRACKIN_PARAMETER = Collections.unmodifiableList(Arrays.asList(
public static final List<String> TRACKING_PARAMETER = Collections.unmodifiableList(Arrays.asList(
// https://en.wikipedia.org/wiki/UTM_parameters
"icid", // Adobe
"gclid", // Google

View file

@ -4,14 +4,23 @@
android:id="@+id/rich_link_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:gravity="center">
<View
android:id="@+id/quote_message"
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="?attr/colorAccent" />
<ImageView
android:id="@+id/rich_link_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="2dp"
android:padding="4dp"
android:scaleType="centerCrop" />
<LinearLayout
@ -27,6 +36,7 @@
android:maxLines="2"
android:padding="2dp"
android:textAppearance="@style/TextAppearance.Conversations.Body2"
android:textColor="?attr/text_Color_Main"
tools:text="Data" />
<TextView
@ -37,6 +47,7 @@
android:maxLines="3"
android:padding="2dp"
android:textAppearance="@style/TextAppearance.Conversations.Status"
android:textColor="?attr/text_Color_Main"
tools:text="Data" />
</LinearLayout>

View file

@ -72,7 +72,7 @@
<!-- grey theme -->
<color name="lightgrey">#fffcfcfc</color> <!-- light theme other -->
<color name="lightgrey2">#ffeaeaea</color> <!-- light theme me -->
<color name="middlegrey">#ff727272</color> <!-- dark theme other -->
<color name="middlegrey">#ff5b5b5b</color> <!-- dark theme other -->
<color name="darkgrey">#ff444444</color> <!-- dark theme me -->
<!-- black theme -->