org.osmdroid.tileprovider.MapTileRequestState Java Examples

The following examples show how to use org.osmdroid.tileprovider.MapTileRequestState. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MapTileModuleProviderBase.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
public void loadMapTileAsync(final MapTileRequestState pState) {
	// Make sure we're not detached
	if (mExecutor.isShutdown())
		return;

	synchronized (mQueueLockObject) {
		if (Configuration.getInstance().isDebugTileProviders()) {
			Log.d(IMapView.LOGTAG,"MapTileModuleProviderBase.loadMaptileAsync() on provider: "
					+ getName() + " for tile: " + MapTileIndex.toString(pState.getMapTile()));
			if (mPending.containsKey(pState.getMapTile()))
				Log.d(IMapView.LOGTAG,"MapTileModuleProviderBase.loadMaptileAsync() tile already exists in request queue for modular provider. Moving to front of queue.");
			else
				Log.d(IMapView.LOGTAG,"MapTileModuleProviderBase.loadMaptileAsync() adding tile to request queue for modular provider.");
		}

		// this will put the tile in the queue, or move it to the front of
		// the queue if it's already present
		mPending.put(pState.getMapTile(), pState);
	}
	try {
		mExecutor.execute(getTileLoader());
	} catch (final RejectedExecutionException e) {
		Log.w(IMapView.LOGTAG,"RejectedExecutionException", e);
	}
}
 
Example #2
Source File: MapTileModuleProviderBase.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
protected void tileLoadedFailed(final MapTileRequestState pState) {
	if (Configuration.getInstance().isDebugTileProviders()) {
		Log.d(IMapView.LOGTAG,"TileLoader.tileLoadedFailed() on provider: " + getName()
				+ " with tile: " + MapTileIndex.toString(pState.getMapTile()));
	}
	removeTileFromQueues(pState.getMapTile());
	pState.getCallback().mapTileRequestFailed(pState);
}
 
Example #3
Source File: MapTileProviderTest.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Test that adding the same tile more than once moves it up the queue.
 */
public void test_jump_queue() throws InterruptedException {
	final long tile1 = MapTileIndex.getTileIndex(1, 1, 1);
	final long tile2 = MapTileIndex.getTileIndex(2, 2, 2);
	final long tile3 = MapTileIndex.getTileIndex(3, 3, 3);

	// request tile1, tile2, tile3, then tile2 again
	final MapTileRequestState state1 = new MapTileRequestState(tile1,
			mProviders, mTileProviderCallback);
	mTileProvider.loadMapTileAsync(state1);
	Thread.sleep(100); // give the thread time to run
	final MapTileRequestState state2 = new MapTileRequestState(tile2,
			mProviders, mTileProviderCallback);
	mTileProvider.loadMapTileAsync(state2);
	Thread.sleep(100); // give the thread time to run
	final MapTileRequestState state3 = new MapTileRequestState(tile3,
			mProviders, mTileProviderCallback);
	mTileProvider.loadMapTileAsync(state3);
	Thread.sleep(100); // give the thread time to run
	final MapTileRequestState state4 = new MapTileRequestState(tile2,
			mProviders, mTileProviderCallback);
	mTileProvider.loadMapTileAsync(state4);

	// wait up to 10 seconds (because it takes 1 second for each tile + an extra
	// second)
	long timeout=System.currentTimeMillis()+10000;
	while (3 != mTiles.size() && System.currentTimeMillis() < timeout){
		Thread.sleep(250);
	}

	// check that there are three tiles in the list (ie no duplicates)
	assertEquals("Three tiles in the list", 3, mTiles.size());

	// the tiles should have been loaded in the order 1, 2, 3
	// 3 jumped ahead of 2, but then 2 jumped ahead of it again
	assertEquals("tile1 is first", tile1, (long)mTiles.get(0));
	assertEquals("tile2 is second", tile2, (long)mTiles.get(1));
	assertEquals("tile3 is third", tile3, (long)mTiles.get(2));
}
 
Example #4
Source File: MapTileProviderTest.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Test that the tiles are loaded in most recently accessed order.
 */
