conflict resolved

This commit is contained in:
simon 2020-09-07 19:27:47 +03:00
commit 96653b3749
7 changed files with 69 additions and 51 deletions

View file

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

View file

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

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

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

View file

@ -1,50 +1,53 @@
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.resources.AsyncLoadingThread;
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 {
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 Bitmap resultBitmap;
public TileEndpoint(MapActivity mapActivity) { public TileEndpoint(MapActivity mapActivity) {
this.mapActivity = mapActivity; this.mapActivity = mapActivity;
} }
@Override @Override
public void onDraw(RotatedTileBox viewport, Bitmap bmp) { public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session, String url) {
this.resultBmpViewport = viewport; // https://tile.osmand.net/hd/6/55/25.png
this.resultBitmap = bmp; int extInd = url.indexOf('.');
} if(extInd >= 0) {
url = url.substring(0, extInd);
@Override }
public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session) { String[] prms = url.split("/");
this.mapActivity.getMapView().setOnImageDrawnListener(this); if(prms.length < 4) {
RotatedTileBox tileBoxCopy = mapActivity.getMapView().getCurrentRotatedTileBox().copy(); return OsmAndHttpServer.ErrorResponses.response500;
Scanner s = new Scanner(session.getUri()).useDelimiter("/"); }
//reading path int zoom = Integer.parseInt(prms[1]);
s.next(); int x = Integer.parseInt(prms[2]);
int zoom = s.nextInt(); int y = Integer.parseInt(prms[3]);
double lat = s.nextDouble(); Bitmap bitmap = requestTile(x, y, zoom);
double lon = s.nextDouble();
Bitmap bitmap = requestTile(lat, lon, zoom);
if (bitmap == null) { if (bitmap == null) {
return OsmAndHttpServer.ErrorResponses.response500; return OsmAndHttpServer.ErrorResponses.response500;
} }
@ -52,16 +55,14 @@ 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);
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) {
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 +70,18 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint, OsmandMapTile
mapActivity.getMapView().setCurrentViewport(rotatedTileBox); mapActivity.getMapView().setCurrentViewport(rotatedTileBox);
int timeout = 0; int timeout = 0;
try { try {
while (timeout < SOCKET_READ_TIMEOUT) { AsyncLoadingThread athread = mapActivity.getMyApplication().getResourceManager().getAsyncLoadingThread();
Bitmap res = null;
while (athread.areResourcesLoading() && timeout < TIMEOUT) {
Thread.sleep(TIMEOUT_STEP); Thread.sleep(TIMEOUT_STEP);
timeout += TIMEOUT_STEP; timeout += TIMEOUT_STEP;
} }
resultBmpViewport = null; if(!athread.areResourcesLoading()) {
return resultBitmap; res = mapActivity.getMapView().getBufferBitmap();
LOG.debug(mapActivity.getMapView().getBufferImgLoc());
}
return res;
} catch (InterruptedException e) { } catch (InterruptedException e) {
LOG.error(e); LOG.error(e);
} }

View file

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

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

Binary file not shown.