blob: b76ae500af93feaed02b1bbdcd7abef56e4222b8 (
plain)
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
|
package com.mapswithme.maps.location;
import android.location.Location;
import android.os.Bundle;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.mapswithme.maps.MwmApplication;
public class GoogleFusedLocationProvider extends BaseLocationProvider
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
{
private static final String GS_LOCATION_PROVIDER = "fused";
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private boolean mIsResolvingError;
public GoogleFusedLocationProvider()
{
mGoogleApiClient = new GoogleApiClient.Builder(MwmApplication.get())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(LOCATION_UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(LOCATION_UPDATE_INTERVAL / 2);
}
@Override
protected void startUpdates()
{
if (mGoogleApiClient != null && !mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting())
mGoogleApiClient.connect();
}
@Override
protected void stopUpdates()
{
if (mGoogleApiClient != null)
{
if (mGoogleApiClient.isConnected())
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}
@Override
protected boolean isLocationBetterThanLast(Location newLocation)
{
if (newLocation == null)
return false;
Location lastLocation = LocationHelper.INSTANCE.getLastLocation();
if (lastLocation == null)
return true;
// We believe that google service always returns good locations.
return GS_LOCATION_PROVIDER.equalsIgnoreCase(newLocation.getProvider()) ||
!GS_LOCATION_PROVIDER.equalsIgnoreCase(lastLocation.getProvider()) && super.isLocationBetterThanLast(newLocation);
}
@Override
public void onConnected(Bundle bundle)
{
mLogger.d("Fused onConnected. Bundle " + bundle);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
LocationHelper.INSTANCE.registerSensorListeners();
final Location l = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (l != null)
LocationHelper.INSTANCE.setLastLocation(l);
}
@Override
public void onConnectionSuspended(int i)
{
mLogger.d("Fused onConnectionSuspended. Code " + i);
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
{
mLogger.d("Fused onConnectionFailed. Fall back to native provider. ConnResult " + connectionResult);
// TODO handle error in a smarter way
LocationHelper.INSTANCE.initLocationProvider(true);
}
@Override
public void onLocationChanged(Location location)
{
// Completely ignore locations without lat and lon
if (location.getAccuracy() <= 0.0)
return;
if (isLocationBetterThanLast(location))
{
LocationHelper.INSTANCE.initMagneticField(location);
LocationHelper.INSTANCE.setLastLocation(location);
}
}
}
|