@@ -75,6 +75,8 @@ static class LocationPoint {
75
75
76
76
private static LocationHandlerThread locationHandlerThread ;
77
77
78
+ protected static final Object syncLock = new Object () {};
79
+
78
80
enum CALLBACK_TYPE {
79
81
STARTUP , PROMPT_LOCATION , SYNC_SERVICE
80
82
}
@@ -178,25 +180,27 @@ static void startGetLocation() {
178
180
return ;
179
181
180
182
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 );
197
203
}
198
- else if (mLastLocation != null )
199
- fireCompleteForLocation (mLastLocation );
200
204
} catch (Throwable t ) {
201
205
OneSignal .Log (OneSignal .LOG_LEVEL .WARN , "Location permission exists but there was an error initializing: " , t );
202
206
fireFailedComplete ();
@@ -225,9 +229,12 @@ public void run() {
225
229
226
230
static void fireFailedComplete () {
227
231
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
+ }
231
238
232
239
fireComplete (null );
233
240
}
@@ -283,30 +290,35 @@ private static void fireCompleteForLocation(Location location) {
283
290
}
284
291
285
292
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 ();
288
298
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 );
292
301
293
- locationUpdateListener = new LocationUpdateListener (googleApiClient );
302
+ locationUpdateListener = new LocationUpdateListener (googleApiClient );
303
+ }
294
304
}
295
305
296
306
static LocationUpdateListener locationUpdateListener ;
297
307
298
308
private static class GoogleApiClientListener implements GoogleApiClient .ConnectionCallbacks , GoogleApiClient .OnConnectionFailedListener {
299
309
@ Override
300
310
public void onConnected (Bundle bundle ) {
301
- PermissionsActivity .answered = false ;
311
+ synchronized (syncLock ) {
312
+ PermissionsActivity .answered = false ;
302
313
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
+ }
308
319
309
- locationUpdateListener = new LocationUpdateListener (mGoogleApiClient .realInstance ());
320
+ locationUpdateListener = new LocationUpdateListener (mGoogleApiClient .realInstance ());
321
+ }
310
322
}
311
323
312
324
@ Override
@@ -324,7 +336,8 @@ public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
324
336
static class LocationUpdateListener implements LocationListener {
325
337
326
338
private GoogleApiClient mGoogleApiClient ;
327
-
339
+
340
+ // this initializer method is already synchronized from LocationGMS with respect to the GoogleApiClient lock
328
341
LocationUpdateListener (GoogleApiClient googleApiClient ) {
329
342
mGoogleApiClient = googleApiClient ;
330
343
@@ -352,17 +365,21 @@ static class FusedLocationApiWrapper {
352
365
@ SuppressWarnings ("MissingPermission" )
353
366
static void requestLocationUpdates (GoogleApiClient googleApiClient , LocationRequest locationRequest , LocationListener locationListener ) {
354
367
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
+ }
357
372
} catch (Throwable t ) {
358
373
OneSignal .Log (OneSignal .LOG_LEVEL .WARN , "FusedLocationApi.requestLocationUpdates failed!" , t );
359
374
}
360
375
}
361
376
362
377
@ SuppressWarnings ("MissingPermission" )
363
378
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
+ }
366
383
return null ;
367
384
}
368
385
}
0 commit comments