package com.xixun.communicate;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/*
 *
 */
public class Configurations {

    public static String strConfigFileName ="DisplayAppConfig";
    public static Configurations config;
    private SharedPreferences sharedPreferences=null;

    public static Configurations getConfigurations(Context ctx){
        if(config == null){
            config = new Configurations(ctx);
        }
        return config;
    }

    @SuppressLint("WorldReadableFiles")
    @SuppressWarnings("deprecation")
    private Configurations(Context ctx){
        sharedPreferences = ctx.getSharedPreferences(strConfigFileName,
                Context.MODE_PRIVATE | Context.MODE_PRIVATE);
    }

    public void setValue(String id, String val){
        if (sharedPreferences==null) return;

        Editor editor = sharedPreferences.edit();
        editor.putString(id, val);
        editor.commit();
        //Log.i("Configuration", "set value " + id + " :" + val);
    }
    public String getValue(String id){
        if (sharedPreferences==null) return null;
        return sharedPreferences.getString(id, "");
    }

    public void setIntValue(String id, int val){
        if (sharedPreferences==null) return;
        Editor editor = sharedPreferences.edit();
        editor.putInt(id, val);
        editor.commit();
    }
    public int getIntValue(String id){
        if (sharedPreferences==null) return -1;
        return sharedPreferences.getInt(id, 0);
    }

    public void setBooleanValue(String id, boolean val){
        if (sharedPreferences==null) return;
        Editor editor = sharedPreferences.edit();
        editor.putInt(id, val?1:0);
        editor.commit();
    }
    public boolean getBooleanValue(String id){
        if (sharedPreferences==null) return false;
        return (sharedPreferences.getInt(id, 0)==1);
    }
}
