Limit the maximum number of auto-resume retry attempts to 3.

This commit is contained in:
Bartlomiej Niechwiej
2010-10-04 21:17:56 -07:00
parent 34bb467b97
commit 1e343b0d12
3 changed files with 50 additions and 12 deletions
+1
View File
@@ -41,6 +41,7 @@ limitations under the License.
<string name="debug_draw_bounds_key">debugDrawBounds</string> <string name="debug_draw_bounds_key">debugDrawBounds</string>
<!-- Preference keys which keep transient state (not user preferences) - DO NOT TRANSLATE --> <!-- Preference keys which keep transient state (not user preferences) - DO NOT TRANSLATE -->
<string name="auto_resume_track_current_retry_key">autoResumeTrackCurrentRetry</string>
<string name="recording_track_key">recordingTrack</string> <string name="recording_track_key">recordingTrack</string>
<string name="selected_track_key">selectedTrack</string> <string name="selected_track_key">selectedTrack</string>
@@ -46,7 +46,7 @@ public class PreferenceManager {
announcementFrequencyKey = announcementFrequencyKey =
service.getString(R.string.announcement_frequency_key); service.getString(R.string.announcement_frequency_key);
autoResumeTrackTimeoutKey = autoResumeTrackTimeoutKey =
service.getString(R.string.auto_resume_track_timeout_key); service.getString(R.string.auto_resume_track_timeout_key);
maxRecordingDistanceKey = maxRecordingDistanceKey =
service.getString(R.string.max_recording_distance_key); service.getString(R.string.max_recording_distance_key);
metricUnitsKey = metricUnitsKey =
@@ -90,10 +90,9 @@ public class PreferenceManager {
+ service.getMinRecordingDistance()); + service.getMinRecordingDistance());
} }
if (key == null || key.equals(maxRecordingDistanceKey)) { if (key == null || key.equals(maxRecordingDistanceKey)) {
service.setMaxRecordingDistance( service.setMaxRecordingDistance(sharedPreferences.getInt(
sharedPreferences.getInt( maxRecordingDistanceKey,
maxRecordingDistanceKey, MyTracksSettings.DEFAULT_MAX_RECORDING_DISTANCE));
MyTracksSettings.DEFAULT_MAX_RECORDING_DISTANCE));
} }
if (key == null || key.equals(minRecordingIntervalKey)) { if (key == null || key.equals(minRecordingIntervalKey)) {
int minRecordingInterval = sharedPreferences.getInt( int minRecordingInterval = sharedPreferences.getInt(
@@ -122,18 +121,18 @@ public class PreferenceManager {
} }
} }
if (key == null || key.equals(minRequiredAccuracyKey)) { if (key == null || key.equals(minRequiredAccuracyKey)) {
service.setMinRequiredAccuracy( service.setMinRequiredAccuracy(sharedPreferences.getInt(
sharedPreferences.getInt( minRequiredAccuracyKey,
minRequiredAccuracyKey, MyTracksSettings.DEFAULT_MIN_REQUIRED_ACCURACY));
MyTracksSettings.DEFAULT_MIN_REQUIRED_ACCURACY));
} }
if (key == null || key.equals(announcementFrequencyKey)) { if (key == null || key.equals(announcementFrequencyKey)) {
service.setAnnouncementFrequency( service.setAnnouncementFrequency(
sharedPreferences.getInt(announcementFrequencyKey, -1)); sharedPreferences.getInt(announcementFrequencyKey, -1));
} }
if (key == null || key.equals(autoResumeTrackTimeoutKey)) { if (key == null || key.equals(autoResumeTrackTimeoutKey)) {
service.setAutoResumeTrackTimeout( service.setAutoResumeTrackTimeout(sharedPreferences.getInt(
sharedPreferences.getInt(autoResumeTrackTimeoutKey, -1)); autoResumeTrackTimeoutKey,
MyTracksSettings.DEFAULT_AUTO_RESUME_TRACK_TIMEOUT));
} }
if (key == null || key.equals(recordingTrackKey)) { if (key == null || key.equals(recordingTrackKey)) {
service.setRecordingTrackId( service.setRecordingTrackId(
@@ -38,6 +38,7 @@ import android.app.Service;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteException;
import android.location.Location; import android.location.Location;
@@ -65,6 +66,8 @@ public class TrackRecordingService extends Service implements LocationListener {
private static final String STATISTICS_ICON_URL = private static final String STATISTICS_ICON_URL =
"http://maps.google.com/mapfiles/ms/micons/ylw-pushpin.png"; "http://maps.google.com/mapfiles/ms/micons/ylw-pushpin.png";
private static final int MAX_AUTO_RESUME_TRACK_RETRY_ATTEMPTS = 3;
private NotificationManager notificationManager; private NotificationManager notificationManager;
private LocationManager locationManager; private LocationManager locationManager;
private WakeLock wakeLock; private WakeLock wakeLock;
@@ -298,6 +301,17 @@ public class TrackRecordingService extends Service implements LocationListener {
} }
} }
private void setAutoResumeTrackRetries(
SharedPreferences sharedPreferences, int retryAttempts) {
Log.d(MyTracksConstants.TAG,
"Updating auto-resume retry attempts to: " + retryAttempts);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(
getString(R.string.auto_resume_track_current_retry_key), retryAttempts);
editor.commit();
}
/** /**
* 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.
@@ -744,8 +758,27 @@ public class TrackRecordingService extends Service implements LocationListener {
if (intent != null && if (intent != null &&
intent.getBooleanExtra(RESUME_TRACK_EXTRA_NAME, false)) { intent.getBooleanExtra(RESUME_TRACK_EXTRA_NAME, false)) {
Log.d(MyTracksConstants.TAG, "TrackRecordingService: requested resume"); Log.d(MyTracksConstants.TAG, "TrackRecordingService: requested resume");
// Check if we haven't exceeded the maximum number of retry attempts.
SharedPreferences sharedPreferences =
getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
int retries = sharedPreferences.getInt(
getString(R.string.auto_resume_track_current_retry_key), 0);
Log.d(MyTracksConstants.TAG,
"TrackRecordingService: Attempting to auto-resume the track ("
+ (retries + 1) + "/" + MAX_AUTO_RESUME_TRACK_RETRY_ATTEMPTS + ")");
if (retries >= MAX_AUTO_RESUME_TRACK_RETRY_ATTEMPTS) {
Log.i(MyTracksConstants.TAG,
"TrackRecordingService: Not resuming because exceeded the maximum "
+ "number of auto-resume retries");
stopSelfResult(startId);
return;
}
// Increase number of retry attempts.
setAutoResumeTrackRetries(sharedPreferences, retries + 1);
// Make sure that the current track exists and is fresh enough. // Make sure that the current track exists and is fresh enough.
// Note: Sometimes we may get null intent, so avoid a NPE.
if (recordingTrack == null || !shouldResumeTrack(recordingTrack)) { if (recordingTrack == null || !shouldResumeTrack(recordingTrack)) {
Log.i(MyTracksConstants.TAG, Log.i(MyTracksConstants.TAG,
"TrackRecordingService: Not resuming because the previous track " "TrackRecordingService: Not resuming because the previous track "
@@ -753,6 +786,7 @@ public class TrackRecordingService extends Service implements LocationListener {
stopSelfResult(startId); stopSelfResult(startId);
return; return;
} }
Log.i(MyTracksConstants.TAG, "TrackRecordingService: resuming"); Log.i(MyTracksConstants.TAG, "TrackRecordingService: resuming");
} }
} }
@@ -880,6 +914,10 @@ public class TrackRecordingService extends Service implements LocationListener {
registerLocationListener(); registerLocationListener();
splitManager.restore(); splitManager.restore();
signalManager.restore(); signalManager.restore();
// Reset the number of auto-resume retries.
SharedPreferences sharedPreferences =
getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
setAutoResumeTrackRetries(sharedPreferences, 0);
return trackId; return trackId;
} }