android & benitlux: use NitObject in clients
[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
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 import nit.app.NitObject;
33 `}
34
35 extern class NativeBundle in "Java" `{ android.os.Bundle `}
36 super JavaObject
37
38 new in "Java" `{ return new Bundle(); `}
39
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();
52
53 for (String element: java_set)
54 HashSet_of_JavaString_add(nit_hashset, element);
55
56 return nit_hashset;
57 `}
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);
61 `}
62 fun put_byte(key: JavaString, value: Int) in "Java" `{
63 self.putByte(key, (byte) value);
64 `}
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);
68 `}
69 fun put_short(key: JavaString, value: Int) in "Java" `{
70 self.putShort(key, (short) value);
71 `}
72 fun put_int(key: JavaString, value: Int) in "Java" `{
73 self.putInt(key, (int) value);
74 `}
75 fun put_long(key: JavaString, value: Int) in "Java" `{
76 self.putLong(key, value);
77 `}
78 fun put_float(key: JavaString, value: Float) in "Java" `{
79 self.putFloat(key, (float) value);
80 `}
81 fun put_double(key: JavaString, value: Float) in "Java" `{
82 self.putDouble(key, value);
83 `}
84 fun put_string(key: JavaString, value: JavaString) in "Java" `{
85 self.putString(key, value);
86 `}
87 fun put_char_sequence(key: JavaString, value: JavaString) in "Java" `{
88 self.putCharSequence(key, value);
89 `}
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));
94
95 for(int i=0; i < java_array.size(); ++i)
96 java_array.add((int) Array_of_Int__index(value, i));
97
98 self.putIntegerArrayList(key, java_array);
99 `}
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));
103
104 for(int i=0; i < java_array.size(); ++i)
105 java_array.add(Array_of_JavaString__index(value, i));
106
107 self.putStringArrayList(key, java_array);
108 `}
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));
113
114 for(int i=0; i < java_array.size(); ++i)
115 java_array.add(Array_of_JavaString__index(value, i));
116
117 self.putCharSequenceArrayList(key, java_array);
118 `}
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)];
122
123 for(int i=0; i < java_array.length; ++i)
124 java_array[i] = Array_of_Bool__index(value, i);
125
126 self.putBooleanArray(key, java_array);
127 `}
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)];
131
132 for(int i=0; i < java_array.length; ++i)
133 java_array[i] = (byte) Array_of_Int__index(value, i);
134
135 self.putByteArray(key, java_array);
136 `}
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)];
140
141 for(int i=0; i < java_array.length; ++i)
142 java_array[i] = (short) Array_of_Int__index(value, i);
143
144 self.putShortArray(key, java_array);
145 `}
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)];
150
151 for(int i=0; i < java_array.length; ++i)
152 java_array[i] = (char)Array_of_Char__index(value, i);
153
154 self.putCharArray(key, java_array);
155 `}
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)];
159
160 for(int i=0; i < java_array.length; ++i)
161 java_array[i] = (int) Array_of_Int__index(value, i);
162
163 self.putIntArray(key, java_array);
164 `}
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)];
168
169 for(int i=0; i < java_array.length; ++i)
170 java_array[i] = Array_of_Int__index(value, i);
171
172 self.putLongArray(key, java_array);
173 `}
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)];
177
178 for(int i=0; i < java_array.length; ++i)
179 java_array[i] = (float) Array_of_Float__index(value, i);
180
181 self.putFloatArray(key, java_array);
182 `}
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)];
186
187 for(int i=0; i < java_array.length; ++i)
188 java_array[i] = Array_of_Float__index(value, i);
189
190 self.putDoubleArray(key, java_array);
191 `}
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)];
195
196 for(int i=0; i < java_array.length; ++i)
197 java_array[i] = Array_of_JavaString__index(value, i);
198
199 self.putStringArray(key, java_array);
200 `}
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)];
204
205 for(int i=0; i < java_array.length; ++i)
206 java_array[i] = Array_of_JavaString__index(value, i);
207
208 self.putCharSequenceArray(key, java_array);
209 `}
210 fun put_bundle(key: JavaString, value: NativeBundle) in "Java" `{
211 self.putBundle(key, value);
212 `}
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);
216 `}
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);
220 `}
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);
226 `}
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);
230 `}
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);
234 `}
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);
238 `}
239 fun get_float(key: JavaString): Float in "Java" `{
240 return (float) self.getFloat(key);
241 `}
242 fun get_float_with_def_value(key: JavaString, def_value: Float): Float in "Java" `{
243 return (float) self.getFloat(key, (float) def_value);
244 `}
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);
248 `}
249 fun get_string(key: JavaString): JavaString in "Java" `{
250 return self.getString(key);
251 `}
252 fun get_char_sequence(key: JavaString): JavaString in "Java" `{
253 return (String) self.getCharSequence(key);
254 `}
255 fun get_bundle(key: JavaString): NativeBundle in "Java" `{
256 return self.getBundle(key);
257 `}
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();
262
263 if (java_array == null) return nit_array;
264
265 for (int element: java_array)
266 Array_of_Int_add(nit_array, element);
267
268 return nit_array;
269 `}
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();
274
275 if (java_array == null) return nit_array;
276
277 for (String element: java_array)
278 StringCopyArray_add(nit_array, element);
279
280 return StringCopyArray_collection(nit_array);
281 `}
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();
286
287 if (java_array == null) return nit_array;
288
289 for (CharSequence element: java_array)
290 StringCopyArray_add(nit_array, (String) element);
291
292 return StringCopyArray_collection(nit_array);
293 `}
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();
298
299 if (java_array == null) return nit_array;
300
301 for (int i=0; i < java_array.length; ++i)
302 Array_of_Bool_add(nit_array, java_array[i]);
303
304 return nit_array;
305 `}
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();
310
311 if (java_array == null) return nit_array;
312
313 for(int i=0; i < java_array.length; ++i)
314 Array_of_Int_add(nit_array, java_array[i]);
315
316 return nit_array;
317 `}
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();
322
323 if (java_array == null) return nit_array;
324
325 for(int i=0; i < java_array.length; ++i)
326 Array_of_Int_add(nit_array, java_array[i]);
327
328 return nit_array;
329 `}
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();
335
336 if (java_array == null) return nit_array;
337
338 for(int i=0; i < java_array.length; ++i)
339 Array_of_Char_add(nit_array, (int)java_array[i]);
340
341 return nit_array;
342 `}
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();
347
348 if (java_array == null) return nit_array;
349
350 for(int i=0; i < java_array.length; ++i)
351 Array_of_Int_add(nit_array, java_array[i]);
352
353 return nit_array;
354 `}
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();
360
361 if (java_array == null) return nit_array;
362
363 for(int i=0; i < java_array.length; ++i)
364 Array_of_Int_add(nit_array, java_array[i]);
365
366 return nit_array;
367 `}
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();
372
373 if (java_array == null) return nit_array;
374
375 for(int i=0; i < java_array.length; ++i)
376 Array_of_Float_add(nit_array, (double) java_array[i]);
377
378 return nit_array;
379 `}
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();
384
385 if (java_array == null) return nit_array;
386
387 for(int i=0; i < java_array.length; ++i)
388 Array_of_Float_add(nit_array, java_array[i]);
389
390 return nit_array;
391 `}
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();
396
397 if (java_array == null) return nit_array;
398
399 for(int i=0; i < java_array.length; ++i)
400 StringCopyArray_add(nit_array, java_array[i]);
401
402 return StringCopyArray_collection(nit_array);
403 `}
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();
408
409 if (java_array == null) return nit_array;
410
411 for(int i=0; i < java_array.length; ++i)
412 StringCopyArray_add(nit_array, (String)java_array[i]);
413
414 return StringCopyArray_collection(nit_array);
415 `}
416 fun describe_contents: Int in "Java" `{ return self.describeContents(); `}
417 fun to_string: JavaString in "Java" `{ return self.toString(); `}
418
419 # HACK for bug #845
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);
424 `}
425 end
426
427 # A class mapping `String` keys to various value types
428 class Bundle
429 private var native_bundle: NativeBundle = (new NativeBundle).new_global_ref is lazy
430
431 # Get a new `Bundle` wrapping `native_bundle`
432 init from(native_bundle: NativeBundle) do self.native_bundle = native_bundle
433
434 # Returns `true` if the Bundle contains this key
435 fun has(key: String): Bool
436 do
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
440 return return_value
441 end
442
443 # Returns the number of entries in the current `Bundle`
444 fun size: Int do return native_bundle.size
445
446 # Returns true if the current `Bundle` is empty
447 fun is_empty: Bool do return native_bundle.is_empty
448
449 # Clears all entries
450 fun clear do native_bundle.clear
451
452 # Removes the entry associated with the given key
453 fun remove(key: String)
454 do
455 sys.jni_env.push_local_frame(1)
456 native_bundle.remove(key.to_java_string)
457 sys.jni_env.pop_local_frame
458 end
459
460 # Returns a `HashSet[String]` containing every mapping keys in the current
461 # `Bundle`
462 fun keys: HashSet[String]
463 do
464 var javastring_set = native_bundle.key_set
465 var string_set = new HashSet[String]
466
467 for element in javastring_set do
468 string_set.add(element.to_s)
469 end
470
471 return string_set
472 end
473
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
477 # overwrites it
478 #
479 # To retrieve entries, you'll have to call the type corresponding method
480 # conforming to these rules:
481 #
482 # | Nit type | corresponding getter |
483 # |:----------------------|:--------------------------------|
484 # | `Int` | `long` |
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
497 do
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
501 return self
502 end
503
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
511 do
512 var serialized_string = self.string(key)
513
514 if serialized_string == null then return null
515
516 var deserializer = new JsonDeserializer(serialized_string)
517
518 return deserializer.deserialize
519 end
520
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]
528 do
529 var serialized_array = self.array_of_string(key)
530
531 if serialized_array == null then return null
532
533 var deserialized_array = new Array[nullable Object]
534
535 for serialized_element in serialized_array do
536 var deserializer = new JsonDeserializer(serialized_element)
537 deserialized_array.add(deserializer.deserialize)
538 end
539
540 return deserialized_array
541 end
542
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
546 do
547 sys.jni_env.push_local_frame(2)
548
549 var jstr = native_bundle.get_string(key.to_java_string)
550 var str = null
551 if not jstr.is_java_null then str = jstr.to_s
552
553 sys.jni_env.pop_local_frame
554
555 return str
556 end
557
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
561 do
562 sys.jni_env.push_local_frame(1)
563 var return_value =
564 native_bundle.get_boolean_with_def_value(key.to_java_string, def_value)
565 sys.jni_env.pop_local_frame
566 return return_value
567 end
568
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
573 do
574 sys.jni_env.push_local_frame(1)
575 var return_value =
576 native_bundle.get_char_with_def_value(key.to_java_string, def_value)
577 sys.jni_env.pop_local_frame
578 return return_value
579 end
580
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
584 do
585 sys.jni_env.push_local_frame(1)
586 var return_value =
587 native_bundle.get_long_with_def_value(key.to_java_string, def_value)
588 sys.jni_env.pop_local_frame
589 return return_value
590 end
591
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
595 do
596 sys.jni_env.push_local_frame(1)
597 var return_value =
598 native_bundle.get_double_with_def_value(key.to_java_string, def_value)
599 sys.jni_env.pop_local_frame
600 return return_value
601 end
602
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]
606 do
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
610
611 if return_value.is_empty then return null
612
613 return return_value
614 end
615
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]
620 do
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
624
625 if return_value.is_empty then return null
626
627 return return_value
628 end
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]
632 do
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
636
637 if return_value.is_empty then return null
638
639 return return_value
640 end
641
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]
645 do
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
649
650 if return_value.is_empty then return null
651
652 return return_value
653 end
654
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]
658 do
659 sys.jni_env.push_local_frame(1)
660
661 var return_value = native_bundle.get_string_array(key.to_java_string)
662 sys.jni_env.pop_local_frame
663
664 if return_value.is_empty then return null
665
666 return return_value
667 end
668 end
669
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)
676 do
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)
681
682 bundle.put_string(key, serialized_string.to_s.to_java_string)
683 end
684 end
685
686 redef class Int
687 redef fun add_to_bundle(bundle, key)
688 do
689 bundle.put_long(key, self)
690 end
691 end
692
693 redef class Char
694 redef fun add_to_bundle(bundle, key)
695 do
696 bundle.put_char(key, self)
697 end
698 end
699
700 redef class Float
701 redef fun add_to_bundle(bundle, key)
702 do
703 bundle.put_double(key, self)
704 end
705 end
706
707 redef class Bool
708 redef fun add_to_bundle(bundle, key)
709 do
710 bundle.put_boolean(key, self)
711 end
712 end
713
714 redef class String
715 redef fun add_to_bundle(bundle, key)
716 do
717 bundle.put_string(key, self.to_java_string)
718 end
719 end
720
721 redef class Array[E]
722 redef fun add_to_bundle(bundle, key)
723 do
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]
735
736 for element in self do
737 java_string_array.push(element.to_s.to_java_string)
738 end
739
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]
744
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)
750 end
751
752 bundle.put_string_array(key, java_string_array)
753 end
754 end
755 end
756
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
761 end