b8b8390e6fedbd200c98e79eea238651ad3d6157
[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 native_app_glue
22 import serialization
23 import json_serialization
24
25 in "Java" `{
26 import android.os.Bundle;
27 import android.app.Activity;
28 import java.util.ArrayList;
29 import java.util.Set;
30 `}
31
32 extern class NativeBundle in "Java" `{ android.os.Bundle `}
33 super JavaObject
34 redef type SELF: NativeBundle
35
36 fun clone: JavaObject in "Java" `{ return recv.clone(); `}
37 fun size: Int in "Java" `{ return recv.size(); `}
38 fun is_empty: Bool in "Java" `{ return recv.isEmpty(); `}
39 fun clear in "Java" `{ recv.clear(); `}
40 fun contains_key(key: JavaString): Bool in "Java" `{ return recv.containsKey(key); `}
41 fun get(key: JavaString): JavaObject in "Java" `{ return recv.get(key); `}
42 fun remove(key: JavaString) in "Java" `{ recv.remove(key); `}
43 fun put_all(bundle: NativeBundle) in "Java" `{ recv.putAll(bundle); `}
44 fun key_set: HashSet[JavaString] import HashSet[JavaString],
45 HashSet[JavaString].add in "Java" `{
46 Set<String> java_set = recv.keySet();
47 int nit_hashset = new_HashSet_of_JavaString();
48
49 for (String element: java_set)
50 HashSet_of_JavaString_add(nit_hashset, element);
51
52 return nit_hashset;
53 `}
54 fun has_file_descriptors: Bool in "Java" `{ return recv.hasFileDescriptors(); `}
55 fun put_boolean(key: JavaString, value: Bool) in "Java" `{
56 recv.putBoolean(key, value);
57 `}
58 fun put_byte(key: JavaString, value: Int) in "Java" `{
59 recv.putByte(key, (byte) value);
60 `}
61 fun put_char(key: JavaString, value: Char) in "Java" `{
62 recv.putChar(key, value);
63 `}
64 fun put_short(key: JavaString, value: Int) in "Java" `{
65 recv.putShort(key, (short) value);
66 `}
67 fun put_int(key: JavaString, value: Int) in "Java" `{
68 recv.putInt(key, (int) value);
69 `}
70 fun put_long(key: JavaString, value: Int) in "Java" `{
71 recv.putLong(key, value);
72 `}
73 fun put_float(key: JavaString, value: Float) in "Java" `{
74 recv.putFloat(key, (float) value);
75 `}
76 fun put_double(key: JavaString, value: Float) in "Java" `{
77 recv.putDouble(key, value);
78 `}
79 fun put_string(key: JavaString, value: JavaString) in "Java" `{
80 recv.putString(key, value);
81 `}
82 fun put_char_sequence(key: JavaString, value: JavaString) in "Java" `{
83 recv.putCharSequence(key, value);
84 `}
85 fun put_integer_array_list(key: JavaString, value: Array[Int])
86 import Array[Int].length, Array[Int].[] in "Java" `{
87 ArrayList<Integer> java_array =
88 new ArrayList<Integer>(Array_of_Int_length(value));
89
90 for(int i=0; i < java_array.size(); ++i)
91 java_array.add((int) Array_of_Int__index(value, i));
92
93 recv.putIntegerArrayList(key, java_array);
94 `}
95 fun put_string_array_list(key: JavaString, value: Array[JavaString])
96 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
97 ArrayList<String> java_array = new ArrayList<String>(Array_of_JavaString_length(value));
98
99 for(int i=0; i < java_array.size(); ++i)
100 java_array.add(Array_of_JavaString__index(value, i));
101
102 recv.putStringArrayList(key, java_array);
103 `}
104 fun put_char_sequence_array_list(key: JavaString, value: Array[JavaString])
105 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
106 ArrayList<CharSequence> java_array =
107 new ArrayList<CharSequence>(Array_of_JavaString_length(value));
108
109 for(int i=0; i < java_array.size(); ++i)
110 java_array.add(Array_of_JavaString__index(value, i));
111
112 recv.putCharSequenceArrayList(key, java_array);
113 `}
114 fun put_boolean_array(key: JavaString, value: Array[Bool])
115 import Array[Bool].length, Array[Bool].[] in "Java" `{
116 boolean[] java_array = new boolean[Array_of_Bool_length(value)];
117
118 for(int i=0; i < java_array.length; ++i)
119 java_array[i] = Array_of_Bool__index(value, i);
120
121 recv.putBooleanArray(key, java_array);
122 `}
123 fun put_byte_array(key: JavaString, value: Array[Int])
124 import Array[Int].length, Array[Int].[] in "Java" `{
125 byte[] java_array = new byte[Array_of_Int_length(value)];
126
127 for(int i=0; i < java_array.length; ++i)
128 java_array[i] = (byte) Array_of_Int__index(value, i);
129
130 recv.putByteArray(key, java_array);
131 `}
132 fun put_short_array(key: JavaString, value: Array[Int])
133 import Array[Int].length, Array[Int].[] in "Java" `{
134 short[] java_array = new short[Array_of_Int_length(value)];
135
136 for(int i=0; i < java_array.length; ++i)
137 java_array[i] = (short) Array_of_Int__index(value, i);
138
139 recv.putShortArray(key, java_array);
140 `}
141 fun put_char_array(key: JavaString, value: Array[Char])
142 import Array[Char].length, Array[Char].[] in "Java" `{
143 char[] java_array = new char[Array_of_Char_length(value)];
144
145 for(int i=0; i < java_array.length; ++i)
146 java_array[i] = Array_of_Char__index(value, i);
147
148 recv.putCharArray(key, java_array);
149 `}
150 fun put_int_array(key: JavaString, value: Array[Int])
151 import Array[Int].length, Array[Int].[] in "Java" `{
152 int[] java_array = new int[Array_of_Int_length(value)];
153
154 for(int i=0; i < java_array.length; ++i)
155 java_array[i] = (int) Array_of_Int__index(value, i);
156
157 recv.putIntArray(key, java_array);
158 `}
159 fun put_long_array(key: JavaString, value: Array[Int])
160 import Array[Int].length, Array[Int].[] in "Java" `{
161 long[] java_array = new long[Array_of_Int_length(value)];
162
163 for(int i=0; i < java_array.length; ++i)
164 java_array[i] = Array_of_Int__index(value, i);
165
166 recv.putLongArray(key, java_array);
167 `}
168 fun put_float_array(key: JavaString, value: Array[Float])
169 import Array[Float].length, Array[Float].[] in "Java" `{
170 float[] java_array = new float[Array_of_Float_length(value)];
171
172 for(int i=0; i < java_array.length; ++i)
173 java_array[i] = (float) Array_of_Float__index(value, i);
174
175 recv.putFloatArray(key, java_array);
176 `}
177 fun put_double_array(key: JavaString, value: Array[Float])
178 import Array[Float].length, Array[Float].[] in "Java" `{
179 double[] java_array = new double[Array_of_Float_length(value)];
180
181 for(int i=0; i < java_array.length; ++i)
182 java_array[i] = Array_of_Float__index(value, i);
183
184 recv.putDoubleArray(key, java_array);
185 `}
186 fun put_string_array(key: JavaString, value: Array[JavaString])
187 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
188 String[] java_array = new String[Array_of_JavaString_length(value)];
189
190 for(int i=0; i < java_array.length; ++i)
191 java_array[i] = Array_of_JavaString__index(value, i);
192
193 recv.putStringArray(key, java_array);
194 `}
195 fun put_char_sequence_array(key: JavaString, value: Array[JavaString])
196 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
197 CharSequence[] java_array = new CharSequence[Array_of_JavaString_length(value)];
198
199 for(int i=0; i < java_array.length; ++i)
200 java_array[i] = Array_of_JavaString__index(value, i);
201
202 recv.putCharSequenceArray(key, java_array);
203 `}
204 fun put_bundle(key: JavaString, value: NativeBundle) in "Java" `{
205 recv.putBundle(key, value);
206 `}
207 fun get_boolean(key: JavaString): Bool in "Java" `{ return recv.getBoolean(key); `}
208 fun get_boolean_with_def_value(key: JavaString, def_value: Bool): Bool in "Java" `{
209 return recv.getBoolean(key, def_value);
210 `}
211 fun get_byte(key: JavaString): Int in "Java" `{ return recv.getByte(key); `}
212 fun get_byte_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
213 return recv.getByte(key, (byte) def_value);
214 `}
215 fun get_char(key: JavaString): Char in "Java" `{ return recv.getChar(key); `}
216 fun get_char_with_def_value(key: JavaString, def_value: Char): Char in "Java" `{
217 return recv.getChar(key, def_value);
218 `}
219 fun get_short(key: JavaString): Int in "Java" `{ return (short) recv.getShort(key); `}
220 fun get_short_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
221 return (short) recv.getShort(key, (short) def_value);
222 `}
223 fun get_int(key: JavaString): Int in "Java" `{ return recv.getInt(key); `}
224 fun get_int_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
225 return (int) recv.getInt(key, (int) def_value);
226 `}
227 # FIXME: Get rid of the int cast as soon as the ffi is fixed
228 fun get_long(key: JavaString): Int in "Java" `{ return (int) recv.getLong(key); `}
229 # FIXME: Get rid of the int cast as soon as the ffi is fixed
230 fun get_long_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
231 return (int) recv.getLong(key);
232 `}
233 fun get_float(key: JavaString): Float in "Java" `{
234 return (float) recv.getFloat(key);
235 `}
236 fun get_float_with_def_value(key: JavaString, def_value: Float): Float in "Java" `{
237 return (float) recv.getFloat(key, (float) def_value);
238 `}
239 fun get_double(key: JavaString): Float in "Java" `{ return recv.getDouble(key); `}
240 fun get_double_with_def_value(key: JavaString, def_value: Float): Float in "Java" `{
241 return recv.getDouble(key, def_value);
242 `}
243 fun get_string(key: JavaString): JavaString in "Java" `{
244 return recv.getString(key);
245 `}
246 fun get_char_sequence(key: JavaString): JavaString in "Java" `{
247 return (String) recv.getCharSequence(key);
248 `}
249 fun get_bundle(key: JavaString): NativeBundle in "Java" `{
250 return recv.getBundle(key);
251 `}
252 fun get_integer_array_list(key: JavaString): Array[Int]
253 import Array[Int], Array[Int].add in "Java" `{
254 ArrayList<Integer> java_array = recv.getIntegerArrayList(key);
255 int nit_array = new_Array_of_Int();
256
257 if (java_array == null) return nit_array;
258
259 for (int element: java_array)
260 Array_of_Int_add(nit_array, element);
261
262 return nit_array;
263 `}
264 fun get_string_array_list(key: JavaString): Array[JavaString]
265 import Array[JavaString], Array[JavaString].add in "Java" `{
266 ArrayList<String> java_array = recv.getStringArrayList(key);
267 int nit_array = new_Array_of_JavaString();
268
269 if (java_array == null) return nit_array;
270
271 for (String element: java_array)
272 Array_of_JavaString_add(nit_array, element);
273
274 return nit_array;
275 `}
276 fun get_char_sequence_array_list(key: JavaString): Array[JavaString]
277 import Array[JavaString], Array[JavaString].add in "Java" `{
278 ArrayList<CharSequence> java_array = recv.getCharSequenceArrayList(key);
279 int nit_array = new_Array_of_JavaString();
280
281 if (java_array == null) return nit_array;
282
283 for (CharSequence element: java_array)
284 Array_of_JavaString_add(nit_array, (String) element);
285
286 return nit_array;
287 `}
288 fun get_boolean_array(key: JavaString): Array[Bool]
289 import Array[Bool], Array[Bool].add in "Java" `{
290 boolean[] java_array = recv.getBooleanArray(key);
291 int nit_array = new_Array_of_Bool();
292
293 if (java_array == null) return nit_array;
294
295 for (int i=0; i < java_array.length; ++i)
296 Array_of_Bool_add(nit_array, java_array[i]);
297
298 return nit_array;
299 `}
300 fun get_byte_array(key: JavaString): Array[Int]
301 import Array[Int], Array[Int].add in "Java" `{
302 byte[] java_array = recv.getByteArray(key);
303 int nit_array = new_Array_of_Int();
304
305 if (java_array == null) return nit_array;
306
307 for(int i=0; i < java_array.length; ++i)
308 Array_of_Int_add(nit_array, (int) java_array[i]);
309
310 return nit_array;
311 `}
312 fun get_short_array(key: JavaString): Array[Int]
313 import Array[Int], Array[Int].add in "Java" `{
314 short[] java_array = recv.getShortArray(key);
315 int nit_array = new_Array_of_Int();
316
317 if (java_array == null) return nit_array;
318
319 for(int i=0; i < java_array.length; ++i)
320 Array_of_Int_add(nit_array, (int) java_array[i]);
321
322 return nit_array;
323 `}
324 fun get_char_array(key: JavaString): Array[Char]
325 import Array[Char], Array[Char].add in "Java" `{
326 char[] java_array = recv.getCharArray(key);
327 int nit_array = new_Array_of_Char();
328
329 if (java_array == null) return nit_array;
330
331 for(int i=0; i < java_array.length; ++i)
332 Array_of_Char_add(nit_array, java_array[i]);
333
334 return nit_array;
335 `}
336 fun get_int_array(key: JavaString): Array[Int]
337 import Array[Int], Array[Int].add in "Java" `{
338 int[] java_array = recv.getIntArray(key);
339 int nit_array = new_Array_of_Int();
340
341 if (java_array == null) return nit_array;
342
343 for(int i=0; i < java_array.length; ++i)
344 Array_of_Int_add(nit_array, java_array[i]);
345
346 return nit_array;
347 `}
348 # FIXME: Get rid of the int cast as soon as the ffi is fixed
349 fun get_long_array(key: JavaString): Array[Int]
350 import Array[Int], Array[Int].add in "Java" `{
351 long[] java_array = recv.getLongArray(key);
352 int nit_array = new_Array_of_Int();
353
354 if (java_array == null) return nit_array;
355
356 for(int i=0; i < java_array.length; ++i)
357 Array_of_Int_add(nit_array, (int) java_array[i]);
358
359 return nit_array;
360 `}
361 fun get_float_array(key: JavaString): Array[Float]
362 import Array[Float], Array[Float].add in "Java" `{
363 float[] java_array = recv.getFloatArray(key);
364 int nit_array = new_Array_of_Float();
365
366 if (java_array == null) return nit_array;
367
368 for(int i=0; i < java_array.length; ++i)
369 Array_of_Float_add(nit_array, (double) java_array[i]);
370
371 return nit_array;
372 `}
373 fun get_double_array(key: JavaString): Array[Float]
374 import Array[Float], Array[Float].add in "Java" `{
375 double[] java_array = recv.getDoubleArray(key);
376 int nit_array = new_Array_of_Float();
377
378 if (java_array == null) return nit_array;
379
380 for(int i=0; i < java_array.length; ++i)
381 Array_of_Float_add(nit_array, java_array[i]);
382
383 return nit_array;
384 `}
385 fun get_string_array(key: JavaString): Array[JavaString]
386 import Array[JavaString], Array[JavaString].add in "Java" `{
387 String[] java_array = recv.getStringArray(key);
388 int nit_array = new_Array_of_JavaString();
389
390 if (java_array == null) return nit_array;
391
392 for(int i=0; i < java_array.length; ++i)
393 Array_of_JavaString_add(nit_array, java_array[i]);
394
395 return nit_array;
396 `}
397 fun get_char_sequence_array(key: JavaString): Array[JavaString]
398 import Array[JavaString], Array[JavaString].add in "Java" `{
399 CharSequence[] java_array = recv.getCharSequenceArray(key);
400 int nit_array = new_Array_of_JavaString();
401
402 if (java_array == null) return nit_array;
403
404 for(int i=0; i < java_array.length; ++i)
405 Array_of_JavaString_add(nit_array, (String) java_array[i]);
406
407 return nit_array;
408 `}
409 fun describe_contents: Int in "Java" `{ return recv.describeContents(); `}
410 fun to_string: JavaString in "Java" `{ return recv.toString(); `}
411 end
412
413 # A class mapping `String` keys to various value types
414 class Bundle
415 private var native_bundle: NativeBundle
416 private var context: NativeActivity
417
418 init(app: App)
419 do
420 self.context = app.native_activity
421 setup
422 end
423
424 private fun set_vars(native_bundle: NativeBundle)
425 do
426 self.native_bundle = native_bundle.new_global_ref
427 end
428
429 private fun setup import context, set_vars in "Java" `{
430 Activity context = (Activity) Bundle_context(recv);
431 Bundle bundle = new Bundle();
432
433 Bundle_set_vars(recv, bundle);
434 `}
435
436 # Returns `true` if the Bundle contains this key
437 fun has(key: String): Bool
438 do
439 sys.jni_env.push_local_frame(1)
440 var return_value = native_bundle.contains_key(key.to_java_string)
441 sys.jni_env.pop_local_frame
442 return return_value
443 end
444
445 # Returns the number of entries in the current `Bundle`
446 fun size: Int do return native_bundle.size
447
448 # Returns true if the current `Bundle` is empty
449 fun is_empty: Bool do return native_bundle.is_empty
450
451 # Clears all entries
452 fun clear do native_bundle.clear
453
454 # Removes the entry associated with the given key
455 fun remove(key: String)
456 do
457 sys.jni_env.push_local_frame(1)
458 native_bundle.remove(key.to_java_string)
459 sys.jni_env.pop_local_frame
460 end
461
462 # Returns a `HashSet[String]` containing every mapping keys in the current
463 # `Bundle`
464 fun keys: HashSet[String]
465 do
466 var javastring_set = native_bundle.key_set
467 var string_set = new HashSet[String]
468
469 for element in javastring_set do
470 string_set.add(element.to_s)
471 end
472
473 return string_set
474 end
475
476 # Add key-value information by dynamically choosing the appropriate
477 # java method according to value type
478 # If there's already a value associated with this key, the new value
479 # overwrites it
480 #
481 # To retrieve entries, you'll have to call the type corresponding method
482 # conforming to these rules :
483 #
484 # | Nit type | corresponding getter |
485 # |:----------------------|:--------------------------------|
486 # ! `Int` | `long` |
487 # | `Float` | `double` |
488 # | `Bool` | `bool` |
489 # | `Char` | `char` |
490 # ! `String` | `string` |
491 # ! `Serializable` | `deserialize` |
492 # ! `Array[Int]` | `array_of_long` |
493 # ! `Array[Float]` | `array_of_double` |
494 # ! `Array[Bool]` | `array_of_bool` |
495 # ! `Array[Char]` | `array_of_char` |
496 # ! `Array[String]` | `array_of_string` |
497 # | `Array[Serializable]` | `deserialize_array` |
498 fun []=(key: String, value: Serializable): Bundle
499 do
500 sys.jni_env.push_local_frame(1)
501 value.add_to_bundle(self.native_bundle, key.to_java_string)
502 sys.jni_env.pop_local_frame
503 return self
504 end
505
506 # Retrieve an `Object` serialized via `[]=` function
507 # Returns `null` if there's no serialized object corresponding to the given key
508 # or if it's the wrong value type
509 # Make sure that the serialized object is `auto_serializable` or that it
510 # redefines the appropriate methods. Refer to `Serializable` documentation
511 # for further details
512 fun deserialize(key: String): nullable Object
513 do
514 var serialized_string = self.string(key)
515
516 if serialized_string == null then return null
517
518 var deserializer = new JsonDeserializer(serialized_string)
519
520 return deserializer.deserialize
521 end
522
523 # Retrieve an `Array` of `Object` serialized via `[]=` function
524 # Returns `null` if there's no serialized `Array` corresponding to the given key
525 # or if it's the wrong value type
526 # Make sure that the serialized objects are `auto_serializable` or that they
527 # redefine the appropriate methods. Refer to `Serializable` documentation
528 # for further details
529 fun deserialize_array(key: String): nullable Array[nullable Object]
530 do
531 var serialized_array = self.array_of_string(key)
532
533 if serialized_array == null then return null
534
535 var deserialized_array = new Array[nullable Object]
536
537 for serialized_element in serialized_array do
538 var deserializer = new JsonDeserializer(serialized_element)
539 deserialized_array.add(deserializer.deserialize)
540 end
541
542 return deserialized_array
543 end
544
545 # Retrieves the `String` value corresponding to the given key
546 # Returns `null` if none or if it's the wrong value type
547 fun string(key: String): nullable String
548 do
549 sys.jni_env.push_local_frame(1)
550 var return_value = native_bundle.get_string(key.to_java_string).to_s
551 sys.jni_env.pop_local_frame
552
553 if return_value == "" then return null
554
555 return return_value.to_s
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 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 fun array_of_char(key: String): nullable Array[Char]
618 do
619 sys.jni_env.push_local_frame(1)
620 var return_value = native_bundle.get_char_array(key.to_java_string)
621 sys.jni_env.pop_local_frame
622
623 if return_value.is_empty then return null
624
625 return return_value
626 end
627 # Retrieves the `Array[Int]` value corresponding to the given key
628 # Returns the `null` if none or if it's the wrong value type
629 fun array_of_int(key: String): nullable Array[Int]
630 do
631 sys.jni_env.push_local_frame(1)
632 var return_value = native_bundle.get_long_array(key.to_java_string)
633 sys.jni_env.pop_local_frame
634
635 if return_value.is_empty then return null
636
637 return return_value
638 end
639
640 # Retrieves the `Array[Bool]` value corresponding to the given key
641 # Returns the `null` if none or if it's the wrong value type
642 fun array_of_bool(key: String): nullable Array[Bool]
643 do
644 sys.jni_env.push_local_frame(1)
645 var return_value = native_bundle.get_boolean_array(key.to_java_string)
646 sys.jni_env.pop_local_frame
647
648 if return_value.is_empty then return null
649
650 return return_value
651 end
652
653 # Retrieves the `Array[String]` value corresponding to the given key
654 # Returns the `null` if none or if it's the wrong value type
655 fun array_of_string(key: String): nullable Array[String]
656 do
657 var string_array = new Array[String]
658 sys.jni_env.push_local_frame(1)
659 var return_value = native_bundle.get_string_array(key.to_java_string)
660
661 for element in return_value
662 do
663 string_array.add(element.to_s)
664 end
665
666 sys.jni_env.pop_local_frame
667
668 if return_value.is_empty then return null
669
670 return string_array
671 end
672 end
673
674 redef class Serializable
675 # Called by `Bundle::[]=` to dynamically choose the appropriate method according
676 # to the value type to store
677 # Non-primitive Object (`String` excluded) will be stored as a serialized json `String`
678 # Refine your class to customize this method behaviour
679 protected fun add_to_bundle(bundle: NativeBundle, key: JavaString)
680 do
681 sys.jni_env.push_local_frame(1)
682 var serialized_string = new StringOStream
683 var serializer = new JsonSerializer(serialized_string)
684 serializer.serialize(self)
685
686 bundle.put_string(key, serialized_string.to_s.to_java_string)
687 end
688 end
689
690 redef class Int
691 redef fun add_to_bundle(bundle, key)
692 do
693 bundle.put_long(key, self)
694 end
695 end
696
697 redef class Char
698 redef fun add_to_bundle(bundle, key)
699 do
700 bundle.put_char(key, self)
701 end
702 end
703
704 redef class Float
705 redef fun add_to_bundle(bundle, key)
706 do
707 bundle.put_double(key, self)
708 end
709 end
710
711 redef class Bool
712 redef fun add_to_bundle(bundle, key)
713 do
714 bundle.put_boolean(key, self)
715 end
716 end
717
718 redef class String
719 redef fun add_to_bundle(bundle, key)
720 do
721 bundle.put_string(key, self.to_java_string)
722 end
723 end
724
725 redef class Array[E]
726 redef fun add_to_bundle(bundle, key)
727 do
728 if self isa Array[Bool] then
729 bundle.put_boolean_array(key, self)
730 else if self isa Array[Int] then
731 bundle.put_long_array(key, self)
732 else if self isa Array[Float] then
733 bundle.put_double_array(key, self)
734 else if self isa Array[Char] then
735 bundle.put_char_array(key, self)
736 else if self isa Array[String] then
737 sys.jni_env.push_local_frame(self.length)
738 var java_string_array = new Array[JavaString]
739
740 for element in self do
741 java_string_array.push(element.to_s.to_java_string)
742 end
743
744 bundle.put_string_array(key, java_string_array)
745 else if self isa Array[Serializable] then
746 sys.jni_env.push_local_frame(self.length)
747 var java_string_array = new Array[JavaString]
748
749 for element in self do
750 var serialized_string = new StringOStream
751 var serializer = new JsonSerializer(serialized_string)
752 serializer.serialize(element)
753 java_string_array.add(serialized_string.to_s.to_java_string)
754 end
755
756 bundle.put_string_array(key, java_string_array)
757 end
758 end
759 end