Skip to content

Commit d1afa34

Browse files
committed
fix(LocationManagerImplementation): Adds try/catch to avoid crashes.
1 parent fe55695 commit d1afa34

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

AndroidSDKLocation/src/main/java/com/leanplum/LocationManagerImplementation.java

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -415,28 +415,32 @@ public void onConnectionFailed(
415415

416416
@Override
417417
public void onLocationChanged(Location location) {
418-
if (!location.hasAccuracy()) {
419-
Log.e("Received a location with no accuracy.");
420-
return;
421-
}
418+
try {
419+
if (!location.hasAccuracy()) {
420+
Log.e("Received a location with no accuracy.");
421+
return;
422+
}
422423

423-
// Currently, location segment treats GPS and CELL the same. In the future, we might want more
424-
// distinction in the accuracy types. For example, a customer might want to send messages only
425-
// to the ones with very accurate location information. We are assuming that it is from GPS if
426-
// the location accuracy is less than or equal to |ACCURACY_THRESHOLD_GPS|.
427-
LeanplumLocationAccuracyType locationAccuracyType =
428-
location.getAccuracy() >= ACCURACY_THRESHOLD_GPS ?
429-
LeanplumLocationAccuracyType.CELL : LeanplumLocationAccuracyType.GPS;
430-
431-
if (!isSendingLocation && needToSendLocation(locationAccuracyType)) {
432-
try {
433-
setUserAttributesForLocationUpdate(location, locationAccuracyType);
434-
} catch (Throwable t) {
435-
Util.handleException(t);
424+
// Currently, location segment treats GPS and CELL the same. In the future, we might want more
425+
// distinction in the accuracy types. For example, a customer might want to send messages only
426+
// to the ones with very accurate location information. We are assuming that it is from GPS if
427+
// the location accuracy is less than or equal to |ACCURACY_THRESHOLD_GPS|.
428+
LeanplumLocationAccuracyType locationAccuracyType =
429+
location.getAccuracy() >= ACCURACY_THRESHOLD_GPS ?
430+
LeanplumLocationAccuracyType.CELL : LeanplumLocationAccuracyType.GPS;
431+
432+
if (!isSendingLocation && needToSendLocation(locationAccuracyType)) {
433+
try {
434+
setUserAttributesForLocationUpdate(location, locationAccuracyType);
435+
} catch (Throwable t) {
436+
Util.handleException(t);
437+
}
436438
}
437-
}
438439

439-
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
440+
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
441+
} catch (Throwable t) {
442+
Log.e("Cannot change location", t);
443+
}
440444
}
441445

442446
/**
@@ -446,17 +450,21 @@ public void onLocationChanged(Location location) {
446450
// permission to their manifest.
447451
@SuppressWarnings("MissingPermission")
448452
private void requestLocation() {
449-
if (!Leanplum.isLocationCollectionEnabled() || googleApiClient == null
450-
|| !googleApiClient.isConnected()) {
451-
return;
453+
try {
454+
if (!Leanplum.isLocationCollectionEnabled() || googleApiClient == null
455+
|| !googleApiClient.isConnected()) {
456+
return;
457+
}
458+
LocationRequest request = new LocationRequest();
459+
// Although we set the interval as |LOCATION_REQUEST_INTERVAL|, we stop listening
460+
// |onLocationChanged|. So we are essentially requesting location only once.
461+
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
462+
.setInterval(LOCATION_REQUEST_INTERVAL)
463+
.setFastestInterval(LOCATION_REQUEST_INTERVAL);
464+
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request, this);
465+
} catch (Throwable throwable) {
466+
Log.e("Cannot request location updates.", throwable);
452467
}
453-
LocationRequest request = new LocationRequest();
454-
// Although we set the interval as |LOCATION_REQUEST_INTERVAL|, we stop listening
455-
// |onLocationChanged|. So we are essentially requesting location only once.
456-
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
457-
.setInterval(LOCATION_REQUEST_INTERVAL)
458-
.setFastestInterval(LOCATION_REQUEST_INTERVAL);
459-
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, request, this);
460468
}
461469

462470
/**

0 commit comments

Comments
 (0)