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
is android_api_min
11
20 import shared_preferences
23 import java.util.HashSet;
25 import android.content.Context;
28 redef extern class NativeSharedPreferences
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();
37 for (String element: java_set)
38 HashSet_of_JavaString_add(nit_hashset, element);
44 redef extern class NativeSharedPreferencesEditor
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);
52 while (Iterator_of_JavaString_is_ok(itr)) {
53 java_set.add(Iterator_of_JavaString_item(itr));
54 Iterator_of_JavaString_next(itr);
57 return self.putStringSet(key, java_set);
61 redef class SharedPreferences
63 # Allows multiple processes to write into the same `SharedPreferences` file
64 init multi_process
(app
: App, file_name
: String)
66 self.init(app
, file_name
, multi_process_mode
)
70 private fun multi_process_mode
: Int in "Java" `{ return Context.MODE_MULTI_PROCESS; `}
72 # Returns the `HashSet[JavaString]` value corresponding the given key or `null` if none
74 # User has to manage local stack deallocation himself
77 # var a_hash_set = app.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
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
99 fun add_string_set(key: String, value: HashSet[JavaString]): SharedPreferences
101 editor.put_string_set(key.to_java_string, value)