Add quotes in messages

This commit is contained in:
Thomas 2022-11-05 18:21:49 +01:00
parent 3f9e221025
commit 84d30676fc
3 changed files with 79 additions and 1 deletions

View file

@ -26,7 +26,6 @@
android:configChanges="orientation|screenSize"
android:icon="@mipmap/ic_launcher_bubbles"
android:label="@string/app_name"
android:hardwareAccelerated="false"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_bubbles_round"
android:supportsRtl="true"

View file

@ -0,0 +1,54 @@
package app.fedilab.android.helper;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Layout;
import android.text.style.LeadingMarginSpan;
import android.text.style.LineBackgroundSpan;
import org.jetbrains.annotations.NotNull;
/**
* Original work from @heath-borders: https://stackoverflow.com/a/29114976/3197259
*/
public class CustomQuoteSpan implements LeadingMarginSpan, LineBackgroundSpan {
private final int backgroundColor;
private final int stripeColor;
private final float stripeWidth;
private final float gap;
public CustomQuoteSpan(int backgroundColor, int stripeColor, float stripeWidth, float gap) {
this.backgroundColor = backgroundColor;
this.stripeColor = stripeColor;
this.stripeWidth = stripeWidth;
this.gap = gap;
}
@Override
public int getLeadingMargin(boolean first) {
return (int) (stripeWidth + gap);
}
@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom,
CharSequence text, int start, int end, boolean first, Layout layout) {
Paint.Style style = p.getStyle();
int paintColor = p.getColor();
p.setStyle(Paint.Style.FILL);
p.setColor(stripeColor);
c.drawRect(x, top, x + dir * stripeWidth, bottom, p);
p.setStyle(style);
p.setColor(paintColor);
}
@Override
public void drawBackground(@NotNull Canvas c, @NotNull Paint p, int left, int right, int top, int baseline, int bottom, @NotNull CharSequence text, int start, int end, int lnum) {
int paintColor = p.getColor();
p.setColor(backgroundColor);
c.drawRect(left, top, right, bottom, p);
p.setColor(paintColor);
}
}

View file

@ -36,6 +36,7 @@ import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.text.style.QuoteSpan;
import android.text.style.URLSpan;
import android.util.Patterns;
import android.view.LayoutInflater;
@ -44,6 +45,7 @@ import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
import com.bumptech.glide.Glide;
@ -90,6 +92,8 @@ public class SpannableHelper {
if (text == null) {
return null;
}
text = text.replaceAll("(&gt;(.*))<\\s?br\\s?/?>", "<blockquote>$2</blockquote>");
text = text.replaceAll("(&gt;(.*))<\\s?/p\\s?/?>", "<blockquote>$2</blockquote>");
Pattern imgPattern = Pattern.compile("<img [^>]*src=\"([^\"]+)\"[^>]*>");
Matcher matcherImg = imgPattern.matcher(text);
HashMap<String, String> imagesToReplace = new LinkedHashMap<>();
@ -144,6 +148,7 @@ public class SpannableHelper {
linkify(context, content, urlDetails);
linkifyURL(context, content, urlDetails);
emails(context, content);
replaceQuoteSpans(context, content);
} else {
content = new SpannableStringBuilder(text);
}
@ -848,6 +853,26 @@ public class SpannableHelper {
}
}
private static void replaceQuoteSpans(Context context, Spannable spannable) {
QuoteSpan[] quoteSpans = spannable.getSpans(0, spannable.length(), QuoteSpan.class);
for (QuoteSpan quoteSpan : quoteSpans) {
int start = spannable.getSpanStart(quoteSpan);
int end = spannable.getSpanEnd(quoteSpan);
int flags = spannable.getSpanFlags(quoteSpan);
spannable.removeSpan(quoteSpan);
int colord = ContextCompat.getColor(context, R.color.cyanea_accent_reference);
spannable.setSpan(new CustomQuoteSpan(
ContextCompat.getColor(context, R.color.transparent),
colord,
10,
20),
start,
end,
flags);
}
}
/**
* Convert HTML content to text. Also, it handles click on link
* This needs to be run asynchronously