Skip to content

Commit 9662fce

Browse files
authored
Improve MessageTemplates public methods (#426)
* Improve MessageTemplates public methods * Add waitFilesAndVariables method to interface * Fix CloseButton for external usage * Fix method accessibility
1 parent 4e17065 commit 9662fce

File tree

11 files changed

+235
-107
lines changed

11 files changed

+235
-107
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package com.leanplum;
2323

2424
import com.leanplum.internal.ActionArg;
25+
import com.leanplum.internal.Constants;
2526
import com.leanplum.internal.Log;
2627

2728
import java.util.ArrayList;
@@ -118,4 +119,16 @@ public ActionArgs withAction(String name, String defaultValue) {
118119
List<ActionArg<?>> getValue() {
119120
return args;
120121
}
122+
123+
/**
124+
* Checks if there is at least one file argument.
125+
*/
126+
public boolean containsFile() {
127+
for (ActionArg<?> arg : args) {
128+
if (Constants.Kinds.FILE.equals(arg.kind())) {
129+
return true;
130+
}
131+
}
132+
return false;
133+
}
121134
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package com.leanplum.messagetemplates;
23+
24+
import android.content.Context;
25+
import androidx.annotation.NonNull;
26+
import com.leanplum.ActionArgs;
27+
import com.leanplum.ActionContext;
28+
29+
/**
30+
* Wraps action name, arguments and handler.
31+
*/
32+
public interface MessageTemplate {
33+
34+
/**
35+
* Returns the name of the action to register.
36+
*/
37+
@NonNull
38+
String getName();
39+
40+
/**
41+
* Creates the user-customizable options for the action.
42+
*
43+
* @param context Android context
44+
*/
45+
@NonNull
46+
ActionArgs createActionArgs(Context context);
47+
48+
/**
49+
* Called in response to the registered action.
50+
* Use it to show your message to the user.
51+
*
52+
* @param context The context in which an action or message is executed.
53+
*/
54+
void handleAction(ActionContext context);
55+
56+
/**
57+
* If your custom template depends on files or variables, that will be downloaded, override and
58+
* return true.
59+
*
60+
* @return true to wait for files and variables to finish downloading before calling
61+
* {@link #handleAction(ActionContext)}.
62+
*/
63+
default boolean waitFilesAndVariables() {
64+
return false;
65+
}
66+
}

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

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

2424
import android.content.Context;
25+
import androidx.annotation.NonNull;
2526
import com.leanplum.ActionArgs;
2627
import com.leanplum.ActionContext;
2728
import com.leanplum.Leanplum;
@@ -43,43 +44,26 @@
4344
* @author Andrew First
4445
*/
4546
public class MessageTemplates {
46-
private static final String ALERT = "Alert";
47-
private static final String CENTER_POPUP = "Center Popup";
48-
private static final String CONFIRM = "Confirm";
49-
private static final String HTML = "HTML";
50-
private static final String INTERSTITIAL = "Interstitial";
51-
private static final String OPEN_URL = "Open URL";
52-
private static final String WEB_INTERSTITIAL = "Web Interstitial";
5347

5448
private static boolean registered = false;
5549

56-
@FunctionalInterface
57-
private interface MessageFactory {
58-
void showMessage(ActionContext context);
59-
}
60-
61-
@FunctionalInterface
62-
private interface ActionArgsFactory {
63-
ActionArgs createActionArgs(Context context);
64-
}
65-
66-
private static ActionCallback createCallback(MessageFactory messageFactory) {
50+
private static ActionCallback createCallback(@NonNull MessageTemplate template) {
6751
return new ActionCallback() {
6852
@Override
6953
public boolean onResponse(ActionContext actionContext) {
7054
LeanplumActivityHelper.queueActionUponActive(
7155
new PostponableAction() {
7256
@Override
7357
public void run() {
74-
messageFactory.showMessage(actionContext);
58+
template.handleAction(actionContext);
7559
}
7660
});
7761
return true;
7862
}
7963
};
8064
}
8165

82-
private static ActionCallback createCallbackWaitVarsAndFiles(MessageFactory messageFactory) {
66+
private static ActionCallback createCallbackWaitVarsAndFiles(@NonNull MessageTemplate template) {
8367
return new ActionCallback() {
8468
@Override
8569
public boolean onResponse(ActionContext actionContext) {
@@ -91,7 +75,7 @@ public void variablesChanged() {
9175
new PostponableAction() {
9276
@Override
9377
public void run() {
94-
messageFactory.showMessage(actionContext);
78+
template.handleAction(actionContext);
9579
}
9680
});
9781
}
@@ -101,32 +85,26 @@ public void run() {
10185
};
10286
}
10387

104-
private static void defineAction(
105-
String name,
106-
ActionArgsFactory argsFactory,
107-
MessageFactory messageFactory,
108-
Context context) {
109-
88+
/**
89+
* Registers a message template to respond to a given action.
90+
*
91+
* @param template Wrapper for action name, action arguments and handler.
92+
* @param context Android context
93+
*/
94+
public static void registerTemplate(
95+
@NonNull MessageTemplate template,
96+
@NonNull Context context) {
97+
98+
String name = template.getName();
11099
int kind = Leanplum.ACTION_KIND_MESSAGE | Leanplum.ACTION_KIND_ACTION;
100+
ActionArgs args = template.createActionArgs(context);
111101

112-
ActionArgs args = argsFactory.createActionArgs(context);
113-
114-
ActionCallback callback = createCallback(messageFactory);
115-
116-
Leanplum.defineAction(name, kind, args, callback);
117-
}
118-
119-
private static void defineActionWaitVarsAndFiles(
120-
String name,
121-
ActionArgsFactory argsFactory,
122-
MessageFactory messageFactory,
123-
Context context) {
124-
125-
int kind = Leanplum.ACTION_KIND_MESSAGE | Leanplum.ACTION_KIND_ACTION;
126-
127-
ActionArgs args = argsFactory.createActionArgs(context);
128-
129-
ActionCallback callback = createCallbackWaitVarsAndFiles(messageFactory);
102+
ActionCallback callback;
103+
if (template.waitFilesAndVariables() || args.containsFile()) { // checks args just in case
104+
callback = createCallbackWaitVarsAndFiles(template);
105+
} else {
106+
callback = createCallback(template);
107+
}
130108

131109
Leanplum.defineAction(name, kind, args, callback);
132110
}
@@ -137,46 +115,12 @@ public synchronized static void register(Context context) {
137115
}
138116
registered = true;
139117

140-
defineAction(
141-
OPEN_URL,
142-
OpenUrlAction::createActionArgs,
143-
OpenUrlAction::onActionTriggered,
144-
context);
145-
146-
defineAction(
147-
ALERT,
148-
AlertMessage::createActionArgs,
149-
AlertMessage::showMessage,
150-
context);
151-
152-
defineAction(
153-
CONFIRM,
154-
ConfirmMessage::createActionArgs,
155-
ConfirmMessage::showMessage,
156-
context);
157-
158-
defineActionWaitVarsAndFiles(
159-
CENTER_POPUP,
160-
CenterPopupMessage::createActionArgs,
161-
CenterPopupMessage::showMessage,
162-
context);
163-
164-
defineActionWaitVarsAndFiles(
165-
INTERSTITIAL,
166-
InterstitialMessage::createActionArgs,
167-
InterstitialMessage::showMessage,
168-
context);
169-
170-
defineAction(
171-
WEB_INTERSTITIAL,
172-
WebInterstitialMessage::createActionArgs,
173-
WebInterstitialMessage::showMessage,
174-
context);
175-
176-
defineActionWaitVarsAndFiles(
177-
HTML,
178-
RichHtmlMessage::createActionArgs,
179-
RichHtmlMessage::showMessage,
180-
context);
118+
registerTemplate(new OpenUrlAction(), context);
119+
registerTemplate(new AlertMessage(), context);
120+
registerTemplate(new ConfirmMessage(), context);
121+
registerTemplate(new CenterPopupMessage(), context);
122+
registerTemplate(new InterstitialMessage(), context);
123+
registerTemplate(new WebInterstitialMessage(), context);
124+
registerTemplate(new RichHtmlMessage(), context);
181125
}
182126
}

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/actions/AlertMessage.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import android.app.AlertDialog;
2626
import android.content.Context;
2727

28+
import androidx.annotation.NonNull;
2829
import com.leanplum.ActionArgs;
2930
import com.leanplum.ActionContext;
3031
import com.leanplum.LeanplumActivityHelper;
3132
import com.leanplum.internal.Util;
33+
import com.leanplum.messagetemplates.MessageTemplate;
3234
import com.leanplum.messagetemplates.MessageTemplateConstants.Args;
3335
import com.leanplum.messagetemplates.MessageTemplateConstants.Values;
3436

@@ -37,17 +39,27 @@
3739
*
3840
* @author Andrew First
3941
*/
40-
public class AlertMessage {
42+
public class AlertMessage implements MessageTemplate {
43+
private static final String ALERT = "Alert";
4144

42-
public static ActionArgs createActionArgs(Context context) {
45+
@NonNull
46+
@Override
47+
public String getName() {
48+
return ALERT;
49+
}
50+
51+
@NonNull
52+
@Override
53+
public ActionArgs createActionArgs(Context context) {
4354
return new ActionArgs()
4455
.with(Args.TITLE, Util.getApplicationName(context))
4556
.with(Args.MESSAGE, Values.ALERT_MESSAGE)
4657
.with(Args.DISMISS_TEXT, Values.OK_TEXT)
4758
.withAction(Args.DISMISS_ACTION, null);
4859
}
4960

50-
public static void showMessage(ActionContext context) {
61+
@Override
62+
public void handleAction(ActionContext context) {
5163
Activity activity = LeanplumActivityHelper.getCurrentActivity();
5264
if (activity == null || activity.isFinishing()) {
5365
return;

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/actions/CenterPopupMessage.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,31 @@
2323

2424
import android.app.Activity;
2525
import android.content.Context;
26+
import androidx.annotation.NonNull;
2627
import com.leanplum.ActionArgs;
2728
import com.leanplum.ActionContext;
2829
import com.leanplum.LeanplumActivityHelper;
30+
import com.leanplum.messagetemplates.MessageTemplate;
2931
import com.leanplum.messagetemplates.controllers.CenterPopupController;
3032
import com.leanplum.messagetemplates.options.CenterPopupOptions;
3133

32-
public class CenterPopupMessage {
33-
public static ActionArgs createActionArgs(Context context) {
34+
public class CenterPopupMessage implements MessageTemplate {
35+
private static final String CENTER_POPUP = "Center Popup";
36+
37+
@NonNull
38+
@Override
39+
public String getName() {
40+
return CENTER_POPUP;
41+
}
42+
43+
@NonNull
44+
@Override
45+
public ActionArgs createActionArgs(Context context) {
3446
return CenterPopupOptions.toArgs(context);
3547
}
3648

37-
public static void showMessage(ActionContext context) {
49+
@Override
50+
public void handleAction(ActionContext context) {
3851
Activity activity = LeanplumActivityHelper.getCurrentActivity();
3952
if (activity == null || activity.isFinishing()) {
4053
return;
@@ -44,4 +57,9 @@ public static void showMessage(ActionContext context) {
4457
CenterPopupController popup = new CenterPopupController(activity, options);
4558
popup.show();
4659
}
60+
61+
@Override
62+
public boolean waitFilesAndVariables() {
63+
return true;
64+
}
4765
}

AndroidSDKCore/src/main/java/com/leanplum/messagetemplates/actions/ConfirmMessage.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import android.app.AlertDialog;
2626
import android.content.Context;
2727

28+
import androidx.annotation.NonNull;
2829
import com.leanplum.ActionArgs;
2930
import com.leanplum.ActionContext;
3031
import com.leanplum.LeanplumActivityHelper;
3132
import com.leanplum.internal.Util;
33+
import com.leanplum.messagetemplates.MessageTemplate;
3234
import com.leanplum.messagetemplates.MessageTemplateConstants.Args;
3335
import com.leanplum.messagetemplates.MessageTemplateConstants.Values;
3436

@@ -37,9 +39,18 @@
3739
*
3840
* @author Andrew First
3941
*/
40-
public class ConfirmMessage {
42+
public class ConfirmMessage implements MessageTemplate {
43+
private static final String CONFIRM = "Confirm";
4144

42-
public static ActionArgs createActionArgs(Context context) {
45+
@NonNull
46+
@Override
47+
public String getName() {
48+
return CONFIRM;
49+
}
50+
51+
@NonNull
52+
@Override
53+
public ActionArgs createActionArgs(Context context) {
4354
return new ActionArgs()
4455
.with(Args.TITLE, Util.getApplicationName(context))
4556
.with(Args.MESSAGE, Values.CONFIRM_MESSAGE)
@@ -49,7 +60,8 @@ public static ActionArgs createActionArgs(Context context) {
4960
.withAction(Args.CANCEL_ACTION, null);
5061
}
5162

52-
public static void showMessage(ActionContext context) {
63+
@Override
64+
public void handleAction(ActionContext context) {
5365
Activity activity = LeanplumActivityHelper.getCurrentActivity();
5466
if (activity == null || activity.isFinishing())
5567
return;

0 commit comments

Comments
 (0)