Skip to content

Commit ef7b393

Browse files
authored
Add files via upload
1 parent db5db21 commit ef7b393

File tree

6 files changed

+277
-160
lines changed

6 files changed

+277
-160
lines changed

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 30
4+
compileSdkVersion 33
55
defaultConfig {
66
applicationId "com.app.livesubtitle"
77
minSdkVersion 23
8-
targetSdkVersion 30
8+
targetSdkVersion 33
99
versionCode 1
10-
versionName "1.0"
10+
versionName "1.0.1"
1111
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1212
}
1313
buildTypes {

app/src/main/java/com/app/livesubtitle/MainActivity.java

Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.widget.ArrayAdapter;
2525
import android.widget.Button;
2626
import android.widget.CheckBox;
27+
import android.widget.CompoundButton;
2728
import android.widget.EditText;
2829
import android.widget.Spinner;
2930
import android.widget.TextView;
@@ -47,6 +48,8 @@ public class MainActivity extends AppCompatActivity {
4748
private static final Integer RecordAudioRequestCode = 1;
4849
private DisplayMetrics display;
4950

51+
@SuppressLint("StaticFieldLeak")
52+
public static CheckBox checkbox_debug_mode;
5053
private TextView textview_src_dialect;
5154
@SuppressLint("StaticFieldLeak")
5255
public static TextView textview_src;
@@ -98,6 +101,7 @@ protected void onCreate(Bundle savedInstanceState) {
98101
setVolumeControlStream(AudioManager.MODE_IN_COMMUNICATION);
99102
AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
100103
am.setSpeakerphoneOn(true);
104+
checkbox_debug_mode = findViewById(R.id.checkbox_debug_mode);
101105
spinner_src_languages = findViewById(R.id.spinner_src_languages);
102106
checkbox_offline_mode = findViewById(R.id.checkbox_offline_mode);
103107
spinner_dst_languages = findViewById(R.id.spinner_dst_languages);
@@ -144,6 +148,113 @@ protected void onCreate(Bundle savedInstanceState) {
144148
startActivity(intent);
145149
}
146150

151+
if(checkbox_debug_mode.isChecked()){
152+
textview_src_dialect.setVisibility(View.VISIBLE);
153+
textview_src.setVisibility(View.VISIBLE);
154+
textview_dst_dialect.setVisibility(View.VISIBLE);
155+
textview_dst.setVisibility(View.VISIBLE);
156+
textview_recognizing.setVisibility(View.VISIBLE);
157+
textview_overlaying.setVisibility(View.VISIBLE);
158+
textview_debug.setVisibility(View.VISIBLE);
159+
textview_debug2.setVisibility(View.VISIBLE);
160+
if (LANGUAGE.SRC_DIALECT != null) {
161+
String lsd = "LANGUAGE.SRC_DIALECT = " + LANGUAGE.SRC_DIALECT;
162+
textview_src_dialect.setText(lsd);
163+
}
164+
else {
165+
textview_src_dialect.setHint("LANGUAGE.SRC_DIALECT");
166+
}
167+
168+
if (LANGUAGE.SRC != null) {
169+
String ls = "LANGUAGE.SRC = " + LANGUAGE.SRC;
170+
textview_src.setText(ls);
171+
}
172+
else {
173+
textview_src.setHint("LANGUAGE.SRC");
174+
}
175+
176+
if (LANGUAGE.DST_DIALECT != null) {
177+
String ldd = "LANGUAGE.DST_DIALECT = " + LANGUAGE.DST_DIALECT;
178+
textview_dst_dialect.setText(ldd);
179+
}
180+
else {
181+
textview_dst_dialect.setHint("LANGUAGE.DST_DIALECT");
182+
}
183+
184+
if (LANGUAGE.DST != null) {
185+
String ld = "LANGUAGE.DST = " + LANGUAGE.DST;
186+
textview_dst.setText(ld);
187+
}
188+
else {
189+
textview_src.setHint("LANGUAGE.SRC");
190+
}
191+
}
192+
193+
else {
194+
textview_src_dialect.setVisibility(View.GONE);
195+
textview_src.setVisibility(View.GONE);
196+
textview_dst_dialect.setVisibility(View.GONE);
197+
textview_dst.setVisibility(View.GONE);
198+
textview_recognizing.setVisibility(View.GONE);
199+
textview_overlaying.setVisibility(View.GONE);
200+
textview_debug.setVisibility(View.GONE);
201+
textview_debug2.setVisibility(View.GONE);
202+
}
203+
204+
checkbox_debug_mode.setOnClickListener(view -> {
205+
if(((CompoundButton) view).isChecked()){
206+
textview_src_dialect.setVisibility(View.VISIBLE);
207+
textview_src.setVisibility(View.VISIBLE);
208+
textview_dst_dialect.setVisibility(View.VISIBLE);
209+
textview_dst.setVisibility(View.VISIBLE);
210+
textview_recognizing.setVisibility(View.VISIBLE);
211+
textview_overlaying.setVisibility(View.VISIBLE);
212+
textview_debug.setVisibility(View.VISIBLE);
213+
textview_debug2.setVisibility(View.VISIBLE);
214+
if (LANGUAGE.SRC_DIALECT != null) {
215+
String lsd = "LANGUAGE.SRC_DIALECT = " + LANGUAGE.SRC_DIALECT;
216+
textview_src_dialect.setText(lsd);
217+
}
218+
else {
219+
textview_src_dialect.setHint("LANGUAGE.SRC_DIALECT");
220+
}
221+
222+
if (LANGUAGE.SRC != null) {
223+
String ls = "LANGUAGE.SRC = " + LANGUAGE.SRC;
224+
textview_src.setText(ls);
225+
}
226+
else {
227+
textview_src.setHint("LANGUAGE.SRC");
228+
}
229+
230+
if (LANGUAGE.DST_DIALECT != null) {
231+
String ldd = "LANGUAGE.DST_DIALECT = " + LANGUAGE.DST_DIALECT;
232+
textview_dst_dialect.setText(ldd);
233+
}
234+
else {
235+
textview_dst_dialect.setHint("LANGUAGE.DST_DIALECT");
236+
}
237+
238+
if (LANGUAGE.DST != null) {
239+
String ld = "LANGUAGE.DST = " + LANGUAGE.DST;
240+
textview_dst.setText(ld);
241+
}
242+
else {
243+
textview_src.setHint("LANGUAGE.SRC");
244+
}
245+
}
246+
else {
247+
textview_src_dialect.setVisibility(View.GONE);
248+
textview_src.setVisibility(View.GONE);
249+
textview_dst_dialect.setVisibility(View.GONE);
250+
textview_dst.setVisibility(View.GONE);
251+
textview_recognizing.setVisibility(View.GONE);
252+
textview_overlaying.setVisibility(View.GONE);
253+
textview_debug.setVisibility(View.GONE);
254+
textview_debug2.setVisibility(View.GONE);
255+
}
256+
});
257+
147258
final Intent ri = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
148259
PackageManager pm = getPackageManager();
149260
boolean isInstalled = isPackageInstalled("com.google.android.googlequicksearchbox", pm);
@@ -548,23 +659,14 @@ else if (!file_en_src_folder.exists() && !file_en_dst_folder.exists() && !file_s
548659
}
549660

550661
private void toast(String message) {
551-
new Handler(Looper.getMainLooper()).post(new Runnable() {
552-
@Override
553-
public void run() {
554-
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
555-
}
556-
});
662+
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show());
557663
}
558664

559665
public void setText(final TextView tv, final String text){
560-
Handler handler = new Handler(Looper.getMainLooper()) {
561-
@Override
562-
public void handleMessage(Message msg) {
666+
new Handler(Looper.getMainLooper()).post(() -> {
563667
// Any UI task, example
564668
tv.setText(text);
565-
}
566-
};
567-
handler.sendEmptyMessage(1);
669+
});
568670
}
569671

570672
}

app/src/main/java/com/app/livesubtitle/VoiceRecognizer.java

Lines changed: 58 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
import com.google.mlkit.nl.translate.TranslatorOptions;
2727

2828
import java.util.ArrayList;
29+
import java.util.Arrays;
2930
import java.util.Locale;
3031
import java.util.Objects;
3132
import java.util.Timer;
3233
import java.util.TimerTask;
34+
import java.util.concurrent.ExecutorService;
35+
import java.util.concurrent.Executors;
3336

3437
public class VoiceRecognizer extends Service {
3538

@@ -84,17 +87,18 @@ public void onCreate() {
8487
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, src_dialect);
8588
//speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS,3600000);
8689
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 5000);
90+
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.google.android.googlequicksearchbox");
8791
//speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, true);
8892

8993
speechRecognizer.setRecognitionListener(new RecognitionListener() {
9094
@Override
9195
public void onReadyForSpeech(Bundle arg0) {
92-
//setText(MainActivity.textview_debug, "onReadyForSpeech");
96+
setText(MainActivity.textview_debug, "onReadyForSpeech");
9397
}
9498

9599
@Override
96100
public void onBeginningOfSpeech() {
97-
//setText(MainActivity.textview_debug, "onBeginningOfSpeech");
101+
setText(MainActivity.textview_debug, "onBeginningOfSpeech");
98102
}
99103

100104
@Override
@@ -104,12 +108,12 @@ public void onRmsChanged(float rmsdB) {
104108

105109
@Override
106110
public void onBufferReceived(byte[] buffer) {
107-
//setText(MainActivity.textview_debug, "onBufferReceived: " + buffer);
111+
setText(MainActivity.textview_debug, "onBufferReceived: " + Arrays.toString(buffer));
108112
}
109113

110114
@Override
111115
public void onEndOfSpeech() {
112-
//setText(MainActivity.textview_debug, "onEndOfSpeech");
116+
setText(MainActivity.textview_debug, "onEndOfSpeech");
113117
if (!RECOGNIZING_STATUS.RECOGNIZING) {
114118
speechRecognizer.stopListening();
115119
if (translator != null) translator.close();
@@ -120,8 +124,8 @@ public void onEndOfSpeech() {
120124

121125
@Override
122126
public void onError(int errorCode) {
123-
//String errorMessage = getErrorText(errorCode);
124-
//setText(MainActivity.textview_debug, "FAILED " + errorMessage);
127+
String errorMessage = getErrorText(errorCode);
128+
setText(MainActivity.textview_debug, "FAILED : " + errorMessage);
125129
if (!RECOGNIZING_STATUS.RECOGNIZING) {
126130
speechRecognizer.stopListening();
127131
if (translator != null) translator.close();
@@ -170,7 +174,7 @@ public void onEvent(int arg0, Bundle arg1) {
170174
//setText(MainActivity.textview_debug, "onEvent");
171175
}
172176

173-
/*public String getErrorText(int errorCode) {
177+
public String getErrorText(int errorCode) {
174178
String message;
175179
switch (errorCode) {
176180
case SpeechRecognizer.ERROR_AUDIO:
@@ -205,7 +209,7 @@ public void onEvent(int arg0, Bundle arg1) {
205209
break;
206210
}
207211
return message;
208-
}*/
212+
}
209213
});
210214
}
211215

@@ -236,9 +240,9 @@ public void onDestroy() {
236240

237241
@SuppressLint("SetTextI18n")
238242
private void get_translation(final String text, String src, String dst) {
239-
Handler handler = new Handler(Looper.getMainLooper()) {
240-
@Override
241-
public void handleMessage(Message msg) {
243+
ExecutorService executor = Executors.newSingleThreadExecutor();
244+
if (RECOGNIZING_STATUS.RECOGNIZING && text != null) {
245+
executor.execute(() -> {
242246
TranslatorOptions options = new TranslatorOptions.Builder()
243247
.setSourceLanguage(src)
244248
.setTargetLanguage(dst)
@@ -249,64 +253,58 @@ public void handleMessage(Message msg) {
249253
translator.downloadModelIfNeeded(conditions)
250254
.addOnSuccessListener(unused -> MLKIT_DICTIONARY.READY = true)
251255
.addOnFailureListener(e -> {});
252-
} else {
253-
String downloaded_status_message = "Dictionary is ready";
254-
MainActivity.textview_debug2.setText(downloaded_status_message);
255-
if (translator != null) translator.translate(text).addOnSuccessListener(s -> {
256-
TRANSLATION_TEXT.STRING = s;
257-
if (RECOGNIZING_STATUS.RECOGNIZING) {
258-
if (TRANSLATION_TEXT.STRING.length() == 0) {
259-
create_overlay_translation_text.overlay_translation_text.setVisibility(View.INVISIBLE);
260-
create_overlay_translation_text.overlay_translation_text_container.setVisibility(View.INVISIBLE);
261-
} else {
262-
create_overlay_translation_text.overlay_translation_text_container.setVisibility(View.VISIBLE);
263-
create_overlay_translation_text.overlay_translation_text_container.setBackgroundColor(Color.TRANSPARENT);
264-
create_overlay_translation_text.overlay_translation_text.setVisibility(View.VISIBLE);
265-
create_overlay_translation_text.overlay_translation_text.setBackgroundColor(Color.TRANSPARENT);
266-
create_overlay_translation_text.overlay_translation_text.setTextIsSelectable(true);
267-
create_overlay_translation_text.overlay_translation_text.setText(TRANSLATION_TEXT.STRING);
268-
create_overlay_translation_text.overlay_translation_text.setSelection(create_overlay_translation_text.overlay_translation_text.getText().length());
269-
Spannable spannableString = new SpannableStringBuilder(TRANSLATION_TEXT.STRING);
270-
spannableString.setSpan(new ForegroundColorSpan(Color.YELLOW),
271-
0,
272-
create_overlay_translation_text.overlay_translation_text.getSelectionEnd(),
273-
0);
274-
spannableString.setSpan(new BackgroundColorSpan(Color.parseColor("#80000000")),
275-
0,
276-
create_overlay_translation_text.overlay_translation_text.getSelectionEnd(),
277-
0);
278-
create_overlay_translation_text.overlay_translation_text.setText(spannableString);
279-
create_overlay_translation_text.overlay_translation_text.setSelection(create_overlay_translation_text.overlay_translation_text.getText().length());
280-
}
281-
} else {
282-
create_overlay_translation_text.overlay_translation_text.setVisibility(View.INVISIBLE);
283-
create_overlay_translation_text.overlay_translation_text_container.setVisibility(View.INVISIBLE);
284-
}
285-
}).addOnFailureListener(e -> {});
286256
}
287-
}
288-
};
289-
handler.sendEmptyMessage(1);
257+
else {
258+
new Handler(Looper.getMainLooper()).post(() -> {
259+
String downloaded_status_message = "Dictionary is ready";
260+
MainActivity.textview_debug2.setText(downloaded_status_message);
261+
if (translator != null)
262+
translator.translate(text).addOnSuccessListener(s -> {
263+
TRANSLATION_TEXT.STRING = s.toLowerCase(Locale.forLanguageTag(LANGUAGE.DST));
264+
if (RECOGNIZING_STATUS.RECOGNIZING) {
265+
if (TRANSLATION_TEXT.STRING.length() == 0) {
266+
create_overlay_translation_text.overlay_translation_text.setVisibility(View.INVISIBLE);
267+
create_overlay_translation_text.overlay_translation_text_container.setVisibility(View.INVISIBLE);
268+
} else {
269+
create_overlay_translation_text.overlay_translation_text_container.setVisibility(View.VISIBLE);
270+
create_overlay_translation_text.overlay_translation_text_container.setBackgroundColor(Color.TRANSPARENT);
271+
create_overlay_translation_text.overlay_translation_text.setVisibility(View.VISIBLE);
272+
create_overlay_translation_text.overlay_translation_text.setBackgroundColor(Color.TRANSPARENT);
273+
create_overlay_translation_text.overlay_translation_text.setTextIsSelectable(true);
274+
create_overlay_translation_text.overlay_translation_text.setText(TRANSLATION_TEXT.STRING);
275+
create_overlay_translation_text.overlay_translation_text.setSelection(create_overlay_translation_text.overlay_translation_text.getText().length());
276+
Spannable spannableString = new SpannableStringBuilder(TRANSLATION_TEXT.STRING);
277+
spannableString.setSpan(new ForegroundColorSpan(Color.YELLOW),
278+
0,
279+
create_overlay_translation_text.overlay_translation_text.getSelectionEnd(),
280+
0);
281+
spannableString.setSpan(new BackgroundColorSpan(Color.parseColor("#80000000")),
282+
0,
283+
create_overlay_translation_text.overlay_translation_text.getSelectionEnd(),
284+
0);
285+
create_overlay_translation_text.overlay_translation_text.setText(spannableString);
286+
create_overlay_translation_text.overlay_translation_text.setSelection(create_overlay_translation_text.overlay_translation_text.getText().length());
287+
}
288+
} else {
289+
create_overlay_translation_text.overlay_translation_text.setVisibility(View.INVISIBLE);
290+
create_overlay_translation_text.overlay_translation_text_container.setVisibility(View.INVISIBLE);
291+
}
292+
}).addOnFailureListener(e -> {});
293+
});
294+
}
295+
});
296+
}
290297
}
291298

292299
/*private void toast(String message) {
293-
new Handler(Looper.getMainLooper()).post(new Runnable() {
294-
@Override
295-
public void run() {
296-
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
297-
}
298-
});
300+
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show());
299301
}*/
300302

301303
public void setText(final TextView tv, final String text){
302-
Handler handler = new Handler(Looper.getMainLooper()) {
303-
@Override
304-
public void handleMessage(Message msg) {
304+
new Handler(Looper.getMainLooper()).post(() -> {
305305
// Any UI task, example
306306
tv.setText(text);
307-
}
308-
};
309-
handler.sendEmptyMessage(1);
307+
});
310308
}
311309

312310
}

0 commit comments

Comments
 (0)