10b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis/* 20b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Copyright (C) 2016 The Android Open Source Project 30b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 40b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Licensed under the Apache License, Version 2.0 (the "License"); 50b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * you may not use this file except in compliance with the License. 60b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * You may obtain a copy of the License at 70b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 80b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * http://www.apache.org/licenses/LICENSE-2.0 90b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 100b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Unless required by applicable law or agreed to in writing, software 110b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * distributed under the License is distributed on an "AS IS" BASIS, 120b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * See the License for the specific language governing permissions and 140b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * limitations under the License 150b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 160b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 170b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlispackage android.preference; 180b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 190b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlisimport android.annotation.Nullable; 200b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 210b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlisimport java.util.Set; 220b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 230b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis/** 24fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * A data store interface to be implemented and provided to the Preferences framework. This can be 25fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * used to replace the default {@link android.content.SharedPreferences}, if needed. 260b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 27fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * <p>In most cases you want to use {@link android.content.SharedPreferences} as it is automatically 28fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * backed up and migrated to new devices. However, providing custom data store to preferences can be 29fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * useful if your app stores its preferences in a local db, cloud or they are device specific like 30fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * "Developer settings". It might be also useful when you want to use the preferences UI but 31fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * the data are not supposed to be stored at all because they are valid per session only. 32fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * 33fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * <p>Once a put method is called it is full responsibility of the data store implementation to 34fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * safely store the given values. Time expensive operations need to be done in the background to 35fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * prevent from blocking the UI. You also need to have a plan on how to serialize the data in case 36fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * the activity holding this object gets destroyed. 37fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * 38fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * <p>By default, all "put" methods throw {@link UnsupportedOperationException}. 39fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * 40fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * @see Preference#setPreferenceDataStore(PreferenceDataStore) 41fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * @see PreferenceManager#setPreferenceDataStore(PreferenceDataStore) 420b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 430b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlispublic interface PreferenceDataStore { 440b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 450b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis /** 460b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Set a String value to the data store. 470b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 48fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * <p>Once the value is set the data store is responsible for holding it. 49fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * 500b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param key The name of the preference to modify. 510b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param value The new value for the preference. 520b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @see #getString(String, String) 530b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 540b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis default void putString(String key, @Nullable String value) { 550b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis throw new UnsupportedOperationException("Not implemented on this data store"); 560b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis } 570b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 580b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis /** 590b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Set a set of String value to the data store. 600b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 61fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * <p>Once the value is set the data store is responsible for holding it. 62fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * 630b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param key The name of the preference to modify. 640b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param values The set of new values for the preference. 650b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @see #getStringSet(String, Set) 660b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 670b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis default void putStringSet(String key, @Nullable Set<String> values) { 680b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis throw new UnsupportedOperationException("Not implemented on this data store"); 690b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis } 700b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 710b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis /** 720b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Set an int value to the data store. 730b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 74fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * <p>Once the value is set the data store is responsible for holding it. 75fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * 760b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param key The name of the preference to modify. 770b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param value The new value for the preference. 780b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @see #getInt(String, int) 790b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 800b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis default void putInt(String key, int value) { 810b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis throw new UnsupportedOperationException("Not implemented on this data store"); 820b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis } 830b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 840b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis /** 850b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Set a long value to the data store. 860b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 87fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * <p>Once the value is set the data store is responsible for holding it. 88fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * 890b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param key The name of the preference to modify. 900b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param value The new value for the preference. 910b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @see #getLong(String, long) 920b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 930b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis default void putLong(String key, long value) { 940b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis throw new UnsupportedOperationException("Not implemented on this data store"); 950b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis } 960b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 970b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis /** 980b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Set a float value to the data store. 990b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 100fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * <p>Once the value is set the data store is responsible for holding it. 101fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * 1020b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param key The name of the preference to modify. 1030b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param value The new value for the preference. 1040b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @see #getFloat(String, float) 1050b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 1060b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis default void putFloat(String key, float value) { 1070b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis throw new UnsupportedOperationException("Not implemented on this data store"); 1080b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis } 1090b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 1100b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis /** 1110b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Set a boolean value to the data store. 1120b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 113fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * <p>Once the value is set the data store is responsible for holding it. 114fd59645ba7d4445b3308489ef07c700aaed85c6fFilip Pavlis * 1150b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param key The name of the preference to modify. 1160b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param value The new value for the preference. 1170b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @see #getBoolean(String, boolean) 1180b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 1190b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis default void putBoolean(String key, boolean value) { 1200b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis throw new UnsupportedOperationException("Not implemented on this data store"); 1210b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis } 1220b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 1230b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis /** 1240b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Retrieve a String value from the data store. 1250b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 1260b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param key The name of the preference to retrieve. 1270b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param defValue Value to return if this preference does not exist. 1280b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @see #putString(String, String) 1290b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 1300b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis @Nullable 1310b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis default String getString(String key, @Nullable String defValue) { 1320b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis return defValue; 1330b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis } 1340b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 1350b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis /** 1360b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Retrieve a set of String values from the data store. 1370b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 1380b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param key The name of the preference to retrieve. 1390b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param defValues Values to return if this preference does not exist. 1400b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @see #putStringSet(String, Set) 1410b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 1420b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis @Nullable 1430b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis default Set<String> getStringSet(String key, @Nullable Set<String> defValues) { 1440b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis return defValues; 1450b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis } 1460b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 1470b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis /** 1480b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Retrieve an int value from the data store. 1490b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 1500b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param key The name of the preference to retrieve. 1510b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param defValue Value to return if this preference does not exist. 1520b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @see #putInt(String, int) 1530b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 1540b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis default int getInt(String key, int defValue) { 1550b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis return defValue; 1560b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis } 1570b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 1580b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis /** 1590b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Retrieve a long value from the data store. 1600b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 1610b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param key The name of the preference to retrieve. 1620b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param defValue Value to return if this preference does not exist. 1630b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @see #putLong(String, long) 1640b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 1650b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis default long getLong(String key, long defValue) { 1660b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis return defValue; 1670b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis } 1680b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 1690b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis /** 1700b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Retrieve a float value from the data store. 1710b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 1720b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param key The name of the preference to retrieve. 1730b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param defValue Value to return if this preference does not exist. 1740b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @see #putFloat(String, float) 1750b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 1760b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis default float getFloat(String key, float defValue) { 1770b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis return defValue; 1780b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis } 1790b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis 1800b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis /** 1810b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * Retrieve a boolean value from the data store. 1820b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * 1830b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param key The name of the preference to retrieve. 1840b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @param defValue Value to return if this preference does not exist. 1850b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis * @see #getBoolean(String, boolean) 1860b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis */ 1870b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis default boolean getBoolean(String key, boolean defValue) { 1880b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis return defValue; 1890b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis } 1900b0c6cbdaf982642a62595e466f0f66447d053e5Filip Pavlis} 191