mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-11 21:43:05 +02:00
Move recording settings to RecordingSettingsActivity.
This commit is contained in:
@@ -15,13 +15,12 @@
|
||||
*/
|
||||
package com.google.android.apps.mytracks.services;
|
||||
|
||||
import com.google.android.apps.mytracks.Constants;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
import com.google.android.apps.mytracks.util.StringUtils;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
@@ -59,7 +58,7 @@ public class DefaultTrackNameFactory {
|
||||
} else {
|
||||
// trackNameSetting equals
|
||||
// R.string.settings_recording_track_name_number_value
|
||||
return String.format(context.getString(R.string.track_name_format), trackId);
|
||||
return context.getString(R.string.track_name_format, trackId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,10 +67,7 @@ public class DefaultTrackNameFactory {
|
||||
*/
|
||||
@VisibleForTesting
|
||||
String getTrackNameSetting() {
|
||||
SharedPreferences sharedPreferences = context.getSharedPreferences(
|
||||
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
|
||||
return sharedPreferences.getString(
|
||||
context.getString(R.string.track_name_key),
|
||||
return PreferencesUtils.getString(context, R.string.track_name_key,
|
||||
context.getString(R.string.settings_recording_track_name_date_local_value));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,156 +16,119 @@
|
||||
package com.google.android.apps.mytracks.services;
|
||||
|
||||
import com.google.android.apps.mytracks.Constants;
|
||||
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* A class that manages reading the shared preferences for the service.
|
||||
*
|
||||
*
|
||||
* @author Sandor Dornbush
|
||||
*/
|
||||
public class PreferenceManager implements OnSharedPreferenceChangeListener {
|
||||
private TrackRecordingService service;
|
||||
private SharedPreferences sharedPreferences;
|
||||
private final String announcementFrequencyKey;
|
||||
private final String autoResumeTrackCurrentRetryKey;
|
||||
private final String autoResumeTrackTimeoutKey;
|
||||
private final String maxRecordingDistanceKey;
|
||||
private final String minRecordingDistanceKey;
|
||||
private final String minRecordingIntervalKey;
|
||||
private final String minRequiredAccuracyKey;
|
||||
private final String splitFrequencyKey;
|
||||
|
||||
public PreferenceManager(TrackRecordingService service) {
|
||||
this.service = service;
|
||||
this.sharedPreferences = service.getSharedPreferences(
|
||||
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
|
||||
if (sharedPreferences == null) {
|
||||
Log.w(Constants.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);
|
||||
autoResumeTrackCurrentRetryKey =
|
||||
service.getString(R.string.auto_resume_track_current_retry_key);
|
||||
autoResumeTrackTimeoutKey =
|
||||
service.getString(R.string.auto_resume_track_timeout_key);
|
||||
maxRecordingDistanceKey =
|
||||
service.getString(R.string.max_recording_distance_key);
|
||||
minRecordingDistanceKey =
|
||||
service.getString(R.string.min_recording_distance_key);
|
||||
minRecordingIntervalKey =
|
||||
service.getString(R.string.min_recording_interval_key);
|
||||
minRequiredAccuracyKey =
|
||||
service.getString(R.string.min_required_accuracy_key);
|
||||
splitFrequencyKey =
|
||||
service.getString(R.string.split_frequency_key);
|
||||
|
||||
// Refresh all properties.
|
||||
onSharedPreferenceChanged(sharedPreferences, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies that preferences have changed.
|
||||
* Call this with key == null to update all preferences in one call.
|
||||
*
|
||||
* 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)
|
||||
*/
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences,
|
||||
String key) {
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
||||
if (service == null) {
|
||||
Log.w(Constants.TAG,
|
||||
"onSharedPreferenceChanged: a preference change (key = " + key
|
||||
Log.w(Constants.TAG, "onSharedPreferenceChanged: a preference change (key = " + key
|
||||
+ ") after a call to shutdown()");
|
||||
return;
|
||||
}
|
||||
if (key == null || key.equals(minRecordingDistanceKey)) {
|
||||
int minRecordingDistance = sharedPreferences.getInt(
|
||||
minRecordingDistanceKey,
|
||||
Constants.DEFAULT_MIN_RECORDING_DISTANCE);
|
||||
service.setMinRecordingDistance(minRecordingDistance);
|
||||
Log.d(Constants.TAG,
|
||||
"TrackRecordingService: minRecordingDistance = "
|
||||
+ minRecordingDistance);
|
||||
if (key == null
|
||||
|| PreferencesUtils.getKey(service, R.string.recording_track_id_key).equals(key)) {
|
||||
long recordingTrackId = PreferencesUtils.getLong(service, R.string.recording_track_id_key);
|
||||
/*
|
||||
* Only set the id if it is valid. Setting it to -1L should only happen
|
||||
* in TrackRecordingService.endCurrentTrack()
|
||||
*/
|
||||
if (recordingTrackId != -1L) {
|
||||
service.setRecordingTrackId(recordingTrackId);
|
||||
}
|
||||
}
|
||||
if (key == null || key.equals(maxRecordingDistanceKey)) {
|
||||
service.setMaxRecordingDistance(sharedPreferences.getInt(
|
||||
maxRecordingDistanceKey,
|
||||
Constants.DEFAULT_MAX_RECORDING_DISTANCE));
|
||||
if (key == null || PreferencesUtils.getKey(service, R.string.metric_units_key).equals(key)) {
|
||||
service.setMetricUnits(PreferencesUtils.getBoolean(service, R.string.metric_units_key, true));
|
||||
}
|
||||
if (key == null || key.equals(minRecordingIntervalKey)) {
|
||||
int minRecordingInterval = sharedPreferences.getInt(
|
||||
minRecordingIntervalKey,
|
||||
Constants.DEFAULT_MIN_RECORDING_INTERVAL);
|
||||
if (key == null
|
||||
|| PreferencesUtils.getKey(service, R.string.announcement_frequency_key).equals(key)) {
|
||||
service.setAnnouncementFrequency(PreferencesUtils.getInt(
|
||||
service, R.string.announcement_frequency_key,
|
||||
PreferencesUtils.ANNOUNCEMENT_FREQUENCY_DEFAULT));
|
||||
}
|
||||
if (key == null || PreferencesUtils.getKey(service, R.string.split_frequency_key).equals(key)) {
|
||||
service.setSplitFrequency(PreferencesUtils.getInt(
|
||||
service, R.string.split_frequency_key, PreferencesUtils.SPLIT_FREQUENCY_DEFAULT));
|
||||
}
|
||||
if (key == null
|
||||
|| PreferencesUtils.getKey(service, R.string.min_recording_interval_key).equals(key)) {
|
||||
int minRecordingInterval = PreferencesUtils.getInt(service,
|
||||
R.string.min_recording_interval_key, PreferencesUtils.MIN_RECORDING_INTERVAL_DEFAULT);
|
||||
switch (minRecordingInterval) {
|
||||
case -2:
|
||||
// Battery Miser
|
||||
// min: 30 seconds
|
||||
// max: 5 minutes
|
||||
// minDist: 5 meters Choose battery life over moving time accuracy.
|
||||
service.setLocationListenerPolicy(
|
||||
new AdaptiveLocationListenerPolicy(30000, 300000, 5));
|
||||
case PreferencesUtils.MIN_RECORDING_INTERVAL_ADAPT_BATTERY_LIFE:
|
||||
/*
|
||||
* Choose battery life over moving time accuracy. min: 30 seconds,
|
||||
* max: 5 minutes, inDist: 5 meters.
|
||||
*/
|
||||
service.setLocationListenerPolicy(new AdaptiveLocationListenerPolicy(30000, 300000, 5));
|
||||
break;
|
||||
case -1:
|
||||
// High Accuracy
|
||||
// min: 1 second
|
||||
// max: 30 seconds
|
||||
// minDist: 0 meters get all updates to properly measure moving time.
|
||||
service.setLocationListenerPolicy(
|
||||
new AdaptiveLocationListenerPolicy(1000, 30000, 0));
|
||||
case PreferencesUtils.MIN_RECORDING_INTERVAL_ADAPT_ACCURACY:
|
||||
/*
|
||||
* Get all the updates. min: 1 second, max: 30 seconds, minDist: 0
|
||||
* meter.
|
||||
*/
|
||||
service.setLocationListenerPolicy(new AdaptiveLocationListenerPolicy(1000, 30000, 0));
|
||||
break;
|
||||
default:
|
||||
service.setLocationListenerPolicy(
|
||||
new AbsoluteLocationListenerPolicy(minRecordingInterval * 1000));
|
||||
}
|
||||
}
|
||||
if (key == null || key.equals(minRequiredAccuracyKey)) {
|
||||
service.setMinRequiredAccuracy(sharedPreferences.getInt(
|
||||
minRequiredAccuracyKey,
|
||||
Constants.DEFAULT_MIN_REQUIRED_ACCURACY));
|
||||
if (key == null
|
||||
|| PreferencesUtils.getKey(service, R.string.min_recording_distance_key).equals(key)) {
|
||||
service.setMinRecordingDistance(PreferencesUtils.getInt(
|
||||
service, R.string.min_recording_distance_key,
|
||||
PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT));
|
||||
}
|
||||
if (key == null || key.equals(announcementFrequencyKey)) {
|
||||
service.setAnnouncementFrequency(
|
||||
sharedPreferences.getInt(announcementFrequencyKey, -1));
|
||||
if (key == null
|
||||
|| PreferencesUtils.getKey(service, R.string.max_recording_distance_key).equals(key)) {
|
||||
service.setMaxRecordingDistance(PreferencesUtils.getInt(
|
||||
service, R.string.max_recording_distance_key,
|
||||
PreferencesUtils.MAX_RECORDING_DISTANCE_DEFAULT));
|
||||
}
|
||||
if (key == null || key.equals(autoResumeTrackTimeoutKey)) {
|
||||
service.setAutoResumeTrackTimeout(sharedPreferences.getInt(
|
||||
autoResumeTrackTimeoutKey,
|
||||
Constants.DEFAULT_AUTO_RESUME_TRACK_TIMEOUT));
|
||||
}
|
||||
if (key == null || key.equals(PreferencesUtils.getKey(service, R.string.recording_track_id_key))) {
|
||||
long recordingTrackId = PreferencesUtils.getLong(service, R.string.recording_track_id_key);
|
||||
// Only read the id if it is valid.
|
||||
// Setting it to -1 should only happen in
|
||||
// TrackRecordingService.endCurrentTrack()
|
||||
if (recordingTrackId != -1L) {
|
||||
service.setRecordingTrackId(recordingTrackId);
|
||||
}
|
||||
}
|
||||
if (key == null || key.equals(splitFrequencyKey)) {
|
||||
service.setSplitFrequency(
|
||||
sharedPreferences.getInt(splitFrequencyKey, 0));
|
||||
}
|
||||
if (key == null || key.equals(PreferencesUtils.getKey(service, R.string.metric_units_key))) {
|
||||
service.setMetricUnits(PreferencesUtils.getBoolean(service, R.string.metric_units_key, true));
|
||||
}
|
||||
}
|
||||
|
||||
public void setAutoResumeTrackCurrentRetry(int retryAttempts) {
|
||||
Editor editor = sharedPreferences.edit();
|
||||
editor.putInt(autoResumeTrackCurrentRetryKey, retryAttempts);
|
||||
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
|
||||
if (key == null
|
||||
|| PreferencesUtils.getKey(service, R.string.min_required_accuracy_key).equals(key)) {
|
||||
service.setMinRequiredAccuracy(PreferencesUtils.getInt(
|
||||
service, R.string.min_required_accuracy_key,
|
||||
PreferencesUtils.MIN_REQUIRED_ACCURACY_DEFAULT));
|
||||
}
|
||||
if (key == null
|
||||
|| PreferencesUtils.getKey(service, R.string.auto_resume_track_timeout_key).equals(key)) {
|
||||
service.setAutoResumeTrackTimeout(PreferencesUtils.getInt(
|
||||
service, R.string.auto_resume_track_timeout_key,
|
||||
PreferencesUtils.AUTO_RESUME_TRACK_TIMEOUT_DEFAULT));
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
|
||||
@@ -50,7 +50,6 @@ 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;
|
||||
@@ -84,14 +83,10 @@ public class TrackRecordingService extends Service {
|
||||
private LocationManager locationManager;
|
||||
private WakeLock wakeLock;
|
||||
|
||||
private int minRecordingDistance =
|
||||
Constants.DEFAULT_MIN_RECORDING_DISTANCE;
|
||||
private int maxRecordingDistance =
|
||||
Constants.DEFAULT_MAX_RECORDING_DISTANCE;
|
||||
private int minRequiredAccuracy =
|
||||
Constants.DEFAULT_MIN_REQUIRED_ACCURACY;
|
||||
private int autoResumeTrackTimeout =
|
||||
Constants.DEFAULT_AUTO_RESUME_TRACK_TIMEOUT;
|
||||
private int minRecordingDistance = PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT;
|
||||
private int maxRecordingDistance = PreferencesUtils.MAX_RECORDING_DISTANCE_DEFAULT;
|
||||
private int minRequiredAccuracy = PreferencesUtils.MIN_REQUIRED_ACCURACY_DEFAULT;
|
||||
private int autoResumeTrackTimeout = PreferencesUtils.AUTO_RESUME_TRACK_TIMEOUT_DEFAULT;
|
||||
|
||||
private long recordingTrackId = -1;
|
||||
|
||||
@@ -123,7 +118,7 @@ public class TrackRecordingService extends Service {
|
||||
|
||||
private SensorManager sensorManager;
|
||||
|
||||
private PreferenceManager prefManager;
|
||||
private PreferenceManager prefereceManager;
|
||||
|
||||
/**
|
||||
* The interval in milliseconds that we have requested to be notified of gps
|
||||
@@ -248,7 +243,7 @@ public class TrackRecordingService extends Service {
|
||||
setUpTaskExecutors();
|
||||
executorService = Executors.newSingleThreadExecutor();
|
||||
|
||||
prefManager = new PreferenceManager(this);
|
||||
prefereceManager = new PreferenceManager(this);
|
||||
|
||||
registerLocationListener();
|
||||
|
||||
@@ -354,8 +349,7 @@ public class TrackRecordingService extends Service {
|
||||
|
||||
isRecording = false;
|
||||
showNotification();
|
||||
prefManager.shutdown();
|
||||
prefManager = null;
|
||||
prefereceManager.shutdown();
|
||||
checkLocationListener.cancel();
|
||||
checkLocationListener = null;
|
||||
timer.cancel();
|
||||
@@ -383,7 +377,7 @@ public class TrackRecordingService extends Service {
|
||||
|
||||
private void setAutoResumeTrackRetries(int retryAttempts) {
|
||||
Log.d(TAG, "Updating auto-resume retry attempts to: " + retryAttempts);
|
||||
prefManager.setAutoResumeTrackCurrentRetry(retryAttempts);
|
||||
PreferencesUtils.setInt(this, R.string.auto_resume_track_current_retry_key, retryAttempts);
|
||||
}
|
||||
|
||||
private boolean shouldResumeTrack(Track track) {
|
||||
@@ -391,10 +385,8 @@ public class TrackRecordingService extends Service {
|
||||
+ autoResumeTrackTimeout);
|
||||
|
||||
// Check if we haven't exceeded the maximum number of retry attempts.
|
||||
SharedPreferences sharedPreferences = getSharedPreferences(
|
||||
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
|
||||
int retries = sharedPreferences.getInt(
|
||||
getString(R.string.auto_resume_track_current_retry_key), 0);
|
||||
int retries = PreferencesUtils.getInt(this, R.string.auto_resume_track_current_retry_key,
|
||||
PreferencesUtils.AUTO_RESUME_TRACK_CURRENT_RETRY_DEFAULT);
|
||||
Log.d(TAG,
|
||||
"shouldResumeTrack: Attempting to auto-resume the track ("
|
||||
+ (retries + 1) + "/" + MAX_AUTO_RESUME_TRACK_RETRY_ATTEMPTS + ")");
|
||||
@@ -409,12 +401,12 @@ public class TrackRecordingService extends Service {
|
||||
setAutoResumeTrackRetries(retries + 1);
|
||||
|
||||
// Check for special cases.
|
||||
if (autoResumeTrackTimeout == 0) {
|
||||
if (autoResumeTrackTimeout == PreferencesUtils.AUTO_RESUME_TRACK_TIMEOUT_NEVER) {
|
||||
// Never resume.
|
||||
Log.d(TAG,
|
||||
"shouldResumeTrack: Auto-resume disabled (never resume)");
|
||||
return false;
|
||||
} else if (autoResumeTrackTimeout == -1) {
|
||||
} else if (autoResumeTrackTimeout == PreferencesUtils.AUTO_RESUME_TRACK_TIMEOUT_ALWAYS) {
|
||||
// Always resume.
|
||||
Log.d(TAG,
|
||||
"shouldResumeTrack: Auto-resume forced (always resume)");
|
||||
@@ -565,12 +557,6 @@ public class TrackRecordingService extends Service {
|
||||
"Location listener now unregistered w/ TrackRecordingService.");
|
||||
}
|
||||
|
||||
private String getDefaultActivityType(Context context) {
|
||||
SharedPreferences prefs = context.getSharedPreferences(
|
||||
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
|
||||
return prefs.getString(context.getString(R.string.default_activity_key), "");
|
||||
}
|
||||
|
||||
/*
|
||||
* Recording lifecycle.
|
||||
*/
|
||||
@@ -593,7 +579,8 @@ public class TrackRecordingService extends Service {
|
||||
track.setId(recordingTrackId);
|
||||
track.setName(new DefaultTrackNameFactory(this).getDefaultTrackName(
|
||||
recordingTrackId, startTime));
|
||||
track.setCategory(getDefaultActivityType(this));
|
||||
track.setCategory(PreferencesUtils.getString(
|
||||
this, R.string.default_activity_key, PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT));
|
||||
isRecording = true;
|
||||
isMoving = true;
|
||||
|
||||
|
||||
+3
-2
@@ -18,6 +18,7 @@ package com.google.android.apps.mytracks.services.tasks;
|
||||
import static com.google.android.apps.mytracks.Constants.TAG;
|
||||
|
||||
import com.google.android.apps.mytracks.services.TrackRecordingService;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
import com.google.android.apps.mytracks.util.UnitConversions;
|
||||
|
||||
import android.util.Log;
|
||||
@@ -34,7 +35,7 @@ public class PeriodicTaskExecutor {
|
||||
* A value greater than zero is a frequency in time.
|
||||
* A value less than zero is considered a frequency in distance.
|
||||
*/
|
||||
private int taskFrequency = 0;
|
||||
private int taskFrequency = PreferencesUtils.PERIODIC_TASK_OFF;
|
||||
|
||||
/**
|
||||
* The next distance when the task should execute.
|
||||
@@ -74,7 +75,7 @@ public class PeriodicTaskExecutor {
|
||||
timerExecutor = null;
|
||||
}
|
||||
}
|
||||
if (taskFrequency == 0) {
|
||||
if (taskFrequency == PreferencesUtils.PERIODIC_TASK_OFF) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user