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