Fix some errors
This commit is contained in:
parent
08ab7a6613
commit
b3cb492558
3 changed files with 40 additions and 25 deletions
|
@ -10,10 +10,11 @@ import net.osmand.plus.server.endpoints.TileEndpoint;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
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 final Map<String, ApiEndpoint> endpoints = new HashMap<>();
|
||||
private MapActivity mapActivity;
|
||||
|
@ -46,9 +47,11 @@ public class OsmAndHttpServer extends NanoHTTPD {
|
|||
|
||||
private NanoHTTPD.Response routeApi(NanoHTTPD.IHTTPSession session) {
|
||||
String uri = session.getUri();
|
||||
for (String path : endpoints.keySet()) {
|
||||
if (uri.startsWith(path)) {
|
||||
return endpoints.get(path).process(session);
|
||||
Iterator<Map.Entry<String, ApiEndpoint>> it = endpoints.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<String, ApiEndpoint> e = it.next();
|
||||
if (uri.startsWith(e.getKey())) {
|
||||
return e.getValue().process(session, uri);
|
||||
}
|
||||
}
|
||||
return ErrorResponses.response404;
|
||||
|
@ -79,7 +82,7 @@ public class OsmAndHttpServer extends NanoHTTPD {
|
|||
OsmandApplication app = mapActivity.getMyApplication();
|
||||
if (app != null) {
|
||||
try {
|
||||
is = app.getAssets().open(FOLDER_NAME + uri);
|
||||
is = app.getAssets().open(ASSETS_FOLDER_NAME + uri);
|
||||
if (is.available() == 0) {
|
||||
return ErrorResponses.response404;
|
||||
}
|
||||
|
@ -107,7 +110,7 @@ public class OsmAndHttpServer extends NanoHTTPD {
|
|||
}
|
||||
|
||||
public interface ApiEndpoint {
|
||||
NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session);
|
||||
NanoHTTPD.Response process(IHTTPSession session, String url);
|
||||
}
|
||||
|
||||
public static class ErrorResponses {
|
||||
|
|
|
@ -1,24 +1,30 @@
|
|||
package net.osmand.plus.server.endpoints;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
import fi.iki.elonen.NanoHTTPD;
|
||||
|
||||
import net.osmand.PlatformUtil;
|
||||
import net.osmand.data.RotatedTileBox;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
import net.osmand.plus.server.OsmAndHttpServer;
|
||||
import net.osmand.plus.views.OsmandMapTileView;
|
||||
import net.osmand.util.MapUtils;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static fi.iki.elonen.NanoHTTPD.SOCKET_READ_TIMEOUT;
|
||||
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
|
||||
|
||||
public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTileView.IMapOnImageDrawn {
|
||||
private static final int TILE_SIZE_PX = 512;
|
||||
private static final int TIMEOUT_STEP = 500;
|
||||
private static final int TIMEOUT = 5000;
|
||||
|
||||
private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class);
|
||||
private final MapActivity mapActivity;
|
||||
private RotatedTileBox resultBmpViewport;
|
||||
|
@ -35,16 +41,21 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTile
|
|||
}
|
||||
|
||||
@Override
|
||||
public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session) {
|
||||
public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session, String url) {
|
||||
this.mapActivity.getMapView().setOnImageDrawnListener(this);
|
||||
RotatedTileBox tileBoxCopy = mapActivity.getMapView().getCurrentRotatedTileBox().copy();
|
||||
Scanner s = new Scanner(session.getUri()).useDelimiter("/");
|
||||
//reading path
|
||||
s.next();
|
||||
int zoom = s.nextInt();
|
||||
double lat = s.nextDouble();
|
||||
double lon = s.nextDouble();
|
||||
Bitmap bitmap = requestTile(lat, lon, zoom);
|
||||
// https://tile.osmand.net/hd/6/55/25.png
|
||||
int extInd = url.indexOf('.');
|
||||
if(extInd >= 0) {
|
||||
url = url.substring(0, extInd);
|
||||
}
|
||||
String[] prms = url.split("/");
|
||||
if(prms.length < 4) {
|
||||
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) {
|
||||
return OsmAndHttpServer.ErrorResponses.response500;
|
||||
}
|
||||
|
@ -52,16 +63,16 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTile
|
|||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
|
||||
byte[] byteArray = stream.toByteArray();
|
||||
ByteArrayInputStream str = new ByteArrayInputStream(byteArray);
|
||||
mapActivity.getMapView().setCurrentViewport(tileBoxCopy);
|
||||
this.mapActivity.getMapView().setOnImageDrawnListener(null);
|
||||
return newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"image/png",
|
||||
str,
|
||||
str.available());
|
||||
NanoHTTPD.Response.Status.OK, "image/png",
|
||||
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()
|
||||
.setLocation(lat, lon)
|
||||
.setZoom(zoom)
|
||||
|
@ -69,12 +80,13 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTile
|
|||
mapActivity.getMapView().setCurrentViewport(rotatedTileBox);
|
||||
int timeout = 0;
|
||||
try {
|
||||
while (!rotatedTileBox.equals(resultBmpViewport) && timeout < SOCKET_READ_TIMEOUT) {
|
||||
while (this.resultBitmap != null && timeout < TIMEOUT) {
|
||||
Thread.sleep(TIMEOUT_STEP);
|
||||
timeout += TIMEOUT_STEP;
|
||||
}
|
||||
resultBmpViewport = null;
|
||||
return resultBitmap;
|
||||
Bitmap res = resultBitmap;
|
||||
this.resultBitmap = null;
|
||||
return res;
|
||||
} catch (InterruptedException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
|
|
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
Loading…
Reference in a new issue