@@ -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,7 @@ public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
324
336
static class LocationUpdateListener implements LocationListener {
325
337
326
338
private GoogleApiClient mGoogleApiClient ;
327
-
339
+
328
340
LocationUpdateListener (GoogleApiClient googleApiClient ) {
329
341
mGoogleApiClient = googleApiClient ;
330
342
0 commit comments