import javax.imageio.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.*;
import java.security.NoSuchAlgorithmException;
import java.security.MessageDigest;
import java.lang.management.*;
import java.lang.reflect.*;
import java.net.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.List;
import java.util.zip.*;
import java.util.*;
import android.app.Activity;
import android.speech.tts.*;



public class main {
  static Activity androidContext;
  static TextToSpeech tts;
  static String text = "Hello Android";
  
  public static void main(String[] args) throws Exception {
    if (args.length != 0 && args[0].length() != 0) text = join(" ", args);
    
    tts = new TextToSpeech(androidContext, new TextToSpeech.OnInitListener() {
      public void onInit(int status) {
        try {
        if (status != TextToSpeech.SUCCESS)
          fail("Speech engine initialization failed");

       /* int result = tts.setLanguage(Locale.US);
        if (result == TextToSpeech.LANG_MISSING_DATA
          || result == TextToSpeech.LANG_NOT_SUPPORTED)
          fail("English is not supported");
       */
       
        int result = tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
          @Override
          public void onDone(String utteranceId)
          {
            print("onDone");
            done();
          }

          @Override
          public void onError(String utteranceId) {
            print("onError");
          }

          @Override
          public void onStart(String utteranceId) {
            print("onStart");
          }
        });
        if (result != TextToSpeech.SUCCESS)
          print("Could not set listener");
            
        speak();
        /*Thread _t_0 = new Thread() {
public void run() {
try {

          sleepSeconds(20); // to be improved :) (OnUtteranceListener)
          androidContext.runOnUiThread(new Runnable() {
public void run() { try {

            done();
          
} catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}});
        } catch (Exception _e) {
  throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } }
};
_t_0.start();*/
        } catch (Throwable e) { e.printStackTrace(); }
      }
    });
  }
  
  static void speak() {
    print(text);
    HashMap params = new HashMap();
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "UniqueID");
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, params);
  }
  
  static void done() {
    print("Closing TTS");
    tts.stop();
    tts.shutdown();
    tts = null;
  }

static void print() {
  System.out.println();
}

static void print(Object o) {
  System.out.println(o);
}

static void print(long i) {
  System.out.println(i);
}

  public static String join(String glue, Iterable<String> strings) {
    StringBuilder buf = new StringBuilder();
    Iterator<String> i = strings.iterator();
    if (i.hasNext()) {
      buf.append(i.next());
      while (i.hasNext())
        buf.append(glue).append(i.next());
    }
    return buf.toString();
  }
  
  public static String join(String glue, String[] strings) {
    return join(glue, Arrays.asList(strings));
  }
  
  public static String join(Iterable<String> strings) {
    return join("", strings);
  }
  
  public static String join(String[] strings) {
    return join("", strings);
  }


  static RuntimeException fail() {
    throw new RuntimeException("fail");
  }
  
  static RuntimeException fail(Object msg) {
    throw new RuntimeException(String.valueOf(msg));
  }
}