From c998dede8e23f9a7ef11fa265a60d64744251b0f Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 1 Oct 2022 10:45:17 +0200 Subject: [PATCH] Added custom LanguageTool URI option --- .../faircode/email/FragmentOptionsMisc.java | 45 +++++++++++++++---- .../java/eu/faircode/email/LanguageTool.java | 10 +++-- .../main/res/layout/fragment_options_misc.xml | 14 +++++- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java index c9ee476a1b..d2feb6ec4a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java @@ -122,9 +122,10 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc private TextView tvFtsPro; private Spinner spLanguage; private SwitchCompat swLanguageTool; + private TextView tvLanguageToolPrivacy; private SwitchCompat swLanguageToolAuto; private SwitchCompat swLanguageToolPicky; - private TextView tvLanguageToolPrivacy; + private EditText etLanguageTool; private ImageButton ibLanguageTool; private SwitchCompat swDeepL; private TextView tvDeepLPrivacy; @@ -243,7 +244,11 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc private final static String[] RESET_OPTIONS = new String[]{ "sort_answers", "shortcuts", "fts", "classification", "class_min_probability", "class_min_difference", - "language", "lt_enabled", "lt_auto", "lt_picky", "deepl_enabled", "vt_enabled", "vt_apikey", "send_enabled", "send_host", + "language", + "lt_enabled", "lt_auto", "lt_picky", "lt_uri", + "deepl_enabled", + "vt_enabled", "vt_apikey", + "send_enabled", "send_host", "updates", "weekly", "show_changelog", "crash_reports", "cleanup_attachments", "watchdog", "experiments", "main_log", "main_log_memory", "protocol", "log_level", "debug", "leak_canary", @@ -328,9 +333,10 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc tvFtsPro = view.findViewById(R.id.tvFtsPro); spLanguage = view.findViewById(R.id.spLanguage); swLanguageTool = view.findViewById(R.id.swLanguageTool); + tvLanguageToolPrivacy = view.findViewById(R.id.tvLanguageToolPrivacy); swLanguageToolAuto = view.findViewById(R.id.swLanguageToolAuto); swLanguageToolPicky = view.findViewById(R.id.swLanguageToolPicky); - tvLanguageToolPrivacy = view.findViewById(R.id.tvLanguageToolPrivacy); + etLanguageTool = view.findViewById(R.id.etLanguageTool); ibLanguageTool = view.findViewById(R.id.ibLanguageTool); swDeepL = view.findViewById(R.id.swDeepL); tvDeepLPrivacy = view.findViewById(R.id.tvDeepLPrivacy); @@ -642,6 +648,14 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc } }); + tvLanguageToolPrivacy.getPaint().setUnderlineText(true); + tvLanguageToolPrivacy.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Helper.view(v.getContext(), Uri.parse(Helper.LT_PRIVACY_URI), true); + } + }); + swLanguageToolAuto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { @@ -656,11 +670,25 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc } }); - tvLanguageToolPrivacy.getPaint().setUnderlineText(true); - tvLanguageToolPrivacy.setOnClickListener(new View.OnClickListener() { + etLanguageTool.setHint(LanguageTool.LT_URI); + etLanguageTool.addTextChangedListener(new TextWatcher() { @Override - public void onClick(View v) { - Helper.view(v.getContext(), Uri.parse(Helper.LT_PRIVACY_URI), true); + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + // Do nothing + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + // Do nothing + } + + @Override + public void afterTextChanged(Editable s) { + String apikey = s.toString().trim(); + if (TextUtils.isEmpty(apikey)) + prefs.edit().remove("lt_uri").apply(); + else + prefs.edit().putString("lt_uri", apikey).apply(); } }); @@ -1869,7 +1897,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc if ("last_cleanup".equals(key)) setLastCleanup(prefs.getLong(key, -1)); - if ("vt_apikey".equals(key) || "send_host".equals(key)) + if ("lt_uri".equals(key) || "vt_apikey".equals(key) || "send_host".equals(key)) return; if ("global_keywords".equals(key)) @@ -2020,6 +2048,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc swLanguageToolAuto.setEnabled(swLanguageTool.isChecked()); swLanguageToolPicky.setChecked(prefs.getBoolean("lt_picky", false)); swLanguageToolPicky.setEnabled(swLanguageTool.isChecked()); + etLanguageTool.setText(prefs.getString("lt_uri", null)); swDeepL.setChecked(prefs.getBoolean("deepl_enabled", false)); swVirusTotal.setChecked(prefs.getBoolean("vt_enabled", false)); etVirusTotal.setText(prefs.getString("vt_apikey", null)); diff --git a/app/src/main/java/eu/faircode/email/LanguageTool.java b/app/src/main/java/eu/faircode/email/LanguageTool.java index fe59137588..a8fdfa240c 100644 --- a/app/src/main/java/eu/faircode/email/LanguageTool.java +++ b/app/src/main/java/eu/faircode/email/LanguageTool.java @@ -46,7 +46,7 @@ import java.util.Locale; import javax.net.ssl.HttpsURLConnection; public class LanguageTool { - private static final String LT_URI = "https://api.languagetool.org/v2/"; + static final String LT_URI = "https://api.languagetool.org/v2/"; private static final int LT_TIMEOUT = 20; // seconds static boolean isEnabled(Context context) { @@ -94,9 +94,13 @@ public class LanguageTool { if (lt_picky) request += "&level=picky"; - Log.i("LT locale=" + locale + " request=" + request); + String uri = prefs.getString("lt_uri", LT_URI); + if (!uri.endsWith("/")) + uri += '/'; - URL url = new URL(LT_URI + "check"); + Log.i("LT locale=" + locale + " uri=" + uri + " request=" + request); + + URL url = new URL(uri + "check"); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); diff --git a/app/src/main/res/layout/fragment_options_misc.xml b/app/src/main/res/layout/fragment_options_misc.xml index 918c9a20f8..ef57fd21e7 100644 --- a/app/src/main/res/layout/fragment_options_misc.xml +++ b/app/src/main/res/layout/fragment_options_misc.xml @@ -367,6 +367,18 @@ app:layout_constraintTop_toBottomOf="@id/swLanguageToolAuto" app:switchPadding="12dp" /> + +