Android Open Source - sthlmtraveling My Location Manager






From Project

Back to project page sthlmtraveling.

License

The source code is released under:

Apache License

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

/*
 * Copyright (C) 2009 Johan Nilsson <http://markupartist.com>
 */* w ww.  j a  v  a  2s  .  c  o  m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.markupartist.sthlmtraveling;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;

/**
 * Manager for retrieving the user location.
 */
public class MyLocationManager {
    private static final String TAG = "MyLocationManager";
    private static final long FIND_LOCATION_TIME_OUT_MILLIS = 8000;
    private static final int ACCEPTED_LOCATION_ACCURACY_METERS = 300;
    private static final int ACCEPTED_LOCATION_AGE_MILLIS = 600000;
    private LocationRequestTimeOut mLocationRequestTimeOut;
    private LocationManager mLocationManager;
    private MyLocationFoundListener mMyLocationFoundListener;
    private MyLocationListener mGpsLocationListener;
    private MyLocationListener mNetworkLocationListener;
    private LocationListener mLocationListener;

    /**
     * Constructs a new MyLocationManager.
     * @param locationManager the location manager
     */
    public MyLocationManager(LocationManager locationManager) {
        mLocationManager = locationManager;
        mLocationRequestTimeOut = new LocationRequestTimeOut();
    }

    public void setLocationListener(LocationListener locationListener) {
        mLocationListener = locationListener;
    }

    /**
     * Request location updates, will periodically query the GPS and network
     * provider for location updates.
     * @param myLocationFoundListener the callback
     */
    public void requestLocationUpdates(MyLocationFoundListener myLocationFoundListener) {
        mMyLocationFoundListener = myLocationFoundListener;

        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            mGpsLocationListener = new MyLocationListener();
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                    0, 0, mGpsLocationListener);
        }

        if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            mNetworkLocationListener = new MyLocationListener();
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                    0, 0, mNetworkLocationListener);
        }

        if (mNetworkLocationListener != null || mGpsLocationListener != null) {
            mLocationRequestTimeOut.start();
        } else {
            reportLocationFound(getLastKnownLocation());
        }
    }

    /**
     * Removes any current registration for location updates. 
     */
    public void removeUpdates() {
        if (mGpsLocationListener != null)
            mLocationManager.removeUpdates(mGpsLocationListener);
        if (mNetworkLocationListener != null)
            mLocationManager.removeUpdates(mNetworkLocationListener);

        mLocationRequestTimeOut.cancel();
        mMyLocationFoundListener = null;
    }

    /**
     * Get the last known location, will look for the best location from both
     * GPS and the network provider.
     * @return
     */
    public Location getLastKnownLocation() {
        Location gpsLocation =
            mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location networkLocation =
            mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        return getBestLocation(gpsLocation, networkLocation);
    }

    /**
     * Get the best location out of the passed {@link Location}S.
     * @param location1 the location
     * @param location2 the location
     * @return the best location
     */
    public Location getBestLocation(Location location1, Location location2) {
        if (location1 == null && location2 == null) {
            return null;
        } else if (location2 == null) {
            return location1;
        } else if (location1 == null) {
            return location2;
        } else {
            int location1Score =
                (int) (getLocationAge(location1) + location1.getAccuracy());
            int location2Score =
                (int) (getLocationAge(location2) + location2.getAccuracy());

            if (location1Score <= location2Score) {
                Log.d(TAG, location1.getProvider() + " won");
                return location1;
            }
            return location2;
        }
    }

    private void reportLocationFound(Location location) {
        Log.d(TAG, "reportLocationFound");
        if (mMyLocationFoundListener != null) {
            mMyLocationFoundListener.onMyLocationFound(location);
        }
    }

    /**
     * Is the provided {@link Location} accepted?
     * @param location the location
     * @return <code>true</code> if accepted <code>false</code> otherwise
     */
    public boolean shouldAcceptLocation(Location location) {
        if (location == null)
            return false;

        long timeSinceUpdate = System.currentTimeMillis() - location.getTime();
        if (timeSinceUpdate <= ACCEPTED_LOCATION_AGE_MILLIS 
                && location.getAccuracy() <= ACCEPTED_LOCATION_ACCURACY_METERS) {
            return true;
        }
        return false;
    }

    public int getLocationAge(Location location) {
        return (int) ((System.currentTimeMillis() - location.getTime()) / 1000);
    }

    /**
     * Used for receiving notifications from the LocationManager when the 
     * location has changed.
     */
    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            if (mLocationListener != null) {
                mLocationListener.onLocationChanged(location);
            }
            
            if (shouldAcceptLocation(location)) {
                reportLocationFound(location);
                removeUpdates();
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            // Needed by the interface.
            
        }

        @Override
        public void onProviderEnabled(String provider) {
            // Needed by the interface.
            
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // Needed by the interface.
        }
    }
    
    
    /**
     * Used to determine when we should stop search for location changes.
     */
    private class LocationRequestTimeOut extends CountDownTimer {
        public LocationRequestTimeOut() {
            super(FIND_LOCATION_TIME_OUT_MILLIS, 1000);
        }

        @Override
        public void onFinish() {
            reportLocationFound(getLastKnownLocation());
            // Once we have reported a location, remove the need to search 
            // for more.
            removeUpdates();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // Needed by the interface.
        }        
    }

    /**
     * 
     */
    public interface MyLocationFoundListener {
        /**
         * Called when a location is found.
         * @param location the location, could be null
         */
        public void onMyLocationFound(Location location);
    }
}




