Merge "CountryDetector no longer requires location permissions." into mnc-dev

This commit is contained in:
Makoto Onuki
2015-05-28 23:54:08 +00:00
committed by Android (Google) Code Review
2 changed files with 28 additions and 14 deletions

View File

@@ -44,8 +44,6 @@ import android.util.Log;
* You do not instantiate this class directly; instead, retrieve it through
* {@link android.content.Context#getSystemService
* Context.getSystemService(Context.COUNTRY_DETECTOR)}.
* <p>
* Both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions are needed.
*
* @hide
*/

View File

@@ -23,6 +23,7 @@ import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.util.Slog;
@@ -95,33 +96,48 @@ public class LocationBasedCountryDetector extends CountryDetectorBase {
* Register a listener with a provider name
*/
protected void registerListener(String provider, LocationListener listener) {
mLocationManager.requestLocationUpdates(provider, 0, 0, listener);
final long bid = Binder.clearCallingIdentity();
try {
mLocationManager.requestLocationUpdates(provider, 0, 0, listener);
} finally {
Binder.restoreCallingIdentity(bid);
}
}
/**
* Unregister an already registered listener
*/
protected void unregisterListener(LocationListener listener) {
mLocationManager.removeUpdates(listener);
final long bid = Binder.clearCallingIdentity();
try {
mLocationManager.removeUpdates(listener);
} finally {
Binder.restoreCallingIdentity(bid);
}
}
/**
* @return the last known location from all providers
*/
protected Location getLastKnownLocation() {
List<String> providers = mLocationManager.getAllProviders();
Location bestLocation = null;
for (String provider : providers) {
Location lastKnownLocation = mLocationManager.getLastKnownLocation(provider);
if (lastKnownLocation != null) {
if (bestLocation == null ||
bestLocation.getElapsedRealtimeNanos() <
lastKnownLocation.getElapsedRealtimeNanos()) {
bestLocation = lastKnownLocation;
final long bid = Binder.clearCallingIdentity();
try {
List<String> providers = mLocationManager.getAllProviders();
Location bestLocation = null;
for (String provider : providers) {
Location lastKnownLocation = mLocationManager.getLastKnownLocation(provider);
if (lastKnownLocation != null) {
if (bestLocation == null ||
bestLocation.getElapsedRealtimeNanos() <
lastKnownLocation.getElapsedRealtimeNanos()) {
bestLocation = lastKnownLocation;
}
}
}
return bestLocation;
} finally {
Binder.restoreCallingIdentity(bid);
}
return bestLocation;
}
/**