package com.aincc.lib.util; import java.util.Set; import android.annotation.TargetApi; import android.content.Context; import android.content.SharedPreferences; /** * * <h3><b>CPreferencesUtil</b></h3></br> * * @author aincc@barusoft.com * @version 1.0.0 * @since 1.0.0 */ public class PreferencesUtil { /** * Preference * * @param context * @param filename * @return */ private static SharedPreferences getPreferences(Context context, String filename) { if (context == null || filename == null) return null; SharedPreferences result = null; try { result = context.getApplicationContext().getSharedPreferences(filename, Context.MODE_PRIVATE); } catch (Exception e) { e.printStackTrace(); } return result; } /** * Preference editor * * @param context * @param filename * @return */ private static SharedPreferences.Editor getPreferencesEditor(Context context, String filename) { if (context == null || filename == null) return null; SharedPreferences result = null; try { result = getPreferences(context, filename); } catch (Exception e) { e.printStackTrace(); } if (result == null) return null; return result.edit(); } /** * * @param context * @param filename * @param key * @return */ public static String getString(Context context, String filename, String key) { if (context == null || filename == null || key == null) return ""; String result = ""; try { SharedPreferences pref = getPreferences(context, filename); result = pref.getString(key, ""); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @param defValue * @return */ public static String getString(Context context, String filename, String key, String defValue) { if (context == null || filename == null || key == null) return ""; String result = ""; try { SharedPreferences pref = context.getApplicationContext().getSharedPreferences(filename, Context.MODE_PRIVATE); result = pref.getString(key, defValue); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @param value * @return */ public static boolean setString(Context context, String filename, String key, String value) { if (context == null || filename == null || key == null) return false; boolean result = false; try { SharedPreferences.Editor editor = getPreferencesEditor(context, filename); editor.putString(key, value); result = editor.commit(); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @return 데이터가 존재하면 Set<String>을 반환하고, 없으면 null 을 반환한다. */ @TargetApi(11) public static Set<String> getStringSet(Context context, String filename, String key) { if (context == null || filename == null || key == null) return null; Set<String> result = null; try { SharedPreferences pref = getPreferences(context, filename); result = pref.getStringSet(key, null); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @param defValue * @return 데이터가 존재하면 Set<String>을 반환하고, 없으면 null 을 반환한다. */ @TargetApi(11) public static Set<String> getStringSet(Context context, String filename, String key, Set<String> def) { if (context == null || filename == null || key == null) return null; Set<String> result = null; try { SharedPreferences pref = context.getApplicationContext().getSharedPreferences(filename, Context.MODE_PRIVATE); result = pref.getStringSet(key, def); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @param value * @return */ @TargetApi(11) public static boolean setStringSet(Context context, String filename, String key, Set<String> value) { if (context == null || filename == null || key == null) return false; boolean result = false; try { SharedPreferences.Editor editor = getPreferencesEditor(context, filename); editor.putStringSet(key, value); result = editor.commit(); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @return */ public static int getInt(Context context, String filename, String key) { if (context == null || filename == null || key == null) return 0; int result = 0; try { SharedPreferences pref = getPreferences(context, filename); result = pref.getInt(key, 0); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @param defValue * @return */ public static int getInt(Context context, String filename, String key, int defValue) { if (context == null || filename == null || key == null) return 0; int result = 0; try { SharedPreferences pref = getPreferences(context, filename); result = pref.getInt(key, defValue); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @param value * @return */ public static boolean setInt(Context context, String filename, String key, int value) { if (context == null || filename == null || key == null) return false; boolean result = false; try { SharedPreferences.Editor editor = getPreferencesEditor(context, filename); editor.putInt(key, value); result = editor.commit(); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @return */ public static long getLong(Context context, String filename, String key) { if (context == null || filename == null || key == null) return 0; long result = 0; try { SharedPreferences pref = getPreferences(context, filename); result = pref.getLong(key, 0); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @param defValue * @return */ public static long getLong(Context context, String filename, String key, long defValue) { if (context == null || filename == null || key == null) return 0; long result = 0; try { SharedPreferences pref = getPreferences(context, filename); result = pref.getLong(key, defValue); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @param value * @return */ public static boolean setLong(Context context, String filename, String key, long value) { if (context == null || filename == null || key == null) return false; boolean result = false; try { SharedPreferences.Editor editor = getPreferencesEditor(context, filename); editor.putLong(key, value); result = editor.commit(); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @return */ public static boolean getBoolean(Context context, String filename, String key) { if (context == null || filename == null || key == null) return false; boolean result = false; try { SharedPreferences pref = getPreferences(context, filename); result = pref.getBoolean(key, false); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @param defValue * @return */ public static boolean getBoolean(Context context, String filename, String key, boolean defValue) { if (context == null || filename == null || key == null) return false; boolean result = false; try { SharedPreferences pref = getPreferences(context, filename); result = pref.getBoolean(key, defValue); } catch (Exception e) { e.printStackTrace(); } return result; } /** * * @param context * @param filename * @param key * @param value * @return */ public static boolean setBoolean(Context context, String filename, String key, boolean value) { if (context == null || filename == null || key == null) return false; boolean result = false; try { SharedPreferences.Editor editor = getPreferencesEditor(context, filename); editor.putBoolean(key, value); result = editor.commit(); } catch (Exception e) { e.printStackTrace(); } return result; } }