mirror of https://github.com/M66B/FairEmail.git
Added DeepL usage stats
This commit is contained in:
parent
3b82c631db
commit
1ee3fece7f
|
@ -78,8 +78,46 @@ public class DeepL {
|
|||
|
||||
JSONObject jroot = new JSONObject(response);
|
||||
JSONArray jtranslations = jroot.getJSONArray("translations");
|
||||
if (jtranslations.length() == 0)
|
||||
throw new FileNotFoundException();
|
||||
JSONObject jtranslation = (JSONObject) jtranslations.get(0);
|
||||
return jtranslation.getString("text");
|
||||
String detected = jtranslation.getString("detected_source_language");
|
||||
String translated = jtranslation.getString("text");
|
||||
return translated;
|
||||
} finally {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer[] getUsage(Context context) throws IOException, JSONException {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String deepl = prefs.getString("deepl", null);
|
||||
|
||||
URL url = new URL(DEEPL_BASE_URI + "usage?auth_key=" + deepl);
|
||||
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 {
|
||||
error += "\n" + Helper.readStream(connection.getErrorStream());
|
||||
} 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};
|
||||
} finally {
|
||||
connection.disconnect();
|
||||
}
|
||||
|
|
|
@ -2068,7 +2068,7 @@ public class FragmentCompose extends FragmentBase {
|
|||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
Log.unexpectedError(getParentFragmentManager(), ex);
|
||||
Log.unexpectedError(getParentFragmentManager(), ex, false);
|
||||
}
|
||||
}.execute(this, args, "compose:translate");
|
||||
}
|
||||
|
@ -6732,15 +6732,14 @@ public class FragmentCompose extends FragmentBase {
|
|||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
||||
|
||||
final Context context = getContext();
|
||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String key = prefs.getString("deepl", null);
|
||||
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.dialog_deepl, null);
|
||||
final EditText etKey = view.findViewById(R.id.etKey);
|
||||
final ImageButton ibInfo = view.findViewById(R.id.ibInfo);
|
||||
|
||||
etKey.setText(prefs.getString("deepl", null));
|
||||
final EditText etKey = view.findViewById(R.id.etKey);
|
||||
final TextView tvUsage = view.findViewById(R.id.tvUsage);
|
||||
|
||||
ibInfo.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
|
@ -6749,6 +6748,37 @@ public class FragmentCompose extends FragmentBase {
|
|||
}
|
||||
});
|
||||
|
||||
etKey.setText(key);
|
||||
|
||||
tvUsage.setVisibility(View.GONE);
|
||||
|
||||
if (!TextUtils.isEmpty(key)) {
|
||||
Bundle args = new Bundle();
|
||||
args.putString("key", key);
|
||||
|
||||
new SimpleTask<Integer[]>() {
|
||||
@Override
|
||||
protected Integer[] onExecute(Context context, Bundle args) throws Throwable {
|
||||
String key = args.getString("key");
|
||||
return DeepL.getUsage(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onExecuted(Bundle args, Integer[] usage) {
|
||||
tvUsage.setText(
|
||||
Helper.humanReadableByteCount(usage[0]) + "/" +
|
||||
Helper.humanReadableByteCount(usage[1]));
|
||||
tvUsage.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.unexpectedError(getParentFragmentManager(), ex);
|
||||
}
|
||||
}.execute(this, new Bundle(), "deepl:usage");
|
||||
}
|
||||
|
||||
return new AlertDialog.Builder(context)
|
||||
.setView(view)
|
||||
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
||||
|
|
|
@ -44,4 +44,14 @@
|
|||
|
||||
<requestFocus />
|
||||
</eu.faircode.email.EditTextPlain>
|
||||
|
||||
<eu.faircode.email.FixedTextView
|
||||
android:id="@+id/tvUsage"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="12dp"
|
||||
android:text="Usage"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/etKey" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue