aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/BusStopOverlay.java
blob: b8377cf344801c46f099d8c9a71ee8cda2b2f8cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * Southampton University Map App
 * Copyright (C) 2011  Christopher Baines
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package net.cbaines.suma;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import org.osmdroid.views.MapView;
import org.osmdroid.views.MapView.Projection;
import org.osmdroid.views.overlay.Overlay;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

public class BusStopOverlay extends Overlay implements RouteColorConstants, Preferences {

	List<BusStop> busStops;

	private final Point mCurScreenCoords = new Point();
	private final Point mTouchScreenPoint = new Point();
	private final Point mItemPoint = new Point();

	private final Rect mRect = new Rect();

	private final Drawable marker;
	private final Drawable favMarker;

	private final Paint paint;

	private static final String TAG = "BusStopOverlay";

	private final MapActivity context;

	private float userScale = 1f;

	private boolean[] routes = new boolean[5];

	private boolean showIdentifiers;

	public BusStopOverlay(MapActivity context, List<BusStop> busStops) throws SQLException {
		super(context);

		this.context = context;

		marker = context.getResources().getDrawable(R.drawable.busstop);
		favMarker = context.getResources().getDrawable(R.drawable.busstop_fav);

		showIdentifiers = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SHOW_IDENTIFIERS,
				SHOW_IDENTIFIERS_ENABLED_BY_DEFAULT);

		paint = new Paint();
		paint.setStyle(Style.FILL);
		paint.setStrokeWidth(6);

