functional: Added universal types to callref
[nit.git] / lib / android / native_app_glue.nit
index 2a8f895..6b9c0b5 100644 (file)
@@ -52,13 +52,27 @@ in "C header" `{
 in "C body" `{
        struct android_app* native_app_glue_data;
 
+       // Was `android_main` called?
+       int android_main_launched = 0;
+
        // Entry point called by the native_app_glue_framework framework
        // We relay the call to the Nit application.
        void android_main(struct android_app* app) {
                native_app_glue_data = app;
 
-               int main(int argc, char ** argv);
-               main(0, NULL);
+               if (android_main_launched) {
+                       // Second call to `android_main`, may happen if `exit 0` was not
+                       // called previously to force unloading the Nit app state.
+                       // This happens sometimes when the `destroy` lifecycle command
+                       // was not correctly received.
+                       // We `exit 0` here hoping the system restarts the app nicely
+                       // without an error popup.
+                       exit(0);
+               } else {
+                       android_main_launched = 1;
+                       int main(int argc, char ** argv);
+                       main(0, NULL);
+               }
        }
 
        // Main callback on the native_app_glue framework
@@ -195,7 +209,7 @@ redef class App
        # Notification from the Android framework, the system is running low on memory
        #
        # Try to reduce your memory use.
-       fun low_memory do end
+       fun low_memory do force_garbage_collection
 
        # Notification from the Android framework, the current device configuration has changed
        fun config_changed do end
@@ -216,7 +230,7 @@ redef class App
        # Raised when the soft input window being shown or hidden, and similar events.
        fun content_rect_changed do end
 
-       # Call the `ALooper` to retrieve events and callback the application
+       # Call the `ALooper_pollAll` to retrieve events and callback the application
        fun poll_looper(timeout_ms: Int) import handle_looper_event `{
                int ident;
                int event;
@@ -226,6 +240,16 @@ redef class App
                }
        `}
 
+       # Call the `ALooper_pollOnce` to retrieve at most one event and callback the application
+       fun poll_looper_pause(timeout_ms: Int) import handle_looper_event `{
+               int event;
+               void* source;
+               int ident = ALooper_pollOnce(timeout_ms, NULL, &event, &source);
+               if (ident >= 0) {
+                       App_handle_looper_event(self, ident, event, source);
+               }
+       `}
+
        # Handle an event retrieved by the `ALooper` and `poll_looper` without a callback
        protected fun handle_looper_event(ident, event: Int, data: Pointer) import native_app_glue,
                save_state, init_window, term_window, gained_focus, lost_focus, pause, stop,
@@ -350,7 +374,7 @@ extern class NativeAppGlue `{ struct android_app* `}
 
        # This is non-zero when the application's NativeActivity is being
        # destroyed and waiting for the app thread to complete.
-       fun detroy_request: Bool `{ return self->destroyRequested; `}
+       fun destroy_requested: Bool `{ return self->destroyRequested; `}
 end
 
 # Android NDK's struture holding configurations of the native app