FairEmail/app/src/main/java/eu/faircode/email/LanguageTool.java

245 lines
9.3 KiB
Java
Raw Normal View History

2022-04-27 15:18:08 +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/>.
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
2022-07-09 05:47:29 +00:00
import android.content.SharedPreferences;
import android.net.Uri;
2022-10-03 16:31:29 +00:00
import android.os.Build;
2022-10-01 07:51:16 +00:00
import android.text.Editable;
import android.text.Spanned;
import android.text.TextPaint;
2022-10-01 09:20:49 +00:00
import android.text.TextUtils;
2022-10-01 07:51:16 +00:00
import android.text.style.SuggestionSpan;
import android.widget.EditText;
2022-07-09 05:47:29 +00:00
import androidx.preference.PreferenceManager;
2022-04-27 15:18:08 +00:00
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
2022-04-27 19:04:10 +00:00
import java.util.Locale;
2022-04-27 15:18:08 +00:00
import javax.net.ssl.HttpsURLConnection;
2022-05-24 17:28:24 +00:00
public class LanguageTool {
2022-10-01 08:45:17 +00:00
static final String LT_URI = "https://api.languagetool.org/v2/";
2022-11-06 11:54:29 +00:00
static final String LT_URI_PLUS = "https://api.languagetoolplus.com/v2/";
2022-04-27 15:18:08 +00:00
private static final int LT_TIMEOUT = 20; // seconds
2022-07-09 05:47:29 +00:00
static boolean isEnabled(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getBoolean("lt_enabled", false);
}
static boolean isAuto(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean lt_enabled = prefs.getBoolean("lt_enabled", false);
2022-10-01 17:34:50 +00:00
boolean lt_auto = prefs.getBoolean("lt_auto", true);
return (lt_enabled && lt_auto);
}
2022-04-27 15:18:08 +00:00
static List<Suggestion> getSuggestions(Context context, CharSequence text) throws IOException, JSONException {
2022-10-01 09:20:49 +00:00
if (TextUtils.isEmpty(text))
return new ArrayList<>();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean lt_picky = prefs.getBoolean("lt_picky", false);
String lt_user = prefs.getString("lt_user", null);
String lt_key = prefs.getString("lt_key", null);
2022-11-06 11:54:29 +00:00
boolean isPlus = (!TextUtils.isEmpty(lt_user) && !TextUtils.isEmpty(lt_key));
String lt_uri = prefs.getString("lt_uri", isPlus ? LT_URI_PLUS : LT_URI);
2022-04-27 15:18:08 +00:00
// https://languagetool.org/http-api/swagger-ui/#!/default/post_check
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("text", text.toString())
.appendQueryParameter("language", "auto");
2022-04-28 11:23:28 +00:00
2022-09-04 15:51:33 +00:00
// curl -X GET --header 'Accept: application/json' 'https://api.languagetool.org/v2/languages'
2022-04-28 11:23:28 +00:00
JSONArray jlanguages;
2022-04-29 05:42:20 +00:00
try (InputStream is = context.getAssets().open("lt.json")) {
2022-04-28 11:23:28 +00:00
String json = Helper.readStream(is);
jlanguages = new JSONArray(json);
}
2022-09-04 15:51:33 +00:00
String code = null;
Locale locale = Locale.getDefault();
2022-04-28 11:23:28 +00:00
for (int i = 0; i < jlanguages.length(); i++) {
JSONObject jlanguage = jlanguages.getJSONObject(i);
String c = jlanguage.optString("longCode");
if (locale.toLanguageTag().equals(c) && c.contains("-")) {
code = c;
break;
}
}
if (code != null)
builder.appendQueryParameter("preferredVariants", code);
2022-09-15 16:18:59 +00:00
if (lt_picky)
builder.appendQueryParameter("level", "picky");
2022-11-06 11:54:29 +00:00
if (isPlus) {
builder.appendQueryParameter("username", lt_user);
builder.appendQueryParameter("apiKey", lt_key);
2022-11-06 11:54:29 +00:00
}
2022-09-15 16:18:59 +00:00
Uri uri = Uri.parse(lt_uri).buildUpon().appendPath("check").build();
String request = builder.build().toString().substring(1);
2022-04-27 15:18:08 +00:00
2022-10-01 08:45:17 +00:00
Log.i("LT locale=" + locale + " uri=" + uri + " request=" + request);
URL url = new URL(uri.toString());
2022-04-27 15:18:08 +00:00
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setReadTimeout(LT_TIMEOUT * 1000);
connection.setConnectTimeout(LT_TIMEOUT * 1000);
ConnectionHelper.setUserAgent(context, connection);
2022-04-27 15:18:08 +00:00
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Length", Integer.toString(request.length()));
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
try {
connection.getOutputStream().write(request.getBytes());
int status = connection.getResponseCode();
if (status != HttpsURLConnection.HTTP_OK) {
String error = "Error " + status + ": " + connection.getResponseMessage();
try {
InputStream is = connection.getErrorStream();
if (is != null)
error += "\n" + Helper.readStream(is);
} catch (Throwable ex) {
Log.w(ex);
}
Log.w("LT " + error);
2022-04-27 15:18:08 +00:00
throw new FileNotFoundException(error);
}
String response = Helper.readStream(connection.getInputStream());
2022-04-27 19:04:10 +00:00
Log.i("LT response=" + response);
2022-04-27 15:18:08 +00:00
List<Suggestion> result = new ArrayList<>();
JSONObject jroot = new JSONObject(response);
JSONArray jmatches = jroot.getJSONArray("matches");
for (int i = 0; i < jmatches.length(); i++) {
JSONObject jmatch = jmatches.getJSONObject(i);
Suggestion suggestion = new Suggestion();
suggestion.title = jmatch.getString("shortMessage");
suggestion.description = jmatch.getString("message");
suggestion.offset = jmatch.getInt("offset");
suggestion.length = jmatch.getInt("length");
JSONArray jreplacements = jmatch.getJSONArray("replacements");
suggestion.replacements = new ArrayList<>();
for (int j = 0; j < jreplacements.length(); j++) {
JSONObject jreplacement = jreplacements.getJSONObject(j);
suggestion.replacements.add(jreplacement.getString("value"));
}
if (suggestion.replacements.size() > 0)
result.add(suggestion);
}
return result;
} finally {
connection.disconnect();
}
}
static void applySuggestions(EditText etBody, int start, int end, List<Suggestion> suggestions) {
2022-10-01 07:51:16 +00:00
Editable edit = etBody.getText();
if (edit == null)
return;
// https://developer.android.com/reference/android/text/style/SuggestionSpan
for (SuggestionSpanEx suggestion : edit.getSpans(start, end, SuggestionSpanEx.class)) {
2022-10-01 10:52:14 +00:00
Log.i("LT removing=" + suggestion);
edit.removeSpan(suggestion);
2022-10-01 07:51:16 +00:00
}
2022-10-01 10:52:14 +00:00
if (suggestions != null)
for (LanguageTool.Suggestion suggestion : suggestions) {
Log.i("LT adding=" + suggestion);
SuggestionSpan span = new SuggestionSpanEx(etBody.getContext(),
suggestion.replacements.toArray(new String[0]),
SuggestionSpan.FLAG_MISSPELLED);
int s = start + suggestion.offset;
int e = s + suggestion.length;
if (s < 0 || s > edit.length() || e < 0 || e > edit.length()) {
Log.w("LT " + s + "..." + e + " length=" + edit.length());
continue;
}
edit.setSpan(span, s, e, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2022-10-01 10:52:14 +00:00
}
2022-10-01 07:51:16 +00:00
}
2022-04-27 15:18:08 +00:00
static class Suggestion {
String title; // shortMessage
String description; // message
int offset;
int length;
List<String> replacements;
@Override
public String toString() {
return title;
}
}
2022-10-01 07:51:16 +00:00
private static class SuggestionSpanEx extends SuggestionSpan {
2022-10-03 16:31:29 +00:00
private final int highlightColor;
private final int dp3;
2022-10-01 07:51:16 +00:00
public SuggestionSpanEx(Context context, String[] suggestions, int flags) {
super(context, suggestions, flags);
2022-10-03 16:31:29 +00:00
highlightColor = Helper.resolveColor(context,
Build.VERSION.SDK_INT < Build.VERSION_CODES.Q
? android.R.attr.textColorHighlight
: android.R.attr.colorError);
dp3 = Helper.dp2pixels(context, 2);
2022-10-01 07:51:16 +00:00
}
@Override
public void updateDrawState(TextPaint tp) {
2022-10-03 16:31:29 +00:00
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
tp.bgColor = highlightColor;
else {
tp.underlineColor = highlightColor;
tp.underlineThickness = dp3;
}
2022-10-01 07:51:16 +00:00
}
}
2022-04-27 15:18:08 +00:00
}