6
6
import org .json .JSONException ;
7
7
import org .json .JSONObject ;
8
8
9
+ import java .text .ParseException ;
10
+ import java .text .SimpleDateFormat ;
9
11
import java .util .ArrayList ;
12
+ import java .util .Date ;
10
13
import java .util .HashMap ;
11
14
import java .util .HashSet ;
12
15
import java .util .Iterator ;
16
+ import java .util .Locale ;
13
17
import java .util .Set ;
14
18
15
19
class OSInAppMessage {
@@ -18,7 +22,8 @@ class OSInAppMessage {
18
22
private static final String IAM_VARIANTS = "variants" ;
19
23
private static final String IAM_TRIGGERS = "triggers" ;
20
24
private static final String IAM_REDISPLAY_STATS = "redisplay" ;
21
- private static final String DISPLAY_DURATION = "display_duration" ;
25
+ private static final String DISPLAY_DURATION = "displayDuration" ;
26
+ private static final String END_TIME = "end_time" ;
22
27
23
28
/**
24
29
* The unique identifier for this in-app message
@@ -57,6 +62,7 @@ class OSInAppMessage {
57
62
private boolean displayedInSession = false ;
58
63
private boolean triggerChanged = false ;
59
64
private boolean actionTaken ;
65
+ private Date endTime ;
60
66
boolean isPreview ;
61
67
62
68
OSInAppMessage (boolean isPreview ) {
@@ -76,11 +82,31 @@ class OSInAppMessage {
76
82
this .variants = parseVariants (json .getJSONObject (IAM_VARIANTS ));
77
83
this .triggers = parseTriggerJson (json .getJSONArray (IAM_TRIGGERS ));
78
84
this .clickedClickIds = new HashSet <>();
85
+ this .endTime = parseEndTimeJson (json );
79
86
80
87
if (json .has (IAM_REDISPLAY_STATS ))
81
88
this .redisplayStats = new OSInAppMessageRedisplayStats (json .getJSONObject (IAM_REDISPLAY_STATS ));
82
89
}
83
90
91
+ private Date parseEndTimeJson (JSONObject json ) {
92
+ String endTimeString ;
93
+ try {
94
+ endTimeString = json .getString (END_TIME );
95
+ } catch (JSONException e ) {
96
+ return null ;
97
+ }
98
+
99
+ SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" , Locale .US );
100
+ try {
101
+ Date date = format .parse (endTimeString );
102
+ return date ;
103
+ } catch (ParseException e ) {
104
+ e .printStackTrace ();
105
+ }
106
+
107
+ return null ;
108
+ }
109
+
84
110
private HashMap <String , HashMap <String , String >> parseVariants (JSONObject json ) throws JSONException {
85
111
HashMap <String , HashMap <String , String >> variantTypes = new HashMap <>();
86
112
@@ -152,6 +178,12 @@ JSONObject toJSONObject() {
152
178
153
179
json .put (IAM_TRIGGERS , orConditions );
154
180
181
+ if (this .endTime != null ) {
182
+ SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" , Locale .US );
183
+ String endTimeString = format .format (this .endTime );
184
+ json .put (END_TIME , endTimeString );
185
+ }
186
+
155
187
} catch (JSONException exception ) {
156
188
exception .printStackTrace ();
157
189
}
@@ -231,6 +263,7 @@ public String toString() {
231
263
", triggerChanged=" + triggerChanged +
232
264
", actionTaken=" + actionTaken +
233
265
", isPreview=" + isPreview +
266
+ ", endTime=" + endTime +
234
267
'}' ;
235
268
}
236
269
@@ -248,4 +281,11 @@ public int hashCode() {
248
281
return result ;
249
282
}
250
283
284
+ public boolean isFinished () {
285
+ if (this .endTime == null ) {
286
+ return false ;
287
+ }
288
+ Date now = new Date ();
289
+ return this .endTime .before (now );
290
+ }
251
291
}
0 commit comments