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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
|
package cgeo.geocaching;
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.connector.IConnector;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.geopoint.DistanceParser;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointFormatter;
import cgeo.geocaching.ui.dialog.CoordinatesInputDialog;
import cgeo.geocaching.utils.BaseUtils;
import cgeo.geocaching.utils.GeoDirHandler;
import cgeo.geocaching.utils.Log;
import org.apache.commons.lang3.StringUtils;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
public class EditWaypointActivity extends AbstractActivity {
private String geocode = null;
private int id = -1;
private ProgressDialog waitDialog = null;
private Waypoint waypoint = null;
private Geopoint gpTemp = null;
private WaypointType type = WaypointType.OWN;
private String prefix = "OWN";
private String lookup = "---";
private boolean own = true;
private boolean visited = false;
ArrayList<WaypointType> wpTypes = null;
String distanceUnit = "";
/**
* 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) {
id = -1;
} else {
geocode = waypoint.getGeocode();
type = waypoint.getWaypointType();
prefix = waypoint.getPrefix();
lookup = waypoint.getLookup();
own = waypoint.isUserDefined();
visited = waypoint.isVisited();
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());
if (BaseUtils.containsHtml(waypoint.getNote())) {
((EditText) findViewById(R.id.note)).setText(Html.fromHtml(StringUtils.trimToEmpty(waypoint.getNote())).toString());
}
else {
((EditText) findViewById(R.id.note)).setText(StringUtils.trimToEmpty(waypoint.getNote()));
}
Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_ONLY);
setCoordsModificationVisibility(ConnectorFactory.getConnector(geocode), cache);
}
if (own) {
initializeWaypointTypeSelector();
}
((CheckBox) findViewById(R.id.wpt_visited_checkbox)).setChecked(visited);
initializeDistanceUnitSelector();
} catch (Exception e) {
Log.e("EditWaypointActivity.loadWaypointHandler", e);
} finally {
if (waitDialog != null) {
waitDialog.dismiss();
waitDialog = null;
}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme();
setContentView(R.layout.edit_waypoint_activity);
setTitle("waypoint");
// get parameters
Bundle extras = getIntent().getExtras();
if (extras != null) {
geocode = extras.getString(Intents.EXTRA_GEOCODE);
wpCount = extras.getInt(Intents.EXTRA_COUNT, 0);
id = extras.getInt(Intents.EXTRA_WAYPOINT_ID);
}
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));
}
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>();
for (WaypointType wpt : WaypointType.ALL_TYPES_EXCEPT_OWN_AND_ORIGINAL) {
wayPointNames.add(wpt.getL10n());
}
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) {
Spinner waypointTypeSelector = (Spinner) findViewById(R.id.type);
waypointTypeSelector.setVisibility(View.GONE);
waitDialog = ProgressDialog.show(this, null, res.getString(R.string.waypoint_loading), true);
waitDialog.setCancelable(true);
(new LoadWaypointThread()).start();
} else {
initializeWaypointTypeSelector();
}
if (geocode != null) {
Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB);
IConnector con = ConnectorFactory.getConnector(geocode);
setCoordsModificationVisibility(con, cache);
}
CheckBox visitedCheckBox = ((CheckBox) findViewById(R.id.wpt_visited_checkbox));
visitedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
visited = isChecked;
}
});
initializeDistanceUnitSelector();
disableSuggestions((EditText) findViewById(R.id.distance));
}
private void setCoordsModificationVisibility(IConnector con, Geocache cache) {
if (cache != null && (cache.getType() == CacheType.MYSTERY || cache.getType() == CacheType.MULTI)) {
findViewById(R.id.modify_cache_coordinates_group).setVisibility(View.VISIBLE);
findViewById(R.id.modify_cache_coordinates_local_and_remote).setVisibility(con.supportsOwnCoordinates() ? View.VISIBLE : View.GONE);
} else {
findViewById(R.id.modify_cache_coordinates_group).setVisibility(View.GONE);
findViewById(R.id.modify_cache_coordinates_local_and_remote).setVisibility(View.GONE);
}
}
@Override
public void onResume() {
super.onResume();
geoDirHandler.startGeo();
if (id > 0) {
if (waitDialog == null) {
waitDialog = ProgressDialog.show(this, null, res.getString(R.string.waypoint_loading), true);
waitDialog.setCancelable(true);
(new LoadWaypointThread()).start();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onPause() {
geoDirHandler.stopGeo();
super.onPause();
}
private void initializeWaypointTypeSelector() {
Spinner waypointTypeSelector = (Spinner) findViewById(R.id.type);
wpTypes = new ArrayList<WaypointType>(WaypointType.ALL_TYPES_EXCEPT_OWN_AND_ORIGINAL);
ArrayAdapter<WaypointType> wpAdapter = new ArrayAdapter<WaypointType>(this, android.R.layout.simple_spinner_item, wpTypes.toArray(new WaypointType[wpTypes.size()]));
wpAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
waypointTypeSelector.setAdapter(wpAdapter);
int typeIndex = wpTypes.indexOf(type);
if (typeIndex < 0) {
typeIndex = wpTypes.indexOf(WaypointType.WAYPOINT);
}
waypointTypeSelector.setSelection(typeIndex);
waypointTypeSelector.setOnItemSelectedListener(new ChangeWaypointType(this));
waypointTypeSelector.setVisibility(View.VISIBLE);
}
private void initializeDistanceUnitSelector() {
Spinner distanceUnitSelector = (Spinner) findViewById(R.id.distanceUnit);
if (StringUtils.isBlank(distanceUnit)) {
if (Settings.isUseMetricUnits()) {
distanceUnitSelector.setSelection(0); // m
distanceUnit = res.getStringArray(R.array.distance_units)[0];
} else {
distanceUnitSelector.setSelection(2); // ft
distanceUnit = res.getStringArray(R.array.distance_units)[2];
}
}
distanceUnitSelector.setOnItemSelectedListener(new ChangeDistanceUnit(this));
}
final private GeoDirHandler geoDirHandler = new GeoDirHandler() {
@Override
public void updateGeoData(final IGeoData geo) {
if (geo.getCoords() == null) {
return;
}
try {
Button bLat = (Button) findViewById(R.id.buttonLatitude);
Button bLon = (Button) findViewById(R.id.buttonLongitude);
bLat.setHint(geo.getCoords().format(GeopointFormatter.Format.LAT_DECMINUTE_RAW));
bLon.setHint(geo.getCoords().format(GeopointFormatter.Format.LON_DECMINUTE_RAW));
} catch (final Exception e) {
Log.e("failed to update location", e);
}
}
};
private class LoadWaypointThread extends Thread {
@Override
public void run() {
try {
waypoint = cgData.loadWaypoint(id);
loadWaypointHandler.sendMessage(Message.obtain());
} catch (Exception e) {
Log.e("cgeowaypoint.loadWaypoint.run", e);
}
}
}
private class CoordDialogListener implements View.OnClickListener {
@Override
public void onClick(View arg0) {
Geopoint gp = null;
if (waypoint != null && waypoint.getCoords() != null) {
gp = waypoint.getCoords();
} else if (gpTemp != null) {
gp = gpTemp;
}
Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
CoordinatesInputDialog coordsDialog = new CoordinatesInputDialog(EditWaypointActivity.this, cache, gp, app.currentGeo());
coordsDialog.setCancelable(true);
coordsDialog.setOnCoordinateUpdate(new CoordinatesInputDialog.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);
} else {
gpTemp = gp;
}
}
});
coordsDialog.show();
}
}
private static class ChangeWaypointType implements OnItemSelectedListener {
private ChangeWaypointType(EditWaypointActivity wpView) {
this.wpView = wpView;
}
private EditWaypointActivity wpView;
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (null != wpView.wpTypes) {
wpView.type = wpView.wpTypes.get(arg2);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
if (null != wpView.wpTypes) {
arg0.setSelection(wpView.wpTypes.indexOf(wpView.type));
}
}
}
private static class ChangeDistanceUnit implements OnItemSelectedListener {
private ChangeDistanceUnit(EditWaypointActivity unitView) {
this.unitView = unitView;
}
private EditWaypointActivity unitView;
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
unitView.distanceUnit = (String) arg0.getItemAtPosition(arg2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
public static final int SUCCESS = 0;
public static final int UPLOAD_START = 1;
public static final int UPLOAD_ERROR = 2;
public static final int UPLOAD_NOT_POSSIBLE = 3;
public static final int UPLOAD_SUCCESS = 4;
public static final int SAVE_ERROR = 5;
private class CoordsListener implements View.OnClickListener {
@Override
public void onClick(View arg0) {
final String bearingText = ((EditText) findViewById(R.id.bearing)).getText().toString();
// combine distance from EditText and distanceUnit saved from Spinner
final String distanceText = ((EditText) findViewById(R.id.distance)).getText().toString() + distanceUnit;
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;
}
Geopoint coords;
if (StringUtils.isNotBlank(latText) && StringUtils.isNotBlank(lonText)) {
try {
coords = new Geopoint(latText, lonText);
} catch (Geopoint.ParseException e) {
showToast(res.getString(e.resource));
return;
}
} else {
final IGeoData geo = app.currentGeo();
if (geo.getCoords() == null) {
showToast(res.getString(R.string.err_point_curr_position_unavailable));
return;
}
coords = geo.getCoords();
}
if (StringUtils.isNotBlank(bearingText) && StringUtils.isNotBlank(distanceText)) {
// bearing & distance
double bearing;
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 = coords.project(bearing, distance);
}
// if no name is given, just give the waypoint its number as name
final String givenName = ((EditText) findViewById(R.id.name)).getText().toString().trim();
final String name = StringUtils.isNotEmpty(givenName) ? givenName : res.getString(R.string.waypoint) + " " + (wpCount + 1);
final String note = ((EditText) findViewById(R.id.note)).getText().toString().trim();
final Geopoint coordsToSave = coords;
final ProgressDialog progress = ProgressDialog.show(EditWaypointActivity.this, getString(R.string.cache), getString(R.string.waypoint_being_saved), true);
final Handler finishHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO: The order of showToast, progress.dismiss and finish is different in these cases. Why?
switch (msg.what) {
case UPLOAD_SUCCESS:
showToast(getString(R.string.waypoint_coordinates_has_been_modified_on_website, coordsToSave));
progress.dismiss();
finish();
break;
case SUCCESS:
progress.dismiss();
finish();
break;
case UPLOAD_START:
progress.setMessage(getString(R.string.waypoint_coordinates_uploading_to_website, coordsToSave));
break;
case UPLOAD_ERROR:
progress.dismiss();
finish();
showToast(getString(R.string.waypoint_coordinates_upload_error));
break;
case UPLOAD_NOT_POSSIBLE:
progress.dismiss();
finish();
showToast(getString(R.string.waypoint_coordinates_couldnt_be_modified_on_website));
break;
case SAVE_ERROR:
progress.dismiss();
finish(); //TODO: should we close activity here ?
showToast(res.getString(R.string.err_waypoint_add_failed));
break;
default:
throw new UnsupportedOperationException();
}
}
};
class SaveWptTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
final Waypoint waypoint = new Waypoint(name, type, own);
waypoint.setGeocode(geocode);
waypoint.setPrefix(prefix);
waypoint.setLookup(lookup);
waypoint.setCoords(coordsToSave);
waypoint.setNote(note);
waypoint.setVisited(visited);
waypoint.setId(id);
Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS);
if (cache == null) {
finishHandler.sendEmptyMessage(SAVE_ERROR);
return null;
}
Waypoint oldWaypoint = cache.getWaypointById(id);
if (cache.addOrChangeWaypoint(waypoint, true)) {
cgData.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB));
if (!StaticMapsProvider.hasAllStaticMapsForWaypoint(geocode, waypoint)) {
StaticMapsProvider.removeWpStaticMaps(oldWaypoint, geocode);
if (Settings.isStoreOfflineWpMaps()) {
StaticMapsProvider.storeWaypointStaticMap(cache, waypoint, false);
}
}
final RadioButton modifyLocal = (RadioButton) findViewById(R.id.modify_cache_coordinates_local);
final RadioButton modifyBoth = (RadioButton) findViewById(R.id.modify_cache_coordinates_local_and_remote);
if (modifyLocal.isChecked() || modifyBoth.isChecked()) {
if (!cache.hasUserModifiedCoords()) {
final Waypoint origWaypoint = new Waypoint(cgeoapplication.getInstance().getString(R.string.cache_coordinates_original), WaypointType.ORIGINAL, false);
origWaypoint.setCoords(cache.getCoords());
cache.addOrChangeWaypoint(origWaypoint, false);
cache.setUserModifiedCoords(true);
}
cache.setCoords(waypoint.getCoords());
cgData.saveChangedCache(cache);
}
if (modifyBoth.isChecked() && waypoint.getCoords() != null) {
finishHandler.sendEmptyMessage(UPLOAD_START);
if (cache.supportsOwnCoordinates()) {
boolean result = uploadModifiedCoords(cache, waypoint.getCoords());
finishHandler.sendEmptyMessage(result ? SUCCESS : UPLOAD_ERROR);
} else {
showToast(getString(R.string.waypoint_coordinates_couldnt_be_modified_on_website));
finishHandler.sendEmptyMessage(UPLOAD_NOT_POSSIBLE);
}
} else {
finishHandler.sendEmptyMessage(SUCCESS);
}
} else {
finishHandler.sendEmptyMessage(SAVE_ERROR);
}
return null;
}
}
new SaveWptTask().execute();
}
}
private static boolean uploadModifiedCoords(final Geocache cache, final Geopoint waypointUploaded) {
final IConnector con = ConnectorFactory.getConnector(cache);
return con.supportsOwnCoordinates() && con.uploadModifiedCoordinates(cache, waypointUploaded);
}
@Override
public void goManual(final View view) {
ActivityMixin.goManual(this, id >= 0 ? "c:geo-waypoint-edit" : "c:geo-waypoint-new");
}
public static void startActivityEditWaypoint(final Context context, final int waypointId) {
context.startActivity(new Intent(context, EditWaypointActivity.class)
.putExtra(Intents.EXTRA_WAYPOINT_ID, waypointId));
}
public static void startActivityAddWaypoint(final Context context, final Geocache cache) {
context.startActivity(new Intent(context, EditWaypointActivity.class)
.putExtra(Intents.EXTRA_GEOCODE, cache.getGeocode())
.putExtra(Intents.EXTRA_COUNT, cache.getWaypoints().size()));
}
}
|