[Android] Handle native thread finishing when not commanded from the Java side
authorGabriel Jacobo <gabomdq@gmail.com>
Thu, 05 Dec 2013 10:51:38 -0300
changeset 8048 a9c5ddad50b0
parent 8047 a5270cef21a7
child 8049 eac8f31e9bbd
[Android] Handle native thread finishing when not commanded from the Java side
android-project/src/org/libsdl/app/SDLActivity.java
test/controllermap.c
test/testjoystick.c
--- a/android-project/src/org/libsdl/app/SDLActivity.java	Tue Dec 03 12:09:58 2013 -0300
+++ b/android-project/src/org/libsdl/app/SDLActivity.java	Thu Dec 05 10:51:38 2013 -0300
@@ -26,6 +26,7 @@
 
     // 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 @@
         // 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 @@
         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 @@
             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 @@
             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();
         }
     }
 
--- a/test/controllermap.c	Tue Dec 03 12:09:58 2013 -0300
+++ b/test/controllermap.c	Thu Dec 05 10:51:38 2013 -0300
@@ -425,11 +425,7 @@
     }
     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
 
-#ifdef ANDROID
-    exit(0);
-#else
     return 0;
-#endif
 }
 
 #else
--- a/test/testjoystick.c	Tue Dec 03 12:09:58 2013 -0300
+++ b/test/testjoystick.c	Thu Dec 05 10:51:38 2013 -0300
@@ -287,11 +287,6 @@
     }
     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
 
-#ifdef ANDROID
-    exit(0);
-#else
-    return 0;
-#endif
 }
 
 #else