From 9f28f64e74ceffa21093f6a4dad5b40846e65942 Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 26 Jul 2022 18:46:58 +0200 Subject: [PATCH] Fix issue #279 --- .../android/activities/MediaActivity.java | 108 ++++++++++++++++++ .../client/entities/api/Attachment.java | 1 + .../main/res/layout/activity_media_pager.xml | 36 +++++- 3 files changed, 142 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/activities/MediaActivity.java b/app/src/main/java/app/fedilab/android/activities/MediaActivity.java index 2fe067f2d..4d5f76620 100644 --- a/app/src/main/java/app/fedilab/android/activities/MediaActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/MediaActivity.java @@ -28,6 +28,7 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.text.Html; import android.view.Display; import android.view.Menu; import android.view.MenuItem; @@ -35,6 +36,7 @@ import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.Window; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; @@ -44,6 +46,12 @@ import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager.widget.ViewPager; +import com.github.stom79.mytransl.MyTransL; +import com.github.stom79.mytransl.client.HttpsConnectionException; +import com.github.stom79.mytransl.client.Results; +import com.github.stom79.mytransl.translate.Params; +import com.github.stom79.mytransl.translate.Translate; + import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -137,8 +145,50 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface { handler = new Handler(); if (description != null && description.trim().length() > 0 && description.trim().compareTo("null") != 0) { binding.mediaDescription.setText(description); + binding.translate.setOnClickListener(v -> { + String descriptionToTranslate = attachments.get(mediaPosition - 1).description; + MyTransL.translatorEngine et = MyTransL.translatorEngine.LIBRETRANSLATE; + final MyTransL myTransL = MyTransL.getInstance(et); + myTransL.setObfuscation(true); + Params params = new Params(); + params.setSplit_sentences(false); + params.setFormat(Params.fType.TEXT); + params.setSource_lang("auto"); + myTransL.setLibretranslateDomain("translate.fedilab.app"); + String statusToTranslate; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) + statusToTranslate = Html.fromHtml(descriptionToTranslate, Html.FROM_HTML_MODE_LEGACY).toString(); + else + statusToTranslate = Html.fromHtml(descriptionToTranslate).toString(); + myTransL.translate(statusToTranslate, MyTransL.getLocale(), params, new Results() { + @Override + public void onSuccess(Translate translate) { + if (translate.getTranslatedContent() != null) { + attachments.get(mediaPosition - 1).translation = translate.getTranslatedContent(); + binding.mediaDescriptionTranslated.setText(translate.getTranslatedContent()); + binding.mediaDescriptionTranslated.setVisibility(View.VISIBLE); + binding.mediaDescription.setVisibility(View.GONE); + } else { + Toasty.error(MediaActivity.this, getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show(); + } + } + @Override + public void onFail(HttpsConnectionException httpsConnectionException) { + + } + }); + }); + if (attachments.get(mediaPosition - 1).translation != null) { + binding.mediaDescription.setVisibility(View.GONE); + binding.mediaDescriptionTranslated.setText(attachments.get(mediaPosition - 1).translation); + binding.mediaDescriptionTranslated.setVisibility(View.VISIBLE); + } else { + binding.mediaDescription.setVisibility(View.VISIBLE); + binding.mediaDescriptionTranslated.setVisibility(View.GONE); + } } + binding.mediaViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { public void onPageScrollStateChanged(int state) { } @@ -155,6 +205,48 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface { if (description != null && description.trim().length() > 0 && description.trim().compareTo("null") != 0) { binding.mediaDescription.setText(description); } + binding.translate.setOnClickListener(v -> { + String descriptionToTranslate = attachments.get(position).description; + MyTransL.translatorEngine et = MyTransL.translatorEngine.LIBRETRANSLATE; + final MyTransL myTransL = MyTransL.getInstance(et); + myTransL.setObfuscation(true); + Params params = new Params(); + params.setSplit_sentences(false); + params.setFormat(Params.fType.TEXT); + params.setSource_lang("auto"); + myTransL.setLibretranslateDomain("translate.fedilab.app"); + String statusToTranslate; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) + statusToTranslate = Html.fromHtml(descriptionToTranslate, Html.FROM_HTML_MODE_LEGACY).toString(); + else + statusToTranslate = Html.fromHtml(descriptionToTranslate).toString(); + myTransL.translate(statusToTranslate, MyTransL.getLocale(), params, new Results() { + @Override + public void onSuccess(Translate translate) { + if (translate.getTranslatedContent() != null) { + attachments.get(position).translation = translate.getTranslatedContent(); + binding.mediaDescriptionTranslated.setText(translate.getTranslatedContent()); + binding.mediaDescriptionTranslated.setVisibility(View.VISIBLE); + binding.mediaDescription.setVisibility(View.GONE); + } else { + Toasty.error(MediaActivity.this, getString(R.string.toast_error_translate), Toast.LENGTH_LONG).show(); + } + } + + @Override + public void onFail(HttpsConnectionException httpsConnectionException) { + + } + }); + }); + if (attachments.get(position).translation != null) { + binding.mediaDescription.setVisibility(View.GONE); + binding.mediaDescriptionTranslated.setText(attachments.get(position).translation); + binding.mediaDescriptionTranslated.setVisibility(View.VISIBLE); + } else { + binding.mediaDescription.setVisibility(View.VISIBLE); + binding.mediaDescriptionTranslated.setVisibility(View.GONE); + } } }); @@ -270,7 +362,21 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface { handler = new Handler(); if (description != null && description.trim().length() > 0 && description.trim().compareTo("null") != 0) { binding.mediaDescription.setText(description); + if (attachments.get(mediaPosition - 1).translation != null) { + binding.mediaDescription.setVisibility(View.GONE); + binding.mediaDescriptionTranslated.setText(attachments.get(binding.mediaViewpager.getCurrentItem()).translation); + binding.mediaDescriptionTranslated.setVisibility(View.VISIBLE); + } else { + binding.mediaDescription.setVisibility(View.VISIBLE); + binding.mediaDescriptionTranslated.setVisibility(View.GONE); + } + } else { + binding.translate.setVisibility(View.GONE); } + } else { + binding.translate.setVisibility(View.GONE); + binding.mediaDescriptionTranslated.setVisibility(View.GONE); + binding.mediaDescription.setVisibility(View.GONE); } } break; @@ -323,8 +429,10 @@ public class MediaActivity extends BaseActivity implements OnDownloadInterface { if (!fullscreen) { showSystemUI(); binding.mediaDescription.setVisibility(View.VISIBLE); + binding.translate.setVisibility(View.VISIBLE); } else { binding.mediaDescription.setVisibility(View.GONE); + binding.translate.setVisibility(View.GONE); hideSystemUI(); } } diff --git a/app/src/main/java/app/fedilab/android/client/entities/api/Attachment.java b/app/src/main/java/app/fedilab/android/client/entities/api/Attachment.java index 3cd5e3c08..c4a2b9aba 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/api/Attachment.java +++ b/app/src/main/java/app/fedilab/android/client/entities/api/Attachment.java @@ -49,6 +49,7 @@ public class Attachment implements Serializable { public String peertubeHost = null; public String peertubeId = null; public String focus = null; + public String translation = null; public static class Meta implements Serializable { @SerializedName("focus") diff --git a/app/src/main/res/layout/activity_media_pager.xml b/app/src/main/res/layout/activity_media_pager.xml index 9ac18524c..3083aa765 100644 --- a/app/src/main/res/layout/activity_media_pager.xml +++ b/app/src/main/res/layout/activity_media_pager.xml @@ -40,7 +40,7 @@ - + + + + +