Skip to content

Commit

Permalink
[Android] Handle native thread finishing when not commanded from the …
Browse files Browse the repository at this point in the history
…Java side
  • Loading branch information
gabomdq committed Dec 5, 2013
1 parent 6c495a8 commit 77d2d55
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
27 changes: 27 additions & 0 deletions android-project/src/org/libsdl/app/SDLActivity.java
Expand Up @@ -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;
Expand Down Expand Up @@ -63,6 +64,9 @@ protected void onCreate(Bundle savedInstanceState) {
// 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();
}
Expand Down Expand Up @@ -115,6 +119,7 @@ protected void onDestroy() {
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
Expand Down Expand Up @@ -168,6 +173,12 @@ public static void handleResume() {
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
Expand Down Expand Up @@ -616,6 +627,22 @@ public void surfaceChanged(SurfaceHolder holder,
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();
}
}

Expand Down
4 changes: 0 additions & 4 deletions test/controllermap.c
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions test/testjoystick.c
Expand Up @@ -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
Expand Down

0 comments on commit 77d2d55

Please sign in to comment.