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,6 +33,7 @@ public class TileEndpoint implements ApiEndpoint {
|
|||
|
||||
@Override
|
||||
public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session) {
|
||||
synchronized (lock) {
|
||||
int zoom;
|
||||
double lat;
|
||||
double lon;
|
||||
|
@ -57,46 +56,38 @@ public class TileEndpoint implements ApiEndpoint {
|
|||
str,
|
||||
str.available());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplication(OsmandApplication application) {
|
||||
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());
|
||||
}
|
||||
});
|
||||
Bitmap bmp;
|
||||
int sleepTime = 500;
|
||||
while ((bmp = map.get(rotatedTileBox)) == null) {
|
||||
try {
|
||||
Pair<RotatedTileBox, Bitmap> pair = future.get();
|
||||
Bitmap bitmap = pair.second;
|
||||
return bitmap;
|
||||
} catch (ExecutionException e) {
|
||||
LOG.error("Execution exception", e);
|
||||
Thread.sleep(sleepTime);
|
||||
} catch (InterruptedException e) {
|
||||
LOG.error("Interrupted exception", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
sleepTime += 500;
|
||||
if (sleepTime > RENDER_WAIT_THRESHOLD) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return bmp;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue