Skip to content

Commit a87d904

Browse files
committed
Done some fixes for the release of RTranslator 2.0.2
1 parent f3c4a56 commit a87d904

File tree

14 files changed

+217
-84
lines changed

14 files changed

+217
-84
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ android {
2929
defaultConfig {
3030
applicationId "nie.translator.rtranslator"
3131
targetSdkVersion 32 //32
32-
versionCode 16
33-
versionName '2.0.1'
32+
versionCode 17
33+
versionName '2.0.2'
3434
minSdkVersion 24
3535
externalNativeBuild {
3636
cmake {

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools">
44

5-
<!-- https://developer.android.com/reference/android/speech/tts/TextToSpeech-->
5+
<!--https://developer.android.com/reference/android/speech/tts/TextToSpeech-->
66
<queries>
77
<intent>
88
<action android:name="android.intent.action.TTS_SERVICE" />

app/src/main/java/nie/translator/rtranslator/Global.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
public class Global extends Application implements DefaultLifecycleObserver {
5353
private ArrayList<CustomLocale> languages = new ArrayList<>();
54+
private ArrayList<CustomLocale> ttsLanguages = new ArrayList<>();
5455
private CustomLocale language;
5556
private CustomLocale firstLanguage;
5657
private CustomLocale secondLanguage;
@@ -117,16 +118,16 @@ public void onDestroyed() {
117118
});
118119
}
119120

120-
public void getLanguages(final boolean recycleResult, boolean ignoreTTSError,final GetLocalesListListener responseListener) {
121+
public void getLanguages(final boolean recycleResult, boolean ignoreTTSError, final GetLocalesListListener responseListener) {
121122
if (recycleResult && !languages.isEmpty()) {
122123
responseListener.onSuccess(languages);
123124
} else {
124-
TTS.getSupportedLanguages(this, new TTS.SupportedLanguagesListener() {
125+
TTS.getSupportedLanguages(this, new TTS.SupportedLanguagesListener() { //we load TTS languages to catch eventual TTS errors
125126
@Override
126127
public void onLanguagesListAvailable(ArrayList<CustomLocale> ttsLanguages) {
127128
ArrayList<CustomLocale> translatorLanguages = Translator.getSupportedLanguages(Global.this, Translator.NLLB);
128129
ArrayList<CustomLocale> speechRecognizerLanguages = Recognizer.getSupportedLanguages(Global.this);
129-
//we return only the languages compatible with the speech recognizer, the translator and the tts
130+
//we return only the languages compatible with the speech recognizer and the translator
130131
final ArrayList<CustomLocale> compatibleLanguages = new ArrayList<>();
131132
for (CustomLocale translatorLanguage : translatorLanguages) {
132133
if (CustomLocale.containsLanguage(speechRecognizerLanguages, translatorLanguage)) {
@@ -142,7 +143,7 @@ public void onError(int reason) {
142143
if(ignoreTTSError) {
143144
ArrayList<CustomLocale> translatorLanguages = Translator.getSupportedLanguages(Global.this, Translator.NLLB);
144145
ArrayList<CustomLocale> speechRecognizerLanguages = Recognizer.getSupportedLanguages(Global.this);
145-
//we return only the languages compatible with the speech recognizer, the translator and the tts
146+
//we return only the languages compatible with the speech recognizer and the translator (without loading TTS languages)
146147
final ArrayList<CustomLocale> compatibleLanguages = new ArrayList<>();
147148
for (CustomLocale translatorLanguage : translatorLanguages) {
148149
if (CustomLocale.containsLanguage(speechRecognizerLanguages, translatorLanguage)) {
@@ -159,6 +160,25 @@ public void onError(int reason) {
159160
}
160161
}
161162

163+
public void getTTSLanguages(final boolean recycleResult, final GetLocalesListListener responseListener){
164+
if(recycleResult && !ttsLanguages.isEmpty()){
165+
responseListener.onSuccess(ttsLanguages);
166+
}else{
167+
TTS.getSupportedLanguages(this, new TTS.SupportedLanguagesListener() { //we load TTS languages to catch eventual TTS errors
168+
@Override
169+
public void onLanguagesListAvailable(ArrayList<CustomLocale> ttsLanguages) {
170+
Global.this.ttsLanguages = ttsLanguages;
171+
responseListener.onSuccess(ttsLanguages);
172+
}
173+
174+
@Override
175+
public void onError(int reason) {
176+
responseListener.onSuccess(new ArrayList<>());
177+
}
178+
});
179+
}
180+
}
181+
162182
public Translator getTranslator() {
163183
return translator;
164184
}

app/src/main/java/nie/translator/rtranslator/settings/LanguagePreference.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,21 @@ public void initializeLanguagesList() {
7373
global.getLanguage(false, new Global.GetLocaleListener() {
7474
@Override
7575
public void onSuccess(CustomLocale result) {
76-
if (getSummary() == null || "".equals((String) getSummary())) { // to avoid changing the summary after a language change after the initializeLanguageList () call
77-
setSummary(result.getDisplayName()); // if we have an error of lack of internet we simply don't insert the summary
78-
} else if (summary != null && summary.equals((String) getSummary())) {
79-
setSummary(result.getDisplayName()); // if we have an error of lack of internet we simply don't insert the summary
80-
}
76+
global.getTTSLanguages(false, new Global.GetLocalesListListener() {
77+
@Override
78+
public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {
79+
if (getSummary() == null || "".equals((String) getSummary())) { // to avoid changing the summary after a language change after the initializeLanguageList () call
80+
setSummary(result.getDisplayName(ttsLanguages)); // if we have an error of lack of internet we simply don't insert the summary
81+
} else if (summary != null && summary.equals((String) getSummary())) {
82+
setSummary(result.getDisplayName(ttsLanguages)); // if we have an error of lack of internet we simply don't insert the summary
83+
}
84+
}
85+
86+
@Override
87+
public void onFailure(int[] reasons, long value) {
88+
//never called in this case
89+
}
90+
});
8191
}
8292

8393
@Override
@@ -148,11 +158,21 @@ public void onItemClick(AdapterView<?> parent, View view, final int position, lo
148158
global.getLanguages(true, true, new Global.GetLocalesListListener() {
149159
@Override
150160
public void onSuccess(ArrayList<CustomLocale> result) {
151-
if (result.contains((CustomLocale) listView.getItem(position))) {
152-
global.setLanguage((CustomLocale) listView.getItem(position));
153-
CustomLocale item=(CustomLocale) listView.getItem(position);
154-
setSummary(item.getDisplayName());
155-
}
161+
global.getTTSLanguages(true, new Global.GetLocalesListListener() {
162+
@Override
163+
public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {
164+
if (result.contains((CustomLocale) listView.getItem(position))) {
165+
global.setLanguage((CustomLocale) listView.getItem(position));
166+
CustomLocale item=(CustomLocale) listView.getItem(position);
167+
setSummary(item.getDisplayName(ttsLanguages));
168+
}
169+
}
170+
171+
@Override
172+
public void onFailure(int[] reasons, long value) {
173+
//never called in this case
174+
}
175+
});
156176
}
157177

158178
@Override

app/src/main/java/nie/translator/rtranslator/tools/CustomLocale.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.MissingResourceException;
2626
import java.util.Set;
2727

28+
import nie.translator.rtranslator.Global;
29+
2830
public class CustomLocale implements Comparable<CustomLocale>, Serializable {
2931
@NonNull
3032
private Locale locale;
@@ -153,14 +155,18 @@ public String getDisplayVariant(Locale inLocale) {
153155
return locale.getDisplayVariant(inLocale);
154156
}
155157

156-
public String getDisplayName() {
157-
if (containsLanguage(TTS.ttsLanguages, CustomLocale.getInstance(locale.getLanguage()))) {
158+
public String getDisplayName(ArrayList<CustomLocale> ttsLanguages) {
159+
if (containsLanguage(ttsLanguages, CustomLocale.getInstance(locale.getLanguage()))) {
158160
return locale.getDisplayName();
159161
} else {
160-
return locale.getDisplayName()+" (no TTS)"; // Notice that users cannot use TTS for this language.
162+
return locale.getDisplayName()+" (no TTS)"; // Notice that users cannot use TTS for this language.
161163
}
162164
}
163165

166+
public String getDisplayNameWithoutTTS() {
167+
return locale.getDisplayName();
168+
}
169+
164170
public String getDisplayName(Locale locale) {
165171
return locale.getDisplayName(locale);
166172
/*String displayLanguage;
@@ -179,7 +185,7 @@ public Object clone() {
179185

180186
@Override
181187
public int compareTo(CustomLocale o) {
182-
return getDisplayName().compareTo(((CustomLocale) o).getDisplayName());
188+
return getDisplayNameWithoutTTS().compareTo(((CustomLocale) o).getDisplayNameWithoutTTS());
183189
}
184190

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

196202
public boolean equalsLanguage(CustomLocale locale) {
197203
if (getLanguage() != null && locale != null && locale.getLanguage() != null) {
198-
return getLanguage().equals(locale.getLanguage());
204+
return getISO3Language().equals(locale.getISO3Language());
199205
} else {
200206
return false;
201207
}

app/src/main/java/nie/translator/rtranslator/tools/TTS.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class TTS {
3838
private static Thread getSupportedLanguageThread;
3939
private static ArrayDeque<SupportedLanguagesListener> supportedLanguagesListeners = new ArrayDeque<>();
4040
private static final Object lock = new Object();
41+
private static final ArrayList<CustomLocale> ttsLanguages = new ArrayList<>();
4142

4243

4344
public TTS(Context context, final InitListener listener) {
@@ -181,8 +182,6 @@ private static void notifyGetSupportedLanguagesFailure(final int reasons) {
181182
}
182183
}
183184

184-
public static ArrayList<CustomLocale> ttsLanguages = new ArrayList<>();
185-
186185
private static class GetSupportedLanguageRunnable implements Runnable {
187186
private SupportedLanguagesListener responseListener;
188187
private Context context;
@@ -211,48 +210,48 @@ public void onInit() {
211210
} else {
212211
quality = Voice.QUALITY_NORMAL;
213212
}
214-
boolean foundLanguage = false; // if there is available languages; keep false if our code cannot get its name
213+
boolean foundLanguage = false; // if there is available languages
215214
if (set != null) {
216215
// we filter the languages that have a tts that reflects the quality characteristics we want
217216
for (Voice aSet : set) {
218-
if (aSet.getQuality() >= quality && !aSet.getFeatures().contains("legacySetLanguageVoice")) { //
219-
int i = aSet.getLocale().toString().indexOf("-");
217+
if (aSet.getQuality() >= quality && (qualityLow || !aSet.getFeatures().contains("legacySetLanguageVoice"))) {
220218
CustomLocale language;
221-
if (i != -1) {
219+
if(aSet.getLocale() != null){
222220
language = new CustomLocale(aSet.getLocale()); // Use .getLocale() for google
223221
foundLanguage = true;
224-
} else {
222+
}else{
225223
language = CustomLocale.getInstance(aSet.getName()); // Use .getName() for samsung/huawei (maybe others also)
226224
foundLanguage = true;
227225
}
226+
228227
ttsLanguages.add(language);
229228
}
230229
}
231230
}
232-
if (foundLanguage) { // start TTS if the above lines find at least 1 supported language
231+
if (foundLanguage) { // start TTS if the above lines find at least 1 supported language
233232
mainHandler.post(new Runnable() {
234233
@Override
235234
public void run() {
236235
responseListener.onLanguagesListAvailable(ttsLanguages);
237236
}
238237
});
239238
} else {
240-
onError(ErrorCodes.GOOGLE_TTS_ERROR);
241-
}
242-
tempTts.stop();
243-
tempTts.shutdown();
239+
onError(ErrorCodes.GOOGLE_TTS_ERROR);
244240
}
241+
tempTts.stop();
242+
tempTts.shutdown();
243+
}
245244

246-
@Override
247-
public void onError(final int reason) {
248-
mainHandler.post(new Runnable() {
249-
@Override
250-
public void run() {
251-
responseListener.onError(reason);
252-
}
253-
});
254-
}
255-
});
245+
@Override
246+
public void onError(final int reason) {
247+
mainHandler.post(new Runnable() {
248+
@Override
249+
public void run() {
250+
responseListener.onError(reason);
251+
}
252+
});
253+
}
254+
});
256255
}
257256
}
258257

app/src/main/java/nie/translator/rtranslator/tools/gui/LanguageListAdapter.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import android.widget.BaseAdapter;
2424
import android.widget.TextView;
2525
import java.util.ArrayList;
26+
27+
import nie.translator.rtranslator.Global;
2628
import nie.translator.rtranslator.R;
2729
import nie.translator.rtranslator.tools.CustomLocale;
2830

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

3539
public LanguageListAdapter(Activity activity, ArrayList<CustomLocale> languages, CustomLocale selectedLanguage) {
3640
this.activity = activity;
3741
this.languages = languages;
3842
this.selectedLanguage = selectedLanguage;
3943
notifyDataSetChanged();
4044
inflater = activity.getLayoutInflater();
45+
initializeTTSLanguageList(activity);
46+
}
47+
48+
public LanguageListAdapter(Activity activity, boolean showTTSInfo, ArrayList<CustomLocale> languages, CustomLocale selectedLanguage) {
49+
this.activity = activity;
50+
this.showTTSInfo = showTTSInfo;
51+
this.languages = languages;
52+
this.selectedLanguage = selectedLanguage;
53+
notifyDataSetChanged();
54+
inflater = activity.getLayoutInflater();
55+
initializeTTSLanguageList(activity);
4156
}
4257

4358
@Override
@@ -79,7 +94,26 @@ public View getView(int position, View view, ViewGroup viewGroup) {
7994
} else {
8095
view.findViewById(R.id.isSelected).setVisibility(View.GONE);
8196
}
82-
((TextView) view.findViewById(R.id.languageName)).setText(item.getDisplayName());
97+
if(showTTSInfo){
98+
((TextView) view.findViewById(R.id.languageName)).setText(item.getDisplayName(ttsLanguages));
99+
}else {
100+
((TextView) view.findViewById(R.id.languageName)).setText(item.getDisplayNameWithoutTTS());
101+
}
83102
return view;
84103
}
104+
105+
private void initializeTTSLanguageList(Activity activity){
106+
Global global = (Global) activity.getApplication();
107+
global.getTTSLanguages(true, new Global.GetLocalesListListener() {
108+
@Override
109+
public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {
110+
LanguageListAdapter.this.ttsLanguages = ttsLanguages;
111+
}
112+
113+
@Override
114+
public void onFailure(int[] reasons, long value) {
115+
//never called in this case
116+
}
117+
});
118+
}
85119
}

app/src/main/java/nie/translator/rtranslator/voice_translation/_conversation_mode/_conversation/ConversationService.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import android.os.Looper;
2323
import android.util.Log;
2424
import androidx.annotation.NonNull;
25+
26+
import java.util.ArrayList;
27+
2528
import nie.translator.rtranslator.Global;
2629
import nie.translator.rtranslator.tools.BluetoothHeadsetUtils;
2730
import nie.translator.rtranslator.tools.CustomLocale;
@@ -166,14 +169,24 @@ public void onSuccess(CustomLocale result) {
166169
translator.translateMessage(conversationMessage, result, TRANSLATOR_BEAM_SIZE, new Translator.TranslateMessageListener() {
167170
@Override
168171
public void onTranslatedMessage(ConversationMessage conversationMessage, long messageID, boolean isFinal) {
169-
if(isFinal && CustomLocale.containsLanguage(TTS.ttsLanguages, conversationMessage.getPayload().getLanguage())) { // check if the language can be speak
170-
speak(conversationMessage.getPayload().getText(), conversationMessage.getPayload().getLanguage());
171-
}
172-
message.setText(conversationMessage.getPayload().getText()); // updating the text with the new translated text (and without the language code)
173-
GuiMessage guiMessage = new GuiMessage(message, messageID, false, true);
174-
notifyMessage(guiMessage);
175-
// we save every new message in the exchanged messages so that the fragment can restore them
176-
addOrUpdateMessage(guiMessage);
172+
global.getTTSLanguages(true, new Global.GetLocalesListListener() {
173+
@Override
174+
public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {
175+
if(isFinal && CustomLocale.containsLanguage(ttsLanguages, conversationMessage.getPayload().getLanguage())) { // check if the language can be speak
176+
speak(conversationMessage.getPayload().getText(), conversationMessage.getPayload().getLanguage());
177+
}
178+
message.setText(conversationMessage.getPayload().getText()); // updating the text with the new translated text (and without the language code)
179+
GuiMessage guiMessage = new GuiMessage(message, messageID, false, true);
180+
notifyMessage(guiMessage);
181+
// we save every new message in the exchanged messages so that the fragment can restore them
182+
addOrUpdateMessage(guiMessage);
183+
}
184+
185+
@Override
186+
public void onFailure(int[] reasons, long value) {
187+
//never called in this case
188+
}
189+
});
177190
}
178191

179192
@Override

0 commit comments

Comments
 (0)