public void test_order() throws InterruptedException {

	final long tile1 = MapTileIndex.getTileIndex(1, 1, 1);
	final long tile2 = MapTileIndex.getTileIndex(2, 2, 2);
	final long tile3 = MapTileIndex.getTileIndex(3, 3, 3);

	// request the three tiles
	final MapTileRequestState state1 = new MapTileRequestState(tile1,
			mProviders, mTileProviderCallback);
	mTileProvider.loadMapTileAsync(state1);
	Thread.sleep(100); // give the thread time to run
	final MapTileRequestState state2 = new MapTileRequestState(tile2,
			mProviders, mTileProviderCallback);
	mTileProvider.loadMapTileAsync(state2);
	Thread.sleep(100); // give the thread time to run
	final MapTileRequestState state3 = new MapTileRequestState(tile3,
			mProviders, mTileProviderCallback);
	mTileProvider.loadMapTileAsync(state3);

	// wait up to 10 seconds (because it takes 1 second for each tile + an extra
	// second)

	long timeout=System.currentTimeMillis()+10000;
	while (3 != mTiles.size() && System.currentTimeMillis() < timeout){
		Thread.sleep(250);
	}

	// check that there are three tiles in the list (ie no duplicates)
	assertEquals("Three tiles in the list", 3, mTiles.size());

	// the tiles should have been loaded in the order 1, 3, 2
	// because 1 was loaded immediately, 2 was next,
	// but 3 was requested before 2 started, so it jumped the queue
	assertEquals("tile1 is first", tile1, (long)mTiles.get(0));
	assertEquals("tile3 is second", tile3, (long)mTiles.get(1));
	assertEquals("tile2 is third", tile2, (long)mTiles.get(2));
}
 
Example #5
Source File: MapTileProviderTest.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
public void test_put_twice() {

		final long tile = MapTileIndex.getTileIndex(1, 1, 1);

		// request the same tile twice
		final MapTileRequestState state = new MapTileRequestState(tile, mProviders,
				mTileProviderCallback);
		mTileProvider.loadMapTileAsync(state);
		mTileProvider.loadMapTileAsync(state);

		// check that is only one tile pending
		assertEquals("One tile pending", 1, mTileProvider.mPending.size());
	}
 
Example #6
Source File: MapTileDownloader.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
protected void tileLoaded(final MapTileRequestState pState, final Drawable pDrawable) {
	removeTileFromQueues(pState.getMapTile());
	// don't return the tile because we'll wait for the fs provider to ask for it
	// this prevent flickering when a load of delayed downloads complete for tiles
	// that we might not even be interested in any more
	pState.getCallback().mapTileRequestCompleted(pState, null);
	// We want to return the Bitmap to the BitmapPool if applicable
	BitmapPool.getInstance().asyncRecycle(pDrawable);
}
 
Example #7
Source File: MapTileModuleProviderBase.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
protected void tileLoadedScaled(final MapTileRequestState pState, final Drawable pDrawable) {
	if (Configuration.getInstance().isDebugTileProviders()) {
		Log.d(IMapView.LOGTAG,"TileLoader.tileLoadedScaled() on provider: " + getName()
				+ " with tile: " + MapTileIndex.toString(pState.getMapTile()));
	}
	removeTileFromQueues(pState.getMapTile());
	ExpirableBitmapDrawable.setState(pDrawable, ExpirableBitmapDrawable.SCALED);
	pState.getCallback().mapTileRequestExpiredTile(pState, pDrawable);
}
 
Example #8
Source File: MapTileModuleProviderBase.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * A tile has loaded but it's expired.
 * Return it <b>and</b> send request to next provider.
 */
protected void tileLoadedExpired(final MapTileRequestState pState, final Drawable pDrawable) {
	if (Configuration.getInstance().isDebugTileProviders()) {
		Log.d(IMapView.LOGTAG,"TileLoader.tileLoadedExpired() on provider: " + getName()
				+ " with tile: " + MapTileIndex.toString(pState.getMapTile()));
	}
	removeTileFromQueues(pState.getMapTile());
	ExpirableBitmapDrawable.setState(pDrawable, ExpirableBitmapDrawable.EXPIRED);
	pState.getCallback().mapTileRequestExpiredTile(pState, pDrawable);
}
 
Example #9
Source File: MapTileModuleProviderBase.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * A tile has loaded.
 */
protected void tileLoaded(final MapTileRequestState pState, final Drawable pDrawable) {
	if (Configuration.getInstance().isDebugTileProviders()) {
		Log.d(IMapView.LOGTAG,"TileLoader.tileLoaded() on provider: " + getName() + " with tile: "
				+ MapTileIndex.toString(pState.getMapTile()));
	}
	removeTileFromQueues(pState.getMapTile());
	ExpirableBitmapDrawable.setState(pDrawable, ExpirableBitmapDrawable.UP_TO_DATE);
	pState.getCallback().mapTileRequestCompleted(pState, pDrawable);
}
 
