Fixing wake lock being held for too long in the service.

This commit is contained in:
Rodrigo Damazio
2010-11-18 17:13:20 -08:00
parent 6e28a5daa4
commit b304dbcba6
@@ -295,7 +295,7 @@ public class TrackRecordingService extends Service implements LocationListener {
* Tries to acquire a partial wake lock if not already acquired. Logs errors * Tries to acquire a partial wake lock if not already acquired. Logs errors
* and gives up trying in case the wake lock cannot be acquired. * and gives up trying in case the wake lock cannot be acquired.
*/ */
public void acquireWakeLock() { private void acquireWakeLock() {
try { try {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm == null) { if (pm == null) {
@@ -326,6 +326,16 @@ public class TrackRecordingService extends Service implements LocationListener {
} }
} }
/**
* Releases the wake lock if it's currently held.
*/
private void releaseWakeLock() {
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
wakeLock = null;
}
}
/** /**
* Shows the notification message and icon in the notification bar. * Shows the notification message and icon in the notification bar.
*/ */
@@ -626,7 +636,6 @@ public class TrackRecordingService extends Service implements LocationListener {
prefManager = new PreferenceManager(this); prefManager = new PreferenceManager(this);
registerLocationListener(); registerLocationListener();
acquireWakeLock();
/** /**
* After 5 min, check every minute that location listener still is * After 5 min, check every minute that location listener still is
* registered and spit out additional debugging info to the logs: * registered and spit out additional debugging info to the logs:
@@ -696,15 +705,13 @@ public class TrackRecordingService extends Service implements LocationListener {
Log.d(MyTracksConstants.TAG, "TrackRecordingService.onDestroy"); Log.d(MyTracksConstants.TAG, "TrackRecordingService.onDestroy");
checkLocationListener.cancel(); checkLocationListener.cancel();
timer.cancel(); timer.cancel();
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
}
isRecording = false; isRecording = false;
showNotification(); showNotification();
unregisterLocationListener(); unregisterLocationListener();
shutdownAnnouncer(); shutdownAnnouncer();
signalManager.shutdown(); signalManager.shutdown();
splitManager.shutdown(); splitManager.shutdown();
releaseWakeLock();
super.onDestroy(); super.onDestroy();
} }
@@ -934,7 +941,7 @@ public class TrackRecordingService extends Service implements LocationListener {
if (recordingTrackId == -1 || !isRecording) { if (recordingTrackId == -1 || !isRecording) {
throw new IllegalStateException("No recording track in progress!"); throw new IllegalStateException("No recording track in progress!");
} }
shutdownAnnouncer(); shutdownAnnouncer();
isRecording = false; isRecording = false;
Track recordingTrack = providerUtils.getTrack(recordingTrackId); Track recordingTrack = providerUtils.getTrack(recordingTrackId);
@@ -956,6 +963,7 @@ public class TrackRecordingService extends Service implements LocationListener {
} }
showNotification(); showNotification();
prefManager.setRecordingTrack(recordingTrackId = -1); prefManager.setRecordingTrack(recordingTrackId = -1);
releaseWakeLock();
} }
@Override @Override
@@ -980,6 +988,7 @@ public class TrackRecordingService extends Service implements LocationListener {
} }
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
acquireWakeLock();
Track track = new Track(); Track track = new Track();
TripStatistics trackStats = track.getStatistics(); TripStatistics trackStats = track.getStatistics();