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