aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/lacolaco/smileessence/preference/Pref.java
blob: 3b37f149fd6cf06aa08131a9962557ab29de89ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package net.lacolaco.smileessence.preference;

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

import java.util.Set;

public class Pref
{

    private Context context;

    public Pref(Context context)
    {
        this.context = context;
    }

    private SharedPreferences getPref()
    {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    private String getString(int resID)
    {
        try
        {
            return context.getString(resID);
        }
        catch(Exception e)
        {
            return null;
        }
    }

    public boolean getValue(int keyID, boolean defaultValue)
    {
        return getString(keyID) != null ? getPref().getBoolean(getString(keyID), defaultValue) : defaultValue;
    }

    public int getValue(int keyID, int defaultValue)
    {
        return getString(keyID) != null ? getPref().getInt(getString(keyID), defaultValue) : defaultValue;
    }

    public float getValue(int keyID, float defaultValue)
    {
        return getString(keyID) != null ? getPref().getFloat(getString(keyID), defaultValue) : defaultValue;
    }

    public long getValue(int keyID, long defaultValue)
    {
        return getString(keyID) != null ? getPref().getLong(getString(keyID), defaultValue) : defaultValue;
    }

    public String getValue(int keyID, String defaultValue)
    {
        return getString(keyID) != null ? getPref().getString(getString(keyID), defaultValue) : defaultValue;
    }

    public Set<String> getValue(int keyID, Set<String> defaultValue)
    {
        return getString(keyID) != null ? getPref().getStringSet(getString(keyID), defaultValue) : defaultValue;
    }

    public boolean putValue(int keyID, boolean value)
    {
        if(getString(keyID) == null)
        {
            return false;
        }
        SharedPreferences.Editor editor = getPref().edit();
        editor.putBoolean(getString(keyID), value);
        return editor.commit();
    }

    public boolean putValue(int keyID, int value)
    {
        if(getString(keyID) == null)
        {
            return false;
        }
        SharedPreferences.Editor editor = getPref().edit();
        editor.putInt(getString(keyID), value);
        return editor.commit();
    }

    public boolean putValue(int keyID, float value)
    {
        if(getString(keyID) == null)
        {
            return false;
        }
        SharedPreferences.Editor editor = getPref().edit();
        editor.putFloat(getString(keyID), value);
        return editor.commit();
    }

    public boolean putValue(int keyID, long value)
    {
        if(getString(keyID) == null)
        {
            return false;
        }
        SharedPreferences.Editor editor = getPref().edit();
        editor.putLong(getString(keyID), value);
        return editor.commit();
    }

    public boolean putValue(int keyID, String value)
    {
        if(getString(keyID) == null)
        {
            return false;
        }
        SharedPreferences.Editor editor = getPref().edit();
        editor.putString(getString(keyID), value);
        return editor.commit();
    }

    public boolean putValue(int keyID, Set<String> value)
    {
        if(getString(keyID) == null)
        {
            return false;
        }
        SharedPreferences.Editor editor = getPref().edit();
        editor.putStringSet(getString(keyID), value);
        return editor.commit();
    }
}