Skip to content

Commit 278e546

Browse files
committed
Merge branch 'master' of github.com:OneSignal/OneSignal-Android-SDK
2 parents 7a245c9 + 68a7551 commit 278e546

28 files changed

+2312
-920
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package com.onesignal;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONException;
5+
import org.json.JSONObject;
6+
7+
import java.util.Iterator;
8+
import java.util.Set;
9+
10+
11+
class JSONUtils {
12+
// Returns a JSONObject of the differences between cur and changedTo.
13+
// If baseOutput is added changes will be applied to this JSONObject.
14+
// includeFields will always be added to the returned JSONObject if they are in cur.
15+
static JSONObject generateJsonDiff(JSONObject cur, JSONObject changedTo, JSONObject baseOutput, Set<String> includeFields) {
16+
if (cur == null)
17+
return null;
18+
if (changedTo == null)
19+
return baseOutput;
20+
21+
Iterator<String> keys = changedTo.keys();
22+
String key;
23+
Object value;
24+
25+
JSONObject output;
26+
if (baseOutput != null)
27+
output = baseOutput;
28+
else
29+
output = new JSONObject();
30+
31+
while (keys.hasNext()) {
32+
try {
33+
key = keys.next();
34+
value = changedTo.get(key);
35+
36+
if (cur.has(key)) {
37+
if (value instanceof JSONObject) {
38+
JSONObject curValue = cur.getJSONObject(key);
39+
JSONObject outValue = null;
40+
if (baseOutput != null && baseOutput.has(key))
41+
outValue = baseOutput.getJSONObject(key);
42+
JSONObject returnedJson = generateJsonDiff(curValue, (JSONObject) value, outValue, includeFields);
43+
String returnedJsonStr = returnedJson.toString();
44+
if (!returnedJsonStr.equals("{}"))
45+
output.put(key, new JSONObject(returnedJsonStr));
46+
}
47+
else if (value instanceof JSONArray)
48+
handleJsonArray(key, (JSONArray) value, cur.getJSONArray(key), output);
49+
else if (includeFields != null && includeFields.contains(key))
50+
output.put(key, value);
51+
else {
52+
Object curValue = cur.get(key);
53+
if (!value.equals(curValue)) {
54+
// Work around for JSON serializer turning doubles/floats into ints since it drops ending 0's
55+
if (curValue instanceof Integer && !"".equals(value)) {
56+
if ( ((Number)curValue).doubleValue() != ((Number)value).doubleValue())
57+
output.put(key, value);
58+
}
59+
else
60+
output.put(key, value);
61+
}
62+
}
63+
}
64+
else {
65+
if (value instanceof JSONObject)
66+
output.put(key, new JSONObject(value.toString()));
67+
else if (value instanceof JSONArray)
68+
handleJsonArray(key, (JSONArray) value, null, output);
69+
else
70+
output.put(key, value);
71+
}
72+
} catch (JSONException e) {
73+
e.printStackTrace();
74+
}
75+
}
76+
77+
return output;
78+
}
79+
80+
private static void handleJsonArray(String key, JSONArray newArray, JSONArray curArray, JSONObject output) throws JSONException {
81+
if (key.endsWith("_a") || key.endsWith("_d")) {
82+
output.put(key, newArray);
83+
return;
84+
}
85+
86+
String arrayStr = toStringNE(newArray);
87+
88+
JSONArray newOutArray = new JSONArray();
89+
JSONArray remOutArray = new JSONArray();
90+
String curArrayStr = curArray == null ? null : toStringNE(curArray);
91+
92+
for (int i = 0; i < newArray.length(); i++) {
93+
String arrayValue = (String)newArray.get(i);
94+
if (curArray == null || !curArrayStr.contains(arrayValue))
95+
newOutArray.put(arrayValue);
96+
}
97+
98+
if (curArray != null) {
99+
for (int i = 0; i < curArray.length(); i++) {
100+
String arrayValue = curArray.getString(i);
101+
if (!arrayStr.contains(arrayValue))
102+
remOutArray.put(arrayValue);
103+
}
104+
}
105+
106+
if (!newOutArray.toString().equals("[]"))
107+
output.put(key + "_a", newOutArray);
108+
if (!remOutArray.toString().equals("[]"))
109+
output.put(key + "_d", remOutArray);
110+
}
111+
112+
static String toStringNE(JSONArray jsonArray) {
113+
String strArray = "[";
114+
115+
try {
116+
for (int i = 0; i < jsonArray.length(); i++)
117+
strArray += "\"" + jsonArray.getString(i) + "\"";
118+
} catch (Throwable t) {}
119+
120+
return strArray + "]";
121+
}
122+
123+
static JSONObject getJSONObjectWithoutBlankValues(JSONObject jsonObject, String getKey) {
124+
if (!jsonObject.has(getKey))
125+
return null;
126+
127+
JSONObject toReturn = new JSONObject();
128+
129+
JSONObject keyValues = jsonObject.optJSONObject(getKey);
130+
131+
Iterator<String> keys = keyValues.keys();
132+
String key;
133+
Object value;
134+
135+
while (keys.hasNext()) {
136+
key = keys.next();
137+
try {
138+
value = keyValues.get(key);
139+
if (!"".equals(value))
140+
toReturn.put(key, value);
141+
} catch (Throwable t) {}
142+
}
143+
144+
return toReturn;
145+
}
146+
147+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2018 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
package com.onesignal;
29+
30+
class OSEmailSubscriptionChangedInternalObserver {
31+
void changed(OSEmailSubscriptionState state) {
32+
fireChangesToPublicObserver(state);
33+
}
34+
35+
// Handles firing a public facing EmailSubscriptionStateChangesObserver
36+
// 1. Generates a OSEmailSubscriptionStateChanges object and sets to and from states
37+
// 2. Persists acknowledgement
38+
// - Prevents duplicated events
39+
// - Notifies if changes were made outside of the app
40+
static void fireChangesToPublicObserver(OSEmailSubscriptionState state) {
41+
OSEmailSubscriptionStateChanges stateChanges = new OSEmailSubscriptionStateChanges();
42+
stateChanges.from = OneSignal.lastEmailSubscriptionState;
43+
stateChanges.to = (OSEmailSubscriptionState)state.clone();
44+
45+
boolean hasReceiver = OneSignal.getEmailSubscriptionStateChangesObserver().notifyChange(stateChanges);
46+
if (hasReceiver) {
47+
OneSignal.lastEmailSubscriptionState = (OSEmailSubscriptionState)state.clone();
48+
OneSignal.lastEmailSubscriptionState.persistAsFrom();
49+
}
50+
}
51+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2018 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
package com.onesignal;
29+
30+
public interface OSEmailSubscriptionObserver {
31+
void onOSEmailSubscriptionChanged(OSEmailSubscriptionStateChanges stateChanges);
32+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2018 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
package com.onesignal;
29+
30+
import android.support.annotation.NonNull;
31+
import android.support.annotation.Nullable;
32+
33+
import org.json.JSONObject;
34+
35+
public class OSEmailSubscriptionState implements Cloneable {
36+
37+
OSObservable<Object, OSEmailSubscriptionState> observable;
38+
39+
OSEmailSubscriptionState(boolean asFrom) {
40+
observable = new OSObservable<>("changed", false);
41+
42+
if (asFrom) {
43+
emailUserId = OneSignalPrefs.getString(OneSignalPrefs.PREFS_ONESIGNAL,
44+
OneSignalPrefs.PREFS_ONESIGNAL_EMAIL_ID_LAST, null);
45+
emailAddress = OneSignalPrefs.getString(OneSignalPrefs.PREFS_ONESIGNAL,
46+
OneSignalPrefs.PREFS_ONESIGNAL_EMAIL_ADDRESS_LAST, null);
47+
}
48+
}
49+
50+
private String emailUserId;
51+
private String emailAddress;
52+
53+
void clearEmailAndId() {
54+
boolean changed = emailUserId != null || emailAddress != null;
55+
emailUserId = null;
56+
emailAddress = null;
57+
if (changed)
58+
observable.notifyChange(this);
59+
}
60+
61+
void setEmailUserId(@NonNull String id) {
62+
boolean changed = !id.equals(emailUserId);
63+
emailUserId = id;
64+
if (changed)
65+
observable.notifyChange(this);
66+
}
67+
68+
public String getEmailUserId() {
69+
return emailUserId;
70+
}
71+
72+
void setEmailAddress(@NonNull String email) {
73+
boolean changed = !email.equals(emailAddress);
74+
emailAddress = email;
75+
if (changed)
76+
observable.notifyChange(this);
77+
}
78+
79+
public String getEmailAddress() {
80+
return emailAddress;
81+
}
82+
83+
public boolean getSubscribed() {
84+
return emailUserId != null && emailAddress != null;
85+
}
86+
87+
void persistAsFrom() {
88+
OneSignalPrefs.saveString(OneSignalPrefs.PREFS_ONESIGNAL,
89+
OneSignalPrefs.PREFS_ONESIGNAL_EMAIL_ID_LAST, emailUserId);
90+
OneSignalPrefs.saveString(OneSignalPrefs.PREFS_ONESIGNAL,
91+
OneSignalPrefs.PREFS_ONESIGNAL_EMAIL_ADDRESS_LAST, emailAddress);
92+
}
93+
94+
boolean compare(OSEmailSubscriptionState from) {
95+
return !(emailUserId != null ? emailUserId : "").equals(from.emailUserId != null ? from.emailUserId : "")
96+
|| !(emailAddress != null ? emailAddress : "").equals(from.emailAddress != null ? from.emailAddress : "");
97+
}
98+
99+
protected Object clone() {
100+
try {
101+
return super.clone();
102+
} catch (Throwable t) {}
103+
return null;
104+
}
105+
106+
public JSONObject toJSONObject() {
107+
JSONObject mainObj = new JSONObject();
108+
109+
try {
110+
if (emailUserId != null)
111+
mainObj.put("emailUserId", emailUserId);
112+
else
113+
mainObj.put("emailUserId", JSONObject.NULL);
114+
115+
if (emailAddress != null)
116+
mainObj.put("emailAddress", emailAddress);
117+
else
118+
mainObj.put("emailAddress", JSONObject.NULL);
119+
120+
mainObj.put("subscribed", getSubscribed());
121+
}
122+
catch(Throwable t) {
123+
t.printStackTrace();
124+
}
125+
126+
return mainObj;
127+
}
128+
129+
@Override
130+
public String toString() {
131+
return toJSONObject().toString();
132+
}
133+
}

0 commit comments

Comments
 (0)