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 two bugs with Location when not using ACCESS_FINE_LOCATION #10291

Closed
wants to merge 1 commit into from

Conversation

mikelambert
Copy link
Contributor

Fix two bugs with Location when not using ACCESS_FINE_LOCATION:

  • If we request highAccuracy=false, because we don't have ACCESS_FINE_LOCATION, but we don't have access to the NETWORK_PROVIDER...then the code should not trigger a SecurityException because it fell-back to using GPS_PROVIDER (which we don't have permission for)
  • If the device is pre-lollipop, and doesn't have a provider, we should detect this properly instead of letting the pre-lollipop code raise a SecurityException.

Unfortunately, SecurityExceptions cannot be caught by the calling JS code. But I am not fixing that one here, instead choosing to fix/obviate the SecurityExceptions altogether.

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @AaaChiuuu and @leeight to be potential reviewers.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Oct 7, 2016
@leeight
Copy link
Contributor

leeight commented Oct 8, 2016

@satya164 If this PR was merged, can we change the default targetSdkVersion to 23 or not?

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 8, 2016
@mikelambert
Copy link
Contributor Author

AFAIK targetSdkVersion=23 requires that apps use an dynamic approach to requesting permissions (a la iOS) instead of the old list-it-all-in-AndroidManifest.xml.

Changing the default targetSdkVersion would force that on everyone, so would be a very major change on downstream users.

Maybe I'm missing context, or maybe that's exactly the desired plan? But I'm not sure how that relates to my change here.

@facebook-github-bot
Copy link
Contributor

@mikelambert updated the pull request - view changes

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 18, 2016
@facebook-github-bot
Copy link
Contributor

@hramos has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@hramos
Copy link
Contributor

hramos commented Nov 11, 2016

This failed to pass some internal tests. Running them again, which is going to take a while...

// If it's an enabled provider, but we don't have permissions, ignore it
int finePermission = ContextCompat.checkSelfPermission(getReactApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION);
if (provider.equals(LocationManager.GPS_PROVIDER) && finePermission != PackageManager.PERMISSION_GRANTED) {
return null;
Copy link
Contributor

@satya164 satya164 Nov 13, 2016

Choose a reason for hiding this comment

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

Should we call onError callback instead if ignoring it?

Copy link
Contributor Author

@mikelambert mikelambert Nov 13, 2016

Choose a reason for hiding this comment

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

The existing error cases above return null, so I was just following that example. I think that's all getValidProvider can do.

It's called from two places.

  • When the getCurrentPosition call receives null, it will call error.invoke("No available location provider.");
  • When the startObserving call receives null, it will emit geolocationError and call watchPosition()'s error function.

@satya164
Copy link
Contributor

@leeight what @mikelambert said.

@leeight
Copy link
Contributor

leeight commented Nov 14, 2016

Make sense. It's not good time to upgrade to 23. So this PR LGTM.

@hramos
Copy link
Contributor

hramos commented Nov 14, 2016

@facebook-github-bot shipit

@facebook-github-bot facebook-github-bot added GH Review: accepted Import Started This pull request has been imported. This does not imply the PR has been approved. and removed GH Review: review-needed Import Started This pull request has been imported. This does not imply the PR has been approved. labels Nov 14, 2016
@facebook-github-bot
Copy link
Contributor

I tried to merge this pull request into the Facebook internal repo but some checks failed. To unblock yourself please check the following: Does this pull request pass all open source tests on GitHub? If not please fix those. Does the code still apply cleanly on top of GitHub master? If not can please rebase. In all other cases this means some internal test failed, for example a part of a fb app won't work with this pull request. I've added the Import Failed label to this pull request so it is easy for someone at fb to find the pull request and check what failed. If you don't see anyone comment in a few days feel free to comment mentioning one of the core contributors to the project so they get a notification.

@mkonicek
Copy link
Contributor

return null;
}
}
// If it's an enabled provider, but we don't have permissions, ignore it
int finePermission = ContextCompat.checkSelfPermission(getReactApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: hasPermission

Copy link
Contributor

Choose a reason for hiding this comment

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

Why check ACCESS_FINE_LOCATION here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure exactly what you're asking, but maybe this answers it?

My app was released prior to RN, and I used COARSE but not FINE permissions. I am not ready to block my app upgrades on "new FINE permission" that RN sometimes triggers accidentally, so wanted to avoid that.

This check is to say "If they are trying to use the GPS, but they don't have a FINE permission enabled, then pretend like we don't have a GPS so we don't trigger the permissions errors that crash the app" (These are not returned through onError, but propagate as a native Java exception).

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you mean by:

used COARSE but not FINE permissions

Does this mean declaring them in the manifest?

"new FINE permission" that RN sometimes triggers accidentally

Why does RN accidentally need a new permission? That seems like a bug.

If they are trying to use the GPS, but they don't have a FINE permission enabled, then pretend like we don't have a GPS

However, if we don't have the fine permission, we may still use Geolocation, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To reply to your comment directly (as opposed to the main thread, where it appears to have been lost):

Default RN apps request FINE permissions, and so RN apps declaring FINE do not have any issues. My app only declares COARSE, and so wanted to fix RN for my use case.

I am calling navigator.geolocation.getCurrentPosition(..., {enableHighAccuracy: false}) to avoid using the GPS and just rely on the NETWORK provider.

To your question: yes, by and large the Geolocation library works just fine. Unfortunately, if there is a problem with the NETWORK provider not being enabled, then the RN code attempts to fall back to using the GPS provider.

And if this GPS provider is enabled, my app will crash with an unhandled Java exception, because it only declares COARSE permission. I am merely checking for FINE permission before using the GPS provider, to avoid this crash.

@mikelambert
Copy link
Contributor Author

And @mkonicek, just to clarify in what you want in a test plan, you're looking for an explanation of what I've specifically tested to verify that this change works? (The tests Travis/CI tests are failing, but that seems independent of my change, so I hope you're not talking about that.)

@lacker
Copy link
Contributor

lacker commented Dec 14, 2016

Better than a test plan would just be a unit test that covers this. It seems like you should be able to put one in the Android unit tests.

@lacker
Copy link
Contributor

lacker commented Feb 8, 2017

Any thoughts on adding a test?

@mkonicek
Copy link
Contributor

Sorry I'm so slow at reviewing but I realized I still don't fully understand why the fine location check is needed. I added some questions explaining (hopefully clearly?) why I don't understand it.

@react-native-bot react-native-bot added Missing Changelog This PR appears to be missing a changelog, or they are incorrectly formatted. Ran Commands One of our bots successfully processed a command. labels Mar 16, 2018
@react-native-bot react-native-bot added Missing Test Plan This PR appears to be missing a test plan. and removed ❔Needs More Information labels May 15, 2018
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@hramos has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@cpojer
Copy link
Contributor

cpojer commented Feb 21, 2019

Hello everyone! @mikelambert, thank you for sending this pull requests almost 2.5 years ago (WHAT!?). I'm deeply sorry we never got around to merging it, even though you provided tests.

I wanna make sure we implement your change, finally, so I took the liberty to rebase your PR but without the changes for Android Lollipop. The reasoning behind this is that so much time has passed that merging this now would just increase technical debt.

I did however include the other fix, as well as the test you wrote. Now, let's see if I can get this landed :)

@pull-bot
Copy link

pull-bot commented Feb 21, 2019

Messages
📖 📋 Missing Summary - Can you add a Summary? To do so, add a "## Summary" section to your PR description. This is a good place to explain the motivation for making this change.
📖 📋 Missing Test Plan - Can you add a Test Plan? To do so, add a "## Test Plan" section to your PR description. A Test Plan lets us know how these changes were tested.
📖

📋 Missing Changelog - Can you add a Changelog? To do so, add a "## Changelog" section to your PR description. A changelog entry has the following format: [CATEGORY] [TYPE] - Message.

Generated by 🚫 dangerJS against 2e0e2e3

cpojer added a commit to mikelambert/react-native that referenced this pull request Feb 21, 2019
cpojer pushed a commit to mikelambert/react-native that referenced this pull request Feb 21, 2019
@facebook-github-bot facebook-github-bot added the Import Started This pull request has been imported. This does not imply the PR has been approved. label Feb 21, 2019
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@cpojer is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@cpojer is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

@mikelambert merged commit f32dc63 into facebook:master.

@facebook facebook locked as resolved and limited conversation to collaborators Feb 21, 2019
@react-native-bot react-native-bot added the Merged This PR has been merged. label Feb 21, 2019
@cpojer
Copy link
Contributor

cpojer commented Feb 21, 2019

Note I removed the test from this PR to land the change. I couldn’t get our internal build system to work with it and it couldn’t locate the DummyLocationProvider. I also couldn’t find any documentation on how to make it work. If somebody knows how to do it, I’m happy to commit the test.

@hramos hramos removed Import Failed Import Started This pull request has been imported. This does not imply the PR has been approved. labels Feb 23, 2019
grabbou pushed a commit that referenced this pull request Feb 27, 2019
Summary:
Fix two bugs with Location when not using ACCESS_FINE_LOCATION:
- If we request highAccuracy=false, because we don't have ACCESS_FINE_LOCATION, but we don't have access to the NETWORK_PROVIDER...then the code should not trigger a SecurityException because it fell-back to using GPS_PROVIDER (which we don't have permission for)
- If the device is pre-lollipop, and doesn't have a provider, we should detect this properly instead of letting the pre-lollipop code raise a SecurityException.

Unfortunately, SecurityExceptions cannot be caught by the calling JS code. But I am not fixing that one here, instead choosing to fix/obviate the SecurityExceptions altogether.
Pull Request resolved: #10291

Differential Revision: D4163659

Pulled By: cpojer

fbshipit-source-id: 18bb4ee7401bc4eac4fcc97341fc2b3a2a0957c9
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Missing Changelog This PR appears to be missing a changelog, or they are incorrectly formatted. Missing Test Plan This PR appears to be missing a test plan. Ran Commands One of our bots successfully processed a command.
Projects
None yet
Development

Successfully merging this pull request may close these issues.