LanguageTool improvements

This commit is contained in:
M66B 2022-04-28 09:13:46 +02:00
parent 472ea58740
commit fe2ee9d68c
2 changed files with 41 additions and 5 deletions

View File

@ -71,6 +71,7 @@ import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.ArrowKeyMovementMethod;
@ -2392,17 +2393,25 @@ public class FragmentCompose extends FragmentBase {
private void onLanguageTool() {
etBody.clearComposingText();
Log.i("LT running enabled=" + etBody.isSuggestionsEnabled());
Bundle args = new Bundle();
args.putCharSequence("text", etBody.getText());
new SimpleTask<List<LanguageTool.Suggestion>>() {
private Toast toast = null;
@Override
protected void onPreExecute(Bundle args) {
toast = ToastEx.makeText(getContext(), R.string.title_suggestions_check, Toast.LENGTH_LONG);
toast.show();
setBusy(true);
}
@Override
protected void onPostExecute(Bundle args) {
if (toast != null)
toast.cancel();
setBusy(false);
}
@ -2414,18 +2423,29 @@ public class FragmentCompose extends FragmentBase {
@Override
protected void onExecuted(Bundle args, List<LanguageTool.Suggestion> suggestions) {
// https://developer.android.com/reference/android/text/style/SuggestionSpan
final Context context = getContext();
if (suggestions == null || suggestions.size() == 0) {
ToastEx.makeText(getContext(), R.string.title_suggestions_none, Toast.LENGTH_LONG).show();
return;
}
Editable edit = etBody.getText();
if (edit == null)
return;
// https://developer.android.com/reference/android/text/style/SuggestionSpan
for (SuggestionSpanEx span : edit.getSpans(0, edit.length(), SuggestionSpanEx.class)) {
Log.i("LT removing=" + span);
edit.removeSpan(span);
}
for (LanguageTool.Suggestion suggestion : suggestions) {
Log.i("LT suggestion=" + suggestion);
SuggestionSpan span = new SuggestionSpan(context,
Log.i("LT adding=" + suggestion);
SuggestionSpan span = new SuggestionSpanEx(getContext(),
suggestion.replacements.toArray(new String[0]),
SuggestionSpan.FLAG_MISSPELLED);
int start = suggestion.offset;
int end = start + suggestion.length;
edit.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
edit.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
@ -2437,6 +2457,20 @@ public class FragmentCompose extends FragmentBase {
}.execute(this, args, "compose:lt");
}
private static class SuggestionSpanEx extends SuggestionSpan {
private final int textColorHighlight;
public SuggestionSpanEx(Context context, String[] suggestions, int flags) {
super(context, suggestions, flags);
textColorHighlight = Helper.resolveColor(context, android.R.attr.textColorHighlight);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.bgColor = textColorHighlight;
}
}
private boolean onActionStyle(int action, View anchor) {
Log.i("Style action=" + action);
return StyleHelper.apply(action, getViewLifecycleOwner(), anchor, etBody);

View File

@ -1387,6 +1387,8 @@
<string name="title_translate_html_hint">This will consume more characters</string>
<string name="title_translate_usage">Usage: %1$s / %2$s (%3$d %%)</string>
<string name="title_translate_tap">Tap the text to be translated</string>
<string name="title_suggestions_check">Checking &#8230;</string>
<string name="title_suggestions_none">No suggestions</string>
<string name="title_edit_plain_text">Edit as plain text</string>
<string name="title_edit_formatted_text">Edit as reformatted text</string>
<string name="title_select_certificate">Select public key</string>