Merge heads

This commit is contained in:
youtaol
2012-08-27 10:31:25 +08:00
parent 3ef2a9c901
commit 9565e81dab
23 changed files with 1376 additions and 1638 deletions
@@ -27,7 +27,7 @@ import com.google.android.apps.mytracks.content.TrackDataListener;
import com.google.android.apps.mytracks.content.TrackDataType;
import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.stats.TripStatistics;
import com.google.android.apps.mytracks.stats.TripStatisticsBuilder;
import com.google.android.apps.mytracks.stats.TripStatisticsUpdater;
import com.google.android.apps.mytracks.util.LocationUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
@@ -61,7 +61,7 @@ public class ChartFragment extends Fragment implements TrackDataListener {
private TrackDataHub trackDataHub;
// Stats gathered from the received data
private TripStatisticsBuilder tripStatisticsBuilder;
private TripStatisticsUpdater tripStatisticsUpdater;
private long startTime;
private boolean metricUnits = PreferencesUtils.METRIC_UNITS_DEFAULT;
@@ -83,7 +83,7 @@ public class ChartFragment extends Fragment implements TrackDataListener {
private final Runnable updateChart = new Runnable() {
@Override
public void run() {
if (trackDataHub == null) {
if (!isResumed() || trackDataHub == null) {
return;
}
@@ -178,111 +178,141 @@ public class ChartFragment extends Fragment implements TrackDataListener {
@Override
public void onTrackUpdated(Track track) {
if (track == null || track.getTripStatistics() == null) {
startTime = -1L;
return;
if (isResumed()) {
if (track == null || track.getTripStatistics() == null) {
startTime = -1L;
return;
}
startTime = track.getTripStatistics().getStartTime();
}
startTime = track.getTripStatistics().getStartTime();
}
@Override
public void clearTrackPoints() {
tripStatisticsBuilder = startTime != -1L ? new TripStatisticsBuilder(startTime) : null;
pendingPoints.clear();
chartView.reset();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
chartView.resetScroll();
}
});
if (isResumed()) {
tripStatisticsUpdater = startTime != -1L ? new TripStatisticsUpdater(startTime) : null;
pendingPoints.clear();
chartView.reset();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (isResumed()) {
chartView.resetScroll();
}
}
});
}
}
@Override
public void onSampledInTrackPoint(Location location) {
double[] data = new double[ChartView.NUM_SERIES + 1];
fillDataPoint(location, data);
pendingPoints.add(data);
if (isResumed()) {
double[] data = new double[ChartView.NUM_SERIES + 1];
fillDataPoint(location, data);
pendingPoints.add(data);
}
}
@Override
public void onSampledOutTrackPoint(Location location) {
fillDataPoint(location, null);
if (isResumed()) {
fillDataPoint(location, null);
}
}
@Override
public void onSegmentSplit(Location location) {
fillDataPoint(location, null);
if (isResumed()) {
fillDataPoint(location, null);
}
}
@Override
public void onNewTrackPointsDone() {
chartView.addDataPoints(pendingPoints);
pendingPoints.clear();
getActivity().runOnUiThread(updateChart);
if (isResumed()) {
chartView.addDataPoints(pendingPoints);
pendingPoints.clear();
getActivity().runOnUiThread(updateChart);
}
}
@Override
public void clearWaypoints() {
chartView.clearWaypoints();
if (isResumed()) {
chartView.clearWaypoints();
}
}
@Override
public void onNewWaypoint(Waypoint waypoint) {
if (waypoint != null && LocationUtils.isValidLocation(waypoint.getLocation())) {
if (isResumed() && waypoint != null && LocationUtils.isValidLocation(waypoint.getLocation())) {
chartView.addWaypoint(waypoint);
}
}
@Override
public void onNewWaypointsDone() {
getActivity().runOnUiThread(updateChart);
if (isResumed()) {
getActivity().runOnUiThread(updateChart);
}
}
@Override
public boolean onMetricUnitsChanged(boolean metric) {
if (metricUnits == metric) {
return false;
}
metricUnits = metric;
chartView.setMetricUnits(metricUnits);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
chartView.requestLayout();
if (isResumed()) {
if (metricUnits == metric) {
return false;
}
});
return true;
metricUnits = metric;
chartView.setMetricUnits(metricUnits);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (isResumed()) {
chartView.requestLayout();
}
}
});
return true;
}
return false;
}
@Override
public boolean onReportSpeedChanged(boolean speed) {
if (reportSpeed == speed) {
return false;
}
reportSpeed = speed;
chartView.setReportSpeed(reportSpeed);
boolean chartShowSpeed = PreferencesUtils.getBoolean(
getActivity(), R.string.chart_show_speed_key, PreferencesUtils.CHART_SHOW_SPEED_DEFAULT);
setSeriesEnabled(ChartView.SPEED_SERIES, chartShowSpeed && reportSpeed);
setSeriesEnabled(ChartView.PACE_SERIES, chartShowSpeed && !reportSpeed);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
chartView.requestLayout();
if (isResumed()) {
if (reportSpeed == speed) {
return false;
}
});
return true;
reportSpeed = speed;
chartView.setReportSpeed(reportSpeed);
boolean chartShowSpeed = PreferencesUtils.getBoolean(
getActivity(), R.string.chart_show_speed_key, PreferencesUtils.CHART_SHOW_SPEED_DEFAULT);
setSeriesEnabled(ChartView.SPEED_SERIES, chartShowSpeed && reportSpeed);
setSeriesEnabled(ChartView.PACE_SERIES, chartShowSpeed && !reportSpeed);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (isResumed()) {
chartView.requestLayout();
}
}
});
return true;
}
return false;
}
@Override
public boolean onMinRecordingDistanceChanged(int value) {
if (minRecordingDistance == value) {
return false;
if (isResumed()) {
if (minRecordingDistance == value) {
return false;
}
minRecordingDistance = value;
return true;
}
minRecordingDistance = value;
return true;
return false;
}
/**
@@ -424,9 +454,9 @@ public class ChartFragment extends Fragment implements TrackDataListener {
double cadence = Double.NaN;
double power = Double.NaN;
if (tripStatisticsBuilder != null) {
tripStatisticsBuilder.addLocation(location, minRecordingDistance);
TripStatistics tripStatistics = tripStatisticsBuilder.getTripStatistics();
if (tripStatisticsUpdater != null) {
tripStatisticsUpdater.addLocation(location, minRecordingDistance);
TripStatistics tripStatistics = tripStatisticsUpdater.getTripStatistics();
if (chartByDistance) {
double distance = tripStatistics.getTotalDistance() * UnitConversions.M_TO_KM;
if (!metricUnits) {
@@ -436,13 +466,13 @@ public class ChartFragment extends Fragment implements TrackDataListener {
} else {
timeOrDistance = tripStatistics.getTotalTime();
}
elevation = tripStatisticsBuilder.getSmoothedElevation();
elevation = tripStatisticsUpdater.getSmoothedElevation();
if (!metricUnits) {
elevation *= UnitConversions.M_TO_FT;
}
speed = tripStatisticsBuilder.getSmoothedSpeed() * UnitConversions.MS_TO_KMH;
speed = tripStatisticsUpdater.getSmoothedSpeed() * UnitConversions.MS_TO_KMH;
if (!metricUnits) {
speed *= UnitConversions.KM_TO_MI;
}
@@ -485,8 +515,8 @@ public class ChartFragment extends Fragment implements TrackDataListener {
}
@VisibleForTesting
void setTripStatisticsBuilder(long time) {
tripStatisticsBuilder = new TripStatisticsBuilder(time);
void setTripStatisticsUpdater(long time) {
tripStatisticsUpdater = new TripStatisticsUpdater(time);
}
@VisibleForTesting
@@ -275,93 +275,104 @@ public class MapFragment extends Fragment
@Override
public void onLocationStateChanged(LocationState state) {
final String message;
final boolean isGpsDisabled;
if (!isSelectedTrackRecording()) {
message = null;
isGpsDisabled = false;
} else {
switch (state) {
case DISABLED:
String setting = getString(
GoogleLocationUtils.isAvailable(getActivity()) ? R.string.gps_google_location_settings
: R.string.gps_location_access);
message = getString(R.string.gps_disabled, setting);
isGpsDisabled = true;
break;
case NO_FIX:
message = getString(R.string.gps_wait_for_signal);
isGpsDisabled = false;
break;
case BAD_FIX:
message = getString(R.string.gps_wait_for_better_signal);
isGpsDisabled = false;
break;
case GOOD_FIX:
message = null;
isGpsDisabled = false;
break;
default:
throw new IllegalArgumentException("Unexpected state: " + state);
}
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (message != null) {
messageTextView.setText(message);
messageTextView.setVisibility(View.VISIBLE);
if (isGpsDisabled) {
Toast.makeText(getActivity(), R.string.gps_not_found, Toast.LENGTH_LONG).show();
// Click to show the location source settings
messageTextView.setOnClickListener(MapFragment.this);
} else {
messageTextView.setOnClickListener(null);
}
} else {
messageTextView.setVisibility(View.GONE);
if (isResumed()) {
final String message;
final boolean isGpsDisabled;
if (!isSelectedTrackRecording()) {
message = null;
isGpsDisabled = false;
} else {
switch (state) {
case DISABLED:
String setting = getString(
GoogleLocationUtils.isAvailable(getActivity()) ? R.string.gps_google_location_settings
: R.string.gps_location_access);
message = getString(R.string.gps_disabled, setting);
isGpsDisabled = true;
break;
case NO_FIX:
message = getString(R.string.gps_wait_for_signal);
isGpsDisabled = false;
break;
case BAD_FIX:
message = getString(R.string.gps_wait_for_better_signal);
isGpsDisabled = false;
break;
case GOOD_FIX:
message = null;
isGpsDisabled = false;
break;
default:
throw new IllegalArgumentException("Unexpected state: " + state);
}
}
});
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (isResumed()) {
if (message == null) {
messageTextView.setVisibility(View.GONE);
return;
}
messageTextView.setText(message);
messageTextView.setVisibility(View.VISIBLE);
if (isGpsDisabled) {
Toast.makeText(getActivity(), R.string.gps_not_found, Toast.LENGTH_LONG).show();
// Click to show the location source settings
messageTextView.setOnClickListener(MapFragment.this);
} else {
messageTextView.setOnClickListener(null);
}
}
}
});
}
}
@Override
public void onLocationChanged(Location location) {
currentLocation = location;
updateCurrentLocation();
if (isResumed()) {
currentLocation = location;
updateCurrentLocation();
}
}
@Override
public void onHeadingChanged(double heading) {
if (mapOverlay.setHeading((float) heading)) {
mapView.postInvalidate();
if (isResumed()) {
if (mapOverlay.setHeading((float) heading)) {
mapView.postInvalidate();
}
}
}
@Override
public void onSelectedTrackChanged(final Track track) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
boolean hasTrack = track != null;
mapOverlay.setTrackDrawingEnabled(hasTrack);
if (isResumed()) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (isResumed()) {
boolean hasTrack = track != null;
mapOverlay.setTrackDrawingEnabled(hasTrack);
if (hasTrack) {
synchronized (this) {
/*
* Synchronize to prevent race condition in changing markerTrackId
* and markerId variables.
*/
currentSelectedTrackId = track.getId();
updateMap(track);
if (hasTrack) {
synchronized (this) {
/*
* Synchronize to prevent race condition in changing
* markerTrackId and markerId variables.
*/
currentSelectedTrackId = track.getId();
updateMap(track);
}
mapOverlay.setShowEndMarker(!isSelectedTrackRecording());
}
mapView.invalidate();
}
mapOverlay.setShowEndMarker(!isSelectedTrackRecording());
}
mapView.invalidate();
}
});
});
}
}
@Override
@@ -371,12 +382,16 @@ public class MapFragment extends Fragment
@Override
public void clearTrackPoints() {
mapOverlay.clearPoints();
if (isResumed()) {
mapOverlay.clearPoints();
}
}
@Override
public void onSampledInTrackPoint(Location location) {
mapOverlay.addLocation(location);
if (isResumed()) {
mapOverlay.addLocation(location);
}
}
@Override
@@ -386,22 +401,28 @@ public class MapFragment extends Fragment
@Override
public void onSegmentSplit(Location location) {
mapOverlay.addSegmentSplit();
if (isResumed()) {
mapOverlay.addSegmentSplit();
}
}
@Override
public void onNewTrackPointsDone() {
mapView.postInvalidate();
if (isResumed()) {
mapView.postInvalidate();
}
}
@Override
public void clearWaypoints() {
mapOverlay.clearWaypoints();
if (isResumed()) {
mapOverlay.clearWaypoints();
}
}
@Override
public void onNewWaypoint(Waypoint waypoint) {
if (waypoint != null && LocationUtils.isValidLocation(waypoint.getLocation())) {
if (isResumed() && waypoint != null && LocationUtils.isValidLocation(waypoint.getLocation())) {
// TODO: Optimize locking inside addWaypoint
mapOverlay.addWaypoint(waypoint);
}
@@ -409,7 +430,9 @@ public class MapFragment extends Fragment
@Override
public void onNewWaypointsDone() {
mapView.postInvalidate();
if (isResumed()) {
mapView.postInvalidate();
}
}
@Override
@@ -29,6 +29,7 @@ import com.google.android.maps.mytracks.R;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@@ -57,7 +58,8 @@ public class StatsFragment extends Fragment implements TrackDataListener {
// A runnable to update the total time field.
private final Runnable updateTotalTime = new Runnable() {
public void run() {
if (isSelectedTrackRecording() && !isSelectedTrackPaused() && lastTripStatistics != null) {
if (isResumed() && isSelectedTrackRecording() && !isSelectedTrackPaused()
&& lastTripStatistics != null) {
StatsUtils.setTotalTimeValue(getActivity(), System.currentTimeMillis()
- lastTripStatistics.getStopTime() + lastTripStatistics.getTotalTime());
}
@@ -71,7 +73,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
@Override
public void run() {
Log.d(TAG, "UI update thread started");
while (true) {
while (isResumed() && isSelectedTrackRecording()) {
getActivity().runOnUiThread(updateTotalTime);
try {
Thread.sleep(1000L);
@@ -93,7 +95,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
updateUi();
updateUi(getActivity());
}
@Override
@@ -114,12 +116,14 @@ public class StatsFragment extends Fragment implements TrackDataListener {
@Override
public void onLocationStateChanged(LocationState state) {
if (isResumed() && (state == LocationState.DISABLED || state == LocationState.NO_FIX)) {
if (isResumed() && state != LocationState.GOOD_FIX) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
lastLocation = null;
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
if (isResumed()) {
lastLocation = null;
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
}
}
});
}
@@ -127,12 +131,21 @@ public class StatsFragment extends Fragment implements TrackDataListener {
@Override
public void onLocationChanged(final Location location) {
if (isResumed() && isSelectedTrackRecording()) {
if (isResumed()) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
lastLocation = location;
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
if (isResumed()) {
if (isSelectedTrackRecording() && !isSelectedTrackPaused()) {
lastLocation = location;
StatsUtils.setLocationValues(getActivity(), location, true);
} else {
if (lastLocation != null) {
lastLocation = null;
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
}
}
}
}
});
}
@@ -146,14 +159,14 @@ public class StatsFragment extends Fragment implements TrackDataListener {
@Override
public void onSelectedTrackChanged(Track track) {
if (isResumed()) {
boolean isRecording = isSelectedTrackRecording();
if (uiUpdateThread == null && isRecording) {
uiUpdateThread = new UiUpdateThread();
uiUpdateThread.start();
} else if (uiUpdateThread != null && !isRecording) {
if (uiUpdateThread != null) {
uiUpdateThread.interrupt();
uiUpdateThread = null;
}
if (isSelectedTrackRecording()) {
uiUpdateThread = new UiUpdateThread();
uiUpdateThread.start();
}
}
}
@@ -163,18 +176,10 @@ public class StatsFragment extends Fragment implements TrackDataListener {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (track == null || track.getTripStatistics() == null) {
lastLocation = null;
lastTripStatistics = null;
updateUi();
return;
if (isResumed()) {
lastTripStatistics = track != null ? track.getTripStatistics() : null;
updateUi(getActivity());
}
lastTripStatistics = track.getTripStatistics();
if (!isSelectedTrackRecording()) {
lastLocation = null;
}
updateUi();
}
});
}
@@ -226,7 +231,9 @@ public class StatsFragment extends Fragment implements TrackDataListener {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
updateUi();
if (isResumed()) {
updateUi(getActivity());
}
}
});
}
@@ -239,7 +246,9 @@ public class StatsFragment extends Fragment implements TrackDataListener {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
updateUi();
if (isResumed()) {
updateUi(getActivity());
}
}
});
}
@@ -258,11 +267,8 @@ public class StatsFragment extends Fragment implements TrackDataListener {
*/
private synchronized void resumeTrackDataHub() {
trackDataHub = ((TrackDetailActivity) getActivity()).getTrackDataHub();
trackDataHub.registerTrackDataListener(this, EnumSet.of(
TrackDataType.SELECTED_TRACK,
TrackDataType.TRACKS_TABLE,
TrackDataType.LOCATION,
TrackDataType.PREFERENCE));
trackDataHub.registerTrackDataListener(this, EnumSet.of(TrackDataType.SELECTED_TRACK,
TrackDataType.TRACKS_TABLE, TrackDataType.LOCATION, TrackDataType.PREFERENCE));
}
/**
@@ -293,8 +299,8 @@ public class StatsFragment extends Fragment implements TrackDataListener {
/**
* Updates the UI.
*/
private void updateUi() {
StatsUtils.setTripStatisticsValues(getActivity(), lastTripStatistics);
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
private void updateUi(FragmentActivity activity) {
StatsUtils.setTripStatisticsValues(activity, lastTripStatistics);
StatsUtils.setLocationValues(activity, lastLocation, true);
}
}