Show suggestion help

This commit is contained in:
M66B
2023-09-13 08:53:24 +02:00
parent bf1193d787
commit bd89910485
3 changed files with 116 additions and 40 deletions

View File

@@ -0,0 +1,70 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2023 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.TextPaint;
import android.text.style.SuggestionSpan;
import java.lang.reflect.Field;
public class SuggestionSpanEx extends SuggestionSpan {
private String description;
private final int highlightColor;
private final int underlineColor;
private final int underlineThickness;
public SuggestionSpanEx(Context context, String description, String[] suggestions, boolean misspelled) {
super(context, suggestions,
misspelled || Build.VERSION.SDK_INT < Build.VERSION_CODES.S
? SuggestionSpan.FLAG_MISSPELLED | SuggestionSpan.FLAG_EASY_CORRECT
: SuggestionSpan.FLAG_GRAMMAR_ERROR);
this.description = description;
highlightColor = Helper.resolveColor(context, android.R.attr.textColorHighlight);
underlineColor = (misspelled ? Color.RED : highlightColor);
underlineThickness = Helper.dp2pixels(context, misspelled ? 2 : 2);
}
public String getDescription() {
return this.description;
}
@Override
public void updateDrawState(TextPaint tp) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
try {
Field fUnderlineColor = tp.getClass().getDeclaredField("underlineColor");
Field fUnderlineThickness = tp.getClass().getDeclaredField("underlineThickness");
fUnderlineColor.setAccessible(true);
fUnderlineThickness.setAccessible(true);
fUnderlineColor.set(tp, underlineColor);
fUnderlineThickness.set(tp, underlineThickness);
} catch (Throwable ex) {
Log.i(ex);
tp.bgColor = highlightColor;
}
else {
tp.underlineColor = underlineColor;
tp.underlineThickness = underlineThickness;
}
}
}