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
|
package cgeo.geocaching;
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.connector.gc.GCParser;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.geopoint.HumanDistance;
import cgeo.geocaching.network.HtmlImage;
import cgeo.geocaching.ui.CacheDetailsCreator;
import cgeo.geocaching.ui.Formatter;
import cgeo.geocaching.utils.Log;
import org.apache.commons.lang3.StringUtils;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
public class cgeotrackable extends AbstractActivity {
private static final int MENU_LOG_TOUCH = 1;
private static final int MENU_BROWSER_TRACKABLE = 2;
private cgTrackable trackable = null;
private String geocode = null;
private String name = null;
private String guid = null;
private String id = null;
private String contextMenuUser = null;
private LayoutInflater inflater = null;
private ProgressDialog waitDialog = null;
private Handler loadTrackableHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (trackable == null) {
if (waitDialog != null) {
waitDialog.dismiss();
}
if (StringUtils.isNotBlank(geocode)) {
showToast(res.getString(R.string.err_tb_find) + " " + geocode + ".");
} else {
showToast(res.getString(R.string.err_tb_find_that));
}
finish();
return;
}
try {
inflater = getLayoutInflater();
geocode = trackable.getGeocode().toUpperCase();
if (StringUtils.isNotBlank(trackable.getName())) {
setTitle(Html.fromHtml(trackable.getName()).toString());
} else {
setTitle(trackable.getName().toUpperCase());
}
((ScrollView) findViewById(R.id.details_list_box)).setVisibility(View.VISIBLE);
final CacheDetailsCreator details = new CacheDetailsCreator(cgeotrackable.this, (LinearLayout) findViewById(R.id.details_list));
// actiobar icon
if (StringUtils.isNotBlank(trackable.getIconUrl())) {
final TrackableIconHandler iconHandler = new TrackableIconHandler(((TextView) findViewById(R.id.actionbar_title)));
final TrackableIconThread iconThread = new TrackableIconThread(trackable.getIconUrl(), iconHandler);
iconThread.start();
}
// trackable name
details.add(R.string.trackable_name, StringUtils.isNotBlank(trackable.getName()) ? Html.fromHtml(trackable.getName()).toString() : res.getString(R.string.trackable_unknown));
// trackable type
String tbType;
if (StringUtils.isNotBlank(trackable.getType())) {
tbType = Html.fromHtml(trackable.getType()).toString();
} else {
tbType = res.getString(R.string.trackable_unknown);
}
details.add(R.string.trackable_type, tbType);
// trackable geocode
details.add(R.string.trackable_code, trackable.getGeocode().toUpperCase());
// trackable owner
TextView owner = details.add(R.string.trackable_owner, res.getString(R.string.trackable_unknown));
if (StringUtils.isNotBlank(trackable.getOwner())) {
owner.setText(Html.fromHtml(trackable.getOwner()), TextView.BufferType.SPANNABLE);
owner.setOnClickListener(new UserActionsListener());
}
// trackable spotted
if (StringUtils.isNotBlank(trackable.getSpottedName()) ||
trackable.getSpottedType() == cgTrackable.SPOTTED_UNKNOWN ||
trackable.getSpottedType() == cgTrackable.SPOTTED_OWNER
) {
boolean showTimeSpan = true;
StringBuilder text;
if (trackable.getSpottedType() == cgTrackable.SPOTTED_CACHE) {
text = new StringBuilder(res.getString(R.string.trackable_spotted_in_cache) + ' ' + Html.fromHtml(trackable.getSpottedName()).toString());
} else if (trackable.getSpottedType() == cgTrackable.SPOTTED_USER) {
text = new StringBuilder(res.getString(R.string.trackable_spotted_at_user) + ' ' + Html.fromHtml(trackable.getSpottedName()).toString());
} else if (trackable.getSpottedType() == cgTrackable.SPOTTED_UNKNOWN) {
text = new StringBuilder(res.getString(R.string.trackable_spotted_unknown_location));
} else if (trackable.getSpottedType() == cgTrackable.SPOTTED_OWNER) {
text = new StringBuilder(res.getString(R.string.trackable_spotted_owner));
} else {
text = new StringBuilder("N/A");
showTimeSpan = false;
}
// days since last spotting
if (showTimeSpan && trackable.getLogs() != null) {
for (LogEntry log : trackable.getLogs()) {
if (log.type == LogType.RETRIEVED_IT || log.type == LogType.GRABBED_IT || log.type == LogType.DISCOVERED_IT || log.type == LogType.PLACED_IT) {
final int days = log.daysSinceLog();
text.append(" (").append(res.getQuantityString(R.plurals.days_ago, days, days)).append(')');
break;
}
}
}
final TextView spotted = details.add(R.string.trackable_spotted, text.toString());
spotted.setClickable(true);
if (cgTrackable.SPOTTED_CACHE == trackable.getSpottedType()) {
spotted.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
CacheDetailActivity.startActivityGuid(cgeotrackable.this, trackable.getSpottedGuid(), trackable.getSpottedName());
}
});
} else if (cgTrackable.SPOTTED_USER == trackable.getSpottedType()) {
spotted.setOnClickListener(new UserActionsListener());
}
}
// trackable origin
if (StringUtils.isNotBlank(trackable.getOrigin())) {
TextView origin = details.add(R.string.trackable_origin, "");
origin.setText(Html.fromHtml(trackable.getOrigin()), TextView.BufferType.SPANNABLE);
}
// trackable released
if (trackable.getReleased() != null) {
details.add(R.string.trackable_released, Formatter.formatDate(trackable.getReleased().getTime()));
}
// trackable distance
if (trackable.getDistance() >= 0) {
details.add(R.string.trackable_distance, HumanDistance.getHumanDistance(trackable.getDistance()));
}
// trackable goal
if (StringUtils.isNotBlank(trackable.getGoal())) {
((LinearLayout) findViewById(R.id.goal_box)).setVisibility(View.VISIBLE);
TextView descView = (TextView) findViewById(R.id.goal);
descView.setVisibility(View.VISIBLE);
descView.setText(Html.fromHtml(trackable.getGoal(), new HtmlImage(geocode, true, 0, false), null), TextView.BufferType.SPANNABLE);
descView.setMovementMethod(LinkMovementMethod.getInstance());
}
// trackable details
if (StringUtils.isNotBlank(trackable.getDetails())) {
((LinearLayout) findViewById(R.id.details_box)).setVisibility(View.VISIBLE);
TextView descView = (TextView) findViewById(R.id.details);
descView.setVisibility(View.VISIBLE);
descView.setText(Html.fromHtml(trackable.getDetails(), new HtmlImage(geocode, true, 0, false), null), TextView.BufferType.SPANNABLE);
descView.setMovementMethod(LinkMovementMethod.getInstance());
}
// trackable image
if (StringUtils.isNotBlank(trackable.getImage())) {
((LinearLayout) findViewById(R.id.image_box)).setVisibility(View.VISIBLE);
LinearLayout imgView = (LinearLayout) findViewById(R.id.image);
final ImageView trackableImage = (ImageView) inflater.inflate(R.layout.trackable_image, null);
trackableImage.setImageResource(R.drawable.image_not_loaded);
trackableImage.setClickable(true);
trackableImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(trackable.getImage())));
}
});
// try to load image
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
BitmapDrawable image = (BitmapDrawable) message.obj;
if (image != null) {
trackableImage.setImageDrawable((BitmapDrawable) message.obj);
}
}
};
new Thread() {
@Override
public void run() {
BitmapDrawable image;
try {
HtmlImage imgGetter = new HtmlImage(geocode, true, 0, false);
image = imgGetter.getDrawable(trackable.getImage());
Message message = handler.obtainMessage(0, image);
handler.sendMessage(message);
} catch (Exception e) {
Log.e("cgeospoilers.onCreate.onClick.run: " + e.toString());
}
}
}.start();
imgView.addView(trackableImage);
}
} catch (Exception e) {
Log.e("cgeotrackable.loadTrackableHandler: " + e.toString() + Arrays.toString(e.getStackTrace()));
}
displayLogs();
if (waitDialog != null) {
waitDialog.dismiss();
}
}
};
public cgeotrackable() {
super("c:geo-trackable-details");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme();
setContentView(R.layout.trackable_detail);
setTitle(res.getString(R.string.trackable));
// get parameters
Bundle extras = getIntent().getExtras();
Uri uri = getIntent().getData();
// try to get data from extras
if (extras != null) {
geocode = extras.getString("geocode");
name = extras.getString("name");
guid = extras.getString("guid");
id = extras.getString("id");
}
// try to get data from URI
if (geocode == null && guid == null && id == null && uri != null) {
String uriHost = uri.getHost().toLowerCase();
if (uriHost.contains("geocaching.com")) {
geocode = uri.getQueryParameter("tracker");
guid = uri.getQueryParameter("guid");
id = uri.getQueryParameter("id");
if (StringUtils.isNotBlank(geocode)) {
geocode = geocode.toUpperCase();
guid = null;
id = null;
} else if (StringUtils.isNotBlank(guid)) {
geocode = null;
guid = guid.toLowerCase();
id = null;
} else if (StringUtils.isNotBlank(id)) {
geocode = null;
guid = null;
id = id.toLowerCase();
} else {
showToast(res.getString(R.string.err_tb_details_open));
finish();
return;
}
} else if (uriHost.contains("coord.info")) {
String uriPath = uri.getPath().toLowerCase();
if (uriPath != null && uriPath.startsWith("/tb")) {
geocode = uriPath.substring(1).toUpperCase();
guid = null;
id = null;
} else {
showToast(res.getString(R.string.err_tb_details_open));
finish();
return;
}
}
}
// no given data
if (geocode == null && guid == null && id == null) {
showToast(res.getString(R.string.err_tb_display));
finish();
return;
}
String message;
if (StringUtils.isNotBlank(name)) {
message = Html.fromHtml(name).toString();
} else if (StringUtils.isNotBlank(geocode)) {
message = geocode.toUpperCase();
} else {
message = res.getString(R.string.trackable);
}
waitDialog = ProgressDialog.show(this, message, res.getString(R.string.trackable_details_loading), true, true);
LoadTrackableThread thread = new LoadTrackableThread(loadTrackableHandler, geocode, guid, id);
thread.start();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo info) {
super.onCreateContextMenu(menu, view, info);
final int viewId = view.getId();
if (viewId == R.id.author) { // Log item author
contextMenuUser = ((TextView) view).getText().toString();
} else { // Trackable owner, and user holding trackable now
RelativeLayout itemLayout = (RelativeLayout) view.getParent();
TextView itemName = (TextView) itemLayout.findViewById(R.id.name);
String selectedName = itemName.getText().toString();
if (selectedName.equals(res.getString(R.string.trackable_owner))) {
contextMenuUser = trackable.getOwner();
} else if (selectedName.equals(res.getString(R.string.trackable_spotted))) {
contextMenuUser = trackable.getSpottedName();
}
}
menu.setHeaderTitle(res.getString(R.string.user_menu_title) + " " + contextMenuUser);
menu.add(viewId, 1, 0, res.getString(R.string.user_menu_view_hidden));
menu.add(viewId, 2, 0, res.getString(R.string.user_menu_view_found));
menu.add(viewId, 3, 0, res.getString(R.string.user_menu_open_browser));
}
@Override
public boolean onContextItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case 1:
cgeocaches.startActivityOwner(this, contextMenuUser);
return true;
case 2:
cgeocaches.startActivityUserName(this, contextMenuUser);
return true;
case 3:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.geocaching.com/profile/?u=" + URLEncoder.encode(contextMenuUser))));
return true;
default:
return false;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_LOG_TOUCH, 0, res.getString(R.string.trackable_log_touch)).setIcon(R.drawable.ic_menu_agenda); // log touch
menu.add(0, MENU_BROWSER_TRACKABLE, 0, res.getString(R.string.trackable_browser_open)).setIcon(R.drawable.ic_menu_info_details); // browser
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_LOG_TOUCH:
LogTrackableActivity.startActivity(this, trackable);
return true;
case MENU_BROWSER_TRACKABLE:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(trackable.getUrl())));
return true;
}
return false;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(MENU_LOG_TOUCH).setEnabled(trackable.isLoggable());
menu.findItem(MENU_BROWSER_TRACKABLE).setEnabled(StringUtils.isNotBlank(trackable.getUrl()));
return super.onPrepareOptionsMenu(menu);
}
private class LoadTrackableThread extends Thread {
final private Handler handler;
final private String geocode;
final private String guid;
final private String id;
public LoadTrackableThread(Handler handlerIn, String geocodeIn, String guidIn, String idIn) {
handler = handlerIn;
geocode = geocodeIn;
guid = guidIn;
id = idIn;
if (geocode == null && guid == null && id == null) {
showToast(res.getString(R.string.err_tb_forgot));
stop();
finish();
}
}
@Override
public void run() {
// for non TB trackables, we should just use what we have in the database
trackable = cgeoapplication.getInstance().getTrackableByGeocode(geocode);
if ((trackable == null || trackable.isLoggable()) && !StringUtils.startsWithIgnoreCase(geocode, "GK")) {
trackable = GCParser.searchTrackable(geocode, guid, id);
}
handler.sendMessage(Message.obtain());
}
}
private void displayLogs() {
// trackable logs
LinearLayout listView = (LinearLayout) findViewById(R.id.log_list);
listView.removeAllViews();
RelativeLayout rowView;
if (trackable != null && trackable.getLogs() != null) {
for (LogEntry log : trackable.getLogs()) {
rowView = (RelativeLayout) inflater.inflate(R.layout.trackable_logs_item, null);
if (log.date > 0) {
((TextView) rowView.findViewById(R.id.added)).setText(Formatter.formatShortDate(log.date));
}
((TextView) rowView.findViewById(R.id.type)).setText(log.type.getL10n());
((TextView) rowView.findViewById(R.id.author)).setText(Html.fromHtml(log.author), TextView.BufferType.SPANNABLE);
if (StringUtils.isBlank(log.cacheName)) {
((TextView) rowView.findViewById(R.id.location)).setVisibility(View.GONE);
} else {
((TextView) rowView.findViewById(R.id.location)).setText(Html.fromHtml(log.cacheName));
final String cacheGuid = log.cacheGuid;
final String cacheName = log.cacheName;
((TextView) rowView.findViewById(R.id.location)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
CacheDetailActivity.startActivityGuid(cgeotrackable.this, cacheGuid, Html.fromHtml(cacheName).toString());
}
});
}
TextView logView = (TextView) rowView.findViewById(R.id.log);
logView.setMovementMethod(LinkMovementMethod.getInstance());
logView.setText(Html.fromHtml(log.log, new HtmlImage(null, false, StoredList.TEMPORARY_LIST_ID, false), null), TextView.BufferType.SPANNABLE);
// add LogImages
LinearLayout logLayout = (LinearLayout) rowView.findViewById(R.id.log_layout);
if (log.hasLogImages()) {
final ArrayList<cgImage> logImages = new ArrayList<cgImage>(log.getLogImages());
final View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
cgeoimages.startActivityLogImages(cgeotrackable.this, trackable.getGeocode(), logImages);
}
};
LinearLayout log_imgView = (LinearLayout) getLayoutInflater().inflate(R.layout.trackable_logs_img, null);
TextView log_img_title = (TextView) log_imgView.findViewById(R.id.title);
log_img_title.setText(log.getImageTitles());
log_img_title.setOnClickListener(listener);
logLayout.addView(log_imgView);
}
((TextView) rowView.findViewById(R.id.author)).setOnClickListener(new UserActionsListener());
listView.addView(rowView);
}
if (trackable.getLogs().size() > 0) {
((LinearLayout) findViewById(R.id.log_box)).setVisibility(View.VISIBLE);
}
}
}
private class UserActionsListener implements View.OnClickListener {
@Override
public void onClick(View view) {
if (view == null) {
return;
}
try {
registerForContextMenu(view);
openContextMenu(view);
} catch (Exception e) {
Log.e("cgeotrackable.UserActionsListener.onClick ", e);
}
}
}
private class TrackableIconThread extends Thread {
final private String url;
final private Handler handler;
public TrackableIconThread(String urlIn, Handler handlerIn) {
url = urlIn;
handler = handlerIn;
}
@Override
public void run() {
if (url == null || handler == null) {
return;
}
BitmapDrawable image;
try {
HtmlImage imgGetter = new HtmlImage(trackable.getGeocode(), false, 0, false);
image = imgGetter.getDrawable(url);
Message message = handler.obtainMessage(0, image);
handler.sendMessage(message);
} catch (Exception e) {
Log.e("cgeotrackable.TrackableIconThread.run: " + e.toString());
}
}
}
private static class TrackableIconHandler extends Handler {
final private TextView view;
public TrackableIconHandler(TextView viewIn) {
view = viewIn;
}
@Override
public void handleMessage(Message message) {
final BitmapDrawable image = (BitmapDrawable) message.obj;
if (image != null && view != null) {
image.setBounds(0, 0, view.getHeight(), view.getHeight());
view.setCompoundDrawables(image, null, null, null);
}
}
}
public static void startActivity(final AbstractActivity fromContext,
final String guid, final String geocode, final String name) {
final Intent trackableIntent = new Intent(fromContext, cgeotrackable.class);
trackableIntent.putExtra("guid", guid);
trackableIntent.putExtra("geocode", geocode);
trackableIntent.putExtra("name", name);
fromContext.startActivity(trackableIntent);
}
}
|