Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added pending callbacks mechanism to solve npe crash #332

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@
import com.google.android.gms.location.Priority;
import com.google.android.gms.location.SettingsClient;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("MissingPermission")
public class PlayServicesLocationManager extends BaseLocationManager {
private FusedLocationProviderClient mFusedLocationClient;
private LocationCallback mLocationCallback;
private LocationCallback mSingleLocationCallback;
private List<LocationCallback> mPendingLocationCallbacks;
private SettingsClient mLocationServicesSettingsClient;
private LocationCallback mLocationCallback;

protected PlayServicesLocationManager(ReactApplicationContext reactContext) {
super(reactContext);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(reactContext);
mLocationServicesSettingsClient = LocationServices.getSettingsClient(reactContext);
mPendingLocationCallbacks = new ArrayList<>();
}

@Override
Expand All @@ -46,9 +50,10 @@ public void getCurrentLocationData(ReadableMap options, Callback success, Callba
Activity currentActivity = mReactContext.getCurrentActivity();

if (currentActivity == null) {
mSingleLocationCallback = createSingleLocationCallback(success, error);
checkLocationSettings(options, mSingleLocationCallback, error);
return;
LocationCallback singleLocationCallback = createSingleLocationCallback(success, error);
mPendingLocationCallbacks.add(singleLocationCallback);
checkLocationSettings(options, singleLocationCallback, error);
return;
}

try {
Expand All @@ -57,8 +62,9 @@ public void getCurrentLocationData(ReadableMap options, Callback success, Callba
if (location != null && (SystemClock.currentTimeMillis() - location.getTime()) < locationOptions.maximumAge) {
success.invoke(locationToMap(location));
} else {
mSingleLocationCallback = createSingleLocationCallback(success, error);
checkLocationSettings(options, mSingleLocationCallback, error);
LocationCallback singleLocationCallback = createSingleLocationCallback(success, error);
mPendingLocationCallbacks.add(singleLocationCallback);
checkLocationSettings(options, singleLocationCallback, error);
}
});
} catch (SecurityException e) {
Expand Down Expand Up @@ -93,10 +99,16 @@ public void onLocationAvailability(LocationAvailability locationAvailability) {

@Override
public void stopObserving() {
if(mLocationCallback == null) {
if(mLocationCallback == null && mPendingLocationCallbacks.isEmpty()) {
return;
}
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
if (mLocationCallback != null) {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}
for (LocationCallback callback : mPendingLocationCallbacks) {
mFusedLocationClient.removeLocationUpdates(callback);
}
mPendingLocationCallbacks.clear();
}

private void checkLocationSettings(ReadableMap options, LocationCallback locationCallback, Callback error) {
Expand Down Expand Up @@ -167,8 +179,8 @@ public void onLocationResult(@NonNull LocationResult locationResult) {

callbackHolder.success(location);

mFusedLocationClient.removeLocationUpdates(mSingleLocationCallback);
mSingleLocationCallback = null;
mFusedLocationClient.removeLocationUpdates(this);
mPendingLocationCallbacks.remove(this);
}

@Override
Expand Down