package com.dl7.mvp.utils; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import com.dl7.mvp.module.manage.setting.SettingsFragment; /** * PreferencesUtils, easy to get or put data * <ul> * <strong>Get Value</strong> * <li>get string {@link #getString(Context, String)}, {@link #getString(Context, String, String)}</li> * <li>get int {@link #getInt(Context, String)}, {@link #getInt(Context, String, int)}</li> * <li>get long {@link #getLong(Context, String)}, {@link #getLong(Context, String, long)}</li> * <li>get float {@link #getFloat(Context, String)}, {@link #getFloat(Context, String, float)}</li> * <li>get boolean {@link #getBoolean(Context, String)}, {@link #getBoolean(Context, String, boolean)}</li> * </ul> * * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-3-6 */ public class PreferencesUtils { private PreferencesUtils() { throw new AssertionError(); } /** * 获取保存路径 * @param context * @return */ public static String getSavePath(Context context) { return getString(context, SettingsFragment.SAVE_PATH_KEY, SettingsFragment.DEFAULT_SAVE_PATH); } /** * 是否非Wifi下不加载图片 * @param context * @return */ public static boolean isShowImageAlways(Context context) { return getBoolean(context, SettingsFragment.NO_IMAGE_KEY, false); } /** * get string preferences * * @param context * @param key The name of the preference to retrieve * @return The preference value if it exists, or null. Throws ClassCastException if there is a preference with this * name that is not a string * @see #getString(Context, String, String) */ public static String getString(Context context, String key) { return getString(context, key, null); } /** * get string preferences * * @param context * @param key The name of the preference to retrieve * @param defaultValue Value to return if this preference does not exist * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with * this name that is not a string */ public static String getString(Context context, String key, String defaultValue) { return PreferenceManager.getDefaultSharedPreferences(context).getString(key, defaultValue); } /** * get int preferences * * @param context * @param key The name of the preference to retrieve * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this * name that is not a int * @see #getInt(Context, String, int) */ public static int getInt(Context context, String key) { return getInt(context, key, -1); } /** * get int preferences * * @param context * @param key The name of the preference to retrieve * @param defaultValue Value to return if this preference does not exist * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with * this name that is not a int */ public static int getInt(Context context, String key, int defaultValue) { return PreferenceManager.getDefaultSharedPreferences(context).getInt(key, defaultValue); } /** * get long preferences * * @param context * @param key The name of the preference to retrieve * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this * name that is not a long * @see #getLong(Context, String, long) */ public static long getLong(Context context, String key) { return getLong(context, key, -1); } /** * get long preferences * * @param context * @param key The name of the preference to retrieve * @param defaultValue Value to return if this preference does not exist * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with * this name that is not a long */ public static long getLong(Context context, String key, long defaultValue) { return PreferenceManager.getDefaultSharedPreferences(context).getLong(key, defaultValue); } /** * get float preferences * * @param context * @param key The name of the preference to retrieve * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this * name that is not a float * @see #getFloat(Context, String, float) */ public static float getFloat(Context context, String key) { return getFloat(context, key, -1); } /** * get float preferences * * @param context * @param key The name of the preference to retrieve * @param defaultValue Value to return if this preference does not exist * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with * this name that is not a float */ public static float getFloat(Context context, String key, float defaultValue) { return PreferenceManager.getDefaultSharedPreferences(context).getFloat(key, defaultValue); } /** * put boolean preferences * * @param context * @param key The name of the preference to modify * @param value The new value for the preference * @return True if the new values were successfully written to persistent storage. */ public static boolean putBoolean(Context context, String key, boolean value) { // SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); // SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); editor.putBoolean(key, value); return editor.commit(); } /** * get boolean preferences, default is false * * @param context * @param key The name of the preference to retrieve * @return The preference value if it exists, or false. Throws ClassCastException if there is a preference with this * name that is not a boolean * @see #getBoolean(Context, String, boolean) */ public static boolean getBoolean(Context context, String key) { return getBoolean(context, key, false); } /** * get boolean preferences * * @param context * @param key The name of the preference to retrieve * @param defaultValue Value to return if this preference does not exist * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with * this name that is not a boolean */ public static boolean getBoolean(Context context, String key, boolean defaultValue) { return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(key, defaultValue); } }