Skip to content

Commit

Permalink
Merge pull request #17741 from osmandapp/bearing_heading_fix
Browse files Browse the repository at this point in the history
Fir write heading
  • Loading branch information
alex-osm committed Jul 31, 2023
2 parents 455e680 + a148487 commit 921e711
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import androidx.annotation.Nullable;

import net.osmand.IndexConstants;
import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.data.LatLon;
import net.osmand.gpx.GPXFile;
Expand Down Expand Up @@ -81,9 +82,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
private static final String POINT_COL_ICON = "icon";
private static final String POINT_COL_BACKGROUND = "background";

private static final float NO_HEADING = -1.0f;

public static final NumberFormat DECIMAL_FORMAT = new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US));
private static final NumberFormat DECIMAL_FORMAT = new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US));


private final OsmandApplication app;
Expand Down Expand Up @@ -386,9 +385,7 @@ private void collectDBTracks(@NonNull SQLiteDatabase db, @NonNull Map<String, GP
pt.speed = query.getDouble(3);
pt.hdop = query.getDouble(4);
pt.time = query.getLong(5);

float heading = query.getFloat(6);
pt.heading = heading == NO_HEADING ? Float.NaN : heading;
pt.heading = query.isNull(6) ? Float.NaN : query.getFloat(6);

Map<String, String> extensions = getPluginsExtensions(query.getString(7));
pt.getExtensionsToWrite().putAll(extensions);
Expand Down Expand Up @@ -480,23 +477,36 @@ private void dropEmptyTracks(@NonNull Map<String, GPXFile> dataTracks) {
public void startNewSegment() {
lastTimeUpdated = 0;
lastPoint = null;
executeInsertTrackQuery(0, 0, 0, 0, 0, System.currentTimeMillis(), NO_HEADING, null);
executeInsertTrackQuery(0, 0, 0, 0, 0, System.currentTimeMillis(), Float.NaN, null);
addTrackPoint(null, true, System.currentTimeMillis());
}

public void updateLocation(net.osmand.Location location, Float heading) {
public void updateLocation(@Nullable Location location, @Nullable Float heading) {
// use because there is a bug on some devices with location.getTime()
long locationTime = System.currentTimeMillis();

OsmandDevelopmentPlugin plugin = PluginsHelper.getEnabledPlugin(OsmandDevelopmentPlugin.class);
boolean writeHeading = plugin != null && plugin.SAVE_HEADING_TO_GPX.get();
heading = heading != null && writeHeading ? MapUtils.normalizeDegrees360(heading) : NO_HEADING;

if (app.getRoutingHelper().isFollowingMode()) {
lastRoutingApplicationMode = settings.getApplicationMode();
} else if (settings.getApplicationMode() == settings.DEFAULT_APPLICATION_MODE.get()) {
lastRoutingApplicationMode = null;
}
boolean record = shouldRecordLocation(location, locationTime);
if (record) {
heading = getAdjustedHeading(heading);

WptPt wptPt = new WptPt(location.getLatitude(), location.getLongitude(), locationTime,
location.getAltitude(), location.getSpeed(), location.getAccuracy(), heading);

String pluginsInfo = getPluginsInfo(location);
Map<String, String> extensions = getPluginsExtensions(pluginsInfo);
wptPt.getExtensionsToWrite().putAll(extensions);

insertData(wptPt, pluginsInfo);
app.getNotificationHelper().refreshNotification(NotificationType.GPX);
}
}

private boolean shouldRecordLocation(@Nullable Location location, long locationTime) {
boolean record = false;
if (location != null && SimulationProvider.isNotSimulatedLocation(location)
&& PluginsHelper.isActive(OsmandMonitoringPlugin.class)) {
Expand All @@ -507,8 +517,8 @@ record = true;
record = true;
}
float minDistance = settings.SAVE_TRACK_MIN_DISTANCE.get();
if (minDistance > 0 && lastPoint != null && MapUtils.getDistance(lastPoint, location.getLatitude(), location.getLongitude()) <
minDistance) {
if (minDistance > 0 && lastPoint != null
&& MapUtils.getDistance(lastPoint, location.getLatitude(), location.getLongitude()) < minDistance) {
record = false;
}
float precision = settings.SAVE_TRACK_PRECISION.get();
Expand All @@ -520,30 +530,30 @@ record = false;
record = false;
}
}
if (record) {
JSONObject json = new JSONObject();
PluginsHelper.attachAdditionalInfoToRecordedTrack(location, json);
return record;
}

boolean writeBearing = plugin != null && plugin.SAVE_BEARING_TO_GPX.get();
if (writeBearing && location.hasBearing()) {
try {
json.put(TRACK_COL_BEARING, DECIMAL_FORMAT.format(location.getBearing()));
} catch (JSONException e) {
log.error(e.getMessage(), e);
}
}
String pluginsInfo = json.length() > 0 ? json.toString() : null;
heading = heading == NO_HEADING ? Float.NaN : heading;
private float getAdjustedHeading(@Nullable Float heading) {
OsmandDevelopmentPlugin plugin = PluginsHelper.getEnabledPlugin(OsmandDevelopmentPlugin.class);
boolean writeHeading = plugin != null && plugin.SAVE_HEADING_TO_GPX.get();
return heading != null && writeHeading ? MapUtils.normalizeDegrees360(heading) : Float.NaN;
}

WptPt wptPt = new WptPt(location.getLatitude(), location.getLongitude(), locationTime,
location.getAltitude(), location.getSpeed(), location.getAccuracy(), heading);
@Nullable
private String getPluginsInfo(@NonNull net.osmand.Location location) {
JSONObject json = new JSONObject();
PluginsHelper.attachAdditionalInfoToRecordedTrack(location, json);

Map<String, String> extensions = getPluginsExtensions(pluginsInfo);
wptPt.getExtensionsToWrite().putAll(extensions);

insertData(wptPt, pluginsInfo);
app.getNotificationHelper().refreshNotification(NotificationType.GPX);
OsmandDevelopmentPlugin plugin = PluginsHelper.getEnabledPlugin(OsmandDevelopmentPlugin.class);
boolean writeBearing = plugin != null && plugin.SAVE_BEARING_TO_GPX.get();
if (writeBearing && location.hasBearing()) {
try {
json.put(TRACK_COL_BEARING, DECIMAL_FORMAT.format(location.getBearing()));
} catch (JSONException e) {
log.error(e.getMessage(), e);
}
}
return json.length() > 0 ? json.toString() : null;
}

private void insertData(@NonNull WptPt wptPt, @Nullable String pluginsInfo) {
Expand Down Expand Up @@ -764,7 +774,7 @@ private void executeInsertTrackQuery(double lat, double lon, double alt, double
rowsMap.put(TRACK_COL_SPEED, speed);
rowsMap.put(TRACK_COL_HDOP, hdop);
rowsMap.put(TRACK_COL_DATE, time);
rowsMap.put(TRACK_COL_HEADING, heading);
rowsMap.put(TRACK_COL_HEADING, Float.isNaN(heading) ? null : heading);
rowsMap.put(TRACK_COL_PLUGINS_INFO, pluginsInfo);
execWithClose(AndroidUtils.createDbInsertQuery(TRACK_NAME, rowsMap.keySet()), rowsMap.values().toArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -82,6 +83,7 @@ public View getCustomRadioButtonView(@NonNull SwitchPreferenceEx preference, boo

TextView textView = view.findViewById(R.id.text);
textView.setText(getSummary(preference, enabled));
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.default_list_text_size));

int margin = getResources().getDimensionPixelSize(R.dimen.content_padding_small);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
Expand Down Expand Up @@ -117,6 +119,7 @@ private void onRadioButtonClick(@NonNull View view, @NonNull SwitchPreferenceEx
}
}
}
dismiss();
}

private void updatePreferenceButtons(boolean checked) {
Expand Down

0 comments on commit 921e711

Please sign in to comment.