|
17 | 17 | package com.google.firebase.messaging; |
18 | 18 |
|
19 | 19 | import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.base.Preconditions.checkNotNull; |
20 | 21 |
|
21 | 22 | import com.google.api.client.util.Key; |
22 | 23 | import com.google.common.base.Strings; |
23 | 24 | import com.google.common.collect.ImmutableList; |
24 | 25 | import com.google.common.collect.ImmutableMap; |
25 | 26 | import com.google.firebase.internal.NonNull; |
26 | | -import java.text.SimpleDateFormat; |
| 27 | +import java.time.Instant; |
| 28 | +import java.time.ZoneOffset; |
| 29 | +import java.time.format.DateTimeFormatter; |
27 | 30 | import java.util.ArrayList; |
28 | | -import java.util.Date; |
29 | 31 | import java.util.List; |
| 32 | +import java.util.Locale; |
30 | 33 | import java.util.Map; |
31 | | -import java.util.TimeZone; |
32 | 34 | import java.util.concurrent.TimeUnit; |
| 35 | +import java.util.regex.Pattern; |
33 | 36 |
|
34 | 37 | /** |
35 | 38 | * Represents the Android-specific notification options that can be included in a |
@@ -125,12 +128,18 @@ public final class AndroidNotificationV2 { |
125 | 128 | .put(NotificationPriority.MAX, "PRIORITY_MAX") |
126 | 129 | .build(); |
127 | 130 |
|
| 131 | + private static final Pattern COLOR_PATTERN = Pattern.compile("^#[0-9a-fA-F]{6}$"); |
| 132 | + |
| 133 | + private static final DateTimeFormatter EVENT_TIME_FORMATTER = |
| 134 | + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS000000'Z'") |
| 135 | + .withZone(ZoneOffset.UTC); |
| 136 | + |
128 | 137 | private AndroidNotificationV2(Builder builder) { |
129 | 138 | this.title = builder.title; |
130 | 139 | this.body = builder.body; |
131 | 140 | this.icon = builder.icon; |
132 | 141 | if (builder.color != null) { |
133 | | - checkArgument(builder.color.matches("^#[0-9a-fA-F]{6}$"), |
| 142 | + checkArgument(COLOR_PATTERN.matcher(builder.color).matches(), |
134 | 143 | "color must be in the form #RRGGBB"); |
135 | 144 | } |
136 | 145 | this.color = builder.color; |
@@ -173,7 +182,7 @@ private AndroidNotificationV2(Builder builder) { |
173 | 182 | this.defaultLightSettings = builder.defaultLightSettings; |
174 | 183 | this.visibility = (builder.visibility != null |
175 | 184 | && builder.visibility != Visibility.UNSPECIFIED) |
176 | | - ? builder.visibility.name().toLowerCase() : null; |
| 185 | + ? builder.visibility.name().toLowerCase(Locale.ENGLISH) : null; |
177 | 186 | if (builder.notificationCount != null) { |
178 | 187 | checkArgument(builder.notificationCount >= 0, |
179 | 188 | "notificationCount if specified must be zero or positive valued"); |
@@ -354,6 +363,7 @@ public Builder addBodyLocalizationArg(String arg) { |
354 | 363 | * @return This builder. |
355 | 364 | */ |
356 | 365 | public Builder addAllBodyLocalizationArgs(@NonNull List<String> args) { |
| 366 | + checkNotNull(args, "args must not be null"); |
357 | 367 | this.bodyLocArgs.addAll(args); |
358 | 368 | return this; |
359 | 369 | } |
@@ -390,6 +400,7 @@ public Builder addTitleLocalizationArg(String arg) { |
390 | 400 | * @return This builder. |
391 | 401 | */ |
392 | 402 | public Builder addAllTitleLocalizationArgs(@NonNull List<String> args) { |
| 403 | + checkNotNull(args, "args must not be null"); |
393 | 404 | this.titleLocArgs.addAll(args); |
394 | 405 | return this; |
395 | 406 | } |
@@ -457,9 +468,7 @@ public Builder setSticky(boolean sticky) { |
457 | 468 | * @return This builder. |
458 | 469 | */ |
459 | 470 | public Builder setEventTimeInMillis(long eventTimeInMillis) { |
460 | | - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS000000'Z'"); |
461 | | - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); |
462 | | - this.eventTime = dateFormat.format(new Date(eventTimeInMillis)); |
| 471 | + this.eventTime = EVENT_TIME_FORMATTER.format(Instant.ofEpochMilli(eventTimeInMillis)); |
463 | 472 | return this; |
464 | 473 | } |
465 | 474 |
|
@@ -503,6 +512,7 @@ public Builder setNotificationPriority(NotificationPriority priority) { |
503 | 512 | * @return This builder. |
504 | 513 | */ |
505 | 514 | public Builder setVibrateTimingsInMillis(long[] vibrateTimingsInMillis) { |
| 515 | + checkNotNull(vibrateTimingsInMillis, "vibrateTimingsInMillis must not be null"); |
506 | 516 | List<String> list = new ArrayList<>(); |
507 | 517 | for (long value : vibrateTimingsInMillis) { |
508 | 518 | checkArgument(value >= 0, "elements in vibrateTimingsInMillis must not be negative"); |
|
0 commit comments