Fix NPE in test execution.

This commit is contained in:
Matthew Simmons
2011-02-28 17:17:35 -05:00
parent b8f40e8314
commit b95a696a5a
@@ -23,12 +23,12 @@ import com.google.android.apps.mytracks.MyTracksSettings;
import com.google.android.apps.mytracks.content.MyTracksLocation; import com.google.android.apps.mytracks.content.MyTracksLocation;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils; import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Sensor; import com.google.android.apps.mytracks.content.Sensor;
import com.google.android.apps.mytracks.content.Sensor.SensorDataSet;
import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.TracksColumns; import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.content.Waypoint; import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.content.WaypointCreationRequest; import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.content.WaypointsColumns; import com.google.android.apps.mytracks.content.WaypointsColumns;
import com.google.android.apps.mytracks.content.Sensor.SensorDataSet;
import com.google.android.apps.mytracks.services.sensors.SensorManager; import com.google.android.apps.mytracks.services.sensors.SensorManager;
import com.google.android.apps.mytracks.services.sensors.SensorManagerFactory; import com.google.android.apps.mytracks.services.sensors.SensorManagerFactory;
import com.google.android.apps.mytracks.stats.TripStatistics; import com.google.android.apps.mytracks.stats.TripStatistics;
@@ -86,8 +86,8 @@ public class TrackRecordingService extends Service implements LocationListener {
private int minRequiredAccuracy = private int minRequiredAccuracy =
MyTracksSettings.DEFAULT_MIN_REQUIRED_ACCURACY; MyTracksSettings.DEFAULT_MIN_REQUIRED_ACCURACY;
private int autoResumeTrackTimeout = private int autoResumeTrackTimeout =
MyTracksSettings.DEFAULT_AUTO_RESUME_TRACK_TIMEOUT; MyTracksSettings.DEFAULT_AUTO_RESUME_TRACK_TIMEOUT;
private long recordingTrackId = -1; private long recordingTrackId = -1;
private long currentWaypointId = -1; private long currentWaypointId = -1;
@@ -117,9 +117,9 @@ public class TrackRecordingService extends Service implements LocationListener {
private SplitManager splitManager; private SplitManager splitManager;
private SensorManager sensorManager; private SensorManager sensorManager;
private PreferenceManager prefManager; private PreferenceManager prefManager;
/** /**
* The interval in milliseconds that we have requested to be notified of gps * The interval in milliseconds that we have requested to be notified of gps
* readings. * readings.
@@ -168,7 +168,7 @@ public class TrackRecordingService extends Service implements LocationListener {
* The most recent recording track. * The most recent recording track.
*/ */
private Track recordingTrack; private Track recordingTrack;
/** /**
* Is the service currently recording a track? * Is the service currently recording a track?
*/ */
@@ -409,7 +409,7 @@ public class TrackRecordingService extends Service implements LocationListener {
Log.d(MyTracksConstants.TAG, Log.d(MyTracksConstants.TAG,
"Location listener now unregistered w/ TrackRecordingService."); "Location listener now unregistered w/ TrackRecordingService.");
} }
private Track getRecordingTrack() { private Track getRecordingTrack() {
if (recordingTrackId < 0) { if (recordingTrackId < 0) {
return null; return null;
@@ -421,7 +421,7 @@ public class TrackRecordingService extends Service implements LocationListener {
private void restoreStats(Track track) { private void restoreStats(Track track) {
Log.d(MyTracksConstants.TAG, Log.d(MyTracksConstants.TAG,
"Restoring stats of track with ID: " + track.getId()); "Restoring stats of track with ID: " + track.getId());
TripStatistics stats = track.getStatistics(); TripStatistics stats = track.getStatistics();
statsBuilder = new TripStatisticsBuilder(stats.getStartTime()); statsBuilder = new TripStatisticsBuilder(stats.getStartTime());
statsBuilder.setMinRecordingDistance(minRecordingDistance); statsBuilder.setMinRecordingDistance(minRecordingDistance);
@@ -721,7 +721,7 @@ public class TrackRecordingService extends Service implements LocationListener {
}); });
} }
} }
private void shutdownAnnouncer() { private void shutdownAnnouncer() {
Log.d(MyTracksConstants.TAG, "TrackRecordingService.shutdownAnnouncer: " Log.d(MyTracksConstants.TAG, "TrackRecordingService.shutdownAnnouncer: "
+ announcementExecuter); + announcementExecuter);
@@ -764,7 +764,7 @@ public class TrackRecordingService extends Service implements LocationListener {
// This should be the last operation. // This should be the last operation.
releaseWakeLock(); releaseWakeLock();
super.onDestroy(); super.onDestroy();
} }
@@ -786,7 +786,7 @@ public class TrackRecordingService extends Service implements LocationListener {
unregisterLocationListener(); unregisterLocationListener();
return super.stopService(name); return super.stopService(name);
} }
@Override @Override
public void onStart(Intent intent, int startId) { public void onStart(Intent intent, int startId) {
handleStartCommand(intent, startId); handleStartCommand(intent, startId);
@@ -806,36 +806,36 @@ public class TrackRecordingService extends Service implements LocationListener {
if (intent != null && if (intent != null &&
intent.getBooleanExtra(RESUME_TRACK_EXTRA_NAME, false)) { intent.getBooleanExtra(RESUME_TRACK_EXTRA_NAME, false)) {
Log.d(MyTracksConstants.TAG, "TrackRecordingService: requested resume"); Log.d(MyTracksConstants.TAG, "TrackRecordingService: requested resume");
// Make sure that the current track exists and is fresh enough. // Make sure that the current track exists and is fresh enough.
if (recordingTrack == null || !shouldResumeTrack(recordingTrack)) { if (recordingTrack == null || !shouldResumeTrack(recordingTrack)) {
Log.i(MyTracksConstants.TAG, Log.i(MyTracksConstants.TAG,
"TrackRecordingService: Not resuming, because the previous track (" "TrackRecordingService: Not resuming, because the previous track ("
+ recordingTrack + ") doesn't exist or is too old"); + recordingTrack + ") doesn't exist or is too old");
isRecording = false; isRecording = false;
prefManager.setRecordingTrack(recordingTrackId = -1); prefManager.setRecordingTrack(recordingTrackId = -1);
stopSelfResult(startId); stopSelfResult(startId);
return; return;
} }
Log.i(MyTracksConstants.TAG, "TrackRecordingService: resuming"); Log.i(MyTracksConstants.TAG, "TrackRecordingService: resuming");
} }
} }
private void setAutoResumeTrackRetries( private void setAutoResumeTrackRetries(
SharedPreferences sharedPreferences, int retryAttempts) { SharedPreferences sharedPreferences, int retryAttempts) {
Log.d(MyTracksConstants.TAG, Log.d(MyTracksConstants.TAG,
"Updating auto-resume retry attempts to: " + retryAttempts); "Updating auto-resume retry attempts to: " + retryAttempts);
prefManager.setAutoResumeTrackCurrentRetry(retryAttempts); prefManager.setAutoResumeTrackCurrentRetry(retryAttempts);
} }
private boolean shouldResumeTrack(Track track) { private boolean shouldResumeTrack(Track track) {
Log.d(MyTracksConstants.TAG, "shouldResumeTrack: autoResumeTrackTimeout = " Log.d(MyTracksConstants.TAG, "shouldResumeTrack: autoResumeTrackTimeout = "
+ autoResumeTrackTimeout); + autoResumeTrackTimeout);
// Check if we haven't exceeded the maximum number of retry attempts. // Check if we haven't exceeded the maximum number of retry attempts.
SharedPreferences sharedPreferences = SharedPreferences sharedPreferences =
getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0); getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
int retries = sharedPreferences.getInt( int retries = sharedPreferences.getInt(
getString(R.string.auto_resume_track_current_retry_key), 0); getString(R.string.auto_resume_track_current_retry_key), 0);
Log.d(MyTracksConstants.TAG, Log.d(MyTracksConstants.TAG,
@@ -853,7 +853,7 @@ public class TrackRecordingService extends Service implements LocationListener {
// Check for special cases. // Check for special cases.
if (autoResumeTrackTimeout == 0) { if (autoResumeTrackTimeout == 0) {
// Never resume. // Never resume.
Log.d(MyTracksConstants.TAG, Log.d(MyTracksConstants.TAG,
"shouldResumeTrack: Auto-resume disabled (never resume)"); "shouldResumeTrack: Auto-resume disabled (never resume)");
return false; return false;
@@ -871,7 +871,7 @@ public class TrackRecordingService extends Service implements LocationListener {
"shouldResumeTrack: lastModified = " + lastModified "shouldResumeTrack: lastModified = " + lastModified
+ ", autoResumeTrackTimeout: " + autoResumeTrackTimeout); + ", autoResumeTrackTimeout: " + autoResumeTrackTimeout);
return lastModified > 0 && System.currentTimeMillis() - lastModified <= return lastModified > 0 && System.currentTimeMillis() - lastModified <=
autoResumeTrackTimeout * 60 * 1000; autoResumeTrackTimeout * 60 * 1000;
} }
public boolean isRecording() { public boolean isRecording() {
@@ -907,8 +907,9 @@ public class TrackRecordingService extends Service implements LocationListener {
} else { } else {
// A null location is ok, and expected on track start. // A null location is ok, and expected on track start.
wpt.setLocation(lastLocation); wpt.setLocation(lastLocation);
wpt.setDuration(lastLocation.getTime() if (lastLocation != null) {
- statsBuilder.getStatistics().getStartTime()); wpt.setDuration(lastLocation.getTime() - statsBuilder.getStatistics().getStartTime());
}
} }
Uri uri = providerUtils.insertWaypoint(wpt); Uri uri = providerUtils.insertWaypoint(wpt);
return Long.parseLong(uri.getLastPathSegment()); return Long.parseLong(uri.getLastPathSegment());
@@ -961,27 +962,27 @@ public class TrackRecordingService extends Service implements LocationListener {
// Create a new stats keeper for the next marker. // Create a new stats keeper for the next marker.
waypointStatsBuilder = new TripStatisticsBuilder(time); waypointStatsBuilder = new TripStatisticsBuilder(time);
} }
private ServiceBinder binder = new ServiceBinder(this); private ServiceBinder binder = new ServiceBinder(this);
/** /**
* TODO: There is a bug in Android that leaks Binder instances. This bug is * TODO: There is a bug in Android that leaks Binder instances. This bug is
* especially visible if we have a non-static class, as there is no way to * especially visible if we have a non-static class, as there is no way to
* nullify reference to the outer class (the service). * nullify reference to the outer class (the service).
* A workaround is to use a static class and explicitly clear service * A workaround is to use a static class and explicitly clear service
* and detach it from the underlying Binder. With this approach, we minimize * and detach it from the underlying Binder. With this approach, we minimize
* the leak to 24 bytes per each service instance. * the leak to 24 bytes per each service instance.
* *
* For more details, see the following bug: * For more details, see the following bug:
* http://code.google.com/p/android/issues/detail?id=6426. * http://code.google.com/p/android/issues/detail?id=6426.
*/ */
private static class ServiceBinder extends ITrackRecordingService.Stub { private static class ServiceBinder extends ITrackRecordingService.Stub {
private TrackRecordingService service; private TrackRecordingService service;
public ServiceBinder(TrackRecordingService service) { public ServiceBinder(TrackRecordingService service) {
this.service = service; this.service = service;
} }
/** /**
* Clears the reference to the outer class to minimize the leak. * Clears the reference to the outer class to minimize the leak.
*/ */
@@ -989,7 +990,7 @@ public class TrackRecordingService extends Service implements LocationListener {
this.service = null; this.service = null;
attachInterface(null, null); attachInterface(null, null);
} }
@Override @Override
public boolean isRecording() { public boolean isRecording() {
checkService(); checkService();
@@ -1004,13 +1005,13 @@ public class TrackRecordingService extends Service implements LocationListener {
@Override @Override
public long getRecordingTrackId() { public long getRecordingTrackId() {
checkService(); checkService();
return service.recordingTrackId; return service.recordingTrackId;
} }
@Override @Override
public boolean hasRecorded() { public boolean hasRecorded() {
checkService(); checkService();
return service.providerUtils.getLastTrackId() >= 0; return service.providerUtils.getLastTrackId() >= 0;
} }
@@ -1048,13 +1049,13 @@ public class TrackRecordingService extends Service implements LocationListener {
@Override @Override
public void recordLocation(Location loc) { public void recordLocation(Location loc) {
checkService(); checkService();
service.onLocationChanged(loc); service.onLocationChanged(loc);
} }
@Override @Override
public byte[] getSensorData() { public byte[] getSensorData() {
checkService(); checkService();
if (service.sensorManager == null) { if (service.sensorManager == null) {
Log.d(MyTracksConstants.TAG, "No sensor manager for data."); Log.d(MyTracksConstants.TAG, "No sensor manager for data.");
return null; return null;
@@ -1068,7 +1069,7 @@ public class TrackRecordingService extends Service implements LocationListener {
@Override @Override
public int getSensorState() { public int getSensorState() {
checkService(); checkService();
if (service.sensorManager == null) { if (service.sensorManager == null) {
Log.d(MyTracksConstants.TAG, "No sensor manager for data."); Log.d(MyTracksConstants.TAG, "No sensor manager for data.");
return Sensor.SensorState.NONE.getNumber(); return Sensor.SensorState.NONE.getNumber();
@@ -1155,12 +1156,12 @@ public class TrackRecordingService extends Service implements LocationListener {
showNotification(); showNotification();
long recordedTrackId = recordingTrackId; long recordedTrackId = recordingTrackId;
prefManager.setRecordingTrack(recordingTrackId = -1); prefManager.setRecordingTrack(recordingTrackId = -1);
if (sensorManager != null) { if (sensorManager != null) {
sensorManager.shutdown(); sensorManager.shutdown();
sensorManager = null; sensorManager = null;
} }
releaseWakeLock(); releaseWakeLock();
// Notify the world that we're no longer recording. // Notify the world that we're no longer recording.
@@ -1242,11 +1243,11 @@ public class TrackRecordingService extends Service implements LocationListener {
LocationListenerPolicy locationListenerPolicy) { LocationListenerPolicy locationListenerPolicy) {
this.locationListenerPolicy = locationListenerPolicy; this.locationListenerPolicy = locationListenerPolicy;
} }
public int getAutoResumeTrackTimeout() { public int getAutoResumeTrackTimeout() {
return autoResumeTrackTimeout; return autoResumeTrackTimeout;
} }
public void setAutoResumeTrackTimeout(int autoResumeTrackTimeout) { public void setAutoResumeTrackTimeout(int autoResumeTrackTimeout) {
this.autoResumeTrackTimeout = autoResumeTrackTimeout; this.autoResumeTrackTimeout = autoResumeTrackTimeout;
} }