LanguageTool: prevent sending links and email addresses

This commit is contained in:
M66B 2022-11-13 14:57:56 +01:00
parent 2c2e8b2407
commit 0bb0e3c6c1
1 changed files with 57 additions and 18 deletions

View File

@ -33,6 +33,7 @@ import android.text.style.SuggestionSpan;
import android.util.Pair; import android.util.Pair;
import android.widget.EditText; import android.widget.EditText;
import androidx.core.util.PatternsCompat;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import org.json.JSONArray; import org.json.JSONArray;
@ -46,6 +47,8 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
@ -102,31 +105,67 @@ public class LanguageTool {
} }
static List<Suggestion> getSuggestions(Context context, CharSequence text) throws IOException, JSONException { static List<Suggestion> getSuggestions(Context context, CharSequence text) throws IOException, JSONException {
if (isPremium(context)) { if (isPremium(context))
// Check per paragraph, so the language is detected by paragraph try {
List<Pair<Integer, Integer>> ranges = new ArrayList<>(); List<Pair<Integer, Integer>> ranges = new ArrayList<>();
int start = 0;
int end = start; Pattern pattern = Pattern.compile("(" + Helper.EMAIL_ADDRESS + ")" +
int len = text.length(); "|(" + PatternsCompat.AUTOLINK_WEB_URL.pattern() + ")");
while (end < len) { Matcher matcher = pattern.matcher(text);
while (end < len && text.charAt(end) != '\n') int index = 0;
end++; boolean links = false;
ranges.add(new Pair<>(start, end)); while (matcher.find()) {
start = end + 1; links = true;
end = start; int start = matcher.start();
} int end = matcher.end();
ranges.addAll(getRanges(index, start, text));
Log.i("LT skipping " + start + "..." + end +
" '" + text.subSequence(start, end).toString().replace('\n', '|') + "'");
index = end;
}
ranges.addAll(getRanges(index, text.length(), text));
if (ranges.size() <= LT_MAX_RANGES) {
List<Suggestion> result = new ArrayList<>();
for (Pair<Integer, Integer> range : ranges) for (Pair<Integer, Integer> range : ranges)
result.addAll(getSuggestions(context, text, range.first, range.second)); Log.i("LT range " + range.first + "..." + range.second +
return result; " '" + text.subSequence(range.first, range.second).toString().replace('\n', '|') + "'");
if (ranges.size() <= LT_MAX_RANGES || links) {
List<Suggestion> result = new ArrayList<>();
for (Pair<Integer, Integer> range : ranges)
result.addAll(getSuggestions(context, text, range.first, range.second));
return result;
}
} catch (Throwable ex) {
if (BuildConfig.DEBUG)
throw ex;
Log.e(ex);
} }
}
return getSuggestions(context, text, 0, text.length()); return getSuggestions(context, text, 0, text.length());
} }
private static List<Pair<Integer, Integer>> getRanges(int from, int to, CharSequence text) {
Log.i("LT ranges " + from + "..." + to +
" '" + text.subSequence(from, to).toString().replace('\n', '|') + "'");
List<Pair<Integer, Integer>> ranges = new ArrayList<>();
int start = from;
int end = start;
while (end < to) {
while (end < to && text.charAt(end) != '\n')
end++;
if (end > start) {
String fragment = text.subSequence(start, end).toString();
if (!TextUtils.isEmpty(fragment.trim()))
ranges.add(new Pair<>(start, end));
}
start = end + 1;
end = start;
}
return ranges;
}
private static List<Suggestion> getSuggestions(Context context, CharSequence text, int start, int end) throws IOException, JSONException { private static List<Suggestion> getSuggestions(Context context, CharSequence text, int start, int end) throws IOException, JSONException {
if (start < 0 || end > text.length() || start == end) if (start < 0 || end > text.length() || start == end)
return new ArrayList<>(); return new ArrayList<>();