Merging Sandor's changes

This commit is contained in:
Rodrigo Damazio
2010-11-12 14:37:04 -08:00
5 changed files with 191 additions and 43 deletions
@@ -25,7 +25,7 @@ import java.util.Timer;
import java.util.TimerTask;
/**
* This class will periodically announce the user's trip statitics.
* This class will periodically announce the user's trip statistics.
*
* @author Sandor Dornbush
*/
@@ -52,6 +52,11 @@ public class PeriodicTaskExecuter {
* @param interval The interval in milliseconds
*/
public void scheduleTask(long interval) {
// TODO: Decouple service from this class once and forever.
if (!service.isRecording()) {
return;
}
timer.cancel();
timer.purge();
timer = new Timer();
@@ -61,12 +66,14 @@ public class PeriodicTaskExecuter {
long now = System.currentTimeMillis();
long next = service.getTripStatistics().getStartTime();
while (next < now) next += interval;
if (next < now) {
next = now + interval - ((now - next) % interval);
}
Date start = new Date(next);
Log.i(MyTracksConstants.TAG,
"StatusAnnouncer scheduled to start at " + start + " every "
+ interval + " milliseconds.");
task.getClass().getSimpleName() + " scheduled to start at " + start
+ " every " + interval + " milliseconds.");
timer.scheduleAtFixedRate(new PeriodicTimerTask(), start, interval);
}
@@ -74,6 +81,8 @@ public class PeriodicTaskExecuter {
* Cleans up this object.
*/
public void shutdown() {
Log.i(MyTracksConstants.TAG,
task.getClass().getSimpleName() + " shutting down.");
timer.cancel();
timer.purge();
timer = null;
@@ -73,6 +73,11 @@ public class SplitManager {
* Calculates the next distance that a split should be inserted at.
*/
public void calculateNextSplit() {
// TODO: Decouple service from this class once and forever.
if (!service.isRecording()) {
return;
}
if (splitFrequency >= 0) {
nextSplitDistance = Double.MAX_VALUE;
Log.d(MyTracksConstants.TAG,
@@ -121,6 +126,12 @@ public class SplitManager {
*/
public void setSplitFrequency(int splitFrequency) {
this.splitFrequency = splitFrequency;
// TODO: Decouple service from this class once and forever.
if (!service.isRecording()) {
return;
}
if (splitFrequency < 1) {
if (splitExecuter != null) {
splitExecuter.shutdown();
@@ -239,8 +239,10 @@ public class StatusAnnouncerTask implements PeriodicTask {
// Stop listening to phone state.
listenToPhoneState(phoneListener, PhoneStateListener.LISTEN_NONE);
tts.shutdown();
tts = null;
if (tts != null) {
tts.shutdown();
tts = null;
}
}
/**
@@ -663,9 +663,11 @@ public class TrackRecordingService extends Service implements LocationListener {
restoreStats(recordingTrack);
isRecording = true;
} else {
// Make sure we have consistent state in shared preferences.
Log.w(MyTracksConstants.TAG, "TrackRecordingService.onCreate: Resetting "
+ "an orphaned recording track: " + recordingTrackId);
if (recordingTrackId != -1) {
// Make sure we have consistent state in shared preferences.
Log.w(MyTracksConstants.TAG, "TrackRecordingService.onCreate: "
+ "Resetting an orphaned recording track = " + recordingTrackId);
}
prefManager.setRecordingTrack(recordingTrackId = -1);
}
showNotification();
@@ -677,7 +679,9 @@ public class TrackRecordingService extends Service implements LocationListener {
* the announcements, otherwise this method is no-op.
*/
private void setUpAnnouncer() {
if (announcementFrequency != -1) {
Log.d(MyTracksConstants.TAG, "TrackRecordingService.setUpAnnouncer: "
+ announcementExecuter);
if (announcementFrequency != -1 && recordingTrackId != -1) {
if (announcementExecuter == null) {
StatusAnnouncerFactory statusAnnouncerFactory =
new StatusAnnouncerFactory(ApiFeatures.getInstance());
@@ -689,6 +693,18 @@ public class TrackRecordingService extends Service implements LocationListener {
announcementExecuter.scheduleTask(announcementFrequency * 60000);
}
}
private void shutdownAnnouncer() {
Log.d(MyTracksConstants.TAG, "TrackRecordingService.shutdownAnnouncer: "
+ announcementExecuter);
if (announcementExecuter != null) {
try {
announcementExecuter.shutdown();
} finally {
announcementExecuter = null;
}
}
}
@Override
public void onDestroy() {
@@ -701,9 +717,7 @@ public class TrackRecordingService extends Service implements LocationListener {
isRecording = false;
showNotification();
unregisterLocationListener();
if (announcementExecuter != null) {
announcementExecuter.shutdown();
}
shutdownAnnouncer();
splitManager.shutdown();
super.onDestroy();
}
@@ -936,6 +950,7 @@ public class TrackRecordingService extends Service implements LocationListener {
}
isRecording = false;
shutdownAnnouncer();
Track recordingTrack = providerUtils.getTrack(recordingTrackId);
if (recordingTrack != null) {
TripStatistics stats = recordingTrack.getStatistics();
@@ -1042,10 +1057,7 @@ public class TrackRecordingService extends Service implements LocationListener {
public void setAnnouncementFrequency(int announcementFrequency) {
this.announcementFrequency = announcementFrequency;
if (announcementFrequency == -1) {
if (announcementExecuter != null) {
announcementExecuter.shutdown();
announcementExecuter = null;
}
shutdownAnnouncer();
} else {
setUpAnnouncer();
}