!747 import android.app.*; import android.widget.*; import android.view.*; import android.view.View; import android.view.KeyEvent; import android.view.inputmethod.*; import android.text.*; m { static S 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(""); } } p { /*if (args.length > 0) ip = args[0]; if (args.length > 1) port = parseInt(args[1]);*/ androidContext.runOnUiThread(runnable { 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) { S line = et.getText().toString(); lg.println(line); io.sendLine(line); } //et.selectAll(); androidContext.runOnUiThread(runnable { //et.selectAll(); et.setText(""); }); } 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 { io = talkTo(ip, port); // input while (io.isStillConnected()) { if (io.waitForLine()) { S line = io.readLineNoBlock(); lg.println("> " + line); } } lg.println(); lg.println("[logout]"); } }); } }