1 /*
2  * Copyright 2014, 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 android.graphics.Bitmap;
26 import android.graphics.BitmapFactory;
27 import android.graphics.Color;
28 import android.util.Log;
29 
30 import com.leanplum.ActionArgs;
31 import com.leanplum.ActionContext;
32 import com.leanplum.messagetemplates.MessageTemplates.Args;
33 import com.leanplum.utils.BitmapUtil;
34 
35 import java.io.InputStream;
36 
37 /**
38  * Options used by Center Popup and Interstitial.
39  *
40  * @author Martin Yanakiev
41  */
42 abstract class BaseMessageOptions {
43   private ActionContext context;
44   private String title;
45   private int titleColor;
46   private String messageText;
47   private int messageColor;
48   private Bitmap backgroundImage;
49   private int backgroundColor;
50   private String acceptButtonText;
51   private int acceptButtonBackgroundColor;
52   private int acceptButtonTextColor;
53 
BaseMessageOptions(ActionContext context)54   protected BaseMessageOptions(ActionContext context) {
55     this.context = context;
56     setTitle(context.stringNamed(Args.TITLE_TEXT));
57     setTitleColor(context.numberNamed(Args.TITLE_COLOR).intValue());
58     setMessageText(context.stringNamed(Args.MESSAGE_TEXT));
59     setMessageColor(context.numberNamed(Args.MESSAGE_COLOR).intValue());
60     InputStream imageStream = context.streamNamed(Args.BACKGROUND_IMAGE);
61     if (imageStream != null) {
62       try {
63         setBackgroundImage(BitmapFactory.decodeStream(imageStream));
64       } catch (Exception e) {
65         Log.e("Leanplum", "Error loading background image", e);
66       }
67     }
68     setBackgroundColor(context.numberNamed(Args.BACKGROUND_COLOR).intValue());
69     setAcceptButtonText(context.stringNamed(Args.ACCEPT_BUTTON_TEXT));
70     setAcceptButtonBackgroundColor(context.numberNamed(
71         Args.ACCEPT_BUTTON_BACKGROUND_COLOR).intValue());
72     setAcceptButtonTextColor(context.numberNamed(
73         Args.ACCEPT_BUTTON_TEXT_COLOR).intValue());
74   }
75 
getBackgroundColor()76   public int getBackgroundColor() {
77     return backgroundColor;
78   }
79 
setBackgroundColor(int backgroundColor)80   private void setBackgroundColor(int backgroundColor) {
81     this.backgroundColor = backgroundColor;
82   }
83 
getAcceptButtonText()84   public String getAcceptButtonText() {
85     return acceptButtonText;
86   }
87 
setAcceptButtonText(String acceptButtonText)88   private void setAcceptButtonText(String acceptButtonText) {
89     this.acceptButtonText = acceptButtonText;
90   }
91 
getTitle()92   public String getTitle() {
93     return title;
94   }
95 
setTitle(String title)96   private void setTitle(String title) {
97     this.title = title;
98   }
99 
getTitleColor()100   public int getTitleColor() {
101     return titleColor;
102   }
103 
setTitleColor(int titleColor)104   private void setTitleColor(int titleColor) {
105     this.titleColor = titleColor;
106   }
107 
getMessageText()108   public String getMessageText() {
109     return messageText;
110   }
111 
setMessageText(String messageText)112   private void setMessageText(String messageText) {
113     this.messageText = messageText;
114   }
115 
getMessageColor()116   public int getMessageColor() {
117     return messageColor;
118   }
119 
setMessageColor(int messageColor)120   private void setMessageColor(int messageColor) {
121     this.messageColor = messageColor;
122   }
123 
getBackgroundImage()124   public Bitmap getBackgroundImage() {
125     return backgroundImage;
126   }
127 
getBackgroundImageRounded(int pixels)128   public Bitmap getBackgroundImageRounded(int pixels) {
129     return BitmapUtil.getRoundedCornerBitmap(backgroundImage, pixels);
130   }
131 
setBackgroundImage(Bitmap backgroundImage)132   private void setBackgroundImage(Bitmap backgroundImage) {
133     this.backgroundImage = backgroundImage;
134   }
135 
getAcceptButtonBackgroundColor()136   public int getAcceptButtonBackgroundColor() {
137     return acceptButtonBackgroundColor;
138   }
139 
setAcceptButtonBackgroundColor(int color)140   private void setAcceptButtonBackgroundColor(int color) {
141     this.acceptButtonBackgroundColor = color;
142   }
143 
getAcceptButtonTextColor()144   public int getAcceptButtonTextColor() {
145     return acceptButtonTextColor;
146   }
147 
setAcceptButtonTextColor(int color)148   private void setAcceptButtonTextColor(int color) {
149     this.acceptButtonTextColor = color;
150   }
151 
accept()152   public void accept() {
153     context.runTrackedActionNamed(Args.ACCEPT_ACTION);
154   }
155 
toArgs(Context currentContext)156   public static ActionArgs toArgs(Context currentContext) {
157     return new ActionArgs()
158         .with(Args.TITLE_TEXT,
159             MessageTemplates.getApplicationName(currentContext))
160         .withColor(Args.TITLE_COLOR, Color.BLACK)
161         .with(Args.MESSAGE_TEXT, MessageTemplates.Values.POPUP_MESSAGE)
162         .withColor(Args.MESSAGE_COLOR, Color.BLACK)
163         .withFile(Args.BACKGROUND_IMAGE, null)
164         .withColor(Args.BACKGROUND_COLOR, Color.WHITE)
165         .with(Args.ACCEPT_BUTTON_TEXT, MessageTemplates.Values.OK_TEXT)
166         .withColor(Args.ACCEPT_BUTTON_BACKGROUND_COLOR, Color.WHITE)
167         .withColor(Args.ACCEPT_BUTTON_TEXT_COLOR, Color.argb(255, 0, 122, 255))
168         .withAction(Args.ACCEPT_ACTION, null);
169   }
170 }
171