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: Ensure permission denied in geolocation.getCurrentPosition rejects the Promise #22843

Closed
wants to merge 1 commit into from

Conversation

Jyrno42
Copy link
Contributor

@Jyrno42 Jyrno42 commented Jan 2, 2019

fixes #22535

Moves the permission request logic on Android M and above to Native side to mimic IOS behavior.

Changelog:

[Android] [Fixed] - Ensure permission denied in geolocation.getCurrentPosition rejects the promise

Test Plan:

Verified manually with a test project which requests location via Geolocation service. If the permission request is denied by the user the promise is rejected as well.

https://github.com/Jyrno42/rn-geoloctest

@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 Jan 2, 2019
@Jyrno42 Jyrno42 force-pushed the bug-22535 branch 2 times, most recently from 54cb006 to d1f02cd Compare January 2, 2019 12:24
@Jyrno42
Copy link
Contributor Author

Jyrno42 commented Jan 2, 2019

The IOS test failure is not releated to this PR.

@dulmandakh dulmandakh added the Platform: Android Android applications. label Jan 13, 2019
error.invoke(PositionError.buildError(PositionError.PERMISSION_DENIED, "Location permission was not granted."));
}
}
}, new Callback() {
Copy link
Contributor

Choose a reason for hiding this comment

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

can you extract these inline Callback ctors to named variables so the code is a bit easier to follow?

Copy link
Contributor Author

@Jyrno42 Jyrno42 Jan 25, 2019

Choose a reason for hiding this comment

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

Done. I will squash the commits once they have been approved.

@hramos hramos removed the 🔶APIs label Jan 24, 2019
@Jyrno42 Jyrno42 force-pushed the bug-22535 branch 2 times, most recently from 9edc5d7 to bf64df4 Compare January 25, 2019 11:24
@Jyrno42
Copy link
Contributor Author

Jyrno42 commented Jan 25, 2019

I also updated the test project to use the latest commit.

https://github.com/Jyrno42/rn-geoloctest

…ts the Promise

fixes facebook#22535

Moves the permission request logic on Android M and above to Native side to mimic IOS behavior.

Changelog:
----------

[Android] [Fixed] - Ensure permission denied in geolocation.getCurrentPosition rejects the promise

Test Plan:
----------

Verified via my test project which requests location via Geolocation service. If the permission request
is denied by the user the promise is rejected as well.

https://github.com/Jyrno42/rn-geoloctest
@pull-bot
Copy link

pull-bot commented Feb 8, 2019

Warnings
⚠️

📋 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

@Jyrno42
Copy link
Contributor Author

Jyrno42 commented Feb 8, 2019

Rebased and squashed

@Jyrno42
Copy link
Contributor Author

Jyrno42 commented Feb 16, 2019

@hramos @matthargett Is there anything else I need to do to get this merged?

@cpojer
Copy link
Contributor

cpojer commented Mar 18, 2019

Unfortunately this PR would currently break code at FB. Could you instead create a new native method with this functionality, and keep the old native method around unchanged and then change your JS code to something like:

if (RCTLocationObserver.newMethod) {
  // handle the new method
} else {
  // Old code
}

Then I can land this code, and we can remove the old code soon.

@Jyrno42
Copy link
Contributor Author

Jyrno42 commented Mar 28, 2019

@cpojer

I can, however would like some clarification. So I keep LocationModule.getCurrentPosition as is and move the new logic to something like LocationModule.getCurrentPositionAndRequestPermissions.

and on the JS side getCurrentPosition becomes this:

getCurrentPosition: async function(
    geo_success: Function,
    geo_error?: Function,
    geo_options?: GeoOptions,
) {
    invariant(
        typeof geo_success === 'function',
        'Must provide a valid geo_success callback.',
    );

    if (LocationModule.getCurrentPositionAndRequestPermissions) {
        // Permission checks/requests are done on the native side
        RCTLocationObserver.getCurrentPosition(
            geo_options || {},
            geo_success,
            geo_error || logError,
        );
    } else {
        let hasPermission = true;
        // Supports Android's new permission model. For Android older devices,
        // it's always on.
        if (Platform.OS === 'android' && Platform.Version >= 23) {
            hasPermission = await PermissionsAndroid.check(
                PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
            );
            if (!hasPermission) {
                const status = await PermissionsAndroid.request(
                PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
                );
                hasPermission = status === PermissionsAndroid.RESULTS.GRANTED;
            }
        }
        if (hasPermission) {
            RCTLocationObserver.getCurrentPosition(
                geo_options || {},
                geo_success,
                geo_error || logError,
            );
        }
    }
}

Do I understand you correctly?

Also, I am wondering do you just strip the new native method during build in FB, or do you depend directly on the old native method not it's js proxy?

@cpojer
Copy link
Contributor

cpojer commented Mar 28, 2019

Exactly, that should work for now :) We can then remove the old code in a couple of weeks.

@cpojer
Copy link
Contributor

cpojer commented Mar 29, 2019

Hey @Jyrno42,

because of App rejections around Geolocation usage we decided to extract Geolocation into https://github.com/react-native-community/react-native-geolocation. You can ship your pull request in that repo, in the original form without the changes that I required as soon as the code appears there. This should be less work for you and allow you to move faster :)

@cpojer cpojer closed this Mar 29, 2019
@Jyrno42
Copy link
Contributor Author

Jyrno42 commented Mar 29, 2019

I was just going to push it but ok :)

Jyrno42 added a commit to Jyrno42/react-native-geolocation that referenced this pull request Mar 30, 2019
…ts the Promise

fixes facebook/react-native#22535

Moves the permission request logic on Android M and above to Native side to mimic IOS behavior.

Changelog:
----------

[Android] [Fixed] - Ensure permission denied in geolocation.getCurrentPosition rejects the promise

Test Plan:
----------

Was originally reviewed and accepted as facebook/react-native#22843 as part
of core react-native. That PR was verified via my test project which requested location via
Geolocation service. If the permission request was denied by the user the promise was rejected as well.

https://github.com/Jyrno42/rn-geoloctest
@Jyrno42
Copy link
Contributor Author

Jyrno42 commented Mar 30, 2019

Filed as michalchudziak/react-native-geolocation#1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: Geolocation Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Platform: Android Android applications.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle permission denied in geolocation service
8 participants