~opensatnav-admins/opensatnav/release-1.0

« back to all changes in this revision

Viewing changes to src/org/opensatnav/android/services/TripStatistics.java

  • Committer: Kieran Fleming
  • Date: 2010-12-13 13:13:48 UTC
  • Revision ID: kieran.fleming@gmail.com-20101213131348-pixo12i0wjf11jk3
Add all the missing stuff from the failed package rename

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.opensatnav.android.services;
 
2
 
 
3
import java.text.DecimalFormat;
 
4
import java.util.ArrayList;
 
5
import java.util.Collection;
 
6
 
 
7
import org.andnav.osm.views.util.Util;
 
8
import org.opensatnav.android.TripStatisticsController;
 
9
import org.opensatnav.android.util.FormatHelper;
 
10
 
 
11
import android.content.Context;
 
12
import android.location.Location;
 
13
import android.util.Log;
 
14
 
 
15
public class TripStatistics {
 
16
 
 
17
        private int pointsReceivedCount;
 
18
        private long tripStartTimeMilisec;
 
19
        private long lastPointTimeMilisec;
 
20
        private float tripDistanceMeters;
 
21
        private Context ctx;
 
22
        
 
23
        // Previous Point
 
24
        private Location lastLocatPoint = null;
 
25
        
 
26
        private Collection<TripStatisticsListener> listeners = null;
 
27
        
 
28
        private DecimalFormat formatter;
 
29
 
 
30
        public TripStatistics(Context ctx) {
 
31
                initializeStats();
 
32
                this.ctx = ctx;
 
33
        }
 
34
        
 
35
        public TripStatistics(TripStatisticsListener firstListener,Context ctx) {
 
36
                this(ctx);
 
37
                
 
38
                
 
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);
 
42
        }
 
43
        
 
44
        private void initializeStats() {
 
45
                pointsReceivedCount = 0;
 
46
                tripStartTimeMilisec = -1;
 
47
                lastPointTimeMilisec = -1;
 
48
                tripDistanceMeters = 0;
 
49
                formatter = new DecimalFormat();
 
50
        }
 
51
        
 
52
        public void addTripStatsListener(TripStatisticsListener listener) {
 
53
                if( listeners == null ) {
 
54
                        listeners = new ArrayList<TripStatisticsListener>();
 
55
                }
 
56
                listeners.add(listener);
 
57
                
 
58
                listener.tripStatisticsChanged(this);  
 
59
        }
 
60
        
 
61
        public void removeTripStatsListener(TripStatisticsListener listener) {
 
62
                if( listeners.contains(listener) ) {
 
63
                        listeners.remove(listener);
 
64
                }
 
65
        }
 
66
 
 
67
        public void removeAllTripStatsListeners() {
 
68
                listeners = null;
 
69
        }
 
70
 
 
71
        public void addNewLocationPoint(Location newLocation, Location oldLocation) {
 
72
                pointsReceivedCount++;
 
73
                if( tripStartTimeMilisec == -1 ) {
 
74
                        tripStartTimeMilisec = newLocation.getTime();
 
75
                }
 
76
                lastPointTimeMilisec = newLocation.getTime();
 
77
 
 
78
                if(lastLocatPoint != null && Util.isValidSpeed(lastPointTimeMilisec, newLocation.getSpeed(), oldLocation.getTime(), oldLocation.getSpeed())) {
 
79
                        tripDistanceMeters += newLocation.distanceTo(lastLocatPoint);
 
80
                }
 
81
                
 
82
                lastLocatPoint = newLocation;
 
83
                
 
84
                if( pointsReceivedCount > 1 ) {
 
85
                        callAllListeners(); // Can't deliver with just one point.
 
86
                }
 
87
        }
 
88
        
 
89
        private void callAllListeners() {
 
90
                if( listeners != null ) {
 
91
                        for( TripStatisticsListener l : listeners) {
 
92
                                l.tripStatisticsChanged(this);
 
93
                        }
 
94
                } else {
 
95
                        Log.v("TripStatistics", "No listeners for TripStats");
 
96
                }
 
97
        }
 
98
 
 
99
        /** Returns the aver trip speed in m/s */
 
100
        public float getAverageTripSpeed() {
 
101
                if( pointsReceivedCount == 0 ) {
 
102
                        return 0f;
 
103
                } else {
 
104
                        return getTripDistance() / (getTripTime() / 1000);
 
105
                }
 
106
        }
 
107
 
 
108
        /** Return total trip time in milisec */
 
109
        public long getTripTime() {
 
110
                if( pointsReceivedCount == 0 ) {
 
111
                        return 0;
 
112
                } else {
 
113
                        return lastPointTimeMilisec - tripStartTimeMilisec;
 
114
                }
 
115
        }
 
116
 
 
117
        /** Returns total trip distance in meters */
 
118
        public float getTripDistance() {
 
119
                if( pointsReceivedCount == 0 ) {
 
120
                        return 0;
 
121
                } else {
 
122
                        return tripDistanceMeters;
 
123
                }
 
124
        }
 
125
 
 
126
        /* Note: the instantenous speed is newPoint.getSpeed()  */
 
127
        public float getInstantSpeed() {
 
128
                if( pointsReceivedCount == 0 ) {
 
129
                        return 0;
 
130
                } else {
 
131
                        return lastLocatPoint.getSpeed();
 
132
                }
 
133
        }
 
134
 
 
135
        public void resetStatistics() {
 
136
                initializeStats();
 
137
                callAllListeners();
 
138
        }
 
139
 
 
140
        public String getAverageTripSpeedString() {
 
141
                return getSpeedString(getAverageTripSpeed());
 
142
        }
 
143
 
 
144
        public String getInstantSpeedString() {
 
145
                return getSpeedString(getInstantSpeed());
 
146
        }
 
147
 
 
148
        private String getSpeedString(float speed) {
 
149
                String speedString = "";
 
150
                speedString = 
 
151
                        new FormatHelper(ctx).formatSpeed(speed);
 
152
 
 
153
                return speedString;
 
154
        }
 
155
 
 
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);
 
161
                
 
162
                formatter.applyLocalizedPattern("00");
 
163
                int min = (tripTimeSec - hr * 3600) / 60;
 
164
                String minStr = formatter.format(min);
 
165
                
 
166
                int sec = tripTimeSec % 60;
 
167
                String secStr = formatter.format(sec);
 
168
                return hrStr + ":" + minStr + ":" + secStr;
 
169
        }
 
170
 
 
171
        public String getTripDistanceString() {
 
172
                String distance = "";
 
173
                distance = 
 
174
                        new FormatHelper(ctx).formatDistance((int)getTripDistance());
 
175
 
 
176
                return distance;
 
177
        }
 
178
 
 
179
        public static class TripStatisticsStrings {
 
180
                public String averSpeed;
 
181
                public String instSpeed;
 
182
                public String tripDistance;
 
183
                public String tripDuration;
 
184
        }
 
185
}