		this.busStops = busStops;
	}

	void setRoutes(int route, boolean visible) {
		routes[route] = visible;
	}

	@Override
	public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) {

		if (shadow) {
			return;
		}

		final float scale = mScale * userScale;

		final Projection pj = mapView.getProjection();

		final int markerWidth = (int) (marker.getIntrinsicWidth() * userScale);
		final int markerHeight = (int) (marker.getIntrinsicHeight() * userScale);

		mRect.set(0, 0, 0 + markerWidth, 0 + markerHeight);
		mRect.offset(-markerWidth / 2, -markerHeight);
		marker.setBounds(mRect);
		favMarker.setBounds(mRect);

		final SharedPreferences favouritesPreferences = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0);

		/*
		 * Draw in backward cycle, so the items with the least index are on the front.
		 */

		for (int stopNum = 0; stopNum < busStops.size(); stopNum++) {
			BusStop stop = busStops.get(stopNum);

			pj.toMapPixels(stop.point, mCurScreenCoords);
			if (!pj.getBoundingBox().increaseByScale(1.2f).contains(stop.point)) {
				continue;
			}

			byte routeNum = 0;
			final byte stopRoutes = stop.routes;

			boolean drawing = false;

			if (stop.uniLink) {

				for (int i = 0; i < 5; i++) {
					if ((stopRoutes & (1 << i)) != 0) {
						routeNum++;
						if (routes[i]) {
							drawing = true;
						}
					}
				}

				if (!drawing)
					continue;

			}

			if (favouritesPreferences.getBoolean(stop.id, false)) {
				Overlay.drawAt(canvas, favMarker, mCurScreenCoords.x, mCurScreenCoords.y, false);
			} else {
				Overlay.drawAt(canvas, marker, mCurScreenCoords.x, mCurScreenCoords.y, false);
			}

			if (stop.uniLink) {

				int makersPlaced = 0;

				final float rectLeft = mCurScreenCoords.x + (8.8f * scale);
				final float rectRight = rectLeft + (int) (8 * scale);

				int yOfsetPerMarker = (int) (10 * scale);
				int markerYSize = (int) (8 * scale);

				if (routeNum == 5) {
					markerYSize = (int) (5 * scale);
					yOfsetPerMarker = (int) (7 * scale);
				} else if (routeNum == 4) {
					markerYSize = (int) (6.5f * scale);
					yOfsetPerMarker = (int) (8 * scale);
				}

				for (int i = 0; i < 5; i++) {
					if ((stopRoutes & (1 << i)) != 0) {

						if (i == 0) {
							paint.setColor(U1);
						} else if (i == 1) {
							paint.setColor(U1N);
						} else if (i == 2) {
							paint.setColor(U2);
						} else if (i == 3) {
							paint.setColor(U6);
						} else if (i == 4) {
							paint.setColor(U9);
						} else {
							Log.e(TAG, "Unknown route code");
						}

						canvas.drawRect(rectLeft, mCurScreenCoords.y + ((yOfsetPerMarker * makersPlaced) - (45 * scale)),
								rectRight, mCurScreenCoords.y + (yOfsetPerMarker * makersPlaced) - ((45 * scale) - markerYSize),
								paint);

						makersPlaced++;
					}
				}
			}
		}
	}

	@Override
	public boolean onSingleTapConfirmed(final MotionEvent event, final MapView mapView) {

		BusStop busStop = getSelectedItem(event, mapView);

		if (busStop == null) {
			// Log.v(TAG, "No busStop pressed");
			return false;
		} else {
			Log.i(TAG, "busStop Pressed " + busStop.id);

			String str = busStop.description;
			if (showIdentifiers) {
				str += " (" + busStop.id + ")";
			}
			context.makeToast(str, context.getResources().getString(R.string.map_activity_toast_help_message), Toast.LENGTH_SHORT);

			return true;
		}
	}

	public boolean onDoubleTap(final MotionEvent event, final MapView mapView) {

		BusStop busStop = getSelectedItem(event, mapView);

		if (busStop == null) {
			// Log.v(TAG, "No busStop pressed");
			return false;
		} else {
			Log.i(TAG, "Pressed " + busStop.id);

			Uri uri = Uri.parse("http://id.southampton.ac.uk/bus-stop/" + busStop.id);

			Log.i(TAG, "Starting a activity for " + uri + " path " + uri.getPath());

			Intent busStopIntent = new Intent(Intent.ACTION_VIEW, uri);
			context.startActivity(busStopIntent);

			return true;
		}

	}

	@Override
	public boolean onLongPress(final MotionEvent event, final MapView mapView) {

		BusStop busStop = getSelectedItem(event, mapView);

		if (busStop == null) {
			// Log.v(TAG, "No busStop pressed");
			return false;
		} else {
			Log.i(TAG, "Pressed " + busStop.id);

			final SharedPreferences favouritesPreferences = context.getSharedPreferences(FAVOURITES_PREFERENCES, 0);
			final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);

			if (favouritesPreferences.getBoolean(busStop.id, false)) {
				favouritesPreferences.edit().remove(busStop.id).commit();

				if (sharedPrefs.getBoolean(SHOW_IDENTIFIERS, SHOW_IDENTIFIERS_ENABLED_BY_DEFAULT)) {
					context.makeToast(busStop.description + " removed from favourites", "(" + busStop.id + ")",
							Toast.LENGTH_SHORT);
				} else {
					context.makeToast(busStop.description + " removed from favourites", Toast.LENGTH_SHORT);
				}
			} else {
				if (sharedPrefs.getBoolean(SHOW_IDENTIFIERS, SHOW_IDENTIFIERS_ENABLED_BY_DEFAULT)) {
					context.makeToast(busStop.description + " added to favourites", "(" + busStop.id + ")", Toast.LENGTH_SHORT);
				} else {
					context.makeToast(busStop.description + " added to favourites", Toast.LENGTH_SHORT);
				}
				favouritesPreferences.edit().putBoolean(busStop.id, true).commit();
			}

			Collections.sort(busStops, new POIFavouriteComparator(context.getSharedPreferences(FAVOURITES_PREFERENCES, 0)));

			mapView.invalidate();

			return true;
		}
	}

	private BusStop getSelectedItem(final MotionEvent event, final MapView mapView) {
		Log.i(TAG, "Getting selected item");

		final Projection pj = mapView.getProjection();
		final int eventX = (int) event.getX();
		final int eventY = (int) event.getY();

		/* These objects are created to avoid construct new ones every cycle. */
		pj.fromMapPixels(eventX, eventY, mTouchScreenPoint);

		for (int i = busStops.size() - 1; i >= 0; i--) {
			BusStop busStop = busStops.get(i);

			pj.toPixels(busStop.point, mItemPoint);

			if (marker.getBounds().contains(mTouchScreenPoint.x - mItemPoint.x, mTouchScreenPoint.y - mItemPoint.y)) {
				if (busStop.uniLink) {
					boolean drawing = false;
					for (int route = 0; route < 5; route++) {
						if ((busStop.routes & (1 << route)) != 0) {
							if (routes[route]) {
								drawing = true;
								break;
							}
						}
					}
					if (!drawing)
						continue;
				}

				return busStop;
			}
		}
		return null;
	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		Log.i(TAG, "Got a favourites change in the BuildingNumOverlay for key " + key);

		Collections.sort(busStops, new POIFavouriteComparator(context.getSharedPreferences(FAVOURITES_PREFERENCES, 0)));
	}

}