Skip to content

Commit 7d832dc

Browse files
committed
feat(message): Tap outside to close Html in-app.
1 parent 6231b40 commit 7d832dc

File tree

5 files changed

+81
-42
lines changed

5 files changed

+81
-42
lines changed

AndroidSDK/src/com/leanplum/messagetemplates/BaseMessageDialog.java

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import android.os.Handler;
3636
import android.text.TextUtils;
3737
import android.util.TypedValue;
38-
import android.view.Display;
3938
import android.view.Gravity;
4039
import android.view.MotionEvent;
4140
import android.view.View;
@@ -232,25 +231,6 @@ public void onClick(View arg0) {
232231
return closeButton;
233232
}
234233

235-
/**
236-
* Gets the size of display in pixels.
237-
*
238-
* @param context Current activity.
239-
* @return A Point object with display size information.
240-
*/
241-
private Point getDisplaySize(Activity context) {
242-
Point size = new Point();
243-
if (context == null) {
244-
return size;
245-
}
246-
try {
247-
Display display = context.getWindowManager().getDefaultDisplay();
248-
display.getSize(size);
249-
} catch (Throwable ignored) {
250-
}
251-
return size;
252-
}
253-
254234
@SuppressWarnings("deprecation")
255235
private RelativeLayout createContainerView(Activity context, boolean fullscreen) {
256236
RelativeLayout view = new RelativeLayout(context);
@@ -269,7 +249,7 @@ private RelativeLayout createContainerView(Activity context, boolean fullscreen)
269249
} else {
270250
int width = htmlWidth.value;
271251
if ("%".equals(htmlWidth.type)) {
272-
Point size = getDisplaySize(context);
252+
Point size = SizeUtil.getDisplaySize(context);
273253
width = size.x * width / 100;
274254
} else {
275255
width = SizeUtil.dpToPx(context, width);
@@ -278,24 +258,15 @@ private RelativeLayout createContainerView(Activity context, boolean fullscreen)
278258
}
279259

280260
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
281-
HTMLOptions.Size offset = htmlOptions.getHtmlYOffset();
282-
if (offset != null && !TextUtils.isEmpty(offset.type)) {
283-
int htmlYOffset = offset.value;
284-
if ("%".equals(offset.type)) {
285-
Point size = getDisplaySize(context);
286-
htmlYOffset = (size.y - SizeUtil.getStatusBarHeight(context)) * htmlYOffset / 100;
287-
} else {
288-
htmlYOffset = SizeUtil.dpToPx(context, htmlYOffset);
289-
}
290-
if ("Top".equals(htmlOptions.getHtmlAlign())) {
291-
layoutParams.topMargin = htmlYOffset;
292-
} else {
293-
layoutParams.bottomMargin = htmlYOffset;
294-
}
261+
int htmlYOffset = htmlOptions.getHtmlYOffset(context);
262+
if (MessageTemplates.Args.HTML_ALIGN_BOTTOM.equals(htmlOptions.getHtmlAlign())) {
263+
layoutParams.bottomMargin = htmlYOffset;
264+
} else {
265+
layoutParams.topMargin = htmlYOffset;
295266
}
296267
} else {
297268
// Make sure the dialog fits on screen.
298-
Point size = getDisplaySize(context);
269+
Point size = SizeUtil.getDisplaySize(context);
299270
int width = SizeUtil.dpToPx(context, ((CenterPopupOptions) options).getWidth());
300271
int height = SizeUtil.dpToPx(context, ((CenterPopupOptions) options).getHeight());
301272

AndroidSDK/src/com/leanplum/messagetemplates/HTMLOptions.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121

2222
package com.leanplum.messagetemplates;
2323

24+
import android.app.Activity;
25+
import android.graphics.Point;
2426
import android.text.TextUtils;
2527
import android.util.Log;
2628

2729
import com.leanplum.ActionArgs;
2830
import com.leanplum.ActionContext;
2931
import com.leanplum.Leanplum;
32+
import com.leanplum.utils.SizeUtil;
3033

3134
import org.json.JSONException;
3235

@@ -54,6 +57,7 @@ class HTMLOptions {
5457
private int htmlHeight;
5558
private Size htmlWidth;
5659
private Size htmlYOffset;
60+
private boolean htmlTabOutsideToClose;
5761

5862
HTMLOptions(ActionContext context) {
5963
this.setActionContext(context);
@@ -67,6 +71,8 @@ class HTMLOptions {
6771
this.setHtmlHeight(context.numberNamed(MessageTemplates.Args.HTML_HEIGHT).intValue());
6872
this.setHtmlWidth(context.stringNamed(MessageTemplates.Args.HTML_WIDTH));
6973
this.setHtmlYOffset(context.stringNamed(MessageTemplates.Args.HTML_Y_OFFSET));
74+
this.setHtmlTabOutsideToClose(context.booleanNamed(
75+
MessageTemplates.Args.HTML_TAP_OUTSIDE_TO_CLOSE));
7076
}
7177

7278
/**
@@ -206,8 +212,23 @@ private void setHtmlWidth(String htmlWidth) {
206212
this.htmlWidth = getSizeValueAndType(htmlWidth);
207213
}
208214

209-
Size getHtmlYOffset() {
210-
return htmlYOffset;
215+
//Gets html y offset in pixels.
216+
int getHtmlYOffset(Activity context) {
217+
int yOffset = 0;
218+
if (context == null) {
219+
return yOffset;
220+
}
221+
222+
if (htmlYOffset != null && !TextUtils.isEmpty(htmlYOffset.type)) {
223+
yOffset = htmlYOffset.value;
224+
if ("%".equals(htmlYOffset.type)) {
225+
Point size = SizeUtil.getDisplaySize(context);
226+
yOffset = (size.y - SizeUtil.getStatusBarHeight(context)) * yOffset / 100;
227+
} else {
228+
yOffset = SizeUtil.dpToPx(context, yOffset);
229+
}
230+
}
231+
return yOffset;
211232
}
212233

213234
private void setHtmlYOffset(String htmlYOffset) {
@@ -236,6 +257,14 @@ private Size getSizeValueAndType(String stringValue) {
236257
return out;
237258
}
238259

260+
boolean isHtmlTabOutsideToClose() {
261+
return htmlTabOutsideToClose;
262+
}
263+
264+
private void setHtmlTabOutsideToClose(boolean htmlTabOutsideToClose) {
265+
this.htmlTabOutsideToClose = htmlTabOutsideToClose;
266+
}
267+
239268
private void setHtmlHeight(int htmlHeight) {
240269
this.htmlHeight = htmlHeight;
241270
}

AndroidSDK/src/com/leanplum/messagetemplates/HTMLTemplate.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
package com.leanplum.messagetemplates;
2323

2424
import android.app.Activity;
25+
import android.content.Context;
26+
import android.graphics.Point;
2527
import android.support.annotation.NonNull;
2628
import android.util.Log;
2729
import android.view.MotionEvent;
@@ -51,12 +53,27 @@ public HTMLTemplate(Activity activity, HTMLOptions htmlOptions) {
5153
@Override
5254
public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
5355
if (!htmlOptions.isFullScreen()) {
56+
Point size = SizeUtil.getDisplaySize(activity);
57+
int dialogWidth = webView.getWidth();
58+
int left = (size.x - dialogWidth) / 2;
59+
int right = (size.x + dialogWidth) / 2;
5460
int height = SizeUtil.dpToPx(Leanplum.getContext(), htmlOptions.getHtmlHeight());
5561
int statusBarHeight = SizeUtil.getStatusBarHeight(Leanplum.getContext());
56-
if (htmlOptions.getHtmlAlign().equals(MessageTemplates.Args.HTML_ALIGN_TOP) && ev.getY()
57-
> height + statusBarHeight ||
58-
htmlOptions.getHtmlAlign().equals(MessageTemplates.Args.HTML_ALIGN_BOTTOM) && ev.getY()
59-
< dialogView.getHeight() + statusBarHeight - height) {
62+
int htmlYOffset = htmlOptions.getHtmlYOffset(activity);
63+
int top;
64+
int bottom;
65+
if (MessageTemplates.Args.HTML_ALIGN_BOTTOM.equals(htmlOptions.getHtmlAlign())) {
66+
top = size.y - height - statusBarHeight - htmlYOffset;
67+
bottom = size.y - htmlYOffset - statusBarHeight;
68+
} else {
69+
top = htmlYOffset + statusBarHeight;
70+
bottom = height + statusBarHeight + htmlYOffset;
71+
}
72+
73+
if (ev.getY() < top || ev.getY() > bottom || ev.getX() < left || ev.getX() > right) {
74+
if (htmlOptions.isHtmlTabOutsideToClose()) {
75+
cancel();
76+
}
6077
activity.dispatchTouchEvent(ev);
6178
}
6279
}

AndroidSDK/src/com/leanplum/messagetemplates/MessageTemplates.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static class Args {
5858
static final String HTML_WIDTH = "HTML Width";
5959
static final String HTML_HEIGHT = "HTML Height";
6060
static final String HTML_Y_OFFSET = "HTML Y Offset";
61+
static final String HTML_TAP_OUTSIDE_TO_CLOSE = "Tap Outside to Close";
6162
static final String HTML_ALIGN = "HTML Align";
6263
static final String HTML_ALIGN_TOP = "Top";
6364
static final String HTML_ALIGN_BOTTOM = "Bottom";

AndroidSDK/src/com/leanplum/utils/SizeUtil.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
import android.app.Activity;
2525
import android.content.Context;
26+
import android.graphics.Point;
2627
import android.util.DisplayMetrics;
28+
import android.view.Display;
2729
import android.view.WindowManager;
2830

2931
/**
@@ -127,4 +129,23 @@ public static int getStatusBarHeight(Context context) {
127129
}
128130
return result;
129131
}
132+
133+
/**
134+
* Gets the size of display in pixels.
135+
*
136+
* @param context Current activity.
137+
* @return A Point object with display size information.
138+
*/
139+
public static Point getDisplaySize(Activity context) {
140+
Point size = new Point();
141+
if (context == null) {
142+
return size;
143+
}
144+
try {
145+
Display display = context.getWindowManager().getDefaultDisplay();
146+
display.getSize(size);
147+
} catch (Throwable ignored) {
148+
}
149+
return size;
150+
}
130151
}

0 commit comments

Comments
 (0)