aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgeo/geocaching/cgeowaypointadd.java
blob: 6d13cb85a6a2884e642547790f61c858db4e1155 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package cgeo.geocaching;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.ProgressDialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.activity.ActivityMixin;

public class cgeowaypointadd extends AbstractActivity {

	private cgeoapplication app = null;
	private Resources res = null;
	private cgSettings settings = null;
	private cgBase base = null;
	private cgWarning warning = null;
	private String geocode = null;
	private int id = -1;
	private cgGeo geo = null;
	private cgUpdateLoc geoUpdate = new update();
	private EditText latEdit = null;
	private EditText lonEdit = null;
	private ProgressDialog waitDialog = null;
	private cgWaypoint waypoint = null;
	private String type = "own";
	private String prefix = "OWN";
	private String lookup = "---";
	/**
	 * number of waypoints that the corresponding cache has until now
	 */
	private int wpCount = 0;
	private Handler loadWaypointHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			try {
				if (waypoint == null) {
					if (waitDialog != null) {
						waitDialog.dismiss();
						waitDialog = null;
					}

					id = -1;
				} else {
					geocode = waypoint.geocode;
					type = waypoint.type;
					prefix = waypoint.prefix;
					lookup = waypoint.lookup;

					app.setAction(geocode);

					((EditText) findViewById(R.id.latitude)).setText(base.formatCoordinate(waypoint.latitude, "lat", true));
					((EditText) findViewById(R.id.longitude)).setText(base.formatCoordinate(waypoint.longitude, "lon", true));
					((EditText) findViewById(R.id.name)).setText(Html.fromHtml(waypoint.name.trim()).toString());
					((EditText) findViewById(R.id.note)).setText(Html.fromHtml(waypoint.note.trim()).toString());

					if (waitDialog != null) {
						waitDialog.dismiss();
						waitDialog = null;
					}
				}
			} catch (Exception e) {
				if (waitDialog != null) {
					waitDialog.dismiss();
					waitDialog = null;
				}
				Log.e(cgSettings.tag, "cgeowaypointadd.loadWaypointHandler: " + e.toString());
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// init
		res = this.getResources();
		app = (cgeoapplication) this.getApplication();
		settings = new cgSettings(this, getSharedPreferences(cgSettings.preferences, 0));
		base = new cgBase(app, settings, getSharedPreferences(cgSettings.preferences, 0));
		warning = new cgWarning(this);

		setTheme();
		setContentView(R.layout.waypoint_new);
		setTitle("waypoint");

		if (geo == null) {
			geo = app.startGeo(this, geoUpdate, base, settings, warning, 0, 0);
		}

		// get parameters
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			geocode = extras.getString("geocode");
			wpCount = extras.getInt("count", 0);
			id = extras.getInt("waypoint");
		}

		if ((geocode == null || geocode.length() == 0) && id <= 0) {
			warning.showToast(res.getString(R.string.err_waypoint_cache_unknown));

			finish();
			return;
		}

		if (id <= 0) {
			setTitle(res.getString(R.string.waypoint_add_title));
		} else {
			setTitle(res.getString(R.string.waypoint_edit_title));
		}

		if (geocode != null) {
			app.setAction(geocode);
		}

		Button buttonCurrent = (Button) findViewById(R.id.current);
		buttonCurrent.setOnClickListener(new currentListener());

		Button addWaypoint = (Button) findViewById(R.id.add_waypoint);
		addWaypoint.setOnClickListener(new coordsListener());

