example: add 24 game task of Rosetta code
[nit.git] / lib / android / intent / intent_api10.nit
index 005b475..5f37179 100644 (file)
@@ -18,7 +18,7 @@
 # `android.content.Intent` for the android platform
 module intent_api10
 
-import native_app_glue
+import dalvik
 import android::bundle
 import serialization
 private import json_serialization
@@ -34,6 +34,8 @@ in "Java" `{
 extern class NativeIntent in "Java" `{ android.content.Intent `}
        super JavaObject
 
+       new in "Java" `{ return new Intent(); `}
+
        fun add_category(category: JavaString) in "Java" `{ recv.addCategory(category); `}
        fun add_flags(flags: Int) in "Java" `{ recv.addFlags((int)flags); `}
        fun filter_equals(other: NativeIntent): Bool in "Java" `{
@@ -626,23 +628,7 @@ end
 
 # Services allowing to launch an activity and start/stop services
 class Intent
-       protected var intent: NativeIntent
-       protected var context: NativeActivity
-
-       init (app: App)
-       do
-               self.context = app.native_activity
-               setup
-       end
-
-       private fun set_vars(intent: NativeIntent) do
-               self.intent = intent.new_global_ref
-       end
-
-       private fun setup import context, intent, set_vars in "Java" `{
-               Intent intent = new Intent();
-               Intent_set_vars(recv, intent);
-       `}
+       protected var intent: NativeIntent = (new NativeIntent).new_global_ref is lazy
 
        # The general action to be performed
        #
@@ -1285,7 +1271,7 @@ class Intent
        # Returns `self` allowing fluent programming
        fun []=(name: String, value: nullable Serializable): Intent
        do
-               var serialized_string = new StringOStream
+               var serialized_string = new StringWriter
                var serializer = new JsonSerializer(serialized_string)
                serializer.serialize(value)
 
@@ -1311,14 +1297,6 @@ class Intent
                sys.jni_env.pop_local_frame
                return self
        end
-       # Execute the intent and launch the appropriate application
-       fun launch_activity do context.start_activity(intent)
-
-       # Start a service that will be running until the `stop_service` call
-       fun start_service do context.start_service(intent)
-
-       # Stop service
-       fun stop_service do context.stop_service(intent)
 
        # Deletes intent global reference
        fun destroy do self.intent.delete_global_ref
@@ -1328,17 +1306,17 @@ class Intent
 end
 
 redef extern class NativeActivity
-       fun start_activity(intent: NativeIntent) in "Java" `{ recv.startActivity(intent); `}
-       fun start_service(intent: NativeIntent) in "Java" `{ recv.startService(intent); `}
-       fun stop_service(intent: NativeIntent) in "Java" `{ recv.stopService(intent); `}
+       private fun start_activity(intent: NativeIntent) in "Java" `{ recv.startActivity(intent); `}
+       private fun start_service(intent: NativeIntent) in "Java" `{ recv.startService(intent); `}
+       private fun stop_service(intent: NativeIntent) in "Java" `{ recv.stopService(intent); `}
 end
 
 # Allows user to get values with enum-like syntax : `intent_action.main`
-protected fun intent_action: Action do return once new Action
+fun intent_action: Action do return once new Action
 # Allows user to get values with enum-like syntax : `intent_category.home`
-protected fun intent_category: Category do return once new Category
+fun intent_category: Category do return once new Category
 # Allows user to get values with enum-like syntax : `intent_flag.activity_brought_to_front`
-protected fun intent_flag: Flag do return once new Flag
+fun intent_flag: Flag do return once new Flag
 
 private class StringCopyArray
        var collection = new Array[String]
@@ -1349,3 +1327,15 @@ private class StringCopyHashSet
        var collection = new HashSet[String]
        fun add(element: JavaString) do collection.add element.to_s
 end
+
+redef class App
+
+       # Execute the intent and launch the appropriate application
+       fun start_activity(intent: Intent) do native_activity.start_activity(intent.intent)
+
+       # Start a service that will be running until the `stop_service` call
+       fun start_service(intent: Intent) do native_activity.start_service(intent.intent)
+
+       # Stop service
+       fun stop_service(intent: Intent) do native_activity.stop_service(intent.intent)
+end