forked from mirrors/Fedilab
Some fixes
This commit is contained in:
parent
ef58c7bc40
commit
6c3356d2be
3 changed files with 29 additions and 8 deletions
|
@ -7,7 +7,6 @@ import android.graphics.Paint;
|
||||||
import android.graphics.drawable.Animatable;
|
import android.graphics.drawable.Animatable;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.text.style.ReplacementSpan;
|
import android.text.style.ReplacementSpan;
|
||||||
import android.util.Log;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
@ -24,7 +23,6 @@ import app.fedilab.android.R;
|
||||||
|
|
||||||
|
|
||||||
public class CustomEmoji extends ReplacementSpan {
|
public class CustomEmoji extends ReplacementSpan {
|
||||||
private final View view;
|
|
||||||
private final float scale;
|
private final float scale;
|
||||||
private final WeakReference<View> viewWeakReference;
|
private final WeakReference<View> viewWeakReference;
|
||||||
private Drawable imageDrawable;
|
private Drawable imageDrawable;
|
||||||
|
@ -33,7 +31,6 @@ public class CustomEmoji extends ReplacementSpan {
|
||||||
CustomEmoji(WeakReference<View> viewWeakReference) {
|
CustomEmoji(WeakReference<View> viewWeakReference) {
|
||||||
Context mContext = viewWeakReference.get().getContext();
|
Context mContext = viewWeakReference.get().getContext();
|
||||||
this.viewWeakReference = viewWeakReference;
|
this.viewWeakReference = viewWeakReference;
|
||||||
view = viewWeakReference.get();
|
|
||||||
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
|
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||||
scale = sharedpreferences.getFloat(mContext.getString(R.string.SET_FONT_SCALE), 1.0f);
|
scale = sharedpreferences.getFloat(mContext.getString(R.string.SET_FONT_SCALE), 1.0f);
|
||||||
}
|
}
|
||||||
|
@ -69,8 +66,6 @@ public class CustomEmoji extends ReplacementSpan {
|
||||||
return new CustomTarget<Drawable>() {
|
return new CustomTarget<Drawable>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
|
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
|
||||||
Log.v(Helper.TAG, "resource: " + resource);
|
|
||||||
Log.v(Helper.TAG, "instanceof: " + (resource instanceof Animatable));
|
|
||||||
View view = viewWeakReference.get();
|
View view = viewWeakReference.get();
|
||||||
if (animate && resource instanceof Animatable) {
|
if (animate && resource instanceof Animatable) {
|
||||||
Drawable.Callback callback = resource.getCallback();
|
Drawable.Callback callback = resource.getCallback();
|
||||||
|
|
|
@ -1739,4 +1739,13 @@ public class Helper {
|
||||||
|
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
|
||||||
|
for (Map.Entry<T, E> entry : map.entrySet()) {
|
||||||
|
if (Objects.equals(value, entry.getValue())) {
|
||||||
|
return entry.getKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,7 @@ import android.text.Spanned;
|
||||||
import android.text.TextPaint;
|
import android.text.TextPaint;
|
||||||
import android.text.style.ClickableSpan;
|
import android.text.style.ClickableSpan;
|
||||||
import android.text.style.URLSpan;
|
import android.text.style.URLSpan;
|
||||||
|
import android.util.Log;
|
||||||
import android.util.Patterns;
|
import android.util.Patterns;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
@ -84,6 +85,7 @@ public class SpannableHelper {
|
||||||
boolean convertHtml,
|
boolean convertHtml,
|
||||||
WeakReference<View> viewWeakReference) {
|
WeakReference<View> viewWeakReference) {
|
||||||
|
|
||||||
|
|
||||||
SpannableString initialContent;
|
SpannableString initialContent;
|
||||||
if (text == null) {
|
if (text == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -97,6 +99,8 @@ public class SpannableHelper {
|
||||||
emojiList = status.emojis;
|
emojiList = status.emojis;
|
||||||
} else if (account != null) {
|
} else if (account != null) {
|
||||||
emojiList = account.emojis;
|
emojiList = account.emojis;
|
||||||
|
} else if (announcement != null) {
|
||||||
|
emojiList = announcement.emojis;
|
||||||
}
|
}
|
||||||
HashMap<String, String> urlDetails = new HashMap<>();
|
HashMap<String, String> urlDetails = new HashMap<>();
|
||||||
if (convertHtml) {
|
if (convertHtml) {
|
||||||
|
@ -110,6 +114,7 @@ public class SpannableHelper {
|
||||||
}
|
}
|
||||||
if (url != null && urlText != null && !url.equals(urlText) && !urlText.contains("<span")) {
|
if (url != null && urlText != null && !url.equals(urlText) && !urlText.contains("<span")) {
|
||||||
urlDetails.put(url, urlText);
|
urlDetails.put(url, urlText);
|
||||||
|
Log.v(Helper.TAG, "PUT: " + urlText + " -> " + url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
||||||
|
@ -162,10 +167,20 @@ public class SpannableHelper {
|
||||||
if (content.toString().length() < matchEnd || matchStart < 0 || matchStart > matchEnd) {
|
if (content.toString().length() < matchEnd || matchStart < 0 || matchStart > matchEnd) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
final String url = content.toString().substring(matchStart, matchEnd);
|
String url_temp = content.toString().substring(matchStart, matchEnd).replace("…", "");
|
||||||
if (!url.startsWith("http")) {
|
if (urlDetails.containsValue(url_temp + "…")) {
|
||||||
continue;
|
String originalURL = Helper.getKeyByValue(urlDetails, url_temp + "…");
|
||||||
|
if (originalURL != null) {
|
||||||
|
content.replace(matchStart, matchEnd, originalURL);
|
||||||
|
offSetTruncate += (originalURL.length() - (url_temp.length() + 1));
|
||||||
|
matchEnd = matchStart + originalURL.length();
|
||||||
|
url_temp = originalURL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
final String url = url_temp;
|
||||||
|
/* if (!url.startsWith("http")) {
|
||||||
|
continue;
|
||||||
|
}*/
|
||||||
String newURL = Helper.transformURL(context, url);
|
String newURL = Helper.transformURL(context, url);
|
||||||
//If URL has been transformed
|
//If URL has been transformed
|
||||||
if (newURL.compareTo(url) != 0) {
|
if (newURL.compareTo(url) != 0) {
|
||||||
|
@ -173,10 +188,12 @@ public class SpannableHelper {
|
||||||
offSetTruncate += (newURL.length() - url.length());
|
offSetTruncate += (newURL.length() - url.length());
|
||||||
matchEnd = matchStart + newURL.length();
|
matchEnd = matchStart + newURL.length();
|
||||||
//The transformed URL was in the list of URLs having a different names
|
//The transformed URL was in the list of URLs having a different names
|
||||||
|
|
||||||
if (urlDetails.containsKey(url)) {
|
if (urlDetails.containsKey(url)) {
|
||||||
urlDetails.put(newURL, urlDetails.get(url));
|
urlDetails.put(newURL, urlDetails.get(url));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Truncate URL if needed
|
//Truncate URL if needed
|
||||||
//TODO: add an option to disable truncated URLs
|
//TODO: add an option to disable truncated URLs
|
||||||
String urlText = newURL;
|
String urlText = newURL;
|
||||||
|
|
Loading…
Reference in a new issue