use of org.y20k.trackbook.core.WayPoint in project trackbook by y20k.
the class ExportHelper method addTrack.
/* Creates Track */
private static String addTrack(Track track) {
StringBuilder gpxTrack = new StringBuilder("");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
// add opening track tag
gpxTrack.append("\t<trk>\n");
// add name to track
gpxTrack.append("\t\t<name>");
gpxTrack.append("Trackbook Recording");
gpxTrack.append("</name>\n");
// add opening track segment tag
gpxTrack.append("\t\t<trkseg>\n");
// add route point
for (WayPoint wayPoint : track.getWayPoints()) {
// get location from waypoint
Location location = wayPoint.getLocation();
// add longitude and latitude
gpxTrack.append("\t\t\t<trkpt lat=\"");
gpxTrack.append(location.getLatitude());
gpxTrack.append("\" lon=\"");
gpxTrack.append(location.getLongitude());
gpxTrack.append("\">\n");
// add time
gpxTrack.append("\t\t\t\t<time>");
gpxTrack.append(dateFormat.format(new Date(location.getTime())));
gpxTrack.append("</time>\n");
// add altitude
gpxTrack.append("\t\t\t\t<ele>");
gpxTrack.append(location.getAltitude());
gpxTrack.append("</ele>\n");
// add closing tag
gpxTrack.append("\t\t\t</trkpt>\n");
}
// add closing track segment tag
gpxTrack.append("\t\t</trkseg>\n");
// add closing track tag
gpxTrack.append("\t</trk>\n");
return gpxTrack.toString();
}
use of org.y20k.trackbook.core.WayPoint in project trackbook by y20k.
the class MapHelper method createTrackOverlay.
/* Creates icon overlay for track */
public static ItemizedIconOverlay createTrackOverlay(final Context context, Track track, boolean trackingActive) {
WayPoint wayPoint;
boolean currentPosition;
final int trackSize = track.getSize();
final List<WayPoint> wayPoints = track.getWayPoints();
final ArrayList<OverlayItem> overlayItems = new ArrayList<>();
for (int i = 0; i < track.getSize(); i++) {
// get WayPoint and check if it is current position
wayPoint = wayPoints.get(i);
currentPosition = i == trackSize - 1;
// create marker
Drawable newMarker;
// CASE 1: Tracking active and WayPoint is not current position
if (trackingActive && !currentPosition) {
if (wayPoint.getIsStopOver()) {
// stop over marker
newMarker = ContextCompat.getDrawable(context, R.drawable.ic_my_location_crumb_grey_24dp);
} else {
// default marker for this case
newMarker = ContextCompat.getDrawable(context, R.drawable.ic_my_location_crumb_red_24dp);
}
} else // CASE 2: Tracking active and WayPoint is current position
if (trackingActive && currentPosition) {
if (wayPoint.getIsStopOver()) {
// stop over marker
newMarker = ContextCompat.getDrawable(context, R.drawable.ic_my_location_dot_grey_24dp);
} else {
// default marker for this case
newMarker = ContextCompat.getDrawable(context, R.drawable.ic_my_location_dot_red_24dp);
}
} else // CASE 3: Tracking not active and WayPoint is not current position
if (!trackingActive && !currentPosition) {
if (wayPoint.getIsStopOver()) {
// stop over marker
newMarker = ContextCompat.getDrawable(context, R.drawable.ic_my_location_crumb_grey_24dp);
} else {
// default marker for this case
newMarker = ContextCompat.getDrawable(context, R.drawable.ic_my_location_crumb_blue_24dp);
}
} else // CASE 4: Tracking not active and WayPoint is current position
{
// default marker
newMarker = ContextCompat.getDrawable(context, R.drawable.ic_my_location_crumb_blue_24dp);
}
// create overlay item
OverlayItem overlayItem = createOverlayItem(context, wayPoint.getLocation());
overlayItem.setMarker(newMarker);
// add marker to list of overlay items
overlayItems.add(overlayItem);
}
// return overlay for current position
return new ItemizedIconOverlay<>(overlayItems, new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
// tap on waypoint
Toast.makeText(context, item.getTitle(), Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onItemLongPress(final int index, final OverlayItem item) {
// long press on waypoint
Toast.makeText(context, item.getSnippet(), Toast.LENGTH_LONG).show();
return true;
}
}, context);
}
use of org.y20k.trackbook.core.WayPoint in project trackbook by y20k.
the class TrackerService method startTracking.
/* Start tracking location */
// todo remove
private void startTracking(@Nullable Intent intent, boolean createNewTrack) {
LogHelper.v(LOG_TAG, "Service received command: START");
// create a new track - if requested
if (createNewTrack) {
mTrack = new Track();
} else {
StorageHelper storageHelper = new StorageHelper(this);
if (storageHelper.tempFileExists()) {
// load temp track file
mTrack = storageHelper.loadTrack(FILE_TEMP_TRACK);
// try to mark last waypoint as stopover
int lastWayPoint = mTrack.getWayPoints().size() - 1;
if (lastWayPoint >= 0) {
mTrack.getWayPoints().get(lastWayPoint).setIsStopOver(true);
}
} else {
// fallback, if tempfile did not exist
LogHelper.e(LOG_TAG, "Unable to find previously saved track temp file.");
mTrack = new Track();
}
}
// get last location
if (intent != null && ACTION_START.equals(intent.getAction()) && intent.hasExtra(EXTRA_LAST_LOCATION)) {
// received START intent and last location - unpack last location
mCurrentBestLocation = intent.getParcelableExtra(EXTRA_LAST_LOCATION);
} else if (ACTION_RESUME.equals(intent.getAction()) && mTrack.getSize() > 0) {
// received RESUME intent - use last waypoint
mCurrentBestLocation = mTrack.getWayPointLocation(mTrack.getSize() - 1);
}
// get last location - fallback
if (mCurrentBestLocation == null) {
mCurrentBestLocation = LocationHelper.determineLastKnownLocation(mLocationManager);
}
// add last location as WayPoint to track
addWayPointToTrack();
// put up notification
mNotificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANEL_ID_RECORDING_CHANNEL);
mNotification = NotificationHelper.getNotification(this, mNotificationBuilder, mTrack, true);
// todo check if necessary in pre Android O
mNotificationManager.notify(TRACKER_SERVICE_NOTIFICATION_ID, mNotification);
// get duration of previously recorded track - in case this service has been restarted / resumed
final long previouslyRecordedDuration = mTrack.getTrackDuration();
// set timer to retrieve new locations and to prevent endless tracking
mTimer = new CountDownTimer(EIGHT_HOURS_IN_MILLISECONDS, FIFTEEN_SECONDS_IN_MILLISECONDS) {
@Override
public void onTick(long millisUntilFinished) {
// update track duration - and add duration from previously interrupted / paused session
long duration = EIGHT_HOURS_IN_MILLISECONDS - millisUntilFinished + previouslyRecordedDuration;
mTrack.setDuration(duration);
// try to add WayPoint to Track
addWayPointToTrack();
// update notification
mNotification = NotificationHelper.getUpdatedNotification(TrackerService.this, mNotificationBuilder, mTrack);
mNotificationManager.notify(TRACKER_SERVICE_NOTIFICATION_ID, mNotification);
// save a temp file in case the service has been killed by the system
SaveTempTrackAsyncHelper saveTempTrackAsyncHelper = new SaveTempTrackAsyncHelper();
saveTempTrackAsyncHelper.execute();
}
@Override
public void onFinish() {
// stop tracking after eight hours
stopTracking();
}
};
mTimer.start();
// initialize step counter
mStepCountOffset = 0;
boolean stepCounterAvailable;
stepCounterAvailable = mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(TYPE_STEP_COUNTER), SensorManager.SENSOR_DELAY_UI);
if (stepCounterAvailable) {
LogHelper.v(LOG_TAG, "Pedometer sensor available: Registering listener.");
} else {
LogHelper.i(LOG_TAG, "Pedometer sensor not available.");
mTrack.setStepCount(-1);
}
// create gps and network location listeners
startFindingLocation();
// register content observer for changes in System Settings
this.getContentResolver().registerContentObserver(android.provider.Settings.Secure.CONTENT_URI, true, mSettingsContentObserver);
// start service in foreground
startForeground(TRACKER_SERVICE_NOTIFICATION_ID, mNotification);
}
use of org.y20k.trackbook.core.WayPoint in project trackbook by y20k.
the class TrackerService method resumeTracking.
/* Resume tracking after stop/pause */
public void resumeTracking() {
if (mLocationSystemSetting) {
LogHelper.v(LOG_TAG, "Recording resumed");
// create a new track - if requested
StorageHelper storageHelper = new StorageHelper(this);
if (storageHelper.tempFileExists()) {
// load temp track file
mTrack = storageHelper.loadTrack(FILE_TEMP_TRACK);
// try to mark last waypoint as stopover
int lastWayPoint = mTrack.getWayPoints().size() - 1;
if (lastWayPoint >= 0) {
mTrack.getWayPoints().get(lastWayPoint).setIsStopOver(true);
}
} else {
// fallback, if tempfile did not exist
LogHelper.e(LOG_TAG, "Unable to find previously saved track temp file.");
mTrack = new Track();
}
// get last location
if (mTrack.getSize() > 0) {
mCurrentBestLocation = mTrack.getWayPointLocation(mTrack.getSize() - 1);
} else {
mCurrentBestLocation = LocationHelper.determineLastKnownLocation(mLocationManager);
}
// initialize step counter
mStepCountOffset = mTrack.getStepCount();
// begin recording
recordMovements();
} else {
LogHelper.i(LOG_TAG, "Location Setting is turned off.");
Toast.makeText(getApplicationContext(), R.string.toast_message_location_offline, Toast.LENGTH_LONG).show();
}
}
use of org.y20k.trackbook.core.WayPoint in project trackbook by y20k.
the class TrackerService method addWayPointToTrack.
/* Adds a new WayPoint to current track */
private void addWayPointToTrack() {
// create new WayPoint
WayPoint newWayPoint = null;
// get number of previously tracked WayPoints
int trackSize = mTrack.getWayPoints().size();
if (trackSize == 0) {
// add first location to track
newWayPoint = mTrack.addWayPoint(mCurrentBestLocation);
} else {
// get last WayPoint and compare it to current location
Location lastWayPoint = mTrack.getWayPointLocation(trackSize - 1);
// default value for average speed
float averageSpeed = 0f;
// compute average speed if new location come from network provider
if (trackSize > 1 && mCurrentBestLocation.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
Location firstWayPoint = mTrack.getWayPointLocation(0);
float distance = firstWayPoint.distanceTo(lastWayPoint);
long timeDifference = lastWayPoint.getElapsedRealtimeNanos() - firstWayPoint.getElapsedRealtimeNanos();
averageSpeed = distance / ((float) timeDifference / ONE_NANOSECOND);
}
if (LocationHelper.isNewWayPoint(lastWayPoint, mCurrentBestLocation, averageSpeed)) {
// if new, add current best location to track
newWayPoint = mTrack.addWayPoint(mCurrentBestLocation);
}
}
// send local broadcast if new WayPoint added
if (newWayPoint != null) {
sendTrackUpdate();
}
}
Aggregations