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() {
instance = null;
}
// @VisibleForTesting
ITrackRecordingService getTrackRecordingService() {
return trackRecordingService;
}
}
@@ -42,13 +42,9 @@ public class PreferenceManager {
private final String signalSamplingFrequencyKey;
private final String splitFrequencyKey;
private final SharedPreferences sharedPreferences;
public PreferenceManager(TrackRecordingService service) {
this.service = service;
this.sharedPreferences =
service.getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
if (sharedPreferences == null) {
if (getSharedPreferences() == null) {
Log.w(MyTracksConstants.TAG,
"TrackRecordingService: 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)
*/
public void onSharedPreferenceChanged(String key) {
SharedPreferences sharedPreferences = getSharedPreferences();
if (key == null || key.equals(minRecordingDistanceKey)) {
service.setMinRecordingDistance(
sharedPreferences.getInt(
@@ -163,14 +160,18 @@ public class PreferenceManager {
}
public void setAutoResumeTrackCurrentRetry(int retryAttempts) {
SharedPreferences.Editor editor = sharedPreferences.edit();
SharedPreferences.Editor editor = getSharedPreferences().edit();
editor.putInt(autoResumeTrackCurrentRetryKey, retryAttempts);
editor.commit();
}
public void setRecordingTrack(long id) {
Editor editor = sharedPreferences.edit();
Editor editor = getSharedPreferences().edit();
editor.putLong(recordingTrackKey, id);
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
*/
public void setSplitFrequency(int splitFrequency) {
Log.d(MyTracksConstants.TAG,
"setSplitFrequency: splitFrequency = " + splitFrequency);
this.splitFrequency = splitFrequency;
// TODO: Decouple service from this class once and forever.
@@ -34,7 +34,7 @@ import android.util.Log;
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
*/
@@ -61,7 +61,7 @@ public class StatusAnnouncerTask implements PeriodicTask {
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;
@@ -166,17 +166,17 @@ public class TrackRecordingService extends Service implements LocationListener {
/**
* 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
*/
private Location lastLocation = null;
private Location lastLocation;
/**
* Last valid location (i.e. not a marker) that was recorded.
*/
private Location lastValidLocation = null;
private Location lastValidLocation;
/**
* 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)
*/
public void onSharedPreferenceChanged(String key) {
public void onSharedPreferenceChanged(final String key) {
Log.d(MyTracksConstants.TAG,
"TrackRecordingService.onSharedPreferenceChanged");
prefManager.onSharedPreferenceChanged(key);
handler.post(new Runnable() {
@Override
public void run() {
prefManager.onSharedPreferenceChanged(key);
if (isRecording) {
registerLocationListener();
}
if (isRecording) {
registerLocationListener();
}
}
});
}
/*
@@ -682,15 +687,23 @@ public class TrackRecordingService extends Service implements LocationListener {
Log.d(MyTracksConstants.TAG, "TrackRecordingService.setUpAnnouncer: "
+ announcementExecuter);
if (announcementFrequency != -1 && recordingTrackId != -1) {
if (announcementExecuter == null) {
StatusAnnouncerFactory statusAnnouncerFactory =
new StatusAnnouncerFactory(ApiFeatures.getInstance());
PeriodicTask announcer = statusAnnouncerFactory.create(this);
if (announcer == null) return;
announcementExecuter = new PeriodicTaskExecuter(announcer, this);
}
announcementExecuter.scheduleTask(announcementFrequency * 60000);
handler.post(new Runnable() {
@Override
public void run() {
if (announcementExecuter == null) {
StatusAnnouncerFactory statusAnnouncerFactory =
new StatusAnnouncerFactory(ApiFeatures.getInstance());
PeriodicTask announcer = statusAnnouncerFactory.create(
TrackRecordingService.this);
if (announcer == null) {
return;
}
announcementExecuter = new PeriodicTaskExecuter(announcer,
TrackRecordingService.this);
}
announcementExecuter.scheduleTask(announcementFrequency * 60000);
}
});
}
}