Better error handling in Android Geolocation module (PR 2)

Summary: Just a testing PR for the shipit bot (please don't close :))
Closes https://github.com/facebook/react-native/pull/4355

Reviewed By: svcscm

Differential Revision: D2702784

Pulled By: mkonicek

fb-gh-sync-id: 867c65dcea486750ca65a8437b37a0f658538111
This commit is contained in:
mkonicek pr tester 2015-11-30 04:25:55 -08:00 committed by facebook-github-bot-3
parent 3dca8cf9fd
commit c9dd4015f1

View File

@ -107,6 +107,7 @@ public class LocationModule extends ReactContextBaseJavaModule {
Callback error) { Callback error) {
LocationOptions locationOptions = LocationOptions.fromReactMap(options); LocationOptions locationOptions = LocationOptions.fromReactMap(options);
try {
LocationManager locationManager = LocationManager locationManager =
(LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE); (LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
String provider = getValidProvider(locationManager, locationOptions.highAccuracy); String provider = getValidProvider(locationManager, locationOptions.highAccuracy);
@ -114,21 +115,17 @@ public class LocationModule extends ReactContextBaseJavaModule {
error.invoke("No available location provider."); error.invoke("No available location provider.");
return; return;
} }
Location location = locationManager.getLastKnownLocation(provider);
Location location = null;
try {
location = locationManager.getLastKnownLocation(provider);
} catch (SecurityException e) {
throwLocationPermissionMissing(e);
}
if (location != null && if (location != null &&
SystemClock.currentTimeMillis() - location.getTime() < locationOptions.maximumAge) { SystemClock.currentTimeMillis() - location.getTime() < locationOptions.maximumAge) {
success.invoke(locationToMap(location)); success.invoke(locationToMap(location));
return; return;
} }
new SingleUpdateRequest(locationManager, provider, locationOptions.timeout, success, error) new SingleUpdateRequest(locationManager, provider, locationOptions.timeout, success, error)
.invoke(); .invoke();
} catch (SecurityException e) {
throwLocationPermissionMissing(e);
}
} }
/** /**
@ -143,6 +140,8 @@ public class LocationModule extends ReactContextBaseJavaModule {
return; return;
} }
LocationOptions locationOptions = LocationOptions.fromReactMap(options); LocationOptions locationOptions = LocationOptions.fromReactMap(options);
try {
LocationManager locationManager = LocationManager locationManager =
(LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE); (LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
String provider = getValidProvider(locationManager, locationOptions.highAccuracy); String provider = getValidProvider(locationManager, locationOptions.highAccuracy);
@ -150,17 +149,14 @@ public class LocationModule extends ReactContextBaseJavaModule {
emitError("No location provider available."); emitError("No location provider available.");
return; return;
} }
try {
if (!provider.equals(mWatchedProvider)) { if (!provider.equals(mWatchedProvider)) {
locationManager.removeUpdates(mLocationListener); locationManager.removeUpdates(mLocationListener);
locationManager.requestLocationUpdates(provider, 1000, 0, mLocationListener); locationManager.requestLocationUpdates(provider, 1000, 0, mLocationListener);
} }
mWatchedProvider = provider;
} catch (SecurityException e) { } catch (SecurityException e) {
throwLocationPermissionMissing(e); throwLocationPermissionMissing(e);
} }
mWatchedProvider = provider;
} }
/** /**