Aurimas Liutikas | 93554f2 | 2022-04-19 16:51:35 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.content; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | /** |
| 25 | * Interface for accessing and modifying preference data returned by {@link |
| 26 | * Context#getSharedPreferences}. For any particular set of preferences, |
| 27 | * there is a single instance of this class that all clients share. |
| 28 | * Modifications to the preferences must go through an {@link Editor} object |
| 29 | * to ensure the preference values remain in a consistent state and control |
| 30 | * when they are committed to storage. Objects that are returned from the |
| 31 | * various <code>get</code> methods must be treated as immutable by the application. |
| 32 | * |
| 33 | * <p>Note: This class provides strong consistency guarantees. It is using expensive operations |
| 34 | * which might slow down an app. Frequently changing properties or properties where loss can be |
| 35 | * tolerated should use other mechanisms. For more details read the comments on |
| 36 | * {@link Editor#commit()} and {@link Editor#apply()}. |
| 37 | * |
| 38 | * <p><em>Note: This class does not support use across multiple processes.</em> |
| 39 | * |
| 40 | * <div class="special reference"> |
| 41 | * <h3>Developer Guides</h3> |
| 42 | * <p>For more information about using SharedPreferences, read the |
| 43 | * <a href="{@docRoot}guide/topics/data/data-storage.html#pref">Data Storage</a> |
| 44 | * developer guide.</p></div> |
| 45 | * |
| 46 | * @see Context#getSharedPreferences |
| 47 | */ |
| 48 | public interface SharedPreferences { |
| 49 | /** |
| 50 | * Interface definition for a callback to be invoked when a shared |
| 51 | * preference is changed. |
| 52 | */ |
| 53 | public interface OnSharedPreferenceChangeListener { |
| 54 | /** |
| 55 | * Called when a shared preference is changed, added, or removed. This |
| 56 | * may be called even if a preference is set to its existing value. |
| 57 | * |
| 58 | * <p>This callback will be run on your main thread. |
| 59 | * |
| 60 | * <p><em>Note: This callback will not be triggered when preferences are cleared |
| 61 | * via {@link Editor#clear()}, unless targeting {@link android.os.Build.VERSION_CODES#R} |
| 62 | * on devices running OS versions {@link android.os.Build.VERSION_CODES#R Android R} |
| 63 | * or later.</em> |
| 64 | * |
| 65 | * @param sharedPreferences The {@link SharedPreferences} that received the change. |
| 66 | * @param key The key of the preference that was changed, added, or removed. Apps targeting |
| 67 | * {@link android.os.Build.VERSION_CODES#R} on devices running OS versions |
| 68 | * {@link android.os.Build.VERSION_CODES#R Android R} or later, will receive |
| 69 | * a {@code null} value when preferences are cleared. |
| 70 | */ |
| 71 | void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Interface used for modifying values in a {@link SharedPreferences} |
| 76 | * object. All changes you make in an editor are batched, and not copied |
| 77 | * back to the original {@link SharedPreferences} until you call {@link #commit} |
| 78 | * or {@link #apply} |
| 79 | */ |
| 80 | public interface Editor { |
| 81 | /** |
| 82 | * Set a String value in the preferences editor, to be written back once |
| 83 | * {@link #commit} or {@link #apply} are called. |
| 84 | * |
| 85 | * @param key The name of the preference to modify. |
| 86 | * @param value The new value for the preference. Passing {@code null} |
| 87 | * for this argument is equivalent to calling {@link #remove(String)} with |
| 88 | * this key. |
| 89 | * |
| 90 | * @return Returns a reference to the same Editor object, so you can |
| 91 | * chain put calls together. |
| 92 | */ |
| 93 | Editor putString(String key, @Nullable String value); |
| 94 | |
| 95 | /** |
| 96 | * Set a set of String values in the preferences editor, to be written |
| 97 | * back once {@link #commit} or {@link #apply} is called. |
| 98 | * |
| 99 | * @param key The name of the preference to modify. |
| 100 | * @param values The set of new values for the preference. Passing {@code null} |
| 101 | * for this argument is equivalent to calling {@link #remove(String)} with |
| 102 | * this key. |
| 103 | * @return Returns a reference to the same Editor object, so you can |
| 104 | * chain put calls together. |
| 105 | */ |
| 106 | Editor putStringSet(String key, @Nullable Set<String> values); |
| 107 | |
| 108 | /** |
| 109 | * Set an int value in the preferences editor, to be written back once |
| 110 | * {@link #commit} or {@link #apply} are called. |
| 111 | * |
| 112 | * @param key The name of the preference to modify. |
| 113 | * @param value The new value for the preference. |
| 114 | * |
| 115 | * @return Returns a reference to the same Editor object, so you can |
| 116 | * chain put calls together. |
| 117 | */ |
| 118 | Editor putInt(String key, int value); |
| 119 | |
| 120 | /** |
| 121 | * Set a long value in the preferences editor, to be written back once |
| 122 | * {@link #commit} or {@link #apply} are called. |
| 123 | * |
| 124 | * @param key The name of the preference to modify. |
| 125 | * @param value The new value for the preference. |
| 126 | * |
| 127 | * @return Returns a reference to the same Editor object, so you can |
| 128 | * chain put calls together. |
| 129 | */ |
| 130 | Editor putLong(String key, long value); |
| 131 | |
| 132 | /** |
| 133 | * Set a float value in the preferences editor, to be written back once |
| 134 | * {@link #commit} or {@link #apply} are called. |
| 135 | * |
| 136 | * @param key The name of the preference to modify. |
| 137 | * @param value The new value for the preference. |
| 138 | * |
| 139 | * @return Returns a reference to the same Editor object, so you can |
| 140 | * chain put calls together. |
| 141 | */ |
| 142 | Editor putFloat(String key, float value); |
| 143 | |
| 144 | /** |
| 145 | * Set a boolean value in the preferences editor, to be written back |
| 146 | * once {@link #commit} or {@link #apply} are called. |
| 147 | * |
| 148 | * @param key The name of the preference to modify. |
| 149 | * @param value The new value for the preference. |
| 150 | * |
| 151 | * @return Returns a reference to the same Editor object, so you can |
| 152 | * chain put calls together. |
| 153 | */ |
| 154 | Editor putBoolean(String key, boolean value); |
| 155 | |
| 156 | /** |
| 157 | * Mark in the editor that a preference value should be removed, which |
| 158 | * will be done in the actual preferences once {@link #commit} is |
| 159 | * called. |
| 160 | * |
| 161 | * <p>Note that when committing back to the preferences, all removals |
| 162 | * are done first, regardless of whether you called remove before |
| 163 | * or after put methods on this editor. |
| 164 | * |
| 165 | * @param key The name of the preference to remove. |
| 166 | * |
| 167 | * @return Returns a reference to the same Editor object, so you can |
| 168 | * chain put calls together. |
| 169 | */ |
| 170 | Editor remove(String key); |
| 171 | |
| 172 | /** |
| 173 | * Mark in the editor to remove <em>all</em> values from the |
| 174 | * preferences. Once commit is called, the only remaining preferences |
| 175 | * will be any that you have defined in this editor. |
| 176 | * |
| 177 | * <p>Note that when committing back to the preferences, the clear |
| 178 | * is done first, regardless of whether you called clear before |
| 179 | * or after put methods on this editor. |
| 180 | * |
| 181 | * @return Returns a reference to the same Editor object, so you can |
| 182 | * chain put calls together. |
| 183 | */ |
| 184 | Editor clear(); |
| 185 | |
| 186 | /** |
| 187 | * Commit your preferences changes back from this Editor to the |
| 188 | * {@link SharedPreferences} object it is editing. This atomically |
| 189 | * performs the requested modifications, replacing whatever is currently |
| 190 | * in the SharedPreferences. |
| 191 | * |
| 192 | * <p>Note that when two editors are modifying preferences at the same |
| 193 | * time, the last one to call commit wins. |
| 194 | * |
| 195 | * <p>If you don't care about the return value and you're |
| 196 | * using this from your application's main thread, consider |
| 197 | * using {@link #apply} instead. |
| 198 | * |
| 199 | * @return Returns true if the new values were successfully written |
| 200 | * to persistent storage. |
| 201 | */ |
| 202 | boolean commit(); |
| 203 | |
| 204 | /** |
| 205 | * Commit your preferences changes back from this Editor to the |
| 206 | * {@link SharedPreferences} object it is editing. This atomically |
| 207 | * performs the requested modifications, replacing whatever is currently |
| 208 | * in the SharedPreferences. |
| 209 | * |
| 210 | * <p>Note that when two editors are modifying preferences at the same |
| 211 | * time, the last one to call apply wins. |
| 212 | * |
| 213 | * <p>Unlike {@link #commit}, which writes its preferences out |
| 214 | * to persistent storage synchronously, {@link #apply} |
| 215 | * commits its changes to the in-memory |
| 216 | * {@link SharedPreferences} immediately but starts an |
| 217 | * asynchronous commit to disk and you won't be notified of |
| 218 | * any failures. If another editor on this |
| 219 | * {@link SharedPreferences} does a regular {@link #commit} |
| 220 | * while a {@link #apply} is still outstanding, the |
| 221 | * {@link #commit} will block until all async commits are |
| 222 | * completed as well as the commit itself. |
| 223 | * |
| 224 | * <p>As {@link SharedPreferences} instances are singletons within |
| 225 | * a process, it's safe to replace any instance of {@link #commit} with |
| 226 | * {@link #apply} if you were already ignoring the return value. |
| 227 | * |
| 228 | * <p>You don't need to worry about Android component |
| 229 | * lifecycles and their interaction with <code>apply()</code> |
| 230 | * writing to disk. The framework makes sure in-flight disk |
| 231 | * writes from <code>apply()</code> complete before switching |
| 232 | * states. |
| 233 | * |
| 234 | * <p class='note'>The SharedPreferences.Editor interface |
| 235 | * isn't expected to be implemented directly. However, if you |
| 236 | * previously did implement it and are now getting errors |
| 237 | * about missing <code>apply()</code>, you can simply call |
| 238 | * {@link #commit} from <code>apply()</code>. |
| 239 | */ |
| 240 | void apply(); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Retrieve all values from the preferences. |
| 245 | * |
| 246 | * <p>Note that you <em>must not</em> modify the collection returned |
| 247 | * by this method, or alter any of its contents. The consistency of your |
| 248 | * stored data is not guaranteed if you do. |
| 249 | * |
| 250 | * @return Returns a map containing a list of pairs key/value representing |
| 251 | * the preferences. |
| 252 | * |
| 253 | * @throws NullPointerException |
| 254 | */ |
| 255 | Map<String, ?> getAll(); |
| 256 | |
| 257 | /** |
| 258 | * Retrieve a String value from the preferences. |
| 259 | * |
| 260 | * @param key The name of the preference to retrieve. |
| 261 | * @param defValue Value to return if this preference does not exist. |
| 262 | * |
| 263 | * @return Returns the preference value if it exists, or defValue. Throws |
| 264 | * ClassCastException if there is a preference with this name that is not |
| 265 | * a String. |
| 266 | * |
| 267 | * @throws ClassCastException |
| 268 | */ |
| 269 | @Nullable |
| 270 | String getString(String key, @Nullable String defValue); |
| 271 | |
| 272 | /** |
| 273 | * Retrieve a set of String values from the preferences. |
| 274 | * |
| 275 | * <p>Note that you <em>must not</em> modify the set instance returned |
| 276 | * by this call. The consistency of the stored data is not guaranteed |
| 277 | * if you do, nor is your ability to modify the instance at all. |
| 278 | * |
| 279 | * @param key The name of the preference to retrieve. |
| 280 | * @param defValues Values to return if this preference does not exist. |
| 281 | * |
| 282 | * @return Returns the preference values if they exist, or defValues. |
| 283 | * Throws ClassCastException if there is a preference with this name |
| 284 | * that is not a Set. |
| 285 | * |
| 286 | * @throws ClassCastException |
| 287 | */ |
| 288 | @Nullable |
| 289 | Set<String> getStringSet(String key, @Nullable Set<String> defValues); |
| 290 | |
| 291 | /** |
| 292 | * Retrieve an int value from the preferences. |
| 293 | * |
| 294 | * @param key The name of the preference to retrieve. |
| 295 | * @param defValue Value to return if this preference does not exist. |
| 296 | * |
| 297 | * @return Returns the preference value if it exists, or defValue. Throws |
| 298 | * ClassCastException if there is a preference with this name that is not |
| 299 | * an int. |
| 300 | * |
| 301 | * @throws ClassCastException |
| 302 | */ |
| 303 | int getInt(String key, int defValue); |
| 304 | |
| 305 | /** |
| 306 | * Retrieve a long value from the preferences. |
| 307 | * |
| 308 | * @param key The name of the preference to retrieve. |
| 309 | * @param defValue Value to return if this preference does not exist. |
| 310 | * |
| 311 | * @return Returns the preference value if it exists, or defValue. Throws |
| 312 | * ClassCastException if there is a preference with this name that is not |
| 313 | * a long. |
| 314 | * |
| 315 | * @throws ClassCastException |
| 316 | */ |
| 317 | long getLong(String key, long defValue); |
| 318 | |
| 319 | /** |
| 320 | * Retrieve a float value from the preferences. |
| 321 | * |
| 322 | * @param key The name of the preference to retrieve. |
| 323 | * @param defValue Value to return if this preference does not exist. |
| 324 | * |
| 325 | * @return Returns the preference value if it exists, or defValue. Throws |
| 326 | * ClassCastException if there is a preference with this name that is not |
| 327 | * a float. |
| 328 | * |
| 329 | * @throws ClassCastException |
| 330 | */ |
| 331 | float getFloat(String key, float defValue); |
| 332 | |
| 333 | /** |
| 334 | * Retrieve a boolean value from the preferences. |
| 335 | * |
| 336 | * @param key The name of the preference to retrieve. |
| 337 | * @param defValue Value to return if this preference does not exist. |
| 338 | * |
| 339 | * @return Returns the preference value if it exists, or defValue. Throws |
| 340 | * ClassCastException if there is a preference with this name that is not |
| 341 | * a boolean. |
| 342 | * |
| 343 | * @throws ClassCastException |
| 344 | */ |
| 345 | boolean getBoolean(String key, boolean defValue); |
| 346 | |
| 347 | /** |
| 348 | * Checks whether the preferences contains a preference. |
| 349 | * |
| 350 | * @param key The name of the preference to check. |
| 351 | * @return Returns true if the preference exists in the preferences, |
| 352 | * otherwise false. |
| 353 | */ |
| 354 | boolean contains(String key); |
| 355 | |
| 356 | /** |
| 357 | * Create a new Editor for these preferences, through which you can make |
| 358 | * modifications to the data in the preferences and atomically commit those |
| 359 | * changes back to the SharedPreferences object. |
| 360 | * |
| 361 | * <p>Note that you <em>must</em> call {@link Editor#commit} to have any |
| 362 | * changes you perform in the Editor actually show up in the |
| 363 | * SharedPreferences. |
| 364 | * |
| 365 | * @return Returns a new instance of the {@link Editor} interface, allowing |
| 366 | * you to modify the values in this SharedPreferences object. |
| 367 | */ |
| 368 | Editor edit(); |
| 369 | |
| 370 | /** |
| 371 | * Registers a callback to be invoked when a change happens to a preference. |
| 372 | * |
| 373 | * <p class="caution"><strong>Caution:</strong> The preference manager does |
| 374 | * not currently store a strong reference to the listener. You must store a |
| 375 | * strong reference to the listener, or it will be susceptible to garbage |
| 376 | * collection. We recommend you keep a reference to the listener in the |
| 377 | * instance data of an object that will exist as long as you need the |
| 378 | * listener.</p> |
| 379 | * |
| 380 | * @param listener The callback that will run. |
| 381 | * @see #unregisterOnSharedPreferenceChangeListener |
| 382 | */ |
| 383 | void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener); |
| 384 | |
| 385 | /** |
| 386 | * Unregisters a previous callback. |
| 387 | * |
| 388 | * @param listener The callback that should be unregistered. |
| 389 | * @see #registerOnSharedPreferenceChangeListener |
| 390 | */ |
| 391 | void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener); |
| 392 | } |