Program: 32260
Project: 32260
Buggy source:
@Override public void onSuccess(List<Position> result){
  updateFailureCount=0;
  alertsMap.clear();
  long currentTime=System.currentTimeMillis();
  int selectedIndex=-1;
  for (int i=0; i < result.size(); i++) {
    Position position=result.get(i);
    Device device=position.getDevice();
    if (device.equals(selectedDevice)) {
      selectedIndex=i;
    }
    long timeout=(long)position.getDevice().getTimeout() * 1000;
    boolean isOffline=currentTime - position.getTime().getTime() > timeout;
    position.setStatus(isOffline ? Position.Status.OFFLINE : Position.Status.LATEST);
    position.setIcon(MarkerIcon.create(position).setName(device.isShowName()));
    deviceVisibilityHandler.offlineStatusChanged(device,isOffline);
    if (position.getIdleStatus() == Position.IdleStatus.MOVING) {
      deviceVisibilityHandler.moving(device);
    }
 else     if (position.getIdleStatus() == Position.IdleStatus.IDLE) {
      deviceVisibilityHandler.idle(device);
    }
    device=deviceStore.findModelWithKey(Long.toString(device.getId()));
    device.setOdometer(position.getDistance());
    for (    Maintenance maintenance : device.getMaintenances()) {
      if (device.getOdometer() >= maintenance.getLastService() + maintenance.getServiceInterval()) {
        alertsMap.put(device.getId(),position);
        break;
      }
    }
  }
  if (selectedIndex >= 0) {
    result.add(result.remove(selectedIndex));
  }
  mapView.clearLatestPositions();
  mapView.showLatestPositions(result,alertsMap.values());
  for (  Position position : result) {
    Device device=position.getDevice();
    Position prevPosition=latestPositionMap.get(device.getId());
    if (prevPosition != null && prevPosition.getId() != position.getId()) {
      if (ApplicationContext.getInstance().isFollowing(device)) {
        mapView.catchPosition(position);
        mapView.zoomIn(device);
      }
      if (ApplicationContext.getInstance().isRecordingTrace(device)) {
        mapView.showLatestTrackPositions(Collections.singletonList(prevPosition));
        mapView.showLatestTrack(new Track(Arrays.asList(prevPosition,position)));
        Short traceInterval=ApplicationContext.getInstance().getUserSettings().getTraceInterval();
        if (traceInterval != null) {
          mapView.clearLatestTrackPositions(device,new Date(position.getTime().getTime() - traceInterval * 60 * 1000));
        }
      }
    }
    if (ApplicationContext.getInstance().isRecordingTrace(device)) {
      Position prevTimestampPosition=timestampMap.get(device.getId());
      if (prevTimestampPosition == null || (position.getTime().getTime() - prevTimestampPosition.getTime().getTime() >= ApplicationContext.getInstance().getUserSettings().getTimePrintInterval() * 60 * 1000)) {
        mapView.showLatestTime(Collections.singletonList(position));
        timestampMap.put(device.getId(),position);
      }
    }
    latestPositionMap.put(device.getId(),position);
  }
  updateTimer.schedule(ApplicationContext.getInstance().getApplicationSettings().getUpdateInterval());
}

Fixed version (ground truth):
@Override public void onSuccess(List<Position> result){
  updateFailureCount=0;
  alertsMap.clear();
  long currentTime=System.currentTimeMillis();
  int selectedIndex=-1;
  for (int i=0; i < result.size(); i++) {
    Position position=result.get(i);
    Device device=position.getDevice();
    if (device.equals(selectedDevice)) {
      selectedIndex=i;
    }
    long timeout=(long)position.getDevice().getTimeout() * 1000;
    boolean isOffline=currentTime - position.getTime().getTime() > timeout;
    position.setStatus(isOffline ? Position.Status.OFFLINE : Position.Status.LATEST);
    position.setIcon(MarkerIcon.create(position).setName(device.isShowName()));
    deviceVisibilityHandler.offlineStatusChanged(device,isOffline);
    if (position.getIdleStatus() == Position.IdleStatus.MOVING) {
      deviceVisibilityHandler.moving(device);
    }
 else     if (position.getIdleStatus() == Position.IdleStatus.IDLE) {
      deviceVisibilityHandler.idle(device);
    }
    Device storedDevice=deviceStore.findModelWithKey(Long.toString(device.getId()));
    if (storedDevice != null) {
      storedDevice.setOdometer(position.getDistance());
      for (      Maintenance maintenance : storedDevice.getMaintenances()) {
        if (storedDevice.getOdometer() >= maintenance.getLastService() + maintenance.getServiceInterval()) {
          alertsMap.put(device.getId(),position);
          break;
        }
      }
    }
  }
  if (selectedIndex >= 0) {
    result.add(result.remove(selectedIndex));
  }
  mapView.clearLatestPositions();
  mapView.showLatestPositions(result,alertsMap.values());
  for (  Position position : result) {
    Device device=position.getDevice();
    Position prevPosition=latestPositionMap.get(device.getId());
    if (prevPosition != null && prevPosition.getId() != position.getId()) {
      if (ApplicationContext.getInstance().isFollowing(device)) {
        mapView.catchPosition(position);
        mapView.zoomIn(device);
      }
      if (ApplicationContext.getInstance().isRecordingTrace(device)) {
        mapView.showLatestTrackPositions(Collections.singletonList(prevPosition));
        mapView.showLatestTrack(new Track(Arrays.asList(prevPosition,position)));
        Short traceInterval=ApplicationContext.getInstance().getUserSettings().getTraceInterval();
        if (traceInterval != null) {
          mapView.clearLatestTrackPositions(device,new Date(position.getTime().getTime() - traceInterval * 60 * 1000));
        }
      }
    }
    if (ApplicationContext.getInstance().isRecordingTrace(device)) {
      Position prevTimestampPosition=timestampMap.get(device.getId());
      if (prevTimestampPosition == null || (position.getTime().getTime() - prevTimestampPosition.getTime().getTime() >= ApplicationContext.getInstance().getUserSettings().getTimePrintInterval() * 60 * 1000)) {
        mapView.showLatestTime(Collections.singletonList(position));
        timestampMap.put(device.getId(),position);
      }
    }
    latestPositionMap.put(device.getId(),position);
  }
  updateTimer.schedule(ApplicationContext.getInstance().getApplicationSettings().getUpdateInterval());
}