|
| 1 | +package com.unity.androidnotifications; |
| 2 | + |
| 3 | +import java.io.DataOutputStream; |
| 4 | +import android.app.Notification; |
| 5 | +import android.os.Build; |
| 6 | +import android.util.Log; |
| 7 | +import static com.unity.androidnotifications.UnityNotificationManager.KEY_ID; |
| 8 | +import static com.unity.androidnotifications.UnityNotificationManager.KEY_FIRE_TIME; |
| 9 | +import static com.unity.androidnotifications.UnityNotificationManager.KEY_REPEAT_INTERVAL; |
| 10 | +import static com.unity.androidnotifications.UnityNotificationManager.KEY_INTENT_DATA; |
| 11 | +import static com.unity.androidnotifications.UnityNotificationManager.KEY_SMALL_ICON; |
| 12 | +import static com.unity.androidnotifications.UnityNotificationManager.KEY_LARGE_ICON; |
| 13 | +import static com.unity.androidnotifications.UnityNotificationManager.TAG_UNITY; |
| 14 | +import static com.unity.androidnotifications.UnityNotificationUtilities.UNITY_MAGIC_NUMBER; |
| 15 | +import static com.unity.androidnotifications.UnityNotificationUtilities.serializeParcelable; |
| 16 | +import static com.unity.androidnotifications.UnityNotificationUtilities.serializeString; |
| 17 | + |
| 18 | +// Java class for testing purposes, not included in regular build |
| 19 | +public class UnityNotificationTestUtils { |
| 20 | + // copy-paste of what serialization was in version 0 |
| 21 | + // except for hardcoded version 0 |
| 22 | + private static boolean serializeNotificationCustom_v0(Notification notification, DataOutputStream out) { |
| 23 | + try { |
| 24 | + out.write(UNITY_MAGIC_NUMBER); |
| 25 | + out.writeInt(0); // NOTIFICATION_SERIALIZATION_VERSION |
| 26 | + |
| 27 | + // serialize extras |
| 28 | + boolean showWhen = notification.extras.getBoolean(Notification.EXTRA_SHOW_WHEN, false); |
| 29 | + byte[] extras = serializeParcelable(notification.extras); |
| 30 | + out.writeInt(extras == null ? 0 : extras.length); |
| 31 | + if (extras != null && extras.length > 0) |
| 32 | + out.write(extras); |
| 33 | + else { |
| 34 | + // parcelling may fail in case it contains binder object, when that happens serialize manually what we care about |
| 35 | + out.writeInt(notification.extras.getInt(KEY_ID)); |
| 36 | + serializeString(out, notification.extras.getString(Notification.EXTRA_TITLE)); |
| 37 | + serializeString(out, notification.extras.getString(Notification.EXTRA_TEXT)); |
| 38 | + serializeString(out, notification.extras.getString(KEY_SMALL_ICON)); |
| 39 | + serializeString(out, notification.extras.getString(KEY_LARGE_ICON)); |
| 40 | + out.writeLong(notification.extras.getLong(KEY_FIRE_TIME, -1)); |
| 41 | + out.writeLong(notification.extras.getLong(KEY_REPEAT_INTERVAL, -1)); |
| 42 | + serializeString(out, Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ? null : notification.extras.getString(Notification.EXTRA_BIG_TEXT)); |
| 43 | + out.writeBoolean(notification.extras.getBoolean(Notification.EXTRA_SHOW_CHRONOMETER, false)); |
| 44 | + out.writeBoolean(showWhen); |
| 45 | + serializeString(out, notification.extras.getString(KEY_INTENT_DATA)); |
| 46 | + } |
| 47 | + |
| 48 | + serializeString(out, Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? null : notification.getChannelId()); |
| 49 | + Integer color = UnityNotificationManager.getNotificationColor(notification); |
| 50 | + out.writeBoolean(color != null); |
| 51 | + if (color != null) |
| 52 | + out.writeInt(color); |
| 53 | + out.writeInt(notification.number); |
| 54 | + out.writeBoolean(0 != (notification.flags & Notification.FLAG_AUTO_CANCEL)); |
| 55 | + serializeString(out, notification.getGroup()); |
| 56 | + out.writeBoolean(0 != (notification.flags & Notification.FLAG_GROUP_SUMMARY)); |
| 57 | + out.writeInt(UnityNotificationManager.getNotificationGroupAlertBehavior(notification)); |
| 58 | + serializeString(out, notification.getSortKey()); |
| 59 | + if (showWhen) |
| 60 | + out.writeLong(notification.when); |
| 61 | + |
| 62 | + return true; |
| 63 | + } catch (Exception e) { |
| 64 | + Log.e(TAG_UNITY, "Failed to serialize notification", e); |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments