lib/android: Bundle do not need a context or to import native_app_glue
[nit.git] / lib / android / bundle / bundle.nit
index 1d1ec0c..68713ad 100644 (file)
 # Android API for various data exchange purposes
 module bundle
 
-import native_app_glue
 import serialization
 import json_serialization
 
+import platform
+import activities
+
 in "Java" `{
        import android.os.Bundle;
        import android.app.Activity;
@@ -31,7 +33,8 @@ in "Java" `{
 
 extern class NativeBundle in "Java" `{ android.os.Bundle `}
        super JavaObject
-       redef type SELF: NativeBundle
+
+       new in "Java" `{ return new Bundle(); `}
 
        fun clone: JavaObject in "Java" `{ return recv.clone(); `}
        fun size: Int in "Java" `{ return recv.size(); `}
@@ -411,30 +414,21 @@ extern class NativeBundle in "Java" `{ android.os.Bundle `}
        `}
        fun describe_contents: Int in "Java" `{ return recv.describeContents(); `}
        fun to_string: JavaString in "Java" `{ return recv.toString(); `}
+
+       # HACK for bug #845
+       redef fun new_global_ref import sys, Sys.jni_env `{
+               Sys sys = NativeBundle_sys(recv);
+               JNIEnv *env = Sys_jni_env(sys);
+               return (*env)->NewGlobalRef(env, recv);
+       `}
 end
 
 # A class mapping `String` keys to various value types
 class Bundle
-       private var native_bundle: NativeBundle
-       private var context: NativeActivity
-
-       init(app: App)
-       do
-               self.context = app.native_activity
-               setup
-       end
-
-       private fun set_vars(native_bundle: NativeBundle)
-       do
-               self.native_bundle = native_bundle.new_global_ref
-       end
+       private var native_bundle: NativeBundle = new NativeBundle is lazy
 
-       private fun setup import context, set_vars in "Java" `{
-               Activity context = (Activity) Bundle_context(recv);
-               Bundle bundle = new Bundle();
-               
-               Bundle_set_vars(recv, bundle);
-       `}
+       # Get a new `Bundle` wrapping `native_bundle`
+       init from(native_bundle: NativeBundle) do self.native_bundle = native_bundle
 
        # Returns `true` if the Bundle contains this key
        fun has(key: String): Bool
@@ -659,7 +653,6 @@ class Bundle
        # Returns the `null` if none or if it's the wrong value type
        fun array_of_string(key: String): nullable Array[String]
        do
-               var string_array = new Array[String]
                sys.jni_env.push_local_frame(1)
 
                var return_value = native_bundle.get_string_array(key.to_java_string)