Created
October 25, 2011 07:19
-
-
Save samueltardieu/1311693 to your computer and use it in GitHub Desktop.
Downloader thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cgeo.geocaching.maps; | |
import cgeo.geocaching.Settings; | |
import cgeo.geocaching.cgBase; | |
import cgeo.geocaching.cgSearch; | |
import cgeo.geocaching.geopoint.Geopoint; | |
import org.apache.commons.lang3.StringUtils; | |
import android.os.Handler; | |
import android.util.Log; | |
import java.util.concurrent.LinkedBlockingQueue; | |
/** | |
* Worker thread downloading caches from the internet. | |
*/ | |
class CachesMapDownloader extends Thread { | |
class ViewPortBox { | |
public final Geopoint topLeft; | |
public final Geopoint bottomRight; | |
public final Runnable refreshCallback; | |
public final Handler noMapTokenHandler; | |
public ViewPortBox(final Geopoint topLeft, final Geopoint bottomRight, final Runnable refreshCallback, final Handler noMapTokenHandler) { | |
this.topLeft = topLeft; | |
this.bottomRight = bottomRight; | |
this.refreshCallback = refreshCallback; | |
this.noMapTokenHandler = noMapTokenHandler; | |
} | |
} | |
private final cgBase base; | |
private static CachesMapDownloader instance; | |
private CachesMapDownloader(final cgBase base) { | |
this.base = base; | |
} | |
public static CachesMapDownloader getInstance(final cgBase base) { | |
if (instance == null) { | |
synchronized (CachesMapDownloader.class) { | |
if (instance == null) { | |
instance = new CachesMapDownloader(base); | |
instance.start(); | |
} | |
} | |
} | |
return instance; | |
} | |
private final LinkedBlockingQueue<ViewPortBox> queue = new LinkedBlockingQueue<ViewPortBox>(1); | |
public synchronized void getCaches(final Geopoint topLeft, final Geopoint bottomRight, final Runnable refreshCallback, final Handler noMapTokenHandler) { | |
queue.clear(); | |
queue.offer(new ViewPortBox(topLeft, bottomRight, refreshCallback, noMapTokenHandler)); | |
} | |
@Override | |
public void run() { | |
String token = null; | |
while (true) { | |
ViewPortBox viewPort; | |
try { | |
viewPort = queue.take(); | |
} catch (InterruptedException e) { | |
// This should not happen | |
continue; | |
} | |
if (StringUtils.isBlank(token)) { | |
Log.d(Settings.tag, "CachesMapDownloader: requesting map user token"); | |
token = cgBase.getMapUserToken(viewPort.noMapTokenHandler); | |
if (StringUtils.isBlank(token)) { | |
continue; | |
} | |
} | |
final double latMin = viewPort.bottomRight.getLatitude(); | |
final double latMax = viewPort.topLeft.getLatitude(); | |
final double lonMin = viewPort.topLeft.getLongitude(); | |
final double lonMax = viewPort.bottomRight.getLongitude(); | |
final cgSearch search = base.searchByViewport(token, latMin, latMax, lonMin, lonMax, 0); | |
if (search != null && viewPort.refreshCallback != null) { | |
viewPort.refreshCallback.run(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment