Skip to content

Commit

Permalink
Merge pull request #1062 from osmdroid/feature/#1044
Browse files Browse the repository at this point in the history
feature/#1044 : more flexibility for tile expiration
  • Loading branch information
monsieurtanuki committed Jun 15, 2018
2 parents f8fe850 + 0bf9d55 commit 19053d1
Showing 1 changed file with 37 additions and 24 deletions.
Expand Up @@ -51,7 +51,7 @@ public class MapTileDownloader extends MapTileModuleProviderBase {

private final IFilesystemCache mFilesystemCache;

private final AtomicReference<OnlineTileSourceBase> mTileSource = new AtomicReference<OnlineTileSourceBase>();
private final AtomicReference<OnlineTileSourceBase> mTileSource = new AtomicReference<>();

private final INetworkAvailablityCheck mNetworkAvailablityCheck;

Expand Down Expand Up @@ -154,6 +154,39 @@ public void setTileSource(final ITileSource tileSource) {
}
}

/**
* @since 6.0.2
*/
private long computeExpirationTime(final String pHttpExpiresHeader) {
Long httpExpirationTime = null;
if (pHttpExpiresHeader != null && pHttpExpiresHeader.length() > 0) {
try {
final Date dateExpires = Configuration.getInstance().getHttpHeaderDateTimeFormat().parse(pHttpExpiresHeader);
httpExpirationTime = dateExpires.getTime();
} catch (final Exception ex) {
if (Configuration.getInstance().isDebugMapTileDownloader())
Log.d(IMapView.LOGTAG, "Unable to parse expiration tag for tile, using default, server returned " + pHttpExpiresHeader, ex);
}
}
return computeExpirationTime(httpExpirationTime);
}

/**
* @since 6.0.2
*/
protected long computeExpirationTime(final Long pHttpExpiresTime) {
final Long override=Configuration.getInstance().getExpirationOverrideDuration();
final long now = System.currentTimeMillis();
if (override != null) {
return now + override;
}
final long extension = Configuration.getInstance().getExpirationExtendedDuration();
if (pHttpExpiresTime != null) {
return pHttpExpiresTime + extension;
}
return now + OpenStreetMapTileProviderConstants.DEFAULT_MAXIMUM_CACHED_FILE_AGE + extension;
}

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
Expand Down Expand Up @@ -272,25 +305,8 @@ protected Drawable downloadTile(final long pMapTileIndex, final int redirectCoun

dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, StreamUtils.IO_BUFFER_SIZE);
//default is 1 week from now
//Date dateExpires;
Long override=Configuration.getInstance().getExpirationOverrideDuration();
Long expirationTime = null;
if (override!=null) {
expirationTime = System.currentTimeMillis() + override;
} else {
expirationTime = System.currentTimeMillis() + OpenStreetMapTileProviderConstants.DEFAULT_MAXIMUM_CACHED_FILE_AGE + Configuration.getInstance().getExpirationExtendedDuration();
final String expires = c.getHeaderField(OpenStreetMapTileProviderConstants.HTTP_EXPIRES_HEADER);
if (expires != null && expires.length() > 0) {
try {
final Date dateExpires = Configuration.getInstance().getHttpHeaderDateTimeFormat().parse(expires);
expirationTime = dateExpires.getTime() + Configuration.getInstance().getExpirationExtendedDuration();
} catch (Exception ex) {
if (Configuration.getInstance().isDebugMapTileDownloader())
Log.d(IMapView.LOGTAG, "Unable to parse expiration tag for tile, using default, server returned " + expires, ex);
}
}
}
final long expirationTime = computeExpirationTime(
c.getHeaderField(OpenStreetMapTileProviderConstants.HTTP_EXPIRES_HEADER));
StreamUtils.copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
Expand All @@ -303,10 +319,7 @@ protected Drawable downloadTile(final long pMapTileIndex, final int redirectCoun
mFilesystemCache.saveFile(tileSource, pMapTileIndex, byteStream, expirationTime);
byteStream.reset();
}
final Drawable result = tileSource.getDrawable(byteStream);


return result;
return tileSource.getDrawable(byteStream);
} catch (final UnknownHostException e) {
Log.w(IMapView.LOGTAG,"UnknownHostException downloading MapTile: " + MapTileIndex.toString(pMapTileIndex) + " : " + e);
Counters.tileDownloadErrors++;
Expand Down

0 comments on commit 19053d1

Please sign in to comment.