source: other-projects/tipple-android/src/org/tipple/mapDisplay/locationListener.java@ 26523

Last change on this file since 26523 was 26523, checked in by davidb, 11 years ago

A version of Tipple with the mods Casey made for her Masters work

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