Skip to content

Commit 48c94cd

Browse files
authored
Merge pull request #536 from OneSignal/location_sync
Synchronize LocationGMS
2 parents d80fb46 + 660fc1b commit 48c94cd

File tree

1 file changed

+56
-39
lines changed

1 file changed

+56
-39
lines changed

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

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ static class LocationPoint {
7575

7676
private static LocationHandlerThread locationHandlerThread;
7777

78+
protected static final Object syncLock = new Object() {};
79+
7880
enum CALLBACK_TYPE {
7981
STARTUP, PROMPT_LOCATION, SYNC_SERVICE
8082
}
@@ -178,25 +180,27 @@ static void startGetLocation() {
178180
return;
179181

180182
try {
181-
startFallBackThread();
182-
183-
if (locationHandlerThread == null)
184-
locationHandlerThread = new LocationHandlerThread();
185-
186-
if (mGoogleApiClient == null || mLastLocation == null) {
187-
GoogleApiClientListener googleApiClientListener = new GoogleApiClientListener();
188-
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(classContext)
189-
.addApi(LocationServices.API)
190-
.addConnectionCallbacks(googleApiClientListener)
191-
.addOnConnectionFailedListener(googleApiClientListener)
192-
.setHandler(locationHandlerThread.mHandler)
193-
.build();
194-
mGoogleApiClient = new GoogleApiClientCompatProxy(googleApiClient);
195-
196-
mGoogleApiClient.connect();
183+
synchronized (syncLock) {
184+
startFallBackThread();
185+
186+
if (locationHandlerThread == null)
187+
locationHandlerThread = new LocationHandlerThread();
188+
189+
if (mGoogleApiClient == null || mLastLocation == null) {
190+
GoogleApiClientListener googleApiClientListener = new GoogleApiClientListener();
191+
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(classContext)
192+
.addApi(LocationServices.API)
193+
.addConnectionCallbacks(googleApiClientListener)
194+
.addOnConnectionFailedListener(googleApiClientListener)
195+
.setHandler(locationHandlerThread.mHandler)
196+
.build();
197+
mGoogleApiClient = new GoogleApiClientCompatProxy(googleApiClient);
198+
199+
mGoogleApiClient.connect();
200+
}
201+
else if (mLastLocation != null)
202+
fireCompleteForLocation(mLastLocation);
197203
}
198-
else if (mLastLocation != null)
199-
fireCompleteForLocation(mLastLocation);
200204
} catch (Throwable t) {
201205
OneSignal.Log(OneSignal.LOG_LEVEL.WARN, "Location permission exists but there was an error initializing: ", t);
202206
fireFailedComplete();
@@ -225,9 +229,12 @@ public void run() {
225229

226230
static void fireFailedComplete() {
227231
PermissionsActivity.answered = false;
228-
if(mGoogleApiClient != null)
229-
mGoogleApiClient.disconnect();
230-
mGoogleApiClient = null;
232+
233+
synchronized (syncLock) {
234+
if(mGoogleApiClient != null)
235+
mGoogleApiClient.disconnect();
236+
mGoogleApiClient = null;
237+
}
231238

232239
fireComplete(null);
233240
}
@@ -283,30 +290,35 @@ private static void fireCompleteForLocation(Location location) {
283290
}
284291

285292
static void onFocusChange() {
286-
if (mGoogleApiClient == null || !mGoogleApiClient.realInstance().isConnected())
287-
return;
293+
synchronized (syncLock) {
294+
if (mGoogleApiClient == null || !mGoogleApiClient.realInstance().isConnected())
295+
return;
296+
297+
GoogleApiClient googleApiClient = mGoogleApiClient.realInstance();
288298

289-
GoogleApiClient googleApiClient = mGoogleApiClient.realInstance();
290-
if (locationUpdateListener != null)
291-
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, locationUpdateListener);
299+
if (locationUpdateListener != null)
300+
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, locationUpdateListener);
292301

293-
locationUpdateListener = new LocationUpdateListener(googleApiClient);
302+
locationUpdateListener = new LocationUpdateListener(googleApiClient);
303+
}
294304
}
295305

296306
static LocationUpdateListener locationUpdateListener;
297307

298308
private static class GoogleApiClientListener implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
299309
@Override
300310
public void onConnected(Bundle bundle) {
301-
PermissionsActivity.answered = false;
311+
synchronized (syncLock) {
312+
PermissionsActivity.answered = false;
302313

303-
if (mLastLocation == null) {
304-
mLastLocation = FusedLocationApiWrapper.getLastLocation(mGoogleApiClient.realInstance());
305-
if (mLastLocation != null)
306-
fireCompleteForLocation(mLastLocation);
307-
}
314+
if (mLastLocation == null) {
315+
mLastLocation = FusedLocationApiWrapper.getLastLocation(mGoogleApiClient.realInstance());
316+
if (mLastLocation != null)
317+
fireCompleteForLocation(mLastLocation);
318+
}
308319

309-
locationUpdateListener = new LocationUpdateListener(mGoogleApiClient.realInstance());
320+
locationUpdateListener = new LocationUpdateListener(mGoogleApiClient.realInstance());
321+
}
310322
}
311323

312324
@Override
@@ -324,7 +336,8 @@ public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
324336
static class LocationUpdateListener implements LocationListener {
325337

326338
private GoogleApiClient mGoogleApiClient;
327-
339+
340+
// this initializer method is already synchronized from LocationGMS with respect to the GoogleApiClient lock
328341
LocationUpdateListener(GoogleApiClient googleApiClient) {
329342
mGoogleApiClient = googleApiClient;
330343

@@ -352,17 +365,21 @@ static class FusedLocationApiWrapper {
352365
@SuppressWarnings("MissingPermission")
353366
static void requestLocationUpdates(GoogleApiClient googleApiClient, LocationRequest locationRequest, LocationListener locationListener) {
354367
try {
355-
if (googleApiClient.isConnected())
356-
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, locationListener);
368+
synchronized (LocationGMS.syncLock) {
369+
if (googleApiClient.isConnected())
370+
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, locationListener);
371+
}
357372
} catch(Throwable t) {
358373
OneSignal.Log(OneSignal.LOG_LEVEL.WARN, "FusedLocationApi.requestLocationUpdates failed!", t);
359374
}
360375
}
361376

362377
@SuppressWarnings("MissingPermission")
363378
static Location getLastLocation(GoogleApiClient googleApiClient) {
364-
if (googleApiClient.isConnected())
365-
return LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
379+
synchronized(LocationGMS.syncLock) {
380+
if (googleApiClient.isConnected())
381+
return LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
382+
}
366383
return null;
367384
}
368385
}

0 commit comments

Comments
 (0)