Android: I'm probably using a lot of bad practices, but the app looks somewhat better now and polls the server status

Android: Put quite some testing code in ToJava.h and app-android.cpp ... I still can't send log messages to Java. The issue is threads, I can't use JNI stuff from threads that were not created by Java (at least not easily)

git-svn-id: http://mc-server.googlecode.com/svn/trunk@752 0a769ca7-a7f5-676a-18bf-c427514a06d6
master
faketruth 2012-08-18 21:00:51 +00:00
parent 1b5eaa92b5
commit b6138129e6
6 changed files with 271 additions and 22 deletions

View File

@ -14,7 +14,12 @@ public final class R {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int ip_address=0x7f050005;
public static final int server_status_text=0x7f050004;
public static final int start_server=0x7f050002;
public static final int stop_server=0x7f050003;
public static final int textView1=0x7f050000;
public static final int textView2=0x7f050001;
}
public static final class layout {
public static final int main=0x7f030000;
@ -22,5 +27,10 @@ public final class R {
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
public static final int mcserver_is_not_running=0x7f040005;
public static final int mcserver_is_running=0x7f040004;
public static final int start=0x7f040002;
public static final int stop=0x7f040003;
public static final int your_ip=0x7f040006;
}
}

View File

@ -3,7 +3,7 @@
#include <jni.h>
#include <android/log.h>
extern JNIEnv* g_CurrentJNIEnv;
extern JavaVM* g_JavaVM;
extern jobject g_JavaThread;
//extern jobject g_JavaActivity;
@ -11,6 +11,12 @@ extern jobject g_JavaThread;
static void CallJavaFunction_Void_String( jobject a_Object, const std::string & a_FunctionName, const std::string & a_StringParam )
{
JNIEnv * oldEnv = g_CurrentJNIEnv;
int status = g_JavaVM->AttachCurrentThread(&g_CurrentJNIEnv, NULL);
__android_log_print(ANDROID_LOG_ERROR,"MCServer", "STATUS: %i old: %p new: %p", status, oldEnv, g_CurrentJNIEnv );
jstring str = g_CurrentJNIEnv->NewStringUTF( a_StringParam.c_str() );
//__android_log_print(ANDROID_LOG_ERROR,"MCServer", "JNIEnv: %i Object: %i", g_CurrentJNIEnv, a_Object );
jclass cls = g_CurrentJNIEnv->GetObjectClass( a_Object );
//__android_log_print(ANDROID_LOG_ERROR,"MCServer", "jclass: %i", cls );
@ -18,26 +24,32 @@ static void CallJavaFunction_Void_String( jobject a_Object, const std::string &
//__android_log_print(ANDROID_LOG_ERROR,"MCServer", "jmethodID: %i", mid );
if (mid != 0)
{
//__android_log_print(ANDROID_LOG_ERROR,"MCServer", "Going to call right NOW! %s", a_FunctionName.c_str() );
g_CurrentJNIEnv->CallVoidMethod( a_Object, mid, g_CurrentJNIEnv->NewStringUTF( a_StringParam.c_str() ) );
__android_log_print(ANDROID_LOG_ERROR,"MCServer", "Going to call right NOW! %s", a_FunctionName.c_str() );
g_CurrentJNIEnv->CallVoidMethod( a_Object, mid, str );
}
else
{
__android_log_print(ANDROID_LOG_ERROR,"MCServer", "It was 0, derp" );
}
if( oldEnv != g_CurrentJNIEnv )
{
g_JavaVM->DetachCurrentThread();
}
}
static void CallJavaFunction_Void_Void( jobject a_Object, const std::string & a_FunctionName )
{
__android_log_print(ANDROID_LOG_ERROR,"MCServer", "JNIEnv: %i Object: %i", g_CurrentJNIEnv, a_Object );
//__android_log_print(ANDROID_LOG_ERROR,"MCServer", "JNIEnv: %i Object: %i", g_CurrentJNIEnv, a_Object );
jclass cls = g_CurrentJNIEnv->GetObjectClass( a_Object );
__android_log_print(ANDROID_LOG_ERROR,"MCServer", "jclass: %i", cls );
//__android_log_print(ANDROID_LOG_ERROR,"MCServer", "jclass: %i", cls );
jmethodID mid = g_CurrentJNIEnv->GetMethodID( cls, a_FunctionName.c_str(), "()V"); // void a_FunctionName( String )
__android_log_print(ANDROID_LOG_ERROR,"MCServer", "jmethodID: %i", mid );
//__android_log_print(ANDROID_LOG_ERROR,"MCServer", "jmethodID: %i", mid );
if (mid != 0)
{
__android_log_print(ANDROID_LOG_ERROR,"MCServer", "Going to call right NOW! %s", a_FunctionName.c_str() );
//__android_log_print(ANDROID_LOG_ERROR,"MCServer", "Going to call right NOW! %s", a_FunctionName.c_str() );
g_CurrentJNIEnv->CallVoidMethod( a_Object, mid );
}
else

View File

@ -21,16 +21,52 @@ cCriticalSection g_CriticalSection;
JNIEnv* g_CurrentJNIEnv = 0;
jobject g_JavaThread = 0;
JavaVM* g_JavaVM = 0;
//jobject g_JavaActivity = 0;
cRoot * pRoot = NULL;
class cMainThread :
public cIsThread
{
public:
cMainThread() :
cIsThread("cMainThread")
{
//Start();
__android_log_print(ANDROID_LOG_ERROR,"MCServer", "%s", "cMainThread");
}
void Stop(void)
{
m_ShouldTerminate = true;
Wait();
}
protected:
virtual void Execute(void) override
{
__android_log_print(ANDROID_LOG_ERROR,"MCServer", "%s", "Execute");
pRoot = new cRoot();
pRoot->Start();
delete pRoot;
}
} ;
cMainThread * pMainThread = NULL;
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
__android_log_print(ANDROID_LOG_ERROR,"MCServer", "%s", "JNI_OnLoad JNI_OnLoad JNI_OnLoad JNI_OnLoad");
g_JavaVM = vm;
return JNI_VERSION_1_4;
}
/* Called when program/activity is created */
extern "C" void Java_com_mcserver_MainThread_NativeOnCreate( JNIEnv* env, jobject thiz )
extern "C" void Java_com_mcserver_MCServerActivity_NativeOnCreate( JNIEnv* env, jobject thiz )
{
g_CriticalSection.Lock();
g_CurrentJNIEnv = env;
@ -44,25 +80,40 @@ extern "C" void Java_com_mcserver_MainThread_NativeOnCreate( JNIEnv* env, jobje
mkdir("/sdcard/mcserver", S_IRWXU | S_IRWXG | S_IRWXO);
// __android_log_print(ANDROID_LOG_ERROR,"MCServer", "%s", "Before mainthread");
// pMainThread = new cMainThread();
// pMainThread->Start();
// __android_log_print(ANDROID_LOG_ERROR,"MCServer", "%s", "AFter mainthread");
pRoot = new cRoot();
pRoot->Start();
delete pRoot;
delete pRoot; pRoot = NULL;
}
extern "C" void Java_com_mcserver_MainThread_NativeCleanUp( JNIEnv* env, jobject thiz )
extern "C" void Java_com_mcserver_MCServerActivity_NativeCleanUp( JNIEnv* env, jobject thiz )
{
g_CriticalSection.Lock();
g_CurrentJNIEnv = env;
g_JavaThread = thiz;
g_CriticalSection.Unlock();
__android_log_print(ANDROID_LOG_ERROR,"MCServer", "pRoot: %p", pRoot);
if( pRoot != NULL )
{
pRoot->ServerCommand("stop");
}
// pMainThread->Stop();
// delete pMainThread; pMainThread = NULL;
}
extern "C" jboolean Java_com_mcserver_MCServerActivity_NativeIsServerRunning( JNIEnv* env, jobject thiz )
{
return pRoot != NULL;
}

View File

@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
@ -10,4 +11,37 @@
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/start_server"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/start" />
<Button
android:id="@+id/stop_server"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:text="@string/stop" />
<TextView
android:id="@+id/server_status_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mcserver_is_not_running"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/ip_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/your_ip" />
</LinearLayout>

View File

@ -3,5 +3,10 @@
<string name="hello">Hello World, MCServerActivity!</string>
<string name="app_name">MCServer</string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="mcserver_is_running">MCServer is running</string>
<string name="mcserver_is_not_running">MCServer is not running</string>
<string name="your_ip">Your IP …</string>
</resources>

View File

@ -1,12 +1,22 @@
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
@ -14,51 +24,178 @@ public class MCServerActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mThread = new MainThread();
((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());
mThread.NativeCleanUp();
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 {
MainThread() {
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(){
Log.d("MCServer", "in testtest");
numlogs++;
//Log.d("MCServer", "in testtest" + numlogs);
mContext.Testtt();
mContext.SetText("log no. " + numlogs);
}
public void run() {
NativeOnCreate();
mContext.NativeOnCreate();
}
public native void NativeOnCreate();
public native void NativeCleanUp();
}