Ejemplo n.º 1
0
  public static boolean addLocation(Location location, Geocoder geocoder) {
    TrackingData ltd = getLastLocation();

    float distance = 0.0f;
    float speed = 0.0f;

    if (ltd != null) {
      float[] results = new float[1];
      Location.distanceBetween(
          ltd.getLatitude(),
          ltd.getLongitude(),
          location.getLatitude(),
          location.getLongitude(),
          results);

      distance = results[0];
      if (distance < 100) {
        log.debug("SKIP SAME " + location + ": " + distance);
        return false;
      }

      long delta = DateTimes.subSeconds(new Date(location.getTime()), ltd.getDate());
      // less than 30 minutes
      if (delta < 30 * 60) {
        speed = distance / delta;
        //				if (lastState == DetectedActivity.STILL) {
        //
        //				}
        // great than 120km/h
        if (speed >= 33) {
          log.debug("SKIP FAST " + location + ": " + distance);
          return false;
        }
      }
    }

    String address = "";
    try {
      List<Address> addresses =
          geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
      if (Collections.isNotEmpty(addresses)) {
        Address a = addresses.get(0);
        address = toAddress(a);
      }

      // if (ltd != null && Strings.isNotEmpty(address)) {
      // if (ltd.getAddress().equals(address)) {
      // log.debug("Skip (" + location + "): " + address);
      // return;
      // }
      // }
    } catch (Exception e) {
      // Catch network or other I/O problems.
      log.error(
          "Failed to get address of (" + location.getLatitude() + ", " + location.getLongitude(),
          e);
    }

    TrackingData td = new TrackingData();
    td.setDate(new Date(location.getTime()));
    td.setState(getBestState());
    td.setLatitude(location.getLatitude());
    td.setLongitude(location.getLongitude());
    td.setAddress(address);

    if (ltd != null) {
      float[] results = new float[1];
      Location.distanceBetween(
          ltd.getLatitude(), ltd.getLongitude(), td.getLatitude(), td.getLongitude(), results);
      td.setDistance(results[0]);
      td.setSpeed(td.getDistance() / DateTimes.subSeconds(td.getDate(), ltd.getDate()));
    }

    trackings.add(td);
    saveTrackingData(td);

    return true;
  }