Make the app single process only, including the following fixes:

1) Simplify SharedPreferences management
2) Shut down signalManager/splitManager to avoid random crashes
3) Get rid of sharedPreferencesChanged from the service interface

All tests pass.
This commit is contained in:
Bartlomiej Niechwiej
2010-11-17 22:18:50 -08:00
parent 9cf51de3f7
commit 7a693a90f7
8 changed files with 58 additions and 219 deletions
@@ -20,7 +20,6 @@ import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.format.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -84,13 +84,4 @@ interface ITrackRecordingService {
* Deletes all the stored tracks.
*/
void deleteAllTracks();
/**
* Notifies the service that its preferences may have been changed.
* This is necessary because the service running on a separate process cannot
* listen to the changes itself.
*
* @param key the preference key which may have changed
*/
void sharedPreferenceChanged(in String key);
}
@@ -20,7 +20,7 @@ import com.google.android.apps.mytracks.MyTracksSettings;
import com.google.android.maps.mytracks.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.util.Log;
/**
@@ -28,8 +28,9 @@ import android.util.Log;
*
* @author Sandor Dornbush
*/
public class PreferenceManager {
public class PreferenceManager implements OnSharedPreferenceChangeListener {
private TrackRecordingService service;
private SharedPreferences sharedPreferences;
private final String announcementFrequencyKey;
private final String autoResumeTrackCurrentRetryKey;
private final String autoResumeTrackTimeoutKey;
@@ -44,11 +45,14 @@ public class PreferenceManager {
public PreferenceManager(TrackRecordingService service) {
this.service = service;
if (getSharedPreferences() == null) {
this.sharedPreferences = service.getSharedPreferences(
MyTracksSettings.SETTINGS_NAME, 0);
if (sharedPreferences == null) {
Log.w(MyTracksConstants.TAG,
"TrackRecordingService: Couldn't get shared preferences.");
throw new IllegalStateException("Couldn't get shared preferences");
}
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
announcementFrequencyKey =
service.getString(R.string.announcement_frequency_key);
@@ -72,6 +76,9 @@ public class PreferenceManager {
service.getString(R.string.signal_sampling_frequency_key);
splitFrequencyKey =
service.getString(R.string.split_frequency_key);
// Refresh all properties.
onSharedPreferenceChanged(sharedPreferences, null);
}
/**
@@ -80,8 +87,9 @@ public class PreferenceManager {
*
* @param key the key that changed (may be null to update all preferences)
*/
public void onSharedPreferenceChanged(String key) {
SharedPreferences sharedPreferences = getSharedPreferences();
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key == null || key.equals(minRecordingDistanceKey)) {
service.setMinRecordingDistance(
sharedPreferences.getInt(
@@ -160,18 +168,16 @@ public class PreferenceManager {
}
public void setAutoResumeTrackCurrentRetry(int retryAttempts) {
SharedPreferences.Editor editor = getSharedPreferences().edit();
editor.putInt(autoResumeTrackCurrentRetryKey, retryAttempts);
editor.commit();
sharedPreferences
.edit()
.putInt(autoResumeTrackCurrentRetryKey, retryAttempts)
.commit();
}
public void setRecordingTrack(long id) {
Editor editor = getSharedPreferences().edit();
editor.putLong(recordingTrackKey, id);
editor.commit();
}
private SharedPreferences getSharedPreferences() {
return service.getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
sharedPreferences
.edit()
.putLong(recordingTrackKey, id)
.commit();
}
}
@@ -15,10 +15,10 @@
*/
package com.google.android.apps.mytracks.services;
import android.util.Log;
import com.google.android.apps.mytracks.MyTracksConstants;
import android.util.Log;
/**
* This class will manage a period task executer.
*
@@ -26,13 +26,12 @@ import com.google.android.apps.mytracks.MyTracksConstants;
*/
public class TaskExecuterManager {
int frequency;
PeriodicTask task;
PeriodicTaskExecuter executer;
private int frequency;
private PeriodicTask task;
private PeriodicTaskExecuter executer;
public TaskExecuterManager(int frequency,
PeriodicTask task,
TrackRecordingService service) {
public TaskExecuterManager(int frequency, PeriodicTask task,
TrackRecordingService service) {
this.task = task;
setFrequency(frequency, service);
}
@@ -69,7 +68,7 @@ public class TaskExecuterManager {
}
/**
* Restore the task at the current frequency.
* Restores the task at the current frequency.
*/
public void restore() {
if (frequency > 0) {
@@ -77,4 +76,13 @@ public class TaskExecuterManager {
executer.scheduleTask(frequency * 60000);
}
}
/**
* Shuts down this executer.
*/
public void shutdown() {
if (executer != null) {
executer.shutdown();
}
}
}
@@ -103,7 +103,7 @@ public class TrackRecordingService extends Service implements LocationListener {
* recorded points (as compared to each location fix). It's used to overlay
* waypoints precisely in the elevation profile chart.
*/
private double length = 0;
private double length;
/**
* Status announcer executer.
@@ -118,7 +118,7 @@ public class TrackRecordingService extends Service implements LocationListener {
* The interval in milliseconds that we have requested to be notified of gps
* readings.
*/
private long currentRecordingInterval = 0;
private long currentRecordingInterval;
/**
* The policy used to decide how often we should request gps updates.
@@ -605,33 +605,6 @@ public class TrackRecordingService extends Service implements LocationListener {
// Do nothing
}
/*
* SharedPreferencesChangeListener interface implementation. Note that
* services don't currently receive this event (Android platform limitation).
* This should be called from an activity whenever settings change.
*/
/**
* Notifies that preferences have changed.
* Call this with key == null to update all preferences in one call.
*
* @param key the key that changed (may be null to update all preferences)
*/
public void onSharedPreferenceChanged(final String key) {
Log.d(MyTracksConstants.TAG,
"TrackRecordingService.onSharedPreferenceChanged");
handler.post(new Runnable() {
@Override
public void run() {
prefManager.onSharedPreferenceChanged(key);
if (isRecording) {
registerLocationListener();
}
}
});
}
/*
* Application lifetime events: ============================
*/
@@ -652,7 +625,6 @@ public class TrackRecordingService extends Service implements LocationListener {
new TaskExecuterManager(-1, strengthTaskFactory.create(this), this);
prefManager = new PreferenceManager(this);
prefManager.onSharedPreferenceChanged(null);
registerLocationListener();
acquireWakeLock();
/**
@@ -731,6 +703,7 @@ public class TrackRecordingService extends Service implements LocationListener {
showNotification();
unregisterLocationListener();
shutdownAnnouncer();
signalManager.shutdown();
splitManager.shutdown();
super.onDestroy();
}
@@ -962,8 +935,8 @@ public class TrackRecordingService extends Service implements LocationListener {
throw new IllegalStateException("No recording track in progress!");
}
isRecording = false;
shutdownAnnouncer();
isRecording = false;
Track recordingTrack = providerUtils.getTrack(recordingTrackId);
if (recordingTrack != null) {
TripStatistics stats = recordingTrack.getStatistics();
@@ -998,13 +971,6 @@ public class TrackRecordingService extends Service implements LocationListener {
public void recordLocation(Location loc) {
onLocationChanged(loc);
}
@Override
public void sharedPreferenceChanged(String key) {
Log.d(MyTracksConstants.TAG,
"TrackRecordingService.sharedPreferenceChanged: " + key);
onSharedPreferenceChanged(key);
}
};
public long startNewTrack() {