Example #10
Source File: MapTileModuleProviderBase.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
protected MapTileRequestState nextTile() {

			synchronized (mQueueLockObject) {
				Long result = null;

				// get the most recently accessed tile
				// - the last item in the iterator that's not already being
				// processed
				Iterator<Long> iterator = mPending.keySet().iterator();

				// TODO this iterates the whole list, make this faster...
				while (iterator.hasNext()) {
					final Long mapTileIndex = iterator.next();
					if (!mWorking.containsKey(mapTileIndex)) {
						if (Configuration.getInstance().isDebugTileProviders()) {
							Log.d(IMapView.LOGTAG,"TileLoader.nextTile() on provider: " + getName()
									+ " found tile in working queue: " + MapTileIndex.toString(mapTileIndex));
						}
						result = mapTileIndex;
					}
				}

				if (result != null) {
					if (Configuration.getInstance().isDebugTileProviders()) {
						Log.d(IMapView.LOGTAG,"TileLoader.nextTile() on provider: " + getName()
								+ " adding tile to working queue: " + result);
					}
					mWorking.put(result, mPending.get(result));
				}

				return (result != null ? mPending.get(result) : null);
			}
		}
 
Example #11
Source File: MapTileModuleProviderBase.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
public MapTileModuleProviderBase(int pThreadPoolSize, final int pPendingQueueSize) {
	if (pPendingQueueSize < pThreadPoolSize) {
              Log.w(IMapView.LOGTAG,"The pending queue size is smaller than the thread pool size. Automatically reducing the thread pool size.");
		pThreadPoolSize = pPendingQueueSize;
	}
	mExecutor = Executors.newFixedThreadPool(pThreadPoolSize,
			new ConfigurablePriorityThreadFactory(Thread.NORM_PRIORITY, getThreadGroupName()));

	mWorking = new HashMap<>();
	mPending = new LinkedHashMap<Long, MapTileRequestState>(pPendingQueueSize + 2, 0.1f,
			true) {

		private static final long serialVersionUID = 6455337315681858866L;

		@Override
		protected boolean removeEldestEntry(
				final Map.Entry<Long, MapTileRequestState> pEldest) {
			if (size() <= pPendingQueueSize) {
				return false;
			}
			// get the oldest tile that isn't in the mWorking queue
			final Iterator<Long> iterator = mPending.keySet().iterator();
			while (iterator.hasNext()) {
				final long mapTileIndex = iterator.next();
				if (!mWorking.containsKey(mapTileIndex)) {
					final MapTileRequestState state = mPending.get(mapTileIndex);
					if (state != null) { // check for concurrency reasons
						removeTileFromQueues(mapTileIndex);
						state.getCallback().mapTileRequestFailedExceedsMaxQueueSize(state);
						return false;
					}
				}
			}
			return false;
		}
	};
}
 
Example #12
Source File: ShadowMapTileModuleProviderBase.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
@Implementation
public void loadMapTileAsync(final MapTileRequestState pState) {
  // do nothing
}
 
