Merge: Nitsmell : Adding new code smells and print console updated
[nit.git] / lib / android / nit_activity.nit
index 765ed01..1bdf597 100644 (file)
@@ -43,6 +43,7 @@ end
 import platform
 import log
 import activities
+import key_event
 import bundle
 import dalvik
 
@@ -133,6 +134,36 @@ in "C body" `{
        {
                Activity_on_restore_instance_state((Activity)nit_activity, saved_state);
        }
+
+       JNIEXPORT jboolean JNICALL Java_nit_app_NitActivity_nitOnBackPressed
+         (JNIEnv *env, jobject java_activity, jint nit_activity)
+       {
+               return (jboolean)Activity_on_back_pressed((Activity)nit_activity);
+       }
+
+       JNIEXPORT jboolean JNICALL Java_nit_app_NitActivity_nitOnKeyDown
+         (JNIEnv *env, jobject java_activity, jint nit_activity, jint keyCode, jobject event)
+       {
+               return (jboolean)Activity_on_key_down((Activity)nit_activity, keyCode, event);
+       }
+
+       JNIEXPORT jboolean JNICALL Java_nit_app_NitActivity_nitOnKeyLongPress
+         (JNIEnv *env, jobject java_activity, jint nit_activity, jint keyCode, jobject event)
+       {
+               return (jboolean)Activity_on_key_long_press((Activity)nit_activity, keyCode, event);
+       }
+
+       JNIEXPORT jboolean JNICALL Java_nit_app_NitActivity_nitOnKeyMultiple
+         (JNIEnv *env, jobject java_activity, jint nit_activity, jint keyCode, jint count, jobject event)
+       {
+               return (jboolean)Activity_on_key_multiple((Activity)nit_activity, keyCode, count, event);
+       }
+
+       JNIEXPORT jboolean JNICALL Java_nit_app_NitActivity_nitOnKeyUp
+         (JNIEnv *env, jobject java_activity, jint nit_activity, jint keyCode, jobject event)
+       {
+               return (jboolean)Activity_on_key_up((Activity)nit_activity, keyCode, event);
+       }
 `}
 
 # Wrapper to our Java `NitActivity`
@@ -158,9 +189,12 @@ redef class App
        Activity.on_create, Activity.on_destroy,
        Activity.on_start, Activity.on_restart, Activity.on_stop,
        Activity.on_pause, Activity.on_resume,
-       Activity.on_save_instance_state, Activity.on_restore_instance_state `{
-               App_incr_ref(recv);
-               global_app = recv;
+       Activity.on_save_instance_state, Activity.on_restore_instance_state,
+       Activity.on_back_pressed,
+       Activity.on_key_down, Activity.on_key_long_press,
+       Activity.on_key_multiple, Activity.on_key_up `{
+               App_incr_ref(self);
+               global_app = self;
        `}
 
        # Create the Nit side to this new `native` Java activity, and return it to Java
@@ -173,6 +207,14 @@ redef class App
        end
 end
 
+redef class AppComponent
+       # The application is starting or restarting, it is visible to the user
+       fun on_start do end
+
+       # The application is being destroyed
+       fun on_destroy do end
+end
+
 # An Android activity
 #
 # You must implement the callbacks (prefixed with `on_`) to follow the
@@ -189,38 +231,44 @@ class Activity
        # as registered by `on_save_instance_state`.
        #
        # Followed by `on_start`.
-       fun on_create(save_state: NativeBundle) do end
+       fun on_create(save_state: NativeBundle)
+       do
+               app.on_create
+               app.on_restore_state
+       end
 
        # Notification from Android, the activity has been restarted
        #
        # Followed by `on_start`.
-       fun on_restart do end
+       fun on_restart do app.on_restart
 
        # Notification from Android, the activity has been started
        #
        # Followed by `on_resume` or `on_stop`.
-       fun on_start do end
+       fun on_start do app.on_start
 
        # Notification from Android, the activity has been resumed
        #
        # Followed by `on_pause`
-       fun on_resume do end
+       fun on_resume do app.on_resume
 
        # Notification from Android, the activity has been paused
        #
        # Followed by `on_resume` or `on_stop`.
-       fun on_pause do end
+       fun on_pause do app.on_pause
 
        # Notification from Android, the activity has been stopped
        #
        # Followed by `on_restart` or `on_destroy`.
-       fun on_stop do end
+       fun on_stop do app.on_stop
 
        # Notification from Android, the activity is being destroyed
        #
        # Clean up and exit.
        fun on_destroy
        do
+               app.on_destroy
+
                native.delete_global_ref
                app.activities.remove self
        end
@@ -233,7 +281,7 @@ class Activity
        # Notification from Android, the activity may be stopped, save state
        #
        # Occurs before `on_stop` and, without guarantee, before or after `on_pause`.
-       fun on_save_instance_state(save_state: NativeBundle) do end
+       fun on_save_instance_state(save_state: NativeBundle) do app.on_save_state
 
        # Notification from Android, the system is running low on memory
        #
@@ -245,6 +293,31 @@ class Activity
 
        # Notification from Android, the current device configuration has changed
        fun on_configuration_changed do end
+
+       # The back key has been pressed
+       #
+       # Return `true` if the event has been handled.
+       fun on_back_pressed: Bool do return false
+
+       # A key has been pressed
+       #
+       # Return `true` if the event has been handled.
+       fun on_key_down(key_code: Int, event: NativeKeyEvent): Bool do return false
+
+       # A key has been long pressed
+       #
+       # Return `true` if the event has been handled.
+       fun on_key_long_press(key_code: Int, event: NativeKeyEvent): Bool do return false
+
+       # Multiple down/up pairs of the same key have occurred in a row
+       #
+       # Return `true` if the event has been handled.
+       fun on_key_multiple(key_code, count: Int, event: NativeKeyEvent): Bool do return false
+
+       # A key has been released
+       #
+       # Return `true` if the event has been handled.
+       fun on_key_up(key_code: Int, event: NativeKeyEvent): Bool do return false
 end
 
 # Set up global data in C and leave it to Android to callback Java, which we relay to Nit