Skip to content

Commit

Permalink
android notif: Parse time in the crunchy shell.
Browse files Browse the repository at this point in the history
We need to parse this field anyway, and already do; move that logic
from the interior out to the "crunchy shell".  Among other things,
this means a malformed value will only cause the notification to be
ignored, rather than cause us to crash.
  • Loading branch information
gnprice committed Mar 16, 2019
1 parent 1620867 commit 739e55a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private static Notification.Builder getNotificationBuilder(
String content = fcmMessage.getContent();
String senderFullName = fcmMessage.getSender().getFullName();
URL avatarURL = fcmMessage.getSender().getAvatarURL();
String time = fcmMessage.getTime();
Long timeMs = fcmMessage.getTimeMs();
int totalMessagesCount = extractTotalMessagesCount(conversations);

if (BuildConfig.DEBUG) {
Expand Down Expand Up @@ -159,8 +159,7 @@ private static Notification.Builder getNotificationBuilder(
Log.e(TAG, "BADGE ERROR: " + e.toString());
}

long timeMillis = Long.parseLong(time) * 1000;
builder.setWhen(timeMillis);
builder.setWhen(timeMs);
long[] vPattern = {0, 100, 200, 100};
// NB the DEFAULT_VIBRATE flag below causes this to have no effect.
// TODO: choose a vibration pattern we like, and unset DEFAULT_VIBRATE.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ internal data class MessageFcmMessage(
val zulipMessageId: Int,
val recipient: Recipient,
val content: String,
val time: String
val timeMs: Long
) : FcmMessage() {

/**
Expand Down Expand Up @@ -180,7 +180,7 @@ internal data class MessageFcmMessage(
zulipMessageId = data.requireInt("zulip_message_id"),
recipient = recipient,
content = data.require("content"),
time = data.require("time")
timeMs = data.requireLong("time") * 1000
)
}
}
Expand Down Expand Up @@ -215,6 +215,14 @@ private fun parseInt(s: String, msg: String): Int = try {
throw FcmMessageParseException("$msg: $s")
}

private fun Map<String, String>.requireLong(key: String): Long = parseLong(require(key), key)

private fun parseLong(s: String, loc: String): Long = try {
s.toLong()
} catch (e: NumberFormatException) {
throw FcmMessageParseException("invalid long at $loc: $s")
}

private fun Map<String, String>.requireUrl(key: String): URL = parseUrl(require(key), key)

private fun parseUrl(s: String, loc: String): URL = try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class MessageFcmMessageTest : FcmMessageTestBase() {
topic = Example.stream["topic"]!!
),
content = Example.stream["content"]!!,
time = Example.stream["time"]!!
timeMs = Example.stream["time"]!!.toLong() * 1000
)
)
expect.that(parse(Example.groupPm).recipient).isEqualTo(
Expand Down Expand Up @@ -165,6 +165,7 @@ class MessageFcmMessageTest : FcmMessageTestBase() {
assertParseFails(Example.pm.plus("zulip_message_id" to "abc"))
assertParseFails(Example.pm.minus("content"))
assertParseFails(Example.pm.minus("time"))
assertParseFails(Example.pm.plus("time" to "12:34"))
}
}

Expand Down

0 comments on commit 739e55a

Please sign in to comment.