Skip to content

Commit

Permalink
Add null Activity check in PlayServicesLocationManager.getCurrentLoca…
Browse files Browse the repository at this point in the history
…tionData (#220)

* add ACTIVITY_NULL error code to PositionError

* invoke error callback if currentActivity is null in getCurrentLocationData
  • Loading branch information
samzmann authored Nov 24, 2022
1 parent dfbbdff commit b6d3a06
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.reactnativecommunity.geolocation;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.location.Location;
import android.os.Build;
import android.os.Looper;
Expand Down Expand Up @@ -45,9 +46,16 @@ protected PlayServicesLocationManager(ReactApplicationContext reactContext) {
public void getCurrentLocationData(ReadableMap options, Callback success, Callback error) {
AndroidLocationManager.LocationOptions locationOptions = AndroidLocationManager.LocationOptions.fromReactMap(options);

Activity currentActivity = mReactContext.getCurrentActivity();

if (currentActivity == null) {
error.invoke(PositionError.buildError(PositionError.ACTIVITY_NULL, "mReactContext.getCurrentActivity() returned null but should be non-null in getCurrentLocationData"));
return;
}

try {
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(mReactContext.getCurrentActivity(), location -> {
.addOnSuccessListener(currentActivity, location -> {
if (location != null && (SystemClock.currentTimeMillis() - location.getTime()) < locationOptions.maximumAge) {
success.invoke(locationToMap(location));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public class PositionError {
*/
public static int TIMEOUT = 3;

/**
* Getting the current Activity returned null, but the logic requires a non-null Activity.
* This error can then be returned to inform the user of some underlying Android error.
*/
public static int ACTIVITY_NULL = 4;

public static WritableMap buildError(int code, String message) {
WritableMap error = Arguments.createMap();
error.putInt("code", code);
Expand All @@ -46,6 +52,7 @@ public static WritableMap buildError(int code, String message) {
error.putInt("PERMISSION_DENIED", PERMISSION_DENIED);
error.putInt("POSITION_UNAVAILABLE", POSITION_UNAVAILABLE);
error.putInt("TIMEOUT", TIMEOUT);
error.putInt("ACTIVITY_NULL", ACTIVITY_NULL);
return error;
}
}

0 comments on commit b6d3a06

Please sign in to comment.