| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729 |
- package com.cooleshow.base.utils;
- import android.content.SharedPreferences;
- import androidx.annotation.NonNull;
- import java.util.Map;
- import java.util.Set;
- /**
- * <pre>
- * author: Blankj
- * blog : http://blankj.com
- * time : 2019/01/04
- * desc : utils about shared preference
- * </pre>
- */
- public final class SPStaticUtils {
- private static SPUtils sDefaultSPUtils;
- /**
- * Set the default instance of {@link SPUtils}.
- *
- * @param spUtils The default instance of {@link SPUtils}.
- */
- public static void setDefaultSPUtils(final SPUtils spUtils) {
- sDefaultSPUtils = spUtils;
- }
- /**
- * Put the string value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- */
- public static void put(@NonNull final String key, final String value) {
- put(key, value, getDefaultSPUtils());
- }
- /**
- * Put the string value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- */
- public static void put(@NonNull final String key, final String value, final boolean isCommit) {
- put(key, value, isCommit, getDefaultSPUtils());
- }
- /**
- * Return the string value in sp.
- *
- * @param key The key of sp.
- * @return the string value if sp exists or {@code ""} otherwise
- */
- public static String getString(@NonNull final String key) {
- return getString(key, getDefaultSPUtils());
- }
- /**
- * Return the string value in sp.
- *
- * @param key The key of sp.
- * @param defaultValue The default value if the sp doesn't exist.
- * @return the string value if sp exists or {@code defaultValue} otherwise
- */
- public static String getString(@NonNull final String key, final String defaultValue) {
- return getString(key, defaultValue, getDefaultSPUtils());
- }
- /**
- * Put the int value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- */
- public static void put(@NonNull final String key, final int value) {
- put(key, value, getDefaultSPUtils());
- }
- /**
- * Put the int value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- */
- public static void put(@NonNull final String key, final int value, final boolean isCommit) {
- put(key, value, isCommit, getDefaultSPUtils());
- }
- /**
- * Return the int value in sp.
- *
- * @param key The key of sp.
- * @return the int value if sp exists or {@code -1} otherwise
- */
- public static int getInt(@NonNull final String key) {
- return getInt(key, getDefaultSPUtils());
- }
- /**
- * Return the int value in sp.
- *
- * @param key The key of sp.
- * @param defaultValue The default value if the sp doesn't exist.
- * @return the int value if sp exists or {@code defaultValue} otherwise
- */
- public static int getInt(@NonNull final String key, final int defaultValue) {
- return getInt(key, defaultValue, getDefaultSPUtils());
- }
- /**
- * Put the long value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- */
- public static void put(@NonNull final String key, final long value) {
- put(key, value, getDefaultSPUtils());
- }
- /**
- * Put the long value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- */
- public static void put(@NonNull final String key, final long value, final boolean isCommit) {
- put(key, value, isCommit, getDefaultSPUtils());
- }
- /**
- * Return the long value in sp.
- *
- * @param key The key of sp.
- * @return the long value if sp exists or {@code -1} otherwise
- */
- public static long getLong(@NonNull final String key) {
- return getLong(key, getDefaultSPUtils());
- }
- /**
- * Return the long value in sp.
- *
- * @param key The key of sp.
- * @param defaultValue The default value if the sp doesn't exist.
- * @return the long value if sp exists or {@code defaultValue} otherwise
- */
- public static long getLong(@NonNull final String key, final long defaultValue) {
- return getLong(key, defaultValue, getDefaultSPUtils());
- }
- /**
- * Put the float value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- */
- public static void put(@NonNull final String key, final float value) {
- put(key, value, getDefaultSPUtils());
- }
- /**
- * Put the float value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- */
- public static void put(@NonNull final String key, final float value, final boolean isCommit) {
- put(key, value, isCommit, getDefaultSPUtils());
- }
- /**
- * Return the float value in sp.
- *
- * @param key The key of sp.
- * @return the float value if sp exists or {@code -1f} otherwise
- */
- public static float getFloat(@NonNull final String key) {
- return getFloat(key, getDefaultSPUtils());
- }
- /**
- * Return the float value in sp.
- *
- * @param key The key of sp.
- * @param defaultValue The default value if the sp doesn't exist.
- * @return the float value if sp exists or {@code defaultValue} otherwise
- */
- public static float getFloat(@NonNull final String key, final float defaultValue) {
- return getFloat(key, defaultValue, getDefaultSPUtils());
- }
- /**
- * Put the boolean value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- */
- public static void put(@NonNull final String key, final boolean value) {
- put(key, value, getDefaultSPUtils());
- }
- /**
- * Put the boolean value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- */
- public static void put(@NonNull final String key, final boolean value, final boolean isCommit) {
- put(key, value, isCommit, getDefaultSPUtils());
- }
- /**
- * Return the boolean value in sp.
- *
- * @param key The key of sp.
- * @return the boolean value if sp exists or {@code false} otherwise
- */
- public static boolean getBoolean(@NonNull final String key) {
- return getBoolean(key, getDefaultSPUtils());
- }
- /**
- * Return the boolean value in sp.
- *
- * @param key The key of sp.
- * @param defaultValue The default value if the sp doesn't exist.
- * @return the boolean value if sp exists or {@code defaultValue} otherwise
- */
- public static boolean getBoolean(@NonNull final String key, final boolean defaultValue) {
- return getBoolean(key, defaultValue, getDefaultSPUtils());
- }
- /**
- * Put the set of string value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- */
- public static void put(@NonNull final String key, final Set<String> value) {
- put(key, value, getDefaultSPUtils());
- }
- /**
- * Put the set of string value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- */
- public static void put(@NonNull final String key,
- final Set<String> value,
- final boolean isCommit) {
- put(key, value, isCommit, getDefaultSPUtils());
- }
- /**
- * Return the set of string value in sp.
- *
- * @param key The key of sp.
- * @return the set of string value if sp exists
- * or {@code Collections.<String>emptySet()} otherwise
- */
- public static Set<String> getStringSet(@NonNull final String key) {
- return getStringSet(key, getDefaultSPUtils());
- }
- /**
- * Return the set of string value in sp.
- *
- * @param key The key of sp.
- * @param defaultValue The default value if the sp doesn't exist.
- * @return the set of string value if sp exists or {@code defaultValue} otherwise
- */
- public static Set<String> getStringSet(@NonNull final String key,
- final Set<String> defaultValue) {
- return getStringSet(key, defaultValue, getDefaultSPUtils());
- }
- /**
- * Return all values in sp.
- *
- * @return all values in sp
- */
- public static Map<String, ?> getAll() {
- return getAll(getDefaultSPUtils());
- }
- /**
- * Return whether the sp contains the preference.
- *
- * @param key The key of sp.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean contains(@NonNull final String key) {
- return contains(key, getDefaultSPUtils());
- }
- /**
- * Remove the preference in sp.
- *
- * @param key The key of sp.
- */
- public static void remove(@NonNull final String key) {
- remove(key, getDefaultSPUtils());
- }
- /**
- * Remove the preference in sp.
- *
- * @param key The key of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- */
- public static void remove(@NonNull final String key, final boolean isCommit) {
- remove(key, isCommit, getDefaultSPUtils());
- }
- /**
- * Remove all preferences in sp.
- */
- public static void clear() {
- clear(getDefaultSPUtils());
- }
- /**
- * Remove all preferences in sp.
- *
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- */
- public static void clear(final boolean isCommit) {
- clear(isCommit, getDefaultSPUtils());
- }
- ///////////////////////////////////////////////////////////////////////////
- // dividing line
- ///////////////////////////////////////////////////////////////////////////
- /**
- * Put the string value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void put(@NonNull final String key, final String value, @NonNull final SPUtils spUtils) {
- spUtils.put(key, value);
- }
- /**
- * Put the string value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void put(@NonNull final String key,
- final String value,
- final boolean isCommit,
- @NonNull final SPUtils spUtils) {
- spUtils.put(key, value, isCommit);
- }
- /**
- * Return the string value in sp.
- *
- * @param key The key of sp.
- * @param spUtils The instance of {@link SPUtils}.
- * @return the string value if sp exists or {@code ""} otherwise
- */
- public static String getString(@NonNull final String key, @NonNull final SPUtils spUtils) {
- return spUtils.getString(key);
- }
- /**
- * Return the string value in sp.
- *
- * @param key The key of sp.
- * @param defaultValue The default value if the sp doesn't exist.
- * @param spUtils The instance of {@link SPUtils}.
- * @return the string value if sp exists or {@code defaultValue} otherwise
- */
- public static String getString(@NonNull final String key,
- final String defaultValue,
- @NonNull final SPUtils spUtils) {
- return spUtils.getString(key, defaultValue);
- }
- /**
- * Put the int value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void put(@NonNull final String key, final int value, @NonNull final SPUtils spUtils) {
- spUtils.put(key, value);
- }
- /**
- * Put the int value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void put(@NonNull final String key,
- final int value,
- final boolean isCommit,
- @NonNull final SPUtils spUtils) {
- spUtils.put(key, value, isCommit);
- }
- /**
- * Return the int value in sp.
- *
- * @param key The key of sp.
- * @param spUtils The instance of {@link SPUtils}.
- * @return the int value if sp exists or {@code -1} otherwise
- */
- public static int getInt(@NonNull final String key, @NonNull final SPUtils spUtils) {
- return spUtils.getInt(key);
- }
- /**
- * Return the int value in sp.
- *
- * @param key The key of sp.
- * @param defaultValue The default value if the sp doesn't exist.
- * @param spUtils The instance of {@link SPUtils}.
- * @return the int value if sp exists or {@code defaultValue} otherwise
- */
- public static int getInt(@NonNull final String key, final int defaultValue, @NonNull final SPUtils spUtils) {
- return spUtils.getInt(key, defaultValue);
- }
- /**
- * Put the long value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void put(@NonNull final String key, final long value, @NonNull final SPUtils spUtils) {
- spUtils.put(key, value);
- }
- /**
- * Put the long value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void put(@NonNull final String key,
- final long value,
- final boolean isCommit,
- @NonNull final SPUtils spUtils) {
- spUtils.put(key, value, isCommit);
- }
- /**
- * Return the long value in sp.
- *
- * @param key The key of sp.
- * @param spUtils The instance of {@link SPUtils}.
- * @return the long value if sp exists or {@code -1} otherwise
- */
- public static long getLong(@NonNull final String key, @NonNull final SPUtils spUtils) {
- return spUtils.getLong(key);
- }
- /**
- * Return the long value in sp.
- *
- * @param key The key of sp.
- * @param defaultValue The default value if the sp doesn't exist.
- * @param spUtils The instance of {@link SPUtils}.
- * @return the long value if sp exists or {@code defaultValue} otherwise
- */
- public static long getLong(@NonNull final String key, final long defaultValue, @NonNull final SPUtils spUtils) {
- return spUtils.getLong(key, defaultValue);
- }
- /**
- * Put the float value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void put(@NonNull final String key, final float value, @NonNull final SPUtils spUtils) {
- spUtils.put(key, value);
- }
- /**
- * Put the float value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void put(@NonNull final String key,
- final float value,
- final boolean isCommit,
- @NonNull final SPUtils spUtils) {
- spUtils.put(key, value, isCommit);
- }
- /**
- * Return the float value in sp.
- *
- * @param key The key of sp.
- * @param spUtils The instance of {@link SPUtils}.
- * @return the float value if sp exists or {@code -1f} otherwise
- */
- public static float getFloat(@NonNull final String key, @NonNull final SPUtils spUtils) {
- return spUtils.getFloat(key);
- }
- /**
- * Return the float value in sp.
- *
- * @param key The key of sp.
- * @param defaultValue The default value if the sp doesn't exist.
- * @param spUtils The instance of {@link SPUtils}.
- * @return the float value if sp exists or {@code defaultValue} otherwise
- */
- public static float getFloat(@NonNull final String key, final float defaultValue, @NonNull final SPUtils spUtils) {
- return spUtils.getFloat(key, defaultValue);
- }
- /**
- * Put the boolean value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void put(@NonNull final String key, final boolean value, @NonNull final SPUtils spUtils) {
- spUtils.put(key, value);
- }
- /**
- * Put the boolean value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void put(@NonNull final String key,
- final boolean value,
- final boolean isCommit,
- @NonNull final SPUtils spUtils) {
- spUtils.put(key, value, isCommit);
- }
- /**
- * Return the boolean value in sp.
- *
- * @param key The key of sp.
- * @param spUtils The instance of {@link SPUtils}.
- * @return the boolean value if sp exists or {@code false} otherwise
- */
- public static boolean getBoolean(@NonNull final String key, @NonNull final SPUtils spUtils) {
- return spUtils.getBoolean(key);
- }
- /**
- * Return the boolean value in sp.
- *
- * @param key The key of sp.
- * @param defaultValue The default value if the sp doesn't exist.
- * @param spUtils The instance of {@link SPUtils}.
- * @return the boolean value if sp exists or {@code defaultValue} otherwise
- */
- public static boolean getBoolean(@NonNull final String key,
- final boolean defaultValue,
- @NonNull final SPUtils spUtils) {
- return spUtils.getBoolean(key, defaultValue);
- }
- /**
- * Put the set of string value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void put(@NonNull final String key, final Set<String> value, @NonNull final SPUtils spUtils) {
- spUtils.put(key, value);
- }
- /**
- * Put the set of string value in sp.
- *
- * @param key The key of sp.
- * @param value The value of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void put(@NonNull final String key,
- final Set<String> value,
- final boolean isCommit,
- @NonNull final SPUtils spUtils) {
- spUtils.put(key, value, isCommit);
- }
- /**
- * Return the set of string value in sp.
- *
- * @param key The key of sp.
- * @param spUtils The instance of {@link SPUtils}.
- * @return the set of string value if sp exists
- * or {@code Collections.<String>emptySet()} otherwise
- */
- public static Set<String> getStringSet(@NonNull final String key, @NonNull final SPUtils spUtils) {
- return spUtils.getStringSet(key);
- }
- /**
- * Return the set of string value in sp.
- *
- * @param key The key of sp.
- * @param defaultValue The default value if the sp doesn't exist.
- * @param spUtils The instance of {@link SPUtils}.
- * @return the set of string value if sp exists or {@code defaultValue} otherwise
- */
- public static Set<String> getStringSet(@NonNull final String key,
- final Set<String> defaultValue,
- @NonNull final SPUtils spUtils) {
- return spUtils.getStringSet(key, defaultValue);
- }
- /**
- * Return all values in sp.
- *
- * @param spUtils The instance of {@link SPUtils}.
- * @return all values in sp
- */
- public static Map<String, ?> getAll(@NonNull final SPUtils spUtils) {
- return spUtils.getAll();
- }
- /**
- * Return whether the sp contains the preference.
- *
- * @param key The key of sp.
- * @param spUtils The instance of {@link SPUtils}.
- * @return {@code true}: yes<br>{@code false}: no
- */
- public static boolean contains(@NonNull final String key, @NonNull final SPUtils spUtils) {
- return spUtils.contains(key);
- }
- /**
- * Remove the preference in sp.
- *
- * @param key The key of sp.
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void remove(@NonNull final String key, @NonNull final SPUtils spUtils) {
- spUtils.remove(key);
- }
- /**
- * Remove the preference in sp.
- *
- * @param key The key of sp.
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void remove(@NonNull final String key, final boolean isCommit, @NonNull final SPUtils spUtils) {
- spUtils.remove(key, isCommit);
- }
- /**
- * Remove all preferences in sp.
- *
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void clear(@NonNull final SPUtils spUtils) {
- spUtils.clear();
- }
- /**
- * Remove all preferences in sp.
- *
- * @param isCommit True to use {@link SharedPreferences.Editor#commit()},
- * false to use {@link SharedPreferences.Editor#apply()}
- * @param spUtils The instance of {@link SPUtils}.
- */
- public static void clear(final boolean isCommit, @NonNull final SPUtils spUtils) {
- spUtils.clear(isCommit);
- }
- private static SPUtils getDefaultSPUtils() {
- return sDefaultSPUtils != null ? sDefaultSPUtils : SPUtils.getInstance();
- }
- }
|