Added option to show background colors

pull/199/head
M66B 3 years ago
parent f1aec9b3cd
commit 72869a9778

@ -124,7 +124,7 @@ public class FragmentOptions extends FragmentBase {
"keywords_header", "labels_header", "flags", "flags_background", "preview", "preview_italic", "preview_lines",
"message_zoom", "overview_mode", "addresses", "button_extra", "attachments_alt", "thumbnails",
"contrast", "monospaced", "monospaced_pre",
"text_color", "text_size", "text_font", "text_align", "text_separators",
"background_color", "text_color", "text_size", "text_font", "text_align", "text_separators",
"collapse_quotes", "image_placeholders", "inline_images",
"seekbar", "actionbar", "actionbar_color", "navbar_colorize",
"autoscroll", "swipenav", "swipe_close", "swipe_move", "autoexpand", "autoclose", "onclose",

@ -127,6 +127,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
private SwitchCompat swContrast;
private SwitchCompat swMonospaced;
private SwitchCompat swMonospacedPre;
private SwitchCompat swBackgroundColor;
private SwitchCompat swTextColor;
private SwitchCompat swTextSize;
private SwitchCompat swTextFont;
@ -158,7 +159,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
"preview", "preview_italic", "preview_lines",
"addresses",
"message_zoom", "overview_mode", "contrast", "monospaced", "monospaced_pre",
"text_color", "text_size", "text_font", "text_align", "text_separators",
"background_color", "text_color", "text_size", "text_font", "text_align", "text_separators",
"collapse_quotes", "image_placeholders", "inline_images", "button_extra", "attachments_alt", "thumbnails",
"parse_classes", "authentication"
};
@ -238,6 +239,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
swContrast = view.findViewById(R.id.swContrast);
swMonospaced = view.findViewById(R.id.swMonospaced);
swMonospacedPre = view.findViewById(R.id.swMonospacedPre);
swBackgroundColor = view.findViewById(R.id.swBackgroundColor);
swTextColor = view.findViewById(R.id.swTextColor);
swTextSize = view.findViewById(R.id.swTextSize);
swTextFont = view.findViewById(R.id.swTextFont);
@ -799,6 +801,18 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
}
});
String theme = prefs.getString("theme", "blue_orange_system");
boolean bw = "black_and_white".equals(theme);
swBackgroundColor.setEnabled(!bw);
swBackgroundColor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("background_color", checked).apply();
}
});
swTextColor.setEnabled(!bw);
swTextColor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@ -806,9 +820,6 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
}
});
String theme = prefs.getString("theme", "blue_orange_system");
swTextColor.setEnabled(!"black_and_white".equals(theme));
swTextSize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@ -1061,6 +1072,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
swContrast.setChecked(prefs.getBoolean("contrast", false));
swMonospaced.setChecked(prefs.getBoolean("monospaced", false));
swMonospacedPre.setChecked(prefs.getBoolean("monospaced_pre", false));
swBackgroundColor.setChecked(prefs.getBoolean("background_color", false));
swTextColor.setChecked(prefs.getBoolean("text_color", true));
swTextSize.setChecked(prefs.getBoolean("text_size", true));
swTextFont.setChecked(prefs.getBoolean("text_font", true));

