DeepL: API domain

This commit is contained in:
M66B 2021-05-19 18:15:48 +02:00
parent 1ee3fece7f
commit 6b60a2e52c
5 changed files with 54 additions and 16 deletions

4
FAQ.md
View File

@ -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.
<br />

View File

@ -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)

View File

@ -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/";
}
}

View File

@ -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<Integer, Integer> 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)

View File

@ -29,6 +29,22 @@
app:layout_constraintTop_toTopOf="@id/tvDeepL"
app:srcCompat="@drawable/twotone_info_24" />
<eu.faircode.email.EditTextPlain
android:id="@+id/etDomain"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="api-free.deepl.com"
android:imeOptions="actionDone"
android:inputType="text"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvDeepL">
<requestFocus />
</eu.faircode.email.EditTextPlain>
<eu.faircode.email.EditTextPlain
android:id="@+id/etKey"
android:layout_width="0dp"
@ -40,7 +56,7 @@
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvDeepL">
app:layout_constraintTop_toBottomOf="@id/etDomain">
<requestFocus />
</eu.faircode.email.EditTextPlain>