Skip to content

Commit

Permalink
Experimental code for facebook#22535
Browse files Browse the repository at this point in the history
  • Loading branch information
Jyrno42 committed Dec 26, 2018
1 parent 193615a commit 4118e10
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
27 changes: 6 additions & 21 deletions Libraries/Geolocation/Geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,12 @@ const Geolocation = {
typeof geo_success === 'function',
'Must provide a valid geo_success callback.',
);
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,
);
}
// Permission checks/requests are done on the native side
RCTLocationObserver.getCurrentPosition(
geo_options || {},
geo_success,
geo_error || logError,
);
},

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.facebook.react.modules.location;

import android.Manifest;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
Expand All @@ -26,6 +27,7 @@
import com.facebook.react.common.SystemClock;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
import com.facebook.react.modules.permissions.PermissionsModule;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -115,6 +117,49 @@ public void getCurrentPosition(
ReadableMap options,
final Callback success,
Callback error) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final PermissionsModule perms = getReactApplicationContext().getNativeModule(PermissionsModule.class);
perms.checkPermission(Manifest.permission.ACCESS_FINE_LOCATION, new PromiseImpl(new Callback() {
@Override
public void invoke(Object... args) {
boolean hasPermission = args[0];

if (!hasPermission) {
perms.requestPermission(Manifest.permission.ACCESS_FINE_LOCATION, new PromiseImpl(new Callback() {
@Override
public void invoke(Object... args) {
if (args[0] === "granted") {
loadCurrentPosition(options, success, error);
} else {
error(PositionError.buildError(PositionError.PERMISSION_DENIED, "Location permission was not granted."));
}
}
}, new Callback() {
@Override
public void invoke(Object... args) {
error(args[0]);
}
}));
} else {
loadCurrentPosition(options, success, error);
}
}
}, new Callback() {
@Override
public void invoke(Object... args) {
error(args[0]);
}
});
return;
}

loadCurrentPosition(options, success, error);
}

private void loadCurrentPosition(
ReadableMap options,
final Callback success,
Callback error) {
LocationOptions locationOptions = LocationOptions.fromReactMap(options);

try {
Expand Down

0 comments on commit 4118e10

Please sign in to comment.