Addressed Sandor's comments.

This commit is contained in:
Bartlomiej Niechwiej
2010-10-25 22:35:38 -07:00
parent 48d8aa4cd8
commit fa7ef68851
3 changed files with 39 additions and 38 deletions
@@ -313,9 +313,6 @@ public class MyTracks extends TabActivity implements OnTouchListener,
SharedPreferences prefs =
getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
if (prefs != null) {
// Dump all preferences.
Log.i(MyTracksConstants.TAG, "Shared preferences:" + prefs.getAll());
selectedTrackId =
prefs.getLong(getString(R.string.selected_track_key), -1);
recordingTrackId =
@@ -118,7 +118,7 @@ public class MyTracksMap extends MapActivity
private double variation;
/**
* From the shared preferences:
* From the shared preferences.
*/
private int minRequiredAccuracy =
MyTracksSettings.DEFAULT_MIN_REQUIRED_ACCURACY;
@@ -135,8 +135,8 @@ public class MyTracksMap extends MapActivity
private Location currentLocation;
/**
* A thread with a looper. Post to updateTrackHandler to execute Runnables on
* this thread.
* A thread with a looper. Post to updateTrackHandler to execute
* {@link Runnable}s on this thread.
*/
private HandlerThread updateTrackThread;
@@ -146,6 +146,8 @@ public class MyTracksMap extends MapActivity
private Handler updateTrackHandler;
private MyTracksProviderUtils providerUtils;
private SharedPreferences sharedPreferences;
/**
* A runnable that updates the track from the provider (looking for points
@@ -331,7 +333,6 @@ public class MyTracksMap extends MapActivity
@Override
public void onChange(boolean selfChange) {
Log.d(MyTracksConstants.TAG, "MyTracksMap: ContentObserver.onChange");
if (!isRecordingSelected()) {
// No track, or one other than the recording track is selected,
// don't bother.
@@ -356,32 +357,26 @@ public class MyTracksMap extends MapActivity
}
};
// Read shared preferences and register change listener:
SharedPreferences preferences =
getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
if (preferences != null) {
minRequiredAccuracy = preferences.getInt(
getString(R.string.min_required_accuracy_key),
MyTracksSettings.DEFAULT_MIN_REQUIRED_ACCURACY);
recordingTrackId =
preferences.getLong(getString(R.string.recording_track_key), -1);
long selectedTrackId =
preferences.getLong(getString(R.string.selected_track_key), -1);
if (selectedTrackId >= 0) {
setSelectedTrack(selectedTrackId);
}
// Read shared preferences and register change listener.
sharedPreferences = getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
if (sharedPreferences != null) {
reloadSharedPreferences(sharedPreferences, null);
updateOptionsButton();
preferences.registerOnSharedPreferenceChangeListener(this);
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
}
@Override
protected void onDestroy() {
Log.d(MyTracksConstants.TAG, "MyTracksMap.onDestroy");
if (updateTrackThread != null) {
ApiFeatures.getInstance().getApiPlatformAdapter().stopHandlerThread(
updateTrackThread);
}
if (sharedPreferences != null) {
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
}
super.onDestroy();
}
@@ -447,9 +442,8 @@ public class MyTracksMap extends MapActivity
Log.d(MyTracksConstants.TAG, "MyTracksMap.onResume");
super.onResume();
// Reload all preferences as they might have changed meanwhile.
reloadSharedPreferences(
getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0), null);
// Reload all preferences as they might have changed since last run.
reloadSharedPreferences(sharedPreferences, null);
// Make sure any updates that might have happened are propagated to the
// Map overlay:
@@ -707,11 +701,14 @@ public class MyTracksMap extends MapActivity
* @param trackId a given track id
*/
public void setSelectedTrack(final long trackId) {
Log.d(MyTracksConstants.TAG, "MyTracksMap.setSelectedTrack: "
+ "id = " + trackId + ", selectedTrackId = " + selectedTrackId);
if (selectedTrackId == trackId) {
// Selected track did not change, nothing to do.
mapOverlay.setTrackDrawingEnabled(isATrackSelected());
mapView.invalidate();
updateOptionsButton();
mapView.invalidate();
return;
}
@@ -906,6 +903,8 @@ public class MyTracksMap extends MapActivity
@Override
public void onSharedPreferenceChanged(
final SharedPreferences sharedPreferences, final String key) {
Log.d(MyTracksConstants.TAG,
"MyTracksMap.onSharedPreferenceChanged: " + key);
if (key != null) {
uiHandler.post(new Runnable() {
@Override
@@ -998,11 +997,6 @@ public class MyTracksMap extends MapActivity
getString(R.string.min_required_accuracy_key),
MyTracksSettings.DEFAULT_MIN_REQUIRED_ACCURACY);
}
if (key == null || key.equals(getString(R.string.selected_track_key))) {
long selectedTrackId = sharedPreferences.getLong(
getString(R.string.selected_track_key), -1);
setSelectedTrack(selectedTrackId);
}
if (key == null || key.equals(getString(R.string.recording_track_key))) {
recordingTrackId = sharedPreferences.getLong(
getString(R.string.recording_track_key), -1);
@@ -1011,6 +1005,10 @@ public class MyTracksMap extends MapActivity
mapView.postInvalidate();
}
}
if (key == null || key.equals(getString(R.string.selected_track_key))) {
setSelectedTrack(sharedPreferences.getLong(
getString(R.string.selected_track_key), -1));
}
}
private void readAllNewTrackPoints() {
@@ -135,12 +135,13 @@ public class TrackRecordingServiceTest
// Clear the number of attempts and set the timeout to 10 min.
updateAutoResumePrefs(0, 10);
// Resume the service. It should resume recording of the previous track.
// Start the service in "resume" mode (simulates the on-reboot action).
Intent startIntent = createStartIntent();
startIntent.putExtra(RESUME_TRACK_EXTRA_NAME, true);
startService(startIntent);
assertNotNull(getService());
// We expect to resume the previous track.
assertTrue(getService().isRecording());
ITrackRecordingService service = bindAndGetService(createStartIntent());
assertEquals(123, service.getRecordingTrackId());
@@ -160,13 +161,14 @@ public class TrackRecordingServiceTest
assertEquals(id, sharedPreferences.getLong(
context.getString(R.string.recording_track_key), -1));
// Start the service in "resume" mode (simulates the on-reboot action).
Intent startIntent = createStartIntent();
startIntent.putExtra(RESUME_TRACK_EXTRA_NAME, true);
startService(startIntent);
assertNotNull(getService());
// TODO: shutdownService() has a bug and doesn't set mServiceCreated
// to false, thus preventing from second call to onCreate().
// to false, thus preventing from a second call to onCreate().
// Report the bug to Android team. Until then, the following check
// must be commented out.
@@ -181,12 +183,13 @@ public class TrackRecordingServiceTest
// Clear the number of attempts and set the timeout to 10 min.
updateAutoResumePrefs(0, 10);
// Resume the service. It should resume recording of the previous track.
// Start the service in "resume" mode (simulates the on-reboot action).
Intent startIntent = createStartIntent();
startIntent.putExtra(RESUME_TRACK_EXTRA_NAME, true);
startService(startIntent);
assertNotNull(getService());
// We don't expect to resume the previous track, because it was stopped.
assertFalse(getService().isRecording());
ITrackRecordingService service = bindAndGetService(createStartIntent());
assertEquals(-1, service.getRecordingTrackId());
@@ -200,12 +203,13 @@ public class TrackRecordingServiceTest
// Clear the number of attempts and set the timeout to 10 min.
updateAutoResumePrefs(0, 10);
// Resume the service. It should resume recording of the previous track.
// Start the service in "resume" mode (simulates the on-reboot action).
Intent startIntent = createStartIntent();
startIntent.putExtra(RESUME_TRACK_EXTRA_NAME, true);
startService(startIntent);
assertNotNull(getService());
// We don't expect to resume the previous track, because it has expired.
assertFalse(getService().isRecording());
ITrackRecordingService service = bindAndGetService(createStartIntent());
assertEquals(-1, service.getRecordingTrackId());
@@ -220,12 +224,14 @@ public class TrackRecordingServiceTest
updateAutoResumePrefs(
TrackRecordingService.MAX_AUTO_RESUME_TRACK_RETRY_ATTEMPTS, 10);
// Resume the service. It should resume recording of the previous track.
// Start the service in "resume" mode (simulates the on-reboot action).
Intent startIntent = createStartIntent();
startIntent.putExtra(RESUME_TRACK_EXTRA_NAME, true);
startService(startIntent);
assertNotNull(getService());
// We don't expect to resume the previous track, because there were already
// too many attempts.
assertFalse(getService().isRecording());
ITrackRecordingService service = bindAndGetService(createStartIntent());
assertEquals(-1, service.getRecordingTrackId());