2023-09-13 06:53:24 +00:00
|
|
|
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/>.
|
|
|
|
|
2024-01-01 07:50:49 +00:00
|
|
|
Copyright 2018-2024 by Marcel Bokhorst (M66B)
|
2023-09-13 06:53:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import android.content.Context;
|
2023-09-29 16:41:34 +00:00
|
|
|
import android.content.SharedPreferences;
|
2023-09-13 06:53:24 +00:00
|
|
|
import android.graphics.Color;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.text.TextPaint;
|
|
|
|
import android.text.style.SuggestionSpan;
|
|
|
|
|
2023-09-29 16:41:34 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
2023-09-13 06:53:24 +00:00
|
|
|
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) {
|
2023-09-29 16:41:34 +00:00
|
|
|
super(context, suggestions, 0);
|
|
|
|
|
|
|
|
int flags = (misspelled || Build.VERSION.SDK_INT < Build.VERSION_CODES.S
|
|
|
|
? SuggestionSpan.FLAG_MISSPELLED
|
|
|
|
: SuggestionSpan.FLAG_GRAMMAR_ERROR);
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean easy_correct = prefs.getBoolean("easy_correct", false);
|
|
|
|
if (easy_correct)
|
|
|
|
flags |= SuggestionSpan.FLAG_EASY_CORRECT;
|
|
|
|
|
|
|
|
this.setFlags(flags);
|
|
|
|
|
2023-09-13 06:53:24 +00:00
|
|
|
this.description = description;
|
|
|
|
highlightColor = Helper.resolveColor(context, android.R.attr.textColorHighlight);
|
2023-11-03 19:46:14 +00:00
|
|
|
underlineColor = (misspelled ? Color.MAGENTA : highlightColor);
|
2023-09-29 16:41:34 +00:00
|
|
|
underlineThickness = Helper.dp2pixels(context, misspelled ? 2 : (BuildConfig.DEBUG ? 1 : 2));
|
2023-09-13 06:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|