Back to project page mclib.
The source code is released under:
Apache License, Version 2.0 FoundationProjectsPeopleGet InvolvedDownloadSupport ApacheHome ? Licenses Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITION...
If you think the Android project mclib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* Copyright 2012 Mikhail Chabanov/*from ww w . 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 mc.lib.location; import java.io.Closeable; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.locks.ReentrantReadWriteLock; import mc.lib.log.Log; import android.content.Context; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; /** * Helper providing simple API for obtaining geographical location. * * This helper keep track of all location providers available on your device and remember the last location changes. Method * getLocation() always provide you with the last and the most accurate location. * * On creation you can specify if you want use only cost free location providers. * * ! Don't forget to call close() method after you done with locations ! * */ public class LocationHelper implements Closeable { private static final String LOGTAG = LocationHelper.class.getSimpleName(); private static final float LOCATION_UPDATE_DISTANCE = 500; private static final long LOCATION_UPDATE_TIME = 5000; private Map<String, LocationListenerImpl> listeners = new HashMap<String, LocationListenerImpl>(); private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); private OnLocationChangeListener onLocationChangeListener; private LocationManager locationManager; private List<String> providers; private int lastAccuracy = -1; private Location location; public LocationHelper(Context context, boolean freeSourcesOnly) { super(); this.locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); Criteria c = new Criteria(); c.setCostAllowed(!freeSourcesOnly); providers = locationManager.getProviders(c, false); Log.i(LOGTAG, "Location providers set " + providers); createListeners(); setupListeners(); for(String p : providers) { Location location = locationManager.getLastKnownLocation(p); if(location != null) { int accuracy = locationManager.getProvider(p).getAccuracy(); if(isAccuracyBetterOrSame(accuracy)) { Log.i(LOGTAG, "New location set " + location); this.location = location; } } } } public OnLocationChangeListener getOnLocationChangeListener() { return onLocationChangeListener; } public void setOnLocationChangeListener(OnLocationChangeListener onLocationChangeListener) { this.onLocationChangeListener = onLocationChangeListener; callLocationListener(); } private void callLocationListener() { if(location != null && onLocationChangeListener != null) { onLocationChangeListener.onLocationChanged(location); } } private void setupListeners() { for(LocationListenerImpl listener : listeners.values()) { locationManager.requestLocationUpdates(listener.locationProviderName, LOCATION_UPDATE_TIME, LOCATION_UPDATE_DISTANCE, listener); } } private void createListeners() { for(String provider : providers) { listeners.put(provider, new LocationListenerImpl(provider)); } } private void removeListeners() { for(LocationListenerImpl listener : listeners.values()) { locationManager.removeUpdates(listener); } } @Override public void close() { onLocationChangeListener = null; removeListeners(); } public Location getLocation() { lock.readLock().lock(); try { return this.location; } finally { lock.readLock().unlock(); } } private void setLocation(String locationProviderName, Location location) { if(location != null) { int accuracy = locationManager.getProvider(locationProviderName).getAccuracy(); if(isAccuracyBetterOrSame(accuracy)) { lock.writeLock().lock(); try { Log.i(LOGTAG, "New location set " + location); this.location = location; } finally { lock.writeLock().unlock(); } callLocationListener(); } } } private boolean isAccuracyBetterOrSame(int newAccuracy) { if(lastAccuracy == newAccuracy) { return true; } if(lastAccuracy == -1 && (newAccuracy == Criteria.ACCURACY_COARSE || newAccuracy == Criteria.ACCURACY_FINE)) { Log.i(LOGTAG, "New accuracy set:" + newAccuracy); lastAccuracy = newAccuracy; return true; } if(lastAccuracy == Criteria.ACCURACY_COARSE && newAccuracy == Criteria.ACCURACY_FINE) { Log.i(LOGTAG, "New accuracy set:" + newAccuracy); lastAccuracy = newAccuracy; return true; } return false; } private class LocationListenerImpl implements LocationListener { private String locationProviderName; public LocationListenerImpl(String locationProviderName) { super(); this.locationProviderName = locationProviderName; } @Override public void onLocationChanged(Location location) { setLocation(locationProviderName, location); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.i(LOGTAG, "Provider " + provider + " status changed to " + status); } @Override public void onProviderEnabled(String provider) { Log.i(LOGTAG, "Provider " + provider + " enabled"); } @Override public void onProviderDisabled(String provider) { Log.i(LOGTAG, "Provider " + provider + " disabled"); } } }