Skip to content

Commit ee1b00f

Browse files
hborisoffmilos1290
andauthored
Refactor IAM templates (part 1) (#409)
* Split base class to all others. Add PopupMessage base class for Interstitial and CenterPopup. * Move common code to BaseMessageDialog. Little fixes. * Move code to ViewUtils.java * Remove fullscreen variable. Improve background View. * Little fixes. * Improve HTMLTemplate readability. * Fix unit tests. Co-authored-by: Milos Jakovljevic <[email protected]>
1 parent d9b2b5d commit ee1b00f

File tree

14 files changed

+833
-625
lines changed

14 files changed

+833
-625
lines changed

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/BaseMessageDialog.java

Lines changed: 68 additions & 587 deletions
Large diffs are not rendered by default.

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/CenterPopup.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014, Leanplum, Inc. All rights reserved.
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file
@@ -23,25 +23,72 @@
2323

2424
import android.app.Activity;
2525
import android.content.Context;
26-
26+
import android.graphics.Point;
27+
import android.os.Build;
28+
import android.view.Window;
29+
import android.view.WindowManager;
30+
import android.widget.RelativeLayout;
2731
import com.leanplum.ActionContext;
2832
import com.leanplum.Leanplum;
2933
import com.leanplum.LeanplumActivityHelper;
3034
import com.leanplum.callbacks.ActionCallback;
3135
import com.leanplum.callbacks.PostponableAction;
3236
import com.leanplum.callbacks.VariablesChangedCallback;
37+
import com.leanplum.utils.SizeUtil;
3338

3439
/**
3540
* Registers a Leanplum action that displays a custom center popup dialog.
3641
*
3742
* @author Andrew First
3843
*/
39-
public class CenterPopup extends BaseMessageDialog {
44+
public class CenterPopup extends PopupMessageTemplate {
4045
private static final String NAME = "Center Popup";
4146

42-
public CenterPopup(Activity activity, CenterPopupOptions options) {
43-
super(activity, false, options, null, null);
44-
this.options = options;
47+
CenterPopup(Activity activity, CenterPopupOptions options) {
48+
super(activity, options);
49+
}
50+
51+
@Override
52+
boolean isFullscreen() {
53+
return false;
54+
}
55+
56+
@Override
57+
protected void applyWindowDecoration() {
58+
Window window = getWindow();
59+
if (window == null) {
60+
return;
61+
}
62+
window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
63+
if (Build.VERSION.SDK_INT >= 14) {
64+
window.setDimAmount(0.7f);
65+
}
66+
}
67+
68+
@Override
69+
protected RelativeLayout.LayoutParams createLayoutParams() {
70+
RelativeLayout.LayoutParams layoutParams;
71+
72+
// Make sure the dialog fits on screen.
73+
Point size = SizeUtil.getDisplaySize(activity);
74+
int width = SizeUtil.dpToPx(activity, ((CenterPopupOptions) options).getWidth());
75+
int height = SizeUtil.dpToPx(activity, ((CenterPopupOptions) options).getHeight());
76+
77+
int maxWidth = size.x - SizeUtil.dp20;
78+
int maxHeight = size.y - SizeUtil.dp20;
79+
double aspectRatio = width / (double) height;
80+
if (width > maxWidth && (int) (width / aspectRatio) < maxHeight) {
81+
width = maxWidth;
82+
height = (int) (width / aspectRatio);
83+
}
84+
if (height > maxHeight && (int) (height * aspectRatio) < maxWidth) {
85+
height = maxHeight;
86+
width = (int) (height * aspectRatio);
87+
}
88+
89+
layoutParams = new RelativeLayout.LayoutParams(width, height);
90+
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
91+
return layoutParams;
4592
}
4693

4794
public static void register(Context currentContext) {

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/HTMLOptions.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017, Leanplum, Inc. All rights reserved.
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file
@@ -273,6 +273,10 @@ String getHtmlAlign() {
273273
return htmlAlign;
274274
}
275275

276+
boolean isHtmlAlignBottom() {
277+
return MessageTemplates.Args.HTML_ALIGN_BOTTOM.equals(getHtmlAlign());
278+
}
279+
276280
private void setHtmlAlign(String htmlAlign) {
277281
this.htmlAlign = htmlAlign;
278282
}
@@ -350,4 +354,16 @@ static class Size {
350354
int value;
351355
String type;
352356
}
357+
358+
/**
359+
* Banners with property TabOutsideToClose = false need to be treated differently
360+
* so they do not block interaction with other dialogs and the keyboard.
361+
* Banners with property TabOutsideToClose = true do not need to be treated this way.
362+
* The original way banners worked was fine because they need to be aware of any touch events
363+
* in its container window
364+
*/
365+
public boolean isBannerWithTapOutsideFalse() {
366+
String templateName = getActionContext().getArgs().get("__file__Template").toString();
367+
return templateName.toLowerCase().contains("banner") && !isHtmlTabOutsideToClose();
368+
}
353369
}

0 commit comments

Comments
 (0)