/*
 * Copyright 2013 wada811<at.wada811@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.wada811.utils;

import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * {@link PreferenceUtils} is wrapper class of {@link SharedPreferences}.
 * {@link PreferenceManager#createPreferenceScreen(Context)} should pass application context.
 * 
 * @author wada811
 * 
 */
public class PreferenceUtils {

    /**
     * {@link PreferenceManager#getDefaultSharedPreferences(Context)} should pass application
     * context.
     * 
     * @param context
     * @return
     */
    public static SharedPreferences getDefaultSharedPreferences(Context context){
        return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
    }

    /**
     * All SharedPreference to String
     * 
     * @param context
     * @return
     */
    public static String toString(Context context){
        return PreferenceUtils.getDefaultSharedPreferences(context).getAll().toString();
    }

    /**
     * All SharedPreference to JSON String
     * 
     * @param context
     * @return jsonString|null
     */
    public static String toJsonString(Context context){
        SharedPreferences preferences = PreferenceUtils.getDefaultSharedPreferences(context);
        JSONObject jsonObject = new JSONObject();
        Map<String, ?> map = preferences.getAll();
        try{
            for(Entry<String, ?> entry : map.entrySet()){
                jsonObject.put(entry.getKey(), entry.getValue());
            }
        }catch(JSONException e){
            e.printStackTrace();
            return null;
        }
        return jsonObject.toString();
    }

    /**
     * keyを持っているかを返す
     * 
     * @param context
     * @param key
     * @return
     */
    public static boolean contains(Context context, String key){
        return PreferenceUtils.getDefaultSharedPreferences(context).contains(key);
    }

    /**
     * SharedPreferences に保存する
     * 
     * @param context
     * @param key
     * @param value
     */
    public static void putString(Context context, String key, String value){
        PreferenceUtils.getDefaultSharedPreferences(context).edit().putString(key, value).commit();
    }

    /**
     * SharedPreferences から値を取得する
     * 
     * @param context
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getString(Context context, String key, String defaultValue){
        return PreferenceUtils.getDefaultSharedPreferences(context).getString(key, defaultValue);
    }

    /**
     * SharedPreferences に保存する
     * 
     * @param context
     * @param key
     * @param value
     */
    public static void putInt(Context context, String key, int value){
        PreferenceUtils.getDefaultSharedPreferences(context).edit().putInt(key, value).commit();
    }

    /**
     * SharedPreferences から値を取得する
     * 
     * @param context
     * @param key
     * @param defaultValue
     * @return
     */
    public static int getInt(Context context, String key, int defaultValue){
        return PreferenceUtils.getDefaultSharedPreferences(context).getInt(key, defaultValue);
    }

    /**
     * SharedPreferences に保存する
     * 
     * @param context
     * @param key
     * @param value
     */
    public static void putBoolean(Context context, String key, Boolean value){
        PreferenceUtils.getDefaultSharedPreferences(context).edit().putBoolean(key, value).commit();
    }

    /**
     * SharedPreferences から値を取得する
     * 
     * @param context
     * @param key
     * @param defaultValue
     * @return
     */
    public static boolean getBoolean(Context context, String key, Boolean defaultValue){
        return PreferenceUtils.getDefaultSharedPreferences(context).getBoolean(key, defaultValue);
    }

    /**
     * SharedPreferences に保存する
     * 
     * @param context
     * @param key
     * @param value
     */
    public static void putFloat(Context context, String key, float value){
        PreferenceUtils.getDefaultSharedPreferences(context).edit().putFloat(key, value).commit();
    }

    /**
     * SharedPreferences から値を取得する
     * 
     * @param context
     * @param key
     * @param defaultValue
     * @return
     */
    public static float getFloat(Context context, String key, float defaultValue){
        return PreferenceUtils.getDefaultSharedPreferences(context).getFloat(key, defaultValue);
    }

}

@@ -661,233 +661,233 @@
 ort 
-java.util.Map;%0D%0Aimport java.util.Map.Entry;%0D%0Aimport org.json.JSONException;%0D%0Aimport org.json.JSONObject;%0D%0Aimport android.content.Context;%0D%0Aimport android.content.SharedPreferences;%0D%0Aimport android.preference.PreferenceManager
+android.content.Context;%0D%0Aimport android.content.SharedPreferences;%0D%0Aimport android.preference.PreferenceManager;%0D%0Aimport org.json.JSONException;%0D%0Aimport org.json.JSONObject;%0D%0Aimport java.util.Map;%0D%0Aimport java.util.Map.Entry
 ;%0D%0A%0D
@@ -5300,11 +5300,650 @@
    %7D%0D%0A%0D%0A
+    /**%0D%0A     * SharedPreferences %E3%81%AB%E4%BF%9D%E5%AD%98%E3%81%99%E3%82%8B%0D%0A     * %0D%0A     * @param context%0D%0A     * @param key%0D%0A     * @param value%0D%0A     */%0D%0A    public static void putLong(Context context, String key, long value)%7B%0D%0A        PreferenceUtils.getDefaultSharedPreferences(context).edit().putLong(key, value).commit();%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * SharedPreferences %E3%81%8B%E3%82%89%E5%80%A4%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%0D%0A     * %0D%0A     * @param context%0D%0A     * @param key%0D%0A     * @param defaultValue%0D%0A     * @return%0D%0A     */%0D%0A    public static long getLong(Context context, String key, long defaultValue)%7B%0D%0A        return PreferenceUtils.getDefaultSharedPreferences(context).getLong(key, defaultValue);%0D%0A    %7D%0D%0A%0D%0A
 %7D%0D%0A

add put/get long method