This source file includes following definitions.
- start
- stop
- isRunning
- setLocationProviderImpl
- get
- start
- stop
- isRunning
- onLocationChanged
- updateNewLocation
- onStatusChanged
- onProviderEnabled
- onProviderDisabled
- ensureLocationManagerCreated
- registerForLocationUpdates
- unregisterFromLocationUpdates
- usePassiveOneShotLocation
- isOnlyPassiveLocationProviderEnabled
package org.chromium.content.browser;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import com.google.common.annotations.VisibleForTesting;
import org.chromium.base.ThreadUtils;
import java.util.List;
public class LocationProviderFactory {
private static LocationProviderFactory.LocationProvider sProviderImpl;
public interface LocationProvider {
public void start(boolean gpsEnabled);
public void stop();
public boolean isRunning();
}
private LocationProviderFactory() {
}
@VisibleForTesting
public static void setLocationProviderImpl(LocationProviderFactory.LocationProvider provider) {
assert sProviderImpl == null;
sProviderImpl = provider;
}
public static LocationProvider get(Context context) {
if (sProviderImpl == null) {
sProviderImpl = new LocationProviderImpl(context);
}
return sProviderImpl;
}
private static class LocationProviderImpl
implements LocationListener, LocationProviderFactory.LocationProvider {
private static final String TAG = "LocationProvider";
private Context mContext;
private LocationManager mLocationManager;
private boolean mIsRunning;
LocationProviderImpl(Context context) {
mContext = context;
}
@Override
public void start(boolean gpsEnabled) {
unregisterFromLocationUpdates();
registerForLocationUpdates(gpsEnabled);
}
@Override
public void stop() {
unregisterFromLocationUpdates();
}
@Override
public boolean isRunning() {
return mIsRunning;
}
@Override
public void onLocationChanged(Location location) {
if (mIsRunning) {
updateNewLocation(location);
}
}
private void updateNewLocation(Location location) {
LocationProviderAdapter.newLocationAvailable(
location.getLatitude(), location.getLongitude(),
location.getTime() / 1000.0,
location.hasAltitude(), location.getAltitude(),
location.hasAccuracy(), location.getAccuracy(),
location.hasBearing(), location.getBearing(),
location.hasSpeed(), location.getSpeed());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
private void ensureLocationManagerCreated() {
if (mLocationManager != null) return;
mLocationManager = (LocationManager) mContext.getSystemService(
Context.LOCATION_SERVICE);
if (mLocationManager == null) {
Log.e(TAG, "Could not get location manager.");
}
}
private void registerForLocationUpdates(boolean isGpsEnabled) {
ensureLocationManagerCreated();
if (usePassiveOneShotLocation()) return;
assert !mIsRunning;
mIsRunning = true;
try {
Criteria criteria = new Criteria();
mLocationManager.requestLocationUpdates(0, 0, criteria, this,
ThreadUtils.getUiThreadLooper());
if (isGpsEnabled) {
criteria.setAccuracy(Criteria.ACCURACY_FINE);
mLocationManager.requestLocationUpdates(0, 0, criteria, this,
ThreadUtils.getUiThreadLooper());
}
} catch (SecurityException e) {
Log.e(TAG, "Caught security exception registering for location updates from " +
"system. This should only happen in DumpRenderTree.");
} catch (IllegalArgumentException e) {
Log.e(TAG, "Caught IllegalArgumentException registering for location updates.");
}
}
private void unregisterFromLocationUpdates() {
if (mIsRunning) {
mIsRunning = false;
mLocationManager.removeUpdates(this);
}
}
private boolean usePassiveOneShotLocation() {
if (!isOnlyPassiveLocationProviderEnabled()) return false;
final Location location = mLocationManager.getLastKnownLocation(
LocationManager.PASSIVE_PROVIDER);
if (location != null) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
updateNewLocation(location);
}
});
}
return true;
}
private boolean isOnlyPassiveLocationProviderEnabled() {
List<String> providers = mLocationManager.getProviders(true);
return providers != null && providers.size() == 1
&& providers.get(0).equals(LocationManager.PASSIVE_PROVIDER);
}
}
}