mapactivity added

This commit is contained in:
simon 2020-09-07 12:37:56 +03:00
parent b4d4e79b6a
commit aeff02d633
4 changed files with 24 additions and 42 deletions

View file

@ -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);
}

View file

@ -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,

View file

@ -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) {

View file

@ -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)