5b01369a637ad24a342ba52e67d5f943cb029691
[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
35 fun clone: JavaObject in "Java" `{ return recv.clone(); `}
36 fun size: Int in "Java" `{ return recv.size(); `}
37 fun is_empty: Bool in "Java" `{ return recv.isEmpty(); `}
38 fun clear in "Java" `{ recv.clear(); `}
39 fun contains_key(key: JavaString): Bool in "Java" `{ return recv.containsKey(key); `}
40 fun get(key: JavaString): JavaObject in "Java" `{ return recv.get(key); `}
41 fun remove(key: JavaString) in "Java" `{ recv.remove(key); `}
42 fun put_all(bundle: NativeBundle) in "Java" `{ recv.putAll(bundle); `}
43 fun key_set: HashSet[JavaString] import HashSet[JavaString],
44 HashSet[JavaString].add in "Java" `{
45 Set<String> java_set = recv.keySet();
46 int nit_hashset = new_HashSet_of_JavaString();
47
48 for (String element: java_set)
49 HashSet_of_JavaString_add(nit_hashset, element);
50
51 return nit_hashset;
52 `}
53 fun has_file_descriptors: Bool in "Java" `{ return recv.hasFileDescriptors(); `}
54 fun put_boolean(key: JavaString, value: Bool) in "Java" `{
55 recv.putBoolean(key, value);
56 `}
57 fun put_byte(key: JavaString, value: Int) in "Java" `{
58 recv.putByte(key, (byte) value);
59 `}
60 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
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 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
142 fun put_char_array(key: JavaString, value: Array[Char])
143 import Array[Char].length, Array[Char].[] in "Java" `{
144 char[] java_array = new char[(int)Array_of_Char_length(value)];
145
146 for(int i=0; i < java_array.length; ++i)
147 java_array[i] = Array_of_Char__index(value, i);
148
149 recv.putCharArray(key, java_array);
150 `}
151 fun put_int_array(key: JavaString, value: Array[Int])
152 import Array[Int].length, Array[Int].[] in "Java" `{
153 int[] java_array = new int[(int)Array_of_Int_length(value)];
154
155 for(int i=0; i < java_array.length; ++i)
156 java_array[i] = (int) Array_of_Int__index(value, i);
157
158 recv.putIntArray(key, java_array);
159 `}
160 fun put_long_array(key: JavaString, value: Array[Int])
161 import Array[Int].length, Array[Int].[] in "Java" `{
162 long[] java_array = new long[(int)Array_of_Int_length(value)];
163
164 for(int i=0; i < java_array.length; ++i)
165 java_array[i] = Array_of_Int__index(value, i);
166
167 recv.putLongArray(key, java_array);
168 `}
169 fun put_float_array(key: JavaString, value: Array[Float])
170 import Array[Float].length, Array[Float].[] in "Java" `{
171 float[] java_array = new float[(int)Array_of_Float_length(value)];
172
173 for(int i=0; i < java_array.length; ++i)
174 java_array[i] = (float) Array_of_Float__index(value, i);
175
176 recv.putFloatArray(key, java_array);
177 `}
178 fun put_double_array(key: JavaString, value: Array[Float])
179 import Array[Float].length, Array[Float].[] in "Java" `{
180 double[] java_array = new double[(int)Array_of_Float_length(value)];
181
182 for(int i=0; i < java_array.length; ++i)
183 java_array[i] = Array_of_Float__index(value, i);
184
185 recv.putDoubleArray(key, java_array);
186 `}
187 fun put_string_array(key: JavaString, value: Array[JavaString])
188 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
189 String[] java_array = new String[(int)Array_of_JavaString_length(value)];
190
191 for(int i=0; i < java_array.length; ++i)
192 java_array[i] = Array_of_JavaString__index(value, i);
193
194 recv.putStringArray(key, java_array);
195 `}
196 fun put_char_sequence_array(key: JavaString, value: Array[JavaString])
197 import Array[JavaString].length, Array[JavaString].[] in "Java" `{
198 CharSequence[] java_array = new CharSequence[(int)Array_of_JavaString_length(value)];
199
200 for(int i=0; i < java_array.length; ++i)
201 java_array[i] = Array_of_JavaString__index(value, i);
202
203 recv.putCharSequenceArray(key, java_array);
204 `}
205 fun put_bundle(key: JavaString, value: NativeBundle) in "Java" `{
206 recv.putBundle(key, value);
207 `}
208 fun get_boolean(key: JavaString): Bool in "Java" `{ return recv.getBoolean(key); `}
209 fun get_boolean_with_def_value(key: JavaString, def_value: Bool): Bool in "Java" `{
210 return recv.getBoolean(key, def_value);
211 `}
212 fun get_byte(key: JavaString): Int in "Java" `{ return recv.getByte(key); `}
213 fun get_byte_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
214 return recv.getByte(key, (byte) def_value);
215 `}
216 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
217 fun get_char(key: JavaString): Char in "Java" `{ return recv.getChar(key); `}
218 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
219 fun get_char_with_def_value(key: JavaString, def_value: Char): Char in "Java" `{
220 return recv.getChar(key, def_value);
221 `}
222 fun get_short(key: JavaString): Int in "Java" `{ return (short) recv.getShort(key); `}
223 fun get_short_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
224 return (short) recv.getShort(key, (short) def_value);
225 `}
226 fun get_int(key: JavaString): Int in "Java" `{ return recv.getInt(key); `}
227 fun get_int_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
228 return recv.getInt(key, (int) def_value);
229 `}
230 fun get_long(key: JavaString): Int in "Java" `{ return recv.getLong(key); `}
231 fun get_long_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
232 return recv.getLong(key);
233 `}
234 fun get_float(key: JavaString): Float in "Java" `{
235 return (float) recv.getFloat(key);
236 `}
237 fun get_float_with_def_value(key: JavaString, def_value: Float): Float in "Java" `{
238 return (float) recv.getFloat(key, (float) def_value);
239 `}
240 fun get_double(key: JavaString): Float in "Java" `{ return recv.getDouble(key); `}
241 fun get_double_with_def_value(key: JavaString, def_value: Float): Float in "Java" `{
242 return recv.getDouble(key, def_value);
243 `}
244 fun get_string(key: JavaString): JavaString in "Java" `{
245 return recv.getString(key);
246 `}
247 fun get_char_sequence(key: JavaString): JavaString in "Java" `{
248 return (String) recv.getCharSequence(key);
249 `}
250 fun get_bundle(key: JavaString): NativeBundle in "Java" `{
251 return recv.getBundle(key);
252 `}
253 fun get_integer_array_list(key: JavaString): Array[Int]
254 import Array[Int], Array[Int].add in "Java" `{
255 ArrayList<Integer> java_array = recv.getIntegerArrayList(key);
256 int nit_array = new_Array_of_Int();
257
258 if (java_array == null) return nit_array;
259
260 for (int element: java_array)
261 Array_of_Int_add(nit_array, element);
262
263 return nit_array;
264 `}
265 fun get_string_array_list(key: JavaString): Array[String]
266 import StringCopyArray, StringCopyArray.add, StringCopyArray.collection in "Java" `{
267 ArrayList<String> java_array = recv.getStringArrayList(key);
268 int nit_array = new_StringCopyArray();
269
270 if (java_array == null) return nit_array;
271
272 for (String element: java_array)
273 StringCopyArray_add(nit_array, element);
274
275 return StringCopyArray_collection(nit_array);
276 `}
277 fun get_char_sequence_array_list(key: JavaString): Array[String]
278 import StringCopyArray, StringCopyArray.add, StringCopyArray.collection in "Java" `{
279 ArrayList<CharSequence> java_array = recv.getCharSequenceArrayList(key);
280 int nit_array = new_StringCopyArray();
281
282 if (java_array == null) return nit_array;
283
284 for (CharSequence element: java_array)
285 StringCopyArray_add(nit_array, (String) element);
286
287 return StringCopyArray_collection(nit_array);
288 `}
289 fun get_boolean_array(key: JavaString): Array[Bool]
290 import Array[Bool], Array[Bool].add in "Java" `{
291 boolean[] java_array = recv.getBooleanArray(key);
292 int nit_array = new_Array_of_Bool();
293
294 if (java_array == null) return nit_array;
295
296 for (int i=0; i < java_array.length; ++i)
297 Array_of_Bool_add(nit_array, java_array[i]);
298
299 return nit_array;
300 `}
301 fun get_byte_array(key: JavaString): Array[Int]
302 import Array[Int], Array[Int].add in "Java" `{
303 byte[] java_array = recv.getByteArray(key);
304 int nit_array = new_Array_of_Int();
305
306 if (java_array == null) return nit_array;
307
308 for(int i=0; i < java_array.length; ++i)
309 Array_of_Int_add(nit_array, java_array[i]);
310
311 return nit_array;
312 `}
313 fun get_short_array(key: JavaString): Array[Int]
314 import Array[Int], Array[Int].add in "Java" `{
315 short[] java_array = recv.getShortArray(key);
316 int nit_array = new_Array_of_Int();
317
318 if (java_array == null) return nit_array;
319
320 for(int i=0; i < java_array.length; ++i)
321 Array_of_Int_add(nit_array, java_array[i]);
322
323 return nit_array;
324 `}
325 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
326 fun get_char_array(key: JavaString): Array[Char]
327 import Array[Char], Array[Char].add in "Java" `{
328 char[] java_array = recv.getCharArray(key);
329 int nit_array = new_Array_of_Char();
330
331 if (java_array == null) return nit_array;
332
333 for(int i=0; i < java_array.length; ++i)
334 Array_of_Char_add(nit_array, java_array[i]);
335
336 return nit_array;
337 `}
338 fun get_int_array(key: JavaString): Array[Int]
339 import Array[Int], Array[Int].add in "Java" `{
340 int[] java_array = recv.getIntArray(key);
341 int nit_array = new_Array_of_Int();
342
343 if (java_array == null) return nit_array;
344
345 for(int i=0; i < java_array.length; ++i)
346 Array_of_Int_add(nit_array, java_array[i]);
347
348 return nit_array;
349 `}
350 # FIXME: Get rid of the int cast as soon as the ffi is fixed
351 fun get_long_array(key: JavaString): Array[Int]
352 import Array[Int], Array[Int].add in "Java" `{
353 long[] java_array = recv.getLongArray(key);
354 int nit_array = new_Array_of_Int();
355
356 if (java_array == null) return nit_array;
357
358 for(int i=0; i < java_array.length; ++i)
359 Array_of_Int_add(nit_array, java_array[i]);
360
361 return nit_array;
362 `}
363 fun get_float_array(key: JavaString): Array[Float]
364 import Array[Float], Array[Float].add in "Java" `{
365 float[] java_array = recv.getFloatArray(key);
366 int nit_array = new_Array_of_Float();
367
368 if (java_array == null) return nit_array;
369
370 for(int i=0; i < java_array.length; ++i)
371 Array_of_Float_add(nit_array, (double) java_array[i]);
372
373 return nit_array;
374 `}
375 fun get_double_array(key: JavaString): Array[Float]
376 import Array[Float], Array[Float].add in "Java" `{
377 double[] java_array = recv.getDoubleArray(key);
378 int nit_array = new_Array_of_Float();
379
380 if (java_array == null) return nit_array;
381
382 for(int i=0; i < java_array.length; ++i)
383 Array_of_Float_add(nit_array, java_array[i]);
384
385 return nit_array;
386 `}
387 fun get_string_array(key: JavaString): Array[String]
388 import StringCopyArray, StringCopyArray.add, StringCopyArray.collection in "Java" `{
389 String[] java_array = recv.getStringArray(key);
390 int nit_array = new_StringCopyArray();
391
392 if (java_array == null) return nit_array;
393
394 for(int i=0; i < java_array.length; ++i)
395 StringCopyArray_add(nit_array, java_array[i]);
396
397 return StringCopyArray_collection(nit_array);
398 `}
399 fun get_char_sequence_array(key: JavaString): Array[String]
400 import StringCopyArray, StringCopyArray.add, StringCopyArray.collection in "Java" `{
401 CharSequence[] java_array = recv.getCharSequenceArray(key);
402 int nit_array = new_StringCopyArray();
403
404 if (java_array == null) return nit_array;
405
406 for(int i=0; i < java_array.length; ++i)
407 StringCopyArray_add(nit_array, (String)java_array[i]);
408
409 return StringCopyArray_collection(nit_array);
410 `}
411 fun describe_contents: Int in "Java" `{ return recv.describeContents(); `}
412 fun to_string: JavaString in "Java" `{ return recv.toString(); `}
413
414 # HACK for bug #845
415 redef fun new_global_ref import sys, Sys.jni_env `{
416 Sys sys = NativeBundle_sys(recv);
417 JNIEnv *env = Sys_jni_env(sys);
418 return (*env)->NewGlobalRef(env, recv);
419 `}
420 end
421
422 # A class mapping `String` keys to various value types
423 class Bundle
424 private var native_bundle: NativeBundle
425 private var context: NativeActivity
426
427 init(app: App)
428 do
429 self.context = app.native_activity
430 setup
431 end
432
433 private fun set_vars(native_bundle: NativeBundle)
434 do
435 self.native_bundle = native_bundle.new_global_ref
436 end
437
438 private fun setup import context, set_vars in "Java" `{
439 Activity context = (Activity) Bundle_context(recv);
440 Bundle bundle = new Bundle();
441
442 Bundle_set_vars(recv, bundle);
443 `}
444
445 # Returns `true` if the Bundle contains this key
446 fun has(key: String): Bool
447 do
448 sys.jni_env.push_local_frame(1)
449 var return_value = native_bundle.contains_key(key.to_java_string)
450 sys.jni_env.pop_local_frame
451 return return_value
452 end
453
454 # Returns the number of entries in the current `Bundle`
455 fun size: Int do return native_bundle.size
456
457 # Returns true if the current `Bundle` is empty
458 fun is_empty: Bool do return native_bundle.is_empty
459
460 # Clears all entries
461 fun clear do native_bundle.clear
462
463 # Removes the entry associated with the given key
464 fun remove(key: String)
465 do
466 sys.jni_env.push_local_frame(1)
467 native_bundle.remove(key.to_java_string)
468 sys.jni_env.pop_local_frame
469 end
470
471 # Returns a `HashSet[String]` containing every mapping keys in the current
472 # `Bundle`
473 fun keys: HashSet[String]
474 do
475 var javastring_set = native_bundle.key_set
476 var string_set = new HashSet[String]
477
478 for element in javastring_set do
479 string_set.add(element.to_s)
480 end
481
482 return string_set
483 end
484
485 # Add key-value information by dynamically choosing the appropriate
486 # java method according to value type
487 # If there's already a value associated with this key, the new value
488 # overwrites it
489 #
490 # To retrieve entries, you'll have to call the type corresponding method
491 # conforming to these rules :
492 #
493 # | Nit type | corresponding getter |
494 # |:----------------------|:--------------------------------|
495 # ! `Int` | `long` |
496 # | `Float` | `double` |
497 # | `Bool` | `bool` |
498 # | `Char` | `char` |
499 # ! `String` | `string` |
500 # ! `Serializable` | `deserialize` |
501 # ! `Array[Int]` | `array_of_long` |
502 # ! `Array[Float]` | `array_of_double` |
503 # ! `Array[Bool]` | `array_of_bool` |
504 # ! `Array[Char]` | `array_of_char` |
505 # ! `Array[String]` | `array_of_string` |
506 # | `Array[Serializable]` | `deserialize_array` |
507 fun []=(key: String, value: Serializable): Bundle
508 do
509 sys.jni_env.push_local_frame(1)
510 value.add_to_bundle(self.native_bundle, key.to_java_string)
511 sys.jni_env.pop_local_frame
512 return self
513 end
514
515 # Retrieve an `Object` serialized via `[]=` function
516 # Returns `null` if there's no serialized object corresponding to the given key
517 # or if it's the wrong value type
518 # Make sure that the serialized object is `auto_serializable` or that it
519 # redefines the appropriate methods. Refer to `Serializable` documentation
520 # for further details
521 fun deserialize(key: String): nullable Object
522 do
523 var serialized_string = self.string(key)
524
525 if serialized_string == null then return null
526
527 var deserializer = new JsonDeserializer(serialized_string)
528
529 return deserializer.deserialize
530 end
531
532 # Retrieve an `Array` of `Object` serialized via `[]=` function
533 # Returns `null` if there's no serialized `Array` corresponding to the given key
534 # or if it's the wrong value type
535 # Make sure that the serialized objects are `auto_serializable` or that they
536 # redefine the appropriate methods. Refer to `Serializable` documentation
537 # for further details
538 fun deserialize_array(key: String): nullable Array[nullable Object]
539 do
540 var serialized_array = self.array_of_string(key)
541
542 if serialized_array == null then return null
543
544 var deserialized_array = new Array[nullable Object]
545
546 for serialized_element in serialized_array do
547 var deserializer = new JsonDeserializer(serialized_element)
548 deserialized_array.add(deserializer.deserialize)
549 end
550
551 return deserialized_array
552 end
553
554 # Retrieves the `String` value corresponding to the given key
555 # Returns `null` if none or if it's the wrong value type
556 fun string(key: String): nullable String
557 do
558 sys.jni_env.push_local_frame(1)
559 var return_value = native_bundle.get_string(key.to_java_string).to_s
560 sys.jni_env.pop_local_frame
561
562 if return_value == "" then return null
563
564 return return_value
565 end
566
567 # Retrieves the `Bool` value corresponding to the given key
568 # Returns the `def_value` if none or if it's the wrong value type
569 fun bool(key: String, def_value: Bool): Bool
570 do
571 sys.jni_env.push_local_frame(1)
572 var return_value =
573 native_bundle.get_boolean_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 `Char` value corresponding to the given key
579 # Returns the `def_value` if none or if it's the wrong value type
580 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
581 fun char(key: String, def_value: Char): Char
582 do
583 sys.jni_env.push_local_frame(1)
584 var return_value =
585 native_bundle.get_char_with_def_value(key.to_java_string, def_value)
586 sys.jni_env.pop_local_frame
587 return return_value
588 end
589
590 # Retrieves the `Int` value corresponding to the given key
591 # Returns the `def_value` if none or if it's the wrong value type
592 fun int(key: String, def_value: Int): Int
593 do
594 sys.jni_env.push_local_frame(1)
595 var return_value =
596 native_bundle.get_long_with_def_value(key.to_java_string, def_value)
597 sys.jni_env.pop_local_frame
598 return return_value
599 end
600
601 # Retrieves the `Float` value corresponding to the given key
602 # Returns the `def_value` if none or if it's the wrong value type
603 fun float(key: String, def_value: Float): Float
604 do
605 sys.jni_env.push_local_frame(1)
606 var return_value =
607 native_bundle.get_double_with_def_value(key.to_java_string, def_value)
608 sys.jni_env.pop_local_frame
609 return return_value
610 end
611
612 # Retrieves the `Array[Float]` value corresponding to the given key
613 # Returns the `null` if none or if it's the wrong value type
614 fun array_of_float(key: String): nullable Array[Float]
615 do
616 sys.jni_env.push_local_frame(1)
617 var return_value = native_bundle.get_double_array(key.to_java_string)
618 sys.jni_env.pop_local_frame
619
620 if return_value.is_empty then return null
621
622 return return_value
623 end
624
625 # Retrieves the `Array[Char]` value corresponding to the given key
626 # Returns the `null` if none or if it's the wrong value type
627 # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
628 fun array_of_char(key: String): nullable Array[Char]
629 do
630 sys.jni_env.push_local_frame(1)
631 var return_value = native_bundle.get_char_array(key.to_java_string)
632 sys.jni_env.pop_local_frame
633
634 if return_value.is_empty then return null
635
636 return return_value
637 end
638 # Retrieves the `Array[Int]` value corresponding to the given key
639 # Returns the `null` if none or if it's the wrong value type
640 fun array_of_int(key: String): nullable Array[Int]
641 do
642 sys.jni_env.push_local_frame(1)
643 var return_value = native_bundle.get_long_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[Bool]` value corresponding to the given key
652 # Returns the `null` if none or if it's the wrong value type
653 fun array_of_bool(key: String): nullable Array[Bool]
654 do
655 sys.jni_env.push_local_frame(1)
656 var return_value = native_bundle.get_boolean_array(key.to_java_string)
657 sys.jni_env.pop_local_frame
658
659 if return_value.is_empty then return null
660
661 return return_value
662 end
663
664 # Retrieves the `Array[String]` value corresponding to the given key
665 # Returns the `null` if none or if it's the wrong value type
666 fun array_of_string(key: String): nullable Array[String]
667 do
668 sys.jni_env.push_local_frame(1)
669
670 var return_value = native_bundle.get_string_array(key.to_java_string)
671 sys.jni_env.pop_local_frame
672
673 if return_value.is_empty then return null
674
675 return return_value
676 end
677 end
678
679 redef class Serializable
680 # Called by `Bundle::[]=` to dynamically choose the appropriate method according
681 # to the value type to store
682 # Non-primitive Object (`String` excluded) will be stored as a serialized json `String`
683 # Refine your class to customize this method behaviour
684 protected fun add_to_bundle(bundle: NativeBundle, key: JavaString)
685 do
686 sys.jni_env.push_local_frame(1)
687 var serialized_string = new StringOStream
688 var serializer = new JsonSerializer(serialized_string)
689 serializer.serialize(self)
690
691 bundle.put_string(key, serialized_string.to_s.to_java_string)
692 end
693 end
694
695 redef class Int
696 redef fun add_to_bundle(bundle, key)
697 do
698 bundle.put_long(key, self)
699 end
700 end
701
702 redef class Char
703 redef fun add_to_bundle(bundle, key)
704 do
705 bundle.put_char(key, self)
706 end
707 end
708
709 redef class Float
710 redef fun add_to_bundle(bundle, key)
711 do
712 bundle.put_double(key, self)
713 end
714 end
715
716 redef class Bool
717 redef fun add_to_bundle(bundle, key)
718 do
719 bundle.put_boolean(key, self)
720 end
721 end
722
723 redef class String
724 redef fun add_to_bundle(bundle, key)
725 do
726 bundle.put_string(key, self.to_java_string)
727 end
728 end
729
730 redef class Array[E]
731 redef fun add_to_bundle(bundle, key)
732 do
733 if self isa Array[Bool] then
734 bundle.put_boolean_array(key, self)
735 else if self isa Array[Int] then
736 bundle.put_long_array(key, self)
737 else if self isa Array[Float] then
738 bundle.put_double_array(key, self)
739 else if self isa Array[Char] then
740 bundle.put_char_array(key, self)
741 else if self isa Array[String] 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 java_string_array.push(element.to_s.to_java_string)
747 end
748
749 bundle.put_string_array(key, java_string_array)
750 else if self isa Array[Serializable] then
751 sys.jni_env.push_local_frame(self.length)
752 var java_string_array = new Array[JavaString]
753
754 for element in self do
755 var serialized_string = new StringOStream
756 var serializer = new JsonSerializer(serialized_string)
757 serializer.serialize(element)
758 java_string_array.add(serialized_string.to_s.to_java_string)
759 end
760
761 bundle.put_string_array(key, java_string_array)
762 end
763 end
764 end
765
766 # Allows JavaString collection copy through FFI with Java
767 private class StringCopyArray
768 var collection = new Array[String]
769 fun add(element: JavaString) do collection.add element.to_s
770 end