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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
|
package cgeo.geocaching;
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.apps.cache.navi.NavigationAppFactory;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.gcvote.GCVote;
import cgeo.geocaching.gcvote.GCVoteRating;
import cgeo.geocaching.utils.CancellableHandler;
import org.apache.commons.lang3.StringUtils;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class cgeopopup extends AbstractActivity {
private boolean fromDetail = false;
private LayoutInflater inflater = null;
private String geocode = null;
private cgCache cache = null;
private cgGeo geo = null;
private UpdateLocationCallback geoUpdate = new update();
private ProgressDialog storeDialog = null;
private ProgressDialog dropDialog = null;
private TextView cacheDistance = null;
private Handler ratingHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
final Bundle data = msg.getData();
setRating(data.getFloat("rating"), data.getInt("votes"));
} catch (Exception e) {
// nothing
}
}
};
private CancellableHandler storeCacheHandler = new CancellableHandler() {
@Override
public void handleRegularMessage(Message msg) {
try {
if (storeDialog != null) {
storeDialog.dismiss();
}
finish();
return;
} catch (Exception e) {
showToast(res.getString(R.string.err_store));
Log.e(Settings.tag, "cgeopopup.storeCacheHandler: " + e.toString());
}
if (storeDialog != null) {
storeDialog.dismiss();
}
init();
}
};
private Handler dropCacheHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
if (dropDialog != null) {
dropDialog.dismiss();
}
finish();
return;
} catch (Exception e) {
showToast(res.getString(R.string.err_drop));
Log.e(Settings.tag, "cgeopopup.dropCacheHandler: " + e.toString());
}
if (dropDialog != null) {
dropDialog.dismiss();
}
init();
}
};
public cgeopopup() {
super("c:geo-cache-info");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout
setTheme(R.style.transparent);
setContentView(R.layout.popup);
setTitle(res.getString(R.string.detail));
// get parameters
Bundle extras = getIntent().getExtras();
if (extras != null) {
fromDetail = extras.getBoolean("fromdetail");
geocode = extras.getString("geocode");
}
if (StringUtils.isBlank(geocode)) {
showToast(res.getString(R.string.err_detail_cache_find));
finish();
return;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 2, 0, res.getString(R.string.cache_menu_compass)).setIcon(android.R.drawable.ic_menu_compass); // compass
SubMenu subMenu = menu.addSubMenu(1, 0, 0, res.getString(R.string.cache_menu_navigate)).setIcon(android.R.drawable.ic_menu_mapmode);
NavigationAppFactory.addMenuItems(subMenu, this, res);
addVisitMenu(menu, cache);
menu.add(0, 5, 0, res.getString(R.string.cache_menu_around)).setIcon(android.R.drawable.ic_menu_rotate); // caches around
menu.add(0, 7, 0, res.getString(R.string.cache_menu_browser)).setIcon(android.R.drawable.ic_menu_info_details); // browser
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
try {
if (cache != null && cache.getCoords() != null) {
menu.findItem(0).setVisible(true);
menu.findItem(2).setVisible(true);
menu.findItem(5).setVisible(true);
} else {
menu.findItem(0).setVisible(false);
menu.findItem(2).setVisible(false);
menu.findItem(5).setVisible(false);
}
boolean visitPossible = !fromDetail && Settings.isLogin();
menu.findItem(MENU_LOG_VISIT).setEnabled(visitPossible);
} catch (Exception e) {
// nothing
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final int menuItem = item.getItemId();
if (menuItem == 2) {
navigateTo();
return true;
} else if (menuItem == 5) {
cachesAround();
return true;
} else if (menuItem == MENU_LOG_VISIT) {
cache.logVisit(this);
finish();
return true;
} else if (menuItem == 7) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.geocaching.com/seek/cache_details.aspx?wp=" + cache.getGeocode())));
return true;
}
if (NavigationAppFactory.onMenuItemSelected(item, geo, this, res, cache, null, null, null)) {
return true;
}
int logType = menuItem - MENU_LOG_VISIT_OFFLINE;
cache.logOffline(this, logType);
return true;
}
private void init() {
if (geo == null) {
geo = app.startGeo(geoUpdate);
}
app.setAction(geocode);
cache = app.getCacheByGeocode(geocode);
if (cache == null) {
showToast(res.getString(R.string.err_detail_cache_find));
finish();
return;
}
try {
RelativeLayout itemLayout;
TextView itemName;
TextView itemValue;
LinearLayout itemStars;
if (StringUtils.isNotBlank(cache.getName())) {
setTitle(cache.getName());
} else {
setTitle(geocode.toUpperCase());
}
inflater = getLayoutInflater();
geocode = cache.getGeocode().toUpperCase();
((ScrollView) findViewById(R.id.details_list_box)).setVisibility(View.VISIBLE);
LinearLayout detailsList = (LinearLayout) findViewById(R.id.details_list);
detailsList.removeAllViews();
// actionbar icon
((TextView) findViewById(R.id.actionbar_title)).setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(cgBase.getCacheIcon(cache.getType())), null, null, null);
// cache type
itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null);
itemName = (TextView) itemLayout.findViewById(R.id.name);
itemValue = (TextView) itemLayout.findViewById(R.id.value);
itemName.setText(res.getString(R.string.cache_type));
String cacheType = cache.getType() != CacheType.UNKNOWN ? cache.getType().getL10n() : CacheType.MYSTERY.getL10n();
if (cache.getSize() != null) {
itemValue.setText(cacheType
+ " (" + cache.getSize().getL10n() + ")");
} else {
itemValue.setText(cacheType);
}
detailsList.addView(itemLayout);
// gc-code
itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null);
itemName = (TextView) itemLayout.findViewById(R.id.name);
itemValue = (TextView) itemLayout.findViewById(R.id.value);
itemName.setText(res.getString(R.string.cache_geocode));
itemValue.setText(cache.getGeocode().toUpperCase());
detailsList.addView(itemLayout);
// cache state
if (cache.isArchived() || cache.isDisabled() || cache.isMembers() || cache.isFound()) {
itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null);
itemName = (TextView) itemLayout.findViewById(R.id.name);
itemValue = (TextView) itemLayout.findViewById(R.id.value);
itemName.setText(res.getString(R.string.cache_status));
StringBuilder state = new StringBuilder();
if (cache.isFound()) {
if (state.length() > 0) {
state.append(", ");
}
state.append(res.getString(R.string.cache_status_found));
}
if (cache.isArchived()) {
if (state.length() > 0) {
state.append(", ");
}
state.append(res.getString(R.string.cache_status_archived));
}
if (cache.isDisabled()) {
if (state.length() > 0) {
state.append(", ");
}
state.append(res.getString(R.string.cache_status_disabled));
}
if (cache.isMembers()) {
if (state.length() > 0) {
state.append(", ");
}
state.append(res.getString(R.string.cache_status_premium));
}
itemValue.setText(state.toString());
detailsList.addView(itemLayout);
state = null;
}
// distance
itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_item, null);
itemName = (TextView) itemLayout.findViewById(R.id.name);
itemValue = (TextView) itemLayout.findViewById(R.id.value);
itemName.setText(res.getString(R.string.cache_distance));
itemValue.setText("--");
detailsList.addView(itemLayout);
cacheDistance = itemValue;
// difficulty
if (cache.getDifficulty() > 0f) {
itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_layout, null);
itemName = (TextView) itemLayout.findViewById(R.id.name);
itemValue = (TextView) itemLayout.findViewById(R.id.value);
itemStars = (LinearLayout) itemLayout.findViewById(R.id.stars);
itemName.setText(res.getString(R.string.cache_difficulty));
itemValue.setText(String.format("%.1f", cache.getDifficulty()) + ' ' + res.getString(R.string.cache_rating_of) + " 5");
for (int i = 0; i <= 4; i++) {
ImageView star = (ImageView) inflater.inflate(R.layout.star, null);
if ((cache.getDifficulty() - i) >= 1.0) {
star.setImageResource(R.drawable.star_on);
} else if ((cache.getDifficulty() - i) > 0.0) {
star.setImageResource(R.drawable.star_half);
} else {
star.setImageResource(R.drawable.star_off);
}
itemStars.addView(star);
}
detailsList.addView(itemLayout);
}
// terrain
if (cache.getTerrain() > 0f) {
itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_layout, null);
itemName = (TextView) itemLayout.findViewById(R.id.name);
itemValue = (TextView) itemLayout.findViewById(R.id.value);
itemStars = (LinearLayout) itemLayout.findViewById(R.id.stars);
itemName.setText(res.getString(R.string.cache_terrain));
itemValue.setText(String.format("%.1f", cache.getTerrain()) + ' ' + res.getString(R.string.cache_rating_of) + " 5");
for (int i = 0; i <= 4; i++) {
ImageView star = (ImageView) inflater.inflate(R.layout.star, null);
if ((cache.getTerrain() - i) >= 1.0) {
star.setImageResource(R.drawable.star_on);
} else if ((cache.getTerrain() - i) > 0.0) {
star.setImageResource(R.drawable.star_half);
} else {
star.setImageResource(R.drawable.star_off);
}
itemStars.addView(star);
}
detailsList.addView(itemLayout);
}
// rating
if (cache.getRating() > 0) {
setRating(cache.getRating(), cache.getVotes());
} else {
if (Settings.isRatingWanted() && cache.supportsGCVote()) {
(new Thread() {
@Override
public void run() {
GCVoteRating rating = GCVote.getRating(cache.getGuid(), geocode);
Message msg = new Message();
Bundle bundle = new Bundle();
if (rating == null) {
return;
}
bundle.putFloat("rating", rating.getRating());
bundle.putInt("votes", rating.getVotes());
msg.setData(bundle);
ratingHandler.sendMessage(msg);
}
}).start();
}
}
// more details
if (!fromDetail) {
((LinearLayout) findViewById(R.id.more_details_box)).setVisibility(View.VISIBLE);
Button buttonMore = (Button) findViewById(R.id.more_details);
buttonMore.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent cachesIntent = new Intent(cgeopopup.this, CacheDetailActivity.class);
cachesIntent.putExtra("geocode", geocode.toUpperCase());
startActivity(cachesIntent);
finish();
return;
}
});
} else {
((LinearLayout) findViewById(R.id.more_details_box)).setVisibility(View.GONE);
}
if (!fromDetail) {
((LinearLayout) findViewById(R.id.offline_box)).setVisibility(View.VISIBLE);
// offline use
final TextView offlineText = (TextView) findViewById(R.id.offline_text);
final Button offlineRefresh = (Button) findViewById(R.id.offline_refresh);
final Button offlineStore = (Button) findViewById(R.id.offline_store);
if (cache.getReason() > 0) {
long diff = (System.currentTimeMillis() / (60 * 1000)) - (cache.getDetailedUpdate() / (60 * 1000)); // minutes
String ago = "";
if (diff < 15) {
ago = res.getString(R.string.cache_offline_time_mins_few);
} else if (diff < 50) {
ago = res.getString(R.string.cache_offline_time_about) + " " + diff + " " + res.getString(R.string.cache_offline_time_mins);
} else if (diff < 90) {
ago = res.getString(R.string.cache_offline_time_about) + " " + res.getString(R.string.cache_offline_time_hour);
} else if (diff < (48 * 60)) {
ago = res.getString(R.string.cache_offline_time_about) + " " + (diff / 60) + " " + res.getString(R.string.cache_offline_time_hours);
} else {
ago = res.getString(R.string.cache_offline_time_about) + " " + (diff / (24 * 60)) + " " + res.getString(R.string.cache_offline_time_days);
}
offlineText.setText(res.getString(R.string.cache_offline_stored) + "\n" + ago);
offlineRefresh.setVisibility(View.VISIBLE);
offlineRefresh.setEnabled(true);
offlineRefresh.setOnClickListener(new storeCache());
offlineStore.setText(res.getString(R.string.cache_offline_drop));
offlineStore.setEnabled(true);
offlineStore.setOnClickListener(new dropCache());
} else {
offlineText.setText(res.getString(R.string.cache_offline_not_ready));
offlineRefresh.setVisibility(View.GONE);
offlineRefresh.setEnabled(false);
offlineRefresh.setOnTouchListener(null);
offlineRefresh.setOnClickListener(null);
offlineStore.setText(res.getString(R.string.cache_offline_store));
offlineStore.setEnabled(true);
offlineStore.setOnClickListener(new storeCache());
}
} else {
((LinearLayout) findViewById(R.id.offline_box)).setVisibility(View.GONE);
}
} catch (Exception e) {
Log.e(Settings.tag, "cgeopopup.init: " + e.toString());
}
if (geo != null) {
geoUpdate.updateLocation(geo);
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
init();
}
@Override
public void onResume() {
super.onResume();
init();
}
@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) {
if (geo == null) {
return;
}
try {
if (geo.coordsNow != null && cache != null && cache.getCoords() != null) {
cacheDistance.setText(cgBase.getHumanDistance(geo.coordsNow.distanceTo(cache.getCoords())));
cacheDistance.bringToFront();
}
} catch (Exception e) {
Log.w(Settings.tag, "Failed to update location.");
}
}
}
private void navigateTo() {
if (cache == null || cache.getCoords() == null) {
showToast(res.getString(R.string.err_location_unknown));
}
cgeonavigate navigateActivity = new cgeonavigate();
Intent navigateIntent = new Intent(this, navigateActivity.getClass());
navigateIntent.putExtra("latitude", cache.getCoords().getLatitude());
navigateIntent.putExtra("longitude", cache.getCoords().getLongitude());
navigateIntent.putExtra("geocode", "");
navigateIntent.putExtra("name", res.getString(R.string.search_some_destination));
startActivity(navigateIntent);
}
private void cachesAround() {
if (cache == null || cache.getCoords() == null) {
showToast(res.getString(R.string.err_location_unknown));
}
cgeocaches.startActivityCachesAround(this, cache.getCoords());
finish();
}
private class storeCache implements View.OnClickListener {
public void onClick(View arg0) {
if (dropDialog != null && dropDialog.isShowing()) {
showToast("Still removing this cache.");
return;
}
storeDialog = ProgressDialog.show(cgeopopup.this, res.getString(R.string.cache_dialog_offline_save_title), res.getString(R.string.cache_dialog_offline_save_message), true);
storeDialog.setCancelable(false);
Thread thread = new storeCacheThread(storeCacheHandler);
thread.start();
}
}
private class storeCacheThread extends Thread {
final private CancellableHandler handler;
public storeCacheThread(final CancellableHandler handler) {
this.handler = handler;
}
@Override
public void run() {
cgBase.storeCache(app, cgeopopup.this, cache, null, 1, handler);
}
}
private class dropCache implements View.OnClickListener {
public void onClick(View arg0) {
if (storeDialog != null && storeDialog.isShowing()) {
showToast("Still saving this cache.");
return;
}
dropDialog = ProgressDialog.show(cgeopopup.this, res.getString(R.string.cache_dialog_offline_drop_title), res.getString(R.string.cache_dialog_offline_drop_message), true);
dropDialog.setCancelable(false);
Thread thread = new dropCacheThread(dropCacheHandler);
thread.start();
}
}
private class dropCacheThread extends Thread {
private Handler handler = null;
public dropCacheThread(Handler handlerIn) {
handler = handlerIn;
}
@Override
public void run() {
cgBase.dropCache(app, cache, handler);
}
}
private void setRating(float rating, int votes) {
if (rating <= 0) {
return;
}
RelativeLayout itemLayout;
TextView itemName;
TextView itemValue;
LinearLayout itemStars;
LinearLayout detailsList = (LinearLayout) findViewById(R.id.details_list);
itemLayout = (RelativeLayout) inflater.inflate(R.layout.cache_layout, null);
itemName = (TextView) itemLayout.findViewById(R.id.name);
itemValue = (TextView) itemLayout.findViewById(R.id.value);
itemStars = (LinearLayout) itemLayout.findViewById(R.id.stars);
itemName.setText(res.getString(R.string.cache_rating));
itemValue.setText(String.format("%.1f", rating) + ' ' + res.getString(R.string.cache_rating_of) + " 5");
itemStars.addView(cgBase.createStarRating(rating, 5, this), 1);
if (votes > 0) {
final TextView itemAddition = (TextView) itemLayout.findViewById(R.id.addition);
itemAddition.setText("(" + votes + ")");
itemAddition.setVisibility(View.VISIBLE);
}
detailsList.addView(itemLayout);
}
/**
* @param view
* unused here but needed since this method is referenced from XML layout
*/
public void goCompass(View view) {
if (cache == null || cache.getCoords() == null) {
showToast(res.getString(R.string.cache_coordinates_no));
return;
}
cgeonavigate navigateActivity = new cgeonavigate();
Intent navigateIntent = new Intent(cgeopopup.this, navigateActivity.getClass());
navigateIntent.putExtra("latitude", cache.getCoords().getLatitude());
navigateIntent.putExtra("longitude", cache.getCoords().getLongitude());
navigateIntent.putExtra("geocode", cache.getGeocode().toUpperCase());
navigateIntent.putExtra("name", cache.getName());
startActivity(navigateIntent);
finish();
}
@Override
public void goManual(View view) {
super.goManual(view);
finish();
}
}
|