QtMvvm  1.1.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
AndroidSettingsAccessor.java
1 package de.skycoder42.qtmvvm.core;
2 
3 import java.util.Set;
4 
5 import android.content.Context;
6 import android.content.SharedPreferences;
7 
8 import android.preference.PreferenceManager;
9 
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();
15 
16  private class Listener implements SharedPreferences.OnSharedPreferenceChangeListener {
17  @Override
18  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
19  if(address != 0 && preferences.equals(sharedPreferences))
20  callback(address, key);
21  }
22  }
23 
24  private static native void callback(long address, Object key);
25 
26  public AndroidSettingsAccessor(Context context, long address) {
27  this.address = address;
28  preferences = PreferenceManager.getDefaultSharedPreferences(context);
29  preferences.registerOnSharedPreferenceChangeListener(listener);
30  }
31 
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);
36  }
37 
38  public void unref() {
39  address = 0;
40  preferences.unregisterOnSharedPreferenceChangeListener(listener);
41  listener = null;
42  sync();
43  }
44 
45  public boolean contains(String key) {
46  return preferences.contains(key);
47  }
48 
49  public Object load(String key) {
50  return preferences.getAll().get(key);
51  }
52 
53  public boolean save(String key, boolean value) {
54  boolean res = beginEdit();
55  editor.putBoolean(key, value);
56  return res;
57  }
58 
59  public boolean save(String key, int value) {
60  boolean res = beginEdit();
61  editor.putInt(key, value);
62  return res;
63  }
64 
65  public boolean save(String key, long value) {
66  boolean res = beginEdit();
67  editor.putLong(key, value);
68  return res;
69  }
70 
71  public boolean save(String key, float value) {
72  boolean res = beginEdit();
73  editor.putFloat(key, value);
74  return res;
75  }
76 
77  public boolean save(String key, String value) {
78  boolean res = beginEdit();
79  editor.putString(key, value);
80  return res;
81  }
82 
83  public boolean save(String key, Set<String> value) {
84  boolean res = beginEdit();
85  editor.putStringSet(key, value);
86  return res;
87  }
88 
89  public boolean remove(String key) {
90  boolean res = beginEdit();
91  editor.remove(key);
92  return res;
93  }
94 
95  public void sync() {
96  if(editor != null) {
97  editor.apply();
98  editor = null;
99  }
100  }
101 
102  private boolean beginEdit() {
103  if(editor != null)
104  return false;
105  else {
106  editor = preferences.edit();
107  return true;
108  }
109  }
110 }