Skip to content

Commit 660fc1b

Browse files
committed
Synchronize LocationUpdateListener
• Adds synchronization to LocationUpdateListener, a class that also access the same instance of GoogleApiClient (from the previous commit)
1 parent f487e71 commit 660fc1b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/LocationGMS.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ static class LocationUpdateListener implements LocationListener {
337337

338338
private GoogleApiClient mGoogleApiClient;
339339

340+
// this initializer method is already synchronized from LocationGMS with respect to the GoogleApiClient lock
340341
LocationUpdateListener(GoogleApiClient googleApiClient) {
341342
mGoogleApiClient = googleApiClient;
342343

@@ -364,17 +365,21 @@ static class FusedLocationApiWrapper {
364365
@SuppressWarnings("MissingPermission")
365366
static void requestLocationUpdates(GoogleApiClient googleApiClient, LocationRequest locationRequest, LocationListener locationListener) {
366367
try {
367-
if (googleApiClient.isConnected())
368-
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, locationListener);
368+
synchronized (LocationGMS.syncLock) {
369+
if (googleApiClient.isConnected())
370+
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, locationListener);
371+
}
369372
} catch(Throwable t) {
370373
OneSignal.Log(OneSignal.LOG_LEVEL.WARN, "FusedLocationApi.requestLocationUpdates failed!", t);
371374
}
372375
}
373376

374377
@SuppressWarnings("MissingPermission")
375378
static Location getLastLocation(GoogleApiClient googleApiClient) {
376-
if (googleApiClient.isConnected())
377-
return LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
379+
synchronized(LocationGMS.syncLock) {
380+
if (googleApiClient.isConnected())
381+
return LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
382+
}
378383
return null;
379384
}
380385
}

0 commit comments

Comments
 (0)