import java.util.*; import java.util.zip.*; import java.util.List; import java.util.regex.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import javax.swing.table.*; import java.io.*; import java.net.*; import java.lang.reflect.*; import java.lang.ref.*; import java.lang.management.*; import java.security.*; import java.security.spec.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import java.math.*; import android.app.Activity; import android.speech.tts.*; import android.app.Activity; import android.speech.tts.*; import android.content.Context; public class main { public static void main(String[] args) throws Exception { androidSayInGerman("hallo Fluggerät"); print("Fertsch :)"); } static volatile StringBuffer local_log = new StringBuffer(); // not redirected static volatile StringBuffer print_log = local_log; // might be redirected, e.g. to main bot // in bytes - will cut to half that static volatile int print_log_max = 1024*1024; static volatile int local_log_max = 100*1024; //static int print_maxLineLength = 0; // 0 = unset static boolean print_silent; // total mute if set static void print() { print(""); } // slightly overblown signature to return original object... static A print(A o) { if (print_silent) return o; String s = String.valueOf(o) + "\n"; // TODO if (print_maxLineLength != 0) StringBuffer loc = local_log; StringBuffer buf = print_log; int loc_max = print_log_max; if (buf != loc && buf != null) { print_append(buf, s, print_log_max); loc_max = local_log_max; } if (loc != null) print_append(loc, s, loc_max); System.out.print(s); return o; } static void print(long l) { print(String.valueOf(l)); } static void print(char c) { print(String.valueOf(c)); } static void print_append(StringBuffer buf, String s, int max) { synchronized(buf) { buf.append(s); max /= 2; if (buf.length() > max) try { int newLength = max/2; int ofs = buf.length()-newLength; String newString = buf.substring(ofs); buf.setLength(0); buf.append("[...] ").append(newString); } catch (Exception e) { buf.setLength(0); } } } static void androidSayInGerman(String text) { androidSay(text, Locale.GERMAN); } static boolean androidSay_keepEngine = false; // locale = language, e.g. Locale.GERMAN static void androidSay(final String text, final Locale locale) { if (androidSay_keepEngine) { androidSay_keepEngine(text, locale); return; } final Flag flag = new Flag(); final TextToSpeech[] tts = new TextToSpeech[1]; tts[0] = new TextToSpeech(androidContext(), new TextToSpeech.OnInitListener() { public void onInit(int status) { try { if (status != TextToSpeech.SUCCESS) throw fail("Speech engine initialization failed"); int result = tts[0].setLanguage(locale); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) throw fail("German is not supported"); result = tts[0].setOnUtteranceProgressListener(new UtteranceProgressListener() { public void onDone(String utteranceId) { tts[0].shutdown(); flag.raise(); } public void onError(String utteranceId) { print("onError"); tts[0].shutdown(); flag.raise(); } public void onStart(String utteranceId) { } }); if (result != TextToSpeech.SUCCESS) print("Could not set listener"); print(text); HashMap params = new HashMap(); params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, randomID(8)); tts[0].speak(text, TextToSpeech.QUEUE_FLUSH, params); } catch (Throwable e) { e.printStackTrace(); flag.raise(); } } }); flag.waitUntilUp(); } static Context androidContext() { return getAndroidContext(); } static RuntimeException fail() { throw new RuntimeException("fail"); } static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); } static RuntimeException fail(String msg) { throw new RuntimeException(unnull(msg)); } // disabled for now to shorten some programs /*static RuntimeException fail(S msg, O... args) { throw new RuntimeException(format(msg, args)); }*/ static int randomID_defaultLength = 12; static String randomID(int length) { return makeRandomID(length); } static String randomID() { return randomID(randomID_defaultLength); } static TextToSpeech androidSay_keepEngine_tts; // TODO: better way to sync than synchronizing on main class static synchronized void androidSay_keepEngine(final String text, final Locale locale) { final Flag flag = new Flag(); if (androidSay_keepEngine_tts == null) androidSay_keepEngine_tts = new TextToSpeech(androidContext(), new TextToSpeech.OnInitListener() { public void onInit(int status) { try { if (status != TextToSpeech.SUCCESS) throw fail("Speech engine initialization failed"); androidSay_keepEngine_go(text, locale, flag); } catch (Throwable e) { e.printStackTrace(); flag.raise(); } } }); else androidSay_keepEngine_go(text, locale, flag); flag.waitUntilUp(); } static void androidSay_keepEngine_go(String text, Locale locale, final Flag flag) { int result = androidSay_keepEngine_tts.setLanguage(locale); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) throw fail("German is not supported"); /*if (empty(text)) { // Empty text = Initialize engine only flag.raise(); ret; }*/ result = androidSay_keepEngine_tts.setOnUtteranceProgressListener(new UtteranceProgressListener() { public void onDone(String utteranceId) { flag.raise(); } public void onError(String utteranceId) { print("onError"); flag.raise(); } public void onStart(String utteranceId) { } }); if (result != TextToSpeech.SUCCESS) print("Could not set listener"); print("[speech output] " + text); HashMap params = new HashMap(); params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, randomID(8)); androidSay_keepEngine_tts.speak(text, TextToSpeech.QUEUE_FLUSH, params); } static String unnull(String s) { return s == null ? "" : s; } static List unnull(List l) { return l == null ? emptyList() : l; } static Iterable unnull(Iterable i) { return i == null ? emptyList() : i; } static Object[] unnull(Object[] a) { return a == null ? new Object[0] : a; } static String makeRandomID(int length) { Random random = new Random(); char[] id = new char[length]; for (int i = 0; i < id.length; i++) id[i] = (char) ((int) 'a' + random.nextInt(26)); return new String(id); } static Context getAndroidContext() { return (Context) getAndroidContext_gen(); } static List emptyList() { return new ArrayList(); //ret Collections.emptyList(); } static Object androidContext; static Object getAndroidContext_gen() { return androidContext; } /** this class is fully thread-safe */ static class Flag { private boolean up; /** returns true if flag was down before */ public synchronized boolean raise() { return doRaise(); } private synchronized boolean doRaise() { if (!up) { up = true; notifyAll(); return true; } else return false; } public synchronized void waitUntilUp() { if (!up) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void waitUntilUp(long timeout) { if (!up) { try { wait(timeout); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized boolean isUp() { return up; } public String toString() { return isUp() ? "up" : "down"; } // currently does a semi-active wait with latency = 50 ms public void waitForThisOr(Flag otherFlag) { try { while (!isUp() && !otherFlag.isUp()) Thread.sleep(50); } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} } }