Anonymous avatar Anonymous committed 4cdc343

clearing view && autoscroll after input

Comments (0)

Files changed (2)

res/values/strings.xml

 <resources>
     <string name="app_name">OCaml toplevel on Android</string>
     <string name='copy'>Copy Text</string>
+    <string name='clear'>Clear View</string>
     <string name="about">About</string>
+    <string name="copy_howto">Touch &amp; drag on the text to copy</string>
     <string name='copyright'>Copyright &#169; 2010 by Keigo IMAI\n
 Copyright &#169; 1995-2010 by INRIA\n
 The source code of this program is fully available at:\n

src/jp/co/itpl/ocamlandroid/MainActivity.java

 import android.webkit.WebView;
 import android.widget.EditText;
 import android.widget.TextView;
+import android.widget.Toast;
 
 public class MainActivity extends Activity {
 	public static final int MENU_COPY = 1;
-	public static final int MENU_ABOUT = 2;
+	public static final int MENU_CLEAR = 2;
+	public static final int MENU_ABOUT = 3;
 	public static final String TAG = "ocaml-android";
+	public static final String SCROLL_JS = "setTimeout(function(){window.scrollTo(0,9999);},0);"; // FIXME! 
+	
 	private WebView outView; // we use WebView for output so that we easily get copying-capability
 	volatile PrintWriter out = null;
 
 	
 	private void println(String line) {
 		Log.d(TAG, "console output:"+line);
-		outView.loadUrl("javascript:document.getElementById('output').innerHTML+='"+ jsescape(line) + "'");
+		outView.loadUrl("javascript:document.getElementById('output').innerHTML+='"+ jsescape(line) + "';"
+				+SCROLL_JS);
 	}
 
+	private void clear() {
+		Log.d(TAG, "console clear");
+		outView.loadUrl("javascript:document.getElementById('output').innerHTML='';");
+	}
+	
 	private void console_output_forever(final WebView outView, final InputStream in) {
 		new Thread() {
 			public void run() {
 		EditText edit = (EditText)findViewById(R.id.input);
 		edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
 			public boolean onEditorAction(TextView view, int arg1, KeyEvent arg2) {
-				if(out!=null) {
+				if(out!=null && !"".equals(view.getText().toString().trim())) {
 					String line = view.getText().toString()+"\n";
 					println(line);
 					out.println(line);
 		super.onCreateOptionsMenu(menu);
 
 		menu.add(Menu.NONE, MENU_COPY, Menu.NONE, R.string.copy);
+		menu.add(Menu.NONE, MENU_CLEAR, Menu.NONE, R.string.clear);
 		menu.add(Menu.NONE, MENU_ABOUT, Menu.NONE, R.string.about).setIcon(
 				R.drawable.ic_menu_info_details);
 		return true;
 				KeyEvent shiftPressEvent = new KeyEvent(0, 0,
 						KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
 				shiftPressEvent.dispatch(outView);
+				Toast.makeText(this, R.string.copy_howto, Toast.LENGTH_LONG);
 			} catch (Exception e) {
 				throw new AssertionError(e);
 			}
 			return true;
+		case MENU_CLEAR:
+			clear();
+			return true;
 		case MENU_ABOUT:
 			showAboutDialog();
 			return true;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.