1
package org.opensatnav.android.services;
3
import java.text.DecimalFormat;
4
import java.util.ArrayList;
5
import java.util.Collection;
7
import org.andnav.osm.views.util.Util;
8
import org.opensatnav.android.TripStatisticsController;
9
import org.opensatnav.android.util.FormatHelper;
11
import android.content.Context;
12
import android.location.Location;
13
import android.util.Log;
15
public class TripStatistics {
17
private int pointsReceivedCount;
18
private long tripStartTimeMilisec;
19
private long lastPointTimeMilisec;
20
private float tripDistanceMeters;
24
private Location lastLocatPoint = null;
26
private Collection<TripStatisticsListener> listeners = null;
28
private DecimalFormat formatter;
30
public TripStatistics(Context ctx) {
35
public TripStatistics(TripStatisticsListener firstListener,Context ctx) {
39
// Tried to do this from where the service is started, but
40
// onCreate() doesn't run until after some time later.
41
addTripStatsListener(firstListener);
44
private void initializeStats() {
45
pointsReceivedCount = 0;
46
tripStartTimeMilisec = -1;
47
lastPointTimeMilisec = -1;
48
tripDistanceMeters = 0;
49
formatter = new DecimalFormat();
52
public void addTripStatsListener(TripStatisticsListener listener) {
53
if( listeners == null ) {
54
listeners = new ArrayList<TripStatisticsListener>();
56
listeners.add(listener);
58
listener.tripStatisticsChanged(this);
61
public void removeTripStatsListener(TripStatisticsListener listener) {
62
if( listeners.contains(listener) ) {
63
listeners.remove(listener);
67
public void removeAllTripStatsListeners() {
71
public void addNewLocationPoint(Location newLocation, Location oldLocation) {
72
pointsReceivedCount++;
73
if( tripStartTimeMilisec == -1 ) {
74
tripStartTimeMilisec = newLocation.getTime();
76
lastPointTimeMilisec = newLocation.getTime();
78
if(lastLocatPoint != null && Util.isValidSpeed(lastPointTimeMilisec, newLocation.getSpeed(), oldLocation.getTime(), oldLocation.getSpeed())) {
79
tripDistanceMeters += newLocation.distanceTo(lastLocatPoint);
82
lastLocatPoint = newLocation;
84
if( pointsReceivedCount > 1 ) {
85
callAllListeners(); // Can't deliver with just one point.
89
private void callAllListeners() {
90
if( listeners != null ) {
91
for( TripStatisticsListener l : listeners) {
92
l.tripStatisticsChanged(this);
95
Log.v("TripStatistics", "No listeners for TripStats");
99
/** Returns the aver trip speed in m/s */
100
public float getAverageTripSpeed() {
101
if( pointsReceivedCount == 0 ) {
104
return getTripDistance() / (getTripTime() / 1000);
108
/** Return total trip time in milisec */
109
public long getTripTime() {
110
if( pointsReceivedCount == 0 ) {
113
return lastPointTimeMilisec - tripStartTimeMilisec;
117
/** Returns total trip distance in meters */
118
public float getTripDistance() {
119
if( pointsReceivedCount == 0 ) {
122
return tripDistanceMeters;
126
/* Note: the instantenous speed is newPoint.getSpeed() */
127
public float getInstantSpeed() {
128
if( pointsReceivedCount == 0 ) {
131
return lastLocatPoint.getSpeed();
135
public void resetStatistics() {
140
public String getAverageTripSpeedString() {
141
return getSpeedString(getAverageTripSpeed());
144
public String getInstantSpeedString() {
145
return getSpeedString(getInstantSpeed());
148
private String getSpeedString(float speed) {
149
String speedString = "";
151
new FormatHelper(ctx).formatSpeed(speed);
156
public String getTripTimeString() {
157
int tripTimeSec = Math.round(getTripTime() / 1000f);
158
formatter.applyLocalizedPattern("#0");
159
int hr = tripTimeSec / 3600;
160
String hrStr = formatter.format(hr);
162
formatter.applyLocalizedPattern("00");
163
int min = (tripTimeSec - hr * 3600) / 60;
164
String minStr = formatter.format(min);
166
int sec = tripTimeSec % 60;
167
String secStr = formatter.format(sec);
168
return hrStr + ":" + minStr + ":" + secStr;
171
public String getTripDistanceString() {
172
String distance = "";
174
new FormatHelper(ctx).formatDistance((int)getTripDistance());
179
public static class TripStatisticsStrings {
180
public String averSpeed;
181
public String instSpeed;
182
public String tripDistance;
183
public String tripDuration;