19
19
import android .view .WindowManager ;
20
20
import android .view .animation .Animation ;
21
21
import android .webkit .WebView ;
22
- import android .widget .LinearLayout ;
23
22
import android .widget .PopupWindow ;
24
23
import android .widget .RelativeLayout ;
25
24
@@ -55,7 +54,6 @@ class InAppMessageView {
55
54
56
55
private static final int ACTIVITY_FINISH_AFTER_DISMISS_DELAY_MS = 600 ;
57
56
private static final int ACTIVITY_INIT_DELAY = 200 ;
58
- private static final int MARGIN_PX_SIZE = dpToPx (24 );
59
57
private static final int DRAG_THRESHOLD_PX_SIZE = dpToPx (4 );
60
58
private PopupWindow popupWindow ;
61
59
@@ -69,7 +67,11 @@ interface InAppMessageViewListener {
69
67
private final Handler handler = new Handler ();
70
68
private int pageWidth ;
71
69
private int pageHeight ;
72
- private double dismissDuration ;
70
+ private int marginPxSizeLeft = dpToPx (24 );
71
+ private int marginPxSizeRight = dpToPx (24 );
72
+ private int marginPxSizeTop = dpToPx (24 );
73
+ private int marginPxSizeBottom = dpToPx (24 );
74
+ private double displayDuration ;
73
75
private boolean hasBackground ;
74
76
private boolean shouldDismissWhenActive = false ;
75
77
private boolean isDragging = false ;
@@ -81,14 +83,27 @@ interface InAppMessageViewListener {
81
83
private InAppMessageViewListener messageController ;
82
84
private Runnable scheduleDismissRunnable ;
83
85
84
- InAppMessageView (@ NonNull WebView webView , @ NonNull WebViewManager . Position displayLocation , int pageHeight , double dismissDuration , boolean disableDragDismiss ) {
86
+ InAppMessageView (@ NonNull WebView webView , @ NonNull OSInAppMessageContent content , boolean disableDragDismiss ) {
85
87
this .webView = webView ;
86
- this .displayLocation = displayLocation ;
87
- this .pageHeight = pageHeight ;
88
+ this .displayLocation = content . getDisplayLocation () ;
89
+ this .pageHeight = content . getPageHeight () ;
88
90
this .pageWidth = ViewGroup .LayoutParams .MATCH_PARENT ;
89
- this .dismissDuration = Double . isNaN ( dismissDuration ) ? 0 : dismissDuration ;
91
+ this .displayDuration = content . getDisplayDuration () == null ? 0 : content . getDisplayDuration () ;
90
92
this .hasBackground = !displayLocation .isBanner ();
91
93
this .disableDragDismiss = disableDragDismiss ;
94
+ setMarginsFromContent (content );
95
+ }
96
+
97
+ /**
98
+ * For now we only support default margin or no margin.
99
+ * Any non-zero value will be treated as default margin
100
+ * @param content in app message content and style
101
+ */
102
+ private void setMarginsFromContent (OSInAppMessageContent content ) {
103
+ this .marginPxSizeTop = content .getUseHeightMargin () ? dpToPx (24 ) : 0 ;
104
+ this .marginPxSizeBottom = content .getUseHeightMargin () ? dpToPx (24 ) : 0 ;
105
+ this .marginPxSizeLeft = content .getUseWidthMargin () ? dpToPx (24 ) : 0 ;
106
+ this .marginPxSizeRight = content .getUseWidthMargin () ? dpToPx (24 ) : 0 ;
92
107
}
93
108
94
109
void setWebView (WebView webView ) {
@@ -114,7 +129,7 @@ void checkIfShouldDismiss() {
114
129
finishAfterDelay (null );
115
130
}
116
131
}
117
-
132
+
118
133
/**
119
134
* This will fired when the device is rotated for example with a new provided height for the WebView
120
135
* Called to shrink or grow the WebView when it receives a JS resize event with a new height.
@@ -160,12 +175,12 @@ void showInAppMessageView(Activity currentActivity) {
160
175
);
161
176
webViewLayoutParams .addRule (RelativeLayout .CENTER_IN_PARENT );
162
177
163
- LinearLayout .LayoutParams linearLayoutParams = hasBackground ? createParentLinearLayoutParams () : null ;
178
+ RelativeLayout .LayoutParams relativeLayoutParams = hasBackground ? createParentRelativeLayoutParams () : null ;
164
179
165
180
showDraggableView (
166
181
displayLocation ,
167
182
webViewLayoutParams ,
168
- linearLayoutParams ,
183
+ relativeLayoutParams ,
169
184
createDraggableLayoutParams (pageHeight , displayLocation , disableDragDismiss )
170
185
);
171
186
}
@@ -174,42 +189,43 @@ private int getDisplayYSize() {
174
189
return OSViewUtils .getWindowHeight (currentActivity );
175
190
}
176
191
177
- private LinearLayout .LayoutParams createParentLinearLayoutParams () {
178
- LinearLayout .LayoutParams linearLayoutParams = new LinearLayout .LayoutParams (pageWidth , LinearLayout .LayoutParams .MATCH_PARENT );
179
-
192
+ private RelativeLayout .LayoutParams createParentRelativeLayoutParams () {
193
+ RelativeLayout .LayoutParams relativeLayoutParams = new RelativeLayout .LayoutParams (pageWidth , RelativeLayout .LayoutParams .MATCH_PARENT );
180
194
switch (displayLocation ) {
181
195
case TOP_BANNER :
182
- linearLayoutParams .gravity = Gravity .CENTER_HORIZONTAL | Gravity .TOP ;
196
+ relativeLayoutParams .addRule (RelativeLayout .ALIGN_PARENT_TOP );
197
+ relativeLayoutParams .addRule (RelativeLayout .CENTER_HORIZONTAL );
183
198
break ;
184
199
case BOTTOM_BANNER :
185
- linearLayoutParams .gravity = Gravity .CENTER_HORIZONTAL | Gravity .BOTTOM ;
200
+ relativeLayoutParams .addRule (RelativeLayout .ALIGN_PARENT_BOTTOM );
201
+ relativeLayoutParams .addRule (RelativeLayout .CENTER_HORIZONTAL );
186
202
break ;
187
203
case CENTER_MODAL :
188
204
case FULL_SCREEN :
189
- linearLayoutParams . gravity = Gravity . CENTER ;
205
+ relativeLayoutParams . addRule ( RelativeLayout . CENTER_IN_PARENT ) ;
190
206
}
191
207
192
- return linearLayoutParams ;
208
+ return relativeLayoutParams ;
193
209
}
194
210
195
211
private DraggableRelativeLayout .Params createDraggableLayoutParams (int pageHeight , WebViewManager .Position displayLocation , boolean disableDragging ) {
196
212
DraggableRelativeLayout .Params draggableParams = new DraggableRelativeLayout .Params ();
197
- draggableParams .maxXPos = MARGIN_PX_SIZE ;
198
- draggableParams .maxYPos = MARGIN_PX_SIZE ;
213
+ draggableParams .maxXPos = marginPxSizeRight ;
214
+ draggableParams .maxYPos = marginPxSizeTop ;
199
215
draggableParams .draggingDisabled = disableDragging ;
200
216
draggableParams .messageHeight = pageHeight ;
201
217
draggableParams .height = getDisplayYSize ();
202
218
203
219
switch (displayLocation ) {
204
220
case TOP_BANNER :
205
- draggableParams .dragThresholdY = MARGIN_PX_SIZE - DRAG_THRESHOLD_PX_SIZE ;
221
+ draggableParams .dragThresholdY = marginPxSizeTop - DRAG_THRESHOLD_PX_SIZE ;
206
222
break ;
207
223
case BOTTOM_BANNER :
208
224
draggableParams .posY = getDisplayYSize () - pageHeight ;
209
- draggableParams .dragThresholdY = MARGIN_PX_SIZE + DRAG_THRESHOLD_PX_SIZE ;
225
+ draggableParams .dragThresholdY = marginPxSizeBottom + DRAG_THRESHOLD_PX_SIZE ;
210
226
break ;
211
227
case FULL_SCREEN :
212
- draggableParams .messageHeight = pageHeight = getDisplayYSize () - (MARGIN_PX_SIZE * 2 );
228
+ draggableParams .messageHeight = pageHeight = getDisplayYSize () - (marginPxSizeBottom + marginPxSizeTop );
213
229
// fall through for FULL_SCREEN since it shares similar params to CENTER_MODAL
214
230
case CENTER_MODAL :
215
231
int y = (getDisplayYSize () / 2 ) - (pageHeight / 2 );
@@ -228,7 +244,7 @@ private DraggableRelativeLayout.Params createDraggableLayoutParams(int pageHeigh
228
244
229
245
private void showDraggableView (final WebViewManager .Position displayLocation ,
230
246
final RelativeLayout .LayoutParams relativeLayoutParams ,
231
- final LinearLayout .LayoutParams linearLayoutParams ,
247
+ final RelativeLayout .LayoutParams draggableRelativeLayoutParams ,
232
248
final DraggableRelativeLayout .Params webViewLayoutParams ) {
233
249
OSUtils .runOnMainUIThread (new Runnable () {
234
250
@ Override
@@ -239,8 +255,8 @@ public void run() {
239
255
webView .setLayoutParams (relativeLayoutParams );
240
256
241
257
Context context = currentActivity .getApplicationContext ();
242
- setUpDraggableLayout (context , linearLayoutParams , webViewLayoutParams );
243
- setUpParentLinearLayout (context );
258
+ setUpDraggableLayout (context , draggableRelativeLayoutParams , webViewLayoutParams );
259
+ setUpParentRelativeLayout (context );
244
260
createPopupWindow (parentRelativeLayout );
245
261
246
262
if (messageController != null ) {
@@ -275,6 +291,10 @@ private void createPopupWindow(@NonNull RelativeLayout parentRelativeLayout) {
275
291
case BOTTOM_BANNER :
276
292
gravity = Gravity .CENTER_HORIZONTAL | Gravity .BOTTOM ;
277
293
break ;
294
+ case CENTER_MODAL :
295
+ case FULL_SCREEN :
296
+ gravity = Gravity .CENTER_HORIZONTAL ;
297
+ break ;
278
298
}
279
299
}
280
300
@@ -293,7 +313,7 @@ private void createPopupWindow(@NonNull RelativeLayout parentRelativeLayout) {
293
313
);
294
314
}
295
315
296
- private void setUpParentLinearLayout (Context context ) {
316
+ private void setUpParentRelativeLayout (Context context ) {
297
317
parentRelativeLayout = new RelativeLayout (context );
298
318
parentRelativeLayout .setBackgroundDrawable (new ColorDrawable (Color .TRANSPARENT ));
299
319
parentRelativeLayout .setClipChildren (false );
@@ -302,11 +322,11 @@ private void setUpParentLinearLayout(Context context) {
302
322
}
303
323
304
324
private void setUpDraggableLayout (final Context context ,
305
- LinearLayout .LayoutParams linearLayoutParams ,
325
+ RelativeLayout .LayoutParams relativeLayoutParams ,
306
326
DraggableRelativeLayout .Params draggableParams ) {
307
327
draggableRelativeLayout = new DraggableRelativeLayout (context );
308
- if (linearLayoutParams != null )
309
- draggableRelativeLayout .setLayoutParams (linearLayoutParams );
328
+ if (relativeLayoutParams != null )
329
+ draggableRelativeLayout .setLayoutParams (relativeLayoutParams );
310
330
draggableRelativeLayout .setParams (draggableParams );
311
331
draggableRelativeLayout .setListener (new DraggableRelativeLayout .DraggableListener () {
312
332
@ Override
@@ -335,7 +355,7 @@ public void onDragEnd() {
335
355
cardView .setTag (IN_APP_MESSAGE_CARD_VIEW_TAG );
336
356
cardView .addView (webView );
337
357
338
- draggableRelativeLayout .setPadding (MARGIN_PX_SIZE , MARGIN_PX_SIZE , MARGIN_PX_SIZE , MARGIN_PX_SIZE );
358
+ draggableRelativeLayout .setPadding (marginPxSizeLeft , marginPxSizeTop , marginPxSizeRight , marginPxSizeBottom );
339
359
draggableRelativeLayout .setClipChildren (false );
340
360
draggableRelativeLayout .setClipToPadding (false );
341
361
draggableRelativeLayout .addView (cardView );
@@ -385,7 +405,7 @@ private CardView createCardView(Context context) {
385
405
* Schedule dismiss behavior, if IAM has a dismiss after X number of seconds timer.
386
406
*/
387
407
private void startDismissTimerIfNeeded () {
388
- if (dismissDuration <= 0 )
408
+ if (displayDuration <= 0 )
389
409
return ;
390
410
391
411
if (scheduleDismissRunnable != null )
@@ -405,7 +425,7 @@ public void run() {
405
425
}
406
426
}
407
427
};
408
- handler .postDelayed (scheduleDismissRunnable , (long ) dismissDuration * 1_000 );
428
+ handler .postDelayed (scheduleDismissRunnable , (long ) displayDuration * 1_000 );
409
429
}
410
430
411
431
// Do not add view until activity is ready
@@ -543,7 +563,7 @@ private void animateTop(View messageView, int height, Animation.AnimationListene
543
563
// Animate the message view from above the screen downward to the top
544
564
OneSignalAnimate .animateViewByTranslation (
545
565
messageView ,
546
- -height - MARGIN_PX_SIZE ,
566
+ -height - marginPxSizeTop ,
547
567
0f ,
548
568
IN_APP_BANNER_ANIMATION_DURATION_MS ,
549
569
new OneSignalBounceInterpolator (0.1 , 8.0 ),
@@ -555,7 +575,7 @@ private void animateBottom(View messageView, int height, Animation.AnimationList
555
575
// Animate the message view from under the screen upward to the bottom
556
576
OneSignalAnimate .animateViewByTranslation (
557
577
messageView ,
558
- height + MARGIN_PX_SIZE ,
578
+ height + marginPxSizeBottom ,
559
579
0f ,
560
580
IN_APP_BANNER_ANIMATION_DURATION_MS ,
561
581
new OneSignalBounceInterpolator (0.1 , 8.0 ),
@@ -618,7 +638,7 @@ public String toString() {
618
638
"currentActivity=" + currentActivity +
619
639
", pageWidth=" + pageWidth +
620
640
", pageHeight=" + pageHeight +
621
- ", dismissDuration =" + dismissDuration +
641
+ ", displayDuration =" + displayDuration +
622
642
", hasBackground=" + hasBackground +
623
643
", shouldDismissWhenActive=" + shouldDismissWhenActive +
624
644
", isDragging=" + isDragging +
0 commit comments