Android Open Source - aBusTripMK Open Street Map Async Tile Provider






From Project

Back to project page aBusTripMK.

License

The source code is released under:

GNU General Public License

If you think the Android project aBusTripMK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.andnav.osm.services.util;
//from  w w w .j  ava 2s  . co m
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.NoSuchElementException;

import org.andnav.osm.services.IOpenStreetMapTileProviderCallback;
import org.andnav.osm.services.util.constants.OpenStreetMapServiceConstants;

import android.os.DeadObjectException;
import android.os.RemoteException;
import android.util.Log;

public abstract class OpenStreetMapAsyncTileProvider implements
    OpenStreetMapServiceConstants {

  private final int mThreadPoolSize;
  private final int mPendingQueueSize;
  private final ThreadGroup mThreadPool = new ThreadGroup(debugtag());
  private final LinkedHashMap<OpenStreetMapTile, Object> mPending;
  private static final Object PRESENT = new Object();

  public OpenStreetMapAsyncTileProvider(final int aThreadPoolSize,
      final int aPendingQueueSize) {
    mThreadPoolSize = aThreadPoolSize;
    mPendingQueueSize = aPendingQueueSize;
    mPending = new LinkedHashMap<OpenStreetMapTile, Object>(
        aPendingQueueSize + 2, 0.1f, true) {
      private static final long serialVersionUID = 1L;

      @Override
      protected boolean removeEldestEntry(
          Entry<OpenStreetMapTile, Object> pEldest) {
        final boolean max = size() > mPendingQueueSize;
        return max;
      }
    };
  }

  public void loadMapTileAsync(final OpenStreetMapTile aTile,
      final IOpenStreetMapTileProviderCallback aCallback) {

    final int activeCount = mThreadPool.activeCount();

    // sanity check
    if (activeCount == 0 && !mPending.isEmpty()) {
      Log
          .w(debugtag(),
              "Unexpected - no active threads but pending queue not empty");
      mPending.clear();
    }

    // this will put the tile in the queue, or move it to the front of the
    // queue if it's already present
    mPending.put(aTile, PRESENT);

    if (DEBUGMODE)
      Log.d(debugtag(), activeCount + " active threads");
    if (activeCount < mThreadPoolSize) {
      final Thread t = new Thread(mThreadPool, getTileLoader(aCallback));
      t.start();
    }
  }

  /**
   * The debug tag. Because the tag of the abstract class is not so
   * interesting.
   * 
   * @return
   */
  protected abstract String debugtag();

  protected abstract Runnable getTileLoader(
      final IOpenStreetMapTileProviderCallback aCallback);

  protected abstract class TileLoader implements Runnable {
    final IOpenStreetMapTileProviderCallback mCallback;
    private Iterator<OpenStreetMapTile> mIterator;

    public TileLoader(final IOpenStreetMapTileProviderCallback aCallback) {
      mCallback = aCallback;
    }

    /**
     * Load the requested tile.
     * 
     * @param aTile
     *            the tile to load
     * @return the path of the requested tile
     * @throws CantContinueException
     *             if it is not possible to continue with processing the
     *             queue
     */
    protected abstract String loadTile(OpenStreetMapTile aTile)
        throws CantContinueException;

    private OpenStreetMapTile nextTile() {
      while (true) {
        if (mIterator == null) {
          mIterator = mPending.keySet().iterator();
        }
        if (!mIterator.hasNext()) {
          return null;
        }
        try {
          synchronized (mPending) {
            final OpenStreetMapTile tile = mIterator.next();
            try {
              mIterator.remove();
            } catch (ConcurrentModificationException e) {
              // couldn't remove this request
              // never mind, we'll process it again
            } catch (NoSuchElementException e) {
              // we shouldn't get this, but just in case
              return null;
            }
            return tile;
          }
        } catch (ConcurrentModificationException e) {
          // get a new iterator and try again
          mIterator = null;
        }
      }
    }

    final public void run() {

      OpenStreetMapTile tile;
      while ((tile = nextTile()) != null) {
        if (DEBUGMODE)
          Log.d(debugtag(), "Next tile: " + tile);
        String path = null;
        try {
          path = loadTile(tile);
        } catch (final CantContinueException e) {
          Log.i(debugtag(), "Tile loader can't continue");
          mPending.clear();
        } catch (final Throwable e) {
          Log.e(debugtag(), "Error downloading tile: " + tile, e);
        } finally {
          // Tell the callback we've finished.
          try {
            mCallback.mapTileRequestCompleted(tile.rendererID,
                tile.zoomLevel, tile.x, tile.y, path);
          } catch (DeadObjectException e) {
            // our caller has died so there's not much point
            // carrying on
            Log.e(debugtag(), "Caller has died");
            break;
          } catch (RemoteException e) {
            Log.e(debugtag(), "Service failed", e);
          }
        }
      }
      if (DEBUGMODE)
        Log.d(debugtag(), "No more tiles");
      mPending.clear();
    }
  }

  protected class CantContinueException extends Exception {
    private static final long serialVersionUID = 146526524087765133L;
  }
}




