Keep requesting location even when not in foreground
See original GitHub issueDescribe the bug
When <MapboxGL.UserLocation/> is used app keeps requesting GPS position from location manager and device. This works as intendend. However app keeps requesting position even when not in foreground. This can happen when user press home button or user switch to another app. App keeps requesting location until is killed by system or manually by user.
To Reproduce
clone repo
cd example
yarn
npx react-native run-android --variant=release
- Run app on device.
- Allow app to use location
- Navigate to User location > Set User Location Render Mode or User Location Updates
- Verify that you can see you position on the map
- Verify that you can see GPS icon in the status bar. Not all the phones has it and not all the phone can turn it on. I understand that it might be difficult to reprocuce this issue without this icon.
- Press home button
- Verify that you can still see GPS icon in the status bar. It should not be there.
- Open task manager and kill Example app
- Verify that GPS icon disapper from status bar
Expected behavior
App should not request GPS postion when paused/in background/switched away. GPS icon in status bar should be visible only when app is in foreground.
Actual behavior
Remove location listeners when app leave foreground.
Screenshots
Note the small GPS icon in both screenshots.
Versions:
- Platform: Android
- Platform OS: Android 9
- Device: Huawei Y6 Pro 2019
- Emulator/ Simulator: no
- Dev OS: MacOS 11.3.1
- react-native-mapbox-gl Version https://github.com/react-native-mapbox-gl/maps/commit/be4e8682290ebf422568669905de4fac1cf474ec
- Mapbox GL version 9.6.1
- React Native Version 0.63.4
Additional context
This issue is quite a big deal as it do not comply with Google Policy. Apps cannot access location while app is not in background unless background permissions and special declariation is made. Apps that do not comply with this are taken down from Google Play Store. More info in the discussion. https://github.com/react-native-mapbox-gl/maps/discussions/1362
Issue Analytics
- State:
- Created 2 years ago
- Comments:18 (16 by maintainers)
Top GitHub Comments
When tesing on actual device make sure that you go to Settings > Apps >Example app > Power managment and disable all the fancy power optimalisation. In my case I can see that app is killed within 10s after is in background.
I tried the same concept as in PR https://github.com/react-native-mapbox-gl/maps/pull/1365 directly in my app and it worked. I did not try the PR itself.
Profiling
I played a bit with profiling in Android Studio. It is very unstable and the app crashed a lot. But in the end I managed to get a following screenshot. On a real device it crashed even more and sometimes to not capture location request at all.
On the screenshot you can see power tab of profiling with CPU, network and location. I started the app navigated to location example, pressed the home butoon and resumed app. “The valley” in the middle is when app was in the background. Those small vertical lines precisely one second apart are actual location updates.
First row is the mapbox telemetry service. It create a location request but do not really ask for any location. Third and fifth row is the location manager. You can see as it is pause when app was in the background and started again on 5th row when app came back to foreground.
On the forth row you can see setFollowUserLocation. It registered for updates and keep registered also while the app is in the background.
As this test was performed on Android Emulator with API 29, I think system simply do not send location update while app is in the background. That would explain where there is a gap of vertical lines on line 4.
I managed to make it work. And is seems like that GPS is not used when app leaves foreground when followUserLocation is set to false. Below code works.
GPS is used for two different purposes. One is to draw the blue dot on the map and another to follow users location with camera. Those are two different things. First works fine and register and unregister it’s listeners when app enter foreground and leaves it. Second one does not.