[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 8048a9c5ddad50b0
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
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Tue Dec 03 12:09:58 2013 -0300
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Thu Dec 05 10:51:38 2013 -0300
     1.3 @@ -26,6 +26,7 @@
     1.4  
     1.5      // Keep track of the paused state
     1.6      public static boolean mIsPaused = false, mIsSurfaceReady = false, mHasFocus = true;
     1.7 +    public static boolean mExitCalledFromJava;
     1.8  
     1.9      // Main components
    1.10      protected static SDLActivity mSingleton;
    1.11 @@ -63,6 +64,9 @@
    1.12          // Set up the surface
    1.13          mSurface = new SDLSurface(getApplication());
    1.14          
    1.15 +        // Make sure this variable is initialized here!
    1.16 +        mExitCalledFromJava = false;
    1.17 +        
    1.18          if(Build.VERSION.SDK_INT >= 12) {
    1.19              mJoystickHandler = new SDLJoystickHandler_API12();
    1.20          }
    1.21 @@ -115,6 +119,7 @@
    1.22          super.onDestroy();
    1.23          Log.v("SDL", "onDestroy()");
    1.24          // Send a quit message to the application
    1.25 +        SDLActivity.mExitCalledFromJava = true;
    1.26          SDLActivity.nativeQuit();
    1.27  
    1.28          // Now wait for the SDL thread to quit
    1.29 @@ -168,6 +173,12 @@
    1.30              mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
    1.31          }
    1.32      }
    1.33 +        
    1.34 +    /* The native thread has finished */
    1.35 +    public static void handleNativeExit() {
    1.36 +        SDLActivity.mSDLThread = null;
    1.37 +        mSingleton.finish();
    1.38 +    }
    1.39  
    1.40  
    1.41      // Messages from the SDLMain thread
    1.42 @@ -616,6 +627,22 @@
    1.43              SDLActivity.mSDLThread = new Thread(new SDLMain(), "SDLThread");
    1.44              enableSensor(Sensor.TYPE_ACCELEROMETER, true);
    1.45              SDLActivity.mSDLThread.start();
    1.46 +            
    1.47 +            // Set up a listener thread to catch when the native thread ends
    1.48 +            new Thread(new Runnable(){
    1.49 +                public void run(){
    1.50 +                    try {
    1.51 +                        SDLActivity.mSDLThread.join();
    1.52 +                    }
    1.53 +                    catch(Exception e){}
    1.54 +                    finally{ 
    1.55 +                        // Native thread has finished
    1.56 +                        if (! SDLActivity.mExitCalledFromJava) {
    1.57 +                            SDLActivity.handleNativeExit();
    1.58 +                        }
    1.59 +                    }
    1.60 +                }
    1.61 +            }).start();
    1.62          }
    1.63      }
    1.64  
     2.1 --- a/test/controllermap.c	Tue Dec 03 12:09:58 2013 -0300
     2.2 +++ b/test/controllermap.c	Thu Dec 05 10:51:38 2013 -0300
     2.3 @@ -425,11 +425,7 @@
     2.4      }
     2.5      SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
     2.6  
     2.7 -#ifdef ANDROID
     2.8 -    exit(0);
     2.9 -#else
    2.10      return 0;
    2.11 -#endif
    2.12  }
    2.13  
    2.14  #else
     3.1 --- a/test/testjoystick.c	Tue Dec 03 12:09:58 2013 -0300
     3.2 +++ b/test/testjoystick.c	Thu Dec 05 10:51:38 2013 -0300
     3.3 @@ -287,11 +287,6 @@
     3.4      }
     3.5      SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
     3.6  
     3.7 -#ifdef ANDROID
     3.8 -    exit(0);
     3.9 -#else
    3.10 -    return 0;
    3.11 -#endif
    3.12  }
    3.13  
    3.14  #else