Provides services to save and load data for the android platform

Introduced properties

fun [](key: String): nullable Object

android :: SharedPreferences :: []

Retrieve an Object stored via []= function
fun []=(key: String, value: nullable Serializable)

android :: SharedPreferences :: []=

Store value as a serialized Json string
fun add_bool(key: String, value: Bool): SharedPreferences

android :: SharedPreferences :: add_bool

Set a key-value pair using a Bool value
fun add_float(key: String, value: Float): SharedPreferences

android :: SharedPreferences :: add_float

Set a key-value pair using a Float value
fun add_int(key: String, value: Int): SharedPreferences

android :: SharedPreferences :: add_int

Set a key-value pair using a Int type value
fun add_long(key: String, value: Int): SharedPreferences

android :: SharedPreferences :: add_long

Set a key-value pair using a Int type value
fun add_string(key: String, value: String): SharedPreferences

android :: SharedPreferences :: add_string

Set a key-value pair using a String type value
fun add_string_set(key: String, value: HashSet[JavaString]): SharedPreferences

android :: SharedPreferences :: add_string_set

Set a key-value pair using a HashSet[JavaString] value
fun all: nullable HashMap[JavaString, JavaObject]

android :: SharedPreferences :: all

Returns a HashMap containing all entries or null if there's no entries
fun auto_commit: Bool

android :: SharedPreferences :: auto_commit

Automatically commits every saving/removing instructions (true by default)
protected fun auto_commit=(auto_commit: Bool)

android :: SharedPreferences :: auto_commit=

Automatically commits every saving/removing instructions (true by default)
fun bool(key: String, def_value: Bool): Bool

android :: SharedPreferences :: bool

Returns the Bool value corresponding the given key or def_value if none
fun clear: SharedPreferences

android :: SharedPreferences :: clear

Clears all the dictionnary entries in the specified file or the default file
fun commit: Bool

android :: SharedPreferences :: commit

If auto_commit is false, has to be called to save the data to persistant memory
protected init defaultinit(app: App, file_name: String, mode: Int)

android :: SharedPreferences :: defaultinit

fun destroy

android :: SharedPreferences :: destroy

Deallocate global references allocated by the SharedPreferences instance
fun float(key: String, def_value: Float): Float

android :: SharedPreferences :: float

Returns the Float value corresponding the given key or def_value if none
fun has(key: String): Bool

android :: SharedPreferences :: has

Returns true if there's an entry corresponding the given key
fun int(key: String, def_value: Int): Int

android :: SharedPreferences :: int

Returns the Int value corresponding the given key or def_value if none
fun long(key: String, def_value: Int): Int

android :: SharedPreferences :: long

Returns the Int value corresponding the given key or def_value if none
init multi_process(app: App, file_name: String)

android :: SharedPreferences :: multi_process

Allows multiple processes to write into the same SharedPreferences file
init privately(app: App, file_name: String)

android :: SharedPreferences :: privately

Restricts file access to the current application
fun remove(key: String): SharedPreferences

android :: SharedPreferences :: remove

Removes the corresponding entry in the file
fun string(key: String, def_value: String): String

android :: SharedPreferences :: string

Returns the String value corresponding the given key or def_value if none
fun string_set(key: String): HashSet[JavaString]

android :: SharedPreferences :: string_set

Returns the HashSet[JavaString] value corresponding the given key or null if none

Redefined properties

redef type SELF: SharedPreferences

android $ SharedPreferences :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun [](key: String): nullable Object

android :: SharedPreferences :: []

Retrieve an Object stored via []= function
fun []=(key: String, value: nullable Serializable)

android :: SharedPreferences :: []=

Store value as a serialized Json string
fun add_bool(key: String, value: Bool): SharedPreferences

android :: SharedPreferences :: add_bool

Set a key-value pair using a Bool value
fun add_float(key: String, value: Float): SharedPreferences

android :: SharedPreferences :: add_float

Set a key-value pair using a Float value
fun add_int(key: String, value: Int): SharedPreferences

android :: SharedPreferences :: add_int

Set a key-value pair using a Int type value
fun add_long(key: String, value: Int): SharedPreferences

android :: SharedPreferences :: add_long

Set a key-value pair using a Int type value
fun add_string(key: String, value: String): SharedPreferences

