577d0d29b037f9c2ad1883cc212350239fe7c91f
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 # Services to save/load data using `android.content.SharedPreferences` for the android platform
18 module shared_preferences_api10
20 import native_app_glue
22 private import json_serialization
25 import android.content.SharedPreferences;
26 import android.content.Context;
27 import android.app.Activity;
29 import java.util.Iterator;
30 import java.lang.ClassCastException;
31 import java.lang.NullPointerException;
34 extern class NativeSharedPreferences in "Java" `{ android.content.SharedPreferences `}
36 redef type SELF: NativeSharedPreferences
38 fun contains(key: JavaString): Bool in "Java" `{ return recv.contains(key); `}
39 fun get_all
: HashMap[JavaString, JavaObject] import HashMap[JavaString, JavaObject],
40 HashMap[JavaString, JavaObject].[]= in "Java" `{
41 Map<String, ?> java_map = null;
42 int nit_hashmap = new_HashMap_of_JavaString_JavaObject();
44 java_map = recv.getAll();
45 } catch (NullPointerException e) {
49 for (Map.Entry<String, ?> entry: java_map.entrySet())
50 HashMap_of_JavaString_JavaObject__index_assign(nit_hashmap,
51 entry.getKey(), entry.getValue());
55 fun get_boolean
(key
: JavaString, def_value
: Bool): Bool in "Java" `{
58 return_value = recv.getBoolean(key, def_value);
59 } catch (ClassCastException e) {
65 fun get_float
(key
: JavaString, def_value
: Float): Float in "Java" `{
68 return_value = recv.getFloat(key, (float) def_value);
69 } catch (ClassCastException e) {
75 fun get_int
(key
: JavaString, def_value
: Int): Int in "Java" `{
78 return_value = recv.getInt(key, def_value);
79 } catch (ClassCastException e) {
85 #FIXME: Get rid of the `int` cast when the ffi is fixed
86 fun get_long
(key
: JavaString, def_value
: Int): Int in "Java" `{
89 return_value = recv.getLong(key, def_value);
90 } catch (ClassCastException e) {
94 return (int) return_value;
96 fun get_string
(key
: JavaString, def_value
: JavaString): JavaString in "Java" `{
97 String return_value = null;
99 return_value = recv.getString(key, def_value);
100 } catch (ClassCastException e) {
108 extern class NativeSharedPreferencesEditor in "Java" `{ android.content.SharedPreferences$Editor `}
110 redef type SELF: NativeSharedPreferencesEditor
112 fun clear: NativeSharedPreferencesEditor in "Java" `{ return recv.clear(); `}
113 fun commit
: Bool in "Java" `{ return recv.commit(); `}
114 fun put_boolean(key: JavaString, value: Bool ): NativeSharedPreferencesEditor in "Java" `{
115 return recv
.putBoolean
(key
, value
);
117 fun put_float(key: JavaString, value: Float): NativeSharedPreferencesEditor in "Java" `{
118 return recv
.putFloat
(key
, (float
) value
);
120 fun put_int(key: JavaString, value: Int): NativeSharedPreferencesEditor in "Java" `{
121 return recv
.putInt
(key
, value
);
123 fun put_long(key: JavaString, value: Int): NativeSharedPreferencesEditor in "Java" `{
124 return recv
.putLong
(key
, value
);
126 fun put_string(key: JavaString, value: JavaString): NativeSharedPreferencesEditor in "Java" `{
127 return recv
.putString
(key
, value
);
129 fun remove(key: JavaString): NativeSharedPreferencesEditor in "Java" `{
130 return recv
.remove
(key
);
134 # Provides services to save and load data for the android platform
135 class SharedPreferences
136 protected var context: NativeActivity
137 protected var shared_preferences: NativeSharedPreferences
138 protected var editor: NativeSharedPreferencesEditor
140 # Automatically commits every saving/removing instructions (`true` by default)
141 var auto_commit = true
143 protected init(app: App, file_name: String, mode: Int)
145 self.context = app.native_activity
146 sys.jni_env.push_local_frame(1)
147 setup(file_name.to_java_string, mode)
148 sys.jni_env.pop_local_frame
151 # Restricts file access to the current application
152 init privately(app: App, file_name: String)
154 self.init(app, file_name, private_mode)
158 private fun private_mode: Int in "Java" `{ return Context.MODE_PRIVATE; `}
160 private fun set_vars
(shared_pref
: NativeSharedPreferences, editor
: NativeSharedPreferencesEditor)
162 self.shared_preferences
= shared_pref
.new_global_ref
163 self.editor
= editor
.new_global_ref
166 private fun setup
(file_name
: JavaString, mode
: Int) import context
, set_vars
in "Java" `{
167 Activity context = (Activity) SharedPreferences_context(recv);
168 SharedPreferences sp;
170 // Uses default SharedPreferences if file_name is an empty String
171 if (file_name.equals("")) {
172 sp = context.getPreferences( mode);
174 sp = context.getSharedPreferences(file_name, mode);
177 SharedPreferences.Editor editor = sp.edit();
179 SharedPreferences_set_vars(recv, sp, editor);
182 private fun commit_if_auto
do if auto_commit
then self.commit
184 # Returns true if there's an entry corresponding the given key
185 fun has
(key
: String): Bool
187 sys
.jni_env
.push_local_frame
(2)
188 var return_value
= shared_preferences
.contains
(key
.to_java_string
)
189 sys
.jni_env
.pop_local_frame
193 # Returns a `HashMap` containing all entries or `null` if there's no entries
195 # User has to manage local stack deallocation himself
199 # var foo = new HashMap[JavaString, JavaObject]
201 # for key, value in foo do
202 # key.delete_local_ref
203 # value.delete_local_ref
206 # *You should use Nit getters instead and get each value one by one*
207 fun all
: nullable HashMap[JavaString, JavaObject]
209 var hashmap
= shared_preferences
.get_all
210 if hashmap
.is_empty
then return null
214 # Returns the `Bool` value corresponding the given key or `def_value` if none
215 # or if the value isn't of correct type
216 fun bool
(key
: String, def_value
: Bool): Bool
218 sys
.jni_env
.push_local_frame
(2)
219 var return_value
= shared_preferences
.get_boolean
(key
.to_java_string
, def_value
)
220 sys
.jni_env
.pop_local_frame
224 # Returns the `Float` value corresponding the given key or `def_value` if none
225 # or if the value isn't of correct type
226 fun float
(key
: String, def_value
: Float): Float
228 sys
.jni_env
.push_local_frame
(2)
229 var return_value
= shared_preferences
.get_float
(key
.to_java_string
, def_value
)
230 sys
.jni_env
.pop_local_frame
234 # Returns the `Int` value corresponding the given key or `def_value` if none
235 # or if the value isn't of correct type
236 # Be aware of possible `def_value` integer overflow as the Nit `Int` corresponds
238 fun int
(key
: String, def_value
: Int): Int
240 sys
.jni_env
.push_local_frame
(2)
241 var return_value
= shared_preferences
.get_int
(key
.to_java_string
, def_value
)
242 sys
.jni_env
.pop_local_frame
246 # Returns the `Int` value corresponding the given key or `def_value` if none
247 # or if the value isn't of correct type
248 # Calls `getLong(key, value)` java method
249 # Nit `Int` is equivalent to Java `long` so that no integer overflow will occur
250 fun long
(key
: String, def_value
: Int): Int
252 sys
.jni_env
.push_local_frame
(2)
253 var return_value
= shared_preferences
.get_long
(key
.to_java_string
, def_value
)
254 sys
.jni_env
.pop_local_frame
258 # Returns the `String` value corresponding the given key or `def_value` if none
259 # or if the value isn't of correct type
260 fun string
(key
: String, def_value
: String): String
262 sys
.jni_env
.push_local_frame
(3)
263 var java_return_value
= shared_preferences
.get_string
(key
.to_java_string
,
264 def_value
.to_java_string
)
265 var nit_return_value
= java_return_value
.to_s
266 sys
.jni_env
.pop_local_frame
267 return nit_return_value
270 # Clears all the dictionnary entries in the specified file or the default file
271 # if none specified at instanciation
272 # Returns `self` allowing fluent programming
273 fun clear
: SharedPreferences
280 # If auto_commit is `false`, has to be called to save the data to persistant memory
283 sys
.jni_env
.push_local_frame
(1)
284 var return_value
= editor
.commit
285 sys
.jni_env
.pop_local_frame
289 # Set a key-value pair using a `Bool` value
290 # Returns `self` allowing fluent programming
291 fun add_bool
(key
: String, value
: Bool ): SharedPreferences
293 sys
.jni_env
.push_local_frame
(1)
294 editor
.put_boolean
(key
.to_java_string
, value
)
295 sys
.jni_env
.pop_local_frame
300 # Set a key-value pair using a `Float` value
301 # Returns `self` allowing fluent programming
303 # Be aware of possible loss of precision as Nit `Float` corresponds to Java `double`
304 # and the methods stores a Java `float`
305 fun add_float
(key
: String, value
: Float): SharedPreferences
307 sys
.jni_env
.push_local_frame
(1)
308 editor
.put_float
(key
.to_java_string
, value
)
309 sys
.jni_env
.pop_local_frame
314 # Set a key-value pair using a `Int` type value
315 # Returns `self` allowing fluent programming
317 # Be aware of possible integer overflow as the Nit `Int` corresponds to Java `long`
318 # and the methods stores a Java `int`
319 # *You might want to use add_long instead*
320 fun add_int
(key
: String, value
: Int): SharedPreferences
322 sys
.jni_env
.push_local_frame
(1)
323 editor
.put_int
(key
.to_java_string
, value
)
324 sys
.jni_env
.pop_local_frame
329 # Set a key-value pair using a `Int` type value
330 # Returns `self` allowing fluent programming
331 fun add_long
(key
: String, value
: Int): SharedPreferences
333 sys
.jni_env
.push_local_frame
(1)
334 editor
.put_long
(key
.to_java_string
, value
)
335 sys
.jni_env
.pop_local_frame
340 # Set a key-value pair using a `String` type value
341 # Returns `self` allowing fluent programming
342 fun add_string
(key
: String, value
: String): SharedPreferences
344 sys
.jni_env
.push_local_frame
(2)
345 editor
.put_string
(key
.to_java_string
, value
.to_java_string
)
346 sys
.jni_env
.pop_local_frame
351 # Removes the corresponding entry in the file
352 # Returns `self` allowing fluent programming
353 fun remove
(key
: String): SharedPreferences
355 sys
.jni_env
.push_local_frame
(1)
356 editor
.remove
(key
.to_java_string
)
357 sys
.jni_env
.pop_local_frame
362 # Deallocate global references allocated by the SharedPreferences instance
365 self.shared_preferences
.delete_global_ref
366 self.editor
.delete_global_ref
369 # Store `value` as a serialized Json string
370 fun []=(key
: String, value
: nullable Serializable)
372 var serialized_string
= new StringOStream
373 var serializer
= new JsonSerializer(serialized_string
)
374 serializer
.serialize
(value
)
376 add_string
(key
, serialized_string
.to_s
)
380 # Retrieve an `Object` stored via `[]=` function
382 # Returns `null` if there's no serialized object corresponding to the given key
383 # Make sure that the serialized object is `auto_serializable` or that it redefines
384 # the appropriate methods. Refer to `Serializable` documentation for further details
385 fun [](key
: String): nullable Object
387 var serialized_string
= self.string
(key
, "")
389 if serialized_string
== "" then return null
391 var deserializer
= new JsonDeserializer(serialized_string
)
392 return deserializer
.deserialize
397 fun shared_preferences
: SharedPreferences is cached
do
398 return new SharedPreferences.privately
(self, "")