Skip to content

Commit c1d6f13

Browse files
authored
Add actionNamed handler. Run dismiss action in templates. (#460)
1 parent 362842a commit c1d6f13

File tree

8 files changed

+72
-3
lines changed

8 files changed

+72
-3
lines changed

AndroidSDKCore/src/main/java/com/leanplum/ActionContext.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014, Leanplum, Inc. All rights reserved.
2+
* Copyright 2021, 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
@@ -24,6 +24,7 @@
2424
import androidx.annotation.NonNull;
2525
import android.text.TextUtils;
2626

27+
import com.leanplum.callbacks.ActionCallback;
2728
import com.leanplum.callbacks.VariablesChangedCallback;
2829
import com.leanplum.internal.ActionManager;
2930
import com.leanplum.internal.BaseActionContext;
@@ -33,7 +34,6 @@
3334
import com.leanplum.internal.JsonConverter;
3435
import com.leanplum.internal.LeanplumInternal;
3536
import com.leanplum.internal.Log;
36-
import com.leanplum.internal.Util;
3737
import com.leanplum.internal.VarCache;
3838

3939
import org.json.JSONException;
@@ -57,6 +57,7 @@ public class ActionContext extends BaseActionContext implements Comparable<Actio
5757
private String key;
5858
private boolean preventRealtimeUpdating = false;
5959
private ContextualValues contextualValues;
60+
private ActionCallback actionNamedHandler;
6061

6162
public static class ContextualValues {
6263
/**
@@ -351,12 +352,27 @@ private Map<String, Object> getChildArgs(String name) {
351352
return actionArgs;
352353
}
353354

355+
public void setActionNamedHandler(ActionCallback handler) {
356+
actionNamedHandler = handler;
357+
}
358+
359+
private void triggerActionNamedHandler(String name, Map<String, Object> args) {
360+
if (actionNamedHandler != null) {
361+
ActionContext actionContext = new ActionContext(name, args, messageId);
362+
actionContext.parentContext = this;
363+
actionNamedHandler.onResponse(actionContext);
364+
}
365+
}
366+
354367
public void runActionNamed(String name) {
355368
if (TextUtils.isEmpty(name)) {
356369
Log.e("runActionNamed - Invalid name parameter provided.");
357370
return;
358371
}
359372
Map<String, Object> args = getChildArgs(name);
373+
374+
triggerActionNamedHandler(name, args);
375+
360376
if (args == null) {
361377
return;
362378
}
@@ -613,4 +629,8 @@ public static JSONObject mapToJsonObject(Map<String, ?> map) throws JSONExceptio
613629
public static <T> Map<String, T> mapFromJson(JSONObject jsonObject) throws JSONException {
614630
return JsonConverter.mapFromJson(jsonObject);
615631
}
632+
633+
public ActionContext getParentContext() {
634+
return parentContext;
635+
}
616636
}

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/controllers/AbstractPopupController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public BaseMessageOptions getOptions() {
6262
return options;
6363
}
6464

65+
@Override
66+
protected void runDismissAction() {
67+
options.dismiss();
68+
}
69+
6570
@Override
6671
void addMessageChildViews(RelativeLayout parent) {
6772
ImageView image = createBackgroundImageView(activity);

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/controllers/BaseController.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ private RelativeLayout createMessageView() {
117117
*/
118118
abstract void applyWindowDecoration();
119119

120+
/**
121+
* Calls ActionContext.runActionNamed for dismiss action.
122+
*/
123+
protected abstract void runDismissAction();
124+
125+
@Override
126+
public void onBackPressed() {
127+
runDismissAction();
128+
super.onBackPressed();
129+
}
130+
120131
@Override
121132
public void cancel() {
122133
if (isClosing) {
@@ -158,7 +169,10 @@ private CloseButton createCloseButton(View alignView) {
158169
closeLayout.setMargins(0, -SizeUtil.dp7, -SizeUtil.dp7, 0);
159170
}
160171
closeButton.setLayoutParams(closeLayout);
161-
closeButton.setOnClickListener(clickedView -> cancel());
172+
closeButton.setOnClickListener(clickedView -> {
173+
runDismissAction();
174+
cancel();
175+
});
162176
return closeButton;
163177
}
164178

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/controllers/RichHtmlController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ boolean isFullscreen() {
7979
return richOptions.isFullScreen();
8080
}
8181

82+
@Override
83+
protected void runDismissAction() {
84+
// need to check state, because touches from banner sometimes trigger more than 1 action
85+
if (!isClosing) {
86+
richOptions.dismiss();
87+
}
88+
}
89+
8290
@Override
8391
protected void applyWindowDecoration() {
8492
if (isFullscreen())
@@ -242,6 +250,7 @@ private boolean handleOpenEvent(String url) {
242250

243251
private boolean handleCloseEvent(String url) {
244252
if (url.contains(richOptions.getCloseUrl())) {
253+
runDismissAction();
245254
cancel();
246255
String queryComponentsFromUrl = queryComponentsFromUrl(url, "result");
247256
if (!TextUtils.isEmpty(queryComponentsFromUrl)) {
@@ -411,6 +420,7 @@ public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
411420

412421
if (ev.getY() < top || ev.getY() > bottom || ev.getX() < left || ev.getX() > right) {
413422
if (richOptions.isHtmlTabOutsideToClose()) {
423+
runDismissAction();
414424
cancel();
415425
}
416426
activity.dispatchTouchEvent(ev);

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/controllers/WebInterstitialController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ protected boolean hasDismissButton() {
5656
return webOptions.hasDismissButton();
5757
}
5858

59+
@Override
60+
protected void runDismissAction() {
61+
webOptions.dismiss();
62+
}
63+
5964
@Override
6065
boolean isFullscreen() {
6166
return true;
@@ -106,6 +111,7 @@ public boolean shouldOverrideUrlLoading(WebView wView, String url) {
106111

107112
private boolean handleCloseEvent(String url) {
108113
if (url.contains(webOptions.getCloseUrl())) {
114+
runDismissAction();
109115
cancel();
110116
String[] urlComponents = url.split("\\?");
111117
if (urlComponents.length > 1) {

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/options/BaseMessageOptions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ public void accept() {
155155
context.runTrackedActionNamed(Args.ACCEPT_ACTION);
156156
}
157157

158+
public void dismiss() {
159+
context.runActionNamed(Args.DISMISS_ACTION);
160+
}
161+
158162
public static ActionArgs toArgs(Context currentContext) {
159163
return new ActionArgs()
160164
.with(Args.TITLE_TEXT, Util.getApplicationName(currentContext))

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/options/RichHtmlOptions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,8 @@ public boolean isBannerWithTapOutsideFalse() {
372372
String templateName = getActionContext().getArgs().get(Values.HTML_TEMPLATE_PREFIX).toString();
373373
return templateName.toLowerCase().contains("banner") && !isHtmlTabOutsideToClose();
374374
}
375+
376+
public void dismiss() {
377+
actionContext.runActionNamed(Args.DISMISS_ACTION);
378+
}
375379
}

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/options/WebInterstitialOptions.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ public class WebInterstitialOptions {
3636
private String url;
3737
private String closeUrl;
3838
private boolean hasDismissButton;
39+
private ActionContext actionContext;
3940

4041
public WebInterstitialOptions(ActionContext context) {
4142
this.setUrl(context.stringNamed(Args.URL));
4243
this.setHasDismissButton(context.booleanNamed(Args.HAS_DISMISS_BUTTON));
4344
this.setCloseUrl(context.stringNamed(Args.CLOSE_URL));
45+
this.actionContext = context;
4446
}
4547

4648
public String getUrl() {
@@ -67,6 +69,10 @@ private void setCloseUrl(String closeUrl) {
6769
this.closeUrl = closeUrl;
6870
}
6971

72+
public void dismiss() {
73+
actionContext.runActionNamed(Args.DISMISS_ACTION);
74+
}
75+
7076
public static ActionArgs toArgs() {
7177
return new ActionArgs()
7278
.with(Args.URL, Values.DEFAULT_URL)

0 commit comments

Comments
 (0)