michalchudziak / react-native-geolocation

Geolocation APIs for React Native
MIT License
1.27k stars 219 forks source link

App force close when GPS is disabled from the notification drawer. #303

Open sourabhsharmait opened 4 months ago

sourabhsharmait commented 4 months ago

Summary In Android version 10, When user using the location services and disabled the location from drawer. App got crash. We tried to debug it on Android studio cat log but didn't able to find any way to fix it in our react native project.

Reproducible sample code "react-native-maps": "^1.10.3", Steps to reproduce Disable the locaiton from drawer. Try on and off

Expected result App should not crash

Actual result Got Crash

React Native Maps Version 1.10.3

What platforms are you seeing the problem on? Android

React Native Version 0.72.4

What version of Expo are you using? Not using Expo

Device(s) realme 3i

Additional information FATAL EXCEPTION: mqt_native_modules Process: com.enviro, PID: 8697 java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onProviderDisabled(java.lang.String)" at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:365) at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:275) at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:291) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:227) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:228) at java.lang.Thread.run(Thread.java:919)

307310282-b5d3ea08-8e26-45b2-ab2c-fa28df260daa

In our code, we are using only geolocation library. I dont know why its showing the name of this library folder while trying to debugging in Android Studio.

The screenshot above is also share from android studio logs. I guess map library might be internally interconnected with Geo location. For more clarity on this issue, I am sharing below code related to fetching current location. This got crash only when user tried to disable the location form notification drawer.

import Geolocation from '@react-native-community/geolocation';

useEffect(() => { getCurrentLocation(); }, []);

useEffect(() => { // getCurrentLocation(); const handleAppStateChange = (nextAppState) => { console.log('nextAppState', nextAppState); if (appState.match(/inactive|background/) && nextAppState === 'active') { getCurrentLocation(); } setAppState(nextAppState);

};

const appStateSubscription = AppState.addEventListener('change', handleAppStateChange);

return () => {
  appStateSubscription.remove();
};

}, [appState]);

const getCurrentLocation = async () => { try { const result = await Promise.race([ request( Platform.OS === "ios" ? PERMISSIONS.IOS.LOCATION_WHEN_IN_USE : PERMISSIONS.ANDROID.ACCESS_FINELOCATION ), new Promise((, reject) => setTimeout(() => reject('Location request timed out'), 7000)), ]);

  setPermissionStatus(result);
  onLocationChange(null);
  if (result === RESULTS.GRANTED) {
    Geolocation.getCurrentPosition(
      (position) => {
        const { latitude, longitude } = position.coords;
        onLocationChange({ latitude, longitude, permissionStatus });
        setRetryCount(0); // Reset retry count on successful location retrieval
      },
      (error) => {
        onLocationChange("location_faild");
        // if (error?.message == 'No location provider available.') {
        //   Alert.alert('GPS Disabled',
        //     'To use this app, please enable GPS (location services) on your device. ' +
        //     'You can enable it in your device settings.', [
        //     { text: AppString.OK, style: 'cancel', onPress: () => navigation.goBack() },
        //   ]);
        //   return;
        // }
        // if (retryCount < getMaxRetries) {
        //   setRetryCount(retryCount + 1);
        //   setTimeout(getCurrentLocation, 1000); // Retry Fetch Location here
        // } else {
        //   onLocationChange("Failed");
        //   console.log("Failed to retrieve location");
        // }
      },
      { timeout: 60000, enableHighAccuracy: false } // Added enableHighAccuracy: false
    );
  } else if (result === RESULTS.DENIED) {
    onLocationChange('location_faild');
    alertPopup();
  } else if (result === RESULTS.BLOCKED) {
    onLocationChange('location_faild');
    alertPopup();
  } else if (result == RESULTS.UNAVAILABLE) {
    onLocationChange('location_faild');
  }
} catch (error) {
  console.error(error);
  onLocationChange(null);
}

};