diff --git a/src/client/clientlauncher.cpp b/src/client/clientlauncher.cpp index c68a485c7..ab32740e1 100644 --- a/src/client/clientlauncher.cpp +++ b/src/client/clientlauncher.cpp @@ -34,6 +34,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "fontengine.h" #include "clientlauncher.h" +#ifdef ANDROID + #include "porting_android.h" +#endif + /* mainmenumanager.h */ gui::IGUIEnvironment *guienv = NULL; @@ -191,6 +195,10 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args) device->setWindowCaption((narrow_to_wide("MultiCraft") + L" [" + text + L"]").c_str()); delete[] text; +#ifdef ANDROID + porting::handleAndroidActivityEvents(); +#endif + try { // This is used for catching disconnects guienv->clear(); diff --git a/src/game.cpp b/src/game.cpp index 69091caf5..949346771 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -68,6 +68,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "touchscreengui.h" #endif +#ifdef ANDROID + #include "porting_android.h" +#endif + extern Settings *g_settings; extern Profiler *g_profiler; @@ -1766,6 +1770,10 @@ void Game::run() while (device->run() && !(*kill || g_gamecallback->shutdown_requested)) { +#ifdef ANDROID + porting::handleAndroidActivityEvents(); +#endif + /* Must be called immediately after a device->run() call because it * uses device->getTimer()->getTime() */ diff --git a/src/mainmenumanager.h b/src/mainmenumanager.h index 6f8aa9137..9affe6f09 100644 --- a/src/mainmenumanager.h +++ b/src/mainmenumanager.h @@ -138,7 +138,7 @@ public: { shutdown_requested = true; #ifndef __ANDROID__ - device->closeDevice(); + porting::handleAndroidActivityEvents(); #endif } diff --git a/src/porting_android.cpp b/src/porting_android.cpp index 910f5a729..dd9bc28d6 100644 --- a/src/porting_android.cpp +++ b/src/porting_android.cpp @@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "filesys.h" #include "log.h" #include +#include #ifdef GPROF #include "prof.h" @@ -63,7 +64,7 @@ void android_main(android_app *app) porting::cleanupAndroid(); errorstream << "Shutting down minetest." << std::endl; - exit(0); + } /* handler for finished message box input */ @@ -88,6 +89,17 @@ android_app* app_global; JNIEnv* jnienv; jclass nativeActivity; +void handleAndroidActivityEvents() +{ + int ident; + int events; + struct android_poll_source *source; + + while ( (ident = ALooper_pollOnce(0, NULL, &events, (void**)&source)) >= 0) + if (source) + source->process(porting::app_global, source); +} + jclass findClass(std::string classname) { if (jnienv == 0) { @@ -163,7 +175,8 @@ void cleanupAndroid() #endif JavaVM *jvm = app_global->activity->vm; - jvm->DetachCurrentThread(); + if (jvm) + jvm->DetachCurrentThread(); } void setExternalStorageDir(JNIEnv* lJNIEnv) diff --git a/src/porting_android.h b/src/porting_android.h index bfdadfbff..47ef42756 100644 --- a/src/porting_android.h +++ b/src/porting_android.h @@ -76,6 +76,7 @@ int getInputDialogState(); */ std::string getInputDialogValue(); +void handleAndroidActivityEvents(); } #endif