e02702a9a0f236d4774f276a38161b753ae93ebf
[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 `}
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 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();
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 self.hasFileDescriptors(); `}
58 fun put_boolean(key: JavaString, value: Bool) in "Java" `{
59 self.putBoolean(key, value);
60 `}
61 fun put_byte(key: JavaString, value: Int) in "Java" `{
62 self.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 self.putChar(key, (char)value);
67 `}
68 fun put_short(key: JavaString, value: Int) in "Java" `{
69 self.putShort(key, (short) value);
70 `}
71 fun put_int(key: JavaString, value: Int) in "Java" `{
72 self.putInt(key, (int) value);
73 `}
74 fun put_long(key: JavaString, value: Int) in "Java" `{
75 self.putLong(key, value);
76 `}
77 fun put_float(key: JavaString, value: Float) in "Java" `{
78 self.putFloat(key, (float) value);
79 `}
80 fun put_double(key: JavaString, value: Float) in "Java" `{
81 self.putDouble(key, value);
82 `}
83 fun put_string(key: JavaString, value: JavaString) in "Java" `{
84 self.putString(key, value);
85 `}
86 fun put_char_sequence(key: JavaString, value: JavaString) in "Java" `{
87 self.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 self.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 self.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 self.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 self.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 self.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 self.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] = (char)Array_of_Char__index(value, i);
152
153 self.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 self.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 self.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 self.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 self.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 self.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 self.putCharSequenceArray(key, java_array);
208 `}
209 fun put_bundle(key: JavaString, value: NativeBundle) in "Java" `{
210 self.putBundle(key, value);
211 `}
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);
215 `}
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);
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 (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);
225 `}
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);
229 `}
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);
233 `}
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);
237 `}
238 fun get_float(key: JavaString): Float in "Java" `{
239 return (float) self.getFloat(key);
240 `}
241 fun get_float_with_def_value(key: JavaString, def_value: Float): Float in "Java" `{
242 return (float) self.getFloat(key, (float) def_value);
243 `}
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);
247 `}
248 fun get_string(key: JavaString): JavaString in "Java" `{
249 return self.getString(key);
250 `}
251 fun get_char_sequence(key: JavaString): JavaString in "Java" `{
252 return (String) self.getCharSequence(key);
253 `}
254 fun get_bundle(key: JavaString): NativeBundle in "Java" `{
255 return self.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 = self.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 = self.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 = self.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 = self.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 = self.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 = self.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 = self.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, (int)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 = self.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 = self.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 = self.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 = self.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 = self.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 = self.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 self.describeContents(); `}
416 fun to_string: JavaString in "Java" `{ return self.toString(); `}
417
418 # HACK for bug #845
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);
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).new_global_ref 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 `serialize` 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 `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]
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(2)
547
548 var jstr = native_bundle.get_string(key.to_java_string)
549 var str = null
550 if not jstr.is_java_null then str = jstr.to_s
551
552 sys.jni_env.pop_local_frame
553
554 return str
555 end
556
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
560 do
561 sys.jni_env.push_local_frame(1)
562 var return_value =
563 native_bundle.get_boolean_with_def_value(key.to_java_string, def_value)
564 sys.jni_env.pop_local_frame
565 return return_value
566 end
567
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
572 do
573 sys.jni_env.push_local_frame(1)
574 var return_value =
575 native_bundle.get_char_with_def_value(key.to_java_string, def_value)
576 sys.jni_env.pop_local_frame
577 return return_value
578 end
579
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
583 do
584 sys.jni_env.push_local_frame(1)
585 var return_value =
586 native_bundle.get_long_with_def_value(key.to_java_string, def_value)
587 sys.jni_env.pop_local_frame
588 return return_value
589 end
590
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
594 do
595 sys.jni_env.push_local_frame(1)
596 var return_value =
597 native_bundle.get_double_with_def_value(key.to_java_string, def_value)
598 sys.jni_env.pop_local_frame
599 return return_value
600 end
601
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]
605 do
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
609
610 if return_value.is_empty then return null
611
612 return return_value
613 end
614
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]
619 do
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
623
624 if return_value.is_empty then return null
625
626 return return_value
627 end
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]
631 do
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
635
636 if return_value.is_empty then return null
637
638 return return_value
639 end
640
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]
644 do
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
648
649 if return_value.is_empty then return null
650
651 return return_value
652 end
653
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]
657 do
658 sys.jni_env.push_local_frame(1)
659
660 var return_value = native_bundle.get_string_array(key.to_java_string)
661 sys.jni_env.pop_local_frame
662
663 if return_value.is_empty then return null
664
665 return return_value
666 end
667 end
668
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)
675 do
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)
680
681 bundle.put_string(key, serialized_string.to_s.to_java_string)
682 end
683 end
684
685 redef class Int
686 redef fun add_to_bundle(bundle, key)
687 do
688 bundle.put_long(key, self)
689 end
690 end
691
692 redef class Char
693 redef fun add_to_bundle(bundle, key)
694 do
695 bundle.put_char(key, self)
696 end
697 end
698
699 redef class Float
700 redef fun add_to_bundle(bundle, key)
701 do
702 bundle.put_double(key, self)
703 end
704 end
705
706 redef class Bool
707 redef fun add_to_bundle(bundle, key)
708 do
709 bundle.put_boolean(key, self)
710 end
711 end
712
713 redef class String
714 redef fun add_to_bundle(bundle, key)
715 do
716 bundle.put_string(key, self.to_java_string)
717 end
718 end
719
720 redef class Array[E]
721 redef fun add_to_bundle(bundle, key)
722 do
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]
734
735 for element in self do
736 java_string_array.push(element.to_s.to_java_string)
737 end
738
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]
743
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)
749 end
750
751 bundle.put_string_array(key, java_string_array)
752 end
753 end
754 end
755
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
760 end