Example #13
Source File: OpenStreetMapTileProviderDirectTest.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
public void test_getMapTile_found() throws RemoteException, BitmapTileSourceBase.LowMemoryException, java.io.IOException {
	final long tile = MapTileIndex.getTileIndex(2, 3, 3);
	if (Build.VERSION.SDK_INT >=23)
		return;

	// create a bitmap, draw something on it, write it to a file and put it in the cache
	String path = getContext().getFilesDir().getAbsolutePath() + File.separator + "osmdroid" + File.separator;

	File temp= new File(path);
	if (!temp.exists())
		temp.mkdirs();
	Configuration.getInstance().setOsmdroidTileCache(temp);;
	path = path + "OpenStreetMapTileProviderTest.png";
	File f = new File(path);
	if (f.exists())
		f.delete();
	final Bitmap bitmap1 = Bitmap.createBitmap(
			TileSourceFactory.MAPNIK.getTileSizePixels(),
			TileSourceFactory.MAPNIK.getTileSizePixels(),
			Config.ARGB_8888);
	bitmap1.eraseColor(Color.YELLOW);
	final Canvas canvas = new Canvas(bitmap1);

	canvas.drawText("test", 10, 20, new Paint());
	try {
		f.createNewFile();
		final FileOutputStream fos = new FileOutputStream(path);
		bitmap1.compress(CompressFormat.PNG, 100, fos);
		fos.close();
	}catch (Exception ex){
		ex.printStackTrace();
		Assert.fail("unable to write temp tile " + ex);
	}
	final MapTileRequestState state = new MapTileRequestState(tile,
			new ArrayList<MapTileModuleProviderBase>(), mProvider);
	mProvider.mapTileRequestCompleted(state, TileSourceFactory.MAPNIK.getDrawable(path));

	// do the test
	final Drawable drawable = mProvider.getMapTile(tile);
	if (f.exists())
		f.delete();
	assertNotNull("Expect tile to be not null from path " + path, drawable);
	assertTrue("Expect instance of BitmapDrawable", drawable instanceof BitmapDrawable);
	final Bitmap bitmap2 = ((BitmapDrawable) drawable).getBitmap();
	assertNotNull("Expect tile to be not null", bitmap2);

	// compare a few things to see if it's the same bitmap
	// commented out due to a number of intermitent failures on API8
	if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
		assertEquals("Compare config", bitmap1.getConfig(), bitmap2.getConfig());
	}
	assertEquals("Compare width", bitmap1.getWidth(), bitmap2.getWidth());
	assertEquals("Compare height", bitmap1.getHeight(), bitmap2.getHeight());

	if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
		// compare the total thing
		final ByteBuffer bb1 = ByteBuffer.allocate(bitmap1.getWidth() * bitmap1.getHeight() * 4);
		bitmap1.copyPixelsToBuffer(bb1);
		final ByteBuffer bb2 = ByteBuffer.allocate(bitmap2.getWidth() * bitmap2.getHeight() * 4);
		bitmap2.copyPixelsToBuffer(bb2);
		assertEquals("Compare pixels", bb1, bb2);
	}
}
 
Example #14
Source File: MapTileProviderTest.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override
public void mapTileRequestCompleted(final MapTileRequestState aState,
		final Drawable aDrawable) {
	mTiles.add(aState.getMapTile());
}
 
Example #15
Source File: MapTileProviderTest.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override
public void mapTileRequestFailed(final MapTileRequestState aState) {
}
 
Example #16
Source File: MapTileProviderTest.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override
public void mapTileRequestFailedExceedsMaxQueueSize(final MapTileRequestState aState) {
}
 
Example #17
Source File: MapTileProviderTest.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override
public void mapTileRequestExpiredTile(final MapTileRequestState aState, final Drawable aDrawable) {
}
 
Example #18
Source File: MapTileModuleProviderBase.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Deprecated
protected Drawable loadTile(MapTileRequestState pState)
		throws CantContinueException {
	return loadTileIfReachable(pState.getMapTile());
}
 
Example #19
Source File: SafeMapTileFilesystemProvider.java    From android_frameworks_mapsv1 with Apache License 2.0 4 votes vote down vote up
@Override
public Drawable loadTile(final MapTileRequestState pState) throws CantContinueException {

	ITileSource tileSource = mTileSource.get();
	if (tileSource == null) {
		return null;
	}

	final MapTile tile = pState.getMapTile();

	// if there's no sdcard then don't do anything
	if (!getSdCardAvailable()) {
		if (OpenStreetMapTileProviderConstants.DEBUGMODE) {
			Log.d(IMapView.LOGTAG,"No sdcard - do nothing for tile: " + tile);
		}
		return null;
	}

	// Check the tile source to see if its file is available and if so, then render the
	// drawable and return the tile
	final File file = new File(safeTilePathBase,
			tileSource.getTileRelativeFilenameString(tile) + OpenStreetMapTileProviderConstants.TILE_PATH_EXTENSION);
	if (file.exists()) {

		try {
			final Drawable drawable = tileSource.getDrawable(file.getPath());

			// Check to see if file has expired
			final long now = System.currentTimeMillis();
			final long lastModified = file.lastModified();
			final boolean fileExpired = lastModified < now - mMaximumCachedFileAge;

			if (fileExpired && drawable != null) {
				if (OpenStreetMapTileProviderConstants.DEBUGMODE) {
					Log.d(IMapView.LOGTAG,"Tile expired: " + tile);
				}
				ExpirableBitmapDrawable.setDrawableExpired(drawable);
			}

			return drawable;
		} catch (final LowMemoryException e) {
			// low memory so empty the queue
			Log.w(IMapView.LOGTAG,"LowMemoryException downloading MapTile: " + tile + " : " + e);
			throw new CantContinueException(e);
		}
	}

	// If we get here then there is no file in the file cache
	return null;
}