forked from upstream-mirrors/OpenTracks
merge heads
This commit is contained in:
@@ -320,11 +320,6 @@ public class TrackListActivity extends AbstractSendToGoogleActivity
|
||||
// Ignore
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHeadingChanged(double heading) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearWaypoints() {
|
||||
// Ignore
|
||||
|
||||
@@ -28,9 +28,6 @@ import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.database.ContentObserver;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
@@ -51,14 +48,12 @@ public class DataSource {
|
||||
private final Context context;
|
||||
private final ContentResolver contentResolver;
|
||||
private final MyTracksLocationManager myTracksLocationManager;
|
||||
private final SensorManager sensorManager;
|
||||
private final SharedPreferences sharedPreferences;
|
||||
|
||||
public DataSource(Context context) {
|
||||
this.context = context;
|
||||
contentResolver = context.getContentResolver();
|
||||
myTracksLocationManager = new MyTracksLocationManager(context);
|
||||
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
|
||||
sharedPreferences = context.getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
@@ -169,29 +164,6 @@ public class DataSource {
|
||||
return location.getTime() > System.currentTimeMillis() - MAX_LOCATION_AGE_MS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a heading listener.
|
||||
*
|
||||
* @param listener the listener
|
||||
*/
|
||||
public void registerHeadingListener(SensorEventListener listener) {
|
||||
Sensor heading = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
|
||||
if (heading == null) {
|
||||
Log.d(TAG, "No heading sensor.");
|
||||
return;
|
||||
}
|
||||
sensorManager.registerListener(listener, heading, SensorManager.SENSOR_DELAY_UI);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters a heading listener.
|
||||
*
|
||||
* @param listener the listener
|
||||
*/
|
||||
public void unregisterHeadingListener(SensorEventListener listener) {
|
||||
sensorManager.unregisterListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a shared preference change listener.
|
||||
*
|
||||
|
||||
@@ -62,13 +62,6 @@ public interface DataSourceListener {
|
||||
*/
|
||||
public void notifyLocationProviderAvailable(boolean available);
|
||||
|
||||
/**
|
||||
* Notifies when the compass heading changes.
|
||||
*
|
||||
* @param heading the new heading
|
||||
*/
|
||||
public void notifyHeadingChanged(float heading);
|
||||
|
||||
/**
|
||||
* Notifies when a preference changes.
|
||||
*
|
||||
|
||||
@@ -23,9 +23,6 @@ import com.google.common.annotations.VisibleForTesting;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.database.ContentObserver;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
@@ -138,25 +135,6 @@ public class DataSourceManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listener for heading changes.
|
||||
*
|
||||
* @author Jimmy Shih
|
||||
*/
|
||||
@VisibleForTesting
|
||||
class HeadingListener implements SensorEventListener {
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
dataSourceListener.notifyHeadingChanged(event.values[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listener for preference changes.
|
||||
*
|
||||
@@ -181,7 +159,6 @@ public class DataSourceManager {
|
||||
private final WaypointsTableObserver waypointsTableObserver;
|
||||
private final TrackPointsTableObserver trackPointsTableObserver;
|
||||
private final CurrentLocationListener currentLocationListener;
|
||||
private final HeadingListener headingListener;
|
||||
private final PreferenceListener preferenceListener;
|
||||
|
||||
public DataSourceManager(DataSource dataSource, DataSourceListener dataSourceListener) {
|
||||
@@ -193,7 +170,6 @@ public class DataSourceManager {
|
||||
waypointsTableObserver = new WaypointsTableObserver();
|
||||
trackPointsTableObserver = new TrackPointsTableObserver();
|
||||
currentLocationListener = new CurrentLocationListener();
|
||||
headingListener = new HeadingListener();
|
||||
preferenceListener = new PreferenceListener();
|
||||
}
|
||||
|
||||
@@ -265,9 +241,6 @@ public class DataSourceManager {
|
||||
case LOCATION:
|
||||
dataSource.registerLocationListener(currentLocationListener);
|
||||
break;
|
||||
case HEADING:
|
||||
dataSource.registerHeadingListener(headingListener);
|
||||
break;
|
||||
case PREFERENCE:
|
||||
dataSource.registerOnSharedPreferenceChangeListener(preferenceListener);
|
||||
break;
|
||||
@@ -301,9 +274,6 @@ public class DataSourceManager {
|
||||
case LOCATION:
|
||||
dataSource.unregisterLocationListener(currentLocationListener);
|
||||
break;
|
||||
case HEADING:
|
||||
dataSource.unregisterHeadingListener(headingListener);
|
||||
break;
|
||||
case PREFERENCE:
|
||||
dataSource.unregisterOnSharedPreferenceChangeListener(preferenceListener);
|
||||
break;
|
||||
|
||||
@@ -52,9 +52,6 @@ public class TrackDataHub implements DataSourceListener {
|
||||
|
||||
private static final String TAG = TrackDataHub.class.getSimpleName();
|
||||
|
||||
// One hour in milliseconds
|
||||
private static final int ONE_HOUR = 60 * 60 * 1000;
|
||||
|
||||
private final Context context;
|
||||
private final TrackDataManager trackDataManager;
|
||||
private final MyTracksProviderUtils myTracksProviderUtils;
|
||||
@@ -75,11 +72,6 @@ public class TrackDataHub implements DataSourceListener {
|
||||
private int minRequiredAccuracy;
|
||||
private int minRecordingDistance;
|
||||
|
||||
// Heading values
|
||||
private float lastHeading = 0;
|
||||
private float lastDeclination = 0;
|
||||
private long lastDeclinationUpdate = 0;
|
||||
|
||||
// Location values
|
||||
private Location lastSeenLocation = null;
|
||||
private boolean hasProviderEnabled = true;
|
||||
@@ -400,31 +392,6 @@ public class TrackDataHub implements DataSourceListener {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyHeadingChanged(final float heading) {
|
||||
runInHanderThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
lastHeading = heading;
|
||||
|
||||
if (lastSeenLocation != null) {
|
||||
// Update the declination at most once an hour
|
||||
long now = System.currentTimeMillis();
|
||||
if (now - lastDeclinationUpdate > ONE_HOUR) {
|
||||
lastDeclinationUpdate = now;
|
||||
long timestamp = lastSeenLocation.getTime();
|
||||
if (timestamp == 0) {
|
||||
// Hack for Samsung phones which don't populate the time field
|
||||
timestamp = now;
|
||||
}
|
||||
lastDeclination = getDeclination(lastSeenLocation, timestamp);
|
||||
}
|
||||
}
|
||||
notifyHeadingChange(trackDataManager.getListeners(TrackDataType.HEADING));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads data for all listeners. To be run in the {@link #handler} thread.
|
||||
*/
|
||||
@@ -459,7 +426,6 @@ public class TrackDataHub implements DataSourceListener {
|
||||
} else {
|
||||
notifyLocationStateChanged(trackDataManager.getListeners(TrackDataType.LOCATION));
|
||||
}
|
||||
notifyHeadingChange(trackDataManager.getListeners(TrackDataType.HEADING));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -510,10 +476,6 @@ public class TrackDataHub implements DataSourceListener {
|
||||
notifyLocationStateChanged(trackDataListeners);
|
||||
}
|
||||
}
|
||||
|
||||
if (trackDataTypes.contains(TrackDataType.HEADING)) {
|
||||
notifyHeadingChange(trackDataListeners);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -746,21 +708,6 @@ public class TrackDataHub implements DataSourceListener {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies heading change. To be run in the {@link #handler} thread.
|
||||
*
|
||||
* @param trackDataListeners the track data listeners to notify
|
||||
*/
|
||||
private void notifyHeadingChange(Set<TrackDataListener> trackDataListeners) {
|
||||
if (trackDataListeners.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
float value = lastHeading + lastDeclination;
|
||||
for (TrackDataListener trackDataListener : trackDataListeners) {
|
||||
trackDataListener.onHeadingChanged(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the track points sampling states.
|
||||
*/
|
||||
|
||||
@@ -49,13 +49,6 @@ public interface TrackDataListener {
|
||||
*/
|
||||
public void onLocationChanged(Location location);
|
||||
|
||||
/**
|
||||
* Called when the heading changes.
|
||||
*
|
||||
* @param heading the heading
|
||||
*/
|
||||
public void onHeadingChanged(double heading);
|
||||
|
||||
/**
|
||||
* Called when the selected track changes. This will be followed by calls to
|
||||
* data methods such as {@link #onTrackUpdated(Track)},
|
||||
|
||||
@@ -28,6 +28,5 @@ public enum TrackDataType {
|
||||
SAMPLED_IN_TRACK_POINTS_TABLE, // sampled-in track points table changes
|
||||
SAMPLED_OUT_TRACK_POINTS_TABLE, // sampled-out track points table changes
|
||||
LOCATION, // location changes
|
||||
HEADING, // heading changes
|
||||
PREFERENCE // preference changes
|
||||
}
|
||||
@@ -165,11 +165,6 @@ public class ChartFragment extends Fragment implements TrackDataListener {
|
||||
// We don't care.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHeadingChanged(double heading) {
|
||||
// We don't care.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSelectedTrackChanged(Track track) {
|
||||
// We don't care.
|
||||
|
||||
@@ -441,11 +441,6 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
||||
updateCurrentLocation();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHeadingChanged(double heading) {
|
||||
// We don't care.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSelectedTrackChanged(final Track track) {
|
||||
|
||||
@@ -132,11 +132,6 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHeadingChanged(double heading) {
|
||||
// We don't care.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSelectedTrackChanged(Track track) {
|
||||
if (isResumed()) {
|
||||
|
||||
@@ -23,7 +23,6 @@ import static com.google.android.testing.mocking.AndroidMock.isA;
|
||||
import com.google.android.apps.mytracks.Constants;
|
||||
import com.google.android.apps.mytracks.TrackStubUtils;
|
||||
import com.google.android.apps.mytracks.content.DataSourceManager.CurrentLocationListener;
|
||||
import com.google.android.apps.mytracks.content.DataSourceManager.HeadingListener;
|
||||
import com.google.android.apps.mytracks.content.MyTracksProviderUtils.LocationFactory;
|
||||
import com.google.android.apps.mytracks.content.MyTracksProviderUtils.LocationIterator;
|
||||
import com.google.android.apps.mytracks.content.TrackDataListener.LocationState;
|
||||
@@ -39,8 +38,6 @@ import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.database.ContentObserver;
|
||||
import android.database.Cursor;
|
||||
import android.database.MatrixCursor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.provider.BaseColumns;
|
||||
@@ -48,7 +45,6 @@ import android.test.AndroidTestCase;
|
||||
import android.test.RenamingDelegatingContext;
|
||||
import android.test.mock.MockContentResolver;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@@ -123,7 +119,6 @@ public class TrackDataHubTest extends AndroidTestCase {
|
||||
dataSource.unregisterContentObserver(isA(ContentObserver.class));
|
||||
AndroidMock.expectLastCall().times(3);
|
||||
dataSource.unregisterLocationListener(isA(LocationListener.class));
|
||||
dataSource.unregisterHeadingListener(isA(SensorEventListener.class));
|
||||
dataSource.unregisterOnSharedPreferenceChangeListener(
|
||||
isA(OnSharedPreferenceChangeListener.class));
|
||||
dataSource.close();
|
||||
@@ -537,74 +532,6 @@ public class TrackDataHubTest extends AndroidTestCase {
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests headings change.
|
||||
*/
|
||||
public void testHeadingsChange() throws Exception {
|
||||
dataSource.registerOnSharedPreferenceChangeListener(capture(preferenceChangeListenerCapture));
|
||||
Capture<SensorEventListener> sensorEventListenerCapture = new Capture<SensorEventListener>();
|
||||
dataSource.registerHeadingListener(capture(sensorEventListenerCapture));
|
||||
Capture<LocationListener> locationListenerCapture = new Capture<LocationListener>();
|
||||
dataSource.registerLocationListener(capture(locationListenerCapture));
|
||||
|
||||
SensorEvent event = newSensorEvent();
|
||||
|
||||
// Expect location state changed
|
||||
trackDataListener1.onLocationStateChanged(isA(LocationState.class));
|
||||
AndroidMock.expectLastCall().anyTimes();
|
||||
|
||||
// First, get a dummy heading update
|
||||
trackDataListener1.onHeadingChanged(0.0);
|
||||
|
||||
// Second, get a heading update without known location
|
||||
trackDataListener1.onHeadingChanged(42.0f);
|
||||
replay();
|
||||
|
||||
// Register one listener and update heading value
|
||||
trackDataHub.start();
|
||||
trackDataHub.registerTrackDataListener(
|
||||
trackDataListener1, EnumSet.of(TrackDataType.HEADING, TrackDataType.LOCATION));
|
||||
SensorEventListener sensorListener = sensorEventListenerCapture.getValue();
|
||||
LocationListener locationListener = locationListenerCapture.getValue();
|
||||
event.values[0] = 42.0f;
|
||||
sensorListener.onSensorChanged(event);
|
||||
verifyAndReset();
|
||||
|
||||
// Expect location state changed
|
||||
trackDataListener1.onLocationStateChanged(isA(LocationState.class));
|
||||
AndroidMock.expectLastCall().anyTimes();
|
||||
|
||||
// Expect location changed
|
||||
trackDataListener1.onLocationChanged(isA(Location.class));
|
||||
AndroidMock.expectLastCall().anyTimes();
|
||||
|
||||
// Expect a heading update with declination
|
||||
trackDataListener1.onHeadingChanged(52.0);
|
||||
AndroidMock.expect(dataSource.isAllowed()).andReturn(true);
|
||||
replay();
|
||||
|
||||
// Update location and sensor
|
||||
Location location = new Location("gps");
|
||||
location.setLatitude(10.0);
|
||||
location.setLongitude(20.0);
|
||||
location.setAltitude(30.0);
|
||||
declination = 10.0f;
|
||||
locationListener.onLocationChanged(location);
|
||||
sensorListener.onSensorChanged(event);
|
||||
verifyAndReset();
|
||||
|
||||
trackDataListener1.onHeadingChanged(52.0);
|
||||
replay();
|
||||
|
||||
/*
|
||||
* Change declination. Should still get the old value since the declination
|
||||
* is only updated once an hour.
|
||||
*/
|
||||
declination = 20.0f;
|
||||
sensorListener.onSensorChanged(event);
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests preferences change.
|
||||
*/
|
||||
@@ -656,15 +583,6 @@ public class TrackDataHubTest extends AndroidTestCase {
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new sensor event.
|
||||
*/
|
||||
private SensorEvent newSensorEvent() throws Exception {
|
||||
Constructor<SensorEvent> constructor = SensorEvent.class.getDeclaredConstructor(int.class);
|
||||
constructor.setAccessible(true);
|
||||
return constructor.newInstance(3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replays mocks.
|
||||
*/
|
||||
@@ -881,21 +799,6 @@ public class TrackDataHubTest extends AndroidTestCase {
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the method {@link TrackDataHub#start()}. This method would also cover
|
||||
* some logic of {@link TrackDataHub#notifyHeadingChanged(float)}.
|
||||
*/
|
||||
public void testRegisterHeadingListener() {
|
||||
dataSource.registerOnSharedPreferenceChangeListener(capture(preferenceChangeListenerCapture));
|
||||
Capture<HeadingListener> compassListener = new Capture<HeadingListener>();
|
||||
dataSource.registerHeadingListener(capture(compassListener));
|
||||
trackDataListener1.onHeadingChanged(capture(new Capture<Float>()));
|
||||
replay();
|
||||
trackDataHub.start();
|
||||
trackDataHub.registerTrackDataListener(trackDataListener1, EnumSet.of(TrackDataType.HEADING));
|
||||
verifyAndReset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the method {@link TrackDataHub#notifyPreferenceChanged(String)} when
|
||||
* the key is R.string.min_required_accuracy_key.
|
||||
|
||||
Reference in New Issue
Block a user