Fix tile endpoints

This commit is contained in:
Victor Shcherb 2020-09-07 18:26:40 +02:00
parent b3cb492558
commit 2b9db97fb7
5 changed files with 33 additions and 30 deletions

View file

@ -69,6 +69,11 @@ public class AsyncLoadingThread extends Thread {
}
}
public boolean areResourcesLoading() {
return !requests.isEmpty();
}
public void requestToLoadTile(TileLoadDownloadRequest req) {
requests.push(req);
}

View file

@ -1176,7 +1176,12 @@ public class ResourceManager {
tc.tilesOnFS.clear();
}
}
public AsyncLoadingThread getAsyncLoadingThread() {
return asyncLoadingThread;
}
/// On low memory method ///
public void onLowMemory() {
log.info("On low memory");

View file

@ -26,6 +26,7 @@ import static android.content.Context.WIFI_SERVICE;
public class ServerFragment extends BaseOsmAndFragment {
private final static Log LOG = PlatformUtil.getLog(ServerFragment.class);
private final int port = 24990;
final int THREAD_ID = 14231; // random number
private boolean initialized = false;
private OsmAndHttpServer server;
private View view;
@ -84,7 +85,7 @@ public class ServerFragment extends BaseOsmAndFragment {
}
private void initServer() {
final int THREAD_ID = 10000;
TrafficStats.setThreadStatsTag(THREAD_ID);
String hostname = getDeviceAddress();
try {
@ -115,7 +116,8 @@ public class ServerFragment extends BaseOsmAndFragment {
if (getActivity() != null) {
try {
getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
} catch (Exception e) {
} catch (RuntimeException e) {
LOG.error(e.getMessage(), e);
}
}
}

View file

@ -7,6 +7,7 @@ import fi.iki.elonen.NanoHTTPD;
import net.osmand.PlatformUtil;
import net.osmand.data.RotatedTileBox;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.resources.AsyncLoadingThread;
import net.osmand.plus.server.OsmAndHttpServer;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.util.MapUtils;
@ -20,29 +21,20 @@ import java.util.Scanner;
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTileView.IMapOnImageDrawn {
public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
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;
private Bitmap resultBitmap;
public TileEndpoint(MapActivity mapActivity) {
this.mapActivity = mapActivity;
}
@Override
public void onDraw(RotatedTileBox viewport, Bitmap bmp) {
this.resultBmpViewport = viewport;
this.resultBitmap = bmp;
}
@Override
public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session, String url) {
this.mapActivity.getMapView().setOnImageDrawnListener(this);
// https://tile.osmand.net/hd/6/55/25.png
int extInd = url.indexOf('.');
if(extInd >= 0) {
@ -63,14 +55,12 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTile
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
ByteArrayInputStream str = new ByteArrayInputStream(byteArray);
this.mapActivity.getMapView().setOnImageDrawnListener(null);
return newFixedLengthResponse(
NanoHTTPD.Response.Status.OK, "image/png",
str, str.available());
}
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()
@ -80,12 +70,17 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTile
mapActivity.getMapView().setCurrentViewport(rotatedTileBox);
int timeout = 0;
try {
while (this.resultBitmap != null && timeout < TIMEOUT) {
AsyncLoadingThread athread = mapActivity.getMyApplication().getResourceManager().getAsyncLoadingThread();
Bitmap res = null;
while (athread.areResourcesLoading() && timeout < TIMEOUT) {
Thread.sleep(TIMEOUT_STEP);
timeout += TIMEOUT_STEP;
}
Bitmap res = resultBitmap;
this.resultBitmap = null;
if(!athread.areResourcesLoading()) {
res = mapActivity.getMapView().getBufferBitmap();
LOG.debug(mapActivity.getMapView().getBufferImgLoc());
}
return res;
} catch (InterruptedException e) {
LOG.error(e);

View file

@ -77,7 +77,6 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
protected OsmandSettings settings = null;
private CanvasColors canvasColors = null;
private Boolean nightMode = null;
private IMapOnImageDrawn mapOnImageDrawnListener;
private class CanvasColors {
int colorDay = MAP_DEFAULT_COLOR;
@ -106,10 +105,6 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
protected static final int emptyTileDivisor = 16;
public interface IMapOnImageDrawn {
void onDraw(RotatedTileBox viewport, Bitmap bmp);
}
public interface OnTrackBallListener {
public boolean onTrackBallEvent(MotionEvent e);
}
@ -346,10 +341,6 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
return application;
}
public void setOnImageDrawnListener(IMapOnImageDrawn iMapOnImageDrawn) {
this.mapOnImageDrawnListener = iMapOnImageDrawn;
}
// ///////////////////////// NON UI PART (could be extracted in common) /////////////////////////////
public LatLon getFirstTouchPointLatLon() {
return firstTouchPointLatLon;
@ -583,9 +574,6 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
if (!bufferBitmap.isRecycled()) {
RectF rct = new RectF(x1, y1, x2, y2);
canvas.drawBitmap(bufferBitmap, null, rct, paintImg);
if (mapOnImageDrawnListener != null){
mapOnImageDrawnListener.onDraw(bufferImgLoc,bufferBitmap);
}
}
canvas.rotate(-rot, currentViewport.getCenterPixelX(), currentViewport.getCenterPixelY());
}
@ -886,6 +874,14 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
refreshMap();
}
public Bitmap getBufferBitmap() {
return bufferBitmap;
}
public RotatedTileBox getBufferImgLoc() {
return bufferImgLoc;
}
public float getDensity() {
return currentViewport.getDensity();
}