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