2626import com .google .mlkit .nl .translate .TranslatorOptions ;
2727
2828import java .util .ArrayList ;
29+ import java .util .Arrays ;
2930import java .util .Locale ;
3031import java .util .Objects ;
3132import java .util .Timer ;
3233import java .util .TimerTask ;
34+ import java .util .concurrent .ExecutorService ;
35+ import java .util .concurrent .Executors ;
3336
3437public 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