[Android] Handle native thread finishing when not commanded from the Java side
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
diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java
index 966d236..045130e 100644
--- a/android-project/src/org/libsdl/app/SDLActivity.java
+++ b/android-project/src/org/libsdl/app/SDLActivity.java
@@ -26,6 +26,7 @@ public class SDLActivity extends Activity {
// Keep track of the paused state
public static boolean mIsPaused = false, mIsSurfaceReady = false, mHasFocus = true;
+ public static boolean mExitCalledFromJava;
// Main components
protected static SDLActivity mSingleton;
@@ -63,6 +64,9 @@ public class SDLActivity extends Activity {
// Set up the surface
mSurface = new SDLSurface(getApplication());
+ // Make sure this variable is initialized here!
+ mExitCalledFromJava = false;
+
if(Build.VERSION.SDK_INT >= 12) {
mJoystickHandler = new SDLJoystickHandler_API12();
}
@@ -115,6 +119,7 @@ public class SDLActivity extends Activity {
super.onDestroy();
Log.v("SDL", "onDestroy()");
// Send a quit message to the application
+ SDLActivity.mExitCalledFromJava = true;
SDLActivity.nativeQuit();
// Now wait for the SDL thread to quit
@@ -168,6 +173,12 @@ public class SDLActivity extends Activity {
mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
}
}
+
+ /* The native thread has finished */
+ public static void handleNativeExit() {
+ SDLActivity.mSDLThread = null;
+ mSingleton.finish();
+ }
// Messages from the SDLMain thread
@@ -616,6 +627,22 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
SDLActivity.mSDLThread = new Thread(new SDLMain(), "SDLThread");
enableSensor(Sensor.TYPE_ACCELEROMETER, true);
SDLActivity.mSDLThread.start();
+
+ // Set up a listener thread to catch when the native thread ends
+ new Thread(new Runnable(){
+ public void run(){
+ try {
+ SDLActivity.mSDLThread.join();
+ }
+ catch(Exception e){}
+ finally{
+ // Native thread has finished
+ if (! SDLActivity.mExitCalledFromJava) {
+ SDLActivity.handleNativeExit();
+ }
+ }
+ }
+ }).start();
}
}
diff --git a/test/controllermap.c b/test/controllermap.c
index dc943cb..c17f17c 100644
--- a/test/controllermap.c
+++ b/test/controllermap.c
@@ -425,11 +425,7 @@ main(int argc, char *argv[])
}
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
-#ifdef ANDROID
- exit(0);
-#else
return 0;
-#endif
}
#else
diff --git a/test/testjoystick.c b/test/testjoystick.c
index 7d3c573..d9db6e8 100644
--- a/test/testjoystick.c
+++ b/test/testjoystick.c
@@ -287,11 +287,6 @@ main(int argc, char *argv[])
}
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
-#ifdef ANDROID
- exit(0);
-#else
- return 0;
-#endif
}
#else