|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from android import Manifest |
| 4 | +from android.content import Context |
| 5 | +from android.content.pm import PackageManager |
| 6 | +from android.location import LocationListener, LocationManager |
| 7 | +from android.os import Build |
| 8 | +from java import dynamic_proxy |
| 9 | +from java.util import List |
| 10 | +from java.util.function import Consumer |
| 11 | + |
| 12 | +from toga import LatLng |
| 13 | + |
| 14 | + |
| 15 | +def toga_location(location): |
| 16 | + """Convert an Android location into a Toga LatLng and altitude.""" |
| 17 | + latlng = LatLng(location.getLatitude(), location.getLongitude()) |
| 18 | + |
| 19 | + # MSL altitude was added in API 34. We can't test this at runtime |
| 20 | + if Build.VERSION.SDK_INT >= 34 and location.hasMslAltitude(): # pragma: no cover |
| 21 | + altitude = location.getMslAltitudeMeters() |
| 22 | + elif location.hasAltitude(): |
| 23 | + altitude = location.getAltitude() |
| 24 | + else: |
| 25 | + altitude = None |
| 26 | + |
| 27 | + return { |
| 28 | + "location": latlng, |
| 29 | + "altitude": altitude, |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | +class TogaLocationConsumer(dynamic_proxy(Consumer)): |
| 34 | + def __init__(self, impl, result): |
| 35 | + super().__init__() |
| 36 | + self.impl = impl |
| 37 | + self.interface = impl.interface |
| 38 | + self.result = result |
| 39 | + |
| 40 | + def accept(self, location): |
| 41 | + loc = toga_location(location) |
| 42 | + self.result.set_result(loc["location"]) |
| 43 | + |
| 44 | + |
| 45 | +class TogaLocationListener(dynamic_proxy(LocationListener)): |
| 46 | + def __init__(self, impl): |
| 47 | + super().__init__() |
| 48 | + self.impl = impl |
| 49 | + self.interface = impl.interface |
| 50 | + |
| 51 | + def onLocationChanged(self, location): |
| 52 | + if isinstance(location, List): |
| 53 | + location = location.get(location.size() - 1) |
| 54 | + |
| 55 | + self.interface.on_change(**toga_location(location)) |
| 56 | + |
| 57 | + |
| 58 | +class Location: |
| 59 | + def __init__(self, interface): |
| 60 | + self.interface = interface |
| 61 | + self.context = self.interface.app._impl.native.getApplicationContext() |
| 62 | + if not any( |
| 63 | + self.context.getPackageManager().hasSystemFeature(feature) |
| 64 | + for feature in [ |
| 65 | + PackageManager.FEATURE_LOCATION, |
| 66 | + PackageManager.FEATURE_LOCATION_GPS, |
| 67 | + PackageManager.FEATURE_LOCATION_NETWORK, |
| 68 | + ] |
| 69 | + ): # pragma: no cover |
| 70 | + # The app doesn't have a feature supporting location services. No-cover |
| 71 | + # because we can't manufacture this condition in testing. |
| 72 | + raise RuntimeError("Location services are not available on this device.") |
| 73 | + |
| 74 | + self.native = self.context.getSystemService(Context.LOCATION_SERVICE) |
| 75 | + self.listener = TogaLocationListener(self) |
| 76 | + |
| 77 | + def has_permission(self): |
| 78 | + return ( |
| 79 | + self.interface.app._impl._native_checkSelfPermission( |
| 80 | + Manifest.permission.ACCESS_COARSE_LOCATION |
| 81 | + ) |
| 82 | + == PackageManager.PERMISSION_GRANTED |
| 83 | + ) or ( |
| 84 | + self.interface.app._impl._native_checkSelfPermission( |
| 85 | + Manifest.permission.ACCESS_FINE_LOCATION |
| 86 | + ) |
| 87 | + == PackageManager.PERMISSION_GRANTED |
| 88 | + ) |
| 89 | + |
| 90 | + def has_background_permission(self): |
| 91 | + return ( |
| 92 | + self.interface.app._impl._native_checkSelfPermission( |
| 93 | + Manifest.permission.ACCESS_BACKGROUND_LOCATION |
| 94 | + ) |
| 95 | + == PackageManager.PERMISSION_GRANTED |
| 96 | + ) |
| 97 | + |
| 98 | + def request_permission(self, future): |
| 99 | + def request_complete(permissions, results): |
| 100 | + # Map the permissions to their result |
| 101 | + perms = dict(zip(permissions, results)) |
| 102 | + try: |
| 103 | + result = ( |
| 104 | + perms[Manifest.permission.ACCESS_COARSE_LOCATION] |
| 105 | + == PackageManager.PERMISSION_GRANTED |
| 106 | + ) or ( |
| 107 | + perms[Manifest.permission.ACCESS_FINE_LOCATION] |
| 108 | + == PackageManager.PERMISSION_GRANTED |
| 109 | + ) |
| 110 | + except KeyError: # pragma: no cover |
| 111 | + # This shouldn't ever happen - we shouldn't get a completion of a |
| 112 | + # location permission request that doesn't include location permissions |
| 113 | + # - but just in case, we'll assume if it's not there, it failed. |
| 114 | + result = False |
| 115 | + future.set_result(result) |
| 116 | + |
| 117 | + self.interface.app._impl.request_permissions( |
| 118 | + [ |
| 119 | + Manifest.permission.ACCESS_COARSE_LOCATION, |
| 120 | + Manifest.permission.ACCESS_FINE_LOCATION, |
| 121 | + ], |
| 122 | + on_complete=request_complete, |
| 123 | + ) |
| 124 | + |
| 125 | + def request_background_permission(self, future): |
| 126 | + def request_complete(permissions, results): |
| 127 | + # Map the permissions to their result |
| 128 | + perms = dict(zip(permissions, results)) |
| 129 | + try: |
| 130 | + result = ( |
| 131 | + perms[Manifest.permission.ACCESS_BACKGROUND_LOCATION] |
| 132 | + == PackageManager.PERMISSION_GRANTED |
| 133 | + ) |
| 134 | + except KeyError: # pragma: no cover |
| 135 | + # This shouldn't ever happen - we shouldn't get a completion of a |
| 136 | + # location permission request that doesn't include location permissions |
| 137 | + # - but just in case, we'll assume if it's not there, it failed. |
| 138 | + result = False |
| 139 | + future.set_result(result) |
| 140 | + |
| 141 | + self.interface.app._impl.request_permissions( |
| 142 | + [ |
| 143 | + Manifest.permission.ACCESS_BACKGROUND_LOCATION, |
| 144 | + ], |
| 145 | + on_complete=request_complete, |
| 146 | + ) |
| 147 | + |
| 148 | + def current_location(self, result): |
| 149 | + consumer = TogaLocationConsumer(self, result) |
| 150 | + self.native.getCurrentLocation( |
| 151 | + LocationManager.FUSED_PROVIDER, |
| 152 | + None, |
| 153 | + self.context.getMainExecutor(), |
| 154 | + consumer, |
| 155 | + ) |
| 156 | + |
| 157 | + def start_tracking(self): |
| 158 | + # Start updates, with pings no more often than every 5 seconds, or 10 meters. |
| 159 | + self.native.requestLocationUpdates( |
| 160 | + LocationManager.FUSED_PROVIDER, |
| 161 | + 5000, |
| 162 | + 10, |
| 163 | + self.listener, |
| 164 | + ) |
| 165 | + |
| 166 | + def stop_tracking(self): |
| 167 | + self.native.removeUpdates(self.listener) |
0 commit comments