		ArrayList<String> wayPointNames = new ArrayList<String>(cgBase.waypointTypes.values());
		AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.name);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, wayPointNames);
		textView.setAdapter(adapter);


		if (id > 0) {
			waitDialog = ProgressDialog.show(this, null, res.getString(R.string.waypoint_loading), true);
			waitDialog.setCancelable(true);

			(new loadWaypoint()).start();
		}
	}

	@Override
	public void onResume() {
		super.onResume();

		settings.load();

		if (geo == null) {
			geo = app.startGeo(this, geoUpdate, base, settings, warning, 0, 0);
		}

		if (id > 0) {
			if (waitDialog == null) {
				waitDialog = ProgressDialog.show(this, null, res.getString(R.string.waypoint_loading), true);
				waitDialog.setCancelable(true);

				(new loadWaypoint()).start();
			}
		}
	}

	@Override
	public void onDestroy() {
		if (geo != null) {
			geo = app.removeGeo();
		}

		super.onDestroy();
	}

	@Override
	public void onStop() {
		if (geo != null) {
			geo = app.removeGeo();
		}

		super.onStop();
	}

	@Override
	public void onPause() {
		if (geo != null) {
			geo = app.removeGeo();
		}

		super.onPause();
	}

	private class update extends cgUpdateLoc {

		@Override
		public void updateLoc(cgGeo geo) {
			if (geo == null) {
				return;
			}

			try {
				if (latEdit == null) {
					latEdit = (EditText) findViewById(R.id.latitude);
				}
				if (lonEdit == null) {
					lonEdit = (EditText) findViewById(R.id.longitude);
				}

				latEdit.setHint(base.formatCoordinate(geo.latitudeNow, "lat", false));
				lonEdit.setHint(base.formatCoordinate(geo.longitudeNow, "lon", false));
			} catch (Exception e) {
				Log.w(cgSettings.tag, "Failed to update location.");
			}
		}
	}

	private class loadWaypoint extends Thread {

		@Override
		public void run() {
			try {
				waypoint = app.loadWaypoint(id);

				loadWaypointHandler.sendMessage(new Message());
			} catch (Exception e) {
				Log.e(cgSettings.tag, "cgeowaypoint.loadWaypoint.run: " + e.toString());
			}
		}
	}

	private class currentListener implements View.OnClickListener {

		public void onClick(View arg0) {
			if (geo == null || geo.latitudeNow == null || geo.longitudeNow == null) {
				warning.showToast(res.getString(R.string.err_point_unknown_position));
				return;
			}

			((EditText) findViewById(R.id.latitude)).setText(base.formatCoordinate(geo.latitudeNow, "lat", true));
			((EditText) findViewById(R.id.longitude)).setText(base.formatCoordinate(geo.longitudeNow, "lon", true));
		}
	}

	private class coordsListener implements View.OnClickListener {

		public void onClick(View arg0) {
			ArrayList<Double> coords = new ArrayList<Double>();
			Double latitude = null;
			Double longitude = null;

			final String bearingText = ((EditText) findViewById(R.id.bearing)).getText().toString();
			final String distanceText = ((EditText) findViewById(R.id.distance)).getText().toString();
			final String latText = ((EditText) findViewById(R.id.latitude)).getText().toString();
			final String lonText = ((EditText) findViewById(R.id.longitude)).getText().toString();

			if ((bearingText == null || bearingText.length() == 0) && (distanceText == null || distanceText.length() == 0)
							&& (latText == null || latText.length() == 0) && (lonText == null || lonText.length() == 0)) {
				warning.helpDialog(res.getString(R.string.err_point_no_position_given_title), res.getString(R.string.err_point_no_position_given));
				return;
			}

			if (latText != null && latText.length() > 0 && lonText != null && lonText.length() > 0) {
				// latitude & longitude
				HashMap<String, Object> latParsed = base.parseCoordinate(latText, "lat");
				HashMap<String, Object> lonParsed = base.parseCoordinate(lonText, "lat");

				if (latParsed == null || latParsed.get("coordinate") == null || latParsed.get("string") == null) {
					warning.showToast(res.getString(R.string.err_parse_lat));
					return;
				}

				if (lonParsed == null || lonParsed.get("coordinate") == null || lonParsed.get("string") == null) {
					warning.showToast(res.getString(R.string.err_parse_lon));
					return;
				}

				latitude = (Double) latParsed.get("coordinate");
				longitude = (Double) lonParsed.get("coordinate");
			} else {
				if (geo == null || geo.latitudeNow == null || geo.longitudeNow == null) {
					warning.showToast(res.getString(R.string.err_point_curr_position_unavailable));
					return;
				}

				latitude = geo.latitudeNow;
				longitude = geo.longitudeNow;
			}

			if (bearingText != null && bearingText.length() > 0 && distanceText != null && distanceText.length() > 0) {
				// bearing & distance
				Double bearing = null;
				try {
					bearing = new Double(bearingText);
				} catch (Exception e) {
					// probably not a number
				}
				if (bearing == null) {
					warning.helpDialog(res.getString(R.string.err_point_bear_and_dist_title), res.getString(R.string.err_point_bear_and_dist));
					return;
				}

				Double distance = null; // km

				final Pattern patternA = Pattern.compile("^([0-9\\.\\,]+)[ ]*m$", Pattern.CASE_INSENSITIVE); // m
				final Pattern patternB = Pattern.compile("^([0-9\\.\\,]+)[ ]*km$", Pattern.CASE_INSENSITIVE); // km
				final Pattern patternC = Pattern.compile("^([0-9\\.\\,]+)[ ]*ft$", Pattern.CASE_INSENSITIVE); // ft - 0.3048m
				final Pattern patternD = Pattern.compile("^([0-9\\.\\,]+)[ ]*yd$", Pattern.CASE_INSENSITIVE); // yd - 0.9144m
				final Pattern patternE = Pattern.compile("^([0-9\\.\\,]+)[ ]*mi$", Pattern.CASE_INSENSITIVE); // mi - 1609.344m

				Matcher matcherA = patternA.matcher(distanceText);
				Matcher matcherB = patternB.matcher(distanceText);
				Matcher matcherC = patternC.matcher(distanceText);
				Matcher matcherD = patternD.matcher(distanceText);
				Matcher matcherE = patternE.matcher(distanceText);

				if (matcherA.find() == true && matcherA.groupCount() > 0) {
					distance = (new Double(matcherA.group(1))) * 0.001;
				} else if (matcherB.find() == true && matcherB.groupCount() > 0) {
					distance = new Double(matcherB.group(1));
				} else if (matcherC.find() == true && matcherC.groupCount() > 0) {
					distance = (new Double(matcherC.group(1))) * 0.0003048;
				} else if (matcherD.find() == true && matcherD.groupCount() > 0) {
					distance = (new Double(matcherD.group(1))) * 0.0009144;
				} else if (matcherE.find() == true && matcherE.groupCount() > 0) {
					distance = (new Double(matcherE.group(1))) * 1.609344;
				} else {
					try {
						if (settings.units == cgSettings.unitsImperial) {
							distance = (new Double(distanceText)) * 1.609344; // considering it miles
						} else {
							distance = (new Double(distanceText)) * 0.001; // considering it meters
						}
					} catch (Exception e) {
						// probably not a number
					}
				}

				if (distance == null) {
					warning.showToast(res.getString(R.string.err_parse_dist));
					return;
				}

				Double latParsed = null;
				Double lonParsed = null;

				HashMap<String, Double> coordsDst = base.getRadialDistance(latitude, longitude, bearing, distance);

				latParsed = coordsDst.get("latitude");
				lonParsed = coordsDst.get("longitude");

				if (latParsed == null || lonParsed == null) {
					warning.showToast(res.getString(R.string.err_point_location_error));
					return;
				}

				coords.add(0, (Double) latParsed);
				coords.add(1, (Double) lonParsed);
			} else if (latitude != null && longitude != null) {
				coords.add(0, latitude);
				coords.add(1, longitude);
			} else {
				warning.showToast(res.getString(R.string.err_point_location_error));
				return;
			}

			String name = ((EditText) findViewById(R.id.name)).getText().toString().trim();
			// if no name is given, just give the waypoint its number as name
			if (name.length() == 0) {
				name = res.getString(R.string.waypoint) + " " + String.valueOf(wpCount + 1);
			}
			final String note = ((EditText) findViewById(R.id.note)).getText().toString().trim();

			final cgWaypoint waypoint = new cgWaypoint();
			waypoint.type = type;
			waypoint.geocode = geocode;
			waypoint.prefix = prefix;
			waypoint.lookup = lookup;
			waypoint.name = name;
			waypoint.latitude = coords.get(0);
			waypoint.longitude = coords.get(1);
			waypoint.latitudeString = base.formatCoordinate(coords.get(0), "lat", true);
			waypoint.longitudeString = base.formatCoordinate(coords.get(1), "lon", true);
			waypoint.note = note;

			if (app.saveOwnWaypoint(id, geocode, waypoint) == true) {
				app.removeCacheFromCache(geocode);

				finish();
				return;
			} else {
				warning.showToast(res.getString(R.string.err_waypoint_add_failed));
			}
		}
	}

	public void goManual(View view) {
		if (id >= 0) {
			ActivityMixin.goManual(this, "c:geo-waypoint-edit");
		} else {
			ActivityMixin.goManual(this, "c:geo-waypoint-new");
		}
	}
}