refactoring
This commit is contained in:
parent
9823461435
commit
fef6f93621
1 changed files with 38 additions and 47 deletions
|
@ -1,7 +1,6 @@
|
|||
package net.osmand.plus.server.endpoints;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.util.Pair;
|
||||
import fi.iki.elonen.NanoHTTPD;
|
||||
import net.osmand.PlatformUtil;
|
||||
import net.osmand.data.RotatedTileBox;
|
||||
|
@ -18,13 +17,12 @@ import java.util.Collections;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
|
||||
|
||||
public class TileEndpoint implements ApiEndpoint {
|
||||
private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class);
|
||||
ExecutorService executor = Executors.newFixedThreadPool(3);
|
||||
private static final int RENDER_WAIT_THRESHOLD = 5000;
|
||||
private static final Object lock = new Object();
|
||||
Map<RotatedTileBox, Bitmap> hashMap = new HashMap<>();
|
||||
Map<RotatedTileBox, Bitmap> map = Collections.synchronizedMap(hashMap);
|
||||
OsmandApplication application;
|
||||
|
@ -35,27 +33,29 @@ public class TileEndpoint implements ApiEndpoint {
|
|||
|
||||
@Override
|
||||
public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session) {
|
||||
int zoom;
|
||||
double lat;
|
||||
double lon;
|
||||
String fullUri = session.getUri().replace("/tile/", "");
|
||||
Scanner s = new Scanner(fullUri).useDelimiter("/");
|
||||
zoom = s.nextInt();
|
||||
lat = s.nextDouble();
|
||||
lon = s.nextDouble();
|
||||
Bitmap bitmap = requestTile(lat, lon, zoom);
|
||||
if (bitmap == null) {
|
||||
return OsmAndHttpServer.ErrorResponses.response500;
|
||||
synchronized (lock) {
|
||||
int zoom;
|
||||
double lat;
|
||||
double lon;
|
||||
String fullUri = session.getUri().replace("/tile/", "");
|
||||
Scanner s = new Scanner(fullUri).useDelimiter("/");
|
||||
zoom = s.nextInt();
|
||||
lat = s.nextDouble();
|
||||
lon = s.nextDouble();
|
||||
Bitmap bitmap = requestTile(lat, lon, zoom);
|
||||
if (bitmap == null) {
|
||||
return OsmAndHttpServer.ErrorResponses.response500;
|
||||
}
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
|
||||
byte[] byteArray = stream.toByteArray();
|
||||
ByteArrayInputStream str = new ByteArrayInputStream(byteArray);
|
||||
return newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"image/png",
|
||||
str,
|
||||
str.available());
|
||||
}
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
|
||||
byte[] byteArray = stream.toByteArray();
|
||||
ByteArrayInputStream str = new ByteArrayInputStream(byteArray);
|
||||
return newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"image/png",
|
||||
str,
|
||||
str.available());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -63,40 +63,31 @@ public class TileEndpoint implements ApiEndpoint {
|
|||
this.application = application;
|
||||
}
|
||||
|
||||
private synchronized Bitmap requestTile(double lat, double lon, int zoom) {
|
||||
Future<Pair<RotatedTileBox, Bitmap>> future;
|
||||
private Bitmap requestTile(double lat, double lon, int zoom) {
|
||||
final RotatedTileBox rotatedTileBox = new RotatedTileBox.RotatedTileBoxBuilder()
|
||||
.setLocation(lat, lon)
|
||||
.setZoom(zoom)
|
||||
.setPixelDimensions(512, 512, 0.5f, 0.5f).build();
|
||||
final MapRenderRepositories renderer = application.getResourceManager().getRenderer();
|
||||
future = executor.submit(new Callable<Pair<RotatedTileBox, Bitmap>>() {
|
||||
@Override
|
||||
public Pair<RotatedTileBox, Bitmap> call() throws Exception {
|
||||
Bitmap bmp;
|
||||
int sleepTime = 500;
|
||||
while ((bmp = map.get(rotatedTileBox)) == null) {
|
||||
Thread.sleep(sleepTime);
|
||||
sleepTime += 500;
|
||||
}
|
||||
return Pair.create(rotatedTileBox, bmp);
|
||||
}
|
||||
});
|
||||
application.getResourceManager().updateRendererMap(rotatedTileBox, new AsyncLoadingThread.OnMapLoadedListener() {
|
||||
@Override
|
||||
public void onMapLoaded(boolean interrupted) {
|
||||
map.put(rotatedTileBox, renderer.getBitmap());
|
||||
}
|
||||
});
|
||||
try {
|
||||
Pair<RotatedTileBox, Bitmap> pair = future.get();
|
||||
Bitmap bitmap = pair.second;
|
||||
return bitmap;
|
||||
} catch (ExecutionException e) {
|
||||
LOG.error("Execution exception", e);
|
||||
} catch (InterruptedException e) {
|
||||
LOG.error("Interrupted exception", e);
|
||||
Bitmap bmp;
|
||||
int sleepTime = 500;
|
||||
while ((bmp = map.get(rotatedTileBox)) == null) {
|
||||
try {
|
||||
Thread.sleep(sleepTime);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
sleepTime += 500;
|
||||
if (sleepTime > RENDER_WAIT_THRESHOLD) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return bmp;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue