From 285209c1a112f4ed8e3da942f689dd3e79b6eb0f Mon Sep 17 00:00:00 2001 From: RZR-UA Date: Wed, 15 Jan 2025 15:00:02 +0100 Subject: [PATCH] Simplify/refactor ClickableWay structure --- .../plus/track/clickable/ClickableWay.java | 4 ++ .../clickable/ClickableWayActivator.java | 40 ++++++------------- .../track/clickable/ClickableWayLoader.java | 36 ++++++++++++++--- .../clickable/ClickableWayReaderTask.java | 27 ++++++------- 4 files changed, 60 insertions(+), 47 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWay.java b/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWay.java index c349b2ad7a9..057369c1f54 100644 --- a/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWay.java +++ b/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWay.java @@ -34,6 +34,10 @@ public long getOsmId() { return osmId; } + public QuadRect getBbox() { + return bbox; + } + public GpxFile getGpxFile() { return gpxFile; } diff --git a/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayActivator.java b/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayActivator.java index 02e9c2ed61c..64d06d0e618 100644 --- a/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayActivator.java +++ b/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayActivator.java @@ -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 readHeights; + private final CallbackWithObject openAsGpxFile; - public ClickableWayActivator(OsmandApplication app, OsmandMapTileView view) { - this.app = app; + public ClickableWayActivator(@NonNull OsmandMapTileView view, + @NonNull CallbackWithObject readHeightData, + @NonNull CallbackWithObject 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 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; } diff --git a/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayLoader.java b/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayLoader.java index 2c11446c3b6..553bb674853 100644 --- a/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayLoader.java +++ b/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayLoader.java @@ -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 @@ -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; @@ -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; @@ -54,14 +60,14 @@ public class ClickableWayLoader { // others are default (red) ); - // TODO implement simple cache - 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 @@ -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 tags) { long id = obfMapObject.getId().getId().longValue(); long osmId = ObfConstants.getOsmId(id >> AMENITY_ID_RIGHT_SHIFT); @@ -149,7 +156,7 @@ private ClickableWay loadClickableWay(LatLon selectedLatLon, QuadRect bbox, } @Nullable - private String getGpxColorByTags(Map tags) { + private String getGpxColorByTags(Map tags) { for (String t : clickableTags) { String val = tags.get(t); if (val != null) { @@ -184,4 +191,23 @@ private boolean isClickableWayTags(@NonNull Map 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; + } } diff --git a/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayReaderTask.java b/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayReaderTask.java index f9841059ccd..03f22e4bc5a 100644 --- a/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayReaderTask.java +++ b/OsmAnd/src/net/osmand/plus/track/clickable/ClickableWayReaderTask.java @@ -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 { private final ClickableWay clickableWay; - private final CallbackWithObject callback; + private final CallbackWithObject readHeightData; + private final CallbackWithObject openAsGpxFile; - public ClickableWayReaderTask(MapActivity mapActivity, ClickableWay clickableWay, - CallbackWithObject callback) { + public ClickableWayReaderTask(@NonNull MapActivity mapActivity, + @NonNull ClickableWay clickableWay, + @NonNull CallbackWithObject readHeightData, + @NonNull CallbackWithObject 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; } @@ -37,9 +36,7 @@ protected void onPreExecute() { @Override protected void onPostExecute(ClickableWay clickableWay) { - if (callback != null) { - callback.processResult(clickableWay); - } + openAsGpxFile.processResult(clickableWay); hideProgress(); } }