Skip to content

Commit 356d098

Browse files
committed
Fixed ANR lock with location by adding a HandlerThread
* Fixed bg type as well
1 parent 7c720be commit 356d098

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import android.location.Location;
4141
import android.os.Build;
4242
import android.os.Bundle;
43+
import android.os.Handler;
44+
import android.os.HandlerThread;
4345
import android.support.annotation.NonNull;
4446

4547
import com.google.android.gms.common.ConnectionResult;
@@ -68,6 +70,8 @@ static class LocationPoint {
6870
private static GoogleApiClientCompatProxy mGoogleApiClient;
6971
static String requestPermission;
7072
private static Context classContext;
73+
74+
private static LocationHandlerThread locationHandlerThread;
7175

7276
interface LocationHandler {
7377
void complete(LocationPoint point);
@@ -167,12 +171,16 @@ static void startGetLocation() {
167171

168172
try {
169173
startFallBackThread();
174+
175+
if (locationHandlerThread == null)
176+
locationHandlerThread = new LocationHandlerThread();
170177

171178
GoogleApiClientListener googleApiClientListener = new GoogleApiClientListener();
172179
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(classContext)
173180
.addApi(LocationServices.API)
174181
.addConnectionCallbacks(googleApiClientListener)
175182
.addOnConnectionFailedListener(googleApiClientListener)
183+
.setHandler(locationHandlerThread.mHandler)
176184
.build();
177185
mGoogleApiClient = new GoogleApiClientCompatProxy(googleApiClient);
178186

@@ -308,4 +316,14 @@ static Location getLastLocation(GoogleApiClient googleApiClient) {
308316
return LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
309317
}
310318
}
319+
320+
static class LocationHandlerThread extends HandlerThread {
321+
Handler mHandler = null;
322+
323+
LocationHandlerThread() {
324+
super("OSH_LocationHandlerThread");
325+
start();
326+
mHandler = new Handler(getLooper());
327+
}
328+
}
311329
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,8 @@ private Set<String> getGroupChangeFields(UserState changedTo) {
285285
|| syncValues.optDouble("lat") != changedTo.syncValues.getDouble("lat")
286286
|| syncValues.optDouble("long") != changedTo.syncValues.getDouble("long")
287287
|| syncValues.optDouble("loc_acc") != changedTo.syncValues.getDouble("loc_acc")
288-
|| syncValues.optInt("loc_type") != changedTo.syncValues.optInt("loc_type")) {
289-
if (changedTo.dependValues.optBoolean("loc_bg"))
290-
changedTo.syncValues.put("loc_bg", changedTo.dependValues.optBoolean("loc_bg"));
288+
|| syncValues.optInt("loc_type ") != changedTo.syncValues.optInt("loc_type")) {
289+
changedTo.syncValues.put("loc_bg", changedTo.dependValues.opt("loc_bg"));
291290
return LOCATION_FIELDS_SET;
292291
}
293292
} catch (Throwable t) {}

OneSignalSDK/unittest/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,8 @@ public void testLocationSchedule() throws Exception {
14041404
Assert.assertEquals(1.1d, ShadowOneSignalRestClient.lastPost.optDouble("lat"));
14051405
Assert.assertEquals(2.2d, ShadowOneSignalRestClient.lastPost.optDouble("long"));
14061406
Assert.assertEquals(3.3f, ShadowOneSignalRestClient.lastPost.opt("loc_acc"));
1407-
Assert.assertFalse(ShadowOneSignalRestClient.lastPost.has("loc_bg"));
1407+
1408+
Assert.assertEquals(false, ShadowOneSignalRestClient.lastPost.opt("loc_bg"));
14081409
Assert.assertEquals("11111111-2222-3333-4444-555555555555", ShadowOneSignalRestClient.lastPost.opt("ad_id"));
14091410

14101411
// Testing loc_bg

0 commit comments

Comments
 (0)