Skip to content

Commit 1620867

Browse files
committed
android notif: Keep parsed URL of avatarURL, rather than String.
We already ensure in the "crunchy shell" that the value does in fact parse successfully as a URL; so this just means keeping the result, rather than discarding it. This also allows a couple of bits of logic in the interior to behave more precisely: * Instead of just appending a string like `&s=160` to the URL -- implicitly assuming that the URL already contains a query (`?...`) and doesn't contain a fragment (`#...`) -- use the URL's structure to add to the query what we want. * Drop a `startsWith("http")` condition. This was a crude heuristic check that the URL was a real absolute URL; since cee71e0, no such check has been necessary.
1 parent 5027e23 commit 1620867

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

android/app/src/main/java/com/zulipmobile/notifications/FCMPushNotifications.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private static Notification.Builder getNotificationBuilder(
114114
Recipient recipient = fcmMessage.getRecipient();
115115
String content = fcmMessage.getContent();
116116
String senderFullName = fcmMessage.getSender().getFullName();
117-
String avatarURL = fcmMessage.getSender().getAvatarURL();
117+
URL avatarURL = fcmMessage.getSender().getAvatarURL();
118118
String time = fcmMessage.getTime();
119119
int totalMessagesCount = extractTotalMessagesCount(conversations);
120120

@@ -137,12 +137,10 @@ private static Notification.Builder getNotificationBuilder(
137137
String displayTopic = r.getStream() + " > " + r.getTopic();
138138
builder.setSubText("Message on " + displayTopic);
139139
}
140-
if (avatarURL.startsWith("http")) {
141-
Bitmap avatar = fetchAvatar(NotificationHelper.sizedURL(context,
142-
avatarURL, 64));
143-
if (avatar != null) {
144-
builder.setLargeIcon(avatar);
145-
}
140+
Bitmap avatar = fetchAvatar(NotificationHelper.sizedURL(context,
141+
avatarURL, 64));
142+
if (avatar != null) {
143+
builder.setLargeIcon(avatar);
146144
}
147145
builder.setStyle(new Notification.BigTextStyle().bigText(content));
148146
} else {

android/app/src/main/java/com/zulipmobile/notifications/FcmMessage.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal data class Identity(
3535
internal data class Sender(
3636
val id: Int?,
3737
val email: String,
38-
val avatarURL: String,
38+
val avatarURL: URL,
3939
val fullName: String
4040
)
4141

@@ -163,8 +163,7 @@ internal data class MessageFcmMessage(
163163
else -> throw FcmMessageParseException("unexpected recipient_type: $recipientType")
164164
}
165165

166-
val avatarURL = data.require("sender_avatar_url")
167-
parseUrl(avatarURL, "sender_avatar_url")
166+
val avatarURL = data.requireUrl("sender_avatar_url")
168167

169168
return MessageFcmMessage(
170169
identity = identity,
@@ -216,6 +215,8 @@ private fun parseInt(s: String, msg: String): Int = try {
216215
throw FcmMessageParseException("$msg: $s")
217216
}
218217

218+
private fun Map<String, String>.requireUrl(key: String): URL = parseUrl(require(key), key)
219+
219220
private fun parseUrl(s: String, loc: String): URL = try {
220221
URL(s)
221222
} catch (e: MalformedURLException) {

android/app/src/main/java/com/zulipmobile/notifications/NotificationHelper.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ static Bitmap fetch(URL url) throws IOException {
4444
return null;
4545
}
4646

47-
static URL sizedURL(Context context, String url, float dpSize) {
47+
static URL sizedURL(Context context, URL url, float dpSize) {
4848
// From http://stackoverflow.com/questions/4605527/
4949
Resources r = context.getResources();
5050
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
5151
dpSize, r.getDisplayMetrics());
52+
final String query = url.getQuery() != null ? url.getQuery() + "&s=" + px : "s=" + px;
5253
try {
53-
return new URL(url + "&s=" + px);
54+
return new URL(url, "?" + query);
5455
} catch (MalformedURLException e) {
5556
Log.e(TAG, "ERROR: " + e.toString());
5657
return null;

android/app/src/test/java/com/zulipmobile/notifications/FcmMessageTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class MessageFcmMessageTest : FcmMessageTestBase() {
108108
sender = Sender(
109109
id = 123,
110110
email = Example.stream["sender_email"]!!,
111-
avatarURL = Example.stream["sender_avatar_url"]!!,
111+
avatarURL = URL(Example.stream["sender_avatar_url"]!!),
112112
fullName = Example.stream["sender_full_name"]!!
113113
),
114114
zulipMessageId = 12345,

0 commit comments

Comments
 (0)