Skip to content

Commit

Permalink
Done some fixes for the release of RTranslator 2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
niedev committed Jun 28, 2024
1 parent f3c4a56 commit a87d904
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 84 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ android {
defaultConfig {
applicationId "nie.translator.rtranslator"
targetSdkVersion 32 //32
versionCode 16
versionName '2.0.1'
versionCode 17
versionName '2.0.2'
minSdkVersion 24
externalNativeBuild {
cmake {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<!-- https://developer.android.com/reference/android/speech/tts/TextToSpeech-->
<!--https://developer.android.com/reference/android/speech/tts/TextToSpeech-->
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
Expand Down
28 changes: 24 additions & 4 deletions app/src/main/java/nie/translator/rtranslator/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

public class Global extends Application implements DefaultLifecycleObserver {
private ArrayList<CustomLocale> languages = new ArrayList<>();
private ArrayList<CustomLocale> ttsLanguages = new ArrayList<>();
private CustomLocale language;
private CustomLocale firstLanguage;
private CustomLocale secondLanguage;
Expand Down Expand Up @@ -117,16 +118,16 @@ public void onDestroyed() {
});
}

public void getLanguages(final boolean recycleResult, boolean ignoreTTSError,final GetLocalesListListener responseListener) {
public void getLanguages(final boolean recycleResult, boolean ignoreTTSError, final GetLocalesListListener responseListener) {
if (recycleResult && !languages.isEmpty()) {
responseListener.onSuccess(languages);
} else {
TTS.getSupportedLanguages(this, new TTS.SupportedLanguagesListener() {
TTS.getSupportedLanguages(this, new TTS.SupportedLanguagesListener() { //we load TTS languages to catch eventual TTS errors
@Override
public void onLanguagesListAvailable(ArrayList<CustomLocale> ttsLanguages) {
ArrayList<CustomLocale> translatorLanguages = Translator.getSupportedLanguages(Global.this, Translator.NLLB);
ArrayList<CustomLocale> speechRecognizerLanguages = Recognizer.getSupportedLanguages(Global.this);
//we return only the languages compatible with the speech recognizer, the translator and the tts
//we return only the languages compatible with the speech recognizer and the translator
final ArrayList<CustomLocale> compatibleLanguages = new ArrayList<>();
for (CustomLocale translatorLanguage : translatorLanguages) {
if (CustomLocale.containsLanguage(speechRecognizerLanguages, translatorLanguage)) {
Expand All @@ -142,7 +143,7 @@ public void onError(int reason) {
if(ignoreTTSError) {
ArrayList<CustomLocale> translatorLanguages = Translator.getSupportedLanguages(Global.this, Translator.NLLB);
ArrayList<CustomLocale> speechRecognizerLanguages = Recognizer.getSupportedLanguages(Global.this);
//we return only the languages compatible with the speech recognizer, the translator and the tts
//we return only the languages compatible with the speech recognizer and the translator (without loading TTS languages)
final ArrayList<CustomLocale> compatibleLanguages = new ArrayList<>();
for (CustomLocale translatorLanguage : translatorLanguages) {
if (CustomLocale.containsLanguage(speechRecognizerLanguages, translatorLanguage)) {
Expand All @@ -159,6 +160,25 @@ public void onError(int reason) {
}
}

public void getTTSLanguages(final boolean recycleResult, final GetLocalesListListener responseListener){
if(recycleResult && !ttsLanguages.isEmpty()){
responseListener.onSuccess(ttsLanguages);
}else{
TTS.getSupportedLanguages(this, new TTS.SupportedLanguagesListener() { //we load TTS languages to catch eventual TTS errors
@Override
public void onLanguagesListAvailable(ArrayList<CustomLocale> ttsLanguages) {
Global.this.ttsLanguages = ttsLanguages;
responseListener.onSuccess(ttsLanguages);
}

@Override
public void onError(int reason) {
responseListener.onSuccess(new ArrayList<>());
}
});
}
}

public Translator getTranslator() {
return translator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,21 @@ public void initializeLanguagesList() {
global.getLanguage(false, new Global.GetLocaleListener() {
@Override
public void onSuccess(CustomLocale result) {
if (getSummary() == null || "".equals((String) getSummary())) { // to avoid changing the summary after a language change after the initializeLanguageList () call
setSummary(result.getDisplayName()); // if we have an error of lack of internet we simply don't insert the summary
} else if (summary != null && summary.equals((String) getSummary())) {
setSummary(result.getDisplayName()); // if we have an error of lack of internet we simply don't insert the summary
}
global.getTTSLanguages(false, new Global.GetLocalesListListener() {
@Override
public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {
if (getSummary() == null || "".equals((String) getSummary())) { // to avoid changing the summary after a language change after the initializeLanguageList () call
setSummary(result.getDisplayName(ttsLanguages)); // if we have an error of lack of internet we simply don't insert the summary
} else if (summary != null && summary.equals((String) getSummary())) {
setSummary(result.getDisplayName(ttsLanguages)); // if we have an error of lack of internet we simply don't insert the summary
}
}

@Override
public void onFailure(int[] reasons, long value) {
//never called in this case
}
});
}

@Override
Expand Down Expand Up @@ -148,11 +158,21 @@ public void onItemClick(AdapterView<?> parent, View view, final int position, lo
global.getLanguages(true, true, new Global.GetLocalesListListener() {
@Override
public void onSuccess(ArrayList<CustomLocale> result) {
if (result.contains((CustomLocale) listView.getItem(position))) {
global.setLanguage((CustomLocale) listView.getItem(position));
CustomLocale item=(CustomLocale) listView.getItem(position);
setSummary(item.getDisplayName());
}
global.getTTSLanguages(true, new Global.GetLocalesListListener() {
@Override
public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {
if (result.contains((CustomLocale) listView.getItem(position))) {
global.setLanguage((CustomLocale) listView.getItem(position));
CustomLocale item=(CustomLocale) listView.getItem(position);
setSummary(item.getDisplayName(ttsLanguages));
}
}

@Override
public void onFailure(int[] reasons, long value) {
//never called in this case
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.MissingResourceException;
import java.util.Set;

import nie.translator.rtranslator.Global;

public class CustomLocale implements Comparable<CustomLocale>, Serializable {
@NonNull
private Locale locale;
Expand Down Expand Up @@ -153,14 +155,18 @@ public String getDisplayVariant(Locale inLocale) {
return locale.getDisplayVariant(inLocale);
}

public String getDisplayName() {
if (containsLanguage(TTS.ttsLanguages, CustomLocale.getInstance(locale.getLanguage()))) {
public String getDisplayName(ArrayList<CustomLocale> ttsLanguages) {
if (containsLanguage(ttsLanguages, CustomLocale.getInstance(locale.getLanguage()))) {
return locale.getDisplayName();
} else {
return locale.getDisplayName()+" (no TTS)"; // Notice that users cannot use TTS for this language.
return locale.getDisplayName()+" (no TTS)"; // Notice that users cannot use TTS for this language.
}
}

public String getDisplayNameWithoutTTS() {
return locale.getDisplayName();
}

public String getDisplayName(Locale locale) {
return locale.getDisplayName(locale);
/*String displayLanguage;
Expand All @@ -179,7 +185,7 @@ public Object clone() {

@Override
public int compareTo(CustomLocale o) {
return getDisplayName().compareTo(((CustomLocale) o).getDisplayName());
return getDisplayNameWithoutTTS().compareTo(((CustomLocale) o).getDisplayNameWithoutTTS());
}

@Override
Expand All @@ -195,7 +201,7 @@ public boolean equals(Object obj) {

public boolean equalsLanguage(CustomLocale locale) {
if (getLanguage() != null && locale != null && locale.getLanguage() != null) {
return getLanguage().equals(locale.getLanguage());
return getISO3Language().equals(locale.getISO3Language());
} else {
return false;
}
Expand Down
43 changes: 21 additions & 22 deletions app/src/main/java/nie/translator/rtranslator/tools/TTS.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class TTS {
private static Thread getSupportedLanguageThread;
private static ArrayDeque<SupportedLanguagesListener> supportedLanguagesListeners = new ArrayDeque<>();
private static final Object lock = new Object();
private static final ArrayList<CustomLocale> ttsLanguages = new ArrayList<>();


public TTS(Context context, final InitListener listener) {
Expand Down Expand Up @@ -181,8 +182,6 @@ private static void notifyGetSupportedLanguagesFailure(final int reasons) {
}
}

public static ArrayList<CustomLocale> ttsLanguages = new ArrayList<>();

private static class GetSupportedLanguageRunnable implements Runnable {
private SupportedLanguagesListener responseListener;
private Context context;
Expand Down Expand Up @@ -211,48 +210,48 @@ public void onInit() {
} else {
quality = Voice.QUALITY_NORMAL;
}
boolean foundLanguage = false; // if there is available languages; keep false if our code cannot get its name
boolean foundLanguage = false; // if there is available languages
if (set != null) {
// we filter the languages that have a tts that reflects the quality characteristics we want
for (Voice aSet : set) {
if (aSet.getQuality() >= quality && !aSet.getFeatures().contains("legacySetLanguageVoice")) { //
int i = aSet.getLocale().toString().indexOf("-");
if (aSet.getQuality() >= quality && (qualityLow || !aSet.getFeatures().contains("legacySetLanguageVoice"))) {
CustomLocale language;
if (i != -1) {
if(aSet.getLocale() != null){
language = new CustomLocale(aSet.getLocale()); // Use .getLocale() for google
foundLanguage = true;
} else {
}else{
language = CustomLocale.getInstance(aSet.getName()); // Use .getName() for samsung/huawei (maybe others also)
foundLanguage = true;
}

ttsLanguages.add(language);
}
}
}
if (foundLanguage) { // start TTS if the above lines find at least 1 supported language
if (foundLanguage) { // start TTS if the above lines find at least 1 supported language
mainHandler.post(new Runnable() {
@Override
public void run() {
responseListener.onLanguagesListAvailable(ttsLanguages);
}
});
} else {
onError(ErrorCodes.GOOGLE_TTS_ERROR);
}
tempTts.stop();
tempTts.shutdown();
onError(ErrorCodes.GOOGLE_TTS_ERROR);
}
tempTts.stop();
tempTts.shutdown();
}

@Override
public void onError(final int reason) {
mainHandler.post(new Runnable() {
@Override
public void run() {
responseListener.onError(reason);
}
});
}
});
@Override
public void onError(final int reason) {
mainHandler.post(new Runnable() {
@Override
public void run() {
responseListener.onError(reason);
}
});
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;

import nie.translator.rtranslator.Global;
import nie.translator.rtranslator.R;
import nie.translator.rtranslator.tools.CustomLocale;

Expand All @@ -31,13 +33,26 @@ public class LanguageListAdapter extends BaseAdapter {
private CustomLocale selectedLanguage;
private Activity activity;
private LayoutInflater inflater;
private boolean showTTSInfo = true;
private ArrayList<CustomLocale> ttsLanguages = new ArrayList<>();

public LanguageListAdapter(Activity activity, ArrayList<CustomLocale> languages, CustomLocale selectedLanguage) {
this.activity = activity;
this.languages = languages;
this.selectedLanguage = selectedLanguage;
notifyDataSetChanged();
inflater = activity.getLayoutInflater();
initializeTTSLanguageList(activity);
}

public LanguageListAdapter(Activity activity, boolean showTTSInfo, ArrayList<CustomLocale> languages, CustomLocale selectedLanguage) {
this.activity = activity;
this.showTTSInfo = showTTSInfo;
this.languages = languages;
this.selectedLanguage = selectedLanguage;
notifyDataSetChanged();
inflater = activity.getLayoutInflater();
initializeTTSLanguageList(activity);
}

@Override
Expand Down Expand Up @@ -79,7 +94,26 @@ public View getView(int position, View view, ViewGroup viewGroup) {
} else {
view.findViewById(R.id.isSelected).setVisibility(View.GONE);
}
((TextView) view.findViewById(R.id.languageName)).setText(item.getDisplayName());
if(showTTSInfo){
((TextView) view.findViewById(R.id.languageName)).setText(item.getDisplayName(ttsLanguages));
}else {
((TextView) view.findViewById(R.id.languageName)).setText(item.getDisplayNameWithoutTTS());
}
return view;
}

private void initializeTTSLanguageList(Activity activity){
Global global = (Global) activity.getApplication();
global.getTTSLanguages(true, new Global.GetLocalesListListener() {
@Override
public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {
LanguageListAdapter.this.ttsLanguages = ttsLanguages;
}

@Override
public void onFailure(int[] reasons, long value) {
//never called in this case
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;

import java.util.ArrayList;

import nie.translator.rtranslator.Global;
import nie.translator.rtranslator.tools.BluetoothHeadsetUtils;
import nie.translator.rtranslator.tools.CustomLocale;
Expand Down Expand Up @@ -166,14 +169,24 @@ public void onSuccess(CustomLocale result) {
translator.translateMessage(conversationMessage, result, TRANSLATOR_BEAM_SIZE, new Translator.TranslateMessageListener() {
@Override
public void onTranslatedMessage(ConversationMessage conversationMessage, long messageID, boolean isFinal) {
if(isFinal && CustomLocale.containsLanguage(TTS.ttsLanguages, conversationMessage.getPayload().getLanguage())) { // check if the language can be speak
speak(conversationMessage.getPayload().getText(), conversationMessage.getPayload().getLanguage());
}
message.setText(conversationMessage.getPayload().getText()); // updating the text with the new translated text (and without the language code)
GuiMessage guiMessage = new GuiMessage(message, messageID, false, true);
notifyMessage(guiMessage);
// we save every new message in the exchanged messages so that the fragment can restore them
addOrUpdateMessage(guiMessage);
global.getTTSLanguages(true, new Global.GetLocalesListListener() {
@Override
public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {
if(isFinal && CustomLocale.containsLanguage(ttsLanguages, conversationMessage.getPayload().getLanguage())) { // check if the language can be speak
speak(conversationMessage.getPayload().getText(), conversationMessage.getPayload().getLanguage());
}
message.setText(conversationMessage.getPayload().getText()); // updating the text with the new translated text (and without the language code)
GuiMessage guiMessage = new GuiMessage(message, messageID, false, true);
notifyMessage(guiMessage);
// we save every new message in the exchanged messages so that the fragment can restore them
addOrUpdateMessage(guiMessage);
}

@Override
public void onFailure(int[] reasons, long value) {
//never called in this case
}
});
}

@Override
Expand Down
Loading

0 comments on commit a87d904

Please sign in to comment.