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

[TIMOB-23135] Android: Modified "locationServicesEnabled" handling #9633

Merged
merged 13 commits into from Feb 19, 2018

Conversation

maggieaxway
Copy link
Contributor

@maggieaxway maggieaxway commented Nov 28, 2017

JIRA: https://jira.appcelerator.org/browse/TIMOB-23135

Summary:

  • Was returning true for "passive" and "test" location providers when it shouldn't have.
    • Now only returns true if GPS or WiFi/Cellular location providers are enabled.
  • Was returning false when if at least 1 location value hasn't been received yet, even if location services is enabled.
    • Not the correct behavior. Especially since it might take a minute for the GPS to get a satellite lock and acquire location data.
  • Note that on Android 6.0 and above, this property will return false until location permission has been granted to the app, regardless if location services is enabled on the device.

Test:
Need manually test turning on/off the location service with the app running.

var win = Ti.UI.createWindow();

var btn = Ti.UI.createButton({
  title: 'Click Me'
});

btn.addEventListener('click', function(e) {

  console.log('Ti.Geolocation.hasLocationPermissions() ' + Ti.Geolocation.hasLocationPermissions());

  console.log('Ti.Geolocation.locationServicesEnabled ' + Ti.Geolocation.locationServicesEnabled);
  
  alert('locationServicesEnabled = ' + Ti.Geolocation.locationServicesEnabled);

  if (Ti.Geolocation.hasLocationPermissions()) {
    getCurrentPosition();

  } else {
    Ti.Geolocation.requestLocationPermissions(null, getCurrentPosition);
  }
});

function getCurrentPosition() {
  Ti.Geolocation.getCurrentPosition(function(e) {
    console.log('Ti.Geolocation.getCurrentPosition() ' + JSON.stringify(e));
  });
}

win.add(btn);
win.open();

@build build added the android label Nov 28, 2017
@build
Copy link
Contributor

build commented Nov 28, 2017

Messages
📖

🎉 Another contribution from our awesome community member, maggieaxway! Thanks again for helping us make Titanium SDK better. 👍

📖

💾 Here's the generated SDK zipfile.

Generated by 🚫 dangerJS

Copy link
Contributor

@garymathews garymathews left a comment

Choose a reason for hiding this comment

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

see above

@@ -106,7 +106,12 @@ public boolean getLocationServicesEnabled()
}
}

return providerNames.size() != 0 && getLastKnownLocation() != null;
for (String providerName : providerNames) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We want to return true if any location provider is enabled, GPS, NETWORK etc..

return providerNames.size() > 0;

Copy link
Contributor

Choose a reason for hiding this comment

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

Gary is right. We need to check all providers. His code change is the correct solution.

Also, we should check if the providerNames collection is null after calling the LocationManager.getProviders() method. If null, return false. (Odds are this isn't going to happen, but better safe than sorry. Especially on Android.)

@@ -106,7 +106,12 @@ public boolean getLocationServicesEnabled()
}
}

return providerNames.size() != 0 && getLastKnownLocation() != null;
for (String providerName : providerNames) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Gary is right. We need to check all providers. His code change is the correct solution.

Also, we should check if the providerNames collection is null after calling the LocationManager.getProviders() method. If null, return false. (Odds are this isn't going to happen, but better safe than sorry. Especially on Android.)

for (String providerName : providerNames) {
if (providerName.equals(LocationManager.GPS_PROVIDER)) {
if (providerName.equals(LocationManager.GPS_PROVIDER) || providerName.equals(LocationManager.NETWORK_PROVIDER)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Question:
Is there a reason why we're limiting this to GPS and NETWORK? Does disabling location services on a phone not disable the PASSIVE provider?

I ask because our "GeolocationModule" reads from the PASSIVE location provider.
https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/geolocation/src/java/ti/modules/titanium/geolocation/GeolocationModule.java#L200

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, so @maggieaxway and I just talked about this. The PASSIVE provider will still provide data, even if location services is disabled. In fact, that was a customer complaint here...
https://jira.appcelerator.org/browse/TIMOB-23135

So, the real question here is should we provide PASSIVE location data at all? The GPS and NETWORK providers are the ones we really want. I'm starting to think reverting the code like Maggie just did now and removing PASSIVE provider support might be the way to go.

@garymathews, you've been playing around with this recently, what are your thoughts? How is this compared to Google Play Service's fused location provider?

return providerNames.size() != 0 && getLastKnownLocation() != null;
//TIMOB-23135 Android: Ti.Geolocation.locationServicesEnabled returns false, but works
for (String providerName : providerNames) {
if (providerName.equals(LocationManager.GPS_PROVIDER) || providerName.equals(LocationManager.NETWORK_PROVIDER)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@jquick-axway sounds good, could do:

return providerNames.contains(LocationManager.GPS_PROVIDER) || providerNames.contains(LocationManager.NETWORK_PROVIDER);

@maggieaxway
Copy link
Contributor Author

So my changes approved? @garymathews @jquick-axway

Copy link
Contributor

@jquick-axway jquick-axway left a comment

Choose a reason for hiding this comment

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

CR: Pass

@build build added the docs label Jan 17, 2018
@jquick-axway
Copy link
Contributor

Updated PR to pass Java code formatting rules.

@jquick-axway jquick-axway changed the title [TIMOB-23135] Fix Android: Ti.Geolocation.locationServicesEnabled return [TIMOB-23135] Android: Modified "locationServicesEnabled" handling Feb 16, 2018
@longton95 longton95 merged commit 5ce893c into tidev:master Feb 19, 2018
@sgtcoolguy sgtcoolguy modified the milestones: 7.2.0, 7.3.0 May 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants