refactoring

This commit is contained in:
simon 2020-09-04 18:22:24 +03:00
parent 9823461435
commit fef6f93621

View file

@ -1,7 +1,6 @@
package net.osmand.plus.server.endpoints; package net.osmand.plus.server.endpoints;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.util.Pair;
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;
@ -18,13 +17,12 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import java.util.concurrent.*;
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse; import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
public class TileEndpoint implements ApiEndpoint { public class TileEndpoint implements ApiEndpoint {
private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class); private static final int RENDER_WAIT_THRESHOLD = 5000;
ExecutorService executor = Executors.newFixedThreadPool(3); private static final Object lock = new Object();
Map<RotatedTileBox, Bitmap> hashMap = new HashMap<>(); Map<RotatedTileBox, Bitmap> hashMap = new HashMap<>();
Map<RotatedTileBox, Bitmap> map = Collections.synchronizedMap(hashMap); Map<RotatedTileBox, Bitmap> map = Collections.synchronizedMap(hashMap);
OsmandApplication application; OsmandApplication application;
@ -35,6 +33,7 @@ public class TileEndpoint implements ApiEndpoint {
@Override @Override
public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session) { public NanoHTTPD.Response process(NanoHTTPD.IHTTPSession session) {
synchronized (lock) {
int zoom; int zoom;
double lat; double lat;
double lon; double lon;
@ -57,46 +56,38 @@ public class TileEndpoint implements ApiEndpoint {
str, str,
str.available()); str.available());
} }
}
@Override @Override
public void setApplication(OsmandApplication application) { public void setApplication(OsmandApplication application) {
this.application = application; this.application = application;
} }
private synchronized Bitmap requestTile(double lat, double lon, int zoom) { private Bitmap requestTile(double lat, double lon, int zoom) {
Future<Pair<RotatedTileBox, Bitmap>> future;
final RotatedTileBox rotatedTileBox = new RotatedTileBox.RotatedTileBoxBuilder() final RotatedTileBox rotatedTileBox = new RotatedTileBox.RotatedTileBoxBuilder()
.setLocation(lat, lon) .setLocation(lat, lon)
.setZoom(zoom) .setZoom(zoom)
.setPixelDimensions(512, 512, 0.5f, 0.5f).build(); .setPixelDimensions(512, 512, 0.5f, 0.5f).build();
final MapRenderRepositories renderer = application.getResourceManager().getRenderer(); 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() { application.getResourceManager().updateRendererMap(rotatedTileBox, new AsyncLoadingThread.OnMapLoadedListener() {
@Override @Override
public void onMapLoaded(boolean interrupted) { public void onMapLoaded(boolean interrupted) {
map.put(rotatedTileBox, renderer.getBitmap()); map.put(rotatedTileBox, renderer.getBitmap());
} }
}); });
Bitmap bmp;
int sleepTime = 500;
while ((bmp = map.get(rotatedTileBox)) == null) {
try { try {
Pair<RotatedTileBox, Bitmap> pair = future.get(); Thread.sleep(sleepTime);
Bitmap bitmap = pair.second;
return bitmap;
} catch (ExecutionException e) {
LOG.error("Execution exception", e);
} catch (InterruptedException e) { } catch (InterruptedException e) {
LOG.error("Interrupted exception", e); e.printStackTrace();
} }
return null; sleepTime += 500;
if (sleepTime > RENDER_WAIT_THRESHOLD) {
break;
}
}
return bmp;
} }
} }