From aeff02d6333949918bb44b511d3cf5e41c61bedb Mon Sep 17 00:00:00 2001 From: simon Date: Mon, 7 Sep 2020 12:37:56 +0300 Subject: [PATCH] mapactivity added --- .../net/osmand/plus/server/ApiEndpoint.java | 4 +-- .../osmand/plus/server/OsmAndHttpServer.java | 34 ++++++------------- .../osmand/plus/server/ServerFragment.java | 4 +-- .../plus/server/endpoints/TileEndpoint.java | 24 ++++++------- 4 files changed, 24 insertions(+), 42 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/server/ApiEndpoint.java b/OsmAnd/src/net/osmand/plus/server/ApiEndpoint.java index 74dd3b03dd..a37e2cfafc 100644 --- a/OsmAnd/src/net/osmand/plus/server/ApiEndpoint.java +++ b/OsmAnd/src/net/osmand/plus/server/ApiEndpoint.java @@ -1,10 +1,10 @@ package net.osmand.plus.server; import fi.iki.elonen.NanoHTTPD; -import net.osmand.plus.OsmandApplication; +import net.osmand.plus.activities.MapActivity; public interface ApiEndpoint { NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session); - void setApplication(OsmandApplication application); + void setMapActivity(MapActivity mapActivity); } diff --git a/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java b/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java index bc0a22b996..69d831bb32 100644 --- a/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java +++ b/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java @@ -1,12 +1,8 @@ package net.osmand.plus.server; -import android.graphics.Bitmap; -import android.util.Log; import android.webkit.MimeTypeMap; -import androidx.fragment.app.FragmentActivity; import fi.iki.elonen.NanoHTTPD; import net.osmand.PlatformUtil; -import net.osmand.data.RotatedTileBox; import net.osmand.plus.OsmandApplication; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.server.endpoints.TileEndpoint; @@ -16,7 +12,7 @@ import java.io.InputStream; import java.util.HashMap; import java.util.Map; -public class OsmAndHttpServer extends NanoHTTPD{ +public class OsmAndHttpServer extends NanoHTTPD { private static final String FOLDER_NAME = "server"; private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(OsmAndHttpServer.class); public static final int PORT = 24990; @@ -25,9 +21,9 @@ public class OsmAndHttpServer extends NanoHTTPD{ private OsmandApplication application; private MapActivity mapActivity; - public OsmAndHttpServer(OsmandApplication application) throws IOException { + public OsmAndHttpServer(MapActivity mapActivity) throws IOException { super(HOSTNAME, PORT); - this.application = application; + setMapActivity(mapActivity); start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); registerEndpoints(); } @@ -36,10 +32,11 @@ public class OsmAndHttpServer extends NanoHTTPD{ return application; } - public void setApplication(OsmandApplication application) { - this.application = application; + public void setMapActivity(MapActivity mapActivity) { + this.application = mapActivity.getMyApplication(); + this.mapActivity = mapActivity; for (String s : endpoints.keySet()) { - endpoints.get(s).setApplication(application); + endpoints.get(s).setMapActivity(mapActivity); } } @@ -57,7 +54,7 @@ public class OsmAndHttpServer extends NanoHTTPD{ private NanoHTTPD.Response routeApi(NanoHTTPD.IHTTPSession session) { String uri = session.getUri(); - for (String path : endpoints.keySet()){ + for (String path : endpoints.keySet()) { if (uri.startsWith(path)) { return endpoints.get(path).process(session); } @@ -77,11 +74,11 @@ public class OsmAndHttpServer extends NanoHTTPD{ } private void registerEndpoints() { - register("/tile", new TileEndpoint(application,mapActivity)); + register("/tile", new TileEndpoint(mapActivity)); } private void register(String path, ApiEndpoint endpoint) { - endpoint.setApplication(application); + endpoint.setMapActivity(mapActivity); endpoints.put(path, endpoint); } @@ -117,17 +114,6 @@ public class OsmAndHttpServer extends NanoHTTPD{ return type; } - public void setActivity(FragmentActivity activity) { - if (activity instanceof MapActivity){ - this.mapActivity = (MapActivity)activity; - } - for (String endpoint : endpoints.keySet()){ - if (endpoints.get(endpoint) instanceof TileEndpoint){ - ((TileEndpoint) endpoints.get(endpoint)).setMapActivity(mapActivity); - } - } - } - public static class ErrorResponses { public static NanoHTTPD.Response response404 = newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND, diff --git a/OsmAnd/src/net/osmand/plus/server/ServerFragment.java b/OsmAnd/src/net/osmand/plus/server/ServerFragment.java index 9544f9a78c..bae8ebffa6 100644 --- a/OsmAnd/src/net/osmand/plus/server/ServerFragment.java +++ b/OsmAnd/src/net/osmand/plus/server/ServerFragment.java @@ -15,6 +15,7 @@ import androidx.annotation.Nullable; import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.BaseOsmAndFragment; import org.apache.commons.logging.Log; @@ -86,8 +87,7 @@ public class ServerFragment extends BaseOsmAndFragment { TrafficStats.setThreadStatsTag(THREAD_ID); OsmAndHttpServer.HOSTNAME = getDeviceAddress(); try { - server = new OsmAndHttpServer(getMyApplication()); - server.setActivity(this.getActivity()); + server = new OsmAndHttpServer((MapActivity) getActivity()); initialized = true; updateTextView("Server started at: http://" + getDeviceAddress() + ":" + OsmAndHttpServer.PORT); } catch (IOException e) { diff --git a/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java b/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java index 9a448ce5d6..21f230a63a 100644 --- a/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java +++ b/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java @@ -36,16 +36,21 @@ public class TileEndpoint implements ApiEndpoint, IMapOnImageDrawn { private Bitmap resultBitmap; private MapActivity mapActivity; + public TileEndpoint(MapActivity mapActivity) { + setMapActivity(mapActivity); + } + + @Override + public void setMapActivity(MapActivity mapActivity) { + this.mapActivity = mapActivity; + this.application = mapActivity.getMyApplication(); + } + @Override public void onDraw(RotatedTileBox viewport, Bitmap bmp) { map.put(viewport, bmp); } - public TileEndpoint(OsmandApplication application, MapActivity mapActivity) { - this.application = application; - this.mapActivity = mapActivity; - } - @Override public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session) { synchronized (lock) { @@ -77,15 +82,6 @@ public class TileEndpoint implements ApiEndpoint, IMapOnImageDrawn { } } - @Override - public void setApplication(OsmandApplication application) { - this.application = application; - } - - public void setMapActivity(MapActivity mapActivity) { - this.mapActivity = mapActivity; - } - private Bitmap requestTile(double lat, double lon, int zoom) { final RotatedTileBox rotatedTileBox = new RotatedTileBox.RotatedTileBoxBuilder() .setLocation(lat, lon)