85623d8b4da4915410b56ab9eff917c0588e04a1
[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 is android_api_min 11
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 = self.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 self.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 # ~~~nitish
77 # var a_hash_set = app.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 # ~~~nitish
95 # var foo = new HashSet[JavaString]
96 # app.shared_preferences.add_string_set("A key", foo)
97 # for element in foo do element.delete_local_ref
98 # ~~~
99 fun add_string_set(key: String, value: HashSet[JavaString]): SharedPreferences
100 do
101 editor.put_string_set(key.to_java_string, value)
102 return self
103 end
104 end