Automatically request location permission when accessing geolocation

Reviewed By: achen1

Differential Revision: D5180727

fbshipit-source-id: 0c67ba31e3271f4ea5167a50119c132ef238cc25
This commit is contained in:
Akshat Asawa 2017-06-05 18:58:37 -07:00 committed by Facebook Github Bot
parent dd8ed629a5
commit 9e026ec358
1 changed files with 25 additions and 6 deletions

View File

@ -20,6 +20,9 @@ const warning = require('fbjs/lib/warning');
const LocationEventEmitter = new NativeEventEmitter(RCTLocationObserver);
const Platform = require('Platform');
const PermissionsAndroid = require('PermissionsAndroid');
var subscriptions = [];
var updatesEnabled = false;
@ -87,7 +90,7 @@ var Geolocation = {
* On Android, if the location is cached this can return almost immediately,
* or it will request an update which might take a while.
*/
getCurrentPosition: function(
getCurrentPosition: async function(
geo_success: Function,
geo_error?: Function,
geo_options?: GeoOptions
@ -96,11 +99,27 @@ var Geolocation = {
typeof geo_success === 'function',
'Must provide a valid geo_success callback.'
);
RCTLocationObserver.getCurrentPosition(
geo_options || {},
geo_success,
geo_error || logError
);
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,
);
}
},
/*