Skip to content

Commit 5ad3a89

Browse files
committed
Fix NPE under UserState getSyncValuesCopy
* If UserState data is inited by load state and JSON parsing ends failing syncValues might end null * Always init sync values
1 parent e30767c commit 5ad3a89

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.onesignal;
22

3+
import androidx.annotation.NonNull;
4+
35
import org.json.JSONException;
46
import org.json.JSONObject;
57

@@ -92,7 +94,7 @@ public JSONObject getSyncValuesCopy() throws JSONException {
9294
}
9395
}
9496

95-
public void setSyncValues(JSONObject syncValues) {
97+
public void setSyncValues(@NonNull JSONObject syncValues) {
9698
synchronized (LOCK) {
9799
this.syncValues = syncValues;
98100
}
@@ -297,21 +299,20 @@ private void loadState() {
297299

298300
String syncValuesStr = OneSignalPrefs.getString(OneSignalPrefs.PREFS_ONESIGNAL,
299301
OneSignalPrefs.PREFS_ONESIGNAL_USERSTATE_SYNCVALYES_ + persistKey,null);
302+
303+
JSONObject syncValues = new JSONObject();
300304
try {
301-
JSONObject syncValues;
302305
if (syncValuesStr == null) {
303-
syncValues = new JSONObject();
304-
String gtRegistrationId = OneSignalPrefs.getString(OneSignalPrefs.PREFS_ONESIGNAL,
306+
String registrationId = OneSignalPrefs.getString(OneSignalPrefs.PREFS_ONESIGNAL,
305307
OneSignalPrefs.PREFS_GT_REGISTRATION_ID,null);
306-
syncValues.put("identifier", gtRegistrationId);
308+
syncValues.put("identifier", registrationId);
307309
} else {
308310
syncValues = new JSONObject(syncValuesStr);
309311
}
310-
311-
setSyncValues(syncValues);
312312
} catch (JSONException e) {
313313
e.printStackTrace();
314314
}
315+
setSyncValues(syncValues);
315316
}
316317

317318
void persistState() {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.onesignal;
22

33
class UserStateEmail extends UserState {
4+
private static final String EMAIL = "email";
5+
46
UserStateEmail(String inPersistKey, boolean load) {
5-
super("email" + inPersistKey, load);
7+
super(EMAIL + inPersistKey, load);
68
}
79

810
@Override

0 commit comments

Comments
 (0)