SharedPreferences.java revision 7f7ce40f90cf00dc046fb9520d77d29e96b474d6
1d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala/*
2d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * Copyright (C) 2006 The Android Open Source Project
3d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala *
4d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * Licensed under the Apache License, Version 2.0 (the "License");
5d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * you may not use this file except in compliance with the License.
6d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * You may obtain a copy of the License at
7d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala *
8d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala *      http://www.apache.org/licenses/LICENSE-2.0
9d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala *
10d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * Unless required by applicable law or agreed to in writing, software
11d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * distributed under the License is distributed on an "AS IS" BASIS,
12d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * See the License for the specific language governing permissions and
14d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * limitations under the License.
15d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala */
16d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
174bb8118816874c696d9f1adab48490df1da365f7Eino-Ville Talvalapackage android.content;
18d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
19d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvalaimport java.util.Map;
20d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvalaimport java.util.Set;
21d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
22d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala/**
23d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * Interface for accessing and modifying preference data returned by {@link
24d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * Context#getSharedPreferences}.  For any particular set of preferences,
25d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * there is a single instance of this class that all clients share.
26d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * Modifications to the preferences must go through an {@link Editor} object
27d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * to ensure the preference values remain in a consistent state and control
28d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * when they are committed to storage.
29d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala *
30d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * <p><em>Note: currently this class does not support use across multiple
31d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * processes.  This will be added later.</em>
32d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala *
33d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala * @see Context#getSharedPreferences
34d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala */
35d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvalapublic interface SharedPreferences {
36d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    /**
37d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * Interface definition for a callback to be invoked when a shared
38d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * preference is changed.
39d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     */
40d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    public interface OnSharedPreferenceChangeListener {
41d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        /**
42cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala         * Called when a shared preference is changed, added, or removed. This
43d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * may be called even if a preference is set to its existing value.
44d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
45d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * <p>This callback will be run on your main thread.
46d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
47d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param sharedPreferences The {@link SharedPreferences} that received
48d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *            the change.
49d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param key The key of the preference that was changed, added, or
50d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *            removed.
51d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         */
52d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key);
53d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    }
54d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
55d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    /**
56d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * Interface used for modifying values in a {@link SharedPreferences}
57d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * object.  All changes you make in an editor are batched, and not copied
58d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * back to the original {@link SharedPreferences} or persistent storage
59d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * until you call {@link #commit}.
60d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     */
61d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    public interface Editor {
62d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        /**
63d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * Set a String value in the preferences editor, to be written back once
64d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * {@link #commit} is called.
65d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
66d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param key The name of the preference to modify.
67d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param value The new value for the preference.
68d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
69d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @return Returns a reference to the same Editor object, so you can
70d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * chain put calls together.
71d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         */
72d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        Editor putString(String key, String value);
73d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
74d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        /**
75d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * Set a set of String values in the preferences editor, to be written
76d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * back once {@link #commit} is called.
77d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
78d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param key The name of the preference to modify.
79d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param values The new values for the preference.
80d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @return Returns a reference to the same Editor object, so you can
81d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * chain put calls together.
82d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         */
83d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        Editor putStringSet(String key, Set<String> values);
84d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
85d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        /**
86d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * Set an int value in the preferences editor, to be written back once
87d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * {@link #commit} is called.
88d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
89ecf17e82505fdb60d59e00b6dd59036df93de655Igor Murashkin         * @param key The name of the preference to modify.
90ecf17e82505fdb60d59e00b6dd59036df93de655Igor Murashkin         * @param value The new value for the preference.
91d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
92d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @return Returns a reference to the same Editor object, so you can
93d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * chain put calls together.
94d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         */
95d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        Editor putInt(String key, int value);
96d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
97d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        /**
98d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * Set a long value in the preferences editor, to be written back once
99d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * {@link #commit} is called.
100d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
101d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param key The name of the preference to modify.
102d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param value The new value for the preference.
103d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
104d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @return Returns a reference to the same Editor object, so you can
105d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * chain put calls together.
106d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         */
107d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        Editor putLong(String key, long value);
108d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
109d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        /**
110d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * Set a float value in the preferences editor, to be written back once
111d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * {@link #commit} is called.
112d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
113d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param key The name of the preference to modify.
114d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param value The new value for the preference.
115d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
116d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @return Returns a reference to the same Editor object, so you can
117d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * chain put calls together.
118d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         */
119d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        Editor putFloat(String key, float value);
120d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
121d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        /**
122d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * Set a boolean value in the preferences editor, to be written back
123d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * once {@link #commit} is called.
124d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
125d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param key The name of the preference to modify.
126d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param value The new value for the preference.
127d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
128d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @return Returns a reference to the same Editor object, so you can
129d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * chain put calls together.
130d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         */
131d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        Editor putBoolean(String key, boolean value);
132cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala
133cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala        /**
134cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala         * Mark in the editor that a preference value should be removed, which
135cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala         * will be done in the actual preferences once {@link #commit} is
136cf70d3469332445dc3ffd09729da3538612b1bb2Eino-Ville Talvala         * called.
137d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
138d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * <p>Note that when committing back to the preferences, all removals
139d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * are done first, regardless of whether you called remove before
140d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * or after put methods on this editor.
141d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
142d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @param key The name of the preference to remove.
143d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
144d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @return Returns a reference to the same Editor object, so you can
145d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * chain put calls together.
146d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         */
147da6665cbd06ca58d3357c3002b7366d13e23f152Eino-Ville Talvala        Editor remove(String key);
148d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
149d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        /**
150d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * Mark in the editor to remove <em>all</em> values from the
151d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * preferences.  Once commit is called, the only remaining preferences
152d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * will be any that you have defined in this editor.
153d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
154d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * <p>Note that when committing back to the preferences, the clear
155d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * is done first, regardless of whether you called clear before
156d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * or after put methods on this editor.
157d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
158d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @return Returns a reference to the same Editor object, so you can
159d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * chain put calls together.
160d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         */
161d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        Editor clear();
162d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
163d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        /**
164d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * Commit your preferences changes back from this Editor to the
165d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * {@link SharedPreferences} object it is editing.  This atomically
166d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * performs the requested modifications, replacing whatever is currently
167d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * in the SharedPreferences.
168d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
169d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * <p>Note that when two editors are modifying preferences at the same
170d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * time, the last one to call commit wins.
171d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
172d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * <p>If you don't care about the return value and you're
173d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * using this from your application's main thread, consider
174d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * using {@link #startCommit} instead.
175d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
176d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * @return Returns true if the new values were successfully written
177d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * to persistent storage.
178d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         */
179d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        boolean commit();
180d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
181d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        /**
182d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * Commit your preferences changes back from this Editor to the
183d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * {@link SharedPreferences} object it is editing.  This atomically
184d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * performs the requested modifications, replacing whatever is currently
185d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * in the SharedPreferences.
186d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
187d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * <p>Note that when two editors are modifying preferences at the same
188d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * time, the last one to call commit wins.
189d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
190d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * <p>Unlike {@link #commit}, which writes its preferences out
191d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * to persistent storage synchronously, {@link #startCommit}
192d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * commits its changes to the in-memory
193d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * {@link SharedPreferences} immediately but starts an
194d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * asynchronous commit to disk and you won't be notified of
195d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * any failures.  If another editor on this
196d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * {@link SharedPreferences} does a regular {@link #commit}
197d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * while a {@link #startCommit} is still outstanding, the
198d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * {@link #commit} will block until all async commits are
199d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * completed as well as the commit itself.
200d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         *
201d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * <p>If you call this from an {@link android.app.Activity},
202d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * the base class will wait for any async commits to finish in
203d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         * its {@link android.app.Activity#onPause}.</p>
204d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala         */
205d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala        void startCommit();
206d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    }
207d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
208d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    /**
209d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * Retrieve all values from the preferences.
210d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
211d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @return Returns a map containing a list of pairs key/value representing
212d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * the preferences.
213d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
214d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @throws NullPointerException
215d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     */
216d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    Map<String, ?> getAll();
217d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
218d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    /**
219d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * Retrieve a String value from the preferences.
220d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
221d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @param key The name of the preference to retrieve.
222d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @param defValue Value to return if this preference does not exist.
223d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
224d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @return Returns the preference value if it exists, or defValue.  Throws
225d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * ClassCastException if there is a preference with this name that is not
226d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * a String.
227d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
228d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @throws ClassCastException
229d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     */
230d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    String getString(String key, String defValue);
231d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
232d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    /**
233d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * Retrieve a set of String values from the preferences.
234d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
235d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @param key The name of the preference to retrieve.
236d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @param defValues Values to return if this preference does not exist.
237d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
238d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @return Returns the preference values if they exist, or defValues.
239d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * Throws ClassCastException if there is a preference with this name
240d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * that is not a Set.
241d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
242d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @throws ClassCastException
243d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     */
244d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    Set<String> getStringSet(String key, Set<String> defValues);
245d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
246d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    /**
247d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * Retrieve an int value from the preferences.
248d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
249d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @param key The name of the preference to retrieve.
250d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @param defValue Value to return if this preference does not exist.
251d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
252d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @return Returns the preference value if it exists, or defValue.  Throws
253d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * ClassCastException if there is a preference with this name that is not
254d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * an int.
255d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
256d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @throws ClassCastException
257d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     */
258d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    int getInt(String key, int defValue);
259d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
260d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    /**
261d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * Retrieve a long value from the preferences.
262d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
263d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @param key The name of the preference to retrieve.
264d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @param defValue Value to return if this preference does not exist.
265d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
266d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @return Returns the preference value if it exists, or defValue.  Throws
267d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * ClassCastException if there is a preference with this name that is not
268d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * a long.
269d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
270d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @throws ClassCastException
271d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     */
272d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    long getLong(String key, long defValue);
273d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
274d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    /**
275d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * Retrieve a float value from the preferences.
276d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
277d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @param key The name of the preference to retrieve.
278d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @param defValue Value to return if this preference does not exist.
279d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
280d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @return Returns the preference value if it exists, or defValue.  Throws
281d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * ClassCastException if there is a preference with this name that is not
282d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * a float.
283d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
284d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @throws ClassCastException
285d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     */
286d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    float getFloat(String key, float defValue);
287d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
288d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    /**
289d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * Retrieve a boolean value from the preferences.
290d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
291d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @param key The name of the preference to retrieve.
292d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @param defValue Value to return if this preference does not exist.
293d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
294d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @return Returns the preference value if it exists, or defValue.  Throws
295d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * ClassCastException if there is a preference with this name that is not
296d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * a boolean.
297d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     *
298d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     * @throws ClassCastException
299d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala     */
300d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala    boolean getBoolean(String key, boolean defValue);
301d86a6880fe86bda21a9b53b240996fc410a512a5Eino-Ville Talvala
302    /**
303     * Checks whether the preferences contains a preference.
304     *
305     * @param key The name of the preference to check.
306     * @return Returns true if the preference exists in the preferences,
307     *         otherwise false.
308     */
309    boolean contains(String key);
310
311    /**
312     * Create a new Editor for these preferences, through which you can make
313     * modifications to the data in the preferences and atomically commit those
314     * changes back to the SharedPreferences object.
315     *
316     * <p>Note that you <em>must</em> call {@link Editor#commit} to have any
317     * changes you perform in the Editor actually show up in the
318     * SharedPreferences.
319     *
320     * @return Returns a new instance of the {@link Editor} interface, allowing
321     * you to modify the values in this SharedPreferences object.
322     */
323    Editor edit();
324
325    /**
326     * Registers a callback to be invoked when a change happens to a preference.
327     *
328     * @param listener The callback that will run.
329     * @see #unregisterOnSharedPreferenceChangeListener
330     */
331    void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener);
332
333    /**
334     * Unregisters a previous callback.
335     *
336     * @param listener The callback that should be unregistered.
337     * @see #registerOnSharedPreferenceChangeListener
338     */
339    void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener);
340}
341