public static void setState(ActivityRecognitionResult result) {
    DetectedActivity da = result.getMostProbableActivity();

    int state = da.getType();

    log.debug(
        "DetectedActivity: "
            + DateTimes.timeFormat().format(result.getTime())
            + " - "
            + getStateText(state)
            + ", "
            + da.getConfidence()
            + ", "
            + DateTimes.timeFormat().format(result.getElapsedRealtimeMillis()));

    if (Collections.isEmpty(states)) {
      states.put(state, 0L);
      lastState = state;
      lastStart = System.currentTimeMillis();
      return;
    }

    Long ltime = states.get(lastState);
    if (ltime != null) {
      ltime += (System.currentTimeMillis() - lastStart);
      states.put(lastState, ltime);
    }

    lastState = state;
    lastStart = System.currentTimeMillis();

    if (!states.containsKey(state)) {
      states.put(state, 0L);
    }
  }
  private static int getBestState() {
    if (Collections.isEmpty(states)) {
      return DetectedActivity.UNKNOWN;
    }

    long max = 0;
    for (Long time : states.values()) {
      if (time > max) {
        max = time;
      }
    }

    int state = DetectedActivity.UNKNOWN;
    for (Entry<Integer, Long> en : states.entrySet()) {
      if (max == en.getValue()) {
        state = en.getKey();
        break;
      }
    }

    states.clear();

    states.put(lastState, 0L);
    lastStart = System.currentTimeMillis();

    return state;
  }
 public static TrackingData getFirstLocation() {
   if (Collections.isNotEmpty(trackings)) {
     return trackings.get(0);
   }
   return null;
 }
  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;
  }
 public static TrackingData getLastLocation() {
   if (Collections.isNotEmpty(trackings)) {
     return trackings.get(trackings.size() - 1);
   }
   return null;
 }