@ -36,7 +36,9 @@ import android.text.Spanned;
import android.text.TextDirectionHeuristics;
import android.text.TextUtils;
import android.text.style.AlignmentSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.BulletSpan;
import android.text.style.CharacterStyle;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
@ -357,7 +359,9 @@ public class HtmlHelper {
private static Document sanitize(Context context, Document parsed, boolean view, boolean show_images) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String theme = prefs.getString("theme", "blue_orange_system");
boolean text_color = (!view || (prefs.getBoolean("text_color", true) && !"black_and_white".equals(theme)));
boolean bw = "black_and_white".equals(theme);
boolean background_color = (!view || (!bw && prefs.getBoolean("background_color", false)));
boolean text_color = (!view || (!bw && prefs.getBoolean("text_color", true)));
boolean text_size = (!view || prefs.getBoolean("text_size", true));
boolean text_font = (!view || prefs.getBoolean("text_font", true));
boolean text_align = prefs.getBoolean("text_align", true);
@ -368,8 +372,6 @@ public class HtmlHelper {
boolean text_separators = prefs.getBoolean("text_separators", true);
boolean image_placeholders = prefs.getBoolean("image_placeholders", true);
int textColorPrimary = Helper.resolveColor(context, android.R.attr.textColorPrimary);
// https://chromium.googlesource.com/chromium/blink/+/master/Source/core/css/html.css
// <!--[if ...]><!--> ... <!--<![endif]-->
@ -575,31 +577,37 @@ public class HtmlHelper {
String value = kv.get(key);
switch (key) {
case "color":
case "background-color":
// https://developer.mozilla.org/en-US/docs/Web/CSS/color
if (!text_color)
if ("color".equals(key) && !text_color)
continue;
if ("background-color".equals(key) && !background_color)
continue;
Integer color = parseColor(value);
if (color != null && !view && Helper.isDarkTheme(context)) {
if (color != null && !view && dark) {
float lum = (float) ColorUtils.calculateLuminance(color);
if (lum < 0.1f)
color = null;
}
if (color == null)
element.removeAttr("color");
else {
if (view)
color = adjustColor(dark, textColorPrimary, color);
// fromHtml does not support transparency
String c = String.format("#%06x", color);
sb.append("color:").append(c).append(";");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
element.attr("color", c);
if (color != null && view)
if ("color".equals(key))
color = adjustColor(dark, color);
else
color = adjustColor(!dark, color);
if (color == null) {
element.removeAttr(key);
continue;
}
// fromHtml does not support transparency
String c = String.format("#%06x", color);
sb.append(key).append(':').append(c).append(";");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
element.attr(key, c);
break;
case "font-size":
@ -1549,15 +1557,14 @@ public class HtmlHelper {
return color;
}
private static Integer adjustColor(boolean dark, int textColorPrimary, Integer color) {
private static Integer adjustColor(boolean dark, Integer color) {
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
if (r == g && r == b && (dark ? 255 - r : r) < GRAY_THRESHOLD)
color = textColorPrimary;
else
color = Helper.adjustLuminance(color, dark, MIN_LUMINANCE);
return null;
color = Helper.adjustLuminance(color, dark, MIN_LUMINANCE);
return (color & 0xFFFFFF);
}
@ -2347,10 +2354,16 @@ public class HtmlHelper {
String value = param.substring(semi + 1);
switch (key) {
case "color":
case "background-color":
if (!TextUtils.isEmpty(value))
try {
int color = Integer.parseInt(value.substring(1), 16) | 0xFF000000;
setSpan(ssb, new ForegroundColorSpan(color), start, ssb.length());
CharacterStyle span;
if ("color".equals(key))
span = new ForegroundColorSpan(color);
else
span = new BackgroundColorSpan(color);
setSpan(ssb, span, start, ssb.length());
} catch (NumberFormatException ex) {
Log.i(ex);
}

@ -1129,6 +1129,18 @@
app:layout_constraintTop_toBottomOf="@id/swMonospaced"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swBackgroundColor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:enabled="true"
android:text="@string/title_advanced_background_color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swMonospacedPre"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swTextColor"
android:layout_width="0dp"
@ -1138,7 +1150,7 @@
android:text="@string/title_advanced_text_color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swMonospacedPre"
app:layout_constraintTop_toBottomOf="@id/swBackgroundColor"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat

@ -422,6 +422,7 @@
<string name="title_advanced_contrast">Use high contrast for message text</string>
<string name="title_advanced_monospaced">Use monospaced font for message text by default</string>
<string name="title_advanced_monospaced_pre">Use monospaced font for preformatted text</string>
<string name="title_advanced_background_color">Use background colors</string>
<string name="title_advanced_text_color">Use text colors</string>
<string name="title_advanced_text_size">Use text sizes</string>
<string name="title_advanced_text_font">Use fonts</string>

Loading…
Cancel
Save