lib/android: extract activities from native_app_glue
[nit.git] / lib / android / native_app_glue.nit
index 6090450..9017a29 100644 (file)
 #   is on the same thread as Nit and manages the synchronization with the
 #   main Android thread.
 #
-# * `NativeActivity` is implemented in Java by `android.app.NativeActivity`,
+# * `NativeNativeActivity` is implemented in Java by `android.app.NativeActivity`,
 #   which is a subclass of `Activity` and `Context` (in Java). It represent
 #   main activity of the running application. Use it to get anything related
 #   to the `Context` and as anchor to execute Java UI code.
-module native_app_glue
+module native_app_glue is ldflags "-landroid"
 
 import platform
 import log
+import activities
 
 in "C header" `{
        #include <android_native_app_glue.h>
@@ -118,8 +119,8 @@ in "C body" `{
 #
 # Can be used to get anything related to the `Context` of the activity in Java
 # and as anchor to execute Java UI code.
-extern class NativeActivity in "Java" `{ android.app.NativeActivity `}
-       super JavaObject
+extern class NativeNativeActivity in "Java" `{ android.app.NativeActivity `}
+       super NativeActivity
 end
 
 redef class App
@@ -207,6 +208,29 @@ 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
+       fun poll_looper(timeout_ms: Int) import handle_looper_event `{
+               int ident;
+               int event;
+               void* source;
+               while ((ident=ALooper_pollAll(timeout_ms, NULL, &event, &source)) >= 0) {
+                       App_handle_looper_event(recv, 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,
+               destroy, start, resume, low_memory, config_changed, input_changed,
+               window_resized, window_redraw_needed, content_rect_changed `{
+
+               struct android_app *app_glue = App_native_app_glue(recv);
+               struct android_poll_source* source = (struct android_poll_source*)data;
+               
+               // Process this event.
+               if (source != NULL) source->process(app_glue, source);
+       `}
 end
 
 # An Android activity implemented in C. This is the C part of `NativeActivity`
@@ -229,11 +253,11 @@ extern class NdkNativeActivity `{ ANativeActivity * `}
        # The `NativeActivity`, as in the Java object, associated to `self`
        fun java_native_activity: NativeActivity `{ return recv->clazz; `}
 
-       # Path to this application's internal data directory.
+       # Path to this application's internal data directory.
        fun internal_data_path: NativeString `{ return (char*)recv->internalDataPath; `}
     
        # Path to this application's external (removable/mountable) data directory.
-       fun external_data_path: NativeString `{ return (char*)recv->externalDataPath; `}
+       fun external_data_path: NativeString `{ return (char*)recv->externalDataPath; `}
     
        # The platform's SDK version code.
        fun sdk_version: Int `{ return recv->sdkVersion; `}
@@ -268,7 +292,10 @@ extern class NativeAppGlue `{ struct android_app* `}
        # We use the `userData` field of the C structure to store an handle to
        # the associated App
        private fun user_data: App `{ return recv->userData; `}
-       private fun user_data=(val: App) `{ recv->userData = val; `}
+       private fun user_data=(val: App) `{
+               App_incr_ref(val);
+               recv->userData = val;
+       `}
 
        # Fill this in with the function to process input events.  At this point
        # the event has already been pre-dispatched, and it will be finished upon
@@ -325,6 +352,7 @@ end
 
 # Android NDK's structure to handle events synchronously
 extern class ALooper `{ ALooper* `}
+       # Returns the looper associated with the calling thread, or NULL if there is not one
        new for_thread `{ return ALooper_forThread(); `}
 end