sync from main

This commit is contained in:
Jimmy Shih
2012-01-05 15:37:55 -08:00
12 changed files with 56 additions and 120 deletions
@@ -805,7 +805,7 @@ public class ChartView extends View {
String s =
(mode == Mode.BY_DISTANCE)
? (shortFormat ? X_SHORT_FORMAT.format(x) : X_FORMAT.format(x))
: StringUtils.formatTime((long) x);
: StringUtils.formatElapsedTime((long) x);
c.drawText(s,
getX(x),
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.SensorManagerFactory;
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.protobuf.InvalidProtocolBufferException;
@@ -32,8 +33,6 @@ import android.os.RemoteException;
import android.util.Log;
import android.widget.TextView;
import java.text.DateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
@@ -44,7 +43,6 @@ import java.util.TimerTask;
*/
public class SensorStateActivity extends Activity {
private static final DateFormat TIMESTAMP_FORMAT = DateFormat.getTimeInstance(DateFormat.SHORT);
private static final long REFRESH_PERIOD_MS = 250;
private final StatsUtilities utils;
@@ -263,7 +261,7 @@ public class SensorStateActivity extends Activity {
* @param sds sensor data set
*/
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) {
setText(id, StringUtils.formatTime(l));
setText(id, StringUtils.formatElapsedTime(l));
}
public void setGrade(int id, double d) {
@@ -315,7 +315,7 @@ public class TrackList extends ListActivity
TextView textView = (TextView) view;
if (columnIndex == startTimeIdx) {
long time = cursor.getLong(startTimeIdx);
textView.setText(StringUtils.formatDateTime(time));
textView.setText(StringUtils.formatDateTime(TrackList.this, time));
} else if (columnIndex == totalDistanceIdx) {
double length = cursor.getDouble(totalDistanceIdx);
String lengthUnit = null;
@@ -336,7 +336,7 @@ public class TrackList extends ListActivity
}
}
textView.setText(String.format("%s %.2f %s",
StringUtils.formatTime(cursor.getLong(totalTimeIdx)),
StringUtils.formatElapsedTime(cursor.getLong(totalTimeIdx)),
length,
lengthUnit));
} else {
@@ -269,7 +269,7 @@ public class WaypointsList extends ListActivity
if (time == 0) {
textView.setVisibility(View.GONE);
} else {
textView.setText(StringUtils.formatDateTime(time));
textView.setText(StringUtils.formatDateTime(WaypointsList.this, time));
textView.setVisibility(View.VISIBLE);
}
} else if (columnIndex == typeIdx) {
@@ -156,7 +156,7 @@ public class BackupActivityHelper {
// Make a user-visible version of the backup filenames
final String backupDateStrs[] = new String[backupDates.length];
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
@@ -288,11 +288,9 @@ public class DocsHelper {
DocsTagBuilder tagBuilder = new DocsTagBuilder(metricUnits)
.append("name", track.getName())
.append("description", track.getDescription())
.append("date", StringUtils.formatDateTime(stats.getStartTime()))
.append("totaltime", StringUtils.formatTimeAlwaysShowingHours(
stats.getTotalTime()))
.append("movingtime", StringUtils.formatTimeAlwaysShowingHours(
stats.getMovingTime()))
.append("date", StringUtils.formatDateTime(context, stats.getStartTime()))
.append("totaltime", StringUtils.formatElapsedTime(stats.getTotalTime()))
.append("movingtime", StringUtils.formatElapsedTime(stats.getMovingTime()))
.appendLargeUnits("distance", stats.getTotalDistance() / 1000)
.append("distanceunit", distanceUnit)
.appendLargeUnits("averagespeed", stats.getAverageSpeed() * 3.6)
@@ -44,7 +44,7 @@ class DefaultTrackNameFactory {
*/
String newTrackName(long trackId, long startTime) {
if (useTimestampTrackName()) {
return StringUtils.formatDateTime(startTime);
return StringUtils.formatDateTime(context, startTime);
} else {
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.SharedPreferences;
import android.text.format.DateUtils;
import java.text.DateFormat;
import java.text.NumberFormat;
@@ -46,35 +47,34 @@ public class StringUtils implements DescriptionGenerator {
private final Context context;
/**
* Formats the time based on user locale.
*
* @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.
* Formats the date and time based on user's phone date/time preferences.
*
* @param time - A period of time in milliseconds.
* @return A string of the format M:SS, MM:SS or HH:MM:SS
* @param context the context
* @param time the time in milliseconds
*/
public static String formatTime(long time) {
return formatTimeInternal(time, false);
public static String formatDateTime(Context context, long time) {
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
* hours to be shown even when it is zero, e.g. exporting data to a
* spreadsheet.
* Formats the time based on user's phone date/time preferences.
*
* @param time - A period of time in milliseconds
* @return A string of the format HH:MM:SS even if time is less than 1 hour
* @param context the context
* @param time the time in milliseconds
*/
public static String formatTimeAlwaysShowingHours(long time) {
return formatTimeInternal(time, true);
public static String formatTime(Context context, long time) {
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();
@@ -182,35 +182,6 @@ public class StringUtils implements DescriptionGenerator {
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.
*/
@@ -328,11 +299,11 @@ public class StringUtils implements DescriptionGenerator {
// Line 3
context.getString(R.string.stat_total_time),
StringUtils.formatTime(trackStats.getTotalTime()),
StringUtils.formatElapsedTime(trackStats.getTotalTime()),
// Line 4
context.getString(R.string.stat_moving_time),
StringUtils.formatTime(trackStats.getMovingTime()),
StringUtils.formatElapsedTime(trackStats.getMovingTime()),
// Line 5
averageSpeed, averageMovingSpeed, maxSpeed,
@@ -360,7 +331,7 @@ public class StringUtils implements DescriptionGenerator {
// Line 11
context.getString(R.string.send_google_recorded),
StringUtils.formatDateTime(trackStats.getStartTime()),
StringUtils.formatDateTime(context, trackStats.getStartTime()),
// Line 12
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),
distanceInMiles, context.getString(R.string.unit_mile),
context.getString(R.string.stat_total_time),
StringUtils.formatTime(stats.getTotalTime()),
StringUtils.formatElapsedTime(stats.getTotalTime()),
context.getString(R.string.stat_moving_time),
StringUtils.formatTime(stats.getMovingTime()),
StringUtils.formatElapsedTime(stats.getMovingTime()),
context.getString(R.string.stat_average_speed),
averageSpeedInKmh, context.getString(R.string.unit_kilometer_per_hour),
averageSpeedInMph, context.getString(R.string.unit_mile_per_hour),
@@ -224,7 +224,7 @@ public class TrackWidgetProvider
StringUtils.formatSingleDecimalPlace(displayDistance) + " " + this.distanceLabel;
// convert ms to minutes
String time = StringUtils.formatTime(stats.getMovingTime());
String time = StringUtils.formatElapsedTime(stats.getMovingTime());
String speed = unknown;
if (!Double.isNaN(stats.getAverageMovingSpeed())) {
// Convert m/s to km/h
@@ -236,7 +236,7 @@ public class TrackWidgetProvider
speed = StringUtils.formatSingleDecimalPlace(displaySpeed) + " " + this.speedLabel;
} else {
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.stats.TripStatistics;
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.UsesMocks;
import android.content.Context;
import android.content.res.Resources;
import android.test.mock.MockContext;
import android.test.AndroidTestCase;
import android.test.mock.MockResources;
import java.io.IOException;
import junit.framework.TestCase;
/**
* Tests for {@link DocsHelper#addTrackRow}
*
* @author Matthew Simmons
*/
public class DocsHelper_AddTrackRowTest extends TestCase {
public class DocsHelper_AddTrackRowTest extends AndroidTestCase {
private static final long TIME = 1288721514000L;
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'>"
+ "<gsx:name><![CDATA[trackName]]></gsx:name>"
+ "<gsx:description><![CDATA[trackDescription]]></gsx:description>"
+ "<gsx:date><![CDATA[" + StringUtils.formatDateTime(TIME) + "]]></gsx:date>"
+ "<gsx:totaltime><![CDATA[0:00:05]]></gsx:totaltime>"
+ "<gsx:movingtime><![CDATA[0:00:04]]></gsx:movingtime>"
+ "<gsx:date><![CDATA[" + StringUtils.formatDateTime(getContext(), TIME) + "]]></gsx:date>"
+ "<gsx:totaltime><![CDATA[00:05]]></gsx:totaltime>"
+ "<gsx:movingtime><![CDATA[00:04]]></gsx:movingtime>"
+ "<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:averagemovingspeed><![CDATA[11,184.68]]>"
+ "</gsx:averagemovingspeed>"
+ "<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:minelevation><![CDATA[-1,640]]></gsx:minelevation>"
+ "<gsx:maxelevation><![CDATA[1,804]]></gsx:maxelevation>"
+ "<gsx:elevationunit><![CDATA[feet]]></gsx:elevationunit>"
+ "<gsx:elevationunit><![CDATA[ft]]></gsx:elevationunit>"
+ "<gsx:map>"
+ "<![CDATA[https://maps.google.com/maps/ms?msa=0&msid=trackMapId]]>"
+ "</gsx:map>"
@@ -97,9 +92,9 @@ public class DocsHelper_AddTrackRowTest extends TestCase {
assertTrue(docsHelper.writtenData.contains(
"<gsx:distanceunit><![CDATA[km]]></gsx:distanceunit>"));
assertTrue(docsHelper.writtenData.contains(
"<gsx:speedunit><![CDATA[kph]]></gsx:speedunit>"));
"<gsx:speedunit><![CDATA[km/h]]></gsx:speedunit>"));
assertTrue(docsHelper.writtenData.contains(
"<gsx:elevationunit><![CDATA[meter]]></gsx:elevationunit>"));
"<gsx:elevationunit><![CDATA[m]]></gsx:elevationunit>"));
assertTrue(docsHelper.writtenData.contains(
"<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. */
@UsesMocks({AuthManager.class, MockResources.class, Track.class})
private void addTrackRow(DocsHelper docsHelper, boolean useMetric)
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;
}
};
private void addTrackRow(DocsHelper docsHelper, boolean useMetric) throws IOException {
AuthManager mockAuthManager = AndroidMock.createMock(AuthManager.class);
AndroidMock.replay(mockAuthManager);
@@ -154,7 +122,7 @@ public class DocsHelper_AddTrackRowTest extends TestCase {
track.setMapId("trackMapId");
track.setStatistics(stats);
docsHelper.addTrackRow(mockContext, mockAuthManager, "ssid", "wsid",
docsHelper.addTrackRow(getContext(), mockAuthManager, "ssid", "wsid",
track, useMetric);
}
}
@@ -50,8 +50,9 @@ public class DefaultTrackNameFactoryTest extends AndroidTestCase {
public void testTimestampTrackName() {
DefaultTrackNameFactory factory =
new MockDefaultTrackNameFactory(getContext(), true);
assertEquals(StringUtils.formatDateTime(TIMESTAMP), factory.newTrackName(1, TIMESTAMP));
assertEquals(
StringUtils.formatDateTime(getContext(), TIMESTAMP), factory.newTrackName(1, TIMESTAMP));
}
public void testIncrementingTrackName() {