Skip to content

Commit 0447c33

Browse files
authored
E2 1702 banner fix interactable background (#339)
* intermediate * update window to not fullscreen * cleanup a little * make dissmissoutside work * minor changes * remove extra line * add back 2 lines * add check for htmlOptions null * remove extra line * rename to isBannerWithTapOutsideFalse
1 parent 050513f commit 0447c33

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

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

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333
import android.graphics.drawable.shapes.Shape;
3434
import android.os.Build;
3535
import android.os.Handler;
36+
import android.text.Layout;
3637
import android.text.TextUtils;
3738
import android.util.TypedValue;
3839
import android.view.Gravity;
3940
import android.view.MotionEvent;
4041
import android.view.View;
42+
import android.view.ViewGroup;
4143
import android.view.ViewGroup.LayoutParams;
4244
import android.view.Window;
4345
import android.view.WindowManager;
@@ -56,6 +58,7 @@
5658
import com.leanplum.ActionContext;
5759
import com.leanplum.Leanplum;
5860
import com.leanplum.core.R;
61+
import com.leanplum.internal.Log;
5962
import com.leanplum.utils.BitmapUtil;
6063
import com.leanplum.utils.SizeUtil;
6164
import com.leanplum.views.BackgroundImageView;
@@ -113,7 +116,6 @@ protected BaseMessageDialog(Activity activity, boolean fullscreen, BaseMessageOp
113116
CloseButton closeButton = createCloseButton(activity, fullscreen, view);
114117
dialogView.addView(closeButton, closeButton.getLayoutParams());
115118
}
116-
117119
setContentView(dialogView, dialogView.getLayoutParams());
118120

119121
dialogView.setAnimation(createFadeInAnimation());
@@ -130,16 +132,51 @@ protected BaseMessageDialog(Activity activity, boolean fullscreen, BaseMessageOp
130132
}
131133
} else {
132134
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
133-
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
134-
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
135+
136+
if (htmlOptions != null && isBannerWithTapOutsideFalse(htmlOptions)) {
137+
// banners need to be positioned at the top manually
138+
// (unless they get repositioned to the bottom later)
139+
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
140+
window.setGravity(Gravity.TOP);
141+
142+
// use the html y offset to determine the y location of the window; this is different
143+
// from non-banners because we don't want to make the window too big (e.g. via a margin
144+
// in the layout) and block other things on the screen (e.g. dialogs)
145+
WindowManager.LayoutParams windowLayoutParams = window.getAttributes();
146+
windowLayoutParams.y = htmlOptions.getHtmlYOffset(activity);
147+
window.setAttributes(windowLayoutParams);
148+
149+
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
150+
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
151+
} else {
152+
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
153+
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
154+
}
155+
135156
if (htmlOptions != null &&
136157
MessageTemplates.Args.HTML_ALIGN_BOTTOM.equals(htmlOptions.getHtmlAlign())) {
137-
dialogView.setGravity(Gravity.BOTTOM);
158+
if (isBannerWithTapOutsideFalse(htmlOptions)) {
159+
window.setGravity(Gravity.BOTTOM);
160+
} else {
161+
dialogView.setGravity(Gravity.BOTTOM);
162+
}
138163
}
139164
}
140165
}
141166
}
142167

168+
/**
169+
* Banners with property TabOutsideToClose = false need to be treated differently
170+
* so they do not block interaction with other dialogs and the keyboard.
171+
* Banners with property TabOutsideToClose = true do not need to be treated this way.
172+
* The original way banners worked was fine because they need to be aware of any touch events
173+
* in its container window
174+
*/
175+
protected static boolean isBannerWithTapOutsideFalse(HTMLOptions htmlOptions) {
176+
String templateName = htmlOptions.getActionContext().getArgs().get("__file__Template").toString();
177+
return templateName.toLowerCase().contains("banner") && !htmlOptions.isHtmlTabOutsideToClose();
178+
}
179+
143180
@Override
144181
public void onWindowFocusChanged(boolean hasFocus) {
145182
try {
@@ -260,10 +297,12 @@ private RelativeLayout createContainerView(Activity context, boolean fullscreen)
260297

261298
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
262299
int htmlYOffset = htmlOptions.getHtmlYOffset(context);
263-
if (MessageTemplates.Args.HTML_ALIGN_BOTTOM.equals(htmlOptions.getHtmlAlign())) {
264-
layoutParams.bottomMargin = htmlYOffset;
265-
} else {
266-
layoutParams.topMargin = htmlYOffset;
300+
if (!isBannerWithTapOutsideFalse(htmlOptions)) {
301+
if (MessageTemplates.Args.HTML_ALIGN_BOTTOM.equals(htmlOptions.getHtmlAlign())) {
302+
layoutParams.bottomMargin = htmlYOffset;
303+
} else {
304+
layoutParams.topMargin = htmlYOffset;
305+
}
267306
}
268307
} else {
269308
// Make sure the dialog fits on screen.

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@
2222
package com.leanplum.messagetemplates;
2323

2424
import android.app.Activity;
25+
import android.app.AlertDialog;
26+
import android.content.DialogInterface;
2527
import android.graphics.Point;
2628
import androidx.annotation.NonNull;
27-
import android.util.Log;
29+
import android.view.Gravity;
2830
import android.view.MotionEvent;
31+
import android.view.WindowManager;
2932

3033
import com.leanplum.ActionContext;
3134
import com.leanplum.Leanplum;
3235
import com.leanplum.LeanplumActivityHelper;
3336
import com.leanplum.callbacks.ActionCallback;
3437
import com.leanplum.callbacks.PostponableAction;
3538
import com.leanplum.callbacks.VariablesChangedCallback;
39+
import com.leanplum.internal.Log;
3640
import com.leanplum.utils.SizeUtil;
3741

3842
/**
@@ -52,6 +56,10 @@ public HTMLTemplate(Activity activity, HTMLOptions htmlOptions) {
5256
@Override
5357
public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
5458
if (!htmlOptions.isFullScreen()) {
59+
if (isBannerWithTapOutsideFalse(htmlOptions)) {
60+
return super.dispatchTouchEvent(ev);
61+
}
62+
5563
Point size = SizeUtil.getDisplaySize(activity);
5664
int dialogWidth = webView.getWidth();
5765
int left = (size.x - dialogWidth) / 2;

0 commit comments

Comments
 (0)