Various fixes to shared preferences.

This commit is contained in:
Bartlomiej Niechwiej
2010-11-15 23:09:18 -08:00
parent 5b65c216b6
commit a0da23f86a
6 changed files with 142 additions and 27 deletions
@@ -1343,4 +1343,9 @@ public class MyTracks extends TabActivity implements OnTouchListener,
static void clearInstance() { static void clearInstance() {
instance = null; instance = null;
} }
// @VisibleForTesting
ITrackRecordingService getTrackRecordingService() {
return trackRecordingService;
}
} }
@@ -42,13 +42,9 @@ public class PreferenceManager {
private final String signalSamplingFrequencyKey; private final String signalSamplingFrequencyKey;
private final String splitFrequencyKey; private final String splitFrequencyKey;
private final SharedPreferences sharedPreferences;
public PreferenceManager(TrackRecordingService service) { public PreferenceManager(TrackRecordingService service) {
this.service = service; this.service = service;
this.sharedPreferences = if (getSharedPreferences() == null) {
service.getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
if (sharedPreferences == null) {
Log.w(MyTracksConstants.TAG, Log.w(MyTracksConstants.TAG,
"TrackRecordingService: Couldn't get shared preferences."); "TrackRecordingService: Couldn't get shared preferences.");
throw new IllegalStateException("Couldn't get shared preferences"); throw new IllegalStateException("Couldn't get shared preferences");
@@ -85,6 +81,7 @@ public class PreferenceManager {
* @param key the key that changed (may be null to update all preferences) * @param key the key that changed (may be null to update all preferences)
*/ */
public void onSharedPreferenceChanged(String key) { public void onSharedPreferenceChanged(String key) {
SharedPreferences sharedPreferences = getSharedPreferences();
if (key == null || key.equals(minRecordingDistanceKey)) { if (key == null || key.equals(minRecordingDistanceKey)) {
service.setMinRecordingDistance( service.setMinRecordingDistance(
sharedPreferences.getInt( sharedPreferences.getInt(
@@ -163,14 +160,18 @@ public class PreferenceManager {
} }
public void setAutoResumeTrackCurrentRetry(int retryAttempts) { public void setAutoResumeTrackCurrentRetry(int retryAttempts) {
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = getSharedPreferences().edit();
editor.putInt(autoResumeTrackCurrentRetryKey, retryAttempts); editor.putInt(autoResumeTrackCurrentRetryKey, retryAttempts);
editor.commit(); editor.commit();
} }
public void setRecordingTrack(long id) { public void setRecordingTrack(long id) {
Editor editor = sharedPreferences.edit(); Editor editor = getSharedPreferences().edit();
editor.putLong(recordingTrackKey, id); editor.putLong(recordingTrackKey, id);
editor.commit(); editor.commit();
} }
private SharedPreferences getSharedPreferences() {
return service.getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
}
} }
@@ -125,6 +125,8 @@ public class SplitManager {
* @param splitFrequency The frequency in time or distance * @param splitFrequency The frequency in time or distance
*/ */
public void setSplitFrequency(int splitFrequency) { public void setSplitFrequency(int splitFrequency) {
Log.d(MyTracksConstants.TAG,
"setSplitFrequency: splitFrequency = " + splitFrequency);
this.splitFrequency = splitFrequency; this.splitFrequency = splitFrequency;
// TODO: Decouple service from this class once and forever. // TODO: Decouple service from this class once and forever.
@@ -34,7 +34,7 @@ import android.util.Log;
import java.util.Locale; import java.util.Locale;
/** /**
* This class will periodically announce the user's trip statitics. * This class will periodically announce the user's trip statistics.
* *
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
@@ -61,7 +61,7 @@ public class StatusAnnouncerTask implements PeriodicTask {
private TextToSpeech tts; private TextToSpeech tts;
/** /**
* The response recieved from the TTS engine ater initialization. * The response received from the TTS engine after initialization.
*/ */
private boolean ready = false; private boolean ready = false;
@@ -166,17 +166,17 @@ public class TrackRecordingService extends Service implements LocationListener {
/** /**
* Is the service currently recording a track? * Is the service currently recording a track?
*/ */
private boolean isRecording = false; private boolean isRecording;
/** /**
* Last good location the service has received from the location listener * Last good location the service has received from the location listener
*/ */
private Location lastLocation = null; private Location lastLocation;
/** /**
* Last valid location (i.e. not a marker) that was recorded. * Last valid location (i.e. not a marker) that was recorded.
*/ */
private Location lastValidLocation = null; private Location lastValidLocation;
/** /**
* The frequency of status announcements. * The frequency of status announcements.
@@ -617,14 +617,19 @@ public class TrackRecordingService extends Service implements LocationListener {
* *
* @param key the key that changed (may be null to update all preferences) * @param key the key that changed (may be null to update all preferences)
*/ */
public void onSharedPreferenceChanged(String key) { public void onSharedPreferenceChanged(final String key) {
Log.d(MyTracksConstants.TAG, Log.d(MyTracksConstants.TAG,
"TrackRecordingService.onSharedPreferenceChanged"); "TrackRecordingService.onSharedPreferenceChanged");
prefManager.onSharedPreferenceChanged(key); handler.post(new Runnable() {
@Override
public void run() {
prefManager.onSharedPreferenceChanged(key);
if (isRecording) { if (isRecording) {
registerLocationListener(); registerLocationListener();
} }
}
});
} }
/* /*
@@ -682,15 +687,23 @@ public class TrackRecordingService extends Service implements LocationListener {
Log.d(MyTracksConstants.TAG, "TrackRecordingService.setUpAnnouncer: " Log.d(MyTracksConstants.TAG, "TrackRecordingService.setUpAnnouncer: "
+ announcementExecuter); + announcementExecuter);
if (announcementFrequency != -1 && recordingTrackId != -1) { if (announcementFrequency != -1 && recordingTrackId != -1) {
if (announcementExecuter == null) { handler.post(new Runnable() {
StatusAnnouncerFactory statusAnnouncerFactory = @Override
new StatusAnnouncerFactory(ApiFeatures.getInstance()); public void run() {
PeriodicTask announcer = statusAnnouncerFactory.create(this); if (announcementExecuter == null) {
if (announcer == null) return; StatusAnnouncerFactory statusAnnouncerFactory =
new StatusAnnouncerFactory(ApiFeatures.getInstance());
announcementExecuter = new PeriodicTaskExecuter(announcer, this); PeriodicTask announcer = statusAnnouncerFactory.create(
} TrackRecordingService.this);
announcementExecuter.scheduleTask(announcementFrequency * 60000); if (announcer == null) {
return;
}
announcementExecuter = new PeriodicTaskExecuter(announcer,
TrackRecordingService.this);
}
announcementExecuter.scheduleTask(announcementFrequency * 60000);
}
});
} }
} }
@@ -15,13 +15,17 @@
*/ */
package com.google.android.apps.mytracks; package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.services.ITrackRecordingService;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.app.Instrumentation.ActivityMonitor;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.net.Uri; import android.net.Uri;
import android.test.ActivityInstrumentationTestCase2; import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
import java.io.File; import java.io.File;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@@ -47,6 +51,7 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
@Override @Override
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
clearSelectedAndRecordingTracks(); clearSelectedAndRecordingTracks();
waitForIdle();
super.tearDown(); super.tearDown();
} }
@@ -138,6 +143,7 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
// Check if not recording. // Check if not recording.
clearSelectedAndRecordingTracks(); clearSelectedAndRecordingTracks();
waitForIdle(); waitForIdle();
assertFalse(getActivity().isRecording()); assertFalse(getActivity().isRecording());
assertEquals(-1, getActivity().getRecordingTrackId()); assertEquals(-1, getActivity().getRecordingTrackId());
long selectedTrackId = getActivity().getSharedPreferences().getLong( long selectedTrackId = getActivity().getSharedPreferences().getLong(
@@ -158,9 +164,96 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
assertEquals(recordingTrackId, selectedTrackId); assertEquals(recordingTrackId, selectedTrackId);
assertEquals(selectedTrackId, getActivity().getSelectedTrackId()); assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
// Watch for MyTracksDetails activity.
ActivityMonitor monitor = getInstrumentation().addMonitor(
MyTracksDetails.class.getName(), null, false);
// Now, stop the track and make sure that it is still selected, but // Now, stop the track and make sure that it is still selected, but
// no longer recording. // no longer recording.
getActivity().stopRecording(); getActivity().stopRecording();
// Check if we got back MyTracksDetails activity.
Activity activity = getInstrumentation().waitForMonitor(monitor);
assertTrue(activity instanceof MyTracksDetails);
// TODO: Update track name and other properties and test if they were
// properly saved.
// Simulate a click on Save button.
Button save = (Button) activity.findViewById(R.id.trackdetails_save);
save.performClick();
// Check the remaining properties.
recordingTrackId = awaitRecordingStatus(5000, false);
assertEquals(-1, recordingTrackId);
assertEquals(recordingTrackId, getActivity().getRecordingTrackId());
assertEquals(recordingTrackId, getActivity().getSharedPreferences().getLong(
getActivity().getString(R.string.recording_track_key), -1));
// Make sure this is the same track as the last recording track ID.
assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
}
public void testRecording_changePreferences() throws Exception {
// Make sure we can start MyTracks and the activity doesn't start recording.
assertNotNull(getActivity());
assertNotNull(MyTracks.getInstance());
assertNotNull(getActivity().getSharedPreferences());
// Check if not recording.
clearSelectedAndRecordingTracks();
waitForIdle();
assertFalse(getActivity().isRecording());
assertEquals(-1, getActivity().getRecordingTrackId());
long selectedTrackId = getActivity().getSharedPreferences().getLong(
getActivity().getString(R.string.selected_track_key), -1);
assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
// Start a new track.
getActivity().startRecording();
long recordingTrackId = awaitRecordingStatus(5000, true);
assertTrue(recordingTrackId >= 0);
// Wait until we are done and make sure that selectedTrack = recordingTrack.
waitForIdle();
assertEquals(recordingTrackId, getActivity().getSharedPreferences().getLong(
getActivity().getString(R.string.recording_track_key), -1));
selectedTrackId = getActivity().getSharedPreferences().getLong(
getActivity().getString(R.string.selected_track_key), -1);
assertEquals(recordingTrackId, selectedTrackId);
assertEquals(selectedTrackId, getActivity().getSelectedTrackId());
// Change shared preferences and observe if the service notices the change.
Editor editor = getActivity().getSharedPreferences().edit();
editor.putInt(getActivity().getString(R.string.announcement_frequency_key),
1);
editor.putInt(getActivity().getString(R.string.split_frequency_key), 1);
editor.putInt(
getActivity().getString(R.string.signal_sampling_frequency_key), 1);
editor.commit();
// Notify the service about changed preferences.
ITrackRecordingService service = getActivity().getTrackRecordingService();
assertNotNull(service);
service.sharedPreferenceChanged(null);
// TODO: Test if the service has updated its preferences.
// Watch for MyTracksDetails activity.
ActivityMonitor monitor = getInstrumentation().addMonitor(
MyTracksDetails.class.getName(), null, false);
// Now, stop the track and make sure that it is still selected, but
// no longer recording.
getActivity().stopRecording();
// Check if we got back MyTracksDetails activity.
Activity activity = getInstrumentation().waitForMonitor(monitor);
assertTrue(activity instanceof MyTracksDetails);
// Simulate a click on Save button.
Button save = (Button) activity.findViewById(R.id.trackdetails_save);
save.performClick();
// Check if after stopping the service all properties are up to date.
recordingTrackId = awaitRecordingStatus(5000, false); recordingTrackId = awaitRecordingStatus(5000, false);
assertEquals(-1, recordingTrackId); assertEquals(-1, recordingTrackId);
assertEquals(recordingTrackId, getActivity().getRecordingTrackId()); assertEquals(recordingTrackId, getActivity().getRecordingTrackId());
@@ -197,10 +290,11 @@ public class MyTracksTest extends ActivityInstrumentationTestCase2<MyTracks>{
* Clears {selected,recording}TrackId in the {@link SharedPreferences}. * Clears {selected,recording}TrackId in the {@link SharedPreferences}.
*/ */
private void clearSelectedAndRecordingTracks() { private void clearSelectedAndRecordingTracks() {
// TODO: Consider clearing all preferences.
Editor editor = getActivity().getSharedPreferences().edit(); Editor editor = getActivity().getSharedPreferences().edit();
editor.putLong(getActivity().getString(R.string.selected_track_key), -1); editor.putLong(getActivity().getString(R.string.selected_track_key), -1);
editor.putLong(getActivity().getString(R.string.recording_track_key), -1); editor.putLong(getActivity().getString(R.string.recording_track_key), -1);
editor.clear();
editor.commit(); editor.commit();
} }