forked from upstream-mirrors/OpenTracks
Limit the maximum number of auto-resume retry attempts to 3.
This commit is contained in:
@@ -41,6 +41,7 @@ limitations under the License.
|
||||
<string name="debug_draw_bounds_key">debugDrawBounds</string>
|
||||
|
||||
<!-- 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="selected_track_key">selectedTrack</string>
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ public class PreferenceManager {
|
||||
announcementFrequencyKey =
|
||||
service.getString(R.string.announcement_frequency_key);
|
||||
autoResumeTrackTimeoutKey =
|
||||
service.getString(R.string.auto_resume_track_timeout_key);
|
||||
service.getString(R.string.auto_resume_track_timeout_key);
|
||||
maxRecordingDistanceKey =
|
||||
service.getString(R.string.max_recording_distance_key);
|
||||
metricUnitsKey =
|
||||
@@ -90,10 +90,9 @@ public class PreferenceManager {
|
||||
+ service.getMinRecordingDistance());
|
||||
}
|
||||
if (key == null || key.equals(maxRecordingDistanceKey)) {
|
||||
service.setMaxRecordingDistance(
|
||||
sharedPreferences.getInt(
|
||||
maxRecordingDistanceKey,
|
||||
MyTracksSettings.DEFAULT_MAX_RECORDING_DISTANCE));
|
||||
service.setMaxRecordingDistance(sharedPreferences.getInt(
|
||||
maxRecordingDistanceKey,
|
||||
MyTracksSettings.DEFAULT_MAX_RECORDING_DISTANCE));
|
||||
}
|
||||
if (key == null || key.equals(minRecordingIntervalKey)) {
|
||||
int minRecordingInterval = sharedPreferences.getInt(
|
||||
@@ -122,18 +121,18 @@ public class PreferenceManager {
|
||||
}
|
||||
}
|
||||
if (key == null || key.equals(minRequiredAccuracyKey)) {
|
||||
service.setMinRequiredAccuracy(
|
||||
sharedPreferences.getInt(
|
||||
minRequiredAccuracyKey,
|
||||
MyTracksSettings.DEFAULT_MIN_REQUIRED_ACCURACY));
|
||||
service.setMinRequiredAccuracy(sharedPreferences.getInt(
|
||||
minRequiredAccuracyKey,
|
||||
MyTracksSettings.DEFAULT_MIN_REQUIRED_ACCURACY));
|
||||
}
|
||||
if (key == null || key.equals(announcementFrequencyKey)) {
|
||||
service.setAnnouncementFrequency(
|
||||
sharedPreferences.getInt(announcementFrequencyKey, -1));
|
||||
}
|
||||
if (key == null || key.equals(autoResumeTrackTimeoutKey)) {
|
||||
service.setAutoResumeTrackTimeout(
|
||||
sharedPreferences.getInt(autoResumeTrackTimeoutKey, -1));
|
||||
service.setAutoResumeTrackTimeout(sharedPreferences.getInt(
|
||||
autoResumeTrackTimeoutKey,
|
||||
MyTracksSettings.DEFAULT_AUTO_RESUME_TRACK_TIMEOUT));
|
||||
}
|
||||
if (key == null || key.equals(recordingTrackKey)) {
|
||||
service.setRecordingTrackId(
|
||||
|
||||
@@ -38,6 +38,7 @@ import android.app.Service;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteException;
|
||||
import android.location.Location;
|
||||
@@ -65,6 +66,8 @@ public class TrackRecordingService extends Service implements LocationListener {
|
||||
private static final String STATISTICS_ICON_URL =
|
||||
"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 LocationManager locationManager;
|
||||
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
|
||||
* 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 &&
|
||||
intent.getBooleanExtra(RESUME_TRACK_EXTRA_NAME, false)) {
|
||||
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.
|
||||
// Note: Sometimes we may get null intent, so avoid a NPE.
|
||||
if (recordingTrack == null || !shouldResumeTrack(recordingTrack)) {
|
||||
Log.i(MyTracksConstants.TAG,
|
||||
"TrackRecordingService: Not resuming because the previous track "
|
||||
@@ -753,6 +786,7 @@ public class TrackRecordingService extends Service implements LocationListener {
|
||||
stopSelfResult(startId);
|
||||
return;
|
||||
}
|
||||
|
||||
Log.i(MyTracksConstants.TAG, "TrackRecordingService: resuming");
|
||||
}
|
||||
}
|
||||
@@ -880,6 +914,10 @@ public class TrackRecordingService extends Service implements LocationListener {
|
||||
registerLocationListener();
|
||||
splitManager.restore();
|
||||
signalManager.restore();
|
||||
// Reset the number of auto-resume retries.
|
||||
SharedPreferences sharedPreferences =
|
||||
getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
|
||||
setAutoResumeTrackRetries(sharedPreferences, 0);
|
||||
return trackId;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user