1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Refines shared_preferences module to add API 11 services
18 module shared_preferences_api11
20 import shared_preferences
23 import java.util.HashSet;
27 redef extern class NativeSharedPreferences
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();
36 for (String element: java_set)
37 HashSet_of_JavaString_add(nit_hashset, element);
43 redef extern class NativeSharedPreferencesEditor
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);
51 while (Iterator_of_JavaString_is_ok(itr)) {
52 java_set.add(Iterator_of_JavaString_item(itr));
53 Iterator_of_JavaString_next(itr);
56 return recv.putStringSet(key, java_set);
60 redef class SharedPreferences
62 # Allows multiple processes to write into the same `SharedPreferences` file
63 init multi_process
(app
: App, file_name
: String)
65 self.init(app
, file_name
, multi_process_mode
)
69 private fun multi_process_mode
: Int in "Java" `{ return Content.MODE_MULTI_PROCESS; `}
71 # Returns the `HashSet[JavaString]` value corresponding the given key or `null` if none
73 # User has to manage local stack deallocation himself
77 # var a_hash_set = shared_preferences.string_set("A key")
79 # for element in a_hash_set do element.delete_local_ref
81 fun string_set(key: String): HashSet[JavaString]
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
89 # Set a key-value pair using a `HashSet[JavaString]` value
90 # Returns self allowing fluent programming
92 # User has to manage local stack deallocation himself
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
100 fun add_string_set(key: String, value: HashSet[JavaString]): SharedPreferences
102 editor.put_string_set(key.to_java_string, value)