Skip to content

Commit 227960d

Browse files
authored
E2-10024 multi-channel geofence (#285)
* add trackGeofence * intermediate * add trackGeofence to Leanplum.java, call trackGeofence in locationImplementation, add featureflag and counters * refactor makeTrackArgs * add tests and some changes * minor change in locationimplementation * create enum for geofence event names * rename to geofenceEventType
1 parent c32db2c commit 227960d

File tree

6 files changed

+96
-29
lines changed

6 files changed

+96
-29
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import android.content.Context;
2626
import android.location.Location;
2727
import android.os.AsyncTask;
28-
import android.os.Message;
2928
import android.support.annotation.VisibleForTesting;
3029
import android.text.TextUtils;
3130

@@ -54,6 +53,7 @@
5453
import com.leanplum.internal.Util.DeviceIdInfo;
5554
import com.leanplum.internal.VarCache;
5655
import com.leanplum.messagetemplates.MessageTemplates;
56+
import com.leanplum.models.GeofenceEventType;
5757
import com.leanplum.models.MessageArchiveData;
5858
import com.leanplum.utils.BuildUtil;
5959
import com.leanplum.utils.SharedPreferencesUtil;
@@ -1857,6 +1857,16 @@ public static void track(String event, double value, String info) {
18571857
* @param params Key-value pairs with metrics or data associated with the state. Parameters can be
18581858
* strings or numbers. You can use up to 200 different parameter names in your app.
18591859
*/
1860+
1861+
public static void trackGeofence(GeofenceEventType event, String info) {
1862+
if (featureFlagManager().isFeatureFlagEnabled("track_geofence")) {
1863+
LeanplumInternal.trackGeofence(event, 0.0, info, null, null);
1864+
countAggregator().incrementCount("track_geofence");
1865+
} else {
1866+
countAggregator().incrementCount("track_geofence_disabled");
1867+
}
1868+
}
1869+
18601870
public static void advanceTo(final String state, String info, final Map<String, ?> params) {
18611871
if (Constants.isNoop()) {
18621872
return;

AndroidSDKCore/src/main/java/com/leanplum/internal/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public static class Methods {
116116
public static final String START = "start";
117117
public static final String STOP = "stop";
118118
public static final String TRACK = "track";
119+
public static final String TRACK_GEOFENCE = "trackGeofence";
119120
public static final String UPLOAD_FILE = "uploadFile";
120121
}
121122

AndroidSDKCore/src/main/java/com/leanplum/internal/LeanplumInternal.java

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.leanplum.callbacks.ActionCallback;
3535
import com.leanplum.callbacks.StartCallback;
3636
import com.leanplum.callbacks.VariablesChangedCallback;
37+
import com.leanplum.models.GeofenceEventType;
3738

3839
import org.json.JSONException;
3940
import org.json.JSONObject;
@@ -270,36 +271,55 @@ public void variablesChanged() {
270271
}
271272
}
272273

274+
private static Map<String, Object> makeTrackArgs(final String event, double value, String info,
275+
Map<String, ?> params, Map<String, String> args) {
276+
final Map<String, Object> requestParams = new HashMap<>();
277+
if (args != null) {
278+
requestParams.putAll(args);
279+
}
280+
requestParams.put(Constants.Params.VALUE, Double.toString(value));
281+
requestParams.put(Constants.Params.INFO, info);
282+
if (event != null) {
283+
requestParams.put(Constants.Params.EVENT, event);
284+
}
285+
if (params != null) {
286+
params = validateAttributes(params, "params", false);
287+
requestParams.put(Constants.Params.PARAMS, JsonConverter.toJson(params));
288+
}
289+
if (!inForeground || LeanplumActivityHelper.isActivityPaused()) {
290+
requestParams.put("allowOffline", Boolean.TRUE.toString());
291+
}
292+
return requestParams;
293+
}
294+
273295
public static void track(final String event, double value, String info,
274296
Map<String, ?> params, Map<String, String> args) {
275297
if (Constants.isNoop()) {
276298
return;
277299
}
278300

279301
try {
280-
final Map<String, Object> requestParams = new HashMap<>();
281-
if (args != null) {
282-
requestParams.putAll(args);
283-
}
284-
requestParams.put(Constants.Params.VALUE, Double.toString(value));
285-
requestParams.put(Constants.Params.INFO, info);
286-
if (event != null) {
287-
requestParams.put(Constants.Params.EVENT, event);
288-
}
289-
if (params != null) {
290-
params = validateAttributes(params, "params", false);
291-
requestParams.put(Constants.Params.PARAMS, JsonConverter.toJson(params));
292-
}
293-
if (!inForeground || LeanplumActivityHelper.isActivityPaused()) {
294-
requestParams.put("allowOffline", Boolean.TRUE.toString());
295-
}
296-
302+
final Map<String, Object> requestParams = makeTrackArgs(event, value, info, params, args);
297303
trackInternalWhenStarted(event, params, requestParams);
298304
} catch (Throwable t) {
299305
Util.handleException(t);
300306
}
301307
}
302308

309+
public static void trackGeofence(final GeofenceEventType event, double value, String info,
310+
Map<String, ?> params, Map<String, String> args) {
311+
if (Constants.isNoop()) {
312+
return;
313+
}
314+
315+
try {
316+
final Map<String, Object> requestParams = makeTrackArgs(event.getName(), value, info, params, args);
317+
RequestOld.post(Constants.Methods.TRACK_GEOFENCE, requestParams).send();
318+
} catch (Throwable t) {
319+
Util.handleException(t);
320+
}
321+
}
322+
303323
/**
304324
* Performs the track operation once Leanplum has started. If Leanplum is already started, perform
305325
* the track immediately.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.leanplum.models;
2+
3+
public enum GeofenceEventType {
4+
ENTER_REGION("enter_region"),
5+
EXIT_REGION("exit_region");
6+
7+
private final String name;
8+
9+
GeofenceEventType(String name) {
10+
this.name = name;
11+
}
12+
13+
public String getName() {
14+
return this.name;
15+
}
16+
}

AndroidSDKLocation/src/main/java/com/leanplum/LocationManagerImplementation.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
3737
import com.google.android.gms.location.Geofence;
3838
import com.google.android.gms.location.Geofence.Builder;
39-
import com.google.android.gms.location.GeofencingRequest;
4039
import com.google.android.gms.location.LocationListener;
4140
import com.google.android.gms.location.LocationRequest;
4241
import com.google.android.gms.location.LocationServices;
@@ -46,6 +45,7 @@
4645
import com.leanplum.internal.LeanplumMessageMatchFilter;
4746
import com.leanplum.internal.Log;
4847
import com.leanplum.internal.Util;
48+
import com.leanplum.models.GeofenceEventType;
4949
import com.leanplum.utils.SharedPreferencesUtil;
5050

5151
import java.util.ArrayList;
@@ -297,7 +297,8 @@ private void updateTrackedGeofences() {
297297
toBeTrackedGeofences, getTransitionPendingIntent());
298298
for (Geofence geofence : toBeTrackedGeofences) {
299299
if (geofence != null && geofence.getRequestId() != null) {
300-
trackedGeofenceIds.add(geofence.getRequestId());
300+
String geofenceId = geofence.getRequestId();
301+
trackedGeofenceIds.add(geofenceId);
301302
//TODO: stateBeforeBackground doesn't get persisted.
302303
// If the app goes to the background and terminates, stateBeforeBackground will be reset.
303304
if (isInBackground && !Util.isInBackground() && stateBeforeBackground != null
@@ -306,14 +307,16 @@ private void updateTrackedGeofences() {
306307
// TODO(aleks): This would not work for in-app messages if we have the same geolocation
307308
// triggering it, as a locally triggered push notification.
308309
&& !backgroundGeofences.contains(geofence)) {
309-
Number lastStatus = (Number) stateBeforeBackground.get(geofence.getRequestId());
310-
Number currentStatus = (Number) lastKnownState.get(geofence.getRequestId());
310+
Number lastStatus = (Number) stateBeforeBackground.get(geofenceId);
311+
Number currentStatus = (Number) lastKnownState.get(geofenceId);
311312
if (currentStatus != null && lastStatus != null) {
312313
if (GeofenceStatus.shouldTriggerEnteredGeofence(lastStatus, currentStatus)) {
313314
maybePerformActions(geofence, "enterRegion");
315+
Leanplum.trackGeofence(GeofenceEventType.ENTER_REGION, geofenceId);
314316
}
315317
if (GeofenceStatus.shouldTriggerExitedGeofence(lastStatus, currentStatus)) {
316318
maybePerformActions(geofence, "exitRegion");
319+
Leanplum.trackGeofence(GeofenceEventType.EXIT_REGION, geofenceId);
317320
}
318321
}
319322
}
@@ -336,27 +339,30 @@ private List<Geofence> getToBeTrackedGeofences() {
336339

337340
void updateStatusForGeofences(List<Geofence> geofences, int transitionType) {
338341
for (Geofence geofence : geofences) {
339-
if (!trackedGeofenceIds.contains(geofence.getRequestId()) &&
340-
geofence.getRequestId().startsWith("__leanplum")) {
342+
String geofenceId = geofence.getRequestId();
343+
if (!trackedGeofenceIds.contains(geofenceId) &&
344+
geofenceId.startsWith("__leanplum")) {
341345
ArrayList<String> geofencesToRemove = new ArrayList<>();
342-
geofencesToRemove.add(geofence.getRequestId());
346+
geofencesToRemove.add(geofenceId);
343347
if (googleApiClient != null && googleApiClient.isConnected()) {
344348
LocationServices.GeofencingApi.removeGeofences(googleApiClient, geofencesToRemove);
345349
}
346350
continue;
347351
}
348-
Number currentStatus = (Number) lastKnownState.get(geofence.getRequestId());
352+
Number currentStatus = (Number) lastKnownState.get(geofenceId);
349353
if (currentStatus != null) {
350354
if (GeofenceStatus.shouldTriggerEnteredGeofence(currentStatus,
351355
getStatusForTransitionType(transitionType))) {
352356
maybePerformActions(geofence, "enterRegion");
357+
Leanplum.trackGeofence(GeofenceEventType.ENTER_REGION, geofenceId);
353358
}
354359
if (GeofenceStatus.shouldTriggerExitedGeofence(currentStatus,
355360
getStatusForTransitionType(transitionType))) {
356361
maybePerformActions(geofence, "exitRegion");
362+
Leanplum.trackGeofence(GeofenceEventType.EXIT_REGION, geofenceId);
357363
}
358364
}
359-
lastKnownState.put(geofence.getRequestId(),
365+
lastKnownState.put(geofenceId,
360366
getStatusForTransitionType(transitionType));
361367
}
362368
saveLastKnownRegionState();

AndroidSDKTests/src/test/java/com/leanplum/LeanplumTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@
4545
import com.leanplum.internal.RequestOld;
4646
import com.leanplum.internal.Util;
4747
import com.leanplum.internal.VarCache;
48+
import com.leanplum.models.GeofenceEventType;
4849
import com.leanplum.models.MessageArchiveData;
4950

5051
import org.json.JSONArray;
5152
import org.json.JSONException;
5253
import org.json.JSONObject;
5354
import org.junit.Test;
54-
import org.mockito.Mock;
5555
import org.mockito.Mockito;
5656
import org.robolectric.RuntimeEnvironment;
5757

@@ -82,7 +82,6 @@
8282
import static org.mockito.Matchers.anyInt;
8383
import static org.mockito.Matchers.anyString;
8484
import static org.mockito.Mockito.never;
85-
import static org.mockito.Mockito.only;
8685
import static org.mockito.Mockito.times;
8786
import static org.mockito.Mockito.when;
8887
import static org.powermock.api.mockito.PowerMockito.doNothing;
@@ -684,6 +683,21 @@ public void onRequest(String httpMethod, String apiMethod, Map<String, Object> p
684683
}
685684
});
686685
Leanplum.trackPurchase(eventName, 1.99, "USD", new HashMap<String, Objects>());
686+
687+
// Validate request for track geofence with event name and info
688+
RequestHelper.addRequestHandler(new RequestHelper.RequestHandler() {
689+
@Override
690+
public void onRequest(String httpMethod, String apiMethod, Map<String, Object> params) {
691+
assertEquals(Constants.Methods.TRACK_GEOFENCE, apiMethod);
692+
693+
String requestEventName = (String) params.get("event");
694+
String requestEventInfo = (String) params.get("info");
695+
696+
assertEquals(GeofenceEventType.ENTER_REGION.getName(), requestEventName);
697+
assertEquals(String.valueOf(eventInfo), requestEventInfo);
698+
}
699+
});
700+
Leanplum.trackGeofence(GeofenceEventType.ENTER_REGION, eventInfo);
687701
}
688702

689703
@Test

0 commit comments

Comments
 (0)