Fix some errors

This commit is contained in:
Victor Shcherb 2020-09-07 18:14:52 +02:00
parent 08ab7a6613
commit b3cb492558
3 changed files with 40 additions and 25 deletions

View file

@ -10,10 +10,11 @@ import net.osmand.plus.server.endpoints.TileEndpoint;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
public class OsmAndHttpServer extends NanoHTTPD { public class OsmAndHttpServer extends NanoHTTPD {
private static final String FOLDER_NAME = "server"; private static final String ASSETS_FOLDER_NAME = "server";
private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(OsmAndHttpServer.class); private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(OsmAndHttpServer.class);
private final Map<String, ApiEndpoint> endpoints = new HashMap<>(); private final Map<String, ApiEndpoint> endpoints = new HashMap<>();
private MapActivity mapActivity; private MapActivity mapActivity;
@ -46,9 +47,11 @@ public class OsmAndHttpServer extends NanoHTTPD {
private NanoHTTPD.Response routeApi(NanoHTTPD.IHTTPSession session) { private NanoHTTPD.Response routeApi(NanoHTTPD.IHTTPSession session) {
String uri = session.getUri(); String uri = session.getUri();
for (String path : endpoints.keySet()) { Iterator<Map.Entry<String, ApiEndpoint>> it = endpoints.entrySet().iterator();
if (uri.startsWith(path)) { while (it.hasNext()) {
return endpoints.get(path).process(session); Map.Entry<String, ApiEndpoint> e = it.next();
if (uri.startsWith(e.getKey())) {
return e.getValue().process(session, uri);
} }
} }
return ErrorResponses.response404; return ErrorResponses.response404;
@ -79,7 +82,7 @@ public class OsmAndHttpServer extends NanoHTTPD {
OsmandApplication app = mapActivity.getMyApplication(); OsmandApplication app = mapActivity.getMyApplication();
if (app != null) { if (app != null) {
try { try {
is = app.getAssets().open(FOLDER_NAME + uri); is = app.getAssets().open(ASSETS_FOLDER_NAME + uri);
if (is.available() == 0) { if (is.available() == 0) {
return ErrorResponses.response404; return ErrorResponses.response404;
} }
@ -107,7 +110,7 @@ public class OsmAndHttpServer extends NanoHTTPD {
} }
public interface ApiEndpoint { public interface ApiEndpoint {
NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session); NanoHTTPD.Response process(IHTTPSession session, String url);
} }
public static class ErrorResponses { public static class ErrorResponses {

View file

@ -1,24 +1,30 @@
package net.osmand.plus.server.endpoints; package net.osmand.plus.server.endpoints;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import fi.iki.elonen.NanoHTTPD; import fi.iki.elonen.NanoHTTPD;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.data.RotatedTileBox; import net.osmand.data.RotatedTileBox;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.server.OsmAndHttpServer; import net.osmand.plus.server.OsmAndHttpServer;
import net.osmand.plus.views.OsmandMapTileView; import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.util.MapUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.util.Scanner; import java.util.Scanner;
import static fi.iki.elonen.NanoHTTPD.SOCKET_READ_TIMEOUT;
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse; import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTileView.IMapOnImageDrawn { public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTileView.IMapOnImageDrawn {
private static final int TILE_SIZE_PX = 512; private static final int TILE_SIZE_PX = 512;
private static final int TIMEOUT_STEP = 500; private static final int TIMEOUT_STEP = 500;
private static final int TIMEOUT = 5000;
private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class); private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class);
private final MapActivity mapActivity; private final MapActivity mapActivity;
private RotatedTileBox resultBmpViewport; private RotatedTileBox resultBmpViewport;
@ -35,16 +41,21 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTile
} }
@Override @Override
public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session) { public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session, String url) {
this.mapActivity.getMapView().setOnImageDrawnListener(this); this.mapActivity.getMapView().setOnImageDrawnListener(this);
RotatedTileBox tileBoxCopy = mapActivity.getMapView().getCurrentRotatedTileBox().copy(); // https://tile.osmand.net/hd/6/55/25.png
Scanner s = new Scanner(session.getUri()).useDelimiter("/"); int extInd = url.indexOf('.');
//reading path if(extInd >= 0) {
s.next(); url = url.substring(0, extInd);
int zoom = s.nextInt(); }
double lat = s.nextDouble(); String[] prms = url.split("/");
double lon = s.nextDouble(); if(prms.length < 4) {
Bitmap bitmap = requestTile(lat, lon, zoom); return OsmAndHttpServer.ErrorResponses.response500;
}
int zoom = Integer.parseInt(prms[1]);
int x = Integer.parseInt(prms[2]);
int y = Integer.parseInt(prms[3]);
Bitmap bitmap = requestTile(x, y, zoom);
if (bitmap == null) { if (bitmap == null) {
return OsmAndHttpServer.ErrorResponses.response500; return OsmAndHttpServer.ErrorResponses.response500;
} }
@ -52,16 +63,16 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTile
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray(); byte[] byteArray = stream.toByteArray();
ByteArrayInputStream str = new ByteArrayInputStream(byteArray); ByteArrayInputStream str = new ByteArrayInputStream(byteArray);
mapActivity.getMapView().setCurrentViewport(tileBoxCopy);
this.mapActivity.getMapView().setOnImageDrawnListener(null); this.mapActivity.getMapView().setOnImageDrawnListener(null);
return newFixedLengthResponse( return newFixedLengthResponse(
NanoHTTPD.Response.Status.OK, NanoHTTPD.Response.Status.OK, "image/png",
"image/png", str, str.available());
str,
str.available());
} }
private synchronized Bitmap requestTile(double lat, double lon, int zoom) { private synchronized Bitmap requestTile(int x, int y, int zoom) {
this.resultBitmap = null;
double lat = MapUtils.getLatitudeFromTile(zoom, y);
double lon = MapUtils.getLongitudeFromTile(zoom, x);
final RotatedTileBox rotatedTileBox = new RotatedTileBox.RotatedTileBoxBuilder() final RotatedTileBox rotatedTileBox = new RotatedTileBox.RotatedTileBoxBuilder()
.setLocation(lat, lon) .setLocation(lat, lon)
.setZoom(zoom) .setZoom(zoom)
@ -69,12 +80,13 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTile
mapActivity.getMapView().setCurrentViewport(rotatedTileBox); mapActivity.getMapView().setCurrentViewport(rotatedTileBox);
int timeout = 0; int timeout = 0;
try { try {
while (!rotatedTileBox.equals(resultBmpViewport) && timeout < SOCKET_READ_TIMEOUT) { while (this.resultBitmap != null && timeout < TIMEOUT) {
Thread.sleep(TIMEOUT_STEP); Thread.sleep(TIMEOUT_STEP);
timeout += TIMEOUT_STEP; timeout += TIMEOUT_STEP;
} }
resultBmpViewport = null; Bitmap res = resultBitmap;
return resultBitmap; this.resultBitmap = null;
return res;
} catch (InterruptedException e) { } catch (InterruptedException e) {
LOG.error(e); LOG.error(e);
} }

BIN
gradle/wrapper/gradle-wrapper.jar vendored Normal file

Binary file not shown.