Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): also use GPS for low accuracy #10728

Merged
merged 5 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ public class GeolocationModule extends KrollModule implements Handler.Callback,
public int numLocationListeners = 0;
public HashMap<String, LocationProviderProxy> simpleLocationProviders =
new HashMap<String, LocationProviderProxy>();
@Deprecated
public HashMap<String, LocationProviderProxy> legacyLocationProviders =
new HashMap<String, LocationProviderProxy>();

protected static final int MSG_ENABLE_LOCATION_PROVIDERS = KrollModule.MSG_LAST_ID + 100;
protected static final int MSG_LAST_ID = MSG_ENABLE_LOCATION_PROVIDERS;
Expand All @@ -135,17 +132,10 @@ public class GeolocationModule extends KrollModule implements Handler.Callback,
private ArrayList<LocationRuleProxy> simpleLocationRules = new ArrayList<LocationRuleProxy>();
private LocationRuleProxy simpleLocationGpsRule;
private LocationRuleProxy simpleLocationNetworkRule;
private int simpleLocationAccuracyProperty = ACCURACY_LOW;
private Location currentLocation;
//currentLocation is conditionally updated. lastLocation is unconditionally updated
//since currentLocation determines when to send out updates, and lastLocation is passive
private Location lastLocation;
@Deprecated
private HashMap<Integer, Double> legacyLocationAccuracyMap = new HashMap<Integer, Double>();
@Deprecated
private double legacyLocationFrequency = 5000;
@Deprecated
private String legacyLocationPreferredProvider = AndroidModule.PROVIDER_NETWORK;

private FusedLocationProvider fusedLocationProvider;

Expand Down Expand Up @@ -178,6 +168,8 @@ public GeolocationModule()
simpleLocationNetworkRule =
new LocationRuleProxy(AndroidModule.PROVIDER_NETWORK, SIMPLE_LOCATION_NETWORK_DISTANCE_RULE,
SIMPLE_LOCATION_NETWORK_MIN_AGE_RULE, null);
simpleLocationRules.add(simpleLocationNetworkRule);
simpleLocationRules.add(simpleLocationGpsRule);
}

