Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose things from provider for region pre-cache functionality #119

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/mahomaps/MahoMapsApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected void destroyApp(boolean arg0) {
if (thread != null)
thread.interrupt();
if (tiles != null)
tiles.Stop();
tiles.Stop(false);
if (canvas != null)
canvas.dispose();
}
Expand All @@ -120,7 +120,7 @@ public void run() {
// just in case
}
try {
tiles = new TilesProvider(Settings.GetLangString()); // wrong lang in settings
tiles = new TilesProvider(Settings.GetLangString(), thread); // wrong lang in settings
} catch (RuntimeException e) {
Form f = new Form(MahoMapsApp.text[88],
new Item[] { new StringItem(MahoMapsApp.text[91], MahoMapsApp.text[92]) });
Expand Down
72 changes: 56 additions & 16 deletions src/mahomaps/map/TilesProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,26 @@ public class TilesProvider implements Runnable {

private Thread networkTh;
private Thread cacheTh;
private Thread accessTh;

public static final String[] tilesUrls = new String[] {
// scheme
"https://core-renderer-tiles.maps.yandex.net/tiles?l=map&lang=",
// sat
"https://core-sat.maps.yandex.net/tiles?l=sat&lang=",
// hybrid
"http://nnp.nnchan.ru/mergedtile.php?lang="
};
"http://nnp.nnchan.ru/mergedtile.php?lang=" };

public TilesProvider(String lang) {
public TilesProvider(String lang, Thread accesser) {
if (lang == null)
throw new NullPointerException("Language must be non-null!");
this.lang = lang;
accessTh = accesser;
}

public void Start() {
if (accessTh == null)
throw new IllegalStateException("Tiles provider must be bound to a thread!");
if (networkTh != null || cacheTh != null)
throw new IllegalStateException("Can't start already running tiles provider!");
networkTh = new Thread(this, "Tiles downloader");
Expand All @@ -79,12 +82,24 @@ public void Start() {
cacheTh.start();
}

public void Stop() {
if (networkTh != null)
public void Stop(boolean blocking) {
if (networkTh != null) {
networkTh.interrupt();
try {
if (blocking)
networkTh.join();
} catch (InterruptedException e) {
}
}
networkTh = null;
if (cacheTh != null)
if (cacheTh != null) {
cacheTh.interrupt();
try {
if (blocking)
cacheTh.join();
} catch (InterruptedException e) {
}
}
cacheTh = null;
}

Expand Down Expand Up @@ -466,7 +481,7 @@ private Image tryLoadFromRMS(TileId id) {
* Выполняет операции, необходимые перед очередной отрисовкой.
*/
public void BeginMapPaint() {
if (Thread.currentThread() != MahoMapsApp.thread)
if (Thread.currentThread() != accessTh)
throw new IllegalThreadStateException(INVALID_THREAD_ERR);
if (paintState)
throw new IllegalStateException("Paint is already in progress.");
Expand All @@ -483,11 +498,7 @@ public void BeginMapPaint() {
* Выполняет операции, необходимые после очередной отрисовки.
*/
public void EndMapPaint(MapState ms) {
if (Thread.currentThread() != MahoMapsApp.thread)
throw new IllegalThreadStateException(INVALID_THREAD_ERR);
if (!paintState)
throw new IllegalStateException("Paint was not performed.");
paintState = false;
exitPaintState();

final int reqZoom = ms.zoom;

Expand Down Expand Up @@ -517,6 +528,36 @@ public void EndMapPaint(MapState ms) {
cacheGate.Reset();
}

public void EndMapPaintThenDiscardAll() {
exitPaintState();

synchronized (cache) {
for (int i = cache.size() - 1; i > -1; i--) {
TileCache t = (TileCache) cache.elementAt(i);
synchronized (t) {
switch (t.state) {
case TileCache.STATE_CACHE_LOADING:
case TileCache.STATE_SERVER_LOADING:
// we can't remove this tile
continue;
default:
t.state = TileCache.STATE_UNLOADED;
cache.removeElementAt(i);
break;
}
}
}
}
}

private void exitPaintState() {
if (Thread.currentThread() != accessTh)
throw new IllegalThreadStateException(INVALID_THREAD_ERR);
if (!paintState)
throw new IllegalStateException("Paint was not performed.");
paintState = false;
}

/**
* Возвращает объект кэша плитки для отрисовки.
*
Expand All @@ -525,10 +566,10 @@ public void EndMapPaint(MapState ms) {
* null</b> если координаты плитки не находятся в пределах карты
* (например, Y отрицательный).
*/
public TileCache getTile(TileId tileId) {
public TileCache GetTile(TileId tileId) {
if (!paintState)
throw new IllegalStateException("Paint was not performing now, can't get tile!");
if (Thread.currentThread() != MahoMapsApp.thread)
if (Thread.currentThread() != accessTh)
throw new IllegalThreadStateException(INVALID_THREAD_ERR);

int max = 0x1 << tileId.zoom;
Expand Down Expand Up @@ -573,8 +614,7 @@ public TileCache getTile(TileId tileId) {
}

private String getUrl(TileId tileId) {
String url = tilesUrls[tileId.map] + lang + "&x=" + tileId.x + "&y=" + tileId.y
+ "&z=" + tileId.zoom;
String url = tilesUrls[tileId.map] + lang + "&x=" + tileId.x + "&y=" + tileId.y + "&z=" + tileId.zoom;
if (Settings.proxyTiles && url.startsWith("https")) {
return Settings.proxyServer + YmapsApiBase.EncodeUrl(url);
}
Expand Down
2 changes: 1 addition & 1 deletion src/mahomaps/screens/MapCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void drawMap(Graphics g, int w, int h) {
int x = xo - trX * 256;
int xi = tx - trX;
while (x < w / 2) {
TileCache tile = tiles.getTile(new TileId(xi, yi, ms.zoom, Settings.map));
TileCache tile = tiles.GetTile(new TileId(xi, yi, ms.zoom, Settings.map));
if (tile != null)
tile.paint(g, x, y);
x += 256;
Expand Down