forked from upstream-mirrors/OpenTracks
Use LocationClient to request for location updates.
This commit is contained in:
@@ -109,9 +109,6 @@ public abstract class Constants {
|
||||
/** Maximum age of a GPS location to be considered current. */
|
||||
public static final long MAX_LOCATION_AGE_MS = 60 * 1000; // 1 minute
|
||||
|
||||
/** Maximum age of a network location to be considered current. */
|
||||
public static final long MAX_NETWORK_AGE_MS = 1000 * 60 * 10; // 10 minutes
|
||||
|
||||
/**
|
||||
* The type of account that we can use for gdata uploads.
|
||||
*/
|
||||
|
||||
@@ -25,10 +25,8 @@ import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.database.ContentObserver;
|
||||
import android.location.LocationManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* Data source on the phone.
|
||||
@@ -37,9 +35,6 @@ import android.util.Log;
|
||||
*/
|
||||
public class DataSource {
|
||||
|
||||
private static final int NETWORK_PROVIDER_MIN_TIME = 5 * 60 * 1000; // 5 minutes
|
||||
private static final String TAG = DataSource.class.getSimpleName();
|
||||
|
||||
private final ContentResolver contentResolver;
|
||||
private final MyTracksLocationManager myTracksLocationManager;
|
||||
private final SharedPreferences sharedPreferences;
|
||||
@@ -86,19 +81,8 @@ public class DataSource {
|
||||
*
|
||||
* @param listener the listener
|
||||
*/
|
||||
public void registerLocationListener(android.location.LocationListener listener) {
|
||||
|
||||
// Listen for GPS location
|
||||
myTracksLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
|
||||
|
||||
// Listen for network location
|
||||
try {
|
||||
myTracksLocationManager.requestLocationUpdates(
|
||||
LocationManager.NETWORK_PROVIDER, NETWORK_PROVIDER_MIN_TIME, 0, listener);
|
||||
} catch (RuntimeException e) {
|
||||
// Network location is optional, so just log the exception
|
||||
Log.w(TAG, "Could not register for network location.", e);
|
||||
}
|
||||
public void registerLocationListener(LocationListener listener) {
|
||||
myTracksLocationManager.requestLocationUpdates(0, 0, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,8 +90,8 @@ public class DataSource {
|
||||
*
|
||||
* @param listener the listener
|
||||
*/
|
||||
public void unregisterLocationListener(android.location.LocationListener listener) {
|
||||
myTracksLocationManager.removeUpdates(listener);
|
||||
public void unregisterLocationListener(LocationListener listener) {
|
||||
myTracksLocationManager.removeLocationUpdates(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,14 +18,13 @@ package com.google.android.apps.mytracks.content;
|
||||
|
||||
import static com.google.android.apps.mytracks.Constants.TAG;
|
||||
|
||||
import com.google.android.gms.location.LocationListener;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.database.ContentObserver;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
@@ -106,15 +105,6 @@ public class DataSourceManager {
|
||||
}
|
||||
dataSourceListener.notifyLocationChanged(location);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderDisabled(String provider) {}
|
||||
|
||||
@Override
|
||||
public void onProviderEnabled(String provider) {}
|
||||
|
||||
@Override
|
||||
public void onStatusChanged(String provider, int status, Bundle extras) {}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,7 +18,6 @@ package com.google.android.apps.mytracks.content;
|
||||
|
||||
import static com.google.android.apps.mytracks.Constants.MAX_DISPLAYED_WAYPOINTS_POINTS;
|
||||
import static com.google.android.apps.mytracks.Constants.MAX_LOCATION_AGE_MS;
|
||||
import static com.google.android.apps.mytracks.Constants.MAX_NETWORK_AGE_MS;
|
||||
import static com.google.android.apps.mytracks.Constants.TARGET_DISPLAYED_TRACK_POINTS;
|
||||
|
||||
import com.google.android.apps.mytracks.Constants;
|
||||
@@ -35,7 +34,6 @@ import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.hardware.GeomagneticField;
|
||||
import android.location.Location;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.util.Log;
|
||||
@@ -683,23 +681,10 @@ public class TrackDataHub implements DataSourceListener {
|
||||
}
|
||||
boolean oldHasFix = hasFix;
|
||||
boolean oldHasGoodFix = hasGoodFix;
|
||||
long now = System.currentTimeMillis();
|
||||
if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
|
||||
hasFix = !isLocationOld(location, now, MAX_LOCATION_AGE_MS);
|
||||
hasGoodFix = location.getAccuracy() <= minRequiredAccuracy;
|
||||
lastSeenLocation = location;
|
||||
} else {
|
||||
// A network location.
|
||||
|
||||
// If has a recent GPS location, ignore the network location.
|
||||
if (!isLocationOld(lastSeenLocation, now, MAX_LOCATION_AGE_MS)) {
|
||||
return;
|
||||
}
|
||||
|
||||
hasFix = false;
|
||||
hasGoodFix = false;
|
||||
lastSeenLocation = isLocationOld(location, now, MAX_NETWORK_AGE_MS) ? null : location;
|
||||
}
|
||||
|
||||
hasFix = !isLocationOld(location, System.currentTimeMillis(), MAX_LOCATION_AGE_MS);
|
||||
hasGoodFix = location.getAccuracy() <= minRequiredAccuracy;
|
||||
lastSeenLocation = location;
|
||||
|
||||
if (trackDataListeners.isEmpty()) {
|
||||
return;
|
||||
|
||||
+55
-22
@@ -22,6 +22,7 @@ import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallback
|
||||
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
|
||||
import com.google.android.gms.location.LocationClient;
|
||||
import com.google.android.gms.location.LocationListener;
|
||||
import com.google.android.gms.location.LocationRequest;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
@@ -56,22 +57,29 @@ public class MyTracksLocationManager {
|
||||
private static final String VALUE = "value";
|
||||
|
||||
private final ConnectionCallbacks connectionCallbacks = new ConnectionCallbacks() {
|
||||
@Override
|
||||
public void onDisconnected() {}
|
||||
@Override
|
||||
public void onDisconnected() {}
|
||||
|
||||
@Override
|
||||
public void onConnected(Bundle bunlde) {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (requestLastLocation != null && locationClient.isConnected()) {
|
||||
requestLastLocation.onLocationChanged(locationClient.getLastLocation());
|
||||
requestLastLocation = null;
|
||||
@Override
|
||||
public void onConnected(Bundle bunlde) {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (requestLastLocation != null && locationClient.isConnected()) {
|
||||
requestLastLocation.onLocationChanged(locationClient.getLastLocation());
|
||||
requestLastLocation = null;
|
||||
}
|
||||
if (requestLocationUpdates != null && locationClient.isConnected()) {
|
||||
LocationRequest locationRequest = new LocationRequest().setPriority(
|
||||
LocationRequest.PRIORITY_HIGH_ACCURACY).setInterval(requestLocationUpdatesTime)
|
||||
.setFastestInterval(requestLocationUpdatesTime)
|
||||
.setSmallestDisplacement(requestLocationUpdatesDistance);
|
||||
locationClient.requestLocationUpdates(locationRequest, requestLocationUpdates, looper);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
private final OnConnectionFailedListener
|
||||
onConnectionFailedListener = new OnConnectionFailedListener() {
|
||||
@@ -79,6 +87,7 @@ public class MyTracksLocationManager {
|
||||
public void onConnectionFailed(ConnectionResult connectionResult) {}
|
||||
};
|
||||
|
||||
private final Looper looper;
|
||||
private final Handler handler;
|
||||
private final LocationClient locationClient;
|
||||
|
||||
@@ -88,8 +97,12 @@ public class MyTracksLocationManager {
|
||||
private boolean isAvailable;
|
||||
private boolean isAllowed;
|
||||
private LocationListener requestLastLocation;
|
||||
private LocationListener requestLocationUpdates;
|
||||
private float requestLocationUpdatesDistance;
|
||||
private long requestLocationUpdatesTime;
|
||||
|
||||
public MyTracksLocationManager(Context context, Looper looper) {
|
||||
this.looper = looper;
|
||||
handler = new Handler(looper);
|
||||
locationClient = new LocationClient(context, connectionCallbacks, onConnectionFailedListener);
|
||||
locationClient.connect();
|
||||
@@ -173,20 +186,40 @@ public class MyTracksLocationManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* @see android.location.LocationManager#requestLocationUpdates(java.lang.String,
|
||||
* long, float, android.location.LocationListener)
|
||||
* Requests location updates.
|
||||
*
|
||||
* @param minTime the minimal time
|
||||
* @param minDistance the minimal distance
|
||||
* @param locationListener the location listener
|
||||
*/
|
||||
public void requestLocationUpdates(
|
||||
String provider, long minTime, float minDistance, android.location.LocationListener listener) {
|
||||
locationManager.requestLocationUpdates(provider, minTime, minDistance, listener);
|
||||
final long minTime, final float minDistance, final LocationListener locationListener) {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
requestLocationUpdatesTime = minTime;
|
||||
requestLocationUpdatesDistance = minDistance;
|
||||
requestLocationUpdates = locationListener;
|
||||
connectionCallbacks.onConnected(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param listener
|
||||
* @see android.location.LocationManager#removeUpdates(android.location.LocationListener)
|
||||
* Removes location updates.
|
||||
*
|
||||
* @param locationListener the location listener
|
||||
*/
|
||||
public void removeUpdates(android.location.LocationListener listener) {
|
||||
locationManager.removeUpdates(listener);
|
||||
public void removeLocationUpdates(final LocationListener locationListener) {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
requestLocationUpdates = null;
|
||||
if (locationClient.isConnected()) {
|
||||
locationClient.removeLocationUpdates(locationListener);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -41,6 +41,7 @@ import com.google.android.apps.mytracks.util.IntentUtils;
|
||||
import com.google.android.apps.mytracks.util.LocationUtils;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
import com.google.android.apps.mytracks.util.TrackNameUtils;
|
||||
import com.google.android.gms.location.LocationListener;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
@@ -54,11 +55,9 @@ import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Binder;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Looper;
|
||||
@@ -216,21 +215,6 @@ public class TrackRecordingService extends Service {
|
||||
};
|
||||
|
||||
private LocationListener locationListener = new LocationListener() {
|
||||
@Override
|
||||
public void onProviderDisabled(String provider) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderEnabled(String provider) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(final Location location) {
|
||||
if (myTracksLocationManager == null || executorService == null
|
||||
@@ -974,8 +958,7 @@ public class TrackRecordingService extends Service {
|
||||
try {
|
||||
long interval = locationListenerPolicy.getDesiredPollingInterval();
|
||||
myTracksLocationManager.requestLocationUpdates(
|
||||
LocationManager.GPS_PROVIDER, interval, locationListenerPolicy.getMinDistance(),
|
||||
locationListener);
|
||||
interval, locationListenerPolicy.getMinDistance(), locationListener);
|
||||
currentRecordingInterval = interval;
|
||||
} catch (RuntimeException e) {
|
||||
Log.e(TAG, "Could not register location listener.", e);
|
||||
@@ -992,7 +975,7 @@ public class TrackRecordingService extends Service {
|
||||
Log.e(TAG, "locationManager is null.");
|
||||
return;
|
||||
}
|
||||
myTracksLocationManager.removeUpdates(locationListener);
|
||||
myTracksLocationManager.removeLocationUpdates(locationListener);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.google.android.apps.mytracks.content.MyTracksProviderUtils.LocationIt
|
||||
import com.google.android.apps.mytracks.content.TrackDataListener.LocationState;
|
||||
import com.google.android.apps.mytracks.services.TrackRecordingServiceTest.MockContext;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
import com.google.android.gms.location.LocationListener;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
import com.google.android.testing.mocking.AndroidMock;
|
||||
import com.google.android.testing.mocking.UsesMocks;
|
||||
@@ -39,7 +40,6 @@ import android.database.ContentObserver;
|
||||
import android.database.Cursor;
|
||||
import android.database.MatrixCursor;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.provider.BaseColumns;
|
||||
import android.test.AndroidTestCase;
|
||||
import android.test.RenamingDelegatingContext;
|
||||
|
||||
Reference in New Issue
Block a user