Skip to content

Commit 9746135

Browse files
committed
Revert "Fix Null Pointer Concurrency Issue"
This reverts commit 69f99c9.
1 parent cc67b06 commit 9746135

File tree

1 file changed

+33
-40
lines changed

1 file changed

+33
-40
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignalPrefs.java

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ class OneSignalPrefs {
7575
static HashMap<String, HashMap<String, Object>> prefsToApply;
7676
public static WritePrefHandlerThread prefsHandler;
7777

78-
static final Object synchronizer = new Object();
79-
8078
static {
8179
initializePool();
8280
}
@@ -115,11 +113,11 @@ public void run() {
115113
}
116114

117115
private void flushBufferToDisk() {
118-
synchronized(synchronizer) {
119-
for (String pref : prefsToApply.keySet()) {
120-
SharedPreferences prefsToWrite = getSharedPrefsByName(pref);
121-
SharedPreferences.Editor editor = prefsToWrite.edit();
122-
HashMap<String, Object> prefHash = prefsToApply.get(pref);
116+
for (String pref : prefsToApply.keySet()) {
117+
SharedPreferences prefsToWrite = getSharedPrefsByName(pref);
118+
SharedPreferences.Editor editor = prefsToWrite.edit();
119+
HashMap<String, Object> prefHash = prefsToApply.get(pref);
120+
synchronized (prefHash) {
123121
for (String key : prefHash.keySet()) {
124122
Object value = prefHash.get(key);
125123
if (value instanceof String)
@@ -132,23 +130,20 @@ else if (value instanceof Long)
132130
editor.putLong(key, (Long)value);
133131
}
134132
prefHash.clear();
135-
136-
editor.apply();
137133
}
138-
139-
lastSyncTime = System.currentTimeMillis();
134+
editor.apply();
140135
}
136+
137+
lastSyncTime = System.currentTimeMillis();
141138
}
142139
}
143140

144141
public static void initializePool() {
145-
synchronized(synchronizer) {
146-
prefsToApply = new HashMap<>();
147-
prefsToApply.put(PREFS_ONESIGNAL, new HashMap<String, Object>());
148-
prefsToApply.put(PREFS_PLAYER_PURCHASES, new HashMap<String, Object>());
142+
prefsToApply = new HashMap<>();
143+
prefsToApply.put(PREFS_ONESIGNAL, new HashMap<String, Object>());
144+
prefsToApply.put(PREFS_PLAYER_PURCHASES, new HashMap<String, Object>());
149145

150-
prefsHandler = new WritePrefHandlerThread();
151-
}
146+
prefsHandler = new WritePrefHandlerThread();
152147
}
153148

154149
static void saveString(final String prefsName, final String key, final String value) {
@@ -168,12 +163,11 @@ static void saveLong(String prefsName, String key, long value) {
168163
}
169164

170165
static private void save(String prefsName, String key, Object value) {
171-
synchronized(synchronizer) {
172-
HashMap<String, Object> pref = prefsToApply.get(prefsName);
166+
HashMap<String, Object> pref = prefsToApply.get(prefsName);
167+
synchronized (pref) {
173168
pref.put(key, value);
174-
175-
prefsHandler.startDelayedWrite();
176169
}
170+
prefsHandler.startDelayedWrite();
177171
}
178172

179173
static String getString(String prefsName, String key, String defValue) {
@@ -194,35 +188,34 @@ static long getLong(String prefsName, String key, long defValue) {
194188

195189
// If type == Object then this is a contains check
196190
private static Object get(String prefsName, String key, Class type, Object defValue) {
197-
synchronized(synchronizer) {
198-
HashMap<String, Object> pref = prefsToApply.get(prefsName);
191+
HashMap<String, Object> pref = prefsToApply.get(prefsName);
199192

193+
synchronized (pref) {
200194
if (type.equals(Object.class) && pref.containsKey(key))
201195
return true;
202196

203197
Object cachedValue = pref.get(key);
204198
if (cachedValue != null || pref.containsKey(key))
205199
return cachedValue;
200+
}
206201

202+
SharedPreferences prefs = getSharedPrefsByName(prefsName);
203+
if (prefs != null ) {
204+
if (type.equals(String.class))
205+
return prefs.getString(key, (String)defValue);
206+
else if (type.equals(Boolean.class))
207+
return prefs.getBoolean(key, (Boolean)defValue);
208+
else if (type.equals(Integer.class))
209+
return prefs.getInt(key, (Integer)defValue);
210+
else if (type.equals(Long.class))
211+
return prefs.getLong(key, (Long)defValue);
212+
else if (type.equals(Object.class))
213+
return prefs.contains(key);
207214

208-
SharedPreferences prefs = getSharedPrefsByName(prefsName);
209-
if (prefs != null ) {
210-
if (type.equals(String.class))
211-
return prefs.getString(key, (String)defValue);
212-
else if (type.equals(Boolean.class))
213-
return prefs.getBoolean(key, (Boolean)defValue);
214-
else if (type.equals(Integer.class))
215-
return prefs.getInt(key, (Integer)defValue);
216-
else if (type.equals(Long.class))
217-
return prefs.getLong(key, (Long)defValue);
218-
else if (type.equals(Object.class))
219-
return prefs.contains(key);
220-
221-
return null;
222-
}
223-
224-
return defValue;
215+
return null;
225216
}
217+
218+
return defValue;
226219
}
227220

228221
private static synchronized SharedPreferences getSharedPrefsByName(String prefsName) {

0 commit comments

Comments
 (0)