/** recompute a lap aggregate based on locations */ private static void recomputeLap(SQLiteDatabase db, long activityId, long lap) { long sum_time = 0; double sum_distance = 0; final String[] cols = new String[] { DB.LOCATION.TIME, DB.LOCATION.LATITUDE, DB.LOCATION.LONGITUDE, DB.LOCATION.TYPE, "_id" }; Cursor c = db.query( DB.LOCATION.TABLE, cols, DB.LOCATION.ACTIVITY + " = " + activityId + " and " + DB.LOCATION.LAP + " = " + lap, null, null, null, "_id", null); if (c.moveToFirst()) { Location lastLocation = null; do { Location l = new Location("Dill poh"); l.setTime(c.getLong(0)); l.setLatitude(c.getDouble(1)); l.setLongitude(c.getDouble(2)); l.setProvider("" + c.getLong(3)); int type = c.getInt(3); switch (type) { case DB.LOCATION.TYPE_START: case DB.LOCATION.TYPE_RESUME: lastLocation = l; break; case DB.LOCATION.TYPE_END: case DB.LOCATION.TYPE_PAUSE: case DB.LOCATION.TYPE_GPS: if (lastLocation == null) { lastLocation = l; break; } sum_distance += l.distanceTo(lastLocation); sum_time += l.getTime() - lastLocation.getTime(); lastLocation = l; break; } } while (c.moveToNext()); } c.close(); ContentValues tmp = new ContentValues(); tmp.put(DB.LAP.DISTANCE, sum_distance); tmp.put(DB.LAP.TIME, (sum_time / 1000)); db.update( DB.LAP.TABLE, tmp, DB.LAP.ACTIVITY + " = " + activityId + " and " + DB.LAP.LAP + " = " + lap, null); }
private static int trimLap(SQLiteDatabase db, long activityId, long lap) { int cnt = 0; final String[] cols = new String[] { DB.LOCATION.TIME, DB.LOCATION.LATITUDE, DB.LOCATION.LONGITUDE, DB.LOCATION.TYPE, "_id" }; Cursor c = db.query( DB.LOCATION.TABLE, cols, DB.LOCATION.ACTIVITY + " = " + activityId + " and " + DB.LOCATION.LAP + " = " + lap, null, null, null, "_id", null); if (c.moveToFirst()) { Location p[] = {null, null}; do { Location l = new Location("Dill poh"); l.setTime(c.getLong(0)); l.setLatitude(c.getDouble(1)); l.setLongitude(c.getDouble(2)); l.setProvider("" + c.getLong(3)); int type = c.getInt(3); switch (type) { case DB.LOCATION.TYPE_START: case DB.LOCATION.TYPE_RESUME: p[0] = l; p[1] = null; break; case DB.LOCATION.TYPE_END: case DB.LOCATION.TYPE_PAUSE: case DB.LOCATION.TYPE_GPS: if (p[0] == null) { p[0] = l; p[1] = null; break; } else if (p[1] == null) { p[1] = l; } else { float d1 = p[0].distanceTo(p[1]); float d2 = p[0].distanceTo(l); if (Math.abs(d1 - d2) <= MIN_DISTANCE) { // p[1] is redundant...prune it p[1] = l; cnt++; } else { p[0] = p[1]; p[1] = null; } } break; } } while (c.moveToNext()); } c.close(); return cnt; }