mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-11 21:43:05 +02:00
Update TrackRecordingService.
This commit is contained in:
@@ -1,139 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010 Google Inc.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
||||||
* use this file except in compliance with the License. You may obtain a copy of
|
|
||||||
* the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
* License for the specific language governing permissions and limitations under
|
|
||||||
* the License.
|
|
||||||
*/
|
|
||||||
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.maps.mytracks.R;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.SharedPreferences;
|
|
||||||
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;
|
|
||||||
|
|
||||||
public PreferenceManager(TrackRecordingService service) {
|
|
||||||
this.service = service;
|
|
||||||
this.sharedPreferences = service.getSharedPreferences(
|
|
||||||
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
|
|
||||||
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
|
|
||||||
|
|
||||||
// Refresh all properties.
|
|
||||||
onSharedPreferenceChanged(sharedPreferences, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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) {
|
|
||||||
if (service == null) {
|
|
||||||
Log.w(Constants.TAG, "onSharedPreferenceChanged: a preference change (key = " + key
|
|
||||||
+ ") after a call to shutdown()");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
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 != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT) {
|
|
||||||
service.setRecordingTrackId(recordingTrackId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (key == null || PreferencesUtils.getKey(service, R.string.metric_units_key).equals(key)) {
|
|
||||||
service.setMetricUnits(PreferencesUtils.getBoolean(
|
|
||||||
service, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT));
|
|
||||||
}
|
|
||||||
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 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 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
|
|
||||||
|| 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
|
|
||||||
|| 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
|
|
||||||
|| 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() {
|
|
||||||
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
|
|
||||||
service = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+859
-954
File diff suppressed because it is too large
Load Diff
+12
-2
@@ -16,6 +16,7 @@
|
|||||||
package com.google.android.apps.mytracks.services.tasks;
|
package com.google.android.apps.mytracks.services.tasks;
|
||||||
|
|
||||||
import com.google.android.apps.mytracks.services.TrackRecordingService;
|
import com.google.android.apps.mytracks.services.TrackRecordingService;
|
||||||
|
import com.google.android.apps.mytracks.stats.TripStatistics;
|
||||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||||
import com.google.android.apps.mytracks.util.UnitConversions;
|
import com.google.android.apps.mytracks.util.UnitConversions;
|
||||||
|
|
||||||
@@ -118,7 +119,11 @@ public class PeriodicTaskExecutor {
|
|||||||
if (!isDistanceFrequency() || periodicTask == null) {
|
if (!isDistanceFrequency() || periodicTask == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
double distance = trackRecordingService.getTripStatistics().getTotalDistance()
|
TripStatistics tripStatistics = trackRecordingService.getTripStatistics();
|
||||||
|
if (tripStatistics == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
double distance = tripStatistics.getTotalDistance()
|
||||||
* UnitConversions.M_TO_KM;
|
* UnitConversions.M_TO_KM;
|
||||||
if (!metricUnits) {
|
if (!metricUnits) {
|
||||||
distance *= UnitConversions.KM_TO_MI;
|
distance *= UnitConversions.KM_TO_MI;
|
||||||
@@ -158,13 +163,18 @@ public class PeriodicTaskExecutor {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TripStatistics tripStatistics = trackRecordingService.getTripStatistics();
|
||||||
|
if (tripStatistics == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!isDistanceFrequency()) {
|
if (!isDistanceFrequency()) {
|
||||||
nextTaskDistance = Double.MAX_VALUE;
|
nextTaskDistance = Double.MAX_VALUE;
|
||||||
Log.d(TAG, "SplitManager: Distance splits disabled.");
|
Log.d(TAG, "SplitManager: Distance splits disabled.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
double distance = trackRecordingService.getTripStatistics().getTotalDistance()
|
double distance = tripStatistics.getTotalDistance()
|
||||||
* UnitConversions.M_TO_KM;
|
* UnitConversions.M_TO_KM;
|
||||||
if (!metricUnits) {
|
if (!metricUnits) {
|
||||||
distance *= UnitConversions.KM_TO_MI;
|
distance *= UnitConversions.KM_TO_MI;
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
package com.google.android.apps.mytracks.services.tasks;
|
package com.google.android.apps.mytracks.services.tasks;
|
||||||
|
|
||||||
import com.google.android.apps.mytracks.services.TrackRecordingService;
|
import com.google.android.apps.mytracks.services.TrackRecordingService;
|
||||||
|
import com.google.android.apps.mytracks.stats.TripStatistics;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
@@ -53,6 +54,11 @@ public class TimerTaskExecutor {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TripStatistics tripStatistics = trackRecordingService.getTripStatistics();
|
||||||
|
if (tripStatistics == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (timer != null) {
|
if (timer != null) {
|
||||||
timer.cancel();
|
timer.cancel();
|
||||||
timer.purge();
|
timer.purge();
|
||||||
@@ -67,7 +73,7 @@ public class TimerTaskExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
long next = trackRecordingService.getTripStatistics().getStartTime();
|
long next = tripStatistics.getStartTime();
|
||||||
if (next < now) {
|
if (next < now) {
|
||||||
next = now + interval - ((now - next) % interval);
|
next = now + interval - ((now - next) % interval);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -174,6 +174,13 @@ public interface MyTracksProviderUtils {
|
|||||||
*/
|
*/
|
||||||
public long getLastWaypointId(long trackId);
|
public long getLastWaypointId(long trackId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the last statistics waypoint for a track. Returns null if it doesn't exist.
|
||||||
|
*
|
||||||
|
* @param trackId the track id
|
||||||
|
*/
|
||||||
|
public Waypoint getLastStatisticsWaypoint(long trackId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the next marker number. Returns -1 if not able to get the next marker
|
* Gets the next marker number. Returns -1 if not able to get the next marker
|
||||||
* number.
|
* number.
|
||||||
|
|||||||
+23
@@ -443,6 +443,29 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
|
|||||||
return -1L;
|
return -1L;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Waypoint getLastStatisticsWaypoint(long trackId) {
|
||||||
|
if (trackId < 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Cursor cursor = null;
|
||||||
|
try {
|
||||||
|
String selection = WaypointsColumns.TRACKID + "=? AND " + WaypointsColumns.TYPE + "="
|
||||||
|
+ Waypoint.TYPE_STATISTICS;
|
||||||
|
String[] selectionArgs = new String[] { Long.toString(trackId) };
|
||||||
|
cursor = getWaypointsCursor(
|
||||||
|
null, selection, selectionArgs, WaypointsColumns._ID + " DESC", 1);
|
||||||
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
|
return createWaypoint(cursor);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (cursor != null) {
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getNextMarkerNumber(long trackId, boolean statistics) {
|
public int getNextMarkerNumber(long trackId, boolean statistics) {
|
||||||
if (trackId < 0) {
|
if (trackId < 0) {
|
||||||
|
|||||||
+30
-2
@@ -430,7 +430,7 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
|
|||||||
/**
|
/**
|
||||||
* Tests the method {@link MyTracksProviderUtilsImpl#getLastWaypointId(long)}.
|
* Tests the method {@link MyTracksProviderUtilsImpl#getLastWaypointId(long)}.
|
||||||
*/
|
*/
|
||||||
public void testGetKLastWaypointId() {
|
public void testGetLastWaypointId() {
|
||||||
long trackId = System.currentTimeMillis();
|
long trackId = System.currentTimeMillis();
|
||||||
Track track = getTrack(trackId, 10);
|
Track track = getTrack(trackId, 10);
|
||||||
providerUtils.insertTrack(track);
|
providerUtils.insertTrack(track);
|
||||||
@@ -448,7 +448,35 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
|
|||||||
assertEquals(-1L, providerUtils.getLastWaypointId(-1));
|
assertEquals(-1L, providerUtils.getLastWaypointId(-1));
|
||||||
assertEquals(3L, providerUtils.getLastWaypointId(trackId));
|
assertEquals(3L, providerUtils.getLastWaypointId(trackId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the method
|
||||||
|
* {@link MyTracksProviderUtils#getLastStatisticsWaypoint(long)}.
|
||||||
|
*/
|
||||||
|
public void testGetLastStatisticsWaypoint() {
|
||||||
|
long trackId = System.currentTimeMillis();
|
||||||
|
Track track = getTrack(trackId, 10);
|
||||||
|
providerUtils.insertTrack(track);
|
||||||
|
|
||||||
|
Waypoint waypoint1 = new Waypoint();
|
||||||
|
waypoint1.setTrackId(trackId);
|
||||||
|
waypoint1.setType(Waypoint.TYPE_STATISTICS);
|
||||||
|
waypoint1.setDescription("Desc1");
|
||||||
|
Waypoint waypoint2 = new Waypoint();
|
||||||
|
waypoint2.setTrackId(trackId);
|
||||||
|
waypoint2.setType(Waypoint.TYPE_STATISTICS);
|
||||||
|
waypoint2.setDescription("Desc2");
|
||||||
|
Waypoint waypoint3 = new Waypoint();
|
||||||
|
waypoint3.setTrackId(trackId);
|
||||||
|
waypoint3.setType(Waypoint.TYPE_WAYPOINT);
|
||||||
|
waypoint3.setDescription("Desc3");
|
||||||
|
providerUtils.insertWaypoint(waypoint1);
|
||||||
|
providerUtils.insertWaypoint(waypoint2);
|
||||||
|
providerUtils.insertWaypoint(waypoint3);
|
||||||
|
|
||||||
|
assertEquals("Desc2", providerUtils.getLastStatisticsWaypoint(trackId).getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests the method {@link MyTracksProviderUtilsImpl#getNextMarkerNumber(long, boolean)}.
|
* Tests the method {@link MyTracksProviderUtilsImpl#getNextMarkerNumber(long, boolean)}.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user