package dolphin.android.preference;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by 97011424 on 2013/8/14.
 */
public class PreferenceUtils {

    private Context mContext;
    private SharedPreferences mSharedPreferences;

    public PreferenceUtils(Context context) {
        mContext = context;
        mSharedPreferences = getDefaultSharedPreferences(mContext);
    }

    public SharedPreferences getSharedPreferences() {
        return mSharedPreferences;
    }

    public SharedPreferences.Editor edit() {
        return mSharedPreferences.edit();
    }

    public void commit() {

    }

    public Set<String> getStringSet(String key) {
        return getStringSet(key, null);
    }

    public Set<String> getStringSet(String key, Set<String> defaultValue) {
        //Android setSringSet() / getStringSet() with compatibility
        //https://gist.github.com/shreeshga/5398506
        Set<String> set = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            set = mSharedPreferences.getStringSet(key, null);
        } else {
            String s = mSharedPreferences.getString(key, null);
            if (s != null)
                set = new HashSet<String>(Arrays.asList(s.split(",")));
        }
        return set;
    }

    public void putStringSet(String key, Set<String> set) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        //Android setSringSet() / getStringSet() with compatibility
        //https://gist.github.com/shreeshga/5398506
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            editor.putStringSet(key, set);
        } else {
            editor.putString(key, join(set, ","));
        }
        editor.commit();
    }

    private static String join(Set<String> set, String delim) {
        StringBuilder sb = new StringBuilder();
        String loopDelim = "";

        for (String s : set) {
            sb.append(loopDelim);
            sb.append(s);

            loopDelim = delim;
        }

        return sb.toString();
    }

    public String getString(String key) {
        return getString(key, null);
    }

    public String getString(String key, String defaultValue) {
        return mSharedPreferences.getString(key, defaultValue);
    }

    public void putString(String key, String value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public void remove(String key) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.remove(key);
        editor.commit();
    }

    public static SharedPreferences getDefaultSharedPreferences(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public int getInt(String key) {
        return getInt(key, 0);
    }

    public int getInt(String key, int defaultValue) {
        return mSharedPreferences.getInt(key, defaultValue);
    }

    public void putInt(String key, int value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }
}

@@ -3477,12 +3477,450 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    public boolean getBoolean(String key) %7B%0D%0A        return getBoolean(key, false);%0D%0A    %7D%0D%0A%0D%0A    public boolean getBoolean(String key, boolean defaultValue) %7B%0D%0A        return mSharedPreferences.getBoolean(key, defaultValue);%0D%0A    %7D%0D%0A%0D%0A    public void putBoolean(String key, boolean value) %7B%0D%0A        SharedPreferences.Editor editor = mSharedPreferences.edit();%0D%0A        editor.putBoolean(key, value);%0D%0A        editor.commit();%0D%0A    %7D%0D
 %0A%7D%0D%0A

add boolean support