beeware / beeware/toga

Failure modes of `current_location` request not accomodated

Open
#2,878 1 comment 0 reactions 0 assignees View on GitHub
android bug
Dominant language
Python
Stars
5.4k
Forks
827
Avg merge
15h 39m
Merged PRs (30d)
40

Description

### Describe the bug

According to the [Android documentation](https://developer.android.com/reference/android/location/LocationManager#getCurrentLocation(java.lang.String,%20android.location.LocationRequest,%20android.os.CancellationSignal,%20java.util.concurrent.Executor,%20java.util.function.Consumer%3Candroid.location.Location%3E)), it is possible for a one-time location request to fail; in which case, a location of `null` is returned. However, Toga's location service currently expects that the location is non-null.

### Steps to reproduce

This is difficult to reproduce specifically as it depends on a location lookup failure from the location service; however, if your code contains a one-time location request:
```
location = await self.location.current_location()
```

it is possible for the underlying `getCurrentLocation()` call to pass a value of `null` to the Consumer, resulting in the error:

```
E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: Process: com.example.astrofilter, PID: 19840
E/AndroidRuntime: com.chaquo.python.PyException: AttributeError: 'NoneType' object has no attribute 'getLatitude'
E/AndroidRuntime: at .toga_android.hardware.location.toga_location(location.py:17)
E/AndroidRuntime: at .toga_android.hardware.location.accept(location.py:41)
E/AndroidRuntime: at .chaquopy_java.call(chaquopy_java.pyx:354)
```

### Expected behavior

If the location service can't obtain a GPS read, it should raise an error rather than crashing.

### Screenshots

_No response_

### Environment

- Operating System: Android
- Python version: All
- Software versions:
- Briefcase: 0.3.19
- Toga: 0.4.7+

### Logs

_No response_

### Additional context

iOS/macOS already handles this edge by raising a `RuntimeError` from the `locationManager:didFailWithError:` delegate method.

The Android testbed probe currently raises an xfail in the `simulate_location_error` handler; this should be returning a `None` result to the consumer (i.e., the same implementation as `simulate_current_location`, but passing a `None` value to the accept call).

Contributor guide

Open the contributing guide

Research direction

Start with the Android location service in location.py and the testbed's simulate_current_location and simulate_location_error handlers. Compare the two probe handlers and trace how a null result reaches the consumer. Done means a failed request raises an error rather than crashing, with the testbed covering the null result.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, python
Domain
mobile-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.