blob: 462f473727a7a435c8b206942566b86474efae5e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content;
18
19import java.util.Map;
Adam Powell212db7d2010-04-08 16:24:46 -070020import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
22/**
23 * Interface for accessing and modifying preference data returned by {@link
24 * Context#getSharedPreferences}. For any particular set of preferences,
25 * there is a single instance of this class that all clients share.
26 * Modifications to the preferences must go through an {@link Editor} object
27 * to ensure the preference values remain in a consistent state and control
Christopher Tate01ed79c2012-10-18 19:01:01 -070028 * when they are committed to storage. Objects that are returned from the
29 * various <code>get</code> methods must be treated as immutable by the application.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030 *
31 * <p><em>Note: currently this class does not support use across multiple
32 * processes. This will be added later.</em>
33 *
Joe Fernandez61fd1e82011-10-26 13:39:11 -070034 * <div class="special reference">
35 * <h3>Developer Guides</h3>
36 * <p>For more information about using SharedPreferences, read the
37 * <a href="{@docRoot}guide/topics/data/data-storage.html#pref">Data Storage</a>
38 * developer guide.</p></div>
39 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 * @see Context#getSharedPreferences
41 */
42public interface SharedPreferences {
43 /**
44 * Interface definition for a callback to be invoked when a shared
45 * preference is changed.
46 */
47 public interface OnSharedPreferenceChangeListener {
48 /**
49 * Called when a shared preference is changed, added, or removed. This
50 * may be called even if a preference is set to its existing value.
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -070051 *
52 * <p>This callback will be run on your main thread.
53 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 * @param sharedPreferences The {@link SharedPreferences} that received
55 * the change.
56 * @param key The key of the preference that was changed, added, or
57 * removed.
58 */
59 void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key);
60 }
61
62 /**
63 * Interface used for modifying values in a {@link SharedPreferences}
64 * object. All changes you make in an editor are batched, and not copied
Brad Fitzpatrick66fce502010-08-30 18:10:49 -070065 * back to the original {@link SharedPreferences} until you call {@link #commit}
66 * or {@link #apply}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 */
68 public interface Editor {
69 /**
70 * Set a String value in the preferences editor, to be written back once
Brad Fitzpatrick66fce502010-08-30 18:10:49 -070071 * {@link #commit} or {@link #apply} are called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 *
73 * @param key The name of the preference to modify.
Romain Vimont06700782014-07-08 16:46:10 +020074 * @param value The new value for the preference.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 *
76 * @return Returns a reference to the same Editor object, so you can
77 * chain put calls together.
78 */
79 Editor putString(String key, String value);
80
81 /**
Adam Powell212db7d2010-04-08 16:24:46 -070082 * Set a set of String values in the preferences editor, to be written
Brian Williammeec8b055a2014-05-12 17:52:18 -070083 * back once {@link #commit} or {@link #apply} is called.
Adam Powell212db7d2010-04-08 16:24:46 -070084 *
85 * @param key The name of the preference to modify.
Christopher Tate9a413f82013-03-08 16:41:24 -080086 * @param values The set of new values for the preference. Passing {@code null}
87 * for this argument is equivalent to calling {@link #remove(String)} with
88 * this key.
Adam Powell212db7d2010-04-08 16:24:46 -070089 * @return Returns a reference to the same Editor object, so you can
90 * chain put calls together.
91 */
92 Editor putStringSet(String key, Set<String> values);
93
94 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 * Set an int value in the preferences editor, to be written back once
Brad Fitzpatrick66fce502010-08-30 18:10:49 -070096 * {@link #commit} or {@link #apply} are called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 *
98 * @param key The name of the preference to modify.
99 * @param value The new value for the preference.
100 *
101 * @return Returns a reference to the same Editor object, so you can
102 * chain put calls together.
103 */
104 Editor putInt(String key, int value);
105
106 /**
107 * Set a long value in the preferences editor, to be written back once
Brad Fitzpatrick66fce502010-08-30 18:10:49 -0700108 * {@link #commit} or {@link #apply} are called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 *
110 * @param key The name of the preference to modify.
111 * @param value The new value for the preference.
112 *
113 * @return Returns a reference to the same Editor object, so you can
114 * chain put calls together.
115 */
116 Editor putLong(String key, long value);
117
118 /**
119 * Set a float value in the preferences editor, to be written back once
Brad Fitzpatrick66fce502010-08-30 18:10:49 -0700120 * {@link #commit} or {@link #apply} are called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 *
122 * @param key The name of the preference to modify.
123 * @param value The new value for the preference.
124 *
125 * @return Returns a reference to the same Editor object, so you can
126 * chain put calls together.
127 */
128 Editor putFloat(String key, float value);
129
130 /**
131 * Set a boolean value in the preferences editor, to be written back
Brad Fitzpatrick66fce502010-08-30 18:10:49 -0700132 * once {@link #commit} or {@link #apply} are called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 *
134 * @param key The name of the preference to modify.
135 * @param value The new value for the preference.
136 *
137 * @return Returns a reference to the same Editor object, so you can
138 * chain put calls together.
139 */
140 Editor putBoolean(String key, boolean value);
141
142 /**
143 * Mark in the editor that a preference value should be removed, which
144 * will be done in the actual preferences once {@link #commit} is
145 * called.
146 *
147 * <p>Note that when committing back to the preferences, all removals
148 * are done first, regardless of whether you called remove before
149 * or after put methods on this editor.
150 *
151 * @param key The name of the preference to remove.
152 *
153 * @return Returns a reference to the same Editor object, so you can
154 * chain put calls together.
155 */
156 Editor remove(String key);
157
158 /**
159 * Mark in the editor to remove <em>all</em> values from the
160 * preferences. Once commit is called, the only remaining preferences
161 * will be any that you have defined in this editor.
162 *
163 * <p>Note that when committing back to the preferences, the clear
164 * is done first, regardless of whether you called clear before
165 * or after put methods on this editor.
166 *
167 * @return Returns a reference to the same Editor object, so you can
168 * chain put calls together.
169 */
170 Editor clear();
171
172 /**
173 * Commit your preferences changes back from this Editor to the
174 * {@link SharedPreferences} object it is editing. This atomically
175 * performs the requested modifications, replacing whatever is currently
176 * in the SharedPreferences.
Brad Fitzpatrickedf32d02010-08-25 13:13:36 -0700177 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 * <p>Note that when two editors are modifying preferences at the same
179 * time, the last one to call commit wins.
Brad Fitzpatrickedf32d02010-08-25 13:13:36 -0700180 *
181 * <p>If you don't care about the return value and you're
182 * using this from your application's main thread, consider
Brad Fitzpatrick66fce502010-08-30 18:10:49 -0700183 * using {@link #apply} instead.
Brad Fitzpatrickedf32d02010-08-25 13:13:36 -0700184 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * @return Returns true if the new values were successfully written
186 * to persistent storage.
187 */
188 boolean commit();
Brad Fitzpatrickedf32d02010-08-25 13:13:36 -0700189
190 /**
191 * Commit your preferences changes back from this Editor to the
192 * {@link SharedPreferences} object it is editing. This atomically
193 * performs the requested modifications, replacing whatever is currently
194 * in the SharedPreferences.
195 *
196 * <p>Note that when two editors are modifying preferences at the same
Brad Fitzpatrick66fce502010-08-30 18:10:49 -0700197 * time, the last one to call apply wins.
Brad Fitzpatrickedf32d02010-08-25 13:13:36 -0700198 *
199 * <p>Unlike {@link #commit}, which writes its preferences out
Brad Fitzpatrick66fce502010-08-30 18:10:49 -0700200 * to persistent storage synchronously, {@link #apply}
Brad Fitzpatrickedf32d02010-08-25 13:13:36 -0700201 * commits its changes to the in-memory
202 * {@link SharedPreferences} immediately but starts an
203 * asynchronous commit to disk and you won't be notified of
204 * any failures. If another editor on this
205 * {@link SharedPreferences} does a regular {@link #commit}
Brad Fitzpatrick66fce502010-08-30 18:10:49 -0700206 * while a {@link #apply} is still outstanding, the
Brad Fitzpatrickedf32d02010-08-25 13:13:36 -0700207 * {@link #commit} will block until all async commits are
208 * completed as well as the commit itself.
209 *
Brad Fitzpatrickdd644c12010-10-10 10:58:47 -0700210 * <p>As {@link SharedPreferences} instances are singletons within
211 * a process, it's safe to replace any instance of {@link #commit} with
212 * {@link #apply} if you were already ignoring the return value.
213 *
214 * <p>You don't need to worry about Android component
215 * lifecycles and their interaction with <code>apply()</code>
216 * writing to disk. The framework makes sure in-flight disk
217 * writes from <code>apply()</code> complete before switching
218 * states.
219 *
220 * <p class='note'>The SharedPreferences.Editor interface
221 * isn't expected to be implemented directly. However, if you
222 * previously did implement it and are now getting errors
223 * about missing <code>apply()</code>, you can simply call
224 * {@link #commit} from <code>apply()</code>.
Brad Fitzpatrickedf32d02010-08-25 13:13:36 -0700225 */
Brad Fitzpatrick66fce502010-08-30 18:10:49 -0700226 void apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 }
228
229 /**
230 * Retrieve all values from the preferences.
231 *
Christopher Tate01ed79c2012-10-18 19:01:01 -0700232 * <p>Note that you <em>must not</em> modify the collection returned
233 * by this method, or alter any of its contents. The consistency of your
234 * stored data is not guaranteed if you do.
235 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * @return Returns a map containing a list of pairs key/value representing
237 * the preferences.
238 *
239 * @throws NullPointerException
240 */
241 Map<String, ?> getAll();
242
243 /**
244 * Retrieve a String value from the preferences.
245 *
246 * @param key The name of the preference to retrieve.
247 * @param defValue Value to return if this preference does not exist.
248 *
249 * @return Returns the preference value if it exists, or defValue. Throws
250 * ClassCastException if there is a preference with this name that is not
251 * a String.
252 *
253 * @throws ClassCastException
254 */
255 String getString(String key, String defValue);
256
257 /**
Adam Powell212db7d2010-04-08 16:24:46 -0700258 * Retrieve a set of String values from the preferences.
259 *
Christopher Tate01ed79c2012-10-18 19:01:01 -0700260 * <p>Note that you <em>must not</em> modify the set instance returned
261 * by this call. The consistency of the stored data is not guaranteed
262 * if you do, nor is your ability to modify the instance at all.
263 *
Adam Powell212db7d2010-04-08 16:24:46 -0700264 * @param key The name of the preference to retrieve.
265 * @param defValues Values to return if this preference does not exist.
266 *
267 * @return Returns the preference values if they exist, or defValues.
268 * Throws ClassCastException if there is a preference with this name
269 * that is not a Set.
270 *
271 * @throws ClassCastException
272 */
273 Set<String> getStringSet(String key, Set<String> defValues);
274
275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 * Retrieve an int value from the preferences.
277 *
278 * @param key The name of the preference to retrieve.
279 * @param defValue Value to return if this preference does not exist.
280 *
281 * @return Returns the preference value if it exists, or defValue. Throws
282 * ClassCastException if there is a preference with this name that is not
283 * an int.
284 *
285 * @throws ClassCastException
286 */
287 int getInt(String key, int defValue);
288
289 /**
290 * Retrieve a long value from the preferences.
291 *
292 * @param key The name of the preference to retrieve.
293 * @param defValue Value to return if this preference does not exist.
294 *
295 * @return Returns the preference value if it exists, or defValue. Throws
296 * ClassCastException if there is a preference with this name that is not
297 * a long.
298 *
299 * @throws ClassCastException
300 */
301 long getLong(String key, long defValue);
302
303 /**
304 * Retrieve a float value from the preferences.
305 *
306 * @param key The name of the preference to retrieve.
307 * @param defValue Value to return if this preference does not exist.
308 *
309 * @return Returns the preference value if it exists, or defValue. Throws
310 * ClassCastException if there is a preference with this name that is not
311 * a float.
312 *
313 * @throws ClassCastException
314 */
315 float getFloat(String key, float defValue);
316
317 /**
318 * Retrieve a boolean value from the preferences.
319 *
320 * @param key The name of the preference to retrieve.
321 * @param defValue Value to return if this preference does not exist.
322 *
323 * @return Returns the preference value if it exists, or defValue. Throws
324 * ClassCastException if there is a preference with this name that is not
325 * a boolean.
326 *
327 * @throws ClassCastException
328 */
329 boolean getBoolean(String key, boolean defValue);
330
331 /**
332 * Checks whether the preferences contains a preference.
333 *
334 * @param key The name of the preference to check.
335 * @return Returns true if the preference exists in the preferences,
336 * otherwise false.
337 */
338 boolean contains(String key);
339
340 /**
341 * Create a new Editor for these preferences, through which you can make
342 * modifications to the data in the preferences and atomically commit those
343 * changes back to the SharedPreferences object.
344 *
345 * <p>Note that you <em>must</em> call {@link Editor#commit} to have any
346 * changes you perform in the Editor actually show up in the
347 * SharedPreferences.
348 *
349 * @return Returns a new instance of the {@link Editor} interface, allowing
350 * you to modify the values in this SharedPreferences object.
351 */
352 Editor edit();
353
354 /**
355 * Registers a callback to be invoked when a change happens to a preference.
Andrew Solovayde0c99e2014-05-12 19:50:37 -0700356 *
357 * <p class="caution"><strong>Caution:</strong> The preference manager does
358 * not currently store a strong reference to the listener. You must store a
359 * strong reference to the listener, or it will be susceptible to garbage
360 * collection. We recommend you keep a reference to the listener in the
361 * instance data of an object that will exist as long as you need the
362 * listener.</p>
363 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 * @param listener The callback that will run.
365 * @see #unregisterOnSharedPreferenceChangeListener
366 */
367 void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener);
368
369 /**
370 * Unregisters a previous callback.
371 *
372 * @param listener The callback that should be unregistered.
373 * @see #registerOnSharedPreferenceChangeListener
374 */
375 void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener);
376}