diff --git a/FAQ.md b/FAQ.md index 88095bd418..302b06791d 100644 --- a/FAQ.md +++ b/FAQ.md @@ -3690,9 +3690,7 @@ Unfortunately, it is not possible to hide messages on the email server too. You might want to read the [privacy policy](https://www.deepl.com/privacy/) of DeepL. -This feature requires an internet connection (host name: *api-free.deepl.com*) and is not available in the Play store version. - -The DeepL API Pro plan is currently not supported. +This feature requires an internet connection and is not available in the Play store version.
diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 3b2a5bb165..bfb9ea1b11 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -474,6 +474,11 @@ public class ApplicationEx extends Application } else if (version < 1558) { if (!prefs.contains("button_extra")) editor.putBoolean("button_extra", true); + } else if (version < 1598) { + if (prefs.contains("deepl")) { + String key = prefs.getString("deepl", null); + editor.putString("deepl_key", key).remove("deepl"); + } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !BuildConfig.DEBUG) diff --git a/app/src/main/java/eu/faircode/email/DeepL.java b/app/src/main/java/eu/faircode/email/DeepL.java index b45cca2413..a9b8221317 100644 --- a/app/src/main/java/eu/faircode/email/DeepL.java +++ b/app/src/main/java/eu/faircode/email/DeepL.java @@ -38,7 +38,6 @@ import javax.net.ssl.HttpsURLConnection; public class DeepL { private static final int DEEPL_TIMEOUT = 20; // seconds - private static final String DEEPL_BASE_URI = "https://api-free.deepl.com/v2/"; public static String translate(String text, String target, Context context) throws IOException, JSONException { String request = @@ -46,9 +45,9 @@ public class DeepL { "&target_lang=" + URLEncoder.encode(target, StandardCharsets.UTF_8.name()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - String deepl = prefs.getString("deepl", null); + String key = prefs.getString("deepl_key", null); - URL url = new URL(DEEPL_BASE_URI + "translate?auth_key=" + deepl); + URL url = new URL(getBaseUri(context) + "translate?auth_key=" + key); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); @@ -91,9 +90,9 @@ public class DeepL { public static Integer[] getUsage(Context context) throws IOException, JSONException { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - String deepl = prefs.getString("deepl", null); + String key = prefs.getString("deepl_key", null); - URL url = new URL(DEEPL_BASE_URI + "usage?auth_key=" + deepl); + URL url = new URL(getBaseUri(context) + "usage?auth_key=" + key); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setReadTimeout(DEEPL_TIMEOUT * 1000); connection.setConnectTimeout(DEEPL_TIMEOUT * 1000); @@ -122,4 +121,11 @@ public class DeepL { connection.disconnect(); } } + + + private static String getBaseUri(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + String domain = prefs.getString("deepl_domain", "api-free.deepl.com"); + return "https://" + domain + "/v2/"; + } } diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index f8795aed8b..ce7ea68181 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -1541,7 +1541,7 @@ public class FragmentCompose extends FragmentBase { boolean save_drafts = prefs.getBoolean("save_drafts", true); boolean send_dialog = prefs.getBoolean("send_dialog", true); boolean image_dialog = prefs.getBoolean("image_dialog", true); - String deepl = prefs.getString("deepl", null); + String deepl_key = prefs.getString("deepl_key", null); menu.findItem(R.id.menu_save_drafts).setChecked(save_drafts); menu.findItem(R.id.menu_send_dialog).setChecked(send_dialog); @@ -1550,7 +1550,7 @@ public class FragmentCompose extends FragmentBase { menu.findItem(R.id.menu_compact).setChecked(compact); SubMenu smenu = menu.findItem(R.id.menu_translate).getSubMenu(); for (int i = 1; i < smenu.size(); i++) - smenu.getItem(i).setEnabled(deepl != null); + smenu.getItem(i).setEnabled(deepl_key != null); if (EntityMessage.PGP_SIGNONLY.equals(encrypt) || EntityMessage.SMIME_SIGNONLY.equals(encrypt)) @@ -1982,7 +1982,7 @@ public class FragmentCompose extends FragmentBase { private void onMenuTranslateKey() { FragmentDialogDeepL fragment = new FragmentDialogDeepL(); - fragment.show(getParentFragmentManager(), "deepl"); + fragment.show(getParentFragmentManager(), "deepl:translate"); } private Pair getParagraph() { @@ -6734,10 +6734,12 @@ public class FragmentCompose extends FragmentBase { public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { final Context context = getContext(); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - String key = prefs.getString("deepl", null); + String domain = prefs.getString("deepl_domain", null); + String key = prefs.getString("deepl_key", null); View view = LayoutInflater.from(context).inflate(R.layout.dialog_deepl, null); final ImageButton ibInfo = view.findViewById(R.id.ibInfo); + final EditText etDomain = view.findViewById(R.id.etDomain); final EditText etKey = view.findViewById(R.id.etKey); final TextView tvUsage = view.findViewById(R.id.tvUsage); @@ -6748,6 +6750,7 @@ public class FragmentCompose extends FragmentBase { } }); + etDomain.setText(domain); etKey.setText(key); tvUsage.setVisibility(View.GONE); @@ -6784,11 +6787,21 @@ public class FragmentCompose extends FragmentBase { .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { + String domain = etDomain.getText().toString().trim(); String key = etKey.getText().toString().trim(); + SharedPreferences.Editor editor = prefs.edit(); if (TextUtils.isEmpty(key)) - prefs.edit().remove("deepl").apply(); - else - prefs.edit().putString("deepl", key).apply(); + editor + .remove("deepl_key") + .remove("deepl_domain"); + else { + editor.putString("deepl_key", key); + if (TextUtils.isEmpty(domain)) + editor.remove("deepl_domain"); + else + editor.putString("deepl_domain", domain); + } + editor.apply(); } }) .setNegativeButton(android.R.string.cancel, null) diff --git a/app/src/main/res/layout/dialog_deepl.xml b/app/src/main/res/layout/dialog_deepl.xml index 8dcecaebe6..6fb29ed3d1 100644 --- a/app/src/main/res/layout/dialog_deepl.xml +++ b/app/src/main/res/layout/dialog_deepl.xml @@ -29,6 +29,22 @@ app:layout_constraintTop_toTopOf="@id/tvDeepL" app:srcCompat="@drawable/twotone_info_24" /> + + + + + + app:layout_constraintTop_toBottomOf="@id/etDomain">