Search in sources :

Example 1 with Area

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;
}
Also used : Area(org.infobip.mobile.messaging.geo.Area) ArraySet(android.support.v4.util.ArraySet)

Example 2 with Area

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);
}
Also used : Geo(org.infobip.mobile.messaging.geo.Geo) Area(org.infobip.mobile.messaging.geo.Area) Message(org.infobip.mobile.messaging.Message) ArrayList(java.util.ArrayList) ArrayMap(android.support.v4.util.ArrayMap) ArrayList(java.util.ArrayList) List(java.util.List) Date(java.util.Date) NonNull(android.support.annotation.NonNull)

Example 3 with Area

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;
}
Also used : Area(org.infobip.mobile.messaging.geo.Area) Message(org.infobip.mobile.messaging.Message) ArrayMap(android.support.v4.util.ArrayMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayMap(android.support.v4.util.ArrayMap) Map(java.util.Map)

Example 4 with Area

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[] {}));
    }
}
Also used : Geo(org.infobip.mobile.messaging.geo.Geo) Area(org.infobip.mobile.messaging.geo.Area) Message(org.infobip.mobile.messaging.Message) ArrayList(java.util.ArrayList) GeoSQLiteMessageStore(org.infobip.mobile.messaging.geo.storage.GeoSQLiteMessageStore) Date(java.util.Date)

Example 5 with Area

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));
}
Also used : Message(org.infobip.mobile.messaging.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.util.Date) Geo(org.infobip.mobile.messaging.geo.Geo) Area(org.infobip.mobile.messaging.geo.Area) Geofence(com.google.android.gms.location.Geofence) Pair(android.util.Pair)

Aggregations

Area (org.infobip.mobile.messaging.geo.Area)6 ArrayList (java.util.ArrayList)5 Message (org.infobip.mobile.messaging.Message)5 Geo (org.infobip.mobile.messaging.geo.Geo)4 Date (java.util.Date)3 ArrayMap (android.support.v4.util.ArrayMap)2 List (java.util.List)2 NonNull (android.support.annotation.NonNull)1 ArraySet (android.support.v4.util.ArraySet)1 Pair (android.util.Pair)1 Geofence (com.google.android.gms.location.Geofence)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 GeoLatLng (org.infobip.mobile.messaging.geo.GeoLatLng)1 GeoSQLiteMessageStore (org.infobip.mobile.messaging.geo.storage.GeoSQLiteMessageStore)1