-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMvPreferences.java
More file actions
186 lines (164 loc) · 5.43 KB
/
MvPreferences.java
File metadata and controls
186 lines (164 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* AndroidWithoutStupid Java Library
* Created by V. Subhash
* http://www.VSubhash.com
* Released as Public Domain Software in 2014
*/
package com.vsubhash.droid.androidwithoutstupid;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
/**
* This class provides methods to retrieve and store app
* preferences. To use the class, first call the constructor
* with the activity instance as the parameter.
*
* @author V. Subhash (<a
* href="http://www.vsubhash.com/">www.VSubhash.com</a>)
* @version 2015.02.13
*/
public class MvPreferences {
Activity mCallingActivity;
Context mContext;
SharedPreferences mPreferences;
Editor mEditor;
public Preference mLoadedPreference = null;
/**
* Creates an instance of this class.
*
* @param aCallingActivity
* activity where the preferences will be changed
*/
public MvPreferences(Activity aCallingActivity) {
mCallingActivity = aCallingActivity;
mContext = mCallingActivity.getApplicationContext();
reload();
}
/**
* Creates an instance of this class. Use this constructor in services.
*
* @param aoApplicationContext
* context with which this instance needs to be initialized
*/
public MvPreferences(Context aoApplicationContext) {
mContext = aoApplicationContext;
reload();
}
/**
* Create an instance of this class with specified preferences.
*
* @param aCallingActivity
* activity where the preferences will be changed
* @param iXmlPreferences
* ID of the settings XML that needs to be loaded
*/
public MvPreferences(Activity aCallingActivity, int iXmlPreferences) {
this(aCallingActivity.getApplicationContext(), iXmlPreferences);
this.mCallingActivity = aCallingActivity;
this.mContext = this.mCallingActivity.getApplicationContext();
PreferenceManager.setDefaultValues(
this.mContext,
iXmlPreferences,
true);
reload();
}
/**
* Creates an instance of this class with specified preferences.
*
* @param aoContext
* context with which this instance needs to be initialized
* @param iXmlPreferences
* ID of the settings XML that needs to be loaded
*/
public MvPreferences(Context aoContext, int iXmlPreferences) {
this.mContext = aoContext;
PreferenceManager.setDefaultValues(
this.mContext,
iXmlPreferences,
true);
reload();
}
void reload() {
mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
}
/**
* Finds preference for specified key.
* @param asKey key for which the preference should be found
* @return preference if successful; or null otherwise
*/
public Preference findPreference(CharSequence asKey) {
if (mCallingActivity != null) {
this.mLoadedPreference = ((PreferenceActivity) mCallingActivity).findPreference(asKey);
}
return(this.mLoadedPreference);
}
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener aoListener) {
this.mPreferences.registerOnSharedPreferenceChangeListener(aoListener);
}
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener aoListener) {
this.mPreferences.unregisterOnSharedPreferenceChangeListener(aoListener);
}
public boolean putInt(String asKey, int aInt) {
this.mEditor = mPreferences.edit();
this.mEditor.putInt(asKey, aInt);
return(this.mEditor.commit());
}
public int getIntent(String asKey, int aiDefaultValue) {
int iRet;
this.reload();
iRet = this.mPreferences.getInt(asKey, aiDefaultValue);
return(iRet);
}
/**
* Sets specified boolean value with specified key.
*
* @param asKey key to find the preference
* @param bNewValue value of the preference
* @return whether it succeeded
* @see #getBoolean(String, boolean)
*/
public boolean putBoolean(String asKey, boolean bNewValue) {
this.mEditor = mPreferences.edit();
this.mEditor.putBoolean(asKey, bNewValue);
return(this.mEditor.commit());
}
/**
* Returns boolean value of specified key;
* @param asKey key to find the preference
* @param bDefaultValue default if the method is unable to retrieve the original value
* @return boolean value of specified key
*/
public boolean getBoolean(String asKey, boolean bDefaultValue) {
boolean bRet;
this.reload();
bRet = this.mPreferences.getBoolean(asKey, bDefaultValue);
return(bRet);
}
public String getString(String asKey, String asDefaultValue) {
String sRet;
this.reload();
sRet = this.mPreferences.getString(asKey, asDefaultValue);
return(sRet);
}
public boolean putString(String asKey, String asNewValue) {
this.mEditor = mPreferences.edit();
this.mEditor.putString(asKey, asNewValue);
return(this.mEditor.commit());
}
public long getLong(String asKey, long alDefaultValue) {
long lRet;
this.reload();
lRet = this.mPreferences.getLong(asKey, alDefaultValue);
return(lRet);
}
public boolean putLong(String asKey, long alNewValue) {
this.mEditor = mPreferences.edit();
this.mEditor.putLong(asKey, alNewValue);
return(this.mEditor.commit());
}
}