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.*; import android.app.Activity; import android.widget.*; import android.graphics.*; import android.app.Activity; import android.widget.*; import android.graphics.*; import android.view.View; public class main { static Activity androidContext; static boolean showPicture = true; // global switch. if false, you get the normal text display instead of the smiley static List emotions = litlist( "#1001283", "happy", "#1001284", "unhappy", "#1001285", "oh", "#1001286", "bored", "#1001287", "wide mouth"); static int borderColor = 0xFFFFFFFF; static String lastEmotion; static void emo(String emotion) { if (!showPicture) return; int i; for (i = 0; i < emotions.size(); i += 2) if (emotions.get(i+1).equalsIgnoreCase(emotion)) break; if (i >= emotions.size()) { print("Emotion not found: " + emotion); // use the last one from the list as default i -= 2; } lastEmotion = emotions.get(i+1); androidShowFullScreenImage(emotions.get(i), borderColor); } static void setBorderColor(int color) { if (color != borderColor) { borderColor = color; if (lastEmotion != null) emo(lastEmotion); } } static void happy() { emo("happy"); } static void unhappy() { emo("unhappy"); } static void oh() { emo("oh"); } static void bored() { emo("bored"); } static void wideMouth() { emo("wide mouth"); } static void disappear() { if (!showPicture) return; happy(); androidLater(1000, new Runnable() { public void run() { try { androidShowFullScreenColor(0xFFFFFFFF); androidLater(1000, new Runnable() { public void run() { try { System.exit(0); // totally unrecommended but works } catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}}); } catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}}); } // Emoticons public static void main(String[] args) throws Exception { oh(); } static void androidLater(final int delay, final Runnable r) { Thread _t_0 = new Thread() { public void run() { try { sleep(delay); androidContext.runOnUiThread(r); } catch (Exception _e) { throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } } }; _t_0.start(); } static void androidShowFullScreenColor(int color) { ImageView iv = new ImageView(androidContext); iv.setBackgroundColor(color); androidShow(iv); } static void androidShowFullScreenImage(String imageID) { ImageView iv = new ImageView(androidContext); iv.setImageBitmap(BitmapFactory.decodeFile(loadLibrary(imageID).getPath())); androidShow(iv); } static void androidShowFullScreenImage(String imageID, int borderColor) { ImageView iv = new ImageView(androidContext); iv.setBackgroundColor(borderColor); iv.setImageBitmap(BitmapFactory.decodeFile(loadLibrary(imageID).getPath())); androidShow(iv); } static ArrayList litlist(A... a) { return new ArrayList(Arrays.asList(a)); } static void print() { System.out.println(); } static void print(Object o) { System.out.println(o); } static void print(long i) { System.out.println(i); } static void androidShow(final View view) { androidContext.runOnUiThread(new Runnable() { public void run() { try { androidContext.setContentView(view); } catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}}); } static File loadLibrary(String snippetID) { return loadBinarySnippet(snippetID); } static void sleep(long ms) { try { Thread.sleep(ms); } catch (Exception e) { throw new RuntimeException(e); } } static void sleep() { try { synchronized(main.class) { main.class.wait(); } } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} static File loadBinarySnippet(String snippetID) { try { long id = parseSnippetID(snippetID); File f = DiskSnippetCache_getLibrary(id); if (f == null) { byte[] data = loadDataSnippetImpl(snippetID); DiskSnippetCache_putLibrary(id, data); f = DiskSnippetCache_getLibrary(id); } return f; } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} // Data files are immutable, use centralized cache public static File DiskSnippetCache_getLibrary(long snippetID) throws IOException { File file = new File(getGlobalCache(), "data_" + snippetID + ".jar"); return file.exists() ? file : null; } public static void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException { saveBinaryFile(new File(getGlobalCache(), "data_" + snippetID).getPath() + ".jar", data); } static byte[] loadDataSnippetImpl(String snippetID) throws IOException { byte[] data; try { URL url = new URL("http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + parseSnippetID(snippetID) + "&contentType=application/binary"); System.err.println("Loading library: " + url); try { data = loadBinaryPage(url.openConnection()); } catch (IOException e) { url = new URL("http://data.tinybrain.de/blobs/" + parseSnippetID(snippetID)); System.err.println("Loading library: " + url); data = loadBinaryPage(url.openConnection()); } System.err.println("Bytes loaded: " + data.length); } catch (FileNotFoundException e) { throw new IOException("Binary snippet #" + snippetID + " not found or not public"); } return data; } public static long parseSnippetID(String snippetID) { return Long.parseLong(shortenSnippetID(snippetID)); } static File getGlobalCache() { File file = new File(userHome(), ".tinybrain/snippet-cache"); file.mkdirs(); return file; } static byte[] loadBinaryPage(String url) throws IOException { return loadBinaryPage(new URL(url).openConnection()); } public static byte[] loadBinaryPage(URLConnection con) throws IOException { //setHeaders(con); ByteArrayOutputStream buf = new ByteArrayOutputStream(); InputStream inputStream = con.getInputStream(); int n = 0; while (true) { int ch = inputStream.read(); if (ch < 0) break; buf.write(ch); if (++n % 100000 == 0) System.err.println(" " + n + " bytes loaded."); } inputStream.close(); return buf.toByteArray(); } /** writes safely (to temp file, then rename) */ public static void saveBinaryFile(String fileName, byte[] contents) throws IOException { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); fileOutputStream.write(contents); fileOutputStream.close(); if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (!new File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); } static void saveBinaryFile(File fileName, byte[] contents) { try { saveBinaryFile(fileName.getPath(), contents); } catch (IOException e) { throw new RuntimeException(e); } } static String shortenSnippetID(String snippetID) { if (snippetID.startsWith("#")) snippetID = snippetID.substring(1); String httpBlaBla = "http://tinybrain.de/"; if (snippetID.startsWith(httpBlaBla)) snippetID = snippetID.substring(httpBlaBla.length()); return snippetID; } static String _userHome; static String userHome() { if (_userHome == null) { if (isAndroid()) _userHome = "/storage/sdcard0/"; else _userHome = System.getProperty("user.home"); //System.out.println("userHome: " + _userHome); } return _userHome; } static boolean isAndroid() { return System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0; } }