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
|
package cgeo.geocaching;
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.geopoint.DistanceParser;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointFormatter;
import cgeo.geocaching.geopoint.GeopointParser;
import org.apache.commons.lang3.StringUtils;
import android.app.ProgressDialog;
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 java.util.ArrayList;
import java.util.List;
public class cgeowaypointadd extends AbstractActivity {
private String geocode = null;
private int id = -1;
private cgGeo geo = null;
private UpdateLocationCallback geoUpdate = new update();
private ProgressDialog waitDialog = null;
private cgWaypoint waypoint = null;
private WaypointType type = WaypointType.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.getGeocode();
type = waypoint.getWaypointType();
prefix = waypoint.getPrefix();
lookup = waypoint.getLookup();
app.setAction(geocode);
if (waypoint.getCoords() != null) {
((Button) findViewById(R.id.buttonLatitude)).setText(waypoint.getCoords().format(GeopointFormatter.Format.LAT_DECMINUTE));
((Button) findViewById(R.id.buttonLongitude)).setText(waypoint.getCoords().format(GeopointFormatter.Format.LON_DECMINUTE));
}
((EditText) findViewById(R.id.name)).setText(Html.fromHtml(StringUtils.trimToEmpty(waypoint.getName())).toString());
((EditText) findViewById(R.id.note)).setText(Html.fromHtml(StringUtils.trimToEmpty(waypoint.getNote())).toString());
if (waitDialog != null) {
waitDialog.dismiss();
waitDialog = null;
}
}
} catch (Exception e) {
if (waitDialog != null) {
waitDialog.dismiss();
waitDialog = null;
}
Log.e(Settings.tag, "cgeowaypointadd.loadWaypointHandler: " + e.toString());
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme();
setContentView(R.layout.waypoint_new);
setTitle("waypoint");
if (geo == null) {
geo = app.startGeo(geoUpdate);
}
// get parameters
Bundle extras = getIntent().getExtras();
if (extras != null) {
geocode = extras.getString("geocode");
wpCount = extras.getInt("count", 0);
id = extras.getInt("waypoint");
}
if (StringUtils.isBlank(geocode) && id <= 0) {
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 buttonLat = (Button) findViewById(R.id.buttonLatitude);
buttonLat.setOnClickListener(new coordDialogListener());
Button buttonLon = (Button) findViewById(R.id.buttonLongitude);
buttonLon.setOnClickListener(new coordDialogListener());
Button addWaypoint = (Button) findViewById(R.id.add_waypoint);
addWaypoint.setOnClickListener(new coordsListener());
List<String> wayPointNames = new ArrayList<String>(WaypointType.ALL_TYPES_EXCEPT_OWN.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();
}
disableSuggestions((EditText) findViewById(R.id.distance));
}
@Override
public void onResume() {
super.onResume();
if (geo == null) {
geo = app.startGeo(geoUpdate);
}
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 implements UpdateLocationCallback {
@Override
public void updateLocation(cgGeo geo) {
Log.d(Settings.tag, "cgeowaypointadd.updateLocation called");
if (geo == null || geo.coordsNow == null) {
return;
}
try {
Button bLat = (Button) findViewById(R.id.buttonLatitude);
Button bLon = (Button) findViewById(R.id.buttonLongitude);
bLat.setHint(geo.coordsNow.format(GeopointFormatter.Format.LAT_DECMINUTE_RAW));
bLon.setHint(geo.coordsNow.format(GeopointFormatter.Format.LON_DECMINUTE_RAW));
} catch (Exception e) {
Log.w(Settings.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(Settings.tag, "cgeowaypoint.loadWaypoint.run: " + e.toString());
}
}
}
private class coordDialogListener implements View.OnClickListener {
public void onClick(View arg0) {
Geopoint gp = null;
if (waypoint != null && waypoint.getCoords() != null) {
gp = waypoint.getCoords();
}
cgeocoords coordsDialog = new cgeocoords(cgeowaypointadd.this, gp, geo);
coordsDialog.setCancelable(true);
coordsDialog.setOnCoordinateUpdate(new cgeocoords.CoordinateUpdate() {
@Override
public void update(final Geopoint gp) {
((Button) findViewById(R.id.buttonLatitude)).setText(gp.format(GeopointFormatter.Format.LAT_DECMINUTE));
((Button) findViewById(R.id.buttonLongitude)).setText(gp.format(GeopointFormatter.Format.LON_DECMINUTE));
if (waypoint != null) {
waypoint.setCoords(gp);
}
}
});
coordsDialog.show();
}
}
private class coordsListener implements View.OnClickListener {
public void onClick(View arg0) {
final String bearingText = ((EditText) findViewById(R.id.bearing)).getText().toString();
final String distanceText = ((EditText) findViewById(R.id.distance)).getText().toString();
final String latText = ((Button) findViewById(R.id.buttonLatitude)).getText().toString();
final String lonText = ((Button) findViewById(R.id.buttonLongitude)).getText().toString();
if (StringUtils.isBlank(bearingText) && StringUtils.isBlank(distanceText)
&& StringUtils.isBlank(latText) && StringUtils.isBlank(lonText)) {
helpDialog(res.getString(R.string.err_point_no_position_given_title), res.getString(R.string.err_point_no_position_given));
return;
}
double latitude;
double longitude;
if (StringUtils.isNotBlank(latText) && StringUtils.isNotBlank(lonText)) {
try {
latitude = GeopointParser.parseLatitude(latText);
longitude = GeopointParser.parseLongitude(lonText);
} catch (GeopointParser.ParseException e) {
showToast(res.getString(e.resource));
return;
}
} else {
if (geo == null || geo.coordsNow == null) {
showToast(res.getString(R.string.err_point_curr_position_unavailable));
return;
}
latitude = geo.coordsNow.getLatitude();
longitude = geo.coordsNow.getLongitude();
}
Geopoint coords = null;
if (StringUtils.isNotBlank(bearingText) && StringUtils.isNotBlank(distanceText)) {
// bearing & distance
double bearing = 0;
try {
bearing = Double.parseDouble(bearingText);
} catch (NumberFormatException e) {
helpDialog(res.getString(R.string.err_point_bear_and_dist_title), res.getString(R.string.err_point_bear_and_dist));
return;
}
double distance;
try {
distance = DistanceParser.parseDistance(distanceText, Settings.isUseMetricUnits());
} catch (NumberFormatException e) {
showToast(res.getString(R.string.err_parse_dist));
return;
}
coords = new Geopoint(latitude, longitude).project(bearing, distance);
} else {
coords = new Geopoint(latitude, longitude);
}
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(name, type);
waypoint.setGeocode(geocode);
waypoint.setPrefix(prefix);
waypoint.setLookup(lookup);
waypoint.setCoords(coords);
waypoint.setNote(note);
waypoint.setId(id);
if (app.saveOwnWaypoint(id, geocode, waypoint)) {
StaticMapsProvider.removeWpStaticMaps(id, geocode);
if (Settings.isStoreOfflineWpMaps()) {
StaticMapsProvider.storeWaypointStaticMap(app.getCacheByGeocode(geocode), cgeowaypointadd.this, waypoint);
}
finish();
return;
} else {
showToast(res.getString(R.string.err_waypoint_add_failed));
}
}
}
@Override
public void goManual(View view) {
if (id >= 0) {
ActivityMixin.goManual(this, "c:geo-waypoint-edit");
} else {
ActivityMixin.goManual(this, "c:geo-waypoint-new");
}
}
}
|