lib/android: update to Java `long` to represent a Nit `Int`
[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>((int) 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>((int)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>((int)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[(int)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[(int)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[(int)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[(int)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[(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[(int)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[(int)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[(int)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[(int)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[(int)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 recv.getInt(key, (int) def_value);
226 `}
227 fun get_long(key: JavaString): Int in "Java" `{ return recv.getLong(key); `}
228 fun get_long_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
229 return recv.getLong(key);
230 `}
231 fun get_float(key: JavaString): Float in "Java" `{
232 return (float) recv.getFloat(key);
233 `}
234 fun get_float_with_def_value(key: JavaString, def_value: Float): Float in "Java" `{
235 return (float) recv.getFloat(key, (float) def_value);
236 `}
237 fun get_double(key: JavaString): Float in "Java" `{ return recv.getDouble(key); `}
238 fun get_double_with_def_value(key: JavaString, def_value: Float): Float in "Java" `{
239 return recv.getDouble(key, def_value);
240 `}
241 fun get_string(key: JavaString): JavaString in "Java" `{
242 return recv.getString(key);
243 `}
244 fun get_char_sequence(key: JavaString): JavaString in "Java" `{
245 return (String) recv.getCharSequence(key);
246 `}
247 fun get_bundle(key: JavaString): NativeBundle in "Java" `{
248 return recv.getBundle(key);
249 `}
250 fun get_integer_array_list(key: JavaString): Array[Int]
251 import Array[Int], Array[Int].add in "Java" `{
252 ArrayList<Integer> java_array = recv.getIntegerArrayList(key);
253 int nit_array = new_Array_of_Int();
254
255 if (java_array == null) return nit_array;
256
257 for (int element: java_array)
258 Array_of_Int_add(nit_array, element);
259
260 return nit_array;
261 `}
262 fun get_string_array_list(key: JavaString): Array[JavaString]
263 import Array[JavaString], Array[JavaString].add in "Java" `{
264 ArrayList<String> java_array = recv.getStringArrayList(key);
265 int nit_array = new_Array_of_JavaString();
266
267 if (java_array == null) return nit_array;
268
269 for (String element: java_array)
270 Array_of_JavaString_add(nit_array, element);
271
272 return nit_array;
273 `}
274 fun get_char_sequence_array_list(key: JavaString): Array[JavaString]
275 import Array[JavaString], Array[JavaString].add in "Java" `{
276 ArrayList<CharSequence> java_array = recv.getCharSequenceArrayList(key);
277 int nit_array = new_Array_of_JavaString();
278
279 if (java_array == null) return nit_array;
280
281 for (CharSequence element: java_array)
282 Array_of_JavaString_add(nit_array, (String) element);
283
284 return nit_array;
285 `}
286 fun get_boolean_array(key: JavaString): Array[Bool]
287 import Array[Bool], Array[Bool].add in "Java" `{
288 boolean[] java_array = recv.getBooleanArray(key);
289 int nit_array = new_Array_of_Bool();
290
291 if (java_array == null) return nit_array;
292
293 for (int i=0; i < java_array.length; ++i)
294 Array_of_Bool_add(nit_array, java_array[i]);
295
296 return nit_array;
297 `}
298 fun get_byte_array(key: JavaString): Array[Int]
299 import Array[Int], Array[Int].add in "Java" `{
300 byte[] java_array = recv.getByteArray(key);
301 int nit_array = new_Array_of_Int();
302
303 if (java_array == null) return nit_array;
304
305 for(int i=0; i < java_array.length; ++i)
306 Array_of_Int_add(nit_array, java_array[i]);
307
308 return nit_array;
309 `}
310 fun get_short_array(key: JavaString): Array[Int]
311 import Array[Int], Array[Int].add in "Java" `{
312 short[] java_array = recv.getShortArray(key);
313 int nit_array = new_Array_of_Int();
314
315 if (java_array == null) return nit_array;
316
317 for(int i=0; i < java_array.length; ++i)
318 Array_of_Int_add(nit_array, java_array[i]);
319
320 return nit_array;
321 `}
322 fun get_char_array(key: JavaString): Array[Char]
323 import Array[Char], Array[Char].add in "Java" `{
324 char[] java_array = recv.getCharArray(key);
325 int nit_array = new_Array_of_Char();
326
327 if (java_array == null) return nit_array;
328
329 for(int i=0; i < java_array.length; ++i)
330 Array_of_Char_add(nit_array, java_array[i]);
331
332 return nit_array;
333 `}
334 fun get_int_array(key: JavaString): Array[Int]
335 import Array[Int], Array[Int].add in "Java" `{
336 int[] java_array = recv.getIntArray(key);
337 int nit_array = new_Array_of_Int();
338
339 if (java_array == null) return nit_array;
340
341 for(int i=0; i < java_array.length; ++i)
342 Array_of_Int_add(nit_array, java_array[i]);
343
344 return nit_array;
345 `}
346 # FIXME: Get rid of the int cast as soon as the ffi is fixed
347 fun get_long_array(key: JavaString): Array[Int]
348 import Array[Int], Array[Int].add in "Java" `{
349 long[] java_array = recv.getLongArray(key);
350 int nit_array = new_Array_of_Int();
351
352 if (java_array == null) return nit_array;
353
354 for(int i=0; i < java_array.length; ++i)
355 Array_of_Int_add(nit_array, java_array[i]);
356
357 return nit_array;
358 `}
359 fun get_float_array(key: JavaString): Array[Float]
360 import Array[Float], Array[Float].add in "Java" `{
361 float[] java_array = recv.getFloatArray(key);
362 int nit_array = new_Array_of_Float();
363
364 if (java_array == null) return nit_array;
365
366 for(int i=0; i < java_array.length; ++i)
367 Array_of_Float_add(nit_array, (double) java_array[i]);
368
369 return nit_array;
370 `}
371 fun get_double_array(key: JavaString): Array[Float]
372 import Array[Float], Array[Float].add in "Java" `{
373 double[] java_array = recv.getDoubleArray(key);
374 int nit_array = new_Array_of_Float();
375
376 if (java_array == null) return nit_array;
377
378 for(int i=0; i < java_array.length; ++i)
379 Array_of_Float_add(nit_array, java_array[i]);
380
381 return nit_array;
382 `}
383 fun get_string_array(key: JavaString): Array[JavaString]
384 import Array[JavaString], Array[JavaString].add in "Java" `{
385 String[] java_array = recv.getStringArray(key);
386 int nit_array = new_Array_of_JavaString();
387
388 if (java_array == null) return nit_array;
389
390 for(int i=0; i < java_array.length; ++i)
391 Array_of_JavaString_add(nit_array, java_array[i]);
392
393 return nit_array;
394 `}
395 fun get_char_sequence_array(key: JavaString): Array[JavaString]
396 import Array[JavaString], Array[JavaString].add in "Java" `{
397 CharSequence[] java_array = recv.getCharSequenceArray(key);
398 int nit_array = new_Array_of_JavaString();
399
400 if (java_array == null) return nit_array;
401
402 for(int i=0; i < java_array.length; ++i)
403 Array_of_JavaString_add(nit_array, (String) java_array[i]);
404
405 return nit_array;
406 `}
407 fun describe_contents: Int in "Java" `{ return recv.describeContents(); `}
408 fun to_string: JavaString in "Java" `{ return recv.toString(); `}
409 end
410
411 # A class mapping `String` keys to various value types
412 class Bundle
413 private var native_bundle: NativeBundle
414 private var context: NativeActivity
415
416 init(app: App)
417 do
418 self.context = app.native_activity
419 setup
420 end
421
422 private fun set_vars(native_bundle: NativeBundle)
423 do
424 self.native_bundle = native_bundle.new_global_ref
425 end
426
427 private fun setup import context, set_vars in "Java" `{
428 Activity context = (Activity) Bundle_context(recv);
429 Bundle bundle = new Bundle();
430
431 Bundle_set_vars(recv, bundle);
432 `}
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 `auto_serializable` 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 `auto_serializable` 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(1)
548 var return_value = native_bundle.get_string(key.to_java_string).to_s
549 sys.jni_env.pop_local_frame
550
551 if return_value == "" then return null
552
553 return return_value.to_s
554 end
555
556 # Retrieves the `Bool` value corresponding to the given key
557 # Returns the `def_value` if none or if it's the wrong value type
558 fun bool(key: String, def_value: Bool): Bool
559 do
560 sys.jni_env.push_local_frame(1)
561 var return_value =
562 native_bundle.get_boolean_with_def_value(key.to_java_string, def_value)
563 sys.jni_env.pop_local_frame
564 return return_value
565 end
566
567 # Retrieves the `Char` value corresponding to the given key
568 # Returns the `def_value` if none or if it's the wrong value type
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 fun array_of_char(key: String): nullable Array[Char]
616 do
617 sys.jni_env.push_local_frame(1)
618 var return_value = native_bundle.get_char_array(key.to_java_string)
619 sys.jni_env.pop_local_frame
620
621 if return_value.is_empty then return null
622
623 return return_value
624 end
625 # Retrieves the `Array[Int]` value corresponding to the given key
626 # Returns the `null` if none or if it's the wrong value type
627 fun array_of_int(key: String): nullable Array[Int]
628 do
629 sys.jni_env.push_local_frame(1)
630 var return_value = native_bundle.get_long_array(key.to_java_string)
631 sys.jni_env.pop_local_frame
632
633 if return_value.is_empty then return null
634
635 return return_value
636 end
637
638 # Retrieves the `Array[Bool]` value corresponding to the given key
639 # Returns the `null` if none or if it's the wrong value type
640 fun array_of_bool(key: String): nullable Array[Bool]
641 do
642 sys.jni_env.push_local_frame(1)
643 var return_value = native_bundle.get_boolean_array(key.to_java_string)
644 sys.jni_env.pop_local_frame
645
646 if return_value.is_empty then return null
647
648 return return_value
649 end
650
651 # Retrieves the `Array[String]` value corresponding to the given key
652 # Returns the `null` if none or if it's the wrong value type
653 fun array_of_string(key: String): nullable Array[String]
654 do
655 var string_array = new Array[String]
656 sys.jni_env.push_local_frame(1)
657 var return_value = native_bundle.get_string_array(key.to_java_string)
658
659 for element in return_value
660 do
661 string_array.add(element.to_s)
662 end
663
664 sys.jni_env.pop_local_frame
665
666 if return_value.is_empty then return null
667
668 return string_array
669 end
670 end
671
672 redef class Serializable
673 # Called by `Bundle::[]=` to dynamically choose the appropriate method according
674 # to the value type to store
675 # Non-primitive Object (`String` excluded) will be stored as a serialized json `String`
676 # Refine your class to customize this method behaviour
677 protected fun add_to_bundle(bundle: NativeBundle, key: JavaString)
678 do
679 sys.jni_env.push_local_frame(1)
680 var serialized_string = new StringOStream
681 var serializer = new JsonSerializer(serialized_string)
682 serializer.serialize(self)
683
684 bundle.put_string(key, serialized_string.to_s.to_java_string)
685 end
686 end
687
688 redef class Int
689 redef fun add_to_bundle(bundle, key)
690 do
691 bundle.put_long(key, self)
692 end
693 end
694
695 redef class Char
696 redef fun add_to_bundle(bundle, key)
697 do
698 bundle.put_char(key, self)
699 end
700 end
701
702 redef class Float
703 redef fun add_to_bundle(bundle, key)
704 do
705 bundle.put_double(key, self)
706 end
707 end
708
709 redef class Bool
710 redef fun add_to_bundle(bundle, key)
711 do
712 bundle.put_boolean(key, self)
713 end
714 end
715
716 redef class String
717 redef fun add_to_bundle(bundle, key)
718 do
719 bundle.put_string(key, self.to_java_string)
720 end
721 end
722
723 redef class Array[E]
724 redef fun add_to_bundle(bundle, key)
725 do
726 if self isa Array[Bool] then
727 bundle.put_boolean_array(key, self)
728 else if self isa Array[Int] then
729 bundle.put_long_array(key, self)
730 else if self isa Array[Float] then
731 bundle.put_double_array(key, self)
732 else if self isa Array[Char] then
733 bundle.put_char_array(key, self)
734 else if self isa Array[String] then
735 sys.jni_env.push_local_frame(self.length)
736 var java_string_array = new Array[JavaString]
737
738 for element in self do
739 java_string_array.push(element.to_s.to_java_string)
740 end
741
742 bundle.put_string_array(key, java_string_array)
743 else if self isa Array[Serializable] then
744 sys.jni_env.push_local_frame(self.length)
745 var java_string_array = new Array[JavaString]
746
747 for element in self do
748 var serialized_string = new StringOStream
749 var serializer = new JsonSerializer(serialized_string)
750 serializer.serialize(element)
751 java_string_array.add(serialized_string.to_s.to_java_string)
752 end
753
754 bundle.put_string_array(key, java_string_array)
755 end
756 end
757 end