admin
2021-06-29 9577602ea5616f34688f57ce9fd4d601ee3502d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/*
 * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
 
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
 
     http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package com.huawei.push.android;
 
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.huawei.push.message.Notification;
import com.huawei.push.model.Importance;
import com.huawei.push.model.Visibility;
import com.huawei.push.util.CollectionUtils;
import com.huawei.push.util.ValidatorUtils;
 
import org.apache.commons.lang3.StringUtils;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
 
public class AndroidNotification {
 
    private static final String COLOR_PATTERN = "^#[0-9a-fA-F]{6}$";
 
    private static final String URL_PATTERN = "^https.*";
 
    private static final String VIBRATE_PATTERN = "[0-9]+|[0-9]+[sS]|[0-9]+[.][0-9]{1,9}|[0-9]+[.][0-9]{1,9}[sS]";
 
    @JSONField(name = "title")
    private String title;
 
    @JSONField(name = "body")
    private String body;
 
    @JSONField(name = "icon")
    private String icon;
 
    @JSONField(name = "color")
    private String color;
 
    @JSONField(name = "sound")
    private String sound;
 
    @JSONField(name = "default_sound")
    private boolean defaultSound;
 
    @JSONField(name = "tag")
    private String tag;
 
    @JSONField(name = "click_action")
    private ClickAction clickAction;
 
    @JSONField(name = "body_loc_key")
    private String bodyLocKey;
 
    @JSONField(name = "body_loc_args")
    private List<String> bodyLocArgs = new ArrayList<>();
 
    @JSONField(name = "title_loc_key")
    private String titleLocKey;
 
    @JSONField(name = "title_loc_args")
    private List<String> titleLocArgs = new ArrayList<>();
 
    @JSONField(name = "multi_lang_key")
    private JSONObject multiLangKey;
 
    @JSONField(name = "channel_id")
    private String channelId;
 
    @JSONField(name = "notify_summary")
    private String notifySummary;
 
    @JSONField(name = "image")
    private String image;
 
    @JSONField(name = "style")
    private Integer style;
 
    @JSONField(name = "big_title")
    private String bigTitle;
 
    @JSONField(name = "big_body")
    private String bigBody;
 
    @JSONField(name = "auto_clear")
    private Integer autoClear;
 
    @JSONField(name = "notify_id")
    private Integer notifyId;
 
    @JSONField(name = "group")
    private String group;
 
    @JSONField(name = "badge")
    private BadgeNotification badge;
 
    @JSONField(name = "ticker")
    private String ticker;
 
    @JSONField(name = "auto_cancel")
    private boolean autoCancel;
 
    @JSONField(name = "when")
    private String when;
 
    @JSONField(name = "local_only")
    private Boolean localOnly;
 
    @JSONField(name = "importance")
    private String importance;
 
    @JSONField(name = "use_default_vibrate")
    private boolean useDefaultVibrate;
 
    @JSONField(name = "use_default_light")
    private boolean useDefaultLight;
 
    @JSONField(name = "vibrate_config")
    private List<String> vibrateConfig = new ArrayList<>();
 
    @JSONField(name = "visibility")
    private String visibility;
 
    @JSONField(name = "light_settings")
    private LightSettings lightSettings;
 
    @JSONField(name = "foreground_show")
    private boolean foregroundShow;
 
    @JSONField(name = "inbox_content")
    private List<String> inboxContent;
 
    @JSONField(name = "buttons")
    private List<Button> buttons;
 
    private AndroidNotification(Builder builder) {
        this.title = builder.title;
        this.body = builder.body;
        this.icon = builder.icon;
        this.color = builder.color;
        this.sound = builder.sound;
        this.defaultSound = builder.defaultSound;
 
        this.tag = builder.tag;
        this.clickAction = builder.clickAction;
 
        this.bodyLocKey = builder.bodyLocKey;
        if (!CollectionUtils.isEmpty(builder.bodyLocArgs)) {
            this.bodyLocArgs.addAll(builder.bodyLocArgs);
        } else {
            this.bodyLocArgs = null;
        }
 
        this.titleLocKey = builder.titleLocKey;
        if (!CollectionUtils.isEmpty(builder.titleLocArgs)) {
            this.titleLocArgs.addAll(builder.titleLocArgs);
        } else {
            this.titleLocArgs = null;
        }
 
        if (builder.multiLangkey != null) {
            this.multiLangKey = builder.multiLangkey;
        } else {
            this.multiLangKey = null;
        }
 
        this.channelId = builder.channelId;
 
        this.notifySummary = builder.notifySummary;
        this.image = builder.image;
        this.style = builder.style;
        this.bigTitle = builder.bigTitle;
        this.bigBody = builder.bigBody;
        this.autoClear = builder.autoClear;
        this.notifyId = builder.notifyId;
        this.group = builder.group;
 
        if (null != builder.badge) {
            this.badge = builder.badge;
        } else {
            this.badge = null;
        }
 
        this.ticker = builder.ticker;
        this.autoCancel = builder.autoCancel;
        this.when = builder.when;
        this.importance = builder.importance;
        this.useDefaultVibrate = builder.useDefaultVibrate;
        this.useDefaultLight = builder.useDefaultLight;
        if (!CollectionUtils.isEmpty(builder.vibrateConfig)) {
            this.vibrateConfig = builder.vibrateConfig;
        } else {
            this.vibrateConfig = null;
        }
 
        this.visibility = builder.visibility;
        this.lightSettings = builder.lightSettings;
        this.foregroundShow = builder.foregroundShow;
 
        if (!CollectionUtils.isEmpty(builder.inboxContent)) {
            this.inboxContent = builder.inboxContent;
        } else {
            this.inboxContent = null;
        }
 
        if (!CollectionUtils.isEmpty(builder.buttons)) {
            this.buttons = builder.buttons;
        } else {
            this.buttons = null;
        }
    }
 
    /**
     * check androidNotification's parameters
     *
     * @param notification which is in message
     */
    public void check(Notification notification) {
        if (null != notification) {
            ValidatorUtils.checkArgument(StringUtils.isNotEmpty(notification.getTitle()) || StringUtils.isNotEmpty(this.title), "title should be set");
            ValidatorUtils.checkArgument(StringUtils.isNotEmpty(notification.getBody()) || StringUtils.isNotEmpty(this.body), "body should be set");
        }
 
        if (StringUtils.isNotEmpty(this.color)) {
            ValidatorUtils.checkArgument(this.color.matches(AndroidNotification.COLOR_PATTERN), "Wrong color format, color must be in the form #RRGGBB");
        }
 
        if (this.clickAction != null) {
            this.clickAction.check();
        }
 
        if (!CollectionUtils.isEmpty(this.bodyLocArgs)) {
            ValidatorUtils.checkArgument(StringUtils.isNotEmpty(this.bodyLocKey), "bodyLocKey is required when specifying bodyLocArgs");
        }
 
        if (!CollectionUtils.isEmpty(this.titleLocArgs)) {
            ValidatorUtils.checkArgument(StringUtils.isNotEmpty(this.titleLocKey), "titleLocKey is required when specifying titleLocArgs");
        }
 
        if (StringUtils.isNotEmpty(this.image)) {
            ValidatorUtils.checkArgument(this.image.matches(URL_PATTERN), "notifyIcon must start with https");
        }
 
        //Style 0,1,2
        if (this.style != null) {
            boolean isTrue = this.style == 0 ||
                    this.style == 1 ||
                    this.style == 2 ||
                    this.style == 3;
            ValidatorUtils.checkArgument(isTrue, "style should be one of 0:default, 1: big text, 2: big picture");
 
            if (this.style == 1) {
                ValidatorUtils.checkArgument(StringUtils.isNotEmpty(this.bigTitle) && StringUtils.isNotEmpty(this.bigBody), "title and body are required when style = 1");
            } else if (this.style == 3) {
                ValidatorUtils.checkArgument(!CollectionUtils.isEmpty(this.inboxContent) && this.inboxContent.size() <= 5, "inboxContent is required when style = 3 and at most 5 inbox content is needed");
            }
        }
 
        if (this.autoClear != null) {
            ValidatorUtils.checkArgument(this.autoClear.intValue() > 0, "auto clear should positive value");
        }
 
        if (badge != null) {
            this.badge.check();
        }
 
        if (this.importance != null) {
            ValidatorUtils.checkArgument(StringUtils.equals(this.importance, Importance.LOW.getValue())
                            || StringUtils.equals(this.importance, Importance.NORMAL.getValue())
                            || StringUtils.equals(this.importance, Importance.HIGH.getValue()),
                    "importance shouid be [HIGH, NORMAL, LOW]");
        }
 
        if (!CollectionUtils.isEmpty(this.vibrateConfig)) {
            ValidatorUtils.checkArgument(this.vibrateConfig.size() <= 10, "vibrate_config array size cannot be more than 10");
            for (String vibrateTiming : this.vibrateConfig) {
                ValidatorUtils.checkArgument(vibrateTiming.matches(AndroidNotification.VIBRATE_PATTERN), "Wrong vibrate timing format");
                long vibrateTimingValue = (long) (1000 * Double
                        .valueOf(StringUtils.substringBefore(vibrateTiming.toLowerCase(Locale.getDefault()), "s")));
                ValidatorUtils.checkArgument(vibrateTimingValue > 0 && vibrateTimingValue < 60000, "Vibrate timing duration must be greater than 0 and less than 60s");
            }
        }
 
        if (this.visibility != null) {
            ValidatorUtils.checkArgument(StringUtils.equals(this.visibility, Visibility.VISIBILITY_UNSPECIFIED.getValue())
                            || StringUtils.equals(this.visibility, Visibility.PRIVATE.getValue())
                            || StringUtils.equals(this.visibility, Visibility.PUBLIC.getValue())
                            || StringUtils.equals(this.visibility, Visibility.SECRET.getValue()),
                    "visibility shouid be [VISIBILITY_UNSPECIFIED, PRIVATE, PUBLIC, SECRET]");
        }
 
        // multiLangKey
        if (null != this.multiLangKey) {
            Iterator<String> keyIterator = this.multiLangKey.keySet().iterator();
            while (keyIterator.hasNext()) {
                String key = keyIterator.next();
                ValidatorUtils.checkArgument((this.multiLangKey.get(key) instanceof JSONObject), "multiLangKey value should be JSONObject");
                JSONObject contentsObj = multiLangKey.getJSONObject(key);
                if (contentsObj != null) {
                    ValidatorUtils.checkArgument(contentsObj.keySet().size() <= 3, "Only three lang property can carry");
                }
            }
        }
 
        if (this.lightSettings != null) {
            this.lightSettings.check();
        }
 
        if (!CollectionUtils.isEmpty(this.buttons)) {
            ValidatorUtils.checkArgument(this.buttons.size() <= 3, "Only three buttons can carry");
            for (Button button : this.buttons) {
                button.check();
            }
        }
    }
 
    /**
     * getter
     */
    public String getTitle() {
        return title;
    }
 
    public String getBody() {
        return body;
    }
 
    public String getIcon() {
        return icon;
    }
 
    public String getColor() {
        return color;
    }
 
    public String getSound() {
        return sound;
    }
 
    public Boolean isDefaultSound() {
        return defaultSound;
    }
 
    public String getTag() {
        return tag;
    }
 
    public ClickAction getClickAction() {
        return clickAction;
    }
 
    public String getBodyLocKey() {
        return bodyLocKey;
    }
 
    public List<String> getBodyLocArgs() {
        return bodyLocArgs;
    }
 
    public String getTitleLocKey() {
        return titleLocKey;
    }
 
    public List<String> getTitleLocArgs() {
        return titleLocArgs;
    }
 
    public JSONObject getMultiLangKey() {
        return multiLangKey;
    }
 
    public String getChannelId() {
        return channelId;
    }
 
    public String getNotifySummary() {
        return notifySummary;
    }
 
    public String getImage() {
        return image;
    }
 
    public Integer getStyle() {
        return style;
    }
 
    public String getBigTitle() {
        return bigTitle;
    }
 
    public String getBigBody() {
        return bigBody;
    }
 
    public Integer getAutoClear() {
        return autoClear;
    }
 
    public Integer getNotifyId() {
        return notifyId;
    }
 
    public String getGroup() {
        return group;
    }
 
    public BadgeNotification getBadge() {
        return badge;
    }
 
    public String getTicker() {
        return ticker;
    }
 
    public String getWhen() {
        return when;
    }
 
    public String getImportance() {
        return importance;
    }
 
    public List<String> getVibrateConfig() {
        return vibrateConfig;
    }
 
    public String getVisibility() {
        return visibility;
    }
 
    public LightSettings getLightSettings() {
        return lightSettings;
    }
 
    public boolean isAutoCancel() {
        return autoCancel;
    }
 
    public Boolean getLocalOnly() {
        return localOnly;
    }
 
    public boolean isUseDefaultVibrate() {
        return useDefaultVibrate;
    }
 
    public boolean isUseDefaultLight() {
        return useDefaultLight;
    }
 
    public boolean isForegroundShow() {
        return foregroundShow;
    }
 
    public List<String> getInboxContent() {
        return inboxContent;
    }
 
    public List<Button> getButtons() {
        return buttons;
    }
 
    /**
     * builder
     *
     * @return
     */
    public static Builder builder() {
        return new Builder();
    }
 
    public static class Builder {
 
        private String title;
        private String body;
        private String icon;
        private String color;
        private String sound;
        private boolean defaultSound;
        private String tag;
        private ClickAction clickAction;
        private String bodyLocKey;
        private List<String> bodyLocArgs = new ArrayList<>();
        private String titleLocKey;
        private List<String> titleLocArgs = new ArrayList<>();
        private JSONObject multiLangkey;
        private String channelId;
        private String notifySummary;
        private String image;
        private Integer style;
        private String bigTitle;
        private String bigBody;
        private Integer autoClear;
        private Integer notifyId;
        private String group;
 
        private BadgeNotification badge;
 
        private String ticker;
        private boolean autoCancel = true;
        private String when;
        private String importance;
        private boolean useDefaultVibrate;
        private boolean useDefaultLight;
        private List<String> vibrateConfig = new ArrayList<>();
        private String visibility;
        private LightSettings lightSettings;
        private boolean foregroundShow;
 
        private List<String> inboxContent = new ArrayList<>();
        private List<Button> buttons = new ArrayList<Button>();
 
        private Builder() {
        }
 
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
 
        public Builder setBody(String body) {
            this.body = body;
            return this;
        }
 
        public Builder setIcon(String icon) {
            this.icon = icon;
            return this;
        }
 
        public Builder setColor(String color) {
            this.color = color;
            return this;
        }
 
        public Builder setSound(String sound) {
            this.sound = sound;
            return this;
        }
 
        public Builder setDefaultSound(boolean defaultSound) {
            this.defaultSound = defaultSound;
            return this;
        }
 
        public Builder setTag(String tag) {
            this.tag = tag;
            return this;
        }
 
        public Builder setClickAction(ClickAction clickAction) {
            this.clickAction = clickAction;
            return this;
        }
 
        public Builder setBodyLocKey(String bodyLocKey) {
            this.bodyLocKey = bodyLocKey;
            return this;
        }
 
        public Builder addBodyLocArgs(String arg) {
            this.bodyLocArgs.add(arg);
            return this;
        }
 
        public Builder addAllBodyLocArgs(List<String> args) {
            this.bodyLocArgs.addAll(args);
            return this;
        }
 
        public Builder setTitleLocKey(String titleLocKey) {
            this.titleLocKey = titleLocKey;
            return this;
        }
 
        public Builder addTitleLocArgs(String arg) {
            this.titleLocArgs.add(arg);
            return this;
        }
 
        public Builder addAllTitleLocArgs(List<String> args) {
            this.titleLocArgs.addAll(args);
            return this;
        }
 
        public Builder setMultiLangkey(JSONObject multiLangkey) {
            this.multiLangkey = multiLangkey;
            return this;
        }
 
        public Builder setChannelId(String channelId) {
            this.channelId = channelId;
            return this;
        }
 
        public Builder setNotifySummary(String notifySummary) {
            this.notifySummary = notifySummary;
            return this;
        }
 
        public Builder setImage(String image) {
            this.image = image;
            return this;
        }
 
        public Builder setStyle(Integer style) {
            this.style = style;
            return this;
        }
 
        public Builder setBigTitle(String bigTitle) {
            this.bigTitle = bigTitle;
            return this;
        }
 
        public Builder setBigBody(String bigBody) {
            this.bigBody = bigBody;
            return this;
        }
 
        public Builder setAutoClear(Integer autoClear) {
            this.autoClear = autoClear;
            return this;
        }
 
        public Builder setNotifyId(Integer notifyId) {
            this.notifyId = notifyId;
            return this;
        }
 
        public Builder setGroup(String group) {
            this.group = group;
            return this;
        }
 
        public Builder setBadge(BadgeNotification badge) {
            this.badge = badge;
            return this;
        }
 
        public Builder setTicker(String ticker) {
            this.ticker = ticker;
            return this;
        }
 
        public Builder setAutoCancel(boolean autoCancel) {
            this.autoCancel = autoCancel;
            return this;
        }
 
        public Builder setWhen(String when) {
            this.when = when;
            return this;
        }
 
        public Builder setImportance(String importance) {
            this.importance = importance;
            return this;
        }
 
        public Builder setUseDefaultVibrate(boolean useDefaultVibrate) {
            this.useDefaultVibrate = useDefaultVibrate;
            return this;
        }
 
        public Builder setUseDefaultLight(boolean useDefaultLight) {
            this.useDefaultLight = useDefaultLight;
            return this;
        }
 
        public Builder addVibrateConfig(String vibrateTiming) {
            this.vibrateConfig.add(vibrateTiming);
            return this;
        }
 
 
        public Builder addAllVibrateConfig(List<String> vibrateTimings) {
            this.vibrateConfig.addAll(vibrateTimings);
            return this;
        }
 
        public Builder setVisibility(String visibility) {
            this.visibility = visibility;
            return this;
        }
 
        public Builder setLightSettings(LightSettings lightSettings) {
            this.lightSettings = lightSettings;
            return this;
        }
 
        public Builder setForegroundShow(boolean foregroundShow) {
            this.foregroundShow = foregroundShow;
            return this;
        }
 
        public Builder addInboxContent(String inboxContent) {
            this.inboxContent.add(inboxContent);
            return this;
        }
 
        public Builder addAllInboxContent(List<String> inboxContents) {
            this.inboxContent.addAll(inboxContents);
            return this;
        }
 
        public Builder addButton(Button button) {
            this.buttons.add(button);
            return this;
        }
 
        public Builder addAllButtons(List<Button> buttons) {
            this.buttons.addAll(buttons);
            return this;
        }
 
        public AndroidNotification build() {
            return new AndroidNotification(this);
        }
    }
}