|
8 | 8 | import static com.unity.androidnotifications.UnityNotificationManager.KEY_FIRE_TIME;
|
9 | 9 | import static com.unity.androidnotifications.UnityNotificationManager.KEY_REPEAT_INTERVAL;
|
10 | 10 | import static com.unity.androidnotifications.UnityNotificationManager.KEY_INTENT_DATA;
|
| 11 | +import static com.unity.androidnotifications.UnityNotificationManager.KEY_SHOW_IN_FOREGROUND; |
11 | 12 | import static com.unity.androidnotifications.UnityNotificationManager.KEY_SMALL_ICON;
|
12 | 13 | import static com.unity.androidnotifications.UnityNotificationManager.KEY_LARGE_ICON;
|
13 | 14 | import static com.unity.androidnotifications.UnityNotificationManager.TAG_UNITY;
|
|
17 | 18 |
|
18 | 19 | // Java class for testing purposes, not included in regular build
|
19 | 20 | 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) |
22 | 22 | private static boolean serializeNotificationCustom_v0(Notification notification, DataOutputStream out) {
|
23 | 23 | try {
|
24 | 24 | out.write(UNITY_MAGIC_NUMBER);
|
@@ -65,4 +65,53 @@ private static boolean serializeNotificationCustom_v0(Notification notification,
|
65 | 65 | return false;
|
66 | 66 | }
|
67 | 67 | }
|
| 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 | + } |
68 | 117 | }
|
0 commit comments