summaryrefslogtreecommitdiffstats
path: root/src/com/mcserver/MCServerActivity.java
blob: f9bb363f461d538a0264292d0fd929b4fc54635d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.mcserver;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MCServerActivity extends Activity {
	MainThread mThread = null;
	Thread ServerStatusThread = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ((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);
		}
    }
    
    
    
    
    
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
	    if(keyCode==KeyEvent.KEYCODE_BACK) 
	    { 
		    //android.os.Process.killProcess(android.os.Process.myPid());
	    	NativeCleanUp();
		    return super.onKeyDown(keyCode, event); 
	    } 
	    return false; 
    }
    
    
    
    
    
    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 Testtt()
    {
    	//Log.d("MCServer", "in Testtt");
    }
    
    
    
    
    
    static {
        System.loadLibrary("mcserver");
    }
    
    
    public native void    NativeOnCreate();
    public native void    NativeCleanUp();
    public native boolean NativeIsServerRunning();
    
}


class MainThread extends Thread {
	MCServerActivity mContext = null;
	int numlogs = 0;
	
    MainThread( MCServerActivity aContext ) {
    	mContext = aContext;
    }

    public void AddToLog( String logMessage ) {
    	mContext.SetText( logMessage );
    	//Log.d("MCServer", "Add to log: " + logMessage);
    }
    
    public void TestTest(){
    	numlogs++;
    	//Log.d("MCServer", "in testtest" + numlogs);
    	mContext.Testtt();
    	mContext.SetText("log no. " + numlogs);
    }
    
    public void run() {
    	mContext.NativeOnCreate();
    }

}