Skip to content

Commit 73ac180

Browse files
authored
Add dialog customizer to allow window flags (#440)
1 parent 88c402e commit 73ac180

File tree

8 files changed

+123
-4
lines changed

8 files changed

+123
-4
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.leanplum.messagetemplates;
2+
3+
import android.app.Dialog;
4+
import android.view.View;
5+
6+
/**
7+
* Interface that provides access to the view and dialog objects of the main templates before
8+
* drawing them. Called just before
9+
* {@link Dialog#setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}.
10+
*
11+
* You can use it to change window flags.
12+
*/
13+
public interface DialogCustomizer {
14+
15+
/**
16+
* Customize "Center Popup" template.
17+
*
18+
* @param messageDialog Dialog container of this message
19+
* @param messageContent Content view of the Dialog object
20+
*/
21+
default void customizeCenterPopup(Dialog messageDialog, View messageContent) {
22+
}
23+
24+
/**
25+
* Customize "Interstitial" template.
26+
*
27+
* @param messageDialog Dialog container of this message
28+
* @param messageContent Content view of the Dialog object
29+
*/
30+
default void customizeInterstitial(Dialog messageDialog, View messageContent) {
31+
}
32+
33+
/**
34+
* Customize "Web Interstitial" template.
35+
*
36+
* @param messageDialog Dialog container of this message
37+
* @param messageContent Content view of the Dialog object
38+
*/
39+
default void customizeWebInterstitial(Dialog messageDialog, View messageContent) {
40+
}
41+
42+
/**
43+
* Customize "Rich Interstitial" template.
44+
*
45+
* @param messageDialog Dialog container of this message
46+
* @param messageContent Content view of the Dialog object
47+
*/
48+
default void customizeRichInterstitial(Dialog messageDialog, View messageContent) {
49+
}
50+
51+
/**
52+
* Customize "Banner" template.
53+
*
54+
* @param messageDialog Dialog container of this message
55+
* @param messageContent Content view of the Dialog object
56+
*/
57+
default void customizeBanner(Dialog messageDialog, View messageContent) {
58+
}
59+
}

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

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

2424
import android.content.Context;
2525
import androidx.annotation.NonNull;
26+
import androidx.annotation.Nullable;
2627
import com.leanplum.ActionArgs;
2728
import com.leanplum.ActionContext;
2829
import com.leanplum.Leanplum;
@@ -46,6 +47,7 @@
4647
public class MessageTemplates {
4748

4849
private static boolean registered = false;
50+
private static DialogCustomizer customizer;
4951

5052
private static ActionCallback createCallback(@NonNull MessageTemplate template) {
5153
return new ActionCallback() {
@@ -85,6 +87,26 @@ public void run() {
8587
};
8688
}
8789

90+
/**
91+
* Registers customizer to use when creating the message templates.
92+
* Call this method before Leanplum.start.
93+
*
94+
* @param customizer Dialog customizer
95+
*/
96+
public static void setCustomizer(DialogCustomizer customizer) {
97+
MessageTemplates.customizer = customizer;
98+
}
99+
100+
/**
101+
* Returns the registered customizer.
102+
*
103+
* @return Dialog customizer
104+
*/
105+
@Nullable
106+
public static DialogCustomizer getCustomizer() {
107+
return customizer;
108+
}
109+
88110
/**
89111
* Registers a message template to respond to a given action.
90112
* Will be shown in the templates group in Dashboard.

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
import android.app.Activity;
2525
import android.app.Dialog;
2626
import android.graphics.Color;
27+
import android.os.Build;
2728
import android.view.View;
2829
import android.view.ViewGroup.LayoutParams;
30+
import android.view.WindowManager;
2931
import android.view.animation.Animation;
3032
import android.widget.RelativeLayout;
3133

3234
import androidx.annotation.VisibleForTesting;
3335
import com.leanplum.core.R;
36+
import com.leanplum.messagetemplates.DialogCustomizer;
37+
import com.leanplum.messagetemplates.MessageTemplates;
3438
import com.leanplum.utils.SizeUtil;
3539
import com.leanplum.views.CloseButton;
3640
import com.leanplum.views.ViewUtils;
@@ -62,11 +66,12 @@ protected void init() {
6266
CloseButton closeButton = createCloseButton(messageView);
6367
contentView.addView(closeButton);
6468
}
69+
70+
applyWindowDecoration();
71+
6572
setContentView(contentView, contentView.getLayoutParams());
6673

6774
contentView.setAnimation(ViewUtils.createFadeInAnimation(350));
68-
69-
applyWindowDecoration();
7075
}
7176

7277
private RelativeLayout createContentView() {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import android.view.Window;
2828
import android.view.WindowManager;
2929
import android.widget.RelativeLayout;
30+
import com.leanplum.messagetemplates.DialogCustomizer;
31+
import com.leanplum.messagetemplates.MessageTemplates;
3032
import com.leanplum.messagetemplates.options.CenterPopupOptions;
3133
import com.leanplum.utils.SizeUtil;
3234

@@ -56,6 +58,10 @@ protected void applyWindowDecoration() {
5658
if (Build.VERSION.SDK_INT >= 14) {
5759
window.setDimAmount(0.7f);
5860
}
61+
DialogCustomizer customizer = MessageTemplates.getCustomizer();
62+
if (customizer != null) {
63+
customizer.customizeCenterPopup(this, contentView);
64+
}
5965
}
6066

6167
@Override

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import android.app.Activity;
2525
import android.view.ViewGroup.LayoutParams;
2626
import android.widget.RelativeLayout;
27+
import com.leanplum.messagetemplates.DialogCustomizer;
28+
import com.leanplum.messagetemplates.MessageTemplates;
2729
import com.leanplum.messagetemplates.options.InterstitialOptions;
2830

2931
/**
@@ -44,7 +46,10 @@ boolean isFullscreen() {
4446

4547
@Override
4648
void applyWindowDecoration() {
47-
// no implementation
49+
DialogCustomizer customizer = MessageTemplates.getCustomizer();
50+
if (customizer != null) {
51+
customizer.customizeInterstitial(this, contentView);
52+
}
4853
}
4954

5055
@Override

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import androidx.annotation.NonNull;
4444
import com.leanplum.ActionContext;
4545
import com.leanplum.Leanplum;
46+
import com.leanplum.messagetemplates.DialogCustomizer;
47+
import com.leanplum.messagetemplates.MessageTemplates;
4648
import com.leanplum.messagetemplates.options.RichHtmlOptions;
4749
import com.leanplum.utils.SizeUtil;
4850
import java.io.UnsupportedEncodingException;
@@ -116,6 +118,16 @@ protected void applyWindowDecoration() {
116118
contentView.setGravity(Gravity.BOTTOM);
117119
}
118120
}
121+
122+
DialogCustomizer customizer = MessageTemplates.getCustomizer();
123+
if (customizer != null) {
124+
if (richOptions.isBanner()) {
125+
customizer.customizeBanner(this, contentView);
126+
} else {
127+
customizer.customizeRichInterstitial(this, contentView);
128+
}
129+
}
130+
119131
}
120132

121133
@Override

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import android.widget.RelativeLayout;
3333
import androidx.annotation.NonNull;
3434
import com.leanplum.Leanplum;
35+
import com.leanplum.messagetemplates.DialogCustomizer;
36+
import com.leanplum.messagetemplates.MessageTemplates;
3537
import com.leanplum.messagetemplates.options.WebInterstitialOptions;
3638

3739
/**
@@ -61,7 +63,10 @@ boolean isFullscreen() {
6163

6264
@Override
6365
void applyWindowDecoration() {
64-
// no implementation
66+
DialogCustomizer customizer = MessageTemplates.getCustomizer();
67+
if (customizer != null) {
68+
customizer.customizeWebInterstitial(this, contentView);
69+
}
6570
}
6671

6772
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,11 @@ public static class Size {
356356
public String type;
357357
}
358358

359+
public boolean isBanner() {
360+
String templateName = getActionContext().getArgs().get(Values.HTML_TEMPLATE_PREFIX).toString();
361+
return templateName.toLowerCase().contains("banner");
362+
}
363+
359364
/**
360365
* Banners with property TabOutsideToClose = false need to be treated differently
361366
* so they do not block interaction with other dialogs and the keyboard.

0 commit comments

Comments
 (0)