use of org.infobip.mobile.messaging.geo.Area in project mobile-messaging-sdk-android by infobip.
the class GeoReportHelper method createReports.
/**
* Generates set of geofencing reports
*
* @param context context
* @param signalingMessage original signaling message
* @param areas list of areas that triggered this geofencing event
* @param event transition type
* @return set of geofencing reports to send to server
*/
private static Set<GeoReport> createReports(Context context, Message signalingMessage, List<Area> areas, @NonNull GeoEventType event, @NonNull GeoLatLng triggeringLocation) {
Set<GeoReport> reports = new ArraySet<>();
for (Area area : areas) {
GeoReport report = createReport(signalingMessage, area, event, triggeringLocation);
reports.add(report);
MobileMessagingCore.getInstance(context).addGeneratedMessageIds(report.getMessageId());
}
return reports;
}
use of org.infobip.mobile.messaging.geo.Area in project mobile-messaging-sdk-android by infobip.
the class GeoReportHelper method findSignalingMessagesAndAreas.
/**
* Returns map of signaling messages and corresponding areas that match geofence and transition event
*
* @param messageStore message store to look messages for
* @param requestIds requestIds received from Google Location Services during transition event
* @param event transition event type
* @return signaling messages with corresponding areas
*/
@NonNull
public static Map<Message, List<Area>> findSignalingMessagesAndAreas(Context context, MessageStore messageStore, Set<String> requestIds, @NonNull GeoEventType event) {
Date now = Time.date();
Map<Message, List<Area>> messagesAndAreas = new ArrayMap<>();
for (Message message : messageStore.findAll(context)) {
Geo geo = GeoDataMapper.geoFromInternalData(message.getInternalData());
if (geo == null || geo.getAreasList() == null || geo.getAreasList().isEmpty()) {
continue;
}
// don't trigger geo event before start date
Date startDate = geo.getStartDate();
if (startDate != null && startDate.after(now)) {
continue;
}
List<Area> campaignAreas = geo.getAreasList();
List<Area> triggeredAreas = new ArrayList<>();
for (Area area : campaignAreas) {
for (String requestId : requestIds) {
if (!requestId.equalsIgnoreCase(area.getId())) {
continue;
}
if (!GeoNotificationHelper.shouldReportTransition(context, geo, event)) {
continue;
}
triggeredAreas.add(area);
}
}
if (!triggeredAreas.isEmpty()) {
messagesAndAreas.put(message, triggeredAreas);
}
}
return filterOverlappingAreas(messagesAndAreas);
}
use of org.infobip.mobile.messaging.geo.Area in project mobile-messaging-sdk-android by infobip.
the class GeoReportHelper method filterOverlappingAreas.
/**
* Filters out overlapping areas for each campaign and returns only the smallest area
*
* @param messagesAndAreas all triggered areas for each message
* @return filtered areas
*/
public static Map<Message, List<Area>> filterOverlappingAreas(Map<Message, List<Area>> messagesAndAreas) {
Map<Message, List<Area>> filteredMessagesAndAreas = new ArrayMap<>(messagesAndAreas.size());
for (Map.Entry<Message, List<Area>> entry : messagesAndAreas.entrySet()) {
Message message = entry.getKey();
List<Area> areasList = entry.getValue();
if (areasList != null) {
// using only area that has the smallest radius
Collections.sort(areasList, new GeoAreaRadiusComparator());
filteredMessagesAndAreas.put(message, Collections.singletonList(areasList.get(0)));
}
}
return filteredMessagesAndAreas;
}
use of org.infobip.mobile.messaging.geo.Area in project mobile-messaging-sdk-android by infobip.
the class GeofencingImpl method removeExpiredAreasFromStorage.
void removeExpiredAreasFromStorage() {
GeoSQLiteMessageStore messageStoreForGeo = (GeoSQLiteMessageStore) geofencingHelper.getMessageStoreForGeo();
List<Message> messages = messageStoreForGeo.findAll(context);
List<String> messageIdsToDelete = new ArrayList<>(messages.size());
Date now = new Date();
for (Message message : messages) {
Geo geo = GeoDataMapper.geoFromInternalData(message.getInternalData());
if (geo == null) {
continue;
}
List<Area> areasList = geo.getAreasList();
Date expiryDate = geo.getExpiryDate();
if (areasList == null || areasList.isEmpty()) {
continue;
}
for (Area area : areasList) {
if (!area.isValid() || expiryDate == null) {
continue;
}
if (expiryDate.before(now)) {
messageIdsToDelete.add(message.getMessageId());
}
}
}
if (!messageIdsToDelete.isEmpty()) {
messageStoreForGeo.deleteByIds(context, messageIdsToDelete.toArray(new String[] {}));
}
}
use of org.infobip.mobile.messaging.geo.Area in project mobile-messaging-sdk-android by infobip.
the class GeofencingImpl method calculateGeofencesToMonitorAndNextCheckDates.
@SuppressWarnings("WeakerAccess")
private Pair<List<Geofence>, Pair<Date, Date>> calculateGeofencesToMonitorAndNextCheckDates(MessageStore messageStore) {
Date nextCheckRefreshDate = null;
Date nextCheckExpireDate = null;
Map<String, Geofence> geofences = new HashMap<>();
Map<String, Date> expiryDates = new HashMap<>();
List<Message> messages = messageStore.findAll(context);
for (Message message : messages) {
Geo geo = GeoDataMapper.geoFromInternalData(message.getInternalData());
if (geo == null || geo.getAreasList() == null || geo.getAreasList().isEmpty()) {
continue;
}
nextCheckExpireDate = calculateNextCheckDateForGeoExpiry(geo, nextCheckExpireDate);
final Set<String> finishedCampaignIds = GeofencingHelper.getFinishedCampaignIds(context);
if (finishedCampaignIds.contains(geo.getCampaignId())) {
continue;
}
if (geo.isEligibleForMonitoring()) {
List<Area> geoAreasList = geo.getAreasList();
for (Area area : geoAreasList) {
if (!area.isValid()) {
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()));
}
}
nextCheckRefreshDate = calculateNextCheckDateForGeoStart(geo, nextCheckRefreshDate);
}
List<Geofence> geofenceList = new ArrayList<>(geofences.values());
return new Pair<>(geofenceList, new Pair<>(nextCheckRefreshDate, nextCheckExpireDate));
}
Aggregations