test adding altitude and timestamp to new favorite

This commit is contained in:
Skalii 2021-02-24 15:02:39 +02:00
parent 775719b480
commit 0f81841c79
8 changed files with 248 additions and 131 deletions

View file

@ -4,6 +4,7 @@ import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion;
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteTypeRule;
import net.osmand.data.LatLon;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import net.osmand.util.TransliterationHelper;
@ -37,7 +38,9 @@ public class RouteDataObject {
public int[] nameIds;
// mixed array [0, height, cumulative_distance height, cumulative_distance, height, ...] - length is length(points)*2
public float[] heightDistanceArray = null;
public float heightByCurrentLocation;
private static final Log LOG = PlatformUtil.getLog(RouteDataObject.class);
public RouteDataObject(RouteRegion region) {
this.region = region;
}
@ -165,6 +168,10 @@ public class RouteDataObject {
}
public float[] calculateHeightArray() {
return calculateHeightArray(null);
}
public float[] calculateHeightArray(LatLon currentLocation) {
if (heightDistanceArray != null) {
return heightDistanceArray;
}
@ -178,7 +185,8 @@ public class RouteDataObject {
heightDistanceArray = new float[2 * getPointsLength()];
double plon = 0;
double plat = 0;
float prevHeight = startHeight;
float prevHeight = heightByCurrentLocation = startHeight;
double prevDistance = 0;
for (int k = 0; k < getPointsLength(); k++) {
double lon = MapUtils.get31LongitudeX(getPoint31XTile(k));
double lat = MapUtils.get31LatitudeY(getPoint31YTile(k));
@ -200,6 +208,15 @@ public class RouteDataObject {
}
heightDistanceArray[2 * k] = (float) dd;
heightDistanceArray[2 * k + 1] = height;
if (currentLocation != null) {
double distance = MapUtils.getDistance(currentLocation, lat, lon);
if (height != HEIGHT_UNDEFINED && distance < prevDistance) {
prevDistance = distance;
heightByCurrentLocation = height;
}
}
if (height != HEIGHT_UNDEFINED) {
// interpolate undefined
double totalDistance = dd;
@ -223,6 +240,9 @@ public class RouteDataObject {
}
plat = lat;
plon = lon;
if (currentLocation != null) {
prevDistance = MapUtils.getDistance(currentLocation, plat, plon);
}
}
return heightDistanceArray;
}

View file

@ -11,6 +11,9 @@ import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.Location;
import net.osmand.ResultMatcher;
import net.osmand.binary.RouteDataObject;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.settings.backend.BooleanPreference;
@ -42,6 +45,8 @@ public class FavouritePoint implements Serializable, LocationPoint {
private boolean visible = true;
private SpecialPointType specialPointType = null;
private BackgroundType backgroundType = null;
private double altitude;
private long timestamp;
public FavouritePoint() {
}
@ -57,6 +62,19 @@ public class FavouritePoint implements Serializable, LocationPoint {
initPersonalType();
}
public FavouritePoint(double latitude, double longitude, String name, String category, double altitude, long timestamp) {
this.latitude = latitude;
this.longitude = longitude;
this.category = category;
if (name == null) {
name = "";
}
this.name = name;
this.altitude = altitude;
this.timestamp = timestamp;
initPersonalType();
}
public FavouritePoint(FavouritePoint favouritePoint) {
this.latitude = favouritePoint.latitude;
this.longitude = favouritePoint.longitude;
@ -69,6 +87,8 @@ public class FavouritePoint implements Serializable, LocationPoint {
this.address = favouritePoint.address;
this.iconId = favouritePoint.iconId;
this.backgroundType = favouritePoint.backgroundType;
this.altitude = favouritePoint.altitude;
this.timestamp = favouritePoint.timestamp;
initPersonalType();
}
@ -82,6 +102,27 @@ public class FavouritePoint implements Serializable, LocationPoint {
}
}
public void initAltitude(final OsmandApplication app, final Runnable callback) {
app.getLocationProvider().getRouteSegment(new Location("", latitude, longitude), null, false, new ResultMatcher<RouteDataObject>() {
@Override
public boolean publish(RouteDataObject routeDataObject) {
if (routeDataObject != null) {
routeDataObject.calculateHeightArray(new LatLon(latitude, longitude));
altitude = routeDataObject.heightByCurrentLocation;
} else {
}
callback.run();
return true;
}
@Override
public boolean isCancelled() {
return false;
}
});
}
public SpecialPointType getSpecialPointType() {
return specialPointType;
}
@ -171,6 +212,22 @@ public class FavouritePoint implements Serializable, LocationPoint {
this.longitude = longitude;
}
public double getAltitude() {
return altitude;
}
public void setAltitude(double altitude) {
this.altitude = altitude;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getCategory() {
return category;
}
@ -256,7 +313,8 @@ public class FavouritePoint implements Serializable, LocationPoint {
} else if (!originObjectName.equals(fp.originObjectName))
return false;
return (this.latitude == fp.latitude) && (this.longitude == fp.longitude);
return (this.latitude == fp.latitude) && (this.longitude == fp.longitude) &&
(this.altitude == fp.altitude) && (this.timestamp == fp.timestamp);
}
@Override
@ -265,6 +323,8 @@ public class FavouritePoint implements Serializable, LocationPoint {
int result = 1;
result = prime * result + (int) Math.floor(latitude * 10000);
result = prime * result + (int) Math.floor(longitude * 10000);
result = prime * result + (int) Math.floor(altitude * 10000);
result = prime * result + (int) Math.floor(timestamp * 10000);
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((category == null) ? 0 : category.hashCode());
result = prime * result + ((description == null) ? 0 : description.hashCode());
@ -289,7 +349,9 @@ public class FavouritePoint implements Serializable, LocationPoint {
this.iconId = iconId;
}
public String getCategory() { return FavouritesDbHelper.FavoriteGroup.PERSONAL_CATEGORY; }
public String getCategory() {
return FavouritesDbHelper.FavoriteGroup.PERSONAL_CATEGORY;
}
public String getName() {
return typeName;
@ -384,7 +446,7 @@ public class FavouritePoint implements Serializable, LocationPoint {
name = "";
}
FavouritePoint fp;
fp = new FavouritePoint(pt.lat, pt.lon, name, categoryName);
fp = new FavouritePoint(pt.lat, pt.lon, name, categoryName, pt.ele, pt.time);
fp.setDescription(pt.desc);
if (pt.comment != null) {
fp.setOriginObjectName(pt.comment);
@ -405,6 +467,8 @@ public class FavouritePoint implements Serializable, LocationPoint {
WptPt pt = new WptPt();
pt.lat = getLatitude();
pt.lon = getLongitude();
pt.ele = getAltitude();
pt.time = getTimestamp();
if (!isVisible()) {
pt.getExtensionsToWrite().put(HIDDEN, "true");
}

View file

@ -671,7 +671,7 @@ public class ImportHelper {
}
public static List<FavouritePoint> asFavourites(OsmandApplication app, List<WptPt> wptPts, String fileName, boolean forceImportFavourites) {
List<FavouritePoint> favourites = new ArrayList<>();
final List<FavouritePoint> favourites = new ArrayList<>();
for (WptPt p : wptPts) {
if (Algorithms.isEmpty(p.name)) {
p.name = app.getResources().getString(R.string.shared_string_waypoint);
@ -687,7 +687,7 @@ public class ImportHelper {
} else {
fpCat = p.category;
}
FavouritePoint point = new FavouritePoint(p.lat, p.lon, p.name, fpCat);
final FavouritePoint point = new FavouritePoint(p.lat, p.lon, p.name, fpCat, p.ele, p.time);
if (p.desc != null) {
point.setDescription(p.desc);
}
@ -698,8 +698,18 @@ public class ImportHelper {
point.setIconIdFromName(app, iconName);
}
point.setBackgroundType(BackgroundType.getByTypeName(p.getBackgroundType(), DEFAULT_BACKGROUND_TYPE));
if (Double.isNaN(p.ele) || p.ele == 0) {
point.initAltitude(app, new Runnable() {
@Override
public void run() {
favourites.add(point);
}
});
} else {
favourites.add(point);
}
}
}
return favourites;
}

View file

@ -1036,6 +1036,8 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL
title = "";
}
String originObjectName = "";
double altitude = 0;
long timestamp = System.currentTimeMillis();
Object object = getObject();
if (object != null) {
if (object instanceof Amenity) {
@ -1043,10 +1045,13 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL
} else if (object instanceof TransportStop) {
originObjectName = ((TransportStop) object).toStringEn();
}
if (object instanceof WptPt) {
altitude = ((WptPt) object).ele;
}
}
FavoritePointEditor favoritePointEditor = getFavoritePointEditor();
if (favoritePointEditor != null) {
favoritePointEditor.add(getLatLon(), title, getStreetStr(), originObjectName);
favoritePointEditor.add(getLatLon(), title, getStreetStr(), originObjectName, altitude, timestamp);
}
}
});
@ -1074,7 +1079,8 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL
for (OsmandMapLayer layer : mapActivity.getMapView().getLayers()) {
layer.populateObjectContextMenu(latLon, getObject(), menuAdapter, mapActivity);
}
mapActivity.getMapActions().addActionsToAdapter(configure ? 0 : latLon.getLatitude(), configure ? 0 : latLon.getLongitude(), menuAdapter, configure ? null : getObject(), configure); }
mapActivity.getMapActions().addActionsToAdapter(configure ? 0 : latLon.getLatitude(), configure ? 0 : latLon.getLongitude(), menuAdapter, configure ? null : getObject(), configure);
}
return menuAdapter;
}

View file

@ -27,8 +27,8 @@ public class FavoritePointEditor extends PointEditor {
return favorite;
}
public void add(LatLon latLon, String title, String address, String originObjectName) {
MapActivity mapActivity = getMapActivity();
public void add(LatLon latLon, String title, String address, String originObjectName, double altitude, long timestamp) {
final MapActivity mapActivity = getMapActivity();
if (latLon == null || mapActivity == null) {
return;
}
@ -37,15 +37,24 @@ public class FavoritePointEditor extends PointEditor {
if (!Algorithms.isEmpty(lastCategory) && !app.getFavorites().groupExists(lastCategory)) {
lastCategory = "";
}
favorite = new FavouritePoint(latLon.getLatitude(), latLon.getLongitude(), title, lastCategory);
favorite = new FavouritePoint(latLon.getLatitude(), latLon.getLongitude(), title, lastCategory, (float) altitude, timestamp);
favorite.setDescription("");
favorite.setAddress(address.isEmpty() ? title : address);
favorite.setOriginObjectName(originObjectName);
if (Double.isNaN(altitude) || altitude == 0) {
favorite.initAltitude(app, new Runnable() {
@Override
public void run() {
FavoritePointEditorFragmentNew.showInstance(mapActivity);
}
});
} else {
FavoritePointEditorFragmentNew.showInstance(mapActivity);
}
}
public void add(LatLon latLon, String title, String originObjectName, String categoryName, int categoryColor, boolean autoFill) {
MapActivity mapActivity = getMapActivity();
public void add(LatLon latLon, String title, String originObjectName, String categoryName, int categoryColor, final boolean autoFill, double altitude, long timestamp) {
final MapActivity mapActivity = getMapActivity();
if (latLon == null || mapActivity == null) {
return;
}
@ -60,13 +69,21 @@ public class FavoritePointEditor extends PointEditor {
categoryName = "";
}
favorite = new FavouritePoint(latLon.getLatitude(), latLon.getLongitude(), title, categoryName);
favorite = new FavouritePoint(latLon.getLatitude(), latLon.getLongitude(), title, categoryName, (float) altitude, timestamp);
favorite.setDescription("");
favorite.setAddress("");
favorite.setOriginObjectName(originObjectName);
if (Double.isNaN(altitude) || altitude == 0) {
favorite.initAltitude(app, new Runnable() {
@Override
public void run() {
FavoritePointEditorFragmentNew.showAutoFillInstance(mapActivity, autoFill);
}
});
} else {
FavoritePointEditorFragmentNew.showAutoFillInstance(mapActivity, autoFill);
}
}
public void edit(FavouritePoint favorite) {
MapActivity mapActivity = getMapActivity();

View file

@ -190,7 +190,7 @@ public class FavoritePointEditorFragment extends PointEditorFragment {
final FavouritePoint favorite = getFavorite();
if (favorite != null) {
final FavouritePoint point = new FavouritePoint(favorite.getLatitude(), favorite.getLongitude(),
getNameTextValue(), getCategoryTextValue());
getNameTextValue(), getCategoryTextValue(), favorite.getAltitude(), favorite.getTimestamp());
point.setDescription(getDescriptionTextValue());
point.setAddress(getAddressTextValue());
AlertDialog.Builder builder = FavouritesDbHelper.checkDuplicates(point, helper, getMapActivity());

View file

@ -242,7 +242,7 @@ public class FavoritePointEditorFragmentNew extends PointEditorFragmentNew {
final FavouritePoint favorite = getFavorite();
if (favorite != null) {
final FavouritePoint point = new FavouritePoint(favorite.getLatitude(), favorite.getLongitude(),
getNameTextValue(), getCategoryTextValue());
getNameTextValue(), getCategoryTextValue(), favorite.getAltitude(), favorite.getTimestamp());
point.setDescription(isDescriptionAvailable() ? getDescriptionTextValue() : null);
point.setAddress(isAddressAvailable() ? getAddressTextValue() : null);
point.setColor(color);
@ -258,7 +258,7 @@ public class FavoritePointEditorFragmentNew extends PointEditorFragmentNew {
final FavouritePoint favorite = getFavorite();
if (favorite != null) {
final FavouritePoint point = new FavouritePoint(favorite.getLatitude(), favorite.getLongitude(),
getNameTextValue(), getCategoryTextValue());
getNameTextValue(), getCategoryTextValue(), favorite.getAltitude(), favorite.getTimestamp());
point.setDescription(isDescriptionAvailable() ? getDescriptionTextValue() : null);
point.setAddress(isAddressAvailable() ? getAddressTextValue() : null);
point.setColor(color);

View file

@ -119,7 +119,7 @@ public class FavoriteAction extends QuickAction {
FavoritePointEditor favoritePointEditor = mapActivity.getContextMenu().getFavoritePointEditor();
if (favoritePointEditor != null) {
favoritePointEditor.add(latLon, title, "", getParams().get(KEY_CATEGORY_NAME),
Integer.valueOf(getParams().get(KEY_CATEGORY_COLOR)), autoFill);
Integer.valueOf(getParams().get(KEY_CATEGORY_COLOR)), autoFill, 0, 0);
}
}