1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # A mapping class of `String` to various value types used by the
18 # Android API for various data exchange purposes
22 import json
::serialization
28 import android.os.Bundle;
29 import android.app.Activity;
30 import java.util.ArrayList;
34 extern class NativeBundle in "Java" `{ android.os.Bundle `}
37 new in "Java" `{ return new Bundle(); `}
39 fun clone
: JavaObject in "Java" `{ return self.clone(); `}
40 fun size: Int in "Java" `{ return self.size(); `}
41 fun is_empty
: Bool in "Java" `{ return self.isEmpty(); `}
42 fun clear in "Java" `{ self.clear(); `}
43 fun contains_key
(key
: JavaString): Bool in "Java" `{ return self.containsKey(key); `}
44 fun get(key: JavaString): JavaObject in "Java" `{ return self.get(key); `}
45 fun remove
(key
: JavaString) in "Java" `{ self.remove(key); `}
46 fun put_all(bundle: NativeBundle) in "Java" `{ self.putAll(bundle); `}
47 fun key_set
: HashSet[JavaString] import HashSet[JavaString],
48 HashSet[JavaString].add
in "Java" `{
49 Set<String> java_set = self.keySet();
50 int nit_hashset = new_HashSet_of_JavaString();
52 for (String element: java_set)
53 HashSet_of_JavaString_add(nit_hashset, element);
57 fun has_file_descriptors
: Bool in "Java" `{ return self.hasFileDescriptors(); `}
58 fun put_boolean(key: JavaString, value: Bool) in "Java" `{
59 self.putBoolean
(key
, value
);
61 fun put_byte(key: JavaString, value: Int) in "Java" `{
62 self.putByte
(key
, (byte
) value
);
64 # FIXME: Java's `char
` are encoded on 16-bits whereas Nit's are on 8-bits.
65 fun put_char(key: JavaString, value: Char) in "Java" `{
66 self.putChar
(key
, (char
)value
);
68 fun put_short(key: JavaString, value: Int) in "Java" `{
69 self.putShort
(key
, (short
) value
);
71 fun put_int(key: JavaString, value: Int) in "Java" `{
72 self.putInt
(key
, (int
) value
);
74 fun put_long(key: JavaString, value: Int) in "Java" `{
75 self.putLong
(key
, value
);
77 fun put_float(key: JavaString, value: Float) in "Java" `{
78 self.putFloat
(key
, (float
) value
);
80 fun put_double(key: JavaString, value: Float) in "Java" `{
81 self.putDouble
(key
, value
);
83 fun put_string(key: JavaString, value: JavaString) in "Java" `{
84 self.putString
(key
, value
);
86 fun put_char_sequence(key: JavaString, value: JavaString) in "Java" `{
87 self.putCharSequence
(key
, value
);
89 fun put_integer_array_list(key: JavaString, value: Array[Int])
90 import Array[Int].length, Array[Int].[] in "Java" `{
91 ArrayList<Integer> java_array
=
92 new ArrayList<Integer>((int
) Array_of_Int_length(value
));
94 for(int i
=0; i
< java_array
.size
(); ++i
)
95 java_array
.add
((int
) Array_of_Int__index(value
, i
));
97 self.putIntegerArrayList
(key
, java_array
);
99 fun put_string_array_list(key: JavaString, value: Array[JavaString])
100 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
101 ArrayList<String> java_array
= new ArrayList<String>((int
)Array_of_JavaString_length(value
));
103 for(int i
=0; i
< java_array
.size
(); ++i
)
104 java_array
.add
(Array_of_JavaString__index(value
, i
));
106 self.putStringArrayList
(key
, java_array
);
108 fun put_char_sequence_array_list(key: JavaString, value: Array[JavaString])
109 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
110 ArrayList<CharSequence> java_array
=
111 new ArrayList<CharSequence>((int
)Array_of_JavaString_length(value
));
113 for(int i
=0; i
< java_array
.size
(); ++i
)
114 java_array
.add
(Array_of_JavaString__index(value
, i
));
116 self.putCharSequenceArrayList
(key
, java_array
);
118 fun put_boolean_array(key: JavaString, value: Array[Bool])
119 import Array[Bool].length, Array[Bool].[] in "Java" `{
120 boolean
[] java_array
= new boolean
[(int
)Array_of_Bool_length(value
)];
122 for(int i
=0; i
< java_array
.length
; ++i
)
123 java_array
[i
] = Array_of_Bool__index(value
, i
);
125 self.putBooleanArray
(key
, java_array
);
127 fun put_byte_array(key: JavaString, value: Array[Int])
128 import Array[Int].length, Array[Int].[] in "Java" `{
129 byte
[] java_array
= new byte
[(int
)Array_of_Int_length(value
)];
131 for(int i
=0; i
< java_array
.length
; ++i
)
132 java_array
[i
] = (byte
) Array_of_Int__index(value
, i
);
134 self.putByteArray
(key
, java_array
);
136 fun put_short_array(key: JavaString, value: Array[Int])
137 import Array[Int].length, Array[Int].[] in "Java" `{
138 short
[] java_array
= new short
[(int
)Array_of_Int_length(value
)];
140 for(int i
=0; i
< java_array
.length
; ++i
)
141 java_array
[i
] = (short
) Array_of_Int__index(value
, i
);
143 self.putShortArray
(key
, java_array
);
145 # FIXME: Java's `char
` are encoded on 16-bits whereas Nit's are on 8-bits.
146 fun put_char_array(key: JavaString, value: Array[Char])
147 import Array[Char].length, Array[Char].[] in "Java" `{
148 char
[] java_array
= new char
[(int
)Array_of_Char_length(value
)];
150 for(int i
=0; i
< java_array
.length
; ++i
)
151 java_array
[i
] = (char
)Array_of_Char__index(value
, i
);
153 self.putCharArray
(key
, java_array
);
155 fun put_int_array(key: JavaString, value: Array[Int])
156 import Array[Int].length, Array[Int].[] in "Java" `{
157 int
[] java_array
= new int
[(int
)Array_of_Int_length(value
)];
159 for(int i
=0; i
< java_array
.length
; ++i
)
160 java_array
[i
] = (int
) Array_of_Int__index(value
, i
);
162 self.putIntArray
(key
, java_array
);
164 fun put_long_array(key: JavaString, value: Array[Int])
165 import Array[Int].length, Array[Int].[] in "Java" `{
166 long
[] java_array
= new long
[(int
)Array_of_Int_length(value
)];
168 for(int i
=0; i
< java_array
.length
; ++i
)
169 java_array
[i
] = Array_of_Int__index(value
, i
);
171 self.putLongArray
(key
, java_array
);
173 fun put_float_array(key: JavaString, value: Array[Float])
174 import Array[Float].length, Array[Float].[] in "Java" `{
175 float
[] java_array
= new float
[(int
)Array_of_Float_length(value
)];
177 for(int i
=0; i
< java_array
.length
; ++i
)
178 java_array
[i
] = (float
) Array_of_Float__index(value
, i
);
180 self.putFloatArray
(key
, java_array
);
182 fun put_double_array(key: JavaString, value: Array[Float])
183 import Array[Float].length, Array[Float].[] in "Java" `{
184 double
[] java_array
= new double
[(int
)Array_of_Float_length(value
)];
186 for(int i
=0; i
< java_array
.length
; ++i
)
187 java_array
[i
] = Array_of_Float__index(value
, i
);
189 self.putDoubleArray
(key
, java_array
);
191 fun put_string_array(key: JavaString, value: Array[JavaString])
192 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
193 String[] java_array
= new String[(int
)Array_of_JavaString_length(value
)];
195 for(int i
=0; i
< java_array
.length
; ++i
)
196 java_array
[i
] = Array_of_JavaString__index(value
, i
);
198 self.putStringArray
(key
, java_array
);
200 fun put_char_sequence_array(key: JavaString, value: Array[JavaString])
201 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
202 CharSequence[] java_array
= new CharSequence[(int
)Array_of_JavaString_length(value
)];
204 for(int i
=0; i
< java_array
.length
; ++i
)
205 java_array
[i
] = Array_of_JavaString__index(value
, i
);
207 self.putCharSequenceArray
(key
, java_array
);
209 fun put_bundle(key: JavaString, value: NativeBundle) in "Java" `{
210 self.putBundle
(key
, value
);
212 fun get_boolean(key: JavaString): Bool in "Java" `{ return self.getBoolean(key); `}
213 fun get_boolean_with_def_value
(key
: JavaString, def_value
: Bool): Bool in "Java" `{
214 return self.getBoolean(key, def_value);
216 fun get_byte
(key
: JavaString): Int in "Java" `{ return self.getByte(key); `}
217 fun get_byte_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
218 return self.getByte
(key
, (byte
) def_value
);
220 # FIXME: Java's `char
` are encoded on 16-bits whereas Nit's are on 8-bits.
221 fun get_char(key: JavaString): Char in "Java" `{ return (int)self.getChar(key); `}
222 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
223 fun get_char_with_def_value
(key
: JavaString, def_value
: Char): Char in "Java" `{
224 return (int)self.getChar(key, (char)def_value);
226 fun get_short
(key
: JavaString): Int in "Java" `{ return (short) self.getShort(key); `}
227 fun get_short_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
228 return (short
) self.getShort
(key
, (short
) def_value
);
230 fun get_int(key: JavaString): Int in "Java" `{ return self.getInt(key); `}
231 fun get_int_with_def_value
(key
: JavaString, def_value
: Int): Int in "Java" `{
232 return self.getInt(key, (int) def_value);
234 fun get_long
(key
: JavaString): Int in "Java" `{ return self.getLong(key); `}
235 fun get_long_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
236 return self.getLong
(key
);
238 fun get_float(key: JavaString): Float in "Java" `{
239 return (float
) self.getFloat
(key
);
241 fun get_float_with_def_value(key: JavaString, def_value: Float): Float in "Java" `{
242 return (float
) self.getFloat
(key
, (float
) def_value
);
244 fun get_double(key: JavaString): Float in "Java" `{ return self.getDouble(key); `}
245 fun get_double_with_def_value
(key
: JavaString, def_value
: Float): Float in "Java" `{
246 return self.getDouble(key, def_value);
248 fun get_string
(key
: JavaString): JavaString in "Java" `{
249 return self.getString(key);
251 fun get_char_sequence
(key
: JavaString): JavaString in "Java" `{
252 return (String) self.getCharSequence(key);
254 fun get_bundle
(key
: JavaString): NativeBundle in "Java" `{
255 return self.getBundle(key);
257 fun get_integer_array_list
(key
: JavaString): Array[Int]
258 import Array[Int], Array[Int].add
in "Java" `{
259 ArrayList<Integer> java_array = self.getIntegerArrayList(key);
260 int nit_array = new_Array_of_Int();
262 if (java_array == null) return nit_array;
264 for (int element: java_array)
265 Array_of_Int_add(nit_array, element);
269 fun get_string_array_list
(key
: JavaString): Array[String]
270 import StringCopyArray, StringCopyArray.add
, StringCopyArray.collection
in "Java" `{
271 ArrayList<String> java_array = self.getStringArrayList(key);
272 int nit_array = new_StringCopyArray();
274 if (java_array == null) return nit_array;
276 for (String element: java_array)
277 StringCopyArray_add(nit_array, element);
279 return StringCopyArray_collection(nit_array);
281 fun get_char_sequence_array_list
(key
: JavaString): Array[String]
282 import StringCopyArray, StringCopyArray.add
, StringCopyArray.collection
in "Java" `{
283 ArrayList<CharSequence> java_array = self.getCharSequenceArrayList(key);
284 int nit_array = new_StringCopyArray();
286 if (java_array == null) return nit_array;
288 for (CharSequence element: java_array)
289 StringCopyArray_add(nit_array, (String) element);
291 return StringCopyArray_collection(nit_array);
293 fun get_boolean_array
(key
: JavaString): Array[Bool]
294 import Array[Bool], Array[Bool].add
in "Java" `{
295 boolean[] java_array = self.getBooleanArray(key);
296 int nit_array = new_Array_of_Bool();
298 if (java_array == null) return nit_array;
300 for (int i=0; i < java_array.length; ++i)
301 Array_of_Bool_add(nit_array, java_array[i]);
305 fun get_byte_array
(key
: JavaString): Array[Int]
306 import Array[Int], Array[Int].add
in "Java" `{
307 byte[] java_array = self.getByteArray(key);
308 int nit_array = new_Array_of_Int();
310 if (java_array == null) return nit_array;
312 for(int i=0; i < java_array.length; ++i)
313 Array_of_Int_add(nit_array, java_array[i]);
317 fun get_short_array
(key
: JavaString): Array[Int]
318 import Array[Int], Array[Int].add
in "Java" `{
319 short[] java_array = self.getShortArray(key);
320 int nit_array = new_Array_of_Int();
322 if (java_array == null) return nit_array;
324 for(int i=0; i < java_array.length; ++i)
325 Array_of_Int_add(nit_array, java_array[i]);
329 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
330 fun get_char_array
(key
: JavaString): Array[Char]
331 import Array[Char], Array[Char].add
in "Java" `{
332 char[] java_array = self.getCharArray(key);
333 int nit_array = new_Array_of_Char();
335 if (java_array == null) return nit_array;
337 for(int i=0; i < java_array.length; ++i)
338 Array_of_Char_add(nit_array, (int)java_array[i]);
342 fun get_int_array
(key
: JavaString): Array[Int]
343 import Array[Int], Array[Int].add
in "Java" `{
344 int[] java_array = self.getIntArray(key);
345 int nit_array = new_Array_of_Int();
347 if (java_array == null) return nit_array;
349 for(int i=0; i < java_array.length; ++i)
350 Array_of_Int_add(nit_array, java_array[i]);
354 # FIXME: Get rid of the int cast as soon as the ffi is fixed
355 fun get_long_array
(key
: JavaString): Array[Int]
356 import Array[Int], Array[Int].add
in "Java" `{
357 long[] java_array = self.getLongArray(key);
358 int nit_array = new_Array_of_Int();
360 if (java_array == null) return nit_array;
362 for(int i=0; i < java_array.length; ++i)
363 Array_of_Int_add(nit_array, java_array[i]);
367 fun get_float_array
(key
: JavaString): Array[Float]
368 import Array[Float], Array[Float].add
in "Java" `{
369 float[] java_array = self.getFloatArray(key);
370 int nit_array = new_Array_of_Float();
372 if (java_array == null) return nit_array;
374 for(int i=0; i < java_array.length; ++i)
375 Array_of_Float_add(nit_array, (double) java_array[i]);
379 fun get_double_array
(key
: JavaString): Array[Float]
380 import Array[Float], Array[Float].add
in "Java" `{
381 double[] java_array = self.getDoubleArray(key);
382 int nit_array = new_Array_of_Float();
384 if (java_array == null) return nit_array;
386 for(int i=0; i < java_array.length; ++i)
387 Array_of_Float_add(nit_array, java_array[i]);
391 fun get_string_array
(key
: JavaString): Array[String]
392 import StringCopyArray, StringCopyArray.add
, StringCopyArray.collection
in "Java" `{
393 String[] java_array = self.getStringArray(key);
394 int nit_array = new_StringCopyArray();
396 if (java_array == null) return nit_array;
398 for(int i=0; i < java_array.length; ++i)
399 StringCopyArray_add(nit_array, java_array[i]);
401 return StringCopyArray_collection(nit_array);
403 fun get_char_sequence_array
(key
: JavaString): Array[String]
404 import StringCopyArray, StringCopyArray.add
, StringCopyArray.collection
in "Java" `{
405 CharSequence[] java_array = self.getCharSequenceArray(key);
406 int nit_array = new_StringCopyArray();
408 if (java_array == null) return nit_array;
410 for(int i=0; i < java_array.length; ++i)
411 StringCopyArray_add(nit_array, (String)java_array[i]);
413 return StringCopyArray_collection(nit_array);
415 fun describe_contents
: Int in "Java" `{ return self.describeContents(); `}
416 fun to_string: JavaString in "Java" `{ return self.toString(); `}
419 redef fun new_global_ref
import sys
, Sys.jni_env
`{
420 Sys sys = NativeBundle_sys(self);
421 JNIEnv *env = Sys_jni_env(sys);
422 return (*env)->NewGlobalRef(env, self);
426 # A class mapping `String` keys to various value types
428 private var native_bundle
: NativeBundle = (new NativeBundle).new_global_ref
is lazy
430 # Get a new `Bundle` wrapping `native_bundle`
431 init from
(native_bundle
: NativeBundle) do self.native_bundle
= native_bundle
433 # Returns `true` if the Bundle contains this key
434 fun has
(key
: String): Bool
436 sys
.jni_env
.push_local_frame
(1)
437 var return_value
= native_bundle
.contains_key
(key
.to_java_string
)
438 sys
.jni_env
.pop_local_frame
442 # Returns the number of entries in the current `Bundle`
443 fun size
: Int do return native_bundle
.size
445 # Returns true if the current `Bundle` is empty
446 fun is_empty
: Bool do return native_bundle
.is_empty
449 fun clear
do native_bundle
.clear
451 # Removes the entry associated with the given key
452 fun remove
(key
: String)
454 sys
.jni_env
.push_local_frame
(1)
455 native_bundle
.remove
(key
.to_java_string
)
456 sys
.jni_env
.pop_local_frame
459 # Returns a `HashSet[String]` containing every mapping keys in the current
461 fun keys
: HashSet[String]
463 var javastring_set
= native_bundle
.key_set
464 var string_set
= new HashSet[String]
466 for element
in javastring_set
do
467 string_set
.add
(element
.to_s
)
473 # Add key-value information by dynamically choosing the appropriate
474 # java method according to value type
475 # If there's already a value associated with this key, the new value
478 # To retrieve entries, you'll have to call the type corresponding method
479 # conforming to these rules:
481 # | Nit type | corresponding getter |
482 # |:----------------------|:--------------------------------|
484 # | `Float` | `double` |
485 # | `Bool` | `bool` |
486 # | `Char` | `char` |
487 # | `String` | `string` |
488 # | `Serializable` | `deserialize` |
489 # | `Array[Int]` | `array_of_long` |
490 # | `Array[Float]` | `array_of_double` |
491 # | `Array[Bool]` | `array_of_bool` |
492 # | `Array[Char]` | `array_of_char` |
493 # | `Array[String]` | `array_of_string` |
494 # | `Array[Serializable]` | `deserialize_array` |
495 fun []=(key
: String, value
: Serializable): Bundle
497 sys
.jni_env
.push_local_frame
(1)
498 value
.add_to_bundle
(self.native_bundle
, key
.to_java_string
)
499 sys
.jni_env
.pop_local_frame
503 # Retrieve an `Object` serialized via `[]=` function
504 # Returns `null` if there's no serialized object corresponding to the given key
505 # or if it's the wrong value type
506 # Make sure that the serialized object is `serialize` or that it
507 # redefines the appropriate methods. Refer to `Serializable` documentation
508 # for further details
509 fun deserialize
(key
: String): nullable Object
511 var serialized_string
= self.string
(key
)
513 if serialized_string
== null then return null
515 var deserializer
= new JsonDeserializer(serialized_string
)
517 return deserializer
.deserialize
520 # Retrieve an `Array` of `Object` serialized via `[]=` function
521 # Returns `null` if there's no serialized `Array` corresponding to the given key
522 # or if it's the wrong value type
523 # Make sure that the serialized objects are `serialize` or that they
524 # redefine the appropriate methods. Refer to `Serializable` documentation
525 # for further details
526 fun deserialize_array
(key
: String): nullable Array[nullable Object]
528 var serialized_array
= self.array_of_string
(key
)
530 if serialized_array
== null then return null
532 var deserialized_array
= new Array[nullable Object]
534 for serialized_element
in serialized_array
do
535 var deserializer
= new JsonDeserializer(serialized_element
)
536 deserialized_array
.add
(deserializer
.deserialize
)
539 return deserialized_array
542 # Retrieves the `String` value corresponding to the given key
543 # Returns `null` if none or if it's the wrong value type
544 fun string
(key
: String): nullable String
546 sys
.jni_env
.push_local_frame
(2)
548 var jstr
= native_bundle
.get_string
(key
.to_java_string
)
550 if not jstr
.is_java_null
then str
= jstr
.to_s
552 sys
.jni_env
.pop_local_frame
557 # Retrieves the `Bool` value corresponding to the given key
558 # Returns the `def_value` if none or if it's the wrong value type
559 fun bool
(key
: String, def_value
: Bool): Bool
561 sys
.jni_env
.push_local_frame
(1)
563 native_bundle
.get_boolean_with_def_value
(key
.to_java_string
, def_value
)
564 sys
.jni_env
.pop_local_frame
568 # Retrieves the `Char` value corresponding to the given key
569 # Returns the `def_value` if none or if it's the wrong value type
570 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
571 fun char
(key
: String, def_value
: Char): Char
573 sys
.jni_env
.push_local_frame
(1)
575 native_bundle
.get_char_with_def_value
(key
.to_java_string
, def_value
)
576 sys
.jni_env
.pop_local_frame
580 # Retrieves the `Int` value corresponding to the given key
581 # Returns the `def_value` if none or if it's the wrong value type
582 fun int
(key
: String, def_value
: Int): Int
584 sys
.jni_env
.push_local_frame
(1)
586 native_bundle
.get_long_with_def_value
(key
.to_java_string
, def_value
)
587 sys
.jni_env
.pop_local_frame
591 # Retrieves the `Float` value corresponding to the given key
592 # Returns the `def_value` if none or if it's the wrong value type
593 fun float
(key
: String, def_value
: Float): Float
595 sys
.jni_env
.push_local_frame
(1)
597 native_bundle
.get_double_with_def_value
(key
.to_java_string
, def_value
)
598 sys
.jni_env
.pop_local_frame
602 # Retrieves the `Array[Float]` value corresponding to the given key
603 # Returns the `null` if none or if it's the wrong value type
604 fun array_of_float
(key
: String): nullable Array[Float]
606 sys
.jni_env
.push_local_frame
(1)
607 var return_value
= native_bundle
.get_double_array
(key
.to_java_string
)
608 sys
.jni_env
.pop_local_frame
610 if return_value
.is_empty
then return null
615 # Retrieves the `Array[Char]` value corresponding to the given key
616 # Returns the `null` if none or if it's the wrong value type
617 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
618 fun array_of_char
(key
: String): nullable Array[Char]
620 sys
.jni_env
.push_local_frame
(1)
621 var return_value
= native_bundle
.get_char_array
(key
.to_java_string
)
622 sys
.jni_env
.pop_local_frame
624 if return_value
.is_empty
then return null
628 # Retrieves the `Array[Int]` value corresponding to the given key
629 # Returns the `null` if none or if it's the wrong value type
630 fun array_of_int
(key
: String): nullable Array[Int]
632 sys
.jni_env
.push_local_frame
(1)
633 var return_value
= native_bundle
.get_long_array
(key
.to_java_string
)
634 sys
.jni_env
.pop_local_frame
636 if return_value
.is_empty
then return null
641 # Retrieves the `Array[Bool]` value corresponding to the given key
642 # Returns the `null` if none or if it's the wrong value type
643 fun array_of_bool
(key
: String): nullable Array[Bool]
645 sys
.jni_env
.push_local_frame
(1)
646 var return_value
= native_bundle
.get_boolean_array
(key
.to_java_string
)
647 sys
.jni_env
.pop_local_frame
649 if return_value
.is_empty
then return null
654 # Retrieves the `Array[String]` value corresponding to the given key
655 # Returns the `null` if none or if it's the wrong value type
656 fun array_of_string
(key
: String): nullable Array[String]
658 sys
.jni_env
.push_local_frame
(1)
660 var return_value
= native_bundle
.get_string_array
(key
.to_java_string
)
661 sys
.jni_env
.pop_local_frame
663 if return_value
.is_empty
then return null
669 redef class Serializable
670 # Called by `Bundle::[]=` to dynamically choose the appropriate method according
671 # to the value type to store
672 # Non-primitive Object (`String` excluded) will be stored as a serialized json `String`
673 # Refine your class to customize this method behaviour
674 protected fun add_to_bundle
(bundle
: NativeBundle, key
: JavaString)
676 sys
.jni_env
.push_local_frame
(1)
677 var serialized_string
= new StringWriter
678 var serializer
= new JsonSerializer(serialized_string
)
679 serializer
.serialize
(self)
681 bundle
.put_string
(key
, serialized_string
.to_s
.to_java_string
)
686 redef fun add_to_bundle
(bundle
, key
)
688 bundle
.put_long
(key
, self)
693 redef fun add_to_bundle
(bundle
, key
)
695 bundle
.put_char
(key
, self)
700 redef fun add_to_bundle
(bundle
, key
)
702 bundle
.put_double
(key
, self)
707 redef fun add_to_bundle
(bundle
, key
)
709 bundle
.put_boolean
(key
, self)
714 redef fun add_to_bundle
(bundle
, key
)
716 bundle
.put_string
(key
, self.to_java_string
)
721 redef fun add_to_bundle
(bundle
, key
)
723 if self isa Array[Bool] then
724 bundle
.put_boolean_array
(key
, self)
725 else if self isa Array[Int] then
726 bundle
.put_long_array
(key
, self)
727 else if self isa Array[Float] then
728 bundle
.put_double_array
(key
, self)
729 else if self isa Array[Char] then
730 bundle
.put_char_array
(key
, self)
731 else if self isa Array[String] then
732 sys
.jni_env
.push_local_frame
(self.length
)
733 var java_string_array
= new Array[JavaString]
735 for element
in self do
736 java_string_array
.push
(element
.to_s
.to_java_string
)
739 bundle
.put_string_array
(key
, java_string_array
)
740 else if self isa Array[Serializable] then
741 sys
.jni_env
.push_local_frame
(self.length
)
742 var java_string_array
= new Array[JavaString]
744 for element
in self do
745 var serialized_string
= new StringWriter
746 var serializer
= new JsonSerializer(serialized_string
)
747 serializer
.serialize
(element
)
748 java_string_array
.add
(serialized_string
.to_s
.to_java_string
)
751 bundle
.put_string_array
(key
, java_string_array
)
756 # Allows JavaString collection copy through FFI with Java
757 private class StringCopyArray
758 var collection
= new Array[String]
759 fun add
(element
: JavaString) do collection
.add element
.to_s