android :: SharedPreferences :: add_string

Set a key-value pair using a String type value
fun add_string_set(key: String, value: HashSet[JavaString]): SharedPreferences

android :: SharedPreferences :: add_string_set

Set a key-value pair using a HashSet[JavaString] value
fun all: nullable HashMap[JavaString, JavaObject]

android :: SharedPreferences :: all

Returns a HashMap containing all entries or null if there's no entries
fun auto_commit: Bool

android :: SharedPreferences :: auto_commit

Automatically commits every saving/removing instructions (true by default)
protected fun auto_commit=(auto_commit: Bool)

android :: SharedPreferences :: auto_commit=

Automatically commits every saving/removing instructions (true by default)
fun bool(key: String, def_value: Bool): Bool

android :: SharedPreferences :: bool

Returns the Bool value corresponding the given key or def_value if none
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun clear: SharedPreferences

android :: SharedPreferences :: clear

Clears all the dictionnary entries in the specified file or the default file
fun commit: Bool

android :: SharedPreferences :: commit

If auto_commit is false, has to be called to save the data to persistant memory
protected init defaultinit(app: App, file_name: String, mode: Int)

android :: SharedPreferences :: defaultinit

fun destroy

android :: SharedPreferences :: destroy

Deallocate global references allocated by the SharedPreferences instance
fun float(key: String, def_value: Float): Float

android :: SharedPreferences :: float

Returns the Float value corresponding the given key or def_value if none
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun has(key: String): Bool

android :: SharedPreferences :: has

Returns true if there's an entry corresponding the given key
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
fun int(key: String, def_value: Int): Int

android :: SharedPreferences :: int

Returns the Int value corresponding the given key or def_value if none
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun long(key: String, def_value: Int): Int

android :: SharedPreferences :: long

Returns the Int value corresponding the given key or def_value if none
init multi_process(app: App, file_name: String)

android :: SharedPreferences :: multi_process

Allows multiple processes to write into the same SharedPreferences file
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
init privately(app: App, file_name: String)

android :: SharedPreferences :: privately

Restricts file access to the current application
fun remove(key: String): SharedPreferences

android :: SharedPreferences :: remove

Removes the corresponding entry in the file
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun string(key: String, def_value: String): String

android :: SharedPreferences :: string

Returns the String value corresponding the given key or def_value if none
fun string_set(key: String): HashSet[JavaString]

android :: SharedPreferences :: string_set

Returns the HashSet[JavaString] value corresponding the given key or null if none
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram android::SharedPreferences SharedPreferences core::Object Object android::SharedPreferences->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