Java Source Code List

com.app.busmk.DataBaseHelper.java
com.app.busmk.MyOverLay.java
com.app.busmk.a12.java
com.app.busmk.a15.java
com.app.busmk.a19.java
com.app.busmk.a22.java
com.app.busmk.a24.java
com.app.busmk.a2.java
com.app.busmk.a3.java
com.app.busmk.a41.java
com.app.busmk.a5.java
com.app.busmk.a65b.java
com.app.busmk.a7.java
com.app.busmk.a8.java
com.app.busmk.about.java
com.app.busmk.baraj_lista.java
com.app.busmk.baraj_mapa.java
com.app.busmk.main.java
com.app.busmk.main_menu.java
com.app.busmk.other.java
com.app.busmk.splash.java
org.andnav.osm.exceptions.EmptyCacheException.java
org.andnav.osm.services.OpenStreetMapTileProviderService.java
org.andnav.osm.services.util.OpenStreetMapAsyncTileProvider.java
org.andnav.osm.services.util.OpenStreetMapTileDownloader.java
org.andnav.osm.services.util.OpenStreetMapTileFilesystemProvider.java
org.andnav.osm.services.util.OpenStreetMapTileProviderDataBase.java
org.andnav.osm.services.util.OpenStreetMapTile.java
org.andnav.osm.services.util.StreamUtils.java
org.andnav.osm.services.util.constants.OpenStreetMapServiceConstants.java
org.andnav.osm.util.BoundingBoxE6.java
org.andnav.osm.util.GeoPoint.java
org.andnav.osm.util.MyMath.java
org.andnav.osm.util.NetworkLocationIgnorer.java
org.andnav.osm.util.constants.GeoConstants.java
org.andnav.osm.util.constants.OpenStreetMapConstants.java
org.andnav.osm.views.OpenStreetMapViewController.java
org.andnav.osm.views.OpenStreetMapView.java
org.andnav.osm.views.overlay.MyLocationOverlay.java
org.andnav.osm.views.overlay.OpenStreetMapTilesOverlay.java
org.andnav.osm.views.overlay.OpenStreetMapViewItemizedOverlay.java
org.andnav.osm.views.overlay.OpenStreetMapViewOverlayItem.java
org.andnav.osm.views.overlay.OpenStreetMapViewOverlay.java
org.andnav.osm.views.util.HttpUserAgentHelper.java
org.andnav.osm.views.util.LRUMapTileCache.java
org.andnav.osm.views.util.Mercator.java
org.andnav.osm.views.util.MyMath.java
org.andnav.osm.views.util.OpenStreetMapRendererInfo.java
org.andnav.osm.views.util.OpenStreetMapTileCache.java
org.andnav.osm.views.util.OpenStreetMapTileProvider.java
org.andnav.osm.views.util.constants.MathConstants.java
org.andnav.osm.views.util.constants.OpenStreetMapViewConstants.java