Fix issue #503 - Wrong URL displayed when long pressing a link that has been transformed

This commit is contained in:
Thomas 2022-11-21 09:55:42 +01:00
parent 9b45f57f87
commit 5160cd65c5
3 changed files with 33 additions and 30 deletions

View File

@ -127,11 +127,7 @@ public class Status implements Serializable, Cloneable {
public synchronized Spannable getSpanContent(Context context, WeakReference<View> viewWeakReference) {
return SpannableHelper.convert(context, content, this, null, null, true, viewWeakReference);
}
//Some extra spannable element - They will be filled automatically when fetching the status
public Spannable getSpanContentNitter() {
return SpannableHelper.convertNitter(content);
}
public synchronized Spannable getSpanSpoiler(Context context, WeakReference<View> viewWeakReference) {
return SpannableHelper.convert(context, spoiler_text, this, null, null, true, viewWeakReference);

View File

@ -745,39 +745,56 @@ public class Helper {
*/
public static String transformURL(Context context, String url) {
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
Matcher matcher = Helper.nitterPattern.matcher(url);
Matcher matcher;
boolean nitter = Helper.getSharedValue(context, context.getString(R.string.SET_NITTER));
if (nitter) {
matcher = Helper.nitterPattern.matcher(url);
if (matcher.find()) {
final String nitter_directory = matcher.group(2);
String nitterHost = sharedpreferences.getString(context.getString(R.string.SET_NITTER_HOST), context.getString(R.string.DEFAULT_NITTER_HOST)).toLowerCase();
if (nitterHost.trim().isEmpty()) {
nitterHost = context.getString(R.string.DEFAULT_NITTER_HOST);
}
return "https://" + nitterHost + nitter_directory;
}
}
matcher = Helper.bibliogramPattern.matcher(url);
boolean bibliogram = Helper.getSharedValue(context, context.getString(R.string.SET_BIBLIOGRAM));
if (bibliogram) {
matcher = Helper.bibliogramPattern.matcher(url);
if (matcher.find()) {
final String bibliogram_directory = matcher.group(2);
String bibliogramHost = sharedpreferences.getString(context.getString(R.string.SET_BIBLIOGRAM_HOST), context.getString(R.string.DEFAULT_BIBLIOGRAM_HOST)).toLowerCase();
if (bibliogramHost.trim().isEmpty()) {
bibliogramHost = context.getString(R.string.DEFAULT_BIBLIOGRAM_HOST);
}
return "https://" + bibliogramHost + bibliogram_directory;
}
}
matcher = Helper.libredditPattern.matcher(url);
boolean libreddit = Helper.getSharedValue(context, context.getString(R.string.SET_LIBREDDIT));
if (libreddit) {
matcher = Helper.libredditPattern.matcher(url);
if (matcher.find()) {
final String libreddit_directory = matcher.group(3);
String libreddit_host = sharedpreferences.getString(context.getString(R.string.SET_LIBREDDIT_HOST), context.getString(R.string.DEFAULT_LIBREDDIT_HOST)).toLowerCase();
if (libreddit_host.trim().isEmpty()) {
libreddit_host = context.getString(R.string.DEFAULT_LIBREDDIT_HOST);
}
return "https://" + libreddit_host + "/" + libreddit_directory;
}
}
matcher = Helper.youtubePattern.matcher(url);
boolean invidious = Helper.getSharedValue(context, context.getString(R.string.SET_INVIDIOUS));
if (invidious) {
matcher = Helper.youtubePattern.matcher(url);
if (matcher.find()) {
final String youtubeId = matcher.group(3);
String invidiousHost = sharedpreferences.getString(context.getString(R.string.SET_INVIDIOUS_HOST), context.getString(R.string.DEFAULT_INVIDIOUS_HOST)).toLowerCase();
if (invidiousHost.trim().isEmpty()) {
invidiousHost = context.getString(R.string.DEFAULT_INVIDIOUS_HOST);
}
if (matcher.group(2) != null && Objects.equals(matcher.group(2), "youtu.be")) {
return "https://" + invidiousHost + "/watch?v=" + youtubeId + "&local=true";
} else {
@ -785,9 +802,10 @@ public class Helper {
}
}
}
matcher = Helper.mediumPattern.matcher(url);
boolean medium = Helper.getSharedValue(context, context.getString(R.string.REPLACE_MEDIUM));
if (medium) {
matcher = Helper.mediumPattern.matcher(url);
if (matcher.find()) {
String path = matcher.group(2);
String user = matcher.group(1);
@ -795,12 +813,16 @@ public class Helper {
path = user + "/" + path;
}
String mediumReplaceHost = sharedpreferences.getString(context.getString(R.string.REPLACE_MEDIUM_HOST), context.getString(R.string.DEFAULT_REPLACE_MEDIUM_HOST)).toLowerCase();
if (mediumReplaceHost.trim().isEmpty()) {
mediumReplaceHost = context.getString(R.string.DEFAULT_REPLACE_MEDIUM_HOST);
}
return "https://" + mediumReplaceHost + "/" + path;
}
}
matcher = Helper.wikipediaPattern.matcher(url);
boolean wikipedia = Helper.getSharedValue(context, context.getString(R.string.REPLACE_WIKIPEDIA));
if (wikipedia) {
matcher = Helper.wikipediaPattern.matcher(url);
if (matcher.find()) {
String subdomain = matcher.group(1);
String path = matcher.group(2);
@ -810,6 +832,9 @@ public class Helper {
lang = (path.contains("?")) ? TextUtils.htmlEncode("&") : "?";
lang = lang + "lang=" + subdomain;
}
if (wikipediaReplaceHost.trim().isEmpty()) {
wikipediaReplaceHost = context.getString(R.string.DEFAULT_REPLACE_WIKIPEDIA_HOST);
}
return "https://" + wikipediaReplaceHost + "/" + path + lang;
}
}

View File

@ -249,8 +249,8 @@ public class SpannableHelper {
dialogBuilder.setView(popupLinksBinding.getRoot());
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
String finalURl = url;
String uniqueUrl = url.endsWith("") ? url : url + "";
String finalURl = newURL;
String uniqueUrl = newURL.endsWith("") ? newURL : newURL + "";
if (urlDetails.containsValue(uniqueUrl)) {
finalURl = Helper.getKeyByValue(urlDetails, uniqueUrl);
}
@ -872,24 +872,6 @@ public class SpannableHelper {
}
}
/**
* Convert HTML content to text. Also, it handles click on link
* This needs to be run asynchronously
*
* @param text String - text to convert, it can be content, spoiler, poll items, etc.
* @return Spannable string
*/
public static Spannable convertNitter(String text) {
SpannableString initialContent;
if (text == null) {
return null;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
initialContent = new SpannableString(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));
else
initialContent = new SpannableString(Html.fromHtml(text));
return initialContent;
}
/**