/
LocationTracker.java
310 lines (268 loc) · 11 KB
/
LocationTracker.java
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
Copyright 2012-present Appium Committers
<p>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
<p>
http://www.apache.org/licenses/LICENSE-2.0
<p>
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.appium.settings;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.CancellationTokenSource;
import io.appium.settings.helpers.PlayServicesHelpers;
import static android.content.Context.LOCATION_SERVICE;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.concurrent.atomic.AtomicBoolean;
public class LocationTracker implements LocationListener {
private static final String TAG = LocationTracker.class.getSimpleName();
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 1 meter
private static final long LOCATION_UPDATES_INTERVAL_MS = 1000 * 60; // 1 minute
private static final long FAST_INTERVAL_MS = 5000;
private volatile LocationManager mLocationManager;
private volatile FusedLocationProviderClient mFusedLocationProviderClient;
private final LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(@NonNull LocationResult locationResult) {
Log.d(TAG, "Got a location update from Play Services");
mLocation = locationResult.getLastLocation();
}
};
private volatile Location mLocation;
private final AtomicBoolean isStarted = new AtomicBoolean(false);
private String mLocationProvider;
private static LocationTracker instance = null;
private LocationTracker() {
}
public synchronized static LocationTracker getInstance() {
if (instance == null) {
instance = new LocationTracker();
}
return instance;
}
public boolean isRunning() {
return isStarted.get();
}
private void setIsRunning(boolean value) {
isStarted.set(value);
}
@Override
public void onLocationChanged(@Nullable Location location) {
if (location == null) {
return;
}
Log.d(TAG, "Got a location update from Location Manager");
mLocation = location;
}
@Override
public void onProviderDisabled(@NonNull String provider) {
}
@Override
public void onProviderEnabled(@NonNull String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
synchronized void start(Context context) {
if (isRunning()) {
return;
}
setIsRunning(true);
if (PlayServicesHelpers.isAvailable(context)) {
initializePlayServices(context);
} else {
initializeLocationManager(context);
}
}
synchronized void stop() {
if (!isRunning()) {
return;
}
try {
stopLocationUpdatesWithPlayServices();
stopLocationUpdatesWithoutPlayServices();
} finally {
setIsRunning(false);
}
}
private void initializePlayServices(Context context) {
if (isFusedLocationProviderInitialized()) {
return;
}
Log.d(TAG, "Configuring location provider for Google Play Services");
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
LocationRequest locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_LOW_POWER)
.setInterval(LOCATION_UPDATES_INTERVAL_MS)
.setFastestInterval(FAST_INTERVAL_MS);
try {
mFusedLocationProviderClient.requestLocationUpdates(
locationRequest, locationCallback, Looper.getMainLooper());
Log.d(TAG, "Google Play Services location provider is connected");
return;
} catch (SecurityException e) {
Log.e(TAG, "Appium Settings has no access to location permission", e);
} catch (Exception e) {
Log.e(TAG, "Cannot connect to Google location service", e);
}
stopLocationUpdatesWithPlayServices();
}
private void initializeLocationManager(Context context) {
if (isLocationManagerConnected()) {
return;
}
Log.d(TAG, "Configuring the default Android location provider");
Object locationManager = context.getSystemService(LOCATION_SERVICE);
if (locationManager == null) {
Log.e(TAG, "Cannot retrieve the location manager");
return;
}
mLocationManager = (LocationManager) locationManager;
startLocationUpdatesWithoutPlayServices();
}
private void stopLocationUpdatesWithPlayServices() {
if (!isFusedLocationProviderInitialized()) {
return;
}
Log.d(TAG, "Stopping Google Play Services location provider");
mFusedLocationProviderClient.removeLocationUpdates(locationCallback);
mFusedLocationProviderClient = null;
}
private void startLocationUpdatesWithoutPlayServices() {
if (mLocationManager == null) {
return;
}
boolean isGPSEnabled = mLocationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = mLocationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
Log.e(TAG, "Both COARSE and GPS location providers are disabled");
return;
}
if (isGPSEnabled) {
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
LOCATION_UPDATES_INTERVAL_MS,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
mLocationProvider = LocationManager.GPS_PROVIDER;
Log.d(TAG, "GPS location provider is enabled. Getting FINE location");
return;
} catch (SecurityException e) {
Log.e(TAG, "Appium Settings has no access to FINE location permission", e);
}
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
LOCATION_UPDATES_INTERVAL_MS,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
mLocationProvider = LocationManager.NETWORK_PROVIDER;
Log.d(TAG, "NETWORK location provider is enabled. Getting COARSE location");
} catch (SecurityException e) {
Log.e(TAG, "Appium Settings has no access to COARSE location permission", e);
}
}
private void stopLocationUpdatesWithoutPlayServices() {
if (mLocationManager == null) {
return;
}
Log.d(TAG, "Stopping Android location provider");
mLocationManager.removeUpdates(this);
mLocationManager = null;
}
private boolean isFusedLocationProviderInitialized() {
return mFusedLocationProviderClient != null;
}
private boolean isLocationManagerConnected() {
return mLocationManager != null && mLocationProvider != null;
}
@Nullable
private Location getCachedLocation() {
if (mLocation != null) {
Log.d(TAG, "The cached location has been successfully retrieved");
return mLocation;
}
Log.d(TAG, "The cached location has not been retrieved");
return null;
}
public synchronized void forceLocationUpdate(Context context) {
if (!isRunning()) {
Log.e(TAG, "The location tracker is not running");
return;
}
if (isFusedLocationProviderInitialized()) {
try {
mFusedLocationProviderClient.getCurrentLocation(
LocationRequest.PRIORITY_HIGH_ACCURACY, new CancellationTokenSource().getToken())
.addOnCompleteListener(t -> {
if (t.isSuccessful()) {
mLocation = t.getResult();
Log.d(TAG, "The current location has been successfully retrieved from " +
"Play Services");
} else {
Log.w(TAG, "Failed to retrieve the current location from Play Services",
t.getException());
}
});
} catch (SecurityException e) {
Log.e(TAG, "Appium Settings has no access to location permission", e);
}
} else if (isLocationManagerConnected() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
try {
mLocationManager.getCurrentLocation(mLocationProvider, null,
context.getMainExecutor(), location -> {
mLocation = location;
Log.d(TAG, "The current location has been successfully retrieved " +
"from Location Manager");
});
} catch (SecurityException e) {
Log.e(TAG, "Appium Settings has no access to location permission", e);
}
}
}
@Nullable
public synchronized Location getLocation(Context context) {
if (!isRunning()) {
Log.e(TAG, "The location tracker is not running");
return null;
}
if (isFusedLocationProviderInitialized()) {
Location location = getCachedLocation();
if (location != null) {
// If Play services worked, make sure location manager is disabled
stopLocationUpdatesWithoutPlayServices();
return location;
}
}
// If Play services didn't work, try location manager
try {
if (!isLocationManagerConnected()) {
initializeLocationManager(context);
}
} catch (SecurityException e) {
Log.e(TAG, "Appium Settings has no access to location permission", e);
}
return isLocationManagerConnected() ? getCachedLocation() : null;
}
}