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
|
package cgeo.geocaching;
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.apps.cache.navi.NavigationAppFactory;
import cgeo.geocaching.enumerations.LoadFlags.RemoveFlag;
import org.apache.commons.lang3.StringUtils;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.EnumSet;
public class cgeowaypoint extends AbstractActivity {
private static final int MENU_ID_NAVIGATION = 0;
private static final int MENU_ID_CACHES_AROUND = 5;
private static final int MENU_ID_DEFAULT_NAVIGATION = 2;
private static final int MENU_ID_OPEN_GEOCACHE = 6;
private cgWaypoint waypoint = null;
private String geocode = null;
private int id = -1;
private ProgressDialog waitDialog = null;
private cgGeo geo = null;
private UpdateLocationCallback geoUpdate = new update();
private Handler loadWaypointHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
if (waypoint == null) {
if (waitDialog != null) {
waitDialog.dismiss();
waitDialog = null;
}
showToast(res.getString(R.string.err_waypoint_load_failed));
finish();
return;
} else {
final TextView identification = (TextView) findViewById(R.id.identification);
final TextView coords = (TextView) findViewById(R.id.coordinates);
final ImageView compass = (ImageView) findViewById(R.id.compass);
final View separator = findViewById(R.id.separator);
final View headline = findViewById(R.id.headline);
registerNavigationMenu(headline);
if (StringUtils.isNotBlank(waypoint.getName())) {
setTitle(Html.fromHtml(waypoint.getName().trim()).toString());
} else {
setTitle(res.getString(R.string.waypoint_title));
}
if (!waypoint.getPrefix().equalsIgnoreCase("OWN")) {
identification.setText(waypoint.getPrefix().trim() + "/" + waypoint.getLookup().trim());
} else {
identification.setText(res.getString(R.string.waypoint_custom));
}
registerNavigationMenu(identification);
waypoint.setIcon(res, identification);
if (waypoint.getCoords() != null) {
coords.setText(Html.fromHtml(waypoint.getCoords().toString()), TextView.BufferType.SPANNABLE);
compass.setVisibility(View.VISIBLE);
separator.setVisibility(View.VISIBLE);
} else {
coords.setText(res.getString(R.string.waypoint_unknown_coordinates));
compass.setVisibility(View.GONE);
separator.setVisibility(View.GONE);
}
registerNavigationMenu(coords);
if (StringUtils.isNotBlank(waypoint.getNote())) {
final TextView note = (TextView) findViewById(R.id.note);
note.setText(Html.fromHtml(waypoint.getNote().trim()), TextView.BufferType.SPANNABLE);
registerNavigationMenu(note);
}
Button buttonEdit = (Button) findViewById(R.id.edit);
buttonEdit.setOnClickListener(new editWaypointListener());
Button buttonDelete = (Button) findViewById(R.id.delete);
if (waypoint.isUserDefined()) {
buttonDelete.setOnClickListener(new deleteWaypointListener());
buttonDelete.setVisibility(View.VISIBLE);
}
if (waitDialog != null) {
waitDialog.dismiss();
waitDialog = null;
}
}
} catch (Exception e) {
if (waitDialog != null) {
waitDialog.dismiss();
waitDialog = null;
}
Log.e(Settings.tag, "cgeowaypoint.loadWaypointHandler: " + e.toString());
}
}
private void registerNavigationMenu(View view) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerForContextMenu(v);
if (navigationPossible()) {
openContextMenu(v);
}
}
});
}
};
public cgeowaypoint() {
super("c:geo-waypoint-details");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme();
setContentView(R.layout.waypoint);
setTitle(R.string.waypoint_title);
// get parameters
Bundle extras = getIntent().getExtras();
// try to get data from extras
if (extras != null) {
id = extras.getInt("waypoint");
geocode = extras.getString("geocode");
}
if (id <= 0) {
showToast(res.getString(R.string.err_waypoint_unknown));
finish();
return;
}
if (geo == null) {
geo = app.startGeo(geoUpdate);
}
waitDialog = ProgressDialog.show(this, null, res.getString(R.string.waypoint_loading), true);
waitDialog.setCancelable(true);
(new loadWaypoint()).start();
}
@Override
public void onResume() {
super.onResume();
if (geo == null) {
geo = app.startGeo(geoUpdate);
}
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();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_ID_DEFAULT_NAVIGATION, 0, NavigationAppFactory.getDefaultNavigationApplication(this).getName()).setIcon(android.R.drawable.ic_menu_compass); // default navigation tool
menu.add(0, MENU_ID_NAVIGATION, 0, res.getString(R.string.cache_menu_navigate)).setIcon(android.R.drawable.ic_menu_mapmode);
menu.add(0, MENU_ID_CACHES_AROUND, 0, res.getString(R.string.cache_menu_around)).setIcon(android.R.drawable.ic_menu_rotate); // caches around
menu.add(0, MENU_ID_OPEN_GEOCACHE, 0, res.getString(R.string.waypoint_menu_open_cache)).setIcon(android.R.drawable.ic_menu_mylocation); // open geocache
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
try {
boolean visible = waypoint != null && waypoint.getCoords() != null;
menu.findItem(MENU_ID_NAVIGATION).setVisible(visible);
menu.findItem(MENU_ID_DEFAULT_NAVIGATION).setVisible(visible);
menu.findItem(MENU_ID_CACHES_AROUND).setVisible(visible);
boolean openGeocache = StringUtils.isEmpty(geocode) && StringUtils.isNotEmpty(waypoint.getGeocode());
menu.findItem(MENU_ID_OPEN_GEOCACHE).setVisible(openGeocache);
} catch (Exception e) {
// nothing
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final int menuItem = item.getItemId();
if (menuItem == MENU_ID_DEFAULT_NAVIGATION) {
goDefaultNavigation(null);
return true;
} else if (menuItem == MENU_ID_CACHES_AROUND) {
cachesAround();
return true;
} else if (menuItem == MENU_ID_OPEN_GEOCACHE) {
goToGeocache();
return true;
} else if (menuItem == MENU_ID_NAVIGATION) {
NavigationAppFactory.showNavigationMenu(geo, this, null, null, waypoint, null);
return true;
}
return false;
}
private void cachesAround() {
if (waypoint == null || waypoint.getCoords() == null) {
showToast(res.getString(R.string.err_location_unknown));
}
cgeocaches.startActivityCachesAround(this, waypoint.getCoords());
finish();
}
private void goToGeocache() {
if (waypoint == null || waypoint.getGeocode() == null) {
showToast(res.getString(R.string.err_waypoint_open_cache_failed));
}
CacheDetailActivity.startActivity(this, waypoint.getGeocode());
finish();
}
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 update implements UpdateLocationCallback {
@Override
public void updateLocation(cgGeo geo) {
// nothing
}
}
private class editWaypointListener implements View.OnClickListener {
public void onClick(View arg0) {
Intent editIntent = new Intent(cgeowaypoint.this, cgeowaypointadd.class);
editIntent.putExtra("waypoint", id);
startActivity(editIntent);
}
}
private class deleteWaypointListener implements View.OnClickListener {
public void onClick(View arg0) {
if (app.deleteWaypoint(id)) {
StaticMapsProvider.removeWpStaticMaps(id, geocode);
app.removeCache(geocode, EnumSet.of(RemoveFlag.REMOVECACHE));
finish();
return;
} else {
showToast(res.getString(R.string.err_waypoint_delete_failed));
}
}
}
/**
* @param view
* this method is also referenced from XML layout
*/
public void goDefaultNavigation(View view) {
if (!navigationPossible()) {
return;
}
NavigationAppFactory.startDefaultNavigationApplication(geo, this, null, null, waypoint, null);
}
private boolean navigationPossible() {
if (waypoint == null || waypoint.getCoords() == null) {
showToast(res.getString(R.string.err_location_unknown));
return false;
}
return true;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (navigationPossible()) {
menu.setHeaderTitle(res.getString(R.string.cache_menu_navigate));
NavigationAppFactory.addMenuItems(menu, this);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
boolean handled = onOptionsItemSelected(item);
if (handled) {
return true;
}
return NavigationAppFactory.onMenuItemSelected(item, geo, this, null, null, waypoint, null);
}
}
|