Skip to content
Permalink
Browse files

Merge pull request #1293 from osmdroid/feature/#1289_2

feature/#1289_2 - normalizing user agent and disabling preventive downloads for MAPNIK
  • Loading branch information...
monsieurtanuki committed Mar 9, 2019
2 parents d3fb350 + 8ddb81f commit 7292c999f85571bcd0f670d2e5b953a960fc2d5a
@@ -2,6 +2,8 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

@@ -62,6 +64,11 @@
protected long mTileGCBulkPauseInMillis = 500;
protected boolean mTileDownloaderFollowRedirects = true;

/**
* @since 6.1.0
*/
private String mNormalizedUserAgent;

public DefaultConfigurationProvider(){


@@ -288,6 +295,8 @@ public void setUserAgentHttpHeader(String userAgentHttpHeader) {
//</editor-fold>
@Override
public void load(Context ctx, SharedPreferences prefs) {
mNormalizedUserAgent = computeNormalizedUserAgent(ctx);

//cache management starts here

//check to see if the shared preferences is set for the tile cache
@@ -549,4 +558,29 @@ public void setMapTileDownloaderFollowRedirects(boolean value) {
public boolean isMapTileDownloaderFollowRedirects() {
return mTileDownloaderFollowRedirects;
}

/**
* @since 6.1.0
*/
@Override
public String getNormalizedUserAgent() {
return mNormalizedUserAgent;
}

/**
* @since 6.1.0
*/
private String computeNormalizedUserAgent(final Context pContext) {
if (pContext == null) {
return null;
}
final String packageName = pContext.getPackageName();
try {
final PackageInfo packageInfo = pContext.getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
final int version = packageInfo.versionCode;
return packageName + "/" + version;
} catch (PackageManager.NameNotFoundException e1) {
return packageName;
}
}
}
@@ -431,4 +431,9 @@
*/
void setMapTileDownloaderFollowRedirects(boolean value);
boolean isMapTileDownloaderFollowRedirects();

/**
* @since 6.1.0
*/
String getNormalizedUserAgent();
}
@@ -3,7 +3,10 @@
import android.graphics.drawable.Drawable;

import org.osmdroid.tileprovider.modules.CantContinueException;
import org.osmdroid.tileprovider.modules.MapTileDownloader;
import org.osmdroid.tileprovider.modules.MapTileModuleProviderBase;
import org.osmdroid.tileprovider.tilesource.ITileSource;
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase;
import org.osmdroid.util.GarbageCollector;
import org.osmdroid.util.MapTileArea;
import org.osmdroid.util.MapTileAreaList;
@@ -111,6 +114,14 @@ private long next() {
private void search(final long pMapTileIndex) {
for (final MapTileModuleProviderBase provider : mProviders) {
try {
if (provider instanceof MapTileDownloader) {
final ITileSource tileSource = ((MapTileDownloader) provider).getTileSource();
if (tileSource instanceof OnlineTileSourceBase) {
if (!((OnlineTileSourceBase)tileSource).getTileSourcePolicy().acceptsPreventive()) {
continue;
}
}
}
final Drawable drawable = provider.getTileLoader().loadTile(pMapTileIndex);
if (drawable == null) {
continue;
@@ -120,6 +120,7 @@ public MapTileProviderBasic(final IRegisterReceiver pRegisterReceiver,
getTileCache().getPreCache().addProvider(assetsProvider);
getTileCache().getPreCache().addProvider(cacheProvider);
getTileCache().getPreCache().addProvider(archiveProvider);
getTileCache().getPreCache().addProvider(mDownloaderProvider);

// tiles currently being processed
getTileCache().getProtectedTileContainers().add(this);
@@ -49,7 +49,13 @@ public Drawable downloadTile(final long pMapTileIndex, final int redirectCount,
return null;
}

final String userAgent = Configuration.getInstance().getUserAgentValue();
String userAgent = null;
if (pTileSource.getTileSourcePolicy().normalizesUserAgent()) {
userAgent = Configuration.getInstance().getNormalizedUserAgent();
}
if (userAgent == null) {
userAgent = Configuration.getInstance().getUserAgentValue();
}
if (!pTileSource.getTileSourcePolicy().acceptsUserAgent(userAgent)) {
Log.e(IMapView.LOGTAG,"Please configure a relevant user agent; current value is: " + userAgent);
return null;
@@ -101,7 +101,12 @@ public static int removeTileSources(final String aRegex) {
"https://a.tile.openstreetmap.org/",
"https://b.tile.openstreetmap.org/",
"https://c.tile.openstreetmap.org/" },"© OpenStreetMap contributors",
new TileSourcePolicy(2, false, false));
new TileSourcePolicy(2,
TileSourcePolicy.FLAG_NO_BULK
| TileSourcePolicy.FLAG_NO_PREVENTIVE
| TileSourcePolicy.FLAG_USER_AGENT_MEANINGFUL
| TileSourcePolicy.FLAG_USER_AGENT_NORMALIZED
));
// max concurrent thread number is 2 (cf. https://operations.osmfoundation.org/policies/tiles/)

public static final OnlineTileSourceBase PUBLIC_TRANSPORT = new XYTileSource(
@@ -7,49 +7,72 @@
* <li>the max number of concurrent downloads</li>
* <li>if it accepts a meaningless user agent</li>
* <li>if it accepts bulk downloads</li>
* <li>if the user agent must be normalized</li>
* </ul>
* @since 6.1.0
* @author Fabrice Fontaine
*/
public class TileSourcePolicy {

/**
* maximum number of concurrent downloads
* No bulk downloads allowed
*/
private final int mMaxConcurrent;
public static final int FLAG_NO_BULK = 1;

/**
* Don't try to preventively download tiles that aren't currently displayed
*/
public static final int FLAG_NO_PREVENTIVE = 2;

/**
* accepts bulk download
* Demands a user agent different from the default value
*/
private final boolean mAcceptsBulkDownload;
public static final int FLAG_USER_AGENT_MEANINGFUL = 4;

/**
* accepts meaningless default user agent
* Uses the "normalized" user agent (package name + version)
*/
private final boolean mAcceptsMeaninglessUserAgent;
public static final int FLAG_USER_AGENT_NORMALIZED = 8;

/**
* maximum number of concurrent downloads
*/
private final int mMaxConcurrent;

private final int mFlags;

public TileSourcePolicy() {
this(0, true, true);
this(0, 0);
}

public TileSourcePolicy(final int pMaxConcurrent,
final boolean pAcceptsBulkDownload,
final boolean pAcceptsMeaninglessUserAgent) {
final int pFlags) {
mMaxConcurrent = pMaxConcurrent;
mAcceptsBulkDownload = pAcceptsBulkDownload;
mAcceptsMeaninglessUserAgent = pAcceptsMeaninglessUserAgent;
mFlags = pFlags;
}

public int getMaxConcurrent() {
return mMaxConcurrent;
}

public boolean acceptsBulkDownload() {
return mAcceptsBulkDownload;
return (mFlags & FLAG_NO_BULK) == 0;
}

private boolean acceptsMeaninglessUserAgent() {
return (mFlags & FLAG_USER_AGENT_MEANINGFUL) == 0;
}

public boolean normalizesUserAgent() {
return (mFlags & FLAG_USER_AGENT_NORMALIZED) != 0;
}

public boolean acceptsPreventive() {
return (mFlags & FLAG_NO_PREVENTIVE) == 0;
}

public boolean acceptsUserAgent(final String pUserAgent) {
if (mAcceptsMeaninglessUserAgent) {
if (acceptsMeaninglessUserAgent()) {
return true;
}
return pUserAgent != null

0 comments on commit 7292c99

Please sign in to comment.
You can’t perform that action at this time.