Android Open Source - aBusTripMK Open Street Map 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

// Created by plusminus on 21:46:22 - 25.09.2008
package org.andnav.osm.views.util;
//from   www  .  j  av a 2s.c om
import java.io.File;

import org.andnav.osm.services.IOpenStreetMapTileProviderCallback;
import org.andnav.osm.services.IOpenStreetMapTileProviderService;
import org.andnav.osm.services.util.OpenStreetMapTile;
import org.andnav.osm.util.constants.OpenStreetMapConstants;
import org.andnav.osm.views.util.constants.OpenStreetMapViewConstants;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

/**
 * 
 * @author Nicolas Gramlich
 * 
 */
public class OpenStreetMapTileProvider implements ServiceConnection, OpenStreetMapConstants,
                OpenStreetMapViewConstants {
        // ===========================================================
        // Constants
        // ===========================================================

        // ===========================================================
        // Fields
        // ===========================================================

        /** cache provider */
        protected OpenStreetMapTileCache mTileCache;

        private IOpenStreetMapTileProviderService mTileService;
        private Handler mDownloadFinishedHandler;

        // ===========================================================
        // Constructors
        // ===========================================================

        public OpenStreetMapTileProvider(final Context ctx,
                        final Handler aDownloadFinishedListener) {
                this.mTileCache = new OpenStreetMapTileCache();
                
                if(!ctx.bindService(new Intent(IOpenStreetMapTileProviderService.class.getName()), this, Context.BIND_AUTO_CREATE))
                        Log.e(DEBUGTAG, "Could not bind to " + IOpenStreetMapTileProviderService.class.getName());
                
                this.mDownloadFinishedHandler = aDownloadFinishedListener;
        }

        // ===========================================================
        // Getter & Setter
        // ===========================================================

        // ===========================================================
        // Methods from SuperClass/Interfaces
        // ===========================================================

        public void onServiceConnected(final ComponentName name, final IBinder service) {
                mTileService = IOpenStreetMapTileProviderService.Stub.asInterface(service);
                try {
                        mDownloadFinishedHandler.sendEmptyMessage(OpenStreetMapTile.MAPTILE_SUCCESS_ID);
                } catch(Exception e) {
                        Log.e(DEBUGTAG, "Error sending success message on connect", e);
                }
                Log.d(DEBUGTAG, "connected");
        };
        
        public void onServiceDisconnected(final ComponentName name) {
                mTileService = null;
                Log.d(DEBUGTAG, "disconnected");
        }
        
        // ===========================================================
        // Methods
        // ===========================================================

        /**
         * Get the tile from the cache.
         * If it's in the cache then it will be returned.
         * If not it will return null and request it from the service.
         * In turn, the service will request it from the file system.
         * If it's found in the file system it will notify the callback.
         * If not it will initiate a download.
         * When the download has finished it will notify the callback.
         * @param aTile the tile being requested
         * @return the tile bitmap if found in the cache, null otherwise
         */
        public Bitmap getMapTile(final OpenStreetMapTile aTile) {
                if (this.mTileCache.containsTile(aTile)) {                                                      // from cache
                        if (DEBUGMODE)
                                Log.d(DEBUGTAG, "MapTileCache succeeded for: " + aTile);
                        return mTileCache.getMapTile(aTile);                    
                } else {                                                                                                                        // from service
                        if (mTileService == null) {
                                if (DEBUGMODE)
                                        Log.d(DEBUGTAG, "Cache failed, can't get from FS because no tile service: " + aTile);
                        } else {
                                if (DEBUGMODE)
                                        Log.d(DEBUGTAG, "Cache failed, trying from FS: " + aTile);
                                try {
                                        mTileService.requestMapTile(aTile.rendererID, aTile.zoomLevel, aTile.x, aTile.y, mServiceCallback);
                                } catch (Throwable e) {
                                        Log.e(DEBUGTAG, "Error getting map tile from tile service: " + aTile, e);
                                }
                        }
                        return null;
                }
        }

        // ===========================================================
        // Inner and Anonymous Classes
        // ===========================================================
        
        IOpenStreetMapTileProviderCallback mServiceCallback = new IOpenStreetMapTileProviderCallback.Stub() {

                public void mapTileRequestCompleted(int rendererID, int zoomLevel, int tileX, int tileY, String aTilePath) throws RemoteException {
                        final OpenStreetMapTile tile = new OpenStreetMapTile(rendererID, zoomLevel, tileX, tileY);
                        if (aTilePath != null) {
                                try {
                                        final Bitmap bitmap = BitmapFactory.decodeFile(aTilePath);
                                        if (bitmap != null) {
                                                mTileCache.putTile(tile, bitmap);
                                        } else {
                                                // if we couldn't load it then it's invalid - delete it
                                                try {
                                                        new File(aTilePath).delete();
                                                } catch (Throwable e) {
                                                        Log.e(DEBUGTAG, "Error deleting invalid file: " + aTilePath, e);
                                                }
                                        }
                                } catch (OutOfMemoryError e) {
                                        Log.e(DEBUGTAG, "OutOfMemoryError putting tile in cache: " + tile);
                                }
                        }
                        mDownloadFinishedHandler.sendEmptyMessage(OpenStreetMapTile.MAPTILE_SUCCESS_ID);
                        if (DEBUGMODE)
                                Log.d(DEBUGTAG, "MapTile request complete: " + tile);
                }
        };

}




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