Skip to content

Commit

Permalink
Simplify/refactor ClickableWay structure
Browse files Browse the repository at this point in the history
  • Loading branch information
RZR-UA committed Jan 15, 2025
1 parent 9d10303 commit 285209c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 47 deletions.
4 changes: 4 additions & 0 deletions OsmAnd/src/net/osmand/plus/track/clickable/ClickableWay.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public long getOsmId() {
return osmId;
}

public QuadRect getBbox() {
return bbox;
}

public GpxFile getGpxFile() {
return gpxFile;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,45 @@
package net.osmand.plus.track.clickable;

import static net.osmand.IndexConstants.GPX_FILE_EXT;

import android.graphics.PointF;
import android.os.AsyncTask;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import net.osmand.CallbackWithObject;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.data.RotatedTileBox;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.track.helpers.GpxUiHelper;
import net.osmand.plus.utils.FileUtils;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.plus.views.layers.ContextMenuLayer;
import net.osmand.shared.gpx.GpxFile;
import net.osmand.shared.gpx.GpxTrackAnalysis;
import net.osmand.shared.gpx.primitives.WptPt;

import java.io.File;
import java.util.List;

public class ClickableWayActivator implements ContextMenuLayer.IContextMenuProvider {
private OsmandApplication app;
private OsmandMapTileView view;
private final OsmandMapTileView view;
private final CallbackWithObject<ClickableWay> readHeights;
private final CallbackWithObject<ClickableWay> openAsGpxFile;

public ClickableWayActivator(OsmandApplication app, OsmandMapTileView view) {
this.app = app;
public ClickableWayActivator(@NonNull OsmandMapTileView view,
@NonNull CallbackWithObject<ClickableWay> readHeightData,
@NonNull CallbackWithObject<ClickableWay> openAsGpxFile) {
this.view = view;
this.readHeights = readHeightData;
this.openAsGpxFile = openAsGpxFile;
// could be derived from OsmandMapLayer(ctx) in case of necessity
}

@Override
public boolean showMenuAction(@Nullable Object object) {
if (object instanceof ClickableWay that) {

MapActivity mapActivity = view.getMapActivity();

CallbackWithObject<ClickableWay> callback = clickableWay -> {
GpxFile gpxFile = that.getGpxFile();
// gpxFile.setWidth("bold"); // TODO just a test
GpxTrackAnalysis analysis = gpxFile.getAnalysis(0);
String safeFileName = that.getGpxFileName() + GPX_FILE_EXT;
File file = new File(FileUtils.getTempDir(app), safeFileName);
WptPt selectedPoint = that.getSelectedGpxPoint().getSelectedPoint();
GpxUiHelper.saveAndOpenGpx(mapActivity, file, gpxFile, selectedPoint, analysis, null, true);
if (mapActivity != null) {
(new ClickableWayReaderTask(mapActivity, that, readHeights, openAsGpxFile))
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return true;
};

ClickableWayReaderTask readerTask = new ClickableWayReaderTask(mapActivity, that, callback);
readerTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return true;
}
}
return false;
}
Expand Down
36 changes: 31 additions & 5 deletions OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayLoader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.osmand.plus.track.clickable;

import static net.osmand.IndexConstants.GPX_FILE_EXT;
import static net.osmand.data.MapObject.AMENITY_ID_RIGHT_SHIFT;

// THINK use similar icon="piste_high_difficulty" for no-name pistes
Expand All @@ -16,9 +17,13 @@
import net.osmand.data.QuadRect;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.Version;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.track.helpers.GpxUiHelper;
import net.osmand.plus.utils.FileUtils;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.plus.views.layers.ContextMenuLayer;
import net.osmand.shared.gpx.GpxFile;
import net.osmand.shared.gpx.GpxTrackAnalysis;
import net.osmand.shared.gpx.GpxUtilities;
import net.osmand.shared.gpx.RouteActivityHelper;
import net.osmand.shared.gpx.primitives.RouteActivity;
Expand All @@ -28,6 +33,7 @@
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -54,14 +60,14 @@ public class ClickableWayLoader {
// others are default (red)
);

// TODO implement simple cache <id, GpxFile>

private final OsmandApplication app;
private final OsmandMapTileView view;
private final ClickableWayActivator activator;

public ClickableWayLoader(@NonNull OsmandApplication app, @NonNull OsmandMapTileView view) {
this.app = app;
this.activator = new ClickableWayActivator(app, view);
this.view = view;
this.activator = new ClickableWayActivator(view, this::readHeightData, this::openAsGpxFile);
}

@NonNull
Expand Down Expand Up @@ -89,7 +95,8 @@ public ClickableWay loadClickableWayV1(@NonNull LatLon selectedLatLon, @NonNull
}

@Nullable
public ClickableWay loadClickableWayV2(@NonNull LatLon selectedLatLon, @NonNull ObfMapObject obfMapObject,
public ClickableWay loadClickableWayV2(@NonNull LatLon selectedLatLon,
@NonNull ObfMapObject obfMapObject,
@NonNull Map<String, String> tags) {
long id = obfMapObject.getId().getId().longValue();
long osmId = ObfConstants.getOsmId(id >> AMENITY_ID_RIGHT_SHIFT);
Expand Down Expand Up @@ -149,7 +156,7 @@ private ClickableWay loadClickableWay(LatLon selectedLatLon, QuadRect bbox,
}

@Nullable
private String getGpxColorByTags(Map <String, String> tags) {
private String getGpxColorByTags(Map<String, String> tags) {
for (String t : clickableTags) {
String val = tags.get(t);
if (val != null) {
Expand Down Expand Up @@ -184,4 +191,23 @@ private boolean isClickableWayTags(@NonNull Map<String, String> tags) {
}
return false;
}

private boolean readHeightData(ClickableWay clickableWay) {
// TODO read height data, implement simple cache
return true;
}

private boolean openAsGpxFile(ClickableWay clickableWay) {
MapActivity mapActivity = view.getMapActivity();
if (mapActivity != null) {
GpxFile gpxFile = clickableWay.getGpxFile();
GpxTrackAnalysis analysis = gpxFile.getAnalysis(0);
String safeFileName = clickableWay.getGpxFileName() + GPX_FILE_EXT;
File file = new File(FileUtils.getTempDir(app), safeFileName);
WptPt selectedPoint = clickableWay.getSelectedGpxPoint().getSelectedPoint();
GpxUiHelper.saveAndOpenGpx(mapActivity, file, gpxFile, selectedPoint, analysis, null, true);
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
package net.osmand.plus.track.clickable;

import androidx.annotation.NonNull;

import net.osmand.CallbackWithObject;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.BaseLoadAsyncTask;

public class ClickableWayReaderTask extends BaseLoadAsyncTask<Void, Void, ClickableWay> {
private final ClickableWay clickableWay;
private final CallbackWithObject<ClickableWay> callback;
private final CallbackWithObject<ClickableWay> readHeightData;
private final CallbackWithObject<ClickableWay> openAsGpxFile;

public ClickableWayReaderTask(MapActivity mapActivity, ClickableWay clickableWay,
CallbackWithObject<ClickableWay> callback) {
public ClickableWayReaderTask(@NonNull MapActivity mapActivity,
@NonNull ClickableWay clickableWay,
@NonNull CallbackWithObject<ClickableWay> readHeightData,
@NonNull CallbackWithObject<ClickableWay> openAsGpxFile) {
super(mapActivity);
this.callback = callback;
this.clickableWay = clickableWay;
this.readHeightData = readHeightData;
this.openAsGpxFile = openAsGpxFile;
}

@Override
protected ClickableWay doInBackground(Void... voids) {
// TODO read height data, get analytics (RouteSectionReader.java?)
// for (int i = 0; i < 3; i++) {
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// }
readHeightData.processResult(clickableWay);
return this.clickableWay;
}

Expand All @@ -37,9 +36,7 @@ protected void onPreExecute() {

@Override
protected void onPostExecute(ClickableWay clickableWay) {
if (callback != null) {
callback.processResult(clickableWay);
}
openAsGpxFile.processResult(clickableWay);
hideProgress();
}
}

0 comments on commit 285209c

Please sign in to comment.