Android Open Source - android_maplib Gps Event Source






From Project

Back to project page android_maplib.

License

The source code is released under:

GNU General Public License

If you think the Android project android_maplib 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

/*
 * Project:  NextGIS Mobile/*from  ww  w  .  j a  v a2s  .  co m*/
 * Purpose:  Mobile GIS for Android.
 * Author:   Dmitry Baryshnikov (aka Bishop), bishop.dev@gmail.com
 *           NikitaFeodonit, nfeodonit@yandex.com
 * *****************************************************************************
 * Copyright (c) 2012-2015. NextGIS, info@nextgis.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.nextgis.maplib.location;

import android.content.Context;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import com.nextgis.maplib.api.GpsEventListener;

import java.util.ArrayList;
import java.util.List;


public class GpsEventSource
{
    protected List<GpsEventListener> mListeners;

    protected LocationManager     mLocationManager;
    protected GpsLocationListener mGpsLocationListener;
    protected GpsStatusListener   mGpsStatusListener;


    public GpsEventSource(Context context)
    {
        mListeners = new ArrayList<>();

        mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        mGpsLocationListener = new GpsLocationListener();
        mGpsStatusListener = new GpsStatusListener();
    }


    /**
     * Add new listener for GPS events. You will likely want to call addListener() from your
     * Activity's or Fragment's onResume() method, to enable the features. Remember to call the
     * corresponding removeListener() in your Activity's or Fragment's onPause() method, to prevent
     * unnecessary use of the battery.
     *
     * @param listener
     *         A listener class implements GpsEventListener adding to listeners array
     */
    public void addListener(GpsEventListener listener)
    {
        if (mListeners != null && !mListeners.contains(listener)) {
            mListeners.add(listener);

            if (mListeners.size() == 1) {

                if (mLocationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER)) {
                    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                                                            mGpsLocationListener);
                }

                if (mLocationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)) {
                    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                                                            mGpsLocationListener);
                }

                mLocationManager.addGpsStatusListener(mGpsStatusListener);
            }
        }
    }


    /**
     * Remove listener from listeners of GPS events. You will likely want to call removeListener()
     * from your Activity's or Fragment's onPause() method, to prevent unnecessary use of the
     * battery. Remember to call the corresponding addListener() in your Activity's or Fragment's
     * onResume() method.
     *
     * @param listener
     *         A listener class implements GpsEventListener removing from listeners array
     */
    public void removeListener(GpsEventListener listener)
    {
        if (mListeners != null) {
            mListeners.remove(listener);

            if (mListeners.size() == 0) {
                mLocationManager.removeUpdates(mGpsLocationListener);
                mLocationManager.removeGpsStatusListener(mGpsStatusListener);
            }
        }
    }


    protected final class GpsLocationListener
            implements LocationListener
    {

        public void onLocationChanged(Location location)
        {
            for (GpsEventListener listener : mListeners) {
                listener.onLocationChanged(location);
            }
        }


        public void onProviderDisabled(String arg0)
        {
            // TODO Auto-generated method stub

        }


        public void onProviderEnabled(String provider)
        {
            // TODO Auto-generated method stub

        }


        public void onStatusChanged(
                String provider,
                int status,
                Bundle extras)
        {
            // TODO Auto-generated method stub

        }
    }


    private final class GpsStatusListener
            implements GpsStatus.Listener
    {

        @Override
        public void onGpsStatusChanged(int event)
        {
            for (GpsEventListener listener : mListeners) {
                listener.onGpsStatusChanged(event);
            }
        }
    }
}




Java Source Code List

com.nextgis.maplib.ApplicationTest.java
com.nextgis.maplib.api.GpsEventListener.java
com.nextgis.maplib.api.IGISApplication.java
com.nextgis.maplib.api.IJSONStore.java
com.nextgis.maplib.api.ILayerView.java
com.nextgis.maplib.api.ILayer.java
com.nextgis.maplib.api.IMapView.java
com.nextgis.maplib.api.INGWLayer.java
com.nextgis.maplib.api.IRenderer.java
com.nextgis.maplib.api.MapEventListener.java
com.nextgis.maplib.datasource.DatabaseHelper.java
com.nextgis.maplib.datasource.GeoEnvelope.java
com.nextgis.maplib.datasource.GeoGeometryCollection.java
com.nextgis.maplib.datasource.GeoGeometry.java
com.nextgis.maplib.datasource.GeoLineString.java
com.nextgis.maplib.datasource.GeoLinearRing.java
com.nextgis.maplib.datasource.GeoMultiLineString.java
com.nextgis.maplib.datasource.GeoMultiPoint.java
com.nextgis.maplib.datasource.GeoMultiPolygon.java
com.nextgis.maplib.datasource.GeoPoint.java
com.nextgis.maplib.datasource.GeoPolygon.java
com.nextgis.maplib.datasource.Geo.java
com.nextgis.maplib.datasource.NGWLayerContentProvider.java
com.nextgis.maplib.datasource.TileItem.java
com.nextgis.maplib.datasource.ngw.Connection.java
com.nextgis.maplib.datasource.ngw.Connections.java
com.nextgis.maplib.datasource.ngw.INGWResource.java
com.nextgis.maplib.datasource.ngw.LayerWithStyles.java
com.nextgis.maplib.datasource.ngw.ResourceGroup.java
com.nextgis.maplib.datasource.ngw.Resource.java
com.nextgis.maplib.display.GISDisplay.java
com.nextgis.maplib.display.Renderer.java
com.nextgis.maplib.display.SimpleFeatureRenderer.java
com.nextgis.maplib.display.SimpleLineStyle.java
com.nextgis.maplib.display.SimpleMarkerStyle.java
com.nextgis.maplib.display.Style.java
com.nextgis.maplib.display.TMSRenderer.java
com.nextgis.maplib.location.GpsEventSource.java
com.nextgis.maplib.map.LayerFactory.java
com.nextgis.maplib.map.LayerGroup.java
com.nextgis.maplib.map.Layer.java
com.nextgis.maplib.map.MapBase.java
com.nextgis.maplib.map.MapContentProviderHelper.java
com.nextgis.maplib.map.MapDrawable.java
com.nextgis.maplib.map.MapEventSource.java
com.nextgis.maplib.map.NGWRasterLayer.java
com.nextgis.maplib.map.NGWVectorLayer.java
com.nextgis.maplib.map.RemoteTMSLayer.java
com.nextgis.maplib.map.TMSLayer.java
com.nextgis.maplib.map.VectorLayer.java
com.nextgis.maplib.util.Constants.java
com.nextgis.maplib.util.DatabaseContext.java
com.nextgis.maplib.util.Feature.java
com.nextgis.maplib.util.FileUtil.java
com.nextgis.maplib.util.GeoConstants.java
com.nextgis.maplib.util.NetworkUtil.java
com.nextgis.maplib.util.SettingsConstants.java
com.nextgis.maplib.util.VectorCacheItem.java