merge heads

This commit is contained in:
Jimmy Shih
2012-01-04 17:17:41 -08:00
parent 8db6f5b569
commit 23ca7bd05e
12 changed files with 56 additions and 120 deletions
@@ -805,7 +805,7 @@ public class ChartView extends View {
String s = String s =
(mode == Mode.BY_DISTANCE) (mode == Mode.BY_DISTANCE)
? (shortFormat ? X_SHORT_FORMAT.format(x) : X_FORMAT.format(x)) ? (shortFormat ? X_SHORT_FORMAT.format(x) : X_FORMAT.format(x))
: StringUtils.formatTime((long) x); : StringUtils.formatElapsedTime((long) x);
c.drawText(s, c.drawText(s,
getX(x), getX(x),
effectiveHeight + UNIT_BORDER + topBorder, effectiveHeight + UNIT_BORDER + topBorder,
@@ -23,6 +23,7 @@ import com.google.android.apps.mytracks.services.TrackRecordingServiceConnection
import com.google.android.apps.mytracks.services.sensors.SensorManager; import com.google.android.apps.mytracks.services.sensors.SensorManager;
import com.google.android.apps.mytracks.services.sensors.SensorManagerFactory; import com.google.android.apps.mytracks.services.sensors.SensorManagerFactory;
import com.google.android.apps.mytracks.services.sensors.SensorUtils; import com.google.android.apps.mytracks.services.sensors.SensorUtils;
import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.InvalidProtocolBufferException;
@@ -32,8 +33,6 @@ import android.os.RemoteException;
import android.util.Log; import android.util.Log;
import android.widget.TextView; import android.widget.TextView;
import java.text.DateFormat;
import java.util.Date;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
@@ -44,7 +43,6 @@ import java.util.TimerTask;
*/ */
public class SensorStateActivity extends Activity { public class SensorStateActivity extends Activity {
private static final DateFormat TIMESTAMP_FORMAT = DateFormat.getTimeInstance(DateFormat.SHORT);
private static final long REFRESH_PERIOD_MS = 250; private static final long REFRESH_PERIOD_MS = 250;
private final StatsUtilities utils; private final StatsUtilities utils;
@@ -263,7 +261,7 @@ public class SensorStateActivity extends Activity {
* @param sds sensor data set * @param sds sensor data set
*/ */
private String getLastSensorTime(Sensor.SensorDataSet sds) { private String getLastSensorTime(Sensor.SensorDataSet sds) {
return TIMESTAMP_FORMAT.format(new Date(sds.getCreationTime())); return StringUtils.formatTime(this, sds.getCreationTime());
} }
/** /**
@@ -153,7 +153,7 @@ public class StatsUtilities {
} }
public void setTime(int id, long l) { public void setTime(int id, long l) {
setText(id, StringUtils.formatTime(l)); setText(id, StringUtils.formatElapsedTime(l));
} }
public void setGrade(int id, double d) { public void setGrade(int id, double d) {
@@ -310,7 +310,7 @@ public class TrackList extends ListActivity
TextView textView = (TextView) view; TextView textView = (TextView) view;
if (columnIndex == startTimeIdx) { if (columnIndex == startTimeIdx) {
long time = cursor.getLong(startTimeIdx); long time = cursor.getLong(startTimeIdx);
textView.setText(StringUtils.formatDateTime(time)); textView.setText(StringUtils.formatDateTime(TrackList.this, time));
} else if (columnIndex == totalDistanceIdx) { } else if (columnIndex == totalDistanceIdx) {
double length = cursor.getDouble(totalDistanceIdx); double length = cursor.getDouble(totalDistanceIdx);
String lengthUnit = null; String lengthUnit = null;
@@ -331,7 +331,7 @@ public class TrackList extends ListActivity
} }
} }
textView.setText(String.format("%s %.2f %s", textView.setText(String.format("%s %.2f %s",
StringUtils.formatTime(cursor.getLong(totalTimeIdx)), StringUtils.formatElapsedTime(cursor.getLong(totalTimeIdx)),
length, length,
lengthUnit)); lengthUnit));
} else { } else {
@@ -269,7 +269,7 @@ public class WaypointsList extends ListActivity
if (time == 0) { if (time == 0) {
textView.setVisibility(View.GONE); textView.setVisibility(View.GONE);
} else { } else {
textView.setText(StringUtils.formatDateTime(time)); textView.setText(StringUtils.formatDateTime(WaypointsList.this, time));
textView.setVisibility(View.VISIBLE); textView.setVisibility(View.VISIBLE);
} }
} else if (columnIndex == typeIdx) { } else if (columnIndex == typeIdx) {
@@ -156,7 +156,7 @@ public class BackupActivityHelper {
// Make a user-visible version of the backup filenames // Make a user-visible version of the backup filenames
final String backupDateStrs[] = new String[backupDates.length]; final String backupDateStrs[] = new String[backupDates.length];
for (int i = 0; i < backupDates.length; i++) { for (int i = 0; i < backupDates.length; i++) {
backupDateStrs[i] = StringUtils.formatDateTime(backupDates[i].getTime()); backupDateStrs[i] = StringUtils.formatDateTime(activity, backupDates[i].getTime());
} }
// Show a dialog for the user to pick which backup to restore // Show a dialog for the user to pick which backup to restore
@@ -288,11 +288,9 @@ public class DocsHelper {
DocsTagBuilder tagBuilder = new DocsTagBuilder(metricUnits) DocsTagBuilder tagBuilder = new DocsTagBuilder(metricUnits)
.append("name", track.getName()) .append("name", track.getName())
.append("description", track.getDescription()) .append("description", track.getDescription())
.append("date", StringUtils.formatDateTime(stats.getStartTime())) .append("date", StringUtils.formatDateTime(context, stats.getStartTime()))
.append("totaltime", StringUtils.formatTimeAlwaysShowingHours( .append("totaltime", StringUtils.formatElapsedTime(stats.getTotalTime()))
stats.getTotalTime())) .append("movingtime", StringUtils.formatElapsedTime(stats.getMovingTime()))
.append("movingtime", StringUtils.formatTimeAlwaysShowingHours(
stats.getMovingTime()))
.appendLargeUnits("distance", stats.getTotalDistance() / 1000) .appendLargeUnits("distance", stats.getTotalDistance() / 1000)
.append("distanceunit", distanceUnit) .append("distanceunit", distanceUnit)
.appendLargeUnits("averagespeed", stats.getAverageSpeed() * 3.6) .appendLargeUnits("averagespeed", stats.getAverageSpeed() * 3.6)
@@ -44,7 +44,7 @@ class DefaultTrackNameFactory {
*/ */
String newTrackName(long trackId, long startTime) { String newTrackName(long trackId, long startTime) {
if (useTimestampTrackName()) { if (useTimestampTrackName()) {
return StringUtils.formatDateTime(startTime); return StringUtils.formatDateTime(context, startTime);
} else { } else {
return String.format(context.getString(R.string.track_name_format), trackId); return String.format(context.getString(R.string.track_name_format), trackId);
} }
@@ -24,6 +24,7 @@ import com.google.android.maps.mytracks.R;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.text.format.DateUtils;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.NumberFormat; import java.text.NumberFormat;
@@ -46,35 +47,34 @@ public class StringUtils implements DescriptionGenerator {
private final Context context; private final Context context;
/** /**
* Formats the time based on user locale. * Formats the date and time based on user's phone date/time preferences.
*
* @param time time
*/
public static String formatDateTime(long time) {
DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
return formatter.format(new Date(time));
}
/**
* Formats a number of milliseconds as a string.
* *
* @param time - A period of time in milliseconds. * @param context the context
* @return A string of the format M:SS, MM:SS or HH:MM:SS * @param time the time in milliseconds
*/ */
public static String formatTime(long time) { public static String formatDateTime(Context context, long time) {
return formatTimeInternal(time, false); DateFormat dateFormatter = android.text.format.DateFormat.getDateFormat(context);
return dateFormatter.format(new Date(time)) + " " + formatTime(context, time);
} }
/** /**
* Formats a number of milliseconds as a string. To be used when we need the * Formats the time based on user's phone date/time preferences.
* hours to be shown even when it is zero, e.g. exporting data to a
* spreadsheet.
* *
* @param time - A period of time in milliseconds * @param context the context
* @return A string of the format HH:MM:SS even if time is less than 1 hour * @param time the time in milliseconds
*/ */
public static String formatTimeAlwaysShowingHours(long time) { public static String formatTime(Context context, long time) {
return formatTimeInternal(time, true); DateFormat timeFormatter = android.text.format.DateFormat.getTimeFormat(context);
return timeFormatter.format(new Date(time));
}
/**
* Formats the elapsed timed in the form "MM:SS" or "H:MM:SS".
*
* @param time the time in milliseconds
*/
public static String formatElapsedTime(long time) {
return DateUtils.formatElapsedTime(time / 1000);
} }
private static final NumberFormat SINGLE_DECIMAL_PLACE_FORMAT = NumberFormat.getNumberInstance(); private static final NumberFormat SINGLE_DECIMAL_PLACE_FORMAT = NumberFormat.getNumberInstance();
@@ -182,35 +182,6 @@ public class StringUtils implements DescriptionGenerator {
return time; return time;
} }
/**
* Formats a number of milliseconds as a string.
*
* @param time - A period of time in milliseconds
* @param alwaysShowHours - Whether to display 00 hours if time is less than 1
* hour
* @return A string of the format HH:MM:SS
*/
private static String formatTimeInternal(long time, boolean alwaysShowHours) {
int[] parts = getTimeParts(time);
StringBuilder builder = new StringBuilder();
if (parts[2] > 0 || alwaysShowHours) {
builder.append(parts[2]);
builder.append(':');
if (parts[1] <= 9) {
builder.append("0");
}
}
builder.append(parts[1]);
builder.append(':');
if (parts[0] <= 9) {
builder.append("0");
}
builder.append(parts[0]);
return builder.toString();
}
/** /**
* Gets the time as an array of parts. * Gets the time as an array of parts.
*/ */
@@ -328,11 +299,11 @@ public class StringUtils implements DescriptionGenerator {
// Line 3 // Line 3
context.getString(R.string.stat_total_time), context.getString(R.string.stat_total_time),
StringUtils.formatTime(trackStats.getTotalTime()), StringUtils.formatElapsedTime(trackStats.getTotalTime()),
// Line 4 // Line 4
context.getString(R.string.stat_moving_time), context.getString(R.string.stat_moving_time),
StringUtils.formatTime(trackStats.getMovingTime()), StringUtils.formatElapsedTime(trackStats.getMovingTime()),
// Line 5 // Line 5
averageSpeed, averageMovingSpeed, maxSpeed, averageSpeed, averageMovingSpeed, maxSpeed,
@@ -360,7 +331,7 @@ public class StringUtils implements DescriptionGenerator {
// Line 11 // Line 11
context.getString(R.string.send_google_recorded), context.getString(R.string.send_google_recorded),
StringUtils.formatDateTime(trackStats.getStartTime()), StringUtils.formatDateTime(context, trackStats.getStartTime()),
// Line 12 // Line 12
context.getString(R.string.track_detail_activity_type_hint), category, context.getString(R.string.track_detail_activity_type_hint), category,
@@ -470,9 +441,9 @@ public class StringUtils implements DescriptionGenerator {
distanceInKm, context.getString(R.string.unit_kilometer), distanceInKm, context.getString(R.string.unit_kilometer),
distanceInMiles, context.getString(R.string.unit_mile), distanceInMiles, context.getString(R.string.unit_mile),
context.getString(R.string.stat_total_time), context.getString(R.string.stat_total_time),
StringUtils.formatTime(stats.getTotalTime()), StringUtils.formatElapsedTime(stats.getTotalTime()),
context.getString(R.string.stat_moving_time), context.getString(R.string.stat_moving_time),
StringUtils.formatTime(stats.getMovingTime()), StringUtils.formatElapsedTime(stats.getMovingTime()),
context.getString(R.string.stat_average_speed), context.getString(R.string.stat_average_speed),
averageSpeedInKmh, context.getString(R.string.unit_kilometer_per_hour), averageSpeedInKmh, context.getString(R.string.unit_kilometer_per_hour),
averageSpeedInMph, context.getString(R.string.unit_mile_per_hour), averageSpeedInMph, context.getString(R.string.unit_mile_per_hour),
@@ -224,7 +224,7 @@ public class TrackWidgetProvider
StringUtils.formatSingleDecimalPlace(displayDistance) + " " + this.distanceLabel; StringUtils.formatSingleDecimalPlace(displayDistance) + " " + this.distanceLabel;
// convert ms to minutes // convert ms to minutes
String time = StringUtils.formatTime(stats.getMovingTime()); String time = StringUtils.formatElapsedTime(stats.getMovingTime());
String speed = unknown; String speed = unknown;
if (!Double.isNaN(stats.getAverageMovingSpeed())) { if (!Double.isNaN(stats.getAverageMovingSpeed())) {
// Convert m/s to km/h // Convert m/s to km/h
@@ -236,7 +236,7 @@ public class TrackWidgetProvider
speed = StringUtils.formatSingleDecimalPlace(displaySpeed) + " " + this.speedLabel; speed = StringUtils.formatSingleDecimalPlace(displaySpeed) + " " + this.speedLabel;
} else { } else {
long displayPace = (long) (3600000.0 / displaySpeed); long displayPace = (long) (3600000.0 / displaySpeed);
speed = StringUtils.formatTime(displayPace) + " " + paceLabel; speed = StringUtils.formatElapsedTime(displayPace) + " " + paceLabel;
} }
} }
@@ -19,25 +19,20 @@ import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.io.AuthManager; import com.google.android.apps.mytracks.io.AuthManager;
import com.google.android.apps.mytracks.stats.TripStatistics; import com.google.android.apps.mytracks.stats.TripStatistics;
import com.google.android.apps.mytracks.util.StringUtils; import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.maps.mytracks.R;
import com.google.android.testing.mocking.AndroidMock; import com.google.android.testing.mocking.AndroidMock;
import com.google.android.testing.mocking.UsesMocks; import com.google.android.testing.mocking.UsesMocks;
import android.content.Context; import android.test.AndroidTestCase;
import android.content.res.Resources;
import android.test.mock.MockContext;
import android.test.mock.MockResources; import android.test.mock.MockResources;
import java.io.IOException; import java.io.IOException;
import junit.framework.TestCase;
/** /**
* Tests for {@link DocsHelper#addTrackRow} * Tests for {@link DocsHelper#addTrackRow}
* *
* @author Matthew Simmons * @author Matthew Simmons
*/ */
public class DocsHelper_AddTrackRowTest extends TestCase { public class DocsHelper_AddTrackRowTest extends AndroidTestCase {
private static final long TIME = 1288721514000L; private static final long TIME = 1288721514000L;
private static class StringWritingDocsHelper extends DocsHelper { private static class StringWritingDocsHelper extends DocsHelper {
@@ -61,20 +56,20 @@ public class DocsHelper_AddTrackRowTest extends TestCase {
+ "xmlns:gsx='http://schemas.google.com/spreadsheets/2006/extended'>" + "xmlns:gsx='http://schemas.google.com/spreadsheets/2006/extended'>"
+ "<gsx:name><![CDATA[trackName]]></gsx:name>" + "<gsx:name><![CDATA[trackName]]></gsx:name>"
+ "<gsx:description><![CDATA[trackDescription]]></gsx:description>" + "<gsx:description><![CDATA[trackDescription]]></gsx:description>"
+ "<gsx:date><![CDATA[" + StringUtils.formatDateTime(TIME) + "]]></gsx:date>" + "<gsx:date><![CDATA[" + StringUtils.formatDateTime(getContext(), TIME) + "]]></gsx:date>"
+ "<gsx:totaltime><![CDATA[0:00:05]]></gsx:totaltime>" + "<gsx:totaltime><![CDATA[00:05]]></gsx:totaltime>"
+ "<gsx:movingtime><![CDATA[0:00:04]]></gsx:movingtime>" + "<gsx:movingtime><![CDATA[00:04]]></gsx:movingtime>"
+ "<gsx:distance><![CDATA[12.43]]></gsx:distance>" + "<gsx:distance><![CDATA[12.43]]></gsx:distance>"
+ "<gsx:distanceunit><![CDATA[mile]]></gsx:distanceunit>" + "<gsx:distanceunit><![CDATA[mi]]></gsx:distanceunit>"
+ "<gsx:averagespeed><![CDATA[8,947.75]]></gsx:averagespeed>" + "<gsx:averagespeed><![CDATA[8,947.75]]></gsx:averagespeed>"
+ "<gsx:averagemovingspeed><![CDATA[11,184.68]]>" + "<gsx:averagemovingspeed><![CDATA[11,184.68]]>"
+ "</gsx:averagemovingspeed>" + "</gsx:averagemovingspeed>"
+ "<gsx:maxspeed><![CDATA[3,355.40]]></gsx:maxspeed>" + "<gsx:maxspeed><![CDATA[3,355.40]]></gsx:maxspeed>"
+ "<gsx:speedunit><![CDATA[mph]]></gsx:speedunit>" + "<gsx:speedunit><![CDATA[mi/h]]></gsx:speedunit>"
+ "<gsx:elevationgain><![CDATA[19,685]]></gsx:elevationgain>" + "<gsx:elevationgain><![CDATA[19,685]]></gsx:elevationgain>"
+ "<gsx:minelevation><![CDATA[-1,640]]></gsx:minelevation>" + "<gsx:minelevation><![CDATA[-1,640]]></gsx:minelevation>"
+ "<gsx:maxelevation><![CDATA[1,804]]></gsx:maxelevation>" + "<gsx:maxelevation><![CDATA[1,804]]></gsx:maxelevation>"
+ "<gsx:elevationunit><![CDATA[feet]]></gsx:elevationunit>" + "<gsx:elevationunit><![CDATA[ft]]></gsx:elevationunit>"
+ "<gsx:map>" + "<gsx:map>"
+ "<![CDATA[https://maps.google.com/maps/ms?msa=0&msid=trackMapId]]>" + "<![CDATA[https://maps.google.com/maps/ms?msa=0&msid=trackMapId]]>"
+ "</gsx:map>" + "</gsx:map>"
@@ -97,9 +92,9 @@ public class DocsHelper_AddTrackRowTest extends TestCase {
assertTrue(docsHelper.writtenData.contains( assertTrue(docsHelper.writtenData.contains(
"<gsx:distanceunit><![CDATA[km]]></gsx:distanceunit>")); "<gsx:distanceunit><![CDATA[km]]></gsx:distanceunit>"));
assertTrue(docsHelper.writtenData.contains( assertTrue(docsHelper.writtenData.contains(
"<gsx:speedunit><![CDATA[kph]]></gsx:speedunit>")); "<gsx:speedunit><![CDATA[km/h]]></gsx:speedunit>"));
assertTrue(docsHelper.writtenData.contains( assertTrue(docsHelper.writtenData.contains(
"<gsx:elevationunit><![CDATA[meter]]></gsx:elevationunit>")); "<gsx:elevationunit><![CDATA[m]]></gsx:elevationunit>"));
assertTrue(docsHelper.writtenData.contains( assertTrue(docsHelper.writtenData.contains(
"<gsx:distance><![CDATA[20.00]]></gsx:distance>")); "<gsx:distance><![CDATA[20.00]]></gsx:distance>"));
@@ -107,34 +102,7 @@ public class DocsHelper_AddTrackRowTest extends TestCase {
/** Adds a row to the spreadsheet, using the provided helper. */ /** Adds a row to the spreadsheet, using the provided helper. */
@UsesMocks({AuthManager.class, MockResources.class, Track.class}) @UsesMocks({AuthManager.class, MockResources.class, Track.class})
private void addTrackRow(DocsHelper docsHelper, boolean useMetric) private void addTrackRow(DocsHelper docsHelper, boolean useMetric) throws IOException {
throws IOException {
final Resources mockResources = AndroidMock.createMock(MockResources.class);
if (useMetric) {
AndroidMock.expect(mockResources.getString(R.string.unit_kilometer))
.andReturn("km");
AndroidMock.expect(mockResources.getString(R.string.unit_kilometer_per_hour))
.andReturn("kph");
AndroidMock.expect(mockResources.getString(R.string.unit_meter))
.andReturn("meter");
} else {
AndroidMock.expect(mockResources.getString(R.string.unit_mile))
.andReturn("mile");
AndroidMock.expect(mockResources.getString(R.string.unit_mile_per_hour))
.andReturn("mph");
AndroidMock.expect(mockResources.getString(R.string.unit_feet))
.andReturn("feet");
}
AndroidMock.replay(mockResources);
Context mockContext = new MockContext() {
@Override
public Resources getResources() {
return mockResources;
}
};
AuthManager mockAuthManager = AndroidMock.createMock(AuthManager.class); AuthManager mockAuthManager = AndroidMock.createMock(AuthManager.class);
AndroidMock.replay(mockAuthManager); AndroidMock.replay(mockAuthManager);
@@ -154,7 +122,7 @@ public class DocsHelper_AddTrackRowTest extends TestCase {
track.setMapId("trackMapId"); track.setMapId("trackMapId");
track.setStatistics(stats); track.setStatistics(stats);
docsHelper.addTrackRow(mockContext, mockAuthManager, "ssid", "wsid", docsHelper.addTrackRow(getContext(), mockAuthManager, "ssid", "wsid",
track, useMetric); track, useMetric);
} }
} }
@@ -50,8 +50,9 @@ public class DefaultTrackNameFactoryTest extends AndroidTestCase {
public void testTimestampTrackName() { public void testTimestampTrackName() {
DefaultTrackNameFactory factory = DefaultTrackNameFactory factory =
new MockDefaultTrackNameFactory(getContext(), true); new MockDefaultTrackNameFactory(getContext(), true);
assertEquals(StringUtils.formatDateTime(TIMESTAMP), factory.newTrackName(1, TIMESTAMP)); assertEquals(
StringUtils.formatDateTime(getContext(), TIMESTAMP), factory.newTrackName(1, TIMESTAMP));
} }
public void testIncrementingTrackName() { public void testIncrementingTrackName() {