Skip to content

Commit

Permalink
Merge branch 'master' into sslavin-MM-1280-new-geofencing
Browse files Browse the repository at this point in the history
# Conflicts:
#	infobip-mobile-messaging-android-sdk/src/main/java/org/infobip/mobile/messaging/MobileMessagingCore.java
#	infobip-mobile-messaging-android-sdk/src/main/java/org/infobip/mobile/messaging/geo/Geo.java
#	infobip-mobile-messaging-android-sdk/src/main/java/org/infobip/mobile/messaging/geo/Geofencing.java
#	infobip-mobile-messaging-android-sdk/src/main/java/org/infobip/mobile/messaging/storage/SharedPreferencesMessageStore.java
  • Loading branch information
stanislavin committed Feb 13, 2017
2 parents 4e10d05 + 697eccb commit 21f2b49
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This guide is designed to get you up and running with Mobile Messaging SDK integ
```groovy
dependencies {
...
compile ('org.infobip.mobile.messaging.api:infobip-mobile-messaging-android-sdk:1.3.25@aar') {
compile ('org.infobip.mobile.messaging.api:infobip-mobile-messaging-android-sdk:1.3.26@aar') {
transitive = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package org.infobip.mobile.messaging.geo;

import android.content.Context;
import android.test.InstrumentationTestCase;

import com.google.android.gms.location.Geofence;

import org.infobip.mobile.messaging.Message;
import org.infobip.mobile.messaging.MobileMessaging;
import org.infobip.mobile.messaging.MobileMessagingProperty;
import org.infobip.mobile.messaging.api.support.Tuple;
import org.infobip.mobile.messaging.storage.MessageStore;
import org.infobip.mobile.messaging.storage.SQLiteMessageStore;
import org.infobip.mobile.messaging.util.DateTimeUtil;
import org.infobip.mobile.messaging.util.PreferenceHelper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author sslavin
* @since 12/02/2017.
*/

public class GeoMonitoringTest extends InstrumentationTestCase {

private Context context;
private MessageStore messageStore;
private Long now;

@Override
protected void setUp() throws Exception {
super.setUp();

context = getInstrumentation().getContext().getApplicationContext();
now = System.currentTimeMillis();

Geofencing.getInstance(context);

PreferenceHelper.saveString(context, MobileMessagingProperty.MESSAGE_STORE_CLASS, SQLiteMessageStore.class.getName());
messageStore = MobileMessaging.getInstance(context).getMessageStore();
messageStore.deleteAll(context);
}

public void test_shouldCalculateRefreshDateForGeoStart() throws Exception {
// Given
Long millis15MinAfterNow = now + 15 * 60 * 1000;
Long millis30MinAfterNow = now + 30 * 60 * 1000;
String date15MinAfterNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinAfterNow));
String date30MinAfterNow = DateTimeUtil.ISO8601DateToString(new Date(millis30MinAfterNow));

Geo geo = new Geo(0.0, 0.0, null, date30MinAfterNow, date15MinAfterNow, "SomeCampaignId", new ArrayList<Area>() {{
add(new Area("SomeAreaId", "SomeAreaTitle", 0.0, 0.0, 10));
}}, new ArrayList<GeoEventSettings>());

Message message = new Message(
"SomeMessageId",
"SomeTitle",
"SomeBody",
"SomeSound",
true,
"SomeIcon",
true,
"SomeCategory",
"SomeFrom",
now,
0,
null,
geo,
"SomeDestination",
Message.Status.UNKNOWN,
"SomeStatusMessage"
);
messageStore.save(context, message);

// When
Tuple<List<Geofence>, Date> geofencesAndNextRefreshDate = Geofencing.calculateGeofencesToMonitorAndNextCheckDate(messageStore);

// Then
assertNotNull(geofencesAndNextRefreshDate);
assertTrue(geofencesAndNextRefreshDate.getLeft().isEmpty());
assertNotNull(geofencesAndNextRefreshDate.getRight());
assertEquals(millis15MinAfterNow, geofencesAndNextRefreshDate.getRight().getTime(), 3000);
}

public void test_shouldNotCalculateRefreshDateIfGeoExpired() throws Exception {
// Given
Long millis30MinBeforeNow = now - 30 * 60 * 1000;
Long millis15MinBeforeNow = now - 15 * 60 * 1000;
String date30MinBeforeNow = DateTimeUtil.ISO8601DateToString(new Date(millis30MinBeforeNow));
String date15MinBeforeNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinBeforeNow));

Geo geo = new Geo(0.0, 0.0, null, date15MinBeforeNow, date30MinBeforeNow, "SomeCampaignId", new ArrayList<Area>() {{
add(new Area("SomeAreaId", "SomeAreaTitle", 0.0, 0.0, 10));
}}, new ArrayList<GeoEventSettings>());

Message message = new Message(
"SomeMessageId",
"SomeTitle",
"SomeBody",
"SomeSound",
true,
"SomeIcon",
true,
"SomeCategory",
"SomeFrom",
now,
0,
null,
geo,
"SomeDestination",
Message.Status.UNKNOWN,
"SomeStatusMessage"
);
messageStore.save(context, message);

// When
Tuple<List<Geofence>, Date> geofencesAndNextRefreshDate = Geofencing.calculateGeofencesToMonitorAndNextCheckDate(messageStore);

// Then
assertNotNull(geofencesAndNextRefreshDate);
assertTrue(geofencesAndNextRefreshDate.getLeft().isEmpty());
assertNull(geofencesAndNextRefreshDate.getRight());
}

public void test_shouldNotCalculateRefreshDateIfGeoIsMonitoredNow() throws Exception {
// Given
Long millis15MinBeforeNow = now - 15 * 60 * 1000;
Long millis15MinAfterNow = now + 15 * 60 * 1000;
String date15MinBeforeNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinBeforeNow));
String date15MinAfterNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinAfterNow));

Geo geo = new Geo(0.0, 0.0, null, date15MinBeforeNow, date15MinAfterNow, "SomeCampaignId", new ArrayList<Area>() {{
add(new Area("SomeAreaId", "SomeAreaTitle", 0.0, 0.0, 10));
}}, new ArrayList<GeoEventSettings>());

Message message = new Message(
"SomeMessageId",
"SomeTitle",
"SomeBody",
"SomeSound",
true,
"SomeIcon",
true,
"SomeCategory",
"SomeFrom",
now,
0,
null,
geo,
"SomeDestination",
Message.Status.UNKNOWN,
"SomeStatusMessage"
);
messageStore.save(context, message);

// When
Tuple<List<Geofence>, Date> geofencesAndNextRefreshDate = Geofencing.calculateGeofencesToMonitorAndNextCheckDate(messageStore);

// Then
assertNotNull(geofencesAndNextRefreshDate);
assertTrue(geofencesAndNextRefreshDate.getLeft().isEmpty());
assertNull(geofencesAndNextRefreshDate.getRight());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public MessageStore getMessageStoreForMessage(Message message) {
return getMessageStore();
}

static public boolean hasGeo(Message message) {
public static boolean hasGeo(Message message) {
return message != null && message.getGeo() != null &&
message.getGeo().getAreasList() != null &&
!message.getGeo().getAreasList().isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public static void scheduleRefresh(Context context, Date when) {
alarmManager.set(AlarmManager.RTC_WAKEUP, when.getTime(), PendingIntent.getBroadcast(context, 0, new Intent(context, GeofencingAlarmReceiver.class), 0));
}

private static Tuple<List<Geofence>, Date> calculateGeofencesToMonitorAndNextCheckDate(MessageStore messageStore) {
@SuppressWarnings("WeakerAccess")
static Tuple<List<Geofence>, Date> calculateGeofencesToMonitorAndNextCheckDate(MessageStore messageStore) {
Date nextCheckDate = null;
Date now = new Date();
Map<String, Geofence> geofences = new HashMap<>();
Map<String, Date> expiryDates = new HashMap<>();
List<Message> messages = messageStore.findAll(context);
Expand All @@ -100,44 +100,54 @@ private static Tuple<List<Geofence>, Date> calculateGeofencesToMonitorAndNextChe
continue;
}

if (!geo.isEligibleForMonitoring()) {
continue;
}

final Set<String> finishedCampaignIds = MobileMessagingCore.getInstance(context).getFinishedCampaignIds();
if (finishedCampaignIds.contains(geo.getCampaignId())) {
continue;
}

List<Area> geoAreasList = message.getGeo().getAreasList();
for (Area area : geoAreasList) {
if (!area.isValid()) {
continue;
}
if (geo.isEligibleForMonitoring()) {
List<Area> geoAreasList = message.getGeo().getAreasList();
for (Area area : geoAreasList) {
if (!area.isValid()) {
continue;
}

Date expiry = expiryDates.get(area.getId());
if (expiry != null && expiry.after(geo.getExpiryDate())) {
continue;
}
Date expiry = expiryDates.get(area.getId());
if (expiry != null && expiry.after(geo.getExpiryDate())) {
continue;
}

expiryDates.put(area.getId(), geo.getExpiryDate());
geofences.put(area.getId(), area.toGeofence(geo.getExpiryDate()));
expiryDates.put(area.getId(), geo.getExpiryDate());
geofences.put(area.getId(), area.toGeofence(geo.getExpiryDate()));
}
}

Date startDate = geo.getStartDate();
Date expiryDate = geo.getExpiryDate();
if (nextCheckDate == null) {
nextCheckDate = startDate;
} else if (startDate != null && startDate.before(nextCheckDate) &&
expiryDate != null && expiryDate.after(now)) {
nextCheckDate = startDate;
}
nextCheckDate = calculateNextCheckDateForGeo(geo, nextCheckDate);
}

List<Geofence> geofenceList = new ArrayList<>(geofences.values());
return new Tuple<>(geofenceList, nextCheckDate);
}

private static Date calculateNextCheckDateForGeo(Geo geo, Date oldCheckDate) {
Date now = new Date();
Date expiryDate = geo.getExpiryDate();
if (expiryDate != null && expiryDate.before(now)) {
return oldCheckDate;
}

Date startDate = geo.getStartDate();
if (startDate == null || startDate.before(now)) {
return oldCheckDate;
}

if (oldCheckDate != null && oldCheckDate.before(startDate)) {
return oldCheckDate;
}

return startDate;
}

@SuppressWarnings("MissingPermission")
public void activate() {

Expand Down Expand Up @@ -171,8 +181,6 @@ public void onResult(@NonNull Status status) {
logGeofenceStatus(status, true);
}
});


}

public void deactivate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.util.Base64;

import org.infobip.mobile.messaging.Message;
import org.infobip.mobile.messaging.MobileMessagingCore;
import org.infobip.mobile.messaging.dal.bundle.FCMMessageMapper;
import org.infobip.mobile.messaging.util.PreferenceHelper;

Expand Down Expand Up @@ -84,8 +83,6 @@ public void mutate(Set<String> set) {
}
}
});

MobileMessagingCore.getInstance(context).activateGeofencing();
}

private String serialize(Bundle in) {
Expand Down

0 comments on commit 21f2b49

Please sign in to comment.