2012-08-15 21:24:11 +00:00
|
|
|
package com.mcserver;
|
|
|
|
|
2012-08-18 21:00:51 +00:00
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.NetworkInterface;
|
|
|
|
import java.net.SocketException;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
|
2012-08-15 21:24:11 +00:00
|
|
|
import android.app.Activity;
|
2012-08-18 21:00:51 +00:00
|
|
|
import android.graphics.Color;
|
2012-08-15 21:24:11 +00:00
|
|
|
import android.os.Bundle;
|
2012-08-17 22:20:35 +00:00
|
|
|
import android.util.Log;
|
2012-08-16 20:28:14 +00:00
|
|
|
import android.view.KeyEvent;
|
2012-08-18 21:00:51 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.TextView;
|
2012-08-15 21:24:11 +00:00
|
|
|
|
|
|
|
public class MCServerActivity extends Activity {
|
2012-08-17 22:20:35 +00:00
|
|
|
MainThread mThread = null;
|
2012-08-18 21:00:51 +00:00
|
|
|
Thread ServerStatusThread = null;
|
2012-08-17 22:20:35 +00:00
|
|
|
|
2012-08-15 21:24:11 +00:00
|
|
|
/** Called when the activity is first created. */
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.main);
|
|
|
|
|
2012-08-18 21:00:51 +00:00
|
|
|
((Button)findViewById(R.id.start_server)).setOnClickListener( new View.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
if( mThread == null || mThread.isAlive() == false ) {
|
|
|
|
mThread = new MainThread( (MCServerActivity)v.getContext() );
|
|
|
|
mThread.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
((Button)findViewById(R.id.stop_server)).setOnClickListener( new View.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
NativeCleanUp();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ServerStatusThread = new Thread( new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
runOnUiThread( new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
UpdateServerStatus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Thread.sleep(1000);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ServerStatusThread.start();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
((TextView)findViewById(R.id.ip_address)).setText("Connect to: " + getLocalIpAddress());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getLocalIpAddress() {
|
|
|
|
try {
|
|
|
|
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
|
|
|
|
NetworkInterface intf = en.nextElement();
|
|
|
|
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
|
|
|
|
InetAddress inetAddress = enumIpAddr.nextElement();
|
|
|
|
if (!inetAddress.isLoopbackAddress()) {
|
|
|
|
return inetAddress.getHostAddress().toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SocketException ex) {
|
|
|
|
Log.e("MCServer", ex.toString());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void UpdateServerStatus()
|
|
|
|
{
|
|
|
|
if( NativeIsServerRunning() ) {
|
|
|
|
((TextView)findViewById(R.id.server_status_text)).setText(R.string.mcserver_is_running);
|
|
|
|
((TextView)findViewById(R.id.server_status_text)).setTextColor(Color.GREEN);
|
|
|
|
((Button)findViewById(R.id.stop_server)).setEnabled(true);
|
|
|
|
((Button)findViewById(R.id.start_server)).setEnabled(false);
|
|
|
|
} else {
|
|
|
|
((TextView)findViewById(R.id.server_status_text)).setText(R.string.mcserver_is_not_running);
|
|
|
|
((TextView)findViewById(R.id.server_status_text)).setTextColor(Color.RED);
|
|
|
|
((Button)findViewById(R.id.stop_server)).setEnabled(false);
|
|
|
|
((Button)findViewById(R.id.start_server)).setEnabled(true);
|
|
|
|
}
|
2012-08-15 21:24:11 +00:00
|
|
|
}
|
|
|
|
|
2012-08-18 21:00:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-17 22:20:35 +00:00
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
2012-08-16 20:28:14 +00:00
|
|
|
if(keyCode==KeyEvent.KEYCODE_BACK)
|
|
|
|
{
|
|
|
|
//android.os.Process.killProcess(android.os.Process.myPid());
|
2012-08-18 21:00:51 +00:00
|
|
|
NativeCleanUp();
|
2012-08-16 20:28:14 +00:00
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-15 21:24:11 +00:00
|
|
|
|
2012-08-18 21:00:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-17 22:20:35 +00:00
|
|
|
public void AddToLog( String logMessage ) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-18 21:00:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 Testtt()
|
|
|
|
{
|
|
|
|
//Log.d("MCServer", "in Testtt");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-15 21:24:11 +00:00
|
|
|
static {
|
|
|
|
System.loadLibrary("mcserver");
|
|
|
|
}
|
|
|
|
|
2012-08-17 22:20:35 +00:00
|
|
|
|
2012-08-18 21:00:51 +00:00
|
|
|
public native void NativeOnCreate();
|
|
|
|
public native void NativeCleanUp();
|
|
|
|
public native boolean NativeIsServerRunning();
|
2012-08-16 20:28:14 +00:00
|
|
|
|
2012-08-15 21:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class MainThread extends Thread {
|
2012-08-18 21:00:51 +00:00
|
|
|
MCServerActivity mContext = null;
|
|
|
|
int numlogs = 0;
|
|
|
|
|
|
|
|
MainThread( MCServerActivity aContext ) {
|
|
|
|
mContext = aContext;
|
2012-08-15 21:24:11 +00:00
|
|
|
}
|
|
|
|
|
2012-08-17 22:20:35 +00:00
|
|
|
public void AddToLog( String logMessage ) {
|
2012-08-18 21:00:51 +00:00
|
|
|
mContext.SetText( logMessage );
|
2012-08-17 22:20:35 +00:00
|
|
|
//Log.d("MCServer", "Add to log: " + logMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void TestTest(){
|
2012-08-18 21:00:51 +00:00
|
|
|
numlogs++;
|
|
|
|
//Log.d("MCServer", "in testtest" + numlogs);
|
|
|
|
mContext.Testtt();
|
|
|
|
mContext.SetText("log no. " + numlogs);
|
2012-08-17 22:20:35 +00:00
|
|
|
}
|
|
|
|
|
2012-08-15 21:24:11 +00:00
|
|
|
public void run() {
|
2012-08-18 21:00:51 +00:00
|
|
|
mContext.NativeOnCreate();
|
2012-08-15 21:24:11 +00:00
|
|
|
}
|
2012-08-18 21:00:51 +00:00
|
|
|
|
2012-08-17 22:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|