Skip to content

Commit 61c7d24

Browse files
committed
Make a copy of v1 custom serialization for tests
1 parent 186bd8a commit 61c7d24

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

com.unity.mobile.notifications/Tests/Runtime/Android/UnityNotificationTestUtils.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static com.unity.androidnotifications.UnityNotificationManager.KEY_FIRE_TIME;
99
import static com.unity.androidnotifications.UnityNotificationManager.KEY_REPEAT_INTERVAL;
1010
import static com.unity.androidnotifications.UnityNotificationManager.KEY_INTENT_DATA;
11+
import static com.unity.androidnotifications.UnityNotificationManager.KEY_SHOW_IN_FOREGROUND;
1112
import static com.unity.androidnotifications.UnityNotificationManager.KEY_SMALL_ICON;
1213
import static com.unity.androidnotifications.UnityNotificationManager.KEY_LARGE_ICON;
1314
import static com.unity.androidnotifications.UnityNotificationManager.TAG_UNITY;
@@ -17,8 +18,7 @@
1718

1819
// Java class for testing purposes, not included in regular build
1920
public class UnityNotificationTestUtils {
20-
// copy-paste of what serialization was in version 0
21-
// except for hardcoded version 0
21+
// copy-paste of what serialization was in version 0 (except for hardcoded version number in here)
2222
private static boolean serializeNotificationCustom_v0(Notification notification, DataOutputStream out) {
2323
try {
2424
out.write(UNITY_MAGIC_NUMBER);
@@ -65,4 +65,53 @@ private static boolean serializeNotificationCustom_v0(Notification notification,
6565
return false;
6666
}
6767
}
68+
69+
// copy-paste of what serialization was in version 1 (except for hardcoded version number in here)
70+
private static boolean serializeNotificationCustom_v1(Notification notification, DataOutputStream out) {
71+
try {
72+
out.write(UNITY_MAGIC_NUMBER);
73+
out.writeInt(1); // NOTIFICATION_SERIALIZATION_VERSION
74+
75+
// serialize extras
76+
boolean showWhen = notification.extras.getBoolean(Notification.EXTRA_SHOW_WHEN, false);
77+
byte[] extras = serializeParcelable(notification.extras);
78+
out.writeInt(extras == null ? 0 : extras.length);
79+
if (extras != null && extras.length > 0)
80+
out.write(extras);
81+
else {
82+
// parcelling may fail in case it contains binder object, when that happens serialize manually what we care about
83+
out.writeInt(notification.extras.getInt(KEY_ID));
84+
serializeString(out, notification.extras.getString(Notification.EXTRA_TITLE));
85+
serializeString(out, notification.extras.getString(Notification.EXTRA_TEXT));
86+
serializeString(out, notification.extras.getString(KEY_SMALL_ICON));
87+
serializeString(out, notification.extras.getString(KEY_LARGE_ICON));
88+
out.writeLong(notification.extras.getLong(KEY_FIRE_TIME, -1));
89+
out.writeLong(notification.extras.getLong(KEY_REPEAT_INTERVAL, -1));
90+
serializeString(out, Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ? null : notification.extras.getString(Notification.EXTRA_BIG_TEXT));
91+
out.writeBoolean(notification.extras.getBoolean(Notification.EXTRA_SHOW_CHRONOMETER, false));
92+
out.writeBoolean(showWhen);
93+
serializeString(out, notification.extras.getString(KEY_INTENT_DATA));
94+
out.writeBoolean(notification.extras.getBoolean(KEY_SHOW_IN_FOREGROUND, true));
95+
}
96+
97+
serializeString(out, Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? null : notification.getChannelId());
98+
Integer color = UnityNotificationManager.getNotificationColor(notification);
99+
out.writeBoolean(color != null);
100+
if (color != null)
101+
out.writeInt(color);
102+
out.writeInt(notification.number);
103+
out.writeBoolean(0 != (notification.flags & Notification.FLAG_AUTO_CANCEL));
104+
serializeString(out, notification.getGroup());
105+
out.writeBoolean(0 != (notification.flags & Notification.FLAG_GROUP_SUMMARY));
106+
out.writeInt(UnityNotificationManager.getNotificationGroupAlertBehavior(notification));
107+
serializeString(out, notification.getSortKey());
108+
if (showWhen)
109+
out.writeLong(notification.when);
110+
111+
return true;
112+
} catch (Exception e) {
113+
Log.e(TAG_UNITY, "Failed to serialize notification", e);
114+
return false;
115+
}
116+
}
68117
}

0 commit comments

Comments
 (0)