From e5bef782fcac6c55770d4a234a3a2c4beb26fc27 Mon Sep 17 00:00:00 2001 From: faketruth Date: Sun, 19 Aug 2012 00:35:19 +0000 Subject: Android: Finally a log! :D git-svn-id: http://mc-server.googlecode.com/svn/trunk@754 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- res/layout/main.xml | 14 +++---- src/com/mcserver/MCServerActivity.java | 76 ++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 40 deletions(-) diff --git a/res/layout/main.xml b/res/layout/main.xml index c6fe79b09..dd5ee0e8f 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -37,12 +37,10 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/your_ip" /> - - - + + + \ No newline at end of file diff --git a/src/com/mcserver/MCServerActivity.java b/src/com/mcserver/MCServerActivity.java index f21c5f9d0..2bdea0524 100644 --- a/src/com/mcserver/MCServerActivity.java +++ b/src/com/mcserver/MCServerActivity.java @@ -6,6 +6,7 @@ import java.io.InputStreamReader; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; +import java.util.ArrayList; import java.util.Enumeration; import java.util.Timer; import java.util.TimerTask; @@ -16,13 +17,19 @@ import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; +import android.widget.ArrayAdapter; import android.widget.Button; +import android.widget.ListView; import android.widget.TextView; public class MCServerActivity extends Activity { MainThread mThread = null; Thread ServerStatusThread = null; boolean mbExiting = false; + boolean mbEnabledLogging = false; + + ArrayList mLogList = new ArrayList(); + ArrayAdapter mAdapter; /** Called when the activity is first created. */ @Override @@ -35,6 +42,7 @@ public class MCServerActivity extends Activity { ((Button)findViewById(R.id.start_server)).setOnClickListener( new View.OnClickListener() { public void onClick(View v) { + mbEnabledLogging = true; if( mThread == null || mThread.isAlive() == false ) { mThread = new MainThread( (MCServerActivity)v.getContext() ); mThread.start(); @@ -44,11 +52,21 @@ public class MCServerActivity extends Activity { ((Button)findViewById(R.id.stop_server)).setOnClickListener( new View.OnClickListener() { public void onClick(View v) { + mbEnabledLogging = true; NativeCleanUp(); } }); - + + + ListView lv = (ListView)this.findViewById(R.id.listView1); + mAdapter = new ArrayAdapter(this, + R.layout.list_item, + mLogList); + lv.setAdapter(mAdapter); + + + mLogList.add("---- LOG ----"); ServerStatusThread = new Thread( new Runnable() { public void run() { @@ -77,8 +95,6 @@ public class MCServerActivity extends Activity { Thread loggerThread = new Thread( new Runnable() { public void run() { Process process = null; - - SetText( "herpaderpa" ); try { process = Runtime.getRuntime().exec("logcat -v raw *:s MCServer ");// Verbose filter @@ -88,15 +104,16 @@ public class MCServerActivity extends Activity { BufferedReader reader = null; try { - reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + InputStreamReader isr = new InputStreamReader(process.getInputStream()); + reader = new BufferedReader( isr ); String line; while( mbExiting == false ) { line = reader.readLine(); - if( line != null ) + if( mbEnabledLogging == true && line != null ) { - SetText( line ); + AddToLog( line ); } } @@ -181,32 +198,27 @@ public class MCServerActivity extends Activity { - public void AddToLog( String logMessage ) { - - } - - - - - - public void SetText( final String aText ) { - //Log.d("MCServer", "in SetText " + aText); - /* - final MCServerActivity context = this; - this.runOnUiThread(new Runnable() - { - public void run() - { - ((TextView)context.findViewById(R.id.textView1)).setText(aText); - } - } ); - */ - final TextView tv = (TextView)this.findViewById(R.id.textView1); - tv.post(new Runnable() { - public void run() { - tv.setText(aText); - } - }); + public void AddToLog( final String logMessage ) { + final ListView lv = ((ListView)findViewById(R.id.listView1)); + lv.post(new Runnable() { + public void run() { + //final boolean bAutoscroll = lv.getLastVisiblePosition() >= mAdapter.getCount() - 1 ? true : false; + + mLogList.add(logMessage); + while( mLogList.size() > 100 ) // only allow 100 messages in the list, otherwise it might slow the GUI down + { + mLogList.remove(0); + } + mAdapter.notifyDataSetChanged(); + + + // Autoscroll detection is dodgy + //if( bAutoscroll ) + { + lv.setSelection(mAdapter.getCount() - 1); + } + } + }); } -- cgit v1.2.3