/**
Expand Down Expand Up @@ -321,40 +313,21 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
@SuppressLint("MissingPermission")
private void propertyChangedAccuracy(Object newValue)
{
// is simple mode enabled (registered with OS, not just selected via the accuracy property)
boolean simpleModeEnabled = false;
if (!(getManualMode()) && (numLocationListeners > 0)) {
simpleModeEnabled = true;
}

int accuracyProperty = TiConvert.toInt(newValue);

if ((accuracyProperty == ACCURACY_HIGH) || (accuracyProperty == ACCURACY_LOW)) {
// has the value changed from the last known good value?
if (accuracyProperty != simpleLocationAccuracyProperty) {
simpleLocationAccuracyProperty = accuracyProperty;
LocationProviderProxy gpsProvider = simpleLocationProviders.get(AndroidModule.PROVIDER_GPS);

if ((accuracyProperty == ACCURACY_HIGH) && (gpsProvider == null)) {
gpsProvider = new LocationProviderProxy(AndroidModule.PROVIDER_GPS, SIMPLE_LOCATION_GPS_DISTANCE,
SIMPLE_LOCATION_GPS_TIME, this);
simpleLocationProviders.put(AndroidModule.PROVIDER_GPS, gpsProvider);
simpleLocationRules.add(simpleLocationNetworkRule);
simpleLocationRules.add(simpleLocationGpsRule);

if (simpleModeEnabled) {
registerLocationProvider(gpsProvider);
}

} else if ((accuracyProperty == ACCURACY_LOW) && (gpsProvider != null)) {
simpleLocationProviders.remove(AndroidModule.PROVIDER_GPS);
simpleLocationRules.remove(simpleLocationNetworkRule);
simpleLocationRules.remove(simpleLocationGpsRule);
double accuracyDistance = SIMPLE_LOCATION_GPS_DISTANCE;
if (accuracyProperty == ACCURACY_LOW) {
accuracyDistance = 3000.0;
}
LocationProviderProxy gpsProvider =
new LocationProviderProxy(AndroidModule.PROVIDER_GPS, accuracyDistance, SIMPLE_LOCATION_GPS_TIME, this);

if (simpleModeEnabled) {
unregisterLocationProvider(gpsProvider);
}
}
unregisterLocationProvider(simpleLocationProviders.get(AndroidModule.PROVIDER_GPS));
simpleLocationProviders.put(AndroidModule.PROVIDER_GPS, gpsProvider);

if (!getManualMode()) {
registerLocationProvider(gpsProvider);
}
}
}
Expand All @@ -367,15 +340,6 @@ private void propertyChangedAccuracy(Object newValue)
private void propertyChangedFrequency(Object newValue)
{
double frequencyProperty = TiConvert.toDouble(newValue) * 1000;
if (frequencyProperty != legacyLocationFrequency) {
legacyLocationFrequency = frequencyProperty;

Iterator<String> iterator = legacyLocationProviders.keySet().iterator();
while (iterator.hasNext()) {
LocationProviderProxy locationProvider = legacyLocationProviders.get(iterator.next());
locationProvider.setProperty(TiC.PROPERTY_MIN_UPDATE_TIME, legacyLocationFrequency);
}
}
}

/**
Expand All @@ -391,24 +355,6 @@ private void propertyChangedPreferredProvider(Object newValue)
&& (!(preferredProviderProperty.equals(AndroidModule.PROVIDER_GPS)))) {
return;
}

if (!(preferredProviderProperty.equals(legacyLocationPreferredProvider))) {
LocationProviderProxy oldProvider = legacyLocationProviders.get(legacyLocationPreferredProvider);
LocationProviderProxy newProvider = legacyLocationProviders.get(preferredProviderProperty);

if (oldProvider != null) {
legacyLocationProviders.remove(legacyLocationPreferredProvider);
}

if (newProvider == null) {
newProvider = new LocationProviderProxy(preferredProviderProperty,
legacyLocationAccuracyMap.get(simpleLocationAccuracyProperty),
legacyLocationFrequency, this);
legacyLocationProviders.put(preferredProviderProperty, newProvider);
}

legacyLocationPreferredProvider = preferredProviderProperty;
}
}

/**
Expand Down Expand Up @@ -562,17 +508,21 @@ public boolean hasLocationPermissions()
public void requestLocationPermissions(@Kroll.argument(optional = true) Object type,
@Kroll.argument(optional = true) KrollFunction permissionCallback)
{
if (hasLocationPermissions()) {
return;
}

KrollFunction permissionCB;
if (type instanceof KrollFunction && permissionCallback == null) {
permissionCB = (KrollFunction) type;
} else {
permissionCB = permissionCallback;
}

// already have permissions, fall through
if (hasLocationPermissions()) {
KrollDict response = new KrollDict();
response.putCodeAndMessage(0, null);
permissionCB.callAsync(getKrollObject(), response);
return;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! Good find.

TiBaseActivity.registerPermissionRequestCallback(TiC.PERMISSION_CODE_LOCATION, permissionCB, getKrollObject());
Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
currentActivity.requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
Expand All @@ -591,6 +541,9 @@ public void registerLocationProvider(final LocationProviderProxy locationProvide
if (!hasLocationPermissions()) {
Log.e(TAG, "Location permissions missing", Log.DEBUG_MODE);
return;
} else if (locationProvider == null) {
Log.e(TAG, "Invalid location provider", Log.DEBUG_MODE);
return;
}

if (FusedLocationProvider.hasPlayServices(context)) {
Expand All @@ -615,6 +568,9 @@ public void registerLocationProvider(final LocationProviderProxy locationProvide
@SuppressLint("MissingPermission")
public void unregisterLocationProvider(LocationProviderProxy locationProvider)
{
if (locationProvider == null) {
return;
}
if (FusedLocationProvider.hasPlayServices(context)) {
fusedLocationProvider.unregisterLocationProvider(locationProvider);
} else {
Expand Down Expand Up @@ -673,10 +629,6 @@ private void doEnableLocationProviders(HashMap<String, LocationProviderProxy> lo
@SuppressLint("MissingPermission")
private void disableLocationProviders()
{
for (LocationProviderProxy locationProvider : legacyLocationProviders.values()) {
unregisterLocationProvider(locationProvider);
}

for (LocationProviderProxy locationProvider : simpleLocationProviders.values()) {
unregisterLocationProvider(locationProvider);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
@Kroll.proxy
public class LocationProviderProxy extends KrollProxy implements LocationListener
{
public static final int STATE_DISABLED = 0;
public static final int STATE_ENABLED = 1;
public static final int STATE_ENABLED = 0;
public static final int STATE_DISABLED = 1;
public static final int STATE_OUT_OF_SERVICE = 2;
public static final int STATE_UNAVAILABLE = 3;
public static final int STATE_AVAILABLE = 4;
Expand Down