lib/android: Intro API wrapping android SharedPreferences
[nit.git] / lib / android / shared_preferences / shared_preferences_api11.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 # Refines shared_preferences module to add API 11 services
18 module shared_preferences_api11
19
20 import shared_preferences
21
22 in "Java" `{
23 import java.util.HashSet;
24 import java.util.Set;
25 `}
26
27 redef extern class NativeSharedPreferences
28
29 # Default value to null instead of Set<String>
30 fun get_string_set(key: JavaString): HashSet[JavaString] import HashSet[JavaString],
31 HashSet[JavaString].add in "Java" `{
32 Set<String> def_value = new HashSet<String>();
33 Set<String> java_set = recv.getStringSet(key, def_value);
34 int nit_hashset = new_HashSet_of_JavaString();
35
36 for (String element: java_set)
37 HashSet_of_JavaString_add(nit_hashset, element);
38
39 return nit_hashset;
40 `}
41 end
42
43 redef extern class NativeSharedPreferencesEditor
44
45 fun put_string_set(key: JavaString, value: HashSet[JavaString]): NativeSharedPreferencesEditor
46 import HashSet[JavaString], HashSet[JavaString].iterator, Iterator[JavaString].is_ok,
47 Iterator[JavaString].item, Iterator[JavaString].next in "Java" `{
48 Set<String> java_set = new HashSet<String>();
49 int itr = HashSet_of_JavaString_iterator(value);
50
51 while (Iterator_of_JavaString_is_ok(itr)) {
52 java_set.add(Iterator_of_JavaString_item(itr));
53 Iterator_of_JavaString_next(itr);
54 }
55
56 return recv.putStringSet(key, java_set);
57 `}
58 end
59
60 redef class SharedPreferences
61
62 # Allows multiple processes to write into the same `SharedPreferences` file
63 init multi_process(app: App, file_name: String)
64 do
65 self.init(app, file_name, multi_process_mode)
66 end
67
68 # File access mode
69 private fun multi_process_mode: Int in "Java" `{ return Content.MODE_MULTI_PROCESS; `}
70
71 # Returns the `HashSet[JavaString]` value corresponding the given key or `null` if none
72 #
73 # User has to manage local stack deallocation himself
74 #
75 # Example :
76 # ~~~
77 # var a_hash_set = shared_preferences.string_set("A key")
78 # ...
79 # for element in a_hash_set do element.delete_local_ref
80 # ~~~
81 fun string_set(key: String): HashSet[JavaString]
82 do
83 sys.jni_env.push_local_frame(3)
84 var return_value = shared_preferences.get_string_set(key.to_java_string)
85 sys.jni_env.pop_local_frame
86 return return_value
87 end
88
89 # Set a key-value pair using a `HashSet[JavaString]` value
90 # Returns self allowing fluent programming
91 #
92 # User has to manage local stack deallocation himself
93 #
94 # Example :
95 # ~~~
96 # var foo = new HashSet[JavaString]
97 # shared_preferences.add_string_set("A key", foo)
98 # for element in foo do element.delete_local_ref
99 # ~~~
100 fun add_string_set(key: String, value: HashSet[JavaString]): SharedPreferences
101 do
102 editor.put_string_set(key.to_java_string, value)
103 return self
104 end
105 end