Update TrackRecordingService.

This commit is contained in:
Jimmy Shih
2012-08-08 18:52:08 -07:00
parent 40e2aba739
commit dc9173e015
7 changed files with 938 additions and 1098 deletions
@@ -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;
}
}
File diff suppressed because it is too large Load Diff
@@ -16,6 +16,7 @@
package com.google.android.apps.mytracks.services.tasks;
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.UnitConversions;
@@ -118,7 +119,11 @@ public class PeriodicTaskExecutor {
if (!isDistanceFrequency() || periodicTask == null) {
return;
}
double distance = trackRecordingService.getTripStatistics().getTotalDistance()
TripStatistics tripStatistics = trackRecordingService.getTripStatistics();
if (tripStatistics == null) {
return;
}
double distance = tripStatistics.getTotalDistance()
* UnitConversions.M_TO_KM;
if (!metricUnits) {
distance *= UnitConversions.KM_TO_MI;
@@ -158,13 +163,18 @@ public class PeriodicTaskExecutor {
return;
}
TripStatistics tripStatistics = trackRecordingService.getTripStatistics();
if (tripStatistics == null) {
return;
}
if (!isDistanceFrequency()) {
nextTaskDistance = Double.MAX_VALUE;
Log.d(TAG, "SplitManager: Distance splits disabled.");
return;
}
double distance = trackRecordingService.getTripStatistics().getTotalDistance()
double distance = tripStatistics.getTotalDistance()
* UnitConversions.M_TO_KM;
if (!metricUnits) {
distance *= UnitConversions.KM_TO_MI;
@@ -17,6 +17,7 @@
package com.google.android.apps.mytracks.services.tasks;
import com.google.android.apps.mytracks.services.TrackRecordingService;
import com.google.android.apps.mytracks.stats.TripStatistics;
import java.util.Date;
import java.util.Timer;
@@ -53,6 +54,11 @@ public class TimerTaskExecutor {
return;
}
TripStatistics tripStatistics = trackRecordingService.getTripStatistics();
if (tripStatistics == null) {
return;
}
if (timer != null) {
timer.cancel();
timer.purge();
@@ -67,7 +73,7 @@ public class TimerTaskExecutor {
}
long now = System.currentTimeMillis();
long next = trackRecordingService.getTripStatistics().getStartTime();
long next = tripStatistics.getStartTime();
if (next < now) {
next = now + interval - ((now - next) % interval);
}
@@ -174,6 +174,13 @@ public interface MyTracksProviderUtils {
*/
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
* number.
@@ -443,6 +443,29 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
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
public int getNextMarkerNumber(long trackId, boolean statistics) {
if (trackId < 0) {
@@ -430,7 +430,7 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
/**
* Tests the method {@link MyTracksProviderUtilsImpl#getLastWaypointId(long)}.
*/
public void testGetKLastWaypointId() {
public void testGetLastWaypointId() {
long trackId = System.currentTimeMillis();
Track track = getTrack(trackId, 10);
providerUtils.insertTrack(track);
@@ -448,7 +448,35 @@ public class MyTracksProviderUtilsImplTest extends AndroidTestCase {
assertEquals(-1L, providerUtils.getLastWaypointId(-1));
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)}.
*/