source: other-projects/tipple-android/trunk-restructured/workspace/tipple-lib/src/org/greenstone/android/tipple/base/TippleLocationListener.java@ 24807

Last change on this file since 24807 was 24807, checked in by davidb, 12 years ago

Reloating base files into 'base' package

  • Property svn:executable set to *
File size: 9.4 KB
Line 
1package org.greenstone.android.tipple.base;
2
3import java.io.File;
4import java.text.DecimalFormat;
5import java.util.ArrayList;
6import java.util.HashMap;
7import java.util.HashSet;
8import java.util.Set;
9
10import org.greenstone.android.tipple.R;
11import org.greenstone.android.tipple.base.TipLocation;
12
13import org.mapsforge.android.maps.ArrayItemizedMarkerOverlay;
14import org.mapsforge.android.maps.CircleOverlay;
15import org.mapsforge.android.maps.GeoPoint;
16import org.mapsforge.android.maps.MapController;
17import org.mapsforge.android.maps.MapView;
18import org.mapsforge.android.maps.OverlayMarkerItem;
19import org.mapsforge.android.maps.OverlayTextAudioItem;
20import org.mapsforge.android.maps.Projection;
21
22import android.app.Activity;
23import android.content.Context;
24import android.graphics.Color;
25import android.graphics.Paint;
26import android.graphics.Point;
27import android.graphics.drawable.Drawable;
28import android.location.Location;
29import android.location.LocationListener;
30import android.media.MediaPlayer;
31import android.os.Bundle;
32import android.os.Vibrator;
33
34import android.widget.TextView;
35
36public class TippleLocationListener implements LocationListener
37{
38 public static Location lastKnownLocation = null;
39
40 protected ArrayList<TipLocation> locations_;
41
42 protected Activity activity_;
43 protected MapView map_view_;
44 protected TextView text_view_;
45 protected TipLocationManager tip_location_manager_;
46
47 protected MapController map_controller_;
48 protected CircleOverlay here_overlay_;
49 protected ArrayItemizedMarkerOverlay highlight_marker_overlay_;
50
51 protected Vibrator vibrator_;
52
53 protected Set<TipLocation> curr_locations_;
54 protected Set<TipLocation> prev_locations_;
55 HashMap<Integer,OverlayMarkerItem> highlight_marker_hash_;
56
57
58 public TippleLocationListener(Activity activity, MapView map_view,TextView text_view,
59 TipLocationManager place_location_manager,
60 ArrayList<TipLocation> locations)
61 {
62 activity_ = activity;
63 map_view_ = map_view;
64 text_view_ = text_view;
65
66 tip_location_manager_ = place_location_manager;
67 locations_ = locations;
68
69 // location aware
70 Paint circleFillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
71 circleFillPaint.setStyle(Paint.Style.FILL);
72 circleFillPaint.setColor(Color.GREEN);
73 circleFillPaint.setAlpha(64);
74
75 Paint circleOutlinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
76 circleOutlinePaint.setStyle(Paint.Style.STROKE);
77 circleOutlinePaint.setColor(Color.GREEN);
78 circleOutlinePaint.setAlpha(128);
79 circleOutlinePaint.setStrokeWidth(3);
80 here_overlay_ = new CircleOverlay(circleFillPaint, circleOutlinePaint);
81 map_view_.getOverlays().add(here_overlay_);
82
83 Drawable highlightMarker = activity_.getResources().getDrawable(R.drawable.markerhighlight);
84 highlight_marker_overlay_ = new ArrayItemizedMarkerOverlay(highlightMarker,activity);
85 map_view_.getOverlays().add(highlight_marker_overlay_);
86
87 vibrator_ = (Vibrator) activity
88 .getSystemService(Context.VIBRATOR_SERVICE);
89
90 curr_locations_ = new HashSet<TipLocation>();
91 prev_locations_ = new HashSet<TipLocation>();
92 highlight_marker_hash_ = new HashMap<Integer,OverlayMarkerItem>();
93
94 }
95
96 boolean checking_location_ = false;
97
98 synchronized protected boolean getLock()
99 {
100 if (!checking_location_) {
101 checking_location_ = true;
102 }
103
104 return checking_location_;
105 }
106
107 synchronized protected void freeLock()
108 {
109 checking_location_ = false;
110 }
111
112 protected double pointDistance(Point place_point, Point here_point)
113 {
114 double delta_x = place_point.x - here_point.x;
115 double delta_y = place_point.y - here_point.y;
116 double pixel_distance = Math.sqrt((delta_x * delta_x) + (delta_y * delta_y));
117
118 return pixel_distance;
119 }
120
121 Point lastLoggedPoint_ = null;
122
123 protected void updateActiveLocations(Location loc)
124 {
125 // Find out if the current GPS location lies within the boundaries
126 // of any of the tour locations
127 Projection projection = map_view_.getProjection();
128 byte zoom_level = map_view_.getZoomLevel();
129 // byte zoom_level = 10; // work at a reasonable level of scale
130
131 float in_out_gap_meters = 3.0f;
132 float in_out_gap_pixels
133 = projection.metersToPixels(in_out_gap_meters, zoom_level);
134
135 double here_latitude = loc.getLatitude();
136 double here_longitude = loc.getLongitude();
137 GeoPoint here_geopoint = new GeoPoint(here_latitude, here_longitude);
138 Point here_point = projection.toPoint(here_geopoint, null,zoom_level);
139
140 if (lastLoggedPoint_==null) {
141 lastLoggedPoint_ = here_point;
142 TippleActivity.log.optMessage("GPS Location Reading");
143 }
144 else {
145 float far_enough_meters = 3.0f; // 3 meters
146 float far_enough_pixels = projection.metersToPixels(far_enough_meters, zoom_level);
147 if (pointDistance(here_point,lastLoggedPoint_)>far_enough_pixels) {
148 TippleActivity.log.optMessage("GPS Location Reading");
149 }
150 }
151
152 for (int i=0; i<locations_.size(); i++) {
153
154 TipLocation place = locations_.get(i);
155
156 double place_latitude = place.getLatitude();
157 double place_longitude = place.getLongitude();
158 double radius_meters = place.getRadiusKM() * 1000.0;
159
160 GeoPoint place_geopoint = new GeoPoint(place_latitude,place_longitude);
161 Point place_point = projection.toPoint(place_geopoint, null,zoom_level);
162
163 float radius_pixel_distance = projection.metersToPixels((float) radius_meters, zoom_level);
164
165 double delta_x = place_point.x - here_point.x;
166 double delta_y = place_point.y - here_point.y;
167 double pixel_distance = Math.sqrt((delta_x * delta_x) + (delta_y * delta_y));
168
169 double outside_pixel_distance = radius_pixel_distance + in_out_gap_pixels;
170 double inside_pixel_distance = radius_pixel_distance;
171
172 if (pixel_distance <= inside_pixel_distance) {
173
174 if (!curr_locations_.contains(place)) {
175 // User has entered new location on tour
176
177 curr_locations_.add(place);
178 vibrator_.vibrate(1000);
179 Bundle bundle = new Bundle();
180 bundle.putString("placeName", place.getTitle());
181
182 boolean visited_previously = prev_locations_.contains(place);
183 tip_location_manager_.textToSpeechToLocation(place,visited_previously);
184
185 OverlayTextAudioItem here_marker_item
186 = (OverlayTextAudioItem) TipLocationManager.itemizedMarkerOverlayArrayList.get(i);
187
188 GeoPoint dm_geopoint = here_marker_item.getPoint();
189 String title = here_marker_item.getTitle();
190
191 //itemized_overlay.onTap(i);
192
193 OverlayMarkerItem highlight_marker_item = new OverlayMarkerItem(dm_geopoint,title);
194 highlight_marker_hash_.put(i,highlight_marker_item);
195 highlight_marker_overlay_.addOverlay(highlight_marker_item);
196
197
198 try {
199 // Cue up audio alert
200 String audio_dir = TippleActivity.audioDirectory;
201 File alert_file = new File(audio_dir, "BeepMorseSonar.wav");
202 String alert_filename = alert_file.getAbsolutePath();
203
204 MediaPlayer mp = new MediaPlayer();
205 mp.setDataSource(alert_filename);
206 mp.prepare();
207 mp.start();
208 }
209 catch (Exception e) {
210 System.err.println("Failed to play audio alert for active GPS location");
211 e.printStackTrace();
212 }
213
214 // Now record the fact we've been here
215 prev_locations_.add(place);
216
217 }
218
219 }
220
221 else if (pixel_distance >= outside_pixel_distance) {
222
223 if (curr_locations_.contains(place)) {
224 curr_locations_.remove(place);
225
226 // TipLocationManager.itemizedMarkerOverlay.unTap();
227
228 OverlayMarkerItem highlight_marker_item = highlight_marker_hash_.remove(i);
229 highlight_marker_overlay_.removeOverlay(highlight_marker_item);
230 }
231 }
232 }
233 }
234 @Override
235 public void onLocationChanged(Location loc)
236 {
237 if (loc != null) {
238
239 //System.err.println("TipLocationListener::onLocationChaged -- start");
240 lastKnownLocation = loc;
241
242 if (getLock()) {
243 updateActiveLocations(loc);
244 freeLock();
245 }
246
247 DecimalFormat truncPlaces = new DecimalFormat("0.0000000000");
248 double longitude = loc.getLongitude();
249 double latitude = loc.getLatitude();
250
251 String trunc_long = truncPlaces.format(longitude);
252 String trunc_lat = truncPlaces.format(latitude);
253
254 String text_mess = "(Long,Lat): (" + trunc_long + "," + trunc_lat + ")";
255 text_view_.setText(text_mess);
256
257 // location aware
258 GeoPoint point = new GeoPoint(latitude, longitude);
259 here_overlay_.setCircleData(point, loc.getAccuracy());
260
261 // Use the following line if you want to map to auto center on the GPS location
262 boolean centre_on_gps_loc = TippleActivity.sharedPreferences.getBoolean("centreOnGPSLoc", false);
263 if (centre_on_gps_loc) {
264 MapController map_controller = map_view_.getController();
265 if (map_controller != null) {
266 map_controller.setCenter(point);
267 }
268 }
269
270 //System.err.println("TipLocationListener::onLocationChaged -- finished");
271 }
272 }
273
274 @Override
275 public void onProviderDisabled(String provider)
276 {
277 // TODO Auto-generated method stub
278 System.err.println("TipLocationListener::onProviderDisabled()");
279 }
280
281 @Override
282 public void onProviderEnabled(String provider)
283 {
284 // TODO Auto-generated method stub
285 System.err.println("TipLocationListener::onProviderEnabled()");
286
287 }
288
289 @Override
290 public void onStatusChanged(String provider, int status, Bundle extras)
291 {
292 // TODO Auto-generated method stub
293 System.err.println("TipLocationListener::onStatusChanged()");
294
295 }
296
297}
Note: See TracBrowser for help on using the repository browser.