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