3131import com .leanplum .callbacks .ActionCallback ;
3232import com .leanplum .utils .SharedPreferencesUtil ;
3333
34- import java .lang . reflect . Modifier ;
34+ import java .util . Date ;
3535import java .util .HashMap ;
3636import java .util .List ;
3737import java .util .Map ;
@@ -54,12 +54,14 @@ public class ActionManager {
5454 private static final String LEANPLUM_LOCAL_PUSH_HELPER =
5555 "com.leanplum.internal.LeanplumLocalPushHelper" ;
5656 private static final String PREFERENCES_NAME = "__leanplum_messaging__" ;
57+ private static LocationManager locationManager ;
5758 private static boolean loggedLocationManagerFailure = false ;
5859
5960 public static class MessageMatchResult {
6061 public boolean matchedTrigger ;
6162 public boolean matchedUnlessTrigger ;
6263 public boolean matchedLimit ;
64+ public boolean matchedActivePeriod ;
6365 }
6466
6567 public static synchronized ActionManager getInstance () {
@@ -69,14 +71,19 @@ public static synchronized ActionManager getInstance() {
6971 return instance ;
7072 }
7173
72- public static LocationManager getLocationManager () {
74+ public static synchronized LocationManager getLocationManager () {
75+ if (locationManager != null ) {
76+ return locationManager ;
77+ }
78+
7379 if (Util .hasPlayServices ()) {
7480 try {
7581 // Reflection here prevents linker errors
7682 // if Google Play Services is not used in the client app.
77- return (LocationManager ) Class
83+ locationManager = (LocationManager ) Class
7884 .forName ("com.leanplum.LocationManagerImplementation" )
7985 .getMethod ("instance" ).invoke (null );
86+ return locationManager ;
8087 } catch (Throwable t ) {
8188 if (!loggedLocationManagerFailure ) {
8289 Log .w ("Geofencing support requires leanplum-location module and Google Play " +
@@ -246,8 +253,8 @@ public MessageMatchResult shouldShowMessage(String messageId, Map<String, Object
246253 // 2. Must match at least one trigger.
247254 result .matchedTrigger = matchedTriggers (messageConfig .get ("whenTriggers" ), when , eventName ,
248255 contextualValues );
249- result .matchedUnlessTrigger = matchedTriggers ( messageConfig . get ( "unlessTriggers" ), when , eventName ,
250- contextualValues );
256+ result .matchedUnlessTrigger =
257+ matchedTriggers ( messageConfig . get ( "unlessTriggers" ), when , eventName , contextualValues );
251258 if (!result .matchedTrigger && !result .matchedUnlessTrigger ) {
252259 return result ;
253260 }
@@ -259,6 +266,18 @@ public MessageMatchResult shouldShowMessage(String messageId, Map<String, Object
259266 limitConfig = CollectionUtil .uncheckedCast (limitConfigObj );
260267 }
261268 result .matchedLimit = matchesLimits (messageId , limitConfig );
269+
270+ // 4. Must be within active period.
271+ Object messageStartTime = messageConfig .get ("startTime" );
272+ Object messageEndTime = messageConfig .get ("endTime" );
273+ if (messageStartTime == null || messageEndTime == null ) {
274+ result .matchedActivePeriod = true ;
275+ } else {
276+ long currentTime = new Date ().getTime ();
277+ result .matchedActivePeriod = currentTime >= (long ) messageStartTime &&
278+ currentTime <= (long ) messageEndTime ;
279+ }
280+
262281 return result ;
263282 }
264283
0 commit comments