-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPSUtils.java
More file actions
180 lines (151 loc) · 5.86 KB
/
GPSUtils.java
File metadata and controls
180 lines (151 loc) · 5.86 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package common.android.fiot.androidcommon;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
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;
/**
* Created by caoxuanphong on 8/16/16.
*/
public class GPSUtils {
private static final String TAG = "GPSUtils";
private static GPSUtilsListener listener;
protected static GoogleApiClient mGoogleApiClient;
protected static LocationRequest mLocationRequest;
private static MODE mode;
private enum MODE {
none,
request,
watch
}
/**
* The desired interval for location updates. Inexact. Updates may be more or less frequent.
*/
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 1000;
/**
* The fastest rate for active location updates. Exact. Updates will never be more frequent
* than this value.
*/
public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
UPDATE_INTERVAL_IN_MILLISECONDS / 2;
public interface GPSUtilsListener {
void didGetGPS(Location location);
}
public static boolean isGPSEnabled(Context mContext) {
LocationManager locationManager = (LocationManager)
mContext.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
public synchronized static void startWatchLocation(Context context, GPSUtilsListener l) {
listener = l;
mode = MODE.watch;
try {
if (mGoogleApiClient != null &&
(mGoogleApiClient.isConnected() ||
mGoogleApiClient.isConnecting())) {
mGoogleApiClient.disconnect();
stopLocationUpdates();
}
} catch (Exception e) {
e.printStackTrace();
}
buildGoogleApiClient(context);
mGoogleApiClient.connect();
}
public synchronized static void stopWatchLocation() {
mode = MODE.none;
}
public synchronized static void requestLocation(Context context, GPSUtilsListener l) {
listener = l;
mode = MODE.request;
try {
if (mGoogleApiClient != null &&
(mGoogleApiClient.isConnected() ||
mGoogleApiClient.isConnecting())) {
mGoogleApiClient.disconnect();
stopLocationUpdates();
}
} catch (Exception e) {
e.printStackTrace();
}
buildGoogleApiClient(context);
mGoogleApiClient.connect();
}
/**
* Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API.
*/
protected synchronized static void buildGoogleApiClient(Context context) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(connectionCallback)
.addOnConnectionFailedListener(connectionFailedListener)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
/**
* Requests location updates from the FusedLocationApi.
*/
private static void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, locationListener);
}
/**
* Removes location updates from the FusedLocationApi.
*/
private static void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, locationListener);
}
private static void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
static GoogleApiClient.ConnectionCallbacks connectionCallback = new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
Log.i(TAG, "old: lat: " + mLastLocation.getLatitude() + ", long: " + mLastLocation.getLongitude());
}
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
};
static GoogleApiClient.OnConnectionFailedListener connectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
};
static LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (mode == MODE.request) {
stopLocationUpdates();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
if (listener != null) {
listener.didGetGPS(location);
}
} else if (mode == MODE.none) {
stopLocationUpdates();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
} else if (mode == MODE.watch) {
if (listener != null) {
listener.didGetGPS(location);
}
}
}
};
}