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.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.widget.*; import android.view.*; import android.view.View; import android.view.KeyEvent; import android.view.inputmethod.*; import android.text.*; public class main { static String ip = "second.tinybrain.de"; static int port = 5000; static boolean function = true; static Activity androidContext; static Lg lg; static volatile DialogIO io; static class Lg { static int maxBufferLength = 2048; Activity context; ScrollView sv; TextView tv; StringBuilder buf = new StringBuilder(); Lg(Activity context) { this.context = context; sv = new ScrollView(context); tv = new TextView(context); tv.setText(buf.toString()); sv.addView(tv); } View getView() { return sv; } void shortenBuffer() { while (buf.length() > maxBufferLength) { String s = buf.toString(); int i = s.indexOf('\n'); if (i < 0) return; buf = new StringBuilder(s.substring(i+1)); } } void print(final String s) { context.runOnUiThread(new Runnable() { public void run() { buf.append(s); shortenBuffer(); tv.setText(buf.toString()); sv.post(new Runnable() { public void run() { // This method works but animates the scrolling // which looks weird on first load sv.fullScroll(View.FOCUS_DOWN); // This method works even better because there are no animations. //sv.scrollTo(0, sv.getBottom()); } }); } }); } void println(String s) { print(s + "\n"); } void println() { println(""); } } public static void main(String[] args) throws Exception { /*if (args.length > 0) ip = args[0]; if (args.length > 1) port = parseInt(args[1]);*/ androidContext.runOnUiThread(new Runnable() { public void run() { try { Activity context = androidContext; LinearLayout ll = new LinearLayout(context); ll.setOrientation(ll.VERTICAL); lg = new Lg(context); lg.tv.setBackgroundColor(0xFFFFFFCC); lg.sv.setBackgroundColor(0xFFFFFFCC); lg.sv.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f)); ll.addView(lg.sv); final EditText et = new EditText(context); et.setInputType(InputType.TYPE_CLASS_TEXT); // Hopefully turns off multiline et.setOnEditorActionListener(new TextView.OnEditorActionListener() { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) { if (function && io != null) { String line = et.getText().toString(); lg.println(line); io.sendLine(line); } //et.selectAll(); androidContext.runOnUiThread(new Runnable() { public void run() { try { //et.selectAll(); et.setText(""); } catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}}); } return false; } }); /*et.setOnFocusChangeListener(new View.OnFocusChangeListener(){ public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) et.selectAll(); } });*/ ll.addView(et); context.setContentView(ll); if (!function) return; Thread _t_0 = new Thread() { public void run() { try { io = talkTo(ip, port); // input while (io.isStillConnected()) { if (io.waitForLine()) { String line = io.readLineNoBlock(); lg.println("> " + line); } } lg.println(); lg.println("[logout]"); } catch (Exception _e) { throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } } }; _t_0.start(); } catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}}); } static abstract class DialogIO { abstract boolean isStillConnected(); abstract String readLineNoBlock(); abstract boolean waitForLine(); abstract void sendLine(String line); abstract void close(); } static DialogIO talkTo(String ip, int port) { try { print("Talking to " + ip + ":" + port); final Socket s = new Socket(ip, port); final Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); final BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8")); return new DialogIO() { String line; boolean buff; boolean isStillConnected() { return !(buff || s.isClosed()); } String readLineNoBlock() { String l = line; line = null; return l; } boolean waitForLine() { try { if (line != null) return true; //print("Readline"); line = in.readLine(); //print("Readline done: " + line); if (line == null) buff = true; return line != null; } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} void sendLine(String line) { try { w.write(line + "\n"); w.flush(); } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} void close() { try { s.close(); } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} }; } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} static void print() { System.out.println(); } static void print(Object o) { System.out.println(o); } static void print(long i) { System.out.println(i); } }