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
28 import android.os.Bundle;
29 import android.app.Activity;
30 import java.util.ArrayList;
32 import nit.app.NitObject;
35 extern class NativeBundle in "Java" `{ android.os.Bundle `}
38 new in "Java" `{ return new Bundle(); `}
40 fun clone
: JavaObject in "Java" `{ return self.clone(); `}
41 fun size: Int in "Java" `{ return self.size(); `}
42 fun is_empty
: Bool in "Java" `{ return self.isEmpty(); `}
43 fun clear in "Java" `{ self.clear(); `}
44 fun contains_key
(key
: JavaString): Bool in "Java" `{ return self.containsKey(key); `}
45 fun get(key: JavaString): JavaObject in "Java" `{ return self.get(key); `}
46 fun remove
(key
: JavaString) in "Java" `{ self.remove(key); `}
47 fun put_all(bundle: NativeBundle) in "Java" `{ self.putAll(bundle); `}
48 fun key_set
: HashSet[JavaString] import HashSet[JavaString],
49 HashSet[JavaString].add
in "Java" `{
50 Set<String> java_set = self.keySet();
51 NitObject nit_hashset = new_HashSet_of_JavaString();
53 for (String element: java_set)
54 HashSet_of_JavaString_add(nit_hashset, element);
58 fun has_file_descriptors
: Bool in "Java" `{ return self.hasFileDescriptors(); `}
59 fun put_boolean(key: JavaString, value: Bool) in "Java" `{
60 self.putBoolean
(key
, value
);
62 fun put_byte(key: JavaString, value: Int) in "Java" `{
63 self.putByte
(key
, (byte
) value
);
65 # FIXME: Java's `char
` are encoded on 16-bits whereas Nit's are on 8-bits.
66 fun put_char(key: JavaString, value: Char) in "Java" `{
67 self.putChar
(key
, (char
)value
);
69 fun put_short(key: JavaString, value: Int) in "Java" `{
70 self.putShort
(key
, (short
) value
);
72 fun put_int(key: JavaString, value: Int) in "Java" `{
73 self.putInt
(key
, (int
) value
);
75 fun put_long(key: JavaString, value: Int) in "Java" `{
76 self.putLong
(key
, value
);
78 fun put_float(key: JavaString, value: Float) in "Java" `{
79 self.putFloat
(key
, (float
) value
);
81 fun put_double(key: JavaString, value: Float) in "Java" `{
82 self.putDouble
(key
, value
);
84 fun put_string(key: JavaString, value: JavaString) in "Java" `{
85 self.putString
(key
, value
);
87 fun put_char_sequence(key: JavaString, value: JavaString) in "Java" `{
88 self.putCharSequence
(key
, value
);
90 fun put_integer_array_list(key: JavaString, value: Array[Int])
91 import Array[Int].length, Array[Int].[] in "Java" `{
92 ArrayList<Integer> java_array
=
93 new ArrayList<Integer>((int
) Array_of_Int_length(value
));
95 for(int i
=0; i
< java_array
.size
(); ++i
)
96 java_array
.add
((int
) Array_of_Int__index(value
, i
));
98 self.putIntegerArrayList
(key
, java_array
);
100 fun put_string_array_list(key: JavaString, value: Array[JavaString])
101 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
102 ArrayList<String> java_array
= new ArrayList<String>((int
)Array_of_JavaString_length(value
));
104 for(int i
=0; i
< java_array
.size
(); ++i
)
105 java_array
.add
(Array_of_JavaString__index(value
, i
));
107 self.putStringArrayList
(key
, java_array
);
109 fun put_char_sequence_array_list(key: JavaString, value: Array[JavaString])
110 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
111 ArrayList<CharSequence> java_array
=
112 new ArrayList<CharSequence>((int
)Array_of_JavaString_length(value
));
114 for(int i
=0; i
< java_array
.size
(); ++i
)
115 java_array
.add
(Array_of_JavaString__index(value
, i
));
117 self.putCharSequenceArrayList
(key
, java_array
);
119 fun put_boolean_array(key: JavaString, value: Array[Bool])
120 import Array[Bool].length, Array[Bool].[] in "Java" `{
121 boolean
[] java_array
= new boolean
[(int
)Array_of_Bool_length(value
)];
123 for(int i
=0; i
< java_array
.length
; ++i
)
124 java_array
[i
] = Array_of_Bool__index(value
, i
);
126 self.putBooleanArray
(key
, java_array
);
128 fun put_byte_array(key: JavaString, value: Array[Int])
129 import Array[Int].length, Array[Int].[] in "Java" `{
130 byte
[] java_array
= new byte
[(int
)Array_of_Int_length(value
)];
132 for(int i
=0; i
< java_array
.length
; ++i
)
133 java_array
[i
] = (byte
) Array_of_Int__index(value
, i
);
135 self.putByteArray
(key
, java_array
);
137 fun put_short_array(key: JavaString, value: Array[Int])
138 import Array[Int].length, Array[Int].[] in "Java" `{
139 short
[] java_array
= new short
[(int
)Array_of_Int_length(value
)];
141 for(int i
=0; i
< java_array
.length
; ++i
)
142 java_array
[i
] = (short
) Array_of_Int__index(value
, i
);
144 self.putShortArray
(key
, java_array
);
146 # FIXME: Java's `char
` are encoded on 16-bits whereas Nit's are on 8-bits.
147 fun put_char_array(key: JavaString, value: Array[Char])
148 import Array[Char].length, Array[Char].[] in "Java" `{
149 char
[] java_array
= new char
[(int
)Array_of_Char_length(value
)];
151 for(int i
=0; i
< java_array
.length
; ++i
)
152 java_array
[i
] = (char
)Array_of_Char__index(value
, i
);
154 self.putCharArray
(key
, java_array
);
156 fun put_int_array(key: JavaString, value: Array[Int])
157 import Array[Int].length, Array[Int].[] in "Java" `{
158 int
[] java_array
= new int
[(int
)Array_of_Int_length(value
)];
160 for(int i
=0; i
< java_array
.length
; ++i
)
161 java_array
[i
] = (int
) Array_of_Int__index(value
, i
);
163 self.putIntArray
(key
, java_array
);
165 fun put_long_array(key: JavaString, value: Array[Int])
166 import Array[Int].length, Array[Int].[] in "Java" `{
167 long
[] java_array
= new long
[(int
)Array_of_Int_length(value
)];
169 for(int i
=0; i
< java_array
.length
; ++i
)
170 java_array
[i
] = Array_of_Int__index(value
, i
);
172 self.putLongArray
(key
, java_array
);
174 fun put_float_array(key: JavaString, value: Array[Float])
175 import Array[Float].length, Array[Float].[] in "Java" `{
176 float
[] java_array
= new float
[(int
)Array_of_Float_length(value
)];
178 for(int i
=0; i
< java_array
.length
; ++i
)
179 java_array
[i
] = (float
) Array_of_Float__index(value
, i
);
181 self.putFloatArray
(key
, java_array
);
183 fun put_double_array(key: JavaString, value: Array[Float])
184 import Array[Float].length, Array[Float].[] in "Java" `{
185 double
[] java_array
= new double
[(int
)Array_of_Float_length(value
)];
187 for(int i
=0; i
< java_array
.length
; ++i
)
188 java_array
[i
] = Array_of_Float__index(value
, i
);
190 self.putDoubleArray
(key
, java_array
);
192 fun put_string_array(key: JavaString, value: Array[JavaString])
193 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
194 String[] java_array
= new String[(int
)Array_of_JavaString_length(value
)];
196 for(int i
=0; i
< java_array
.length
; ++i
)
197 java_array
[i
] = Array_of_JavaString__index(value
, i
);
199 self.putStringArray
(key
, java_array
);
201 fun put_char_sequence_array(key: JavaString, value: Array[JavaString])
202 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
203 CharSequence[] java_array
= new CharSequence[(int
)Array_of_JavaString_length(value
)];
205 for(int i
=0; i
< java_array
.length
; ++i
)
206 java_array
[i
] = Array_of_JavaString__index(value
, i
);
208 self.putCharSequenceArray
(key
, java_array
);
210 fun put_bundle(key: JavaString, value: NativeBundle) in "Java" `{
211 self.putBundle
(key
, value
);
213 fun get_boolean(key: JavaString): Bool in "Java" `{ return self.getBoolean(key); `}
214 fun get_boolean_with_def_value
(key
: JavaString, def_value
: Bool): Bool in "Java" `{
215 return self.getBoolean(key, def_value);
217 fun get_byte
(key
: JavaString): Int in "Java" `{ return self.getByte(key); `}
218 fun get_byte_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
219 return self.getByte
(key
, (byte
) def_value
);
221 # FIXME: Java's `char
` are encoded on 16-bits whereas Nit's are on 8-bits.
222 fun get_char(key: JavaString): Char in "Java" `{ return (int)self.getChar(key); `}
223 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
224 fun get_char_with_def_value
(key
: JavaString, def_value
: Char): Char in "Java" `{
225 return (int)self.getChar(key, (char)def_value);
227 fun get_short
(key
: JavaString): Int in "Java" `{ return (short) self.getShort(key); `}
228 fun get_short_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
229 return (short
) self.getShort
(key
, (short
) def_value
);
231 fun get_int(key: JavaString): Int in "Java" `{ return self.getInt(key); `}
232 fun get_int_with_def_value
(key
: JavaString, def_value
: Int): Int in "Java" `{
233 return self.getInt(key, (int) def_value);
235 fun get_long
(key
: JavaString): Int in "Java" `{ return self.getLong(key); `}
236 fun get_long_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
237 return self.getLong
(key
);
239 fun get_float(key: JavaString): Float in "Java" `{
240 return (float
) self.getFloat
(key
);
242 fun get_float_with_def_value(key: JavaString, def_value: Float): Float in "Java" `{
243 return (float
) self.getFloat
(key
, (float
) def_value
);
245 fun get_double(key: JavaString): Float in "Java" `{ return self.getDouble(key); `}
246 fun get_double_with_def_value
(key
: JavaString, def_value
: Float): Float in "Java" `{
247 return self.getDouble(key, def_value);
249 fun get_string
(key
: JavaString): JavaString in "Java" `{
250 return self.getString(key);
252 fun get_char_sequence
(key
: JavaString): JavaString in "Java" `{
253 return (String) self.getCharSequence(key);
255 fun get_bundle
(key
: JavaString): NativeBundle in "Java" `{
256 return self.getBundle(key);
258 fun get_integer_array_list
(key
: JavaString): Array[Int]
259 import Array[Int], Array[Int].add
in "Java" `{
260 ArrayList<Integer> java_array = self.getIntegerArrayList(key);
261 NitObject nit_array = new_Array_of_Int();
263 if (java_array == null) return nit_array;
265 for (int element: java_array)
266 Array_of_Int_add(nit_array, element);
270 fun get_string_array_list
(key
: JavaString): Array[String]
271 import StringCopyArray, StringCopyArray.add
, StringCopyArray.collection
in "Java" `{
272 ArrayList<String> java_array = self.getStringArrayList(key);
273 NitObject nit_array = new_StringCopyArray();
275 if (java_array == null) return nit_array;
277 for (String element: java_array)
278 StringCopyArray_add(nit_array, element);
280 return StringCopyArray_collection(nit_array);
282 fun get_char_sequence_array_list
(key
: JavaString): Array[String]
283 import StringCopyArray, StringCopyArray.add
, StringCopyArray.collection
in "Java" `{
284 ArrayList<CharSequence> java_array = self.getCharSequenceArrayList(key);
285 NitObject nit_array = new_StringCopyArray();
287 if (java_array == null) return nit_array;
289 for (CharSequence element: java_array)
290 StringCopyArray_add(nit_array, (String) element);
292 return StringCopyArray_collection(nit_array);
294 fun get_boolean_array
(key
: JavaString): Array[Bool]
295 import Array[Bool], Array[Bool].add
in "Java" `{
296 boolean[] java_array = self.getBooleanArray(key);
297 NitObject nit_array = new_Array_of_Bool();
299 if (java_array == null) return nit_array;
301 for (int i=0; i < java_array.length; ++i)
302 Array_of_Bool_add(nit_array, java_array[i]);
306 fun get_byte_array
(key
: JavaString): Array[Int]
307 import Array[Int], Array[Int].add
in "Java" `{
308 byte[] java_array = self.getByteArray(key);
309 NitObject nit_array = new_Array_of_Int();
311 if (java_array == null) return nit_array;
313 for(int i=0; i < java_array.length; ++i)
314 Array_of_Int_add(nit_array, java_array[i]);
318 fun get_short_array
(key
: JavaString): Array[Int]
319 import Array[Int], Array[Int].add
in "Java" `{
320 short[] java_array = self.getShortArray(key);
321 NitObject nit_array = new_Array_of_Int();
323 if (java_array == null) return nit_array;
325 for(int i=0; i < java_array.length; ++i)
326 Array_of_Int_add(nit_array, java_array[i]);
330 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
331 fun get_char_array
(key
: JavaString): Array[Char]
332 import Array[Char], Array[Char].add
in "Java" `{
333 char[] java_array = self.getCharArray(key);
334 NitObject nit_array = new_Array_of_Char();
336 if (java_array == null) return nit_array;
338 for(int i=0; i < java_array.length; ++i)
339 Array_of_Char_add(nit_array, (int)java_array[i]);
343 fun get_int_array
(key
: JavaString): Array[Int]
344 import Array[Int], Array[Int].add
in "Java" `{
345 int[] java_array = self.getIntArray(key);
346 NitObject nit_array = new_Array_of_Int();
348 if (java_array == null) return nit_array;
350 for(int i=0; i < java_array.length; ++i)
351 Array_of_Int_add(nit_array, java_array[i]);
355 # FIXME: Get rid of the int cast as soon as the ffi is fixed
356 fun get_long_array
(key
: JavaString): Array[Int]
357 import Array[Int], Array[Int].add
in "Java" `{
358 long[] java_array = self.getLongArray(key);
359 NitObject nit_array = new_Array_of_Int();
361 if (java_array == null) return nit_array;
363 for(int i=0; i < java_array.length; ++i)
364 Array_of_Int_add(nit_array, java_array[i]);
368 fun get_float_array
(key
: JavaString): Array[Float]
369 import Array[Float], Array[Float].add
in "Java" `{
370 float[] java_array = self.getFloatArray(key);
371 NitObject nit_array = new_Array_of_Float();
373 if (java_array == null) return nit_array;
375 for(int i=0; i < java_array.length; ++i)
376 Array_of_Float_add(nit_array, (double) java_array[i]);
380 fun get_double_array
(key
: JavaString): Array[Float]
381 import Array[Float], Array[Float].add
in "Java" `{
382 double[] java_array = self.getDoubleArray(key);
383 NitObject nit_array = new_Array_of_Float();
385 if (java_array == null) return nit_array;
387 for(int i=0; i < java_array.length; ++i)
388 Array_of_Float_add(nit_array, java_array[i]);
392 fun get_string_array
(key
: JavaString): Array[String]
393 import StringCopyArray, StringCopyArray.add
, StringCopyArray.collection
in "Java" `{
394 String[] java_array = self.getStringArray(key);
395 NitObject nit_array = new_StringCopyArray();
397 if (java_array == null) return nit_array;
399 for(int i=0; i < java_array.length; ++i)
400 StringCopyArray_add(nit_array, java_array[i]);
402 return StringCopyArray_collection(nit_array);
404 fun get_char_sequence_array
(key
: JavaString): Array[String]
405 import StringCopyArray, StringCopyArray.add
, StringCopyArray.collection
in "Java" `{
406 CharSequence[] java_array = self.getCharSequenceArray(key);
407 NitObject nit_array = new_StringCopyArray();
409 if (java_array == null) return nit_array;
411 for(int i=0; i < java_array.length; ++i)
412 StringCopyArray_add(nit_array, (String)java_array[i]);
414 return StringCopyArray_collection(nit_array);
416 fun describe_contents
: Int in "Java" `{ return self.describeContents(); `}
417 fun to_string: JavaString in "Java" `{ return self.toString(); `}
420 redef fun new_global_ref
import sys
, Sys.jni_env
`{
421 Sys sys = NativeBundle_sys(self);
422 JNIEnv *env = Sys_jni_env(sys);
423 return (*env)->NewGlobalRef(env, self);
427 # A class mapping `String` keys to various value types
429 private var native_bundle
: NativeBundle = (new NativeBundle).new_global_ref
is lazy
431 # Get a new `Bundle` wrapping `native_bundle`
432 init from
(native_bundle
: NativeBundle) do self.native_bundle
= native_bundle
434 # Returns `true` if the Bundle contains this key
435 fun has
(key
: String): Bool
437 sys
.jni_env
.push_local_frame
(1)
438 var return_value
= native_bundle
.contains_key
(key
.to_java_string
)
439 sys
.jni_env
.pop_local_frame
443 # Returns the number of entries in the current `Bundle`
444 fun size
: Int do return native_bundle
.size
446 # Returns true if the current `Bundle` is empty
447 fun is_empty
: Bool do return native_bundle
.is_empty
450 fun clear
do native_bundle
.clear
452 # Removes the entry associated with the given key
453 fun remove
(key
: String)
455 sys
.jni_env
.push_local_frame
(1)
456 native_bundle
.remove
(key
.to_java_string
)
457 sys
.jni_env
.pop_local_frame
460 # Returns a `HashSet[String]` containing every mapping keys in the current
462 fun keys
: HashSet[String]
464 var javastring_set
= native_bundle
.key_set
465 var string_set
= new HashSet[String]
467 for element
in javastring_set
do
468 string_set
.add
(element
.to_s
)
474 # Add key-value information by dynamically choosing the appropriate
475 # java method according to value type
476 # If there's already a value associated with this key, the new value
479 # To retrieve entries, you'll have to call the type corresponding method
480 # conforming to these rules:
482 # | Nit type | corresponding getter |
483 # |:----------------------|:--------------------------------|
485 # | `Float` | `double` |
486 # | `Bool` | `bool` |
487 # | `Char` | `char` |
488 # | `String` | `string` |
489 # | `Serializable` | `deserialize` |
490 # | `Array[Int]` | `array_of_long` |
491 # | `Array[Float]` | `array_of_double` |
492 # | `Array[Bool]` | `array_of_bool` |
493 # | `Array[Char]` | `array_of_char` |
494 # | `Array[String]` | `array_of_string` |
495 # | `Array[Serializable]` | `deserialize_array` |
496 fun []=(key
: String, value
: Serializable): Bundle
498 sys
.jni_env
.push_local_frame
(1)
499 value
.add_to_bundle
(self.native_bundle
, key
.to_java_string
)
500 sys
.jni_env
.pop_local_frame
504 # Retrieve an `Object` serialized via `[]=` function
505 # Returns `null` if there's no serialized object corresponding to the given key
506 # or if it's the wrong value type
507 # Make sure that the serialized object is `serialize` or that it
508 # redefines the appropriate methods. Refer to `Serializable` documentation
509 # for further details
510 fun deserialize
(key
: String): nullable Object
512 var serialized_string
= self.string
(key
)
514 if serialized_string
== null then return null
516 var deserializer
= new JsonDeserializer(serialized_string
)
518 return deserializer
.deserialize
521 # Retrieve an `Array` of `Object` serialized via `[]=` function
522 # Returns `null` if there's no serialized `Array` corresponding to the given key
523 # or if it's the wrong value type
524 # Make sure that the serialized objects are `serialize` or that they
525 # redefine the appropriate methods. Refer to `Serializable` documentation
526 # for further details
527 fun deserialize_array
(key
: String): nullable Array[nullable Object]
529 var serialized_array
= self.array_of_string
(key
)
531 if serialized_array
== null then return null
533 var deserialized_array
= new Array[nullable Object]
535 for serialized_element
in serialized_array
do
536 var deserializer
= new JsonDeserializer(serialized_element
)
537 deserialized_array
.add
(deserializer
.deserialize
)
540 return deserialized_array
543 # Retrieves the `String` value corresponding to the given key
544 # Returns `null` if none or if it's the wrong value type
545 fun string
(key
: String): nullable String
547 sys
.jni_env
.push_local_frame
(2)
549 var jstr
= native_bundle
.get_string
(key
.to_java_string
)
551 if not jstr
.is_java_null
then str
= jstr
.to_s
553 sys
.jni_env
.pop_local_frame
558 # Retrieves the `Bool` value corresponding to the given key
559 # Returns the `def_value` if none or if it's the wrong value type
560 fun bool
(key
: String, def_value
: Bool): Bool
562 sys
.jni_env
.push_local_frame
(1)
564 native_bundle
.get_boolean_with_def_value
(key
.to_java_string
, def_value
)
565 sys
.jni_env
.pop_local_frame
569 # Retrieves the `Char` value corresponding to the given key
570 # Returns the `def_value` if none or if it's the wrong value type
571 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
572 fun char
(key
: String, def_value
: Char): Char
574 sys
.jni_env
.push_local_frame
(1)
576 native_bundle
.get_char_with_def_value
(key
.to_java_string
, def_value
)
577 sys
.jni_env
.pop_local_frame
581 # Retrieves the `Int` value corresponding to the given key
582 # Returns the `def_value` if none or if it's the wrong value type
583 fun int
(key
: String, def_value
: Int): Int
585 sys
.jni_env
.push_local_frame
(1)
587 native_bundle
.get_long_with_def_value
(key
.to_java_string
, def_value
)
588 sys
.jni_env
.pop_local_frame
592 # Retrieves the `Float` value corresponding to the given key
593 # Returns the `def_value` if none or if it's the wrong value type
594 fun float
(key
: String, def_value
: Float): Float
596 sys
.jni_env
.push_local_frame
(1)
598 native_bundle
.get_double_with_def_value
(key
.to_java_string
, def_value
)
599 sys
.jni_env
.pop_local_frame
603 # Retrieves the `Array[Float]` value corresponding to the given key
604 # Returns the `null` if none or if it's the wrong value type
605 fun array_of_float
(key
: String): nullable Array[Float]
607 sys
.jni_env
.push_local_frame
(1)
608 var return_value
= native_bundle
.get_double_array
(key
.to_java_string
)
609 sys
.jni_env
.pop_local_frame
611 if return_value
.is_empty
then return null
616 # Retrieves the `Array[Char]` value corresponding to the given key
617 # Returns the `null` if none or if it's the wrong value type
618 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
619 fun array_of_char
(key
: String): nullable Array[Char]
621 sys
.jni_env
.push_local_frame
(1)
622 var return_value
= native_bundle
.get_char_array
(key
.to_java_string
)
623 sys
.jni_env
.pop_local_frame
625 if return_value
.is_empty
then return null
629 # Retrieves the `Array[Int]` value corresponding to the given key
630 # Returns the `null` if none or if it's the wrong value type
631 fun array_of_int
(key
: String): nullable Array[Int]
633 sys
.jni_env
.push_local_frame
(1)
634 var return_value
= native_bundle
.get_long_array
(key
.to_java_string
)
635 sys
.jni_env
.pop_local_frame
637 if return_value
.is_empty
then return null
642 # Retrieves the `Array[Bool]` value corresponding to the given key
643 # Returns the `null` if none or if it's the wrong value type
644 fun array_of_bool
(key
: String): nullable Array[Bool]
646 sys
.jni_env
.push_local_frame
(1)
647 var return_value
= native_bundle
.get_boolean_array
(key
.to_java_string
)
648 sys
.jni_env
.pop_local_frame
650 if return_value
.is_empty
then return null
655 # Retrieves the `Array[String]` value corresponding to the given key
656 # Returns the `null` if none or if it's the wrong value type
657 fun array_of_string
(key
: String): nullable Array[String]
659 sys
.jni_env
.push_local_frame
(1)
661 var return_value
= native_bundle
.get_string_array
(key
.to_java_string
)
662 sys
.jni_env
.pop_local_frame
664 if return_value
.is_empty
then return null
670 redef class Serializable
671 # Called by `Bundle::[]=` to dynamically choose the appropriate method according
672 # to the value type to store
673 # Non-primitive Object (`String` excluded) will be stored as a serialized json `String`
674 # Refine your class to customize this method behaviour
675 protected fun add_to_bundle
(bundle
: NativeBundle, key
: JavaString)
677 sys
.jni_env
.push_local_frame
(1)
678 var serialized_string
= new StringWriter
679 var serializer
= new JsonSerializer(serialized_string
)
680 serializer
.serialize
(self)
682 bundle
.put_string
(key
, serialized_string
.to_s
.to_java_string
)
687 redef fun add_to_bundle
(bundle
, key
)
689 bundle
.put_long
(key
, self)
694 redef fun add_to_bundle
(bundle
, key
)
696 bundle
.put_char
(key
, self)
701 redef fun add_to_bundle
(bundle
, key
)
703 bundle
.put_double
(key
, self)
708 redef fun add_to_bundle
(bundle
, key
)
710 bundle
.put_boolean
(key
, self)
715 redef fun add_to_bundle
(bundle
, key
)
717 bundle
.put_string
(key
, self.to_java_string
)
722 redef fun add_to_bundle
(bundle
, key
)
724 if self isa Array[Bool] then
725 bundle
.put_boolean_array
(key
, self)
726 else if self isa Array[Int] then
727 bundle
.put_long_array
(key
, self)
728 else if self isa Array[Float] then
729 bundle
.put_double_array
(key
, self)
730 else if self isa Array[Char] then
731 bundle
.put_char_array
(key
, self)
732 else if self isa Array[String] then
733 sys
.jni_env
.push_local_frame
(self.length
)
734 var java_string_array
= new Array[JavaString]
736 for element
in self do
737 java_string_array
.push
(element
.to_s
.to_java_string
)
740 bundle
.put_string_array
(key
, java_string_array
)
741 else if self isa Array[Serializable] then
742 sys
.jni_env
.push_local_frame
(self.length
)
743 var java_string_array
= new Array[JavaString]
745 for element
in self do
746 var serialized_string
= new StringWriter
747 var serializer
= new JsonSerializer(serialized_string
)
748 serializer
.serialize
(element
)
749 java_string_array
.add
(serialized_string
.to_s
.to_java_string
)
752 bundle
.put_string_array
(key
, java_string_array
)
757 # Allows JavaString collection copy through FFI with Java
758 private class StringCopyArray
759 var collection
= new Array[String]
760 fun add
(element
: JavaString) do collection
.add element
.to_s