Skip to content

Commit

Permalink
Merge branch 'master' of github.com:michalchudziak/react-native-geolo…
Browse files Browse the repository at this point in the history
…cation
  • Loading branch information
michalchudziak committed Aug 31, 2024
2 parents 39fb46d + f388394 commit 99f5636
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
5 changes: 5 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ if (isNewArchitectureEnabled()) {
}

android {
def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
if (agpVersion.tokenize('.')[0].toInteger() >= 7) {
namespace = "com.reactnativecommunity.geolocation"
}

compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')

defaultConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public void onProviderEnabled(String provider) { }
public void onProviderEnabled(String provider) {
}

@Override
public void onProviderDisabled(String provider) { }
public void onProviderDisabled(String provider) {
}
};

protected AndroidLocationManager(ReactApplicationContext reactContext) {
Expand Down Expand Up @@ -134,7 +136,8 @@ private String getValidProvider(LocationManager locationManager, boolean highAcc
// If it's an enabled provider, but we don't have permissions, ignore it
int finePermission = ContextCompat.checkSelfPermission(mReactContext, android.Manifest.permission.ACCESS_FINE_LOCATION);
int coarsePermission = ContextCompat.checkSelfPermission(mReactContext, android.Manifest.permission.ACCESS_COARSE_LOCATION);
if (provider.equals(LocationManager.GPS_PROVIDER) && (finePermission != PackageManager.PERMISSION_GRANTED && coarsePermission != PackageManager.PERMISSION_GRANTED)) {
if ((provider.equals(LocationManager.GPS_PROVIDER) && finePermission != PackageManager.PERMISSION_GRANTED) ||
(provider.equals(LocationManager.NETWORK_PROVIDER) && coarsePermission != PackageManager.PERMISSION_GRANTED)) {
return null;
}
return provider;
Expand Down Expand Up @@ -178,13 +181,16 @@ public void onLocationChanged(Location location) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public void onProviderEnabled(String provider) {}
public void onProviderEnabled(String provider) {
}

@Override
public void onProviderDisabled(String provider) {}
public void onProviderDisabled(String provider) {
}
};
private boolean mTriggered;

Expand All @@ -209,11 +215,12 @@ public void invoke(Location location) {

private static final int TWO_MINUTES = 1000 * 60 * 2;

/** Determines whether one Location reading is better than the current Location fix
/**
* Determines whether one Location reading is better than the current Location fix
* taken from Android Examples https://developer.android.com/guide/topics/location/strategies.html
*
* @param location The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new one
* @param location The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new one
*/
private boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
Expand Down Expand Up @@ -258,7 +265,9 @@ private boolean isBetterLocation(Location location, Location currentBestLocation
return false;
}

/** Checks whether two providers are the same */
/**
* Checks whether two providers are the same
*/
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
Expand Down
28 changes: 22 additions & 6 deletions ios/RNCGeolocation.mm
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ - (void)beginLocationUpdatesWithDesiredAccuracy:(CLLocationAccuracy)desiredAccur

if (@available(iOS 14.0, *)) {
if (
#if ! TARGET_OS_VISION
_lastUpdatedAuthorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
#endif
_lastUpdatedAuthorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse
) {
[self startMonitoring];
Expand All @@ -202,16 +204,24 @@ - (void)beginLocationUpdatesWithDesiredAccuracy:(CLLocationAccuracy)desiredAccur

- (void)startMonitoring
{
_usingSignificantChanges
? [_locationManager startMonitoringSignificantLocationChanges]
: [_locationManager startUpdatingLocation];
#if !TARGET_OS_VISION
_usingSignificantChanges
? [_locationManager startMonitoringSignificantLocationChanges]
: [_locationManager startUpdatingLocation];
#else
[_locationManager startUpdatingLocation];
#endif
}

- (void)stopMonitoring
{
_usingSignificantChanges
? [_locationManager stopMonitoringSignificantLocationChanges]
: [_locationManager stopUpdatingLocation];
#if !TARGET_OS_VISION
_usingSignificantChanges
? [_locationManager stopMonitoringSignificantLocationChanges]
: [_locationManager stopUpdatingLocation];
#else
[_locationManager stopUpdatingLocation];
#endif
}

#pragma mark - Timeout handler
Expand Down Expand Up @@ -273,22 +283,26 @@ - (void)timeout:(NSTimer *)timer

// Request location access permission
if (wantsAlways) {
#if !TARGET_OS_VISION
[_locationManager requestAlwaysAuthorization];
[self enableBackgroundLocationUpdates];
#endif
} else if (wantsWhenInUse) {
[_locationManager requestWhenInUseAuthorization];
}
}

- (void)enableBackgroundLocationUpdates
{
#if !TARGET_OS_VISION
// iOS 9+ requires explicitly enabling background updates
NSArray *backgroundModes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIBackgroundModes"];
if (backgroundModes && [backgroundModes containsObject:@"location"]) {
if ([_locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[_locationManager setAllowsBackgroundLocationUpdates:YES];
}
}
#endif
}


Expand Down Expand Up @@ -445,7 +459,9 @@ - (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager
}

if (
#if !TARGET_OS_VISION
currentStatus == kCLAuthorizationStatusAuthorizedAlways ||
#endif
currentStatus == kCLAuthorizationStatusAuthorizedWhenInUse
) {
if (_queuedAuthorizationCallbacks != nil && _queuedAuthorizationCallbacks.count > 0){
Expand Down
2 changes: 1 addition & 1 deletion react-native-geolocation.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Pod::Spec.new do |s|

s.authors = package['author']
s.homepage = package['homepage']
s.platform = :ios, "9.0"
s.platforms = { :ios => '9.0', :visionos => '1.0' }

s.source = { :git => "https://github.com/react-native-community/react-native-geolocation.git", :tag => "v#{s.version}" }
s.source_files = "ios/**/*.{h,m,mm}"
Expand Down

0 comments on commit 99f5636

Please sign in to comment.