| 1 |
''' |
| 2 |
Android GPS |
| 3 |
----------- |
| 4 |
''' |
| 5 |
|
| 6 |
from plyer.facades import GPS |
| 7 |
from plyer.platforms.android import activity |
| 8 |
from jnius import autoclass, java_method, PythonJavaClass |
| 9 |
|
| 10 |
Looper = autoclass('android.os.Looper') |
| 11 |
LocationManager = autoclass('android.location.LocationManager') |
| 12 |
Context = autoclass('android.content.Context') |
| 13 |
|
| 14 |
|
| 15 |
class _LocationListener(PythonJavaClass): |
| 16 |
__javainterfaces__ = ['android/location/LocationListener'] |
| 17 |
|
| 18 |
def __init__(self, root): |
| 19 |
self.root = root |
| 20 |
super().__init__() |
| 21 |
|
| 22 |
@java_method('(Landroid/location/Location;)V') |
| 23 |
def onLocationChanged(self, location): |
| 24 |
self.root.on_location( |
| 25 |
lat=location.getLatitude(), |
| 26 |
lon=location.getLongitude(), |
| 27 |
speed=location.getSpeed(), |
| 28 |
bearing=location.getBearing(), |
| 29 |
altitude=location.getAltitude(), |
| 30 |
accuracy=location.getAccuracy()) |
| 31 |
|
| 32 |
@java_method('(Ljava/lang/String;)V') |
| 33 |
def onProviderEnabled(self, status): |
| 34 |
if self.root.on_status: |
| 35 |
self.root.on_status('provider-enabled', status) |
| 36 |
|
| 37 |
@java_method('(Ljava/lang/String;)V') |
| 38 |
def onProviderDisabled(self, status): |
| 39 |
if self.root.on_status: |
| 40 |
self.root.on_status('provider-disabled', status) |
| 41 |
|
| 42 |
@java_method('(Ljava/lang/String;ILandroid/os/Bundle;)V') |
| 43 |
def onStatusChanged(self, provider, status, extras): |
| 44 |
if self.root.on_status: |
| 45 |
s_status = 'unknown' |
| 46 |
if status == 0x00: |
| 47 |
s_status = 'out-of-service' |
| 48 |
elif status == 0x01: |
| 49 |
s_status = 'temporarily-unavailable' |
| 50 |
elif status == 0x02: |
| 51 |
s_status = 'available' |
| 52 |
self.root.on_status('provider-status', '{}: {}'.format( |
| 53 |
provider, s_status)) |
| 54 |
|
| 55 |
|
| 56 |
class AndroidGPS(GPS): |
| 57 |
|
| 58 |
def _configure(self): |
| 59 |
if not hasattr(self, '_location_manager'): |
| 60 |
self._location_manager = activity.getSystemService( |
| 61 |
Context.LOCATION_SERVICE |
| 62 |
) |
| 63 |
self._location_listener = _LocationListener(self) |
| 64 |
|
| 65 |
def _start(self, **kwargs): |
| 66 |
min_time = kwargs.get('minTime') |
| 67 |
min_distance = kwargs.get('minDistance') |
| 68 |
providers = self._location_manager.getProviders(False).toArray() |
| 69 |
for provider in providers: |
| 70 |
self._location_manager.requestLocationUpdates( |
| 71 |
provider, |
| 72 |
min_time, |
| 73 |
min_distance, |
| 74 |
self._location_listener, |
| 75 |
Looper.getMainLooper()) |
| 76 |
|
| 77 |
def _stop(self): |
| 78 |
self._location_manager.removeUpdates(self._location_listener) |
| 79 |
|
| 80 |
|
| 81 |
def instance(): |
| 82 |
return AndroidGPS() |