lib/android: Bundle do not need a context or to import native_app_glue
[nit.git] / lib / android / bundle / bundle.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # A mapping class of `String` to various value types used by the
18 # Android API for various data exchange purposes
19 module bundle
20
21 import serialization
22 import json_serialization
23
24 import platform
25 import activities
26
27 in "Java" `{
28 import android.os.Bundle;
29 import android.app.Activity;
30 import java.util.ArrayList;
31 import java.util.Set;
32 `}
33
34 extern class NativeBundle in "Java" `{ android.os.Bundle `}
35 super JavaObject
36
37 new in "Java" `{ return new Bundle(); `}
38
39 fun clone: JavaObject in "Java" `{ return recv.clone(); `}
40 fun size: Int in "Java" `{ return recv.size(); `}
41 fun is_empty: Bool in "Java" `{ return recv.isEmpty(); `}
42 fun clear in "Java" `{ recv.clear(); `}
43 fun contains_key(key: JavaString): Bool in "Java" `{ return recv.containsKey(key); `}
44 fun get(key: JavaString): JavaObject in "Java" `{ return recv.get(key); `}
45 fun remove(key: JavaString) in "Java" `{ recv.remove(key); `}
46 fun put_all(bundle: NativeBundle) in "Java" `{ recv.putAll(bundle); `}
47 fun key_set: HashSet[JavaString] import HashSet[JavaString],
48 HashSet[JavaString].add in "Java" `{
49 Set<String> java_set = recv.keySet();
50 int nit_hashset = new_HashSet_of_JavaString();
51
52 for (String element: java_set)
53 HashSet_of_JavaString_add(nit_hashset, element);
54
55 return nit_hashset;
56 `}
57 fun has_file_descriptors: Bool in "Java" `{ return recv.hasFileDescriptors(); `}
58 fun put_boolean(key: JavaString, value: Bool) in "Java" `{
59 recv.putBoolean(key, value);
60 `}
61 fun put_byte(key: JavaString, value: Int) in "Java" `{
62 recv.putByte(key, (byte) value);
63 `}
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 recv.putChar(key, value);
67 `}
68 fun put_short(key: JavaString, value: Int) in "Java" `{
69 recv.putShort(key, (short) value);
70 `}
71 fun put_int(key: JavaString, value: Int) in "Java" `{
72 recv.putInt(key, (int) value);
73 `}
74 fun put_long(key: JavaString, value: Int) in "Java" `{
75 recv.putLong(key, value);
76 `}
77 fun put_float(key: JavaString, value: Float) in "Java" `{
78 recv.putFloat(key, (float) value);
79 `}
80 fun put_double(key: JavaString, value: Float) in "Java" `{
81 recv.putDouble(key, value);
82 `}
83 fun put_string(key: JavaString, value: JavaString) in "Java" `{
84 recv.putString(key, value);
85 `}
86 fun put_char_sequence(key: JavaString, value: JavaString) in "Java" `{
87 recv.putCharSequence(key, value);
88 `}
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));
93
94 for(int i=0; i < java_array.size(); ++i)
95 java_array.add((int) Array_of_Int__index(value, i));
96
97 recv.putIntegerArrayList(key, java_array);
98 `}
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));
102
103 for(int i=0; i < java_array.size(); ++i)
104 java_array.add(Array_of_JavaString__index(value, i));
105
106 recv.putStringArrayList(key, java_array);
107 `}
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));
112
113 for(int i=0; i < java_array.size(); ++i)
114 java_array.add(Array_of_JavaString__index(value, i));
115
116 recv.putCharSequenceArrayList(key, java_array);
117 `}
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)];
121
122 for(int i=0; i < java_array.length; ++i)
123 java_array[i] = Array_of_Bool__index(value, i);
124
125 recv.putBooleanArray(key, java_array);
126 `}
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)];
130
131 for(int i=0; i < java_array.length; ++i)
132 java_array[i] = (byte) Array_of_Int__index(value, i);
133
134 recv.putByteArray(key, java_array);
135 `}
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)];
139
140 for(int i=0; i < java_array.length; ++i)
141 java_array[i] = (short) Array_of_Int__index(value, i);
142
143 recv.putShortArray(key, java_array);
144 `}
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)];
149
150 for(int i=0; i < java_array.length; ++i)
151 java_array[i] = Array_of_Char__index(value, i);
152
153 recv.putCharArray(key, java_array);
154 `}
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)];
158
159 for(int i=0; i < java_array.length; ++i)
160 java_array[i] = (int) Array_of_Int__index(value, i);
161
162 recv.putIntArray(key, java_array);
163 `}
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)];
167
168 for(int i=0; i < java_array.length; ++i)
169 java_array[i] = Array_of_Int__index(value, i);
170
171 recv.putLongArray(key, java_array);
172 `}
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)];
176
177 for(int i=0; i < java_array.length; ++i)
178 java_array[i] = (float) Array_of_Float__index(value, i);
179
180 recv.putFloatArray(key, java_array);
181 `}
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)];
185
186 for(int i=0; i < java_array.length; ++i)
187 java_array[i] = Array_of_Float__index(value, i);
188
189 recv.putDoubleArray(key, java_array);
190 `}
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)];
194
195 for(int i=0; i < java_array.length; ++i)
196 java_array[i] = Array_of_JavaString__index(value, i);
197
198 recv.putStringArray(key, java_array);
199 `}
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)];
203
204 for(int i=0; i < java_array.length; ++i)
205 java_array[i] = Array_of_JavaString__index(value, i);
206
207 recv.putCharSequenceArray(key, java_array);
208 `}
209 fun put_bundle(key: JavaString, value: NativeBundle) in "Java" `{
210 recv.putBundle(key, value);
211 `}
212 fun get_boolean(key: JavaString): Bool in "Java" `{ return recv.getBoolean(key); `}
213 fun get_boolean_with_def_value(key: JavaString, def_value: Bool): Bool in "Java" `{
214 return recv.getBoolean(key, def_value);
215 `}
216 fun get_byte(key: JavaString): Int in "Java" `{ return recv.getByte(key); `}
217 fun get_byte_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
218 return recv.getByte(key, (byte) def_value);
219 `}
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 recv.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 recv.getChar(key, def_value);
225 `}
226 fun get_short(key: JavaString): Int in "Java" `{ return (short) recv.getShort(key); `}
227 fun get_short_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
228 return (short) recv.getShort(key, (short) def_value);
229 `}
230 fun get_int(key: JavaString): Int in "Java" `{ return recv.getInt(key); `}
231 fun get_int_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
232 return recv.getInt(key, (int) def_value);
233 `}
234 fun get_long(key: JavaString): Int in "Java" `{ return recv.getLong(key); `}
235 fun get_long_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
236 return recv.getLong(key);
237 `}
238 fun get_float(key: JavaString): Float in "Java" `{
239 return (float) recv.getFloat(key);
240 `}
241 fun get_float_with_def_value(key: JavaString, def_value: Float): Float in "Java" `{
242 return (float) recv.getFloat(key, (float) def_value);
243 `}
244 fun get_double(key: JavaString): Float in "Java" `{ return recv.getDouble(key); `}
245 fun get_double_with_def_value(key: JavaString, def_value: Float): Float in "Java" `{
246 return recv.getDouble(key, def_value);
247 `}
248 fun get_string(key: JavaString): JavaString in "Java" `{
249 return recv.getString(key);
250 `}
251 fun get_char_sequence(key: JavaString): JavaString in "Java" `{
252 return (String) recv.getCharSequence(key);
253 `}
254 fun get_bundle(key: JavaString): NativeBundle in "Java" `{
255 return recv.getBundle(key);
256 `}
257 fun get_integer_array_list(key: JavaString): Array[Int]
258 import Array[Int], Array[Int].add in "Java" `{
259 ArrayList<Integer> java_array = recv.getIntegerArrayList(key);
260 int nit_array = new_Array_of_Int();
261
262 if (java_array == null) return nit_array;
263
264 for (int element: java_array)
265 Array_of_Int_add(nit_array, element);
266
267 return nit_array;
268 `}
269 fun get_string_array_list(key: JavaString): Array[String]
270 import StringCopyArray, StringCopyArray.add, StringCopyArray.collection in "Java" `{
271 ArrayList<String> java_array = recv.getStringArrayList(key);
272 int nit_array = new_StringCopyArray();
273
274 if (java_array == null) return nit_array;
275
276 for (String element: java_array)
277 StringCopyArray_add(nit_array, element);
278
279 return StringCopyArray_collection(nit_array);
280 `}
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 = recv.getCharSequenceArrayList(key);
284 int nit_array = new_StringCopyArray();
285
286 if (java_array == null) return nit_array;
287
288 for (CharSequence element: java_array)
289 StringCopyArray_add(nit_array, (String) element);
290
291 return StringCopyArray_collection(nit_array);
292 `}
293 fun get_boolean_array(key: JavaString): Array[Bool]
294 import Array[Bool], Array[Bool].add in "Java" `{
295 boolean[] java_array = recv.getBooleanArray(key);
296 int nit_array = new_Array_of_Bool();
297
298 if (java_array == null) return nit_array;
299
300 for (int i=0; i < java_array.length; ++i)
301 Array_of_Bool_add(nit_array, java_array[i]);
302
303 return nit_array;
304 `}
305 fun get_byte_array(key: JavaString): Array[Int]
306 import Array[Int], Array[Int].add in "Java" `{
307 byte[] java_array = recv.getByteArray(key);
308 int nit_array = new_Array_of_Int();
309
310 if (java_array == null) return nit_array;
311
312 for(int i=0; i < java_array.length; ++i)
313 Array_of_Int_add(nit_array, java_array[i]);
314
315 return nit_array;
316 `}
317 fun get_short_array(key: JavaString): Array[Int]
318 import Array[Int], Array[Int].add in "Java" `{
319 short[] java_array = recv.getShortArray(key);
320 int nit_array = new_Array_of_Int();
321
322 if (java_array == null) return nit_array;
323
324 for(int i=0; i < java_array.length; ++i)
325 Array_of_Int_add(nit_array, java_array[i]);
326
327 return nit_array;
328 `}
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 = recv.getCharArray(key);
333 int nit_array = new_Array_of_Char();
334
335 if (java_array == null) return nit_array;
336
337 for(int i=0; i < java_array.length; ++i)
338 Array_of_Char_add(nit_array, java_array[i]);
339
340 return nit_array;
341 `}
342 fun get_int_array(key: JavaString): Array[Int]
343 import Array[Int], Array[Int].add in "Java" `{
344 int[] java_array = recv.getIntArray(key);
345 int nit_array = new_Array_of_Int();
346
347 if (java_array == null) return nit_array;
348
349 for(int i=0; i < java_array.length; ++i)
350 Array_of_Int_add(nit_array, java_array[i]);
351
352 return nit_array;
353 `}
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 = recv.getLongArray(key);
358 int nit_array = new_Array_of_Int();
359
360 if (java_array == null) return nit_array;
361
362 for(int i=0; i < java_array.length; ++i)
363 Array_of_Int_add(nit_array, java_array[i]);
364
365 return nit_array;
366 `}
367 fun get_float_array(key: JavaString): Array[Float]
368 import Array[Float], Array[Float].add in "Java" `{
369 float[] java_array = recv.getFloatArray(key);
370 int nit_array = new_Array_of_Float();
371
372 if (java_array == null) return nit_array;
373
374 for(int i=0; i < java_array.length; ++i)
375 Array_of_Float_add(nit_array, (double) java_array[i]);
376
377 return nit_array;
378 `}
379 fun get_double_array(key: JavaString): Array[Float]
380 import Array[Float], Array[Float].add in "Java" `{
381 double[] java_array = recv.getDoubleArray(key);
382 int nit_array = new_Array_of_Float();
383
384 if (java_array == null) return nit_array;
385
386 for(int i=0; i < java_array.length; ++i)
387 Array_of_Float_add(nit_array, java_array[i]);
388
389 return nit_array;
390 `}
391 fun get_string_array(key: JavaString): Array[String]
392 import StringCopyArray, StringCopyArray.add, StringCopyArray.collection in "Java" `{
393 String[] java_array = recv.getStringArray(key);
394 int nit_array = new_StringCopyArray();
395
396 if (java_array == null) return nit_array;
397
398 for(int i=0; i < java_array.length; ++i)
399 StringCopyArray_add(nit_array, java_array[i]);
400
401 return StringCopyArray_collection(nit_array);
402 `}
403 fun get_char_sequence_array(key: JavaString): Array[String]
404 import StringCopyArray, StringCopyArray.add, StringCopyArray.collection in "Java" `{
405 CharSequence[] java_array = recv.getCharSequenceArray(key);
406 int nit_array = new_StringCopyArray();
407
408 if (java_array == null) return nit_array;
409
410 for(int i=0; i < java_array.length; ++i)
411 StringCopyArray_add(nit_array, (String)java_array[i]);
412
413 return StringCopyArray_collection(nit_array);
414 `}
415 fun describe_contents: Int in "Java" `{ return recv.describeContents(); `}
416 fun to_string: JavaString in "Java" `{ return recv.toString(); `}
417
418 # HACK for bug #845
419 redef fun new_global_ref import sys, Sys.jni_env `{
420 Sys sys = NativeBundle_sys(recv);
421 JNIEnv *env = Sys_jni_env(sys);
422 return (*env)->NewGlobalRef(env, recv);
423 `}
424 end
425
426 # A class mapping `String` keys to various value types
427 class Bundle
428 private var native_bundle: NativeBundle = new NativeBundle is lazy
429
430 # Get a new `Bundle` wrapping `native_bundle`
431 init from(native_bundle: NativeBundle) do self.native_bundle = native_bundle
432
433 # Returns `true` if the Bundle contains this key
434 fun has(key: String): Bool
435 do
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
439 return return_value
440 end
441
442 # Returns the number of entries in the current `Bundle`
443 fun size: Int do return native_bundle.size
444
445 # Returns true if the current `Bundle` is empty
446 fun is_empty: Bool do return native_bundle.is_empty
447
448 # Clears all entries
449 fun clear do native_bundle.clear
450
451 # Removes the entry associated with the given key
452 fun remove(key: String)
453 do
454 sys.jni_env.push_local_frame(1)
455 native_bundle.remove(key.to_java_string)
456 sys.jni_env.pop_local_frame
457 end
458
459 # Returns a `HashSet[String]` containing every mapping keys in the current
460 # `Bundle`
461 fun keys: HashSet[String]
462 do
463 var javastring_set = native_bundle.key_set
464 var string_set = new HashSet[String]
465
466 for element in javastring_set do
467 string_set.add(element.to_s)
468 end
469
470 return string_set
471 end
472
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
476 # overwrites it
477 #
478 # To retrieve entries, you'll have to call the type corresponding method
479 # conforming to these rules :
480 #
481 # | Nit type | corresponding getter |
482 # |:----------------------|:--------------------------------|
483 # ! `Int` | `long` |
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
496 do
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
500 return self
501 end
502
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 `auto_serializable` or that it
507 # redefines the appropriate methods. Refer to `Serializable` documentation
508 # for further details
509 fun deserialize(key: String): nullable Object
510 do
511 var serialized_string = self.string(key)
512
513 if serialized_string == null then return null
514
515 var deserializer = new JsonDeserializer(serialized_string)
516
517 return deserializer.deserialize
518 end
519
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 `auto_serializable` 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]
527 do
528 var serialized_array = self.array_of_string(key)
529
530 if serialized_array == null then return null
531
532 var deserialized_array = new Array[nullable Object]
533
534 for serialized_element in serialized_array do
535 var deserializer = new JsonDeserializer(serialized_element)
536 deserialized_array.add(deserializer.deserialize)
537 end
538
539 return deserialized_array
540 end
541
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
545 do
546 sys.jni_env.push_local_frame(1)
547 var return_value = native_bundle.get_string(key.to_java_string).to_s
548 sys.jni_env.pop_local_frame
549
550 if return_value == "" then return null
551
552 return return_value
553 end
554
555 # Retrieves the `Bool` value corresponding to the given key
556 # Returns the `def_value` if none or if it's the wrong value type
557 fun bool(key: String, def_value: Bool): Bool
558 do
559 sys.jni_env.push_local_frame(1)
560 var return_value =
561 native_bundle.get_boolean_with_def_value(key.to_java_string, def_value)
562 sys.jni_env.pop_local_frame
563 return return_value
564 end
565
566 # Retrieves the `Char` value corresponding to the given key
567 # Returns the `def_value` if none or if it's the wrong value type
568 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
569 fun char(key: String, def_value: Char): Char
570 do
571 sys.jni_env.push_local_frame(1)
572 var return_value =
573 native_bundle.get_char_with_def_value(key.to_java_string, def_value)
574 sys.jni_env.pop_local_frame
575 return return_value
576 end
577
578 # Retrieves the `Int` value corresponding to the given key
579 # Returns the `def_value` if none or if it's the wrong value type
580 fun int(key: String, def_value: Int): Int
581 do
582 sys.jni_env.push_local_frame(1)
583 var return_value =
584 native_bundle.get_long_with_def_value(key.to_java_string, def_value)
585 sys.jni_env.pop_local_frame
586 return return_value
587 end
588
589 # Retrieves the `Float` value corresponding to the given key
590 # Returns the `def_value` if none or if it's the wrong value type
591 fun float(key: String, def_value: Float): Float
592 do
593 sys.jni_env.push_local_frame(1)
594 var return_value =
595 native_bundle.get_double_with_def_value(key.to_java_string, def_value)
596 sys.jni_env.pop_local_frame
597 return return_value
598 end
599
600 # Retrieves the `Array[Float]` value corresponding to the given key
601 # Returns the `null` if none or if it's the wrong value type
602 fun array_of_float(key: String): nullable Array[Float]
603 do
604 sys.jni_env.push_local_frame(1)
605 var return_value = native_bundle.get_double_array(key.to_java_string)
606 sys.jni_env.pop_local_frame
607
608 if return_value.is_empty then return null
609
610 return return_value
611 end
612
613 # Retrieves the `Array[Char]` value corresponding to the given key
614 # Returns the `null` if none or if it's the wrong value type
615 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
616 fun array_of_char(key: String): nullable Array[Char]
617 do
618 sys.jni_env.push_local_frame(1)
619 var return_value = native_bundle.get_char_array(key.to_java_string)
620 sys.jni_env.pop_local_frame
621
622 if return_value.is_empty then return null
623
624 return return_value
625 end
626 # Retrieves the `Array[Int]` value corresponding to the given key
627 # Returns the `null` if none or if it's the wrong value type
628 fun array_of_int(key: String): nullable Array[Int]
629 do
630 sys.jni_env.push_local_frame(1)
631 var return_value = native_bundle.get_long_array(key.to_java_string)
632 sys.jni_env.pop_local_frame
633
634 if return_value.is_empty then return null
635
636 return return_value
637 end
638
639 # Retrieves the `Array[Bool]` value corresponding to the given key
640 # Returns the `null` if none or if it's the wrong value type
641 fun array_of_bool(key: String): nullable Array[Bool]
642 do
643 sys.jni_env.push_local_frame(1)
644 var return_value = native_bundle.get_boolean_array(key.to_java_string)
645 sys.jni_env.pop_local_frame
646
647 if return_value.is_empty then return null
648
649 return return_value
650 end
651
652 # Retrieves the `Array[String]` value corresponding to the given key
653 # Returns the `null` if none or if it's the wrong value type
654 fun array_of_string(key: String): nullable Array[String]
655 do
656 sys.jni_env.push_local_frame(1)
657
658 var return_value = native_bundle.get_string_array(key.to_java_string)
659 sys.jni_env.pop_local_frame
660
661 if return_value.is_empty then return null
662
663 return return_value
664 end
665 end
666
667 redef class Serializable
668 # Called by `Bundle::[]=` to dynamically choose the appropriate method according
669 # to the value type to store
670 # Non-primitive Object (`String` excluded) will be stored as a serialized json `String`
671 # Refine your class to customize this method behaviour
672 protected fun add_to_bundle(bundle: NativeBundle, key: JavaString)
673 do
674 sys.jni_env.push_local_frame(1)
675 var serialized_string = new StringOStream
676 var serializer = new JsonSerializer(serialized_string)
677 serializer.serialize(self)
678
679 bundle.put_string(key, serialized_string.to_s.to_java_string)
680 end
681 end
682
683 redef class Int
684 redef fun add_to_bundle(bundle, key)
685 do
686 bundle.put_long(key, self)
687 end
688 end
689
690 redef class Char
691 redef fun add_to_bundle(bundle, key)
692 do
693 bundle.put_char(key, self)
694 end
695 end
696
697 redef class Float
698 redef fun add_to_bundle(bundle, key)
699 do
700 bundle.put_double(key, self)
701 end
702 end
703
704 redef class Bool
705 redef fun add_to_bundle(bundle, key)
706 do
707 bundle.put_boolean(key, self)
708 end
709 end
710
711 redef class String
712 redef fun add_to_bundle(bundle, key)
713 do
714 bundle.put_string(key, self.to_java_string)
715 end
716 end
717
718 redef class Array[E]
719 redef fun add_to_bundle(bundle, key)
720 do
721 if self isa Array[Bool] then
722 bundle.put_boolean_array(key, self)
723 else if self isa Array[Int] then
724 bundle.put_long_array(key, self)
725 else if self isa Array[Float] then
726 bundle.put_double_array(key, self)
727 else if self isa Array[Char] then
728 bundle.put_char_array(key, self)
729 else if self isa Array[String] then
730 sys.jni_env.push_local_frame(self.length)
731 var java_string_array = new Array[JavaString]
732
733 for element in self do
734 java_string_array.push(element.to_s.to_java_string)
735 end
736
737 bundle.put_string_array(key, java_string_array)
738 else if self isa Array[Serializable] then
739 sys.jni_env.push_local_frame(self.length)
740 var java_string_array = new Array[JavaString]
741
742 for element in self do
743 var serialized_string = new StringOStream
744 var serializer = new JsonSerializer(serialized_string)
745 serializer.serialize(element)
746 java_string_array.add(serialized_string.to_s.to_java_string)
747 end
748
749 bundle.put_string_array(key, java_string_array)
750 end
751 end
752 end
753
754 # Allows JavaString collection copy through FFI with Java
755 private class StringCopyArray
756 var collection = new Array[String]
757 fun add(element: JavaString) do collection.add element.to_s
758 end