source: other-projects/tipple-android/src/org/mapsforge/android/maps/ArrayItemizedMarkerOverlay.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.2 KB
Line 
1package org.mapsforge.android.maps;
2
3import java.io.File;
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.List;
7
8import org.tipple.mapDisplay.R;
9import org.tipple.mapDisplay.TakePicture;
10import org.tipple.mapDisplay.tippleActivity;
11
12import android.app.AlertDialog;
13import android.content.Context;
14import android.content.Intent;
15import android.graphics.Bitmap;
16import android.graphics.BitmapFactory;
17import android.graphics.Matrix;
18import android.graphics.Point;
19import android.graphics.Rect;
20import android.graphics.drawable.BitmapDrawable;
21import android.graphics.drawable.Drawable;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.View.MeasureSpec;
25import android.widget.Button;
26import android.widget.ImageView;
27import android.widget.LinearLayout.LayoutParams;
28import android.widget.TextView;
29
30/**
31 * Thread-safe implementation of the {@link ItemizedMarkerOverlay} class using
32 * an {@link ArrayList} as internal data structure. casey: implement the ontap
33 * method for the customized overlay item
34 */
35public class ArrayItemizedMarkerOverlay extends
36 ItemizedMarkerOverlay<OverlayMarkerItem> {
37 private static final int ARRAY_LIST_INITIAL_CAPACITY = 8;
38 private static final String THREAD_NAME = "ArrayItemizedMarkerOverlay";
39
40 protected final Context context;
41 protected AlertDialog.Builder builder;// Builder builder
42 protected View dialogLayout;
43 protected AlertDialog alertDialog;
44 Bitmap bmScreen;
45
46 protected OverlayItem item;
47 protected final ArrayList<OverlayMarkerItem> overlayMarkerItems;
48 protected OverlayMarkerItem overlayItem;
49 protected Drawable marker;
50 protected Drawable defaultMarker;
51
52 // casey
53 private List<Integer> visibleItems;
54 private int numberOfSize;
55 // private OverlayMarkerItem checkOverlayItem;
56 // casey: popup window
57 private MarkerPopup popup;
58 private String imageName;
59
60 /**
61 * Constructs a new ArrayItemizedOverlay.
62 *
63 * @param defaultMarker
64 * the default marker.
65 * @param context
66 * the reference to the application context.
67 */
68 public ArrayItemizedMarkerOverlay(Drawable defaultMarker, Context context) {
69
70 super(boundCenterBottom(defaultMarker));
71 this.context = context;
72 this.defaultMarker = defaultMarker;
73 this.overlayMarkerItems = new ArrayList<OverlayMarkerItem>(
74 ARRAY_LIST_INITIAL_CAPACITY);
75 this.visibleItems = new ArrayList<Integer>(numberOfSize);
76
77 }
78
79 public void setMarker(Drawable marker) {
80 this.marker = boundCenterBottom(marker);
81 }
82
83 public Drawable getMarker() {
84 return this.marker;
85 }
86
87 /**
88 * Adds the given item to the Overlay.
89 *
90 * @param overlayItem
91 * the item that should be added to the Overlay.
92 */
93 public synchronized void addOverlay(OverlayMarkerItem overlayItem) {
94 this.overlayMarkerItems.add(overlayItem);
95 populate();
96 }
97
98 /**
99 * Adds all items of the given collection to the Overlay.
100 *
101 * @param c
102 * collection whose items should be added to the Overlay.
103 */
104 public synchronized void addOverlays(
105 Collection<? extends OverlayMarkerItem> c) {
106 this.overlayMarkerItems.addAll(c);
107 populate();
108 }
109
110 /**
111 * Removes all items from the Overlay.
112 */
113 public synchronized void clear() {
114 this.overlayMarkerItems.clear();
115 populate();
116 }
117
118 @Override
119 public String getThreadName() {
120 return THREAD_NAME;
121 }
122
123 /**
124 * Removes the given item from the Overlay.
125 *
126 * @param overlayMarkerItem
127 * the item that should be removed from the Overlay.
128 */
129 public synchronized void removeOverlay(OverlayMarkerItem overlayMarkerItem) {
130 this.overlayMarkerItems.remove(overlayMarkerItem);
131 populate();
132 }
133
134 @Override
135 public synchronized int size() {
136 numberOfSize = this.overlayMarkerItems.size();
137 return this.overlayMarkerItems.size();
138 }
139
140 @Override
141 public synchronized OverlayMarkerItem createItem(int i) {
142 return this.overlayMarkerItems.get(i);
143 }
144
145 protected boolean onTap(int index, MapView mapView) {
146
147 this.overlayItem = createItem(index);
148 if (this.overlayItem != null) {
149 if (this.overlayItem.getDescription() != null) {
150 hideOtherDialog(this.overlayMarkerItems, this.overlayItem,
151 mapView);
152 LayoutInflater inflater = (LayoutInflater) this.context
153 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
154 View view = inflater.inflate(R.layout.custom_dialog, null);
155 dialogLayout = (View) view.findViewById(R.id.ScrollView01);// ScrollView01
156 TextView text = (TextView) dialogLayout.findViewById(R.id.text);
157 text.setText(this.overlayItem.getDescription() + "\n" + "\n"
158 + this.overlayItem.getContent());
159 // String fileName = "/sdcard/";
160 String fileName = tippleActivity.imageDirectory;
161 fileName += this.overlayItem.getDescription() + ".jpg";
162 File imgFile = new File(fileName);
163
164 // Take image for chapter without image initially
165 // save the image then next time can retrieve it
166 if (!imgFile.exists()) {
167 Button camera = (Button) dialogLayout
168 .findViewById(R.id.button);
169 camera.setEnabled(true);
170 camera.setVisibility(0);
171 String tmpFileName = this.overlayItem.getDescription()
172 + ".jpg";
173 camera.setOnClickListener(new RandomClickListener(
174 tmpFileName));
175 }
176
177 if (imgFile.exists()) {
178
179 Bitmap bitmap = BitmapFactory.decodeFile(imgFile
180 .getAbsolutePath());
181 int width = bitmap.getWidth();
182 int height = bitmap.getHeight();
183 int newWidth = 200;
184 int newHeight = 150;
185 float scaleWidth = ((float) newWidth) / width;
186 float scaleHeight = ((float) newHeight) / height;
187 Matrix matrix = new Matrix();
188 matrix.postScale(scaleWidth, scaleHeight);
189
190 Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
191 width, height, matrix, true);
192 BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
193 ImageView image = (ImageView) dialogLayout
194 .findViewById(R.id.image);
195 image.setImageDrawable(bmd);
196
197 }
198
199 builder = new AlertDialog.Builder(this.context);
200 builder.setView(dialogLayout);
201
202 builder.setPositiveButton("OK", null);
203 alertDialog = builder.create();
204 alertDialog.show();
205 }
206 return true;
207 }
208 return false;
209
210 }
211
212 public boolean onTap(GeoPoint p, MapView mapView) {
213
214 Projection projection = mapView.getProjection();
215 Point eventPosition = projection.toPixels(p, null);
216
217 // check if the translation to pixel coordinates has failed
218 if (eventPosition == null) {
219 return false;
220 }
221 OverlayMarkerItem checkOverlayItem;
222 Point checkItemPoint = new Point();
223 Rect checkMarkerBounds;
224 int checkLeft;
225 int checkRight;
226 int checkTop;
227 int checkBottom;
228
229 synchronized (this.visibleItems) {
230 int numberOfSize = this.size();
231 Context contexto = mapView.getContext();
232 if (numberOfSize < 1) {
233 return false;
234 }
235
236 for (int i = 0; i < this.size(); ++i) {
237 checkOverlayItem = createItem(i);
238
239 if (checkOverlayItem == null) {
240 continue;
241 }
242 synchronized (checkOverlayItem) {
243 if (checkOverlayItem.getPoint() == null) {
244 continue;
245 }
246
247 checkItemPoint = projection.toPixels(
248 checkOverlayItem.getPoint(), checkItemPoint);
249 // check if the translation to pixel coordinates has failed
250 if (checkItemPoint == null) {
251 continue;
252 }
253 checkMarkerBounds = this.defaultMarker.getBounds();
254
255 checkLeft = checkItemPoint.x + checkMarkerBounds.left;
256 checkRight = checkItemPoint.x + checkMarkerBounds.right;
257 checkTop = checkItemPoint.y + checkMarkerBounds.top;
258 checkBottom = checkItemPoint.y + checkMarkerBounds.bottom;
259
260 if (checkRight >= eventPosition.x
261 && checkLeft <= eventPosition.x
262 && checkBottom >= eventPosition.y
263 && checkTop <= eventPosition.y) {
264
265 return onTap(i, mapView);
266 }
267 }// end of synchronized (checkOverlayItem)
268 }
269 }
270
271 // no hit
272 return false;
273 }
274
275 private boolean onBalloonTap(MapView mapView,
276 OverlayMarkerItem checkOverlayItem) {
277 hideOtherBalloons(this.overlayMarkerItems, checkOverlayItem);
278 this.popup = new MarkerPopup(mapView);
279 MapView.LayoutParams params = new MapView.LayoutParams(
280 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
281 popup.setData(checkOverlayItem);
282
283 // position
284 GeoPoint point = checkOverlayItem.getPoint();
285
286 // change parameter
287 popup.setLayoutParams(params);
288
289 // visibility
290 popup.setVisibility(View.VISIBLE);
291 mapView.addView(popup, params);
292
293 popup.setDrawingCacheEnabled(true);
294 popup.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
295 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
296 popup.layout(0, 0, popup.getMeasuredWidth(), popup.getMeasuredHeight());
297 popup.buildDrawingCache(true);
298 popup.setDrawingCacheEnabled(false); // clear drawing
299 // cache
300
301 mapView.getController().setCenter(point);
302
303 return true;
304
305 }
306
307 public void hideDialog(MapView mapView) {
308 if (dialogLayout != null) {
309 mapView.removeView(dialogLayout);
310 }
311
312 }
313
314 private void hideOtherDialog(ArrayList<OverlayMarkerItem> overlays,
315 OverlayMarkerItem item, MapView mapView) {
316
317 for (OverlayMarkerItem overlay : overlays) {
318 if ((overlay != null) && (overlay != item))
319 hideDialog(mapView);
320 }
321
322 }
323
324 public void hideBalloon() {
325 if (popup != null) {
326 popup.setVisibility(View.GONE);
327 }
328
329 }
330
331 private void hideOtherBalloons(ArrayList<OverlayMarkerItem> overlays,
332 OverlayMarkerItem item) {
333
334 for (OverlayMarkerItem overlay : overlays) {
335 if ((overlay != null) && (overlay != item))
336 hideBalloon();
337 }
338
339 }
340
341 public synchronized boolean unTap(GeoPoint p, MapView mapView) {
342
343 alertDialog.cancel();
344 return true;
345 }
346
347 // inner class for taking pictures
348 public class RandomClickListener implements View.OnClickListener {
349 private final String tmpImage;
350
351 public RandomClickListener(final String randomIndex) {
352 this.tmpImage = randomIndex;
353 }
354
355 @Override
356 public void onClick(View view) {
357 Intent myIntent = new Intent(view.getContext(), TakePicture.class);
358 myIntent.putExtra("filename", tmpImage);
359 context.startActivity(myIntent);
360
361 }
362 }
363
364}
Note: See TracBrowser for help on using the repository browser.