Skip to content

Commit

Permalink
fix(android)(8_3_X): getCurrentPosition() compatibility with some Sam…
Browse files Browse the repository at this point in the history
…sung devices (#11426)

* fix(android): getCurrentPosition() compatibility with Samsung devices

* fix(android): only register location provider when necessary

* fix(android): return error when provider is unavailable

* fix(android): clone callback array

Co-authored-by: Christopher Williams <[email protected]>
  • Loading branch information
2 people authored and lokeshchdhry committed Jan 15, 2020
1 parent 981869a commit 1e832a6
Showing 1 changed file with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public class GeolocationModule extends KrollModule implements Handler.Callback,
private static final double SIMPLE_LOCATION_PASSIVE_DISTANCE = 0.0;
private static final double SIMPLE_LOCATION_PASSIVE_TIME = 0;
private static final double SIMPLE_LOCATION_NETWORK_DISTANCE = 10.0;
private static final double SIMPLE_LOCATION_NETWORK_TIME = 10000;
private static final double SIMPLE_LOCATION_NETWORK_TIME = 3000;
private static final double SIMPLE_LOCATION_GPS_DISTANCE = 3.0;
private static final double SIMPLE_LOCATION_GPS_TIME = 3000;
private static final double SIMPLE_LOCATION_NETWORK_DISTANCE_RULE = 200;
Expand All @@ -136,6 +136,7 @@ public class GeolocationModule extends KrollModule implements Handler.Callback,
//currentLocation is conditionally updated. lastLocation is unconditionally updated
//since currentLocation determines when to send out updates, and lastLocation is passive
private Location lastLocation;
private ArrayList<KrollFunction> currentPositionCallback = new ArrayList<>();

private FusedLocationProvider fusedLocationProvider;

Expand Down Expand Up @@ -200,6 +201,20 @@ public boolean handleMessage(Message message)
public void onLocationChanged(Location location)
{
lastLocation = location;

// Execute current position callbacks.
if (currentPositionCallback.size() > 0) {
ArrayList<KrollFunction> currentPositionCallbackClone =
(ArrayList<KrollFunction>) currentPositionCallback.clone();
currentPositionCallback.clear();
for (KrollFunction callback : currentPositionCallbackClone) {
callback.call(this.getKrollObject(),
new Object[] { buildLocationEvent(
lastLocation, tiLocation.locationManager.getProvider(lastLocation.getProvider())) });
}
}

// Fire 'location' event listeners.
if (shouldUseUpdate(location)) {
fireEvent(TiC.EVENT_LOCATION,
buildLocationEvent(location, tiLocation.locationManager.getProvider(location.getProvider())));
Expand All @@ -225,50 +240,42 @@ public void onProviderStateChanged(String providerName, int state)
switch (state) {
case LocationProviderProxy.STATE_DISABLED:
message += " is disabled";
Log.i(TAG, message, Log.DEBUG_MODE);
fireEvent(TiC.EVENT_LOCATION, buildLocationErrorEvent(state, message));

break;

case LocationProviderProxy.STATE_ENABLED:
message += " is enabled";
Log.d(TAG, message, Log.DEBUG_MODE);

break;

case LocationProviderProxy.STATE_OUT_OF_SERVICE:
message += " is out of service";
Log.d(TAG, message, Log.DEBUG_MODE);
fireEvent(TiC.EVENT_LOCATION, buildLocationErrorEvent(state, message));

break;

case LocationProviderProxy.STATE_UNAVAILABLE:
message += " is unavailable";
Log.d(TAG, message, Log.DEBUG_MODE);
fireEvent(TiC.EVENT_LOCATION, buildLocationErrorEvent(state, message));

break;

case LocationProviderProxy.STATE_AVAILABLE:
message += " is available";
Log.d(TAG, message, Log.DEBUG_MODE);

break;

case LocationProviderProxy.STATE_UNKNOWN:
message += " is in a unknown state [" + state + "]";
Log.d(TAG, message, Log.DEBUG_MODE);
fireEvent(TiC.EVENT_LOCATION, buildLocationErrorEvent(state, message));

break;

default:
message += " is in a unknown state [" + state + "]";
Log.d(TAG, message, Log.DEBUG_MODE);
fireEvent(TiC.EVENT_LOCATION, buildLocationErrorEvent(state, message));

break;
}
Log.d(TAG, message, Log.DEBUG_MODE);

if (state != LocationProviderProxy.STATE_ENABLED && state != LocationProviderProxy.STATE_AVAILABLE) {
fireEvent(TiC.EVENT_LOCATION, buildLocationErrorEvent(state, message));

// Execute current position callbacks.
if (currentPositionCallback.size() > 0) {
ArrayList<KrollFunction> currentPositionCallbackClone =
(ArrayList<KrollFunction>) currentPositionCallback.clone();
currentPositionCallback.clear();
for (KrollFunction callback : currentPositionCallbackClone) {
callback.call(this.getKrollObject(), new Object[] { buildLocationErrorEvent(state, message) });
}
}
}
}

Expand Down Expand Up @@ -670,6 +677,20 @@ public void getCurrentPosition(KrollFunction callback)
}
if (callback != null) {
Location latestKnownLocation = tiLocation.getLastKnownLocation();
if (latestKnownLocation == null) {
latestKnownLocation = lastLocation;
}

// TIMOB-27572: Samsung devices require a location provider to be registered
// in order to obtain last known location.
if (latestKnownLocation == null) {
if (numLocationListeners == 0) {
numLocationListeners++;
enableLocationProviders(simpleLocationProviders);
}
currentPositionCallback.add(callback);
return;
}

if (latestKnownLocation != null) {
callback.call(
Expand Down

0 comments on commit 1e832a6

Please sign in to comment.