FairEmail/app/src/main/java/eu/faircode/email/DeepL.java

446 lines
17 KiB
Java
Raw Normal View History

2021-05-19 05:22:36 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 by Marcel Bokhorst (M66B)
2021-05-19 05:22:36 +00:00
*/
2021-05-24 06:51:13 +00:00
import android.app.Dialog;
2021-05-19 05:22:36 +00:00
import android.content.Context;
2021-05-24 06:51:13 +00:00
import android.content.DialogInterface;
2021-05-19 05:22:36 +00:00
import android.content.SharedPreferences;
2021-06-27 15:26:53 +00:00
import android.content.res.Resources;
2021-06-29 15:47:57 +00:00
import android.graphics.Paint;
import android.net.Uri;
2021-05-24 06:51:13 +00:00
import android.os.Bundle;
import android.text.Editable;
2021-05-23 06:26:31 +00:00
import android.text.TextUtils;
2021-05-24 06:51:13 +00:00
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
2021-05-19 05:22:36 +00:00
import androidx.preference.PreferenceManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.FileNotFoundException;
import java.io.IOException;
2021-05-21 09:45:39 +00:00
import java.io.InputStream;
2021-05-19 05:22:36 +00:00
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
2021-05-21 09:45:39 +00:00
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
2022-02-03 11:47:52 +00:00
import java.util.Objects;
2021-05-19 05:22:36 +00:00
import javax.net.ssl.HttpsURLConnection;
public class DeepL {
2021-05-19 17:29:50 +00:00
// https://www.deepl.com/docs-api/
2021-06-27 12:50:54 +00:00
private static JSONArray jlanguages = null;
2021-05-19 05:22:36 +00:00
private static final int DEEPL_TIMEOUT = 20; // seconds
2021-07-28 11:05:10 +00:00
private static final String PLAN_URI = "https://www.deepl.com/pro-account/plan";
2021-06-29 15:47:57 +00:00
private static final String PRIVACY_URI = "https://www.deepl.com/privacy/";
2021-05-19 05:22:36 +00:00
2021-05-19 17:29:50 +00:00
// curl https://api-free.deepl.com/v2/languages \
2021-06-27 12:50:54 +00:00
// -d auth_key=... \
2021-05-19 17:29:50 +00:00
// -d type=target
2021-05-24 06:51:13 +00:00
public static boolean isAvailable(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2021-06-29 15:12:22 +00:00
return prefs.getBoolean("deepl_enabled", false);
2021-05-24 06:51:13 +00:00
}
public static boolean canTranslate(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String deepl_key = prefs.getString("deepl_key", null);
return !TextUtils.isEmpty(deepl_key);
}
public static List<Language> getTargetLanguages(Context context, boolean favorites) {
2021-06-27 12:50:54 +00:00
try {
ensureLanguages(context);
2021-05-21 09:45:39 +00:00
2021-05-22 17:14:18 +00:00
String pkg = context.getPackageName();
2021-06-27 15:26:53 +00:00
Resources res = context.getResources();
2021-05-21 09:45:39 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2021-05-22 17:14:18 +00:00
List<Language> languages = new ArrayList<>();
2021-05-21 09:45:39 +00:00
Map<String, Integer> frequencies = new HashMap<>();
2021-06-27 12:50:54 +00:00
for (int i = 0; i < jlanguages.length(); i++) {
JSONObject jlanguage = jlanguages.getJSONObject(i);
2021-05-21 09:45:39 +00:00
String name = jlanguage.getString("name");
String target = jlanguage.getString("language");
2022-02-03 11:47:52 +00:00
boolean formality = jlanguage.optBoolean("supports_formality");
2021-05-21 09:45:39 +00:00
Locale locale = Locale.forLanguageTag(target);
if (locale != null)
name = locale.getDisplayName();
int frequency = prefs.getInt("translated_" + target, 0);
2021-05-22 17:14:18 +00:00
String resname = "language_" + target.toLowerCase().replace('-', '_');
2021-06-27 15:26:53 +00:00
int resid = res.getIdentifier(resname, "drawable", pkg);
2021-05-22 17:14:18 +00:00
2022-02-03 11:47:52 +00:00
languages.add(new Language(name, target, formality,
resid == 0 ? null : resid,
favorites && frequency > 0));
2021-05-21 09:45:39 +00:00
frequencies.put(target, frequency);
}
Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
2021-05-22 17:14:18 +00:00
Collections.sort(languages, new Comparator<Language>() {
2021-05-21 09:45:39 +00:00
@Override
2021-05-22 17:14:18 +00:00
public int compare(Language l1, Language l2) {
int freq1 = frequencies.get(l1.target);
int freq2 = frequencies.get(l2.target);
2021-05-21 09:45:39 +00:00
if (freq1 == freq2 || !favorites)
2021-05-22 17:14:18 +00:00
return collator.compare(l1.name, l2.name);
2021-05-21 09:45:39 +00:00
else
return -Integer.compare(freq1, freq2);
}
});
return languages;
} catch (Throwable ex) {
Log.e(ex);
return null;
}
}
2021-06-27 12:50:54 +00:00
private static void ensureLanguages(Context context) throws IOException, JSONException {
if (jlanguages != null)
return;
try (InputStream is = context.getAssets().open("deepl.json")) {
String json = Helper.readStream(is);
jlanguages = new JSONArray(json);
}
}
2021-05-24 06:51:13 +00:00
public static Pair<Integer, Integer> getParagraph(EditText etBody) {
int start = etBody.getSelectionStart();
int end = etBody.getSelectionEnd();
Editable edit = etBody.getText();
if (start < 0 || end < 0)
return null;
if (start > end) {
int tmp = start;
start = end;
end = tmp;
}
// Expand selection at start
while (start > 0 && edit.charAt(start - 1) != '\n')
start--;
if (start == end && end < edit.length())
end++;
// Expand selection at end
while (end > 0 && end < edit.length() && edit.charAt(end - 1) != '\n')
end++;
// Trim start
while (start < edit.length() - 1 && edit.charAt(start) == '\n')
start++;
// Trim end
while (end > 0 && edit.charAt(end - 1) == '\n')
end--;
if (start < end)
return new Pair(start, end);
return null;
}
2021-06-27 12:50:54 +00:00
public static Translation translate(String text, String target, Context context) throws IOException, JSONException {
2022-02-03 11:47:52 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean formality = prefs.getBoolean("deepl_formal", true);
return translate(text, target, formality, context);
}
public static Translation translate(String text, String target, boolean formality, Context context) throws IOException, JSONException {
2021-07-28 11:01:40 +00:00
// https://www.deepl.com/docs-api/translating-text/request/
2021-05-19 05:22:36 +00:00
String request =
"text=" + URLEncoder.encode(text, StandardCharsets.UTF_8.name()) +
"&target_lang=" + URLEncoder.encode(target, StandardCharsets.UTF_8.name());
2022-02-03 11:47:52 +00:00
ensureLanguages(context);
for (int i = 0; i < jlanguages.length(); i++) {
JSONObject jlanguage = jlanguages.getJSONObject(i);
if (Objects.equals(target, jlanguage.getString("language"))) {
boolean supports_formality = jlanguage.optBoolean("supports_formality");
if (supports_formality)
request += "&formality=" + (formality ? "more" : "less");
break;
}
}
2021-05-19 05:22:36 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2021-05-19 16:15:48 +00:00
String key = prefs.getString("deepl_key", null);
2021-05-19 05:22:36 +00:00
2021-05-19 16:15:48 +00:00
URL url = new URL(getBaseUri(context) + "translate?auth_key=" + key);
2021-05-19 05:22:36 +00:00
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setReadTimeout(DEEPL_TIMEOUT * 1000);
connection.setConnectTimeout(DEEPL_TIMEOUT * 1000);
connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context));
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-Length", Integer.toString(request.length()));
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
try {
connection.getOutputStream().write(request.getBytes());
int status = connection.getResponseCode();
if (status != HttpsURLConnection.HTTP_OK) {
2021-05-19 14:34:52 +00:00
String error = "Error " + status + ": " + connection.getResponseMessage();
2021-05-19 05:22:36 +00:00
try {
2021-08-14 07:23:01 +00:00
InputStream is = connection.getErrorStream();
if (is != null)
error += "\n" + Helper.readStream(is);
2021-05-19 05:22:36 +00:00
} catch (Throwable ex) {
Log.w(ex);
}
2021-05-19 14:34:52 +00:00
throw new FileNotFoundException(error);
2021-05-19 05:22:36 +00:00
}
String response = Helper.readStream(connection.getInputStream());
JSONObject jroot = new JSONObject(response);
JSONArray jtranslations = jroot.getJSONArray("translations");
2021-05-19 15:58:09 +00:00
if (jtranslations.length() == 0)
throw new FileNotFoundException();
2021-05-19 05:22:36 +00:00
JSONObject jtranslation = (JSONObject) jtranslations.get(0);
2021-06-27 12:50:54 +00:00
Translation result = new Translation();
result.target_language = target;
result.detected_language = jtranslation.getString("detected_source_language");
result.translated_text = jtranslation.getString("text");
return result;
2021-05-19 15:58:09 +00:00
} finally {
connection.disconnect();
}
}
public static Integer[] getUsage(Context context) throws IOException, JSONException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2021-05-19 16:15:48 +00:00
String key = prefs.getString("deepl_key", null);
2021-05-19 15:58:09 +00:00
2021-07-28 11:01:40 +00:00
// https://www.deepl.com/docs-api/other-functions/monitoring-usage/
2021-05-19 16:15:48 +00:00
URL url = new URL(getBaseUri(context) + "usage?auth_key=" + key);
2021-05-19 15:58:09 +00:00
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setReadTimeout(DEEPL_TIMEOUT * 1000);
connection.setConnectTimeout(DEEPL_TIMEOUT * 1000);
connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context));
connection.connect();
try {
int status = connection.getResponseCode();
if (status != HttpsURLConnection.HTTP_OK) {
String error = "Error " + status + ": " + connection.getResponseMessage();
try {
2021-08-14 07:23:01 +00:00
InputStream is = connection.getErrorStream();
if (is != null)
error += "\n" + Helper.readStream(is);
2021-05-19 15:58:09 +00:00
} catch (Throwable ex) {
Log.w(ex);
}
throw new FileNotFoundException(error);
}
String response = Helper.readStream(connection.getInputStream());
JSONObject jroot = new JSONObject(response);
int count = jroot.getInt("character_count");
int limit = jroot.getInt("character_limit");
return new Integer[]{count, limit};
2021-05-19 05:22:36 +00:00
} finally {
connection.disconnect();
}
}
2021-05-19 16:15:48 +00:00
private static String getBaseUri(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2021-06-13 16:28:44 +00:00
String domain = (prefs.getBoolean("deepl_pro", false)
? "api.deepl.com" : "api-free.deepl.com");
2021-05-19 16:15:48 +00:00
return "https://" + domain + "/v2/";
}
2021-05-22 17:14:18 +00:00
public static class Language {
public String name;
public String target;
2022-02-03 11:47:52 +00:00
public boolean formality;
2021-05-22 17:14:18 +00:00
public Integer icon;
public boolean favorite;
2021-05-22 17:14:18 +00:00
2022-02-03 11:47:52 +00:00
private Language(String name, String target, boolean formality, Integer icon, boolean favorit) {
2021-05-22 17:14:18 +00:00
this.name = name;
this.target = target;
2022-02-03 11:47:52 +00:00
this.formality = formality;
2021-05-22 17:14:18 +00:00
this.icon = icon;
this.favorite = favorit;
2021-05-22 17:14:18 +00:00
}
2021-06-29 14:09:28 +00:00
@Override
public String toString() {
return name;
}
2021-05-22 17:14:18 +00:00
}
2021-05-24 06:51:13 +00:00
2021-06-27 12:50:54 +00:00
public static class Translation {
public String detected_language;
public String target_language;
public String translated_text;
}
2021-05-24 06:51:13 +00:00
public static class FragmentDialogDeepL extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Context context = getContext();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String key = prefs.getString("deepl_key", null);
2021-06-13 16:28:44 +00:00
boolean pro = prefs.getBoolean("deepl_pro", false);
2022-02-03 11:47:52 +00:00
boolean formal = prefs.getBoolean("deepl_formal", true);
2021-05-24 06:51:13 +00:00
boolean small = prefs.getBoolean("deepl_small", false);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_deepl, null);
final ImageButton ibInfo = view.findViewById(R.id.ibInfo);
final EditText etKey = view.findViewById(R.id.etKey);
2021-06-13 16:28:44 +00:00
final CheckBox cbPro = view.findViewById(R.id.cbPro);
2022-02-03 11:47:52 +00:00
final CheckBox cbFormal = view.findViewById(R.id.cbFormal);
final TextView tvFormal = view.findViewById(R.id.tvFormal);
2021-05-24 06:51:13 +00:00
final CheckBox cbSmall = view.findViewById(R.id.cbSmall);
final TextView tvUsage = view.findViewById(R.id.tvUsage);
2021-06-29 15:47:57 +00:00
final TextView tvPrivacy = view.findViewById(R.id.tvPrivacy);
2021-05-24 06:51:13 +00:00
ibInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.viewFAQ(v.getContext(), 167, true);
}
});
2021-07-28 11:05:10 +00:00
tvUsage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Helper.view(view.getContext(), Uri.parse(PLAN_URI), true);
}
});
2021-06-29 15:47:57 +00:00
tvPrivacy.setPaintFlags(tvPrivacy.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
tvPrivacy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.view(v.getContext(), Uri.parse(PRIVACY_URI), false);
}
});
2021-05-24 06:51:13 +00:00
etKey.setText(key);
2021-06-13 16:28:44 +00:00
cbPro.setChecked(pro);
2022-02-03 11:47:52 +00:00
cbFormal.setChecked(formal);
try {
List<String> formals = new ArrayList<>();
for (Language lang : getTargetLanguages(context, false))
if (lang.formality)
formals.add(lang.name);
tvFormal.setText(TextUtils.join(", ", formals));
} catch (Throwable ex) {
tvFormal.setText(Log.formatThrowable(ex, false));
}
2021-05-24 06:51:13 +00:00
cbSmall.setChecked(small);
tvUsage.setVisibility(View.GONE);
2021-06-13 16:28:44 +00:00
if (!TextUtils.isEmpty(key)) {
2021-05-24 06:51:13 +00:00
Bundle args = new Bundle();
args.putString("key", key);
new SimpleTask<Integer[]>() {
@Override
protected Integer[] onExecute(Context context, Bundle args) throws Throwable {
return DeepL.getUsage(context);
}
@Override
protected void onExecuted(Bundle args, Integer[] usage) {
tvUsage.setText(getString(R.string.title_translate_usage,
Helper.humanReadableByteCount(usage[0]),
Helper.humanReadableByteCount(usage[1]),
Math.round(100f * usage[0] / usage[1])));
tvUsage.setVisibility(View.VISIBLE);
}
@Override
protected void onException(Bundle args, Throwable ex) {
2021-06-27 12:50:54 +00:00
tvUsage.setText(Log.formatThrowable(ex, false));
tvUsage.setVisibility(View.VISIBLE);
2021-05-24 06:51:13 +00:00
}
}.execute(this, new Bundle(), "deepl:usage");
}
return new AlertDialog.Builder(context)
.setView(view)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String key = etKey.getText().toString().trim();
SharedPreferences.Editor editor = prefs.edit();
if (TextUtils.isEmpty(key))
2021-06-13 16:28:44 +00:00
editor.remove("deepl_key");
else
2021-05-24 06:51:13 +00:00
editor.putString("deepl_key", key);
2021-06-13 16:28:44 +00:00
editor.putBoolean("deepl_pro", cbPro.isChecked());
2022-02-03 11:47:52 +00:00
editor.putBoolean("deepl_formal", cbFormal.isChecked());
2021-05-24 06:51:13 +00:00
editor.putBoolean("deepl_small", cbSmall.isChecked());
editor.apply();
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
}
2021-05-19 05:22:36 +00:00
}