コード例 #1
0
  private static void saveTrackingData(TrackingData td) {
    String s = Jsons.toJson(td);

    Writer r = null;
    try {
      log.info("ADD: " + s);

      File file = new File(getTrackingFile(Calendar.getInstance()));
      r = new OutputStreamWriter(new FileOutputStream(file, true), Charsets.UTF_8);
      r.append(s);
      r.append(Streams.LINE_SEPARATOR);
    } catch (IOException e) {
      log.error("Failed to save " + s, e);
    } finally {
      Streams.safeClose(r);
    }
  }
コード例 #2
0
  public static void loadTrackings(Calendar c) {
    trackings.clear();

    File file = new File(getTrackingFile(c));
    if (!file.exists()) {
      log.warn(file + "does not exist");
      return;
    }

    log.info("Loading " + file);

    float[] results = new float[1];
    LineIterator li = null;
    try {
      li = Files.lineIterator(file);
      while (li.hasNext()) {
        String line = li.next();
        if (Strings.isEmpty(line)) {
          continue;
        }

        TrackingData td = Jsons.fromJson(line, TrackingData.class);
        TrackingData ltd = getLastLocation();
        if (ltd != null) {
          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);
      }
    } catch (IOException e) {
      log.error(e);
    } finally {
      Streams.safeClose(li);
    }
  }