Translate paragraphs on demand

pull/199/head
M66B 4 years ago
parent 452c02dde8
commit 52d1d26b60

@ -70,6 +70,7 @@ import android.text.style.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.QuoteSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.util.Pair;
import android.util.TypedValue;
@ -6655,14 +6656,12 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Context context = getContext();
final View view = LayoutInflater.from(context).inflate(R.layout.dialog_translate, null);
final TextView tvTranslated = view.findViewById(R.id.tvTranslated);
final TextView tvLanguage = view.findViewById(R.id.tvLanguage);
final TextView tvText = view.findViewById(R.id.tvText);
final ContentLoadingProgressBar pbWait = view.findViewById(R.id.pbWait);
tvTranslated.setText(null);
tvLanguage.setText(null);
tvText.setText(null);
new SimpleTask<DeepL.Translation>() {
new SimpleTask<String>() {
@Override
protected void onPreExecute(Bundle args) {
pbWait.setVisibility(View.VISIBLE);
@ -6674,28 +6673,93 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
@Override
protected DeepL.Translation onExecute(Context context, Bundle args) throws Throwable {
protected String onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
File file = EntityMessage.getFile(context, id);
Document d = JsoupEx.parse(file);
HtmlHelper.truncate(d, MAX_TRANSLATE);
d.select("blockquote").remove();
return HtmlHelper.getText(context, d.html());
}
@Override
protected void onExecuted(Bundle args, String text) {
tvText.setText(text);
tvText.setMovementMethod(new ArrowKeyMovementMethod() {
private boolean running = false;
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP && !running)
translate(widget, buffer, event);
return super.onTouchEvent(widget, buffer, event);
}
private void translate(TextView widget, Spannable buffer, MotionEvent event) {
int off = Helper.getOffset(widget, buffer, event);
int start = off;
while (start > 0 && buffer.charAt(start - 1) != '\n')
start--;
int end = off;
while (end < buffer.length() && buffer.charAt(end) != '\n')
end++;
if (end <= start)
return;
StyleSpan[] spans = buffer.getSpans(start, end, StyleSpan.class);
if (spans != null && spans.length > 0)
return;
Bundle args = new Bundle();
args.putInt("start", start);
args.putInt("end", end);
args.putString("text", buffer.subSequence(start, end).toString());
String text = d.text();
new SimpleTask<DeepL.Translation>() {
@Override
protected void onPreExecute(Bundle args) {
running = true;
pbWait.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bundle args) {
running = false;
pbWait.setVisibility(View.GONE);
}
@Override
protected DeepL.Translation onExecute(Context context, Bundle args) throws Throwable {
String text = args.getString("text");
String language = DeepL.getCurrentLanguage(context);
return DeepL.translate(text, language, context);
}
@Override
protected void onExecuted(Bundle args, DeepL.Translation translation) {
tvTranslated.setText(translation.translated_text);
tvLanguage.setText(getString(R.string.title_from_to,
translation.detected_language, translation.target_language));
int start = args.getInt("start");
int end = args.getInt("end");
SpannableStringBuilder ssb = new SpannableStringBuilder(buffer);
ssb = ssb.replace(start, end, translation.translated_text);
ssb.setSpan(new StyleSpan(Typeface.ITALIC), start, start + translation.translated_text.length(), 0);
tvText.setText(ssb);
}
@Override
protected void onException(Bundle args, Throwable ex) {
tvText.setText(ex.toString());
}
}.execute(FragmentDialogTranslate.this, args, "paragraph:translate");
}
});
}
@Override
protected void onException(Bundle args, Throwable ex) {
tvTranslated.setText(ex.toString());
tvText.setText(ex.toString());
}
}.execute(this, getArguments(), "message:translate");

@ -3,6 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:padding="24dp"
android:scrollbarStyle="outsideOverlay">
@ -21,10 +22,10 @@
app:layout_constraintTop_toTopOf="parent" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvTranslated"
android:id="@+id/tvText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginTop="12dp"
android:minHeight="60dp"
android:text="Text"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
@ -32,27 +33,17 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvTitle" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvLanguage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_from_to"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvTranslated" />
<eu.faircode.email.ContentLoadingProgressBar
android:id="@+id/pbWait"
style="@style/Base.Widget.AppCompat.ProgressBar"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:indeterminate="true"
app:layout_constraintBottom_toBottomOf="@id/tvTranslated"
app:layout_constraintEnd_toEndOf="@id/tvTranslated"
app:layout_constraintStart_toStartOf="@id/tvTranslated"
app:layout_constraintTop_toTopOf="@id/tvTranslated" />
app:layout_constraintBottom_toBottomOf="@id/tvText"
app:layout_constraintEnd_toEndOf="@id/tvText"
app:layout_constraintStart_toStartOf="@id/tvText"
app:layout_constraintTop_toTopOf="@id/tvText"
app:show_delay="0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</eu.faircode.email.ScrollViewEx>
Loading…
Cancel
Save