Java Source Code List

com.markupartist.sthlmtraveling.AboutActivity.java
com.markupartist.sthlmtraveling.AllTests.java
com.markupartist.sthlmtraveling.AppConfig.java
com.markupartist.sthlmtraveling.AutoCompleteStopAdapter.java
com.markupartist.sthlmtraveling.BaseActivity.java
com.markupartist.sthlmtraveling.BaseFragmentActivity.java
com.markupartist.sthlmtraveling.BaseFragment.java
com.markupartist.sthlmtraveling.BaseListActivity.java
com.markupartist.sthlmtraveling.BaseListFragmentActivity.java
com.markupartist.sthlmtraveling.BaseListFragment.java
com.markupartist.sthlmtraveling.BasePreferenceActivity.java
com.markupartist.sthlmtraveling.ChangeRouteTimeActivity.java
com.markupartist.sthlmtraveling.DepartureAdapter.java
com.markupartist.sthlmtraveling.DeparturesActivity.java
com.markupartist.sthlmtraveling.DeviationDetailActivity.java
com.markupartist.sthlmtraveling.DeviationsActivity.java
com.markupartist.sthlmtraveling.DialogHelper.java
com.markupartist.sthlmtraveling.FavoritesFragment.java
com.markupartist.sthlmtraveling.MultipleListAdapter.java
com.markupartist.sthlmtraveling.MyApplication.java
com.markupartist.sthlmtraveling.MyLocationManager.java
com.markupartist.sthlmtraveling.NearbyActivity.java
com.markupartist.sthlmtraveling.PlannerFragmentActivity.java
com.markupartist.sthlmtraveling.PlannerFragment.java
com.markupartist.sthlmtraveling.PointOnMapActivity.java
com.markupartist.sthlmtraveling.RouteDetailActivity.java
com.markupartist.sthlmtraveling.RouteParserTest.java
com.markupartist.sthlmtraveling.RoutesActivity.java
com.markupartist.sthlmtraveling.SearchDeparturesFragmentActivity.java
com.markupartist.sthlmtraveling.SearchDeparturesFragment.java
com.markupartist.sthlmtraveling.SectionedAdapter.java
com.markupartist.sthlmtraveling.SettingsActivity.java
com.markupartist.sthlmtraveling.StartActivity.java
com.markupartist.sthlmtraveling.TrafficStatusFragment.java
com.markupartist.sthlmtraveling.ViewOnMapActivity.java
com.markupartist.sthlmtraveling.provider.FavoritesDbAdapter.java
com.markupartist.sthlmtraveling.provider.HistoryDbAdapter.java
com.markupartist.sthlmtraveling.provider.JourneysProvider.java
com.markupartist.sthlmtraveling.provider.PlacesProvider.java
com.markupartist.sthlmtraveling.provider.TransportMode.java
com.markupartist.sthlmtraveling.provider.departure.DeparturesStore.java
com.markupartist.sthlmtraveling.provider.deviation.DeviationNotificationDbAdapter.java
com.markupartist.sthlmtraveling.provider.deviation.DeviationStore.java
com.markupartist.sthlmtraveling.provider.deviation.Deviation.java
com.markupartist.sthlmtraveling.provider.planner.JourneyQuery.java
com.markupartist.sthlmtraveling.provider.planner.Planner.java
com.markupartist.sthlmtraveling.provider.site.Site.java
com.markupartist.sthlmtraveling.provider.site.SitesStore.java
com.markupartist.sthlmtraveling.receivers.OnAlarmReceiver.java
com.markupartist.sthlmtraveling.receivers.OnBootReceiver.java
com.markupartist.sthlmtraveling.service.DataMigrationService.java
com.markupartist.sthlmtraveling.service.DeviationService.java
com.markupartist.sthlmtraveling.service.WakefulIntentService.java
com.markupartist.sthlmtraveling.ui.view.DelayAutoCompleteTextView.java
com.markupartist.sthlmtraveling.ui.view.LineSegment.java
com.markupartist.sthlmtraveling.ui.view.SmsTicketDialog.java
com.markupartist.sthlmtraveling.ui.view.TripView.java
com.markupartist.sthlmtraveling.utils.Analytics.java
com.markupartist.sthlmtraveling.utils.BarcodeScannerIntegrator.java
com.markupartist.sthlmtraveling.utils.DateTimeUtil.java
com.markupartist.sthlmtraveling.utils.DisplayMetricsHelper.java
com.markupartist.sthlmtraveling.utils.ErrorReporter.java
com.markupartist.sthlmtraveling.utils.HttpHelper.java
com.markupartist.sthlmtraveling.utils.IntentUtil.java
com.markupartist.sthlmtraveling.utils.LocationUtils.java
com.markupartist.sthlmtraveling.utils.StreamUtils.java
com.markupartist.sthlmtraveling.utils.StringUtils.java
com.markupartist.sthlmtraveling.utils.ViewHelper.java
com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java