-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prefs.java
145 lines (112 loc) · 3.51 KB
/
Prefs.java
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* @Author: Sumeet Gehi
*********************
* Copyright (c) 2013
* All Rights Reserved.
*
**********************/
package com.sumit.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.text.TextUtils;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class Prefs {
private SharedPreferences preferences;
public static final String PREFS_NAME = "mySharedPrefs";
/**
* initialize default SharedPreferences
* */
public Prefs(Context context) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
}
/**
* @name SharedPreferences name
* */
public Prefs(Context context, String name) {
preferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
}
public SharedPreferences getPreferences() {
if (preferences == null)
throw new RuntimeException("Opps you have forget to intialize Prefs class.. Prefs prefs = new Prefs(context)");
return preferences;
}
public SharedPreferences.Editor getEditor() {
return getPreferences().edit();
}
public int get(String key, int defaultValue) {
return getPreferences().getInt(key, defaultValue);
}
public long get(String key, long defaultValue) {
return getPreferences().getLong(key, defaultValue);
}
public float get(String key, float defaultValue) {
return getPreferences().getFloat(key, defaultValue);
}
public double get(String key, double defaultValue) {
String number = get(key);
try {
return Double.parseDouble(number);
} catch (NumberFormatException e) {
return defaultValue;
}
}
public String get(String key) {
return getPreferences().getString(key, "");
}
public ArrayList<String> getList(String key) {
return new ArrayList<String>(Arrays.asList(TextUtils.split(
getPreferences().getString(key, ""), "‚‗‚")));
}
public boolean get(String key, boolean defaultValue) {
return getPreferences().getBoolean(key, defaultValue);
}
public void set(String key, int value) {
getEditor().putInt(key, value).apply();
}
public void set(String key, long value) {
getEditor().putLong(key, value).apply();
}
public void set(String key, float value) {
getEditor().putFloat(key, value).apply();
}
public void set(String key, double value) {
set(key, String.valueOf(value));
}
public void set(String key, String value) {
getEditor().putString(key, value).apply();
}
public void setList(String key, ArrayList<String> stringList) {
String[] myStringList = stringList
.toArray(new String[stringList.size()]);
getEditor().putString(key, TextUtils.join("‚‗‚", myStringList)).apply();
}
public void set(String key, boolean value) {
getEditor().putBoolean(key, value).apply();
}
public boolean contains(String key) {
return getPreferences().contains(key);
}
public void remove(String key) {
getEditor().remove(key).apply();
}
public void clear() {
getEditor().clear().apply();
}
public Map<String, ?> getAll() {
return getPreferences().getAll();
}
public void registerOnSharedPreferenceChangeListener(
SharedPreferences.OnSharedPreferenceChangeListener listener) {
getPreferences().registerOnSharedPreferenceChangeListener(listener);
}
public void unregisterOnSharedPreferenceChangeListener(
SharedPreferences.OnSharedPreferenceChangeListener listener) {
getPreferences().unregisterOnSharedPreferenceChangeListener(listener);
}
}