DeepL: API domain

pull/199/head
M66B 4 years ago
parent 1ee3fece7f
commit 6b60a2e52c

@ -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. 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. This feature requires an internet connection and is not available in the Play store version.
The DeepL API Pro plan is currently not supported.
<br /> <br />

@ -474,6 +474,11 @@ public class ApplicationEx extends Application
} else if (version < 1558) { } else if (version < 1558) {
if (!prefs.contains("button_extra")) if (!prefs.contains("button_extra"))
editor.putBoolean("button_extra", true); 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) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !BuildConfig.DEBUG)

@ -38,7 +38,6 @@ import javax.net.ssl.HttpsURLConnection;
public class DeepL { public class DeepL {
private static final int DEEPL_TIMEOUT = 20; // seconds 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 { public static String translate(String text, String target, Context context) throws IOException, JSONException {
String request = String request =
@ -46,9 +45,9 @@ public class DeepL {
"&target_lang=" + URLEncoder.encode(target, StandardCharsets.UTF_8.name()); "&target_lang=" + URLEncoder.encode(target, StandardCharsets.UTF_8.name());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 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(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
connection.setDoOutput(true); connection.setDoOutput(true);
@ -91,9 +90,9 @@ public class DeepL {
public static Integer[] getUsage(Context context) throws IOException, JSONException { public static Integer[] getUsage(Context context) throws IOException, JSONException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 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(); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setReadTimeout(DEEPL_TIMEOUT * 1000); connection.setReadTimeout(DEEPL_TIMEOUT * 1000);
connection.setConnectTimeout(DEEPL_TIMEOUT * 1000); connection.setConnectTimeout(DEEPL_TIMEOUT * 1000);
@ -122,4 +121,11 @@ public class DeepL {
connection.disconnect(); 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/";
}
} }

@ -1541,7 +1541,7 @@ public class FragmentCompose extends FragmentBase {
boolean save_drafts = prefs.getBoolean("save_drafts", true); boolean save_drafts = prefs.getBoolean("save_drafts", true);
boolean send_dialog = prefs.getBoolean("send_dialog", true); boolean send_dialog = prefs.getBoolean("send_dialog", true);
boolean image_dialog = prefs.getBoolean("image_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_save_drafts).setChecked(save_drafts);
menu.findItem(R.id.menu_send_dialog).setChecked(send_dialog); 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); menu.findItem(R.id.menu_compact).setChecked(compact);
SubMenu smenu = menu.findItem(R.id.menu_translate).getSubMenu(); SubMenu smenu = menu.findItem(R.id.menu_translate).getSubMenu();
for (int i = 1; i < smenu.size(); i++) 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) || if (EntityMessage.PGP_SIGNONLY.equals(encrypt) ||
EntityMessage.SMIME_SIGNONLY.equals(encrypt)) EntityMessage.SMIME_SIGNONLY.equals(encrypt))
@ -1982,7 +1982,7 @@ public class FragmentCompose extends FragmentBase {
private void onMenuTranslateKey() { private void onMenuTranslateKey() {
FragmentDialogDeepL fragment = new FragmentDialogDeepL(); FragmentDialogDeepL fragment = new FragmentDialogDeepL();
fragment.show(getParentFragmentManager(), "deepl"); fragment.show(getParentFragmentManager(), "deepl:translate");
} }
private Pair<Integer, Integer> getParagraph() { private Pair<Integer, Integer> getParagraph() {
@ -6734,10 +6734,12 @@ public class FragmentCompose extends FragmentBase {
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Context context = getContext(); final Context context = getContext();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 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); View view = LayoutInflater.from(context).inflate(R.layout.dialog_deepl, null);
final ImageButton ibInfo = view.findViewById(R.id.ibInfo); 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 EditText etKey = view.findViewById(R.id.etKey);
final TextView tvUsage = view.findViewById(R.id.tvUsage); final TextView tvUsage = view.findViewById(R.id.tvUsage);
@ -6748,6 +6750,7 @@ public class FragmentCompose extends FragmentBase {
} }
}); });
etDomain.setText(domain);
etKey.setText(key); etKey.setText(key);
tvUsage.setVisibility(View.GONE); tvUsage.setVisibility(View.GONE);
@ -6784,11 +6787,21 @@ public class FragmentCompose extends FragmentBase {
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
String domain = etDomain.getText().toString().trim();
String key = etKey.getText().toString().trim(); String key = etKey.getText().toString().trim();
SharedPreferences.Editor editor = prefs.edit();
if (TextUtils.isEmpty(key)) if (TextUtils.isEmpty(key))
prefs.edit().remove("deepl").apply(); editor
.remove("deepl_key")
.remove("deepl_domain");
else {
editor.putString("deepl_key", key);
if (TextUtils.isEmpty(domain))
editor.remove("deepl_domain");
else else
prefs.edit().putString("deepl", key).apply(); editor.putString("deepl_domain", domain);
}
editor.apply();
} }
}) })
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)

@ -29,6 +29,22 @@
app:layout_constraintTop_toTopOf="@id/tvDeepL" app:layout_constraintTop_toTopOf="@id/tvDeepL"
app:srcCompat="@drawable/twotone_info_24" /> 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 <eu.faircode.email.EditTextPlain
android:id="@+id/etKey" android:id="@+id/etKey"
android:layout_width="0dp" android:layout_width="0dp"
@ -40,7 +56,7 @@
android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvDeepL"> app:layout_constraintTop_toBottomOf="@id/etDomain">
<requestFocus /> <requestFocus />
</eu.faircode.email.EditTextPlain> </eu.faircode.email.EditTextPlain>

Loading…
Cancel
Save