1 package de.skycoder42.qtmvvm.core;
5 import android.content.Context;
6 import android.content.SharedPreferences;
8 import android.preference.PreferenceManager;
10 class AndroidSettingsAccessor {
11 private long address = 0;
12 private SharedPreferences preferences = null;
13 private SharedPreferences.Editor editor = null;
14 private Listener listener =
new Listener();
16 private class Listener
implements SharedPreferences.OnSharedPreferenceChangeListener {
18 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
19 if(address != 0 && preferences.equals(sharedPreferences))
20 callback(address, key);
24 private static native
void callback(
long address, Object key);
26 public AndroidSettingsAccessor(Context context,
long address) {
27 this.address = address;
28 preferences = PreferenceManager.getDefaultSharedPreferences(context);
29 preferences.registerOnSharedPreferenceChangeListener(listener);
32 public AndroidSettingsAccessor(Context context, String name,
int mode,
long address) {
33 this.address = address;
34 preferences = context.getSharedPreferences(name, mode);
35 preferences.registerOnSharedPreferenceChangeListener(listener);
40 preferences.unregisterOnSharedPreferenceChangeListener(listener);
45 public boolean contains(String key) {
46 return preferences.contains(key);
49 public Object load(String key) {
50 return preferences.getAll().get(key);
53 public boolean save(String key,
boolean value) {
54 boolean res = beginEdit();
55 editor.putBoolean(key, value);
59 public boolean save(String key,
int value) {
60 boolean res = beginEdit();
61 editor.putInt(key, value);
65 public boolean save(String key,
long value) {
66 boolean res = beginEdit();
67 editor.putLong(key, value);
71 public boolean save(String key,
float value) {
72 boolean res = beginEdit();
73 editor.putFloat(key, value);
77 public boolean save(String key, String value) {
78 boolean res = beginEdit();
79 editor.putString(key, value);
83 public boolean save(String key, Set<String> value) {
84 boolean res = beginEdit();
85 editor.putStringSet(key, value);
89 public boolean remove(String key) {
90 boolean res = beginEdit();
102 private boolean beginEdit() {
106 editor = preferences.edit();