android $ SharedPreferences
# Provides services to save and load data for the android platform
class SharedPreferences
	protected var shared_preferences: NativeSharedPreferences
	protected var editor: NativeSharedPreferencesEditor

	# Automatically commits every saving/removing instructions (`true` by default)
	var auto_commit = true

	protected init(app: App, file_name: String, mode: Int)
	do
		sys.jni_env.push_local_frame(1)
		setup(file_name.to_java_string, mode, app.native_context)
		sys.jni_env.pop_local_frame
	end

	# Restricts file access to the current application
	init privately(app: App, file_name: String)
	do
		self.init(app, file_name, private_mode)
	end

	# File access mode
	private fun private_mode: Int in "Java" `{ return Context.MODE_PRIVATE; `}

	private fun set_vars(shared_pref: NativeSharedPreferences, editor: NativeSharedPreferencesEditor)
	do
		self.shared_preferences = shared_pref.new_global_ref
		self.editor = editor.new_global_ref
	end

	private fun setup(file_name: JavaString, mode: Int, context: NativeContext) import set_vars in "Java" `{
		SharedPreferences sp;

		// Uses default SharedPreferences if file_name is an empty String
		if (file_name.equals("")) {
			Activity activity = (Activity)context;
			sp = activity.getPreferences((int)mode);
		} else {
			sp = context.getSharedPreferences(file_name, (int)mode);
		}

		SharedPreferences.Editor editor = sp.edit();

		SharedPreferences_set_vars(self, sp, editor);
	`}

	private fun commit_if_auto do if auto_commit then self.commit

	# Returns true if there's an entry corresponding the given key
	fun has(key: String): Bool
	do
		sys.jni_env.push_local_frame(2)
		var return_value = shared_preferences.contains(key.to_java_string)
		sys.jni_env.pop_local_frame
		return return_value
	end

	# Returns a `HashMap` containing all entries or `null` if there's no entries
	#
	# User has to manage local stack deallocation himself
	#
	# ~~~nitish
	# var foo = new HashMap[JavaString, JavaObject]
	# # ...
	# for key, value in foo do
	#	  key.delete_local_ref
	#	  value.delete_local_ref
	# end
	# ~~~
	# *You should use Nit getters instead and get each value one by one*
	fun all: nullable HashMap[JavaString, JavaObject]
	do
		var hashmap = shared_preferences.get_all
		if hashmap.is_empty then return null
		return hashmap
	end

	# Returns the `Bool` value corresponding the given key or `def_value` if none
	# or if the value isn't of correct type
	fun bool(key: String, def_value: Bool): Bool
	do
		sys.jni_env.push_local_frame(2)
		var return_value = shared_preferences.get_boolean(key.to_java_string, def_value)
		sys.jni_env.pop_local_frame
		return return_value
	end

	# Returns the `Float` value corresponding the given key or `def_value` if none
	# or if the value isn't of correct type
	fun float(key: String, def_value: Float): Float
	do
		sys.jni_env.push_local_frame(2)
		var return_value = shared_preferences.get_float(key.to_java_string, def_value)
		sys.jni_env.pop_local_frame
		return return_value
	end

	# Returns the `Int` value corresponding the given key or `def_value` if none
	# or if the value isn't of correct type
	# Be aware of possible `def_value` integer overflow as the Nit `Int` corresponds
	# to Java `long`
	fun int(key: String, def_value: Int): Int
	do
		sys.jni_env.push_local_frame(2)
		var return_value = shared_preferences.get_int(key.to_java_string, def_value)
		sys.jni_env.pop_local_frame
		return return_value
	end

	# Returns the `Int` value corresponding the given key or `def_value` if none
	# or if the value isn't of correct type
	# Calls `getLong(key, value)` java method
	# Nit `Int` is equivalent to Java `long` so that no integer overflow will occur
	fun long(key: String, def_value: Int): Int
	do
		sys.jni_env.push_local_frame(2)
		var return_value = shared_preferences.get_long(key.to_java_string, def_value)
		sys.jni_env.pop_local_frame
		return return_value
	end

	# Returns the `String` value corresponding the given key or `def_value` if none
	# or if the value isn't of correct type
	fun string(key: String, def_value: String): String
	do
		sys.jni_env.push_local_frame(3)
		var java_return_value = shared_preferences.get_string(key.to_java_string,
			def_value.to_java_string)
		var nit_return_value = java_return_value.to_s
		sys.jni_env.pop_local_frame
		return nit_return_value
	end

	# Clears all the dictionnary entries in the specified file or the default file
	# if none specified at instanciation
	# Returns `self` allowing fluent programming
	fun clear: SharedPreferences
	do
		editor.clear
		commit_if_auto
		return self
	end

	# If auto_commit is `false`, has to be called to save the data to persistant memory
	fun commit: Bool
	do
		sys.jni_env.push_local_frame(1)
		var return_value = editor.commit
		sys.jni_env.pop_local_frame
		return return_value
	end

	# Set a key-value pair using a `Bool` value
	# Returns `self` allowing fluent programming
	fun add_bool(key: String, value: Bool): SharedPreferences
	do
		sys.jni_env.push_local_frame(1)
		editor.put_boolean(key.to_java_string, value)
		sys.jni_env.pop_local_frame
		commit_if_auto
		return self
	end

	# Set a key-value pair using a `Float` value
	# Returns `self` allowing fluent programming
	#
	# Be aware of possible loss of precision as Nit `Float` corresponds to Java `double`
	# and the methods stores a Java `float`
	fun add_float(key: String, value: Float): SharedPreferences
	do
		sys.jni_env.push_local_frame(1)
		editor.put_float(key.to_java_string, value)
		sys.jni_env.pop_local_frame
		commit_if_auto
		return self
	end

	# Set a key-value pair using a `Int` type value
	# Returns `self` allowing fluent programming
	#
	# Be aware of possible integer overflow as the Nit `Int` corresponds to Java `long`
	# and the methods stores a Java `int`
	# *You might want to use add_long instead*
	fun add_int(key: String, value: Int): SharedPreferences
	do
		sys.jni_env.push_local_frame(1)
		editor.put_int(key.to_java_string, value)
		sys.jni_env.pop_local_frame
		commit_if_auto
		return self
	end

	# Set a key-value pair using a `Int` type value
	# Returns `self` allowing fluent programming
	fun add_long(key: String, value: Int): SharedPreferences
	do
		sys.jni_env.push_local_frame(1)
		editor.put_long(key.to_java_string, value)
		sys.jni_env.pop_local_frame
		commit_if_auto
		return self
	end

	# Set a key-value pair using a `String` type value
	# Returns `self` allowing fluent programming
	fun add_string(key: String, value: String): SharedPreferences
	do
		sys.jni_env.push_local_frame(2)
		editor.put_string(key.to_java_string, value.to_java_string)
		sys.jni_env.pop_local_frame
		commit_if_auto
		return self
	end

	# Removes the corresponding entry in the file
	# Returns `self` allowing fluent programming
	fun remove(key: String): SharedPreferences
	do
		sys.jni_env.push_local_frame(1)
		editor.remove(key.to_java_string)
		sys.jni_env.pop_local_frame
		commit_if_auto
		return self
	end

	# Deallocate global references allocated by the SharedPreferences instance
	fun destroy
	do
		self.shared_preferences.delete_global_ref
		self.editor.delete_global_ref
	end

	# Store `value` as a serialized Json string
	fun []=(key: String, value: nullable Serializable)
	do
		var serialized_string = new StringWriter
		var serializer = new JsonSerializer(serialized_string)
		serializer.serialize(value)

		add_string(key, serialized_string.to_s)
		commit_if_auto
	end

	# Retrieve an `Object` stored via `[]=` function
	#
	# Returns `null` if there's no serialized object corresponding to the given key
	# Make sure that the serialized object is `serialize` or that it redefines
	# the appropriate methods. Refer to `Serializable` documentation for further details
	fun [](key: String): nullable Object
	do
		var serialized_string = self.string(key, "")

		if serialized_string == "" then return null

		var deserializer = new JsonDeserializer(serialized_string)
		var deserialized = deserializer.deserialize

		var errors = deserializer.errors
		if errors.not_empty then
			# An update may have broken the versioning compatibility
			print_error "{class_name} error at deserialization: {errors.join(", ")}"
			return null # Let's be safe
		end

		return deserialized
	end
