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 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); } }
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); } }
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; }