Skip to content

Commit

Permalink
Replace android files with ones with proper history.
Browse files Browse the repository at this point in the history
  • Loading branch information
michalchudziak committed Apr 1, 2019
1 parent 33eea7b commit fab6410
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.modules.location;
package com.reactnativecommunity.geolocation;

import android.annotation.SuppressLint;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
Expand All @@ -17,10 +18,11 @@
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import androidx.core.content.ContextCompat;
import android.support.v4.content.ContextCompat;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.PromiseImpl;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
Expand All @@ -30,16 +32,17 @@
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;

/**
* Native module that exposes Geolocation to JS.
*/
@SuppressLint("MissingPermission")
@ReactModule(name = LocationModule.NAME)
public class LocationModule extends ReactContextBaseJavaModule {
@ReactModule(name = GeolocationModule.NAME)
public class GeolocationModule extends ReactContextBaseJavaModule {

public static final String NAME = "LocationObserver";
public static final String NAME = "RNCGeolocation";
private @Nullable String mWatchedProvider;
private static final float RCT_DEFAULT_LOCATION_ACCURACY = 100;

Expand All @@ -66,7 +69,7 @@ public void onProviderEnabled(String provider) { }
public void onProviderDisabled(String provider) { }
};

public LocationModule(ReactApplicationContext reactContext) {
public GeolocationModule(ReactApplicationContext reactContext) {
super(reactContext);
}

Expand Down Expand Up @@ -110,13 +113,74 @@ private static LocationOptions fromReactMap(ReadableMap map) {

/**
* Get the current position. This can return almost immediately if the location is cached or
* request an update, which might take a while.
* request an update, which might take a while. This method also requests location
* permissions on API level 23 and above when needed.
*
* @param options map containing optional arguments: timeout (millis), maximumAge (millis) and
* highAccuracy (boolean)
*/
@ReactMethod
public void getCurrentPosition(
final ReadableMap options,
final Callback success,
final Callback error) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final PermissionsModule perms = getReactApplicationContext().getNativeModule(PermissionsModule.class);

final Callback onPermissionGranted = new Callback() {
@Override
public void invoke(Object... args) {
String result = (String) args[0];
if (result == "granted") {
getCurrentLocationData(options, success, error);
} else {
error.invoke(PositionError.buildError(PositionError.PERMISSION_DENIED, "Location permission was not granted."));
}
}
};

final Callback onPermissionDenied = new Callback() {
@Override
public void invoke(Object... args) {
error.invoke(PositionError.buildError(PositionError.PERMISSION_DENIED, "Failed to request location permission."));
}
};

Callback onPermissionCheckFailed = new Callback() {
@Override
public void invoke(Object... args) {
error.invoke(PositionError.buildError(PositionError.PERMISSION_DENIED, "Failed to check location permission."));
}
};

Callback onPermissionChecked = new Callback() {
@Override
public void invoke(Object... args) {
boolean hasPermission = (boolean) args[0];

if (!hasPermission) {
perms.requestPermission(Manifest.permission.ACCESS_FINE_LOCATION, new PromiseImpl(onPermissionGranted, onPermissionDenied));
} else {
getCurrentLocationData(options, success, error);
}
}
};

perms.checkPermission(Manifest.permission.ACCESS_FINE_LOCATION, new PromiseImpl(onPermissionChecked, onPermissionCheckFailed));
return;
}

getCurrentLocationData(options, success, error);
}

/**
* Get the current position. This can return almost immediately if the location is cached or
* request an update, which might take a while.
*
* @param options map containing optional arguments: timeout (millis), maximumAge (millis) and
* highAccuracy (boolean)
*/
public void getCurrentLocationData(
ReadableMap options,
final Callback success,
Callback error) {
Expand Down Expand Up @@ -374,4 +438,4 @@ private boolean isSameProvider(String provider1, String provider2) {
return provider1.equals(provider2);
}
}
}
}
File renamed without changes.

0 comments on commit fab6410

Please sign in to comment.