end
lib/android/shared_preferences/shared_preferences_api10.nit:145,1--411,3

android :: shared_preferences_api11 $ SharedPreferences
redef class SharedPreferences

	# Allows multiple processes to write into the same `SharedPreferences` file
	init multi_process(app: App, file_name: String)
	do
		self.init(app, file_name, multi_process_mode)
	end

	# File access mode
	private fun multi_process_mode: Int in "Java" `{ return Context.MODE_MULTI_PROCESS; `}

	# Returns the `HashSet[JavaString]` value corresponding the given key or `null` if none
	#
	# User has to manage local stack deallocation himself
	#
	# ~~~nitish
	# var a_hash_set = app.shared_preferences.string_set("A key")
	# # ...
	# for element in a_hash_set do element.delete_local_ref
	# ~~~
	fun string_set(key: String): HashSet[JavaString]
	do
		sys.jni_env.push_local_frame(3)
		var return_value = shared_preferences.get_string_set(key.to_java_string)
		sys.jni_env.pop_local_frame
		return return_value
	end

	# Set a key-value pair using a `HashSet[JavaString]` value
	# Returns self allowing fluent programming
	#
	# User has to manage local stack deallocation himself
	#
	# ~~~nitish
	# var foo = new HashSet[JavaString]
	# app.shared_preferences.add_string_set("A key", foo)
	# for element in foo do element.delete_local_ref
	# ~~~
	fun add_string_set(key: String, value: HashSet[JavaString]): SharedPreferences
	do
		editor.put_string_set(key.to_java_string, value)
		return self
	end
end
lib/android/shared_preferences/shared_preferences_api11.nit:61,1--104,3