Source:
protected Collection<Event> analyzePosition(Position position) { Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId()); if (device == null) { return null; } if (!Context.getIdentityManager().isLatestPosition(position) || !position.getValid()) { return null; } List<Long> currentGeofences = geofenceManager.getCurrentDeviceGeofences(position); List<Long> oldGeofences = new ArrayList<>(); if (device.getGeofenceIds() != null) { oldGeofences.addAll(device.getGeofenceIds()); } List<Long> newGeofences = new ArrayList<>(currentGeofences); newGeofences.removeAll(oldGeofences); oldGeofences.removeAll(currentGeofences); device.setGeofenceIds(currentGeofences); Collection<Event> events = new ArrayList<>(); for (long geofenceId : newGeofences) { long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); if (calendarId == 0 || Context.getCalendarManager().getById(calendarId) == null || ((Calendar) Context.getCalendarManager().getById(calendarId)) .checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); } } for (long geofenceId : oldGeofences) { long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); if (calendarId == 0 || Context.getCalendarManager().getById(calendarId) == null || ((Calendar) Context.getCalendarManager().getById(calendarId)) .checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); } } return events; }

Target:
protected Collection<Event> analyzePosition(Position position) { Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId()); if (device == null) { return null; } if (!Context.getIdentityManager().isLatestPosition(position) || !position.getValid()) { return null; } List<Long> currentGeofences = geofenceManager.getCurrentDeviceGeofences(position); List<Long> oldGeofences = new ArrayList<>(); if (device.getGeofenceIds() != null) { oldGeofences.addAll(device.getGeofenceIds()); } List<Long> newGeofences = new ArrayList<>(currentGeofences); newGeofences.removeAll(oldGeofences); oldGeofences.removeAll(currentGeofences); device.setGeofenceIds(currentGeofences); Collection<Event> events = new ArrayList<>(); for (long geofenceId : newGeofences) { long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); Calendar calendar = calendarId != 0 ? (Calendar) Context.getCalendarManager().getById(calendarId) : null; if (calendar == null || calendar.checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); } } for (long geofenceId : oldGeofences) { long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); Calendar calendar = calendarId != 0 ? (Calendar) Context.getCalendarManager().getById(calendarId) : null; if (calendar == null || calendar.checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); } } return events; }