1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package com.mapswithme.maps.location;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import com.mapswithme.maps.MWMApplication;
import com.mapswithme.util.ConnectionState;
import com.mapswithme.util.LocationUtils;
import java.util.ArrayList;
import java.util.List;
public class AndroidNativeProvider extends BaseLocationProvider implements android.location.LocationListener
{
private LocationManager mLocationManager;
public AndroidNativeProvider()
{
mLocationManager = (LocationManager) MWMApplication.get().getSystemService(Context.LOCATION_SERVICE);
}
@Override
protected void startUpdates()
{
final List<String> providers = getFilteredProviders();
if (providers.size() == 0)
LocationHelper.INSTANCE.notifyLocationError(LocationHelper.ERROR_DENIED);
else
{
for (final String provider : providers)
mLocationManager.requestLocationUpdates(provider, LOCATION_UPDATE_INTERVAL, 0, this);
LocationHelper.INSTANCE.registerSensorListeners();
// Choose best location from available
final Location newLocation = findBestNotExpiredLocation(providers);
if (isLocationBetterThanLast(newLocation))
LocationHelper.INSTANCE.setLastLocation(newLocation);
else
{
final Location lastLocation = LocationHelper.INSTANCE.getLastLocation();
if (lastLocation != null && !LocationUtils.isExpired(lastLocation, LocationHelper.INSTANCE.getLastLocationTime(),
LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_SHORT))
LocationHelper.INSTANCE.setLastLocation(lastLocation);
}
}
}
@Override
protected void stopUpdates()
{
mLocationManager.removeUpdates(this);
}
private Location findBestNotExpiredLocation(List<String> providers)
{
Location res = null;
for (final String pr : providers)
{
final Location l = mLocationManager.getLastKnownLocation(pr);
if (l != null && !LocationUtils.isExpired(l, l.getTime(), LocationUtils.LOCATION_EXPIRATION_TIME_MILLIS_SHORT))
{
if (res == null || res.getAccuracy() > l.getAccuracy())
res = l;
}
}
return res;
}
private List<String> getFilteredProviders()
{
final List<String> allProviders = mLocationManager.getProviders(false);
final List<String> acceptedProviders = new ArrayList<>(allProviders.size());
for (final String prov : allProviders)
{
if (LocationManager.PASSIVE_PROVIDER.equals(prov))
continue;
if (!mLocationManager.isProviderEnabled(prov))
{
if (LocationManager.GPS_PROVIDER.equals(prov))
LocationHelper.INSTANCE.notifyLocationError(LocationHelper.ERROR_GPS_OFF);
continue;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB &&
LocationManager.NETWORK_PROVIDER.equals(prov) &&
!ConnectionState.isConnected())
continue;
acceptedProviders.add(prov);
}
return acceptedProviders;
}
@Override
public void onLocationChanged(Location l)
{
// Completely ignore locations without lat and lon
if (l.getAccuracy() <= 0.0)
return;
if (isLocationBetterThanLast(l))
{
LocationHelper.INSTANCE.initMagneticField(l);
LocationHelper.INSTANCE.setLastLocation(l);
}
}
@Override
public void onProviderDisabled(String provider)
{
mLogger.d("Disabled location provider: ", provider);
}
@Override
public void onProviderEnabled(String provider)
{
mLogger.d("Enabled location provider: ", provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
mLogger.d("Status changed for location provider: ", provider, status);
}
}
|