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
|
package cgeo.geocaching;
import butterknife.ButterKnife;
import butterknife.InjectView;
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.activity.AbstractViewPagerActivity;
import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.connector.trackable.TrackableConnector;
import cgeo.geocaching.connector.trackable.TravelBugConnector;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.location.Units;
import cgeo.geocaching.network.AndroidBeam;
import cgeo.geocaching.network.HtmlImage;
import cgeo.geocaching.ui.AbstractCachingPageViewCreator;
import cgeo.geocaching.ui.AnchorAwareLinkMovementMethod;
import cgeo.geocaching.ui.CacheDetailsCreator;
import cgeo.geocaching.ui.ImagesList;
import cgeo.geocaching.ui.UserActionsClickListener;
import cgeo.geocaching.ui.UserNameClickListener;
import cgeo.geocaching.ui.logs.TrackableLogsViewCreator;
import cgeo.geocaching.utils.Formatter;
import cgeo.geocaching.utils.HtmlUtils;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.RxUtils;
import cgeo.geocaching.utils.UnknownTagsHandler;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.jdt.annotation.Nullable;
import rx.Observable;
import rx.android.app.AppObservable;
import rx.android.view.OnClickEvent;
import rx.android.view.ViewObservable;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.subscriptions.CompositeSubscription;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.view.ActionMode;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class TrackableActivity extends AbstractViewPagerActivity<TrackableActivity.Page> implements AndroidBeam.ActivitySharingInterface {
private CompositeSubscription createSubscriptions;
public enum Page {
DETAILS(R.string.detail),
LOGS(R.string.cache_logs),
IMAGES(R.string.cache_images);
private final int resId;
Page(final int resId) {
this.resId = resId;
}
}
private Trackable trackable = null;
private String geocode = null;
private String name = null;
private String guid = null;
private String id = null;
private LayoutInflater inflater = null;
private ProgressDialog waitDialog = null;
private CharSequence clickedItemText = null;
private ImagesList imagesList = null;
/**
* Action mode of the current contextual action bar (e.g. for copy and share actions).
*/
private ActionMode currentActionMode;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState, R.layout.viewpager_activity);
createSubscriptions = new CompositeSubscription();
// set title in code, as the activity needs a hard coded title due to the intent filters
setTitle(res.getString(R.string.trackable));
// get parameters
final Bundle extras = getIntent().getExtras();
final Uri uri = AndroidBeam.getUri(getIntent());
if (extras != null) {
// try to get data from extras
geocode = extras.getString(Intents.EXTRA_GEOCODE);
name = extras.getString(Intents.EXTRA_NAME);
guid = extras.getString(Intents.EXTRA_GUID);
id = extras.getString(Intents.EXTRA_ID);
}
// try to get data from URI
if (geocode == null && guid == null && id == null && uri != null) {
geocode = ConnectorFactory.getTrackableFromURL(uri.toString());
final String uriHost = uri.getHost().toLowerCase(Locale.US);
if (uriHost.endsWith("geocaching.com")) {
geocode = uri.getQueryParameter("tracker");
guid = uri.getQueryParameter("guid");
id = uri.getQueryParameter("id");
if (StringUtils.isNotBlank(geocode)) {
geocode = geocode.toUpperCase(Locale.US);
guid = null;
id = null;
} else if (StringUtils.isNotBlank(guid)) {
geocode = null;
guid = guid.toLowerCase(Locale.US);
id = null;
} else if (StringUtils.isNotBlank(id)) {
geocode = null;
guid = null;
id = id.toLowerCase(Locale.US);
} 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;
}
final String message;
if (StringUtils.isNotBlank(name)) {
message = Html.fromHtml(name).toString();
} else if (StringUtils.isNotBlank(geocode)) {
message = geocode;
} else {
message = res.getString(R.string.trackable);
}
// If we have a newer Android device setup Android Beam for easy cache sharing
AndroidBeam.enable(this, this);
createViewPager(0, new OnPageSelectedListener() {
@Override
public void onPageSelected(final int position) {
// Lazy loading of trackable images
if (getPage(position) == Page.IMAGES) {
loadTrackableImages();
}
}
});
refreshTrackable(message);
}
private void refreshTrackable(final String message) {
waitDialog = ProgressDialog.show(this, message, res.getString(R.string.trackable_details_loading), true, true);
createSubscriptions.add(AppObservable.bindActivity(this, loadTrackable(geocode, guid, id)).singleOrDefault(null).subscribe(new Action1<Trackable>() {
@Override
public void call(final Trackable trackable) {
TrackableActivity.this.trackable = trackable;
displayTrackable();
}
}));
}
@Nullable
@Override
public String getAndroidBeamUri() {
return trackable != null ? trackable.getUrl() : null;
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.trackable_activity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_log_touch:
startActivityForResult(LogTrackableActivity.getIntent(this, trackable), LogTrackableActivity.LOG_TRACKABLE);
return true;
case R.id.menu_browser_trackable:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(trackable.getUrl())));
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
if (trackable != null) {
menu.findItem(R.id.menu_log_touch).setVisible(StringUtils.isNotBlank(geocode) && trackable.isLoggable());
menu.findItem(R.id.menu_browser_trackable).setVisible(trackable.hasUrl());
}
return super.onPrepareOptionsMenu(menu);
}
private static Observable<Trackable> loadTrackable(final String geocode, final String guid, final String id) {
return Observable.defer(new Func0<Observable<Trackable>>() {
@Override
public Observable<Trackable> call() {
if (StringUtils.isNotEmpty(geocode)) {
// iterate over the connectors as some codes may be handled by multiple connectors
for (final TrackableConnector trackableConnector : ConnectorFactory.getTrackableConnectors()) {
if (trackableConnector.canHandleTrackable(geocode)) {
final Trackable trackable = trackableConnector.searchTrackable(geocode, guid, id);
if (trackable != null) {
return Observable.just(trackable);
}
}
}
// Check local storage (offline case)
final Trackable trackable = DataStore.loadTrackable(geocode);
if (trackable != null) {
return Observable.just(trackable);
}
}
// Fall back to GC search by GUID
final Trackable trackable = TravelBugConnector.getInstance().searchTrackable(geocode, guid, id);
return trackable != null ? Observable.just(trackable) : Observable.<Trackable>empty();
}
}).subscribeOn(RxUtils.networkScheduler);
}
public void displayTrackable() {
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();
if (StringUtils.isNotBlank(trackable.getName())) {
setTitle(Html.fromHtml(trackable.getName()).toString());
} else {
setTitle(trackable.getName());
}
invalidateOptionsMenuCompatible();
reinitializeViewPager();
} catch (final Exception e) {
Log.e("TrackableActivity.loadTrackableHandler: ", e);
}
if (waitDialog != null) {
waitDialog.dismiss();
}
}
private void setupIcon(final ActionBar actionBar, final String url) {
final HtmlImage imgGetter = new HtmlImage(HtmlImage.SHARED, false, 0, false);
AppObservable.bindActivity(this, imgGetter.fetchDrawable(url)).subscribe(new Action1<BitmapDrawable>() {
@Override
public void call(final BitmapDrawable image) {
if (actionBar != null) {
final int height = actionBar.getHeight();
image.setBounds(0, 0, height, height);
actionBar.setIcon(image);
}
}
});
}
public static void startActivity(final AbstractActivity fromContext,
final String guid, final String geocode, final String name) {
final Intent trackableIntent = new Intent(fromContext, TrackableActivity.class);
trackableIntent.putExtra(Intents.EXTRA_GUID, guid);
trackableIntent.putExtra(Intents.EXTRA_GEOCODE, geocode);
trackableIntent.putExtra(Intents.EXTRA_NAME, name);
fromContext.startActivity(trackableIntent);
}
@Override
protected PageViewCreator createViewCreator(final Page page) {
switch (page) {
case DETAILS:
return new DetailsViewCreator();
case LOGS:
return new TrackableLogsViewCreator(this);
case IMAGES:
return new ImagesViewCreator();
}
throw new IllegalStateException(); // cannot happen as long as switch case is enum complete
}
private class ImagesViewCreator extends AbstractCachingPageViewCreator<View> {
@Override
public View getDispatchedView(final ViewGroup parentView) {
view = getLayoutInflater().inflate(R.layout.cachedetail_images_page, parentView, false);
return view;
}
}
private void loadTrackableImages() {
if (imagesList != null) {
return;
}
final PageViewCreator creator = getViewCreator(Page.IMAGES);
if (creator == null) {
return;
}
final View imageView = creator.getView(null);
if (imageView == null) {
return;
}
imagesList = new ImagesList(this, trackable.getGeocode());
createSubscriptions.add(imagesList.loadImages(imageView, trackable.getImages(), false));
}
@Override
protected String getTitle(final Page page) {
return res.getString(page.resId);
}
@Override
protected Pair<List<? extends Page>, Integer> getOrderedPages() {
final List<Page> pages = new ArrayList<>();
pages.add(Page.DETAILS);
if (CollectionUtils.isNotEmpty(trackable.getLogs())) {
pages.add(Page.LOGS);
}
if (CollectionUtils.isNotEmpty(trackable.getImages())) {
pages.add(Page.IMAGES);
}
return new ImmutablePair<List<? extends Page>, Integer>(pages, 0);
}
public class DetailsViewCreator extends AbstractCachingPageViewCreator<ScrollView> {
@InjectView(R.id.goal_box) protected LinearLayout goalBox;
@InjectView(R.id.goal) protected TextView goalTextView;
@InjectView(R.id.details_box) protected LinearLayout detailsBox;
@InjectView(R.id.details) protected TextView detailsTextView;
@InjectView(R.id.image_box) protected LinearLayout imageBox;
@InjectView(R.id.details_list) protected LinearLayout detailsList;
@InjectView(R.id.image) protected LinearLayout imageView;
@Override
public ScrollView getDispatchedView(final ViewGroup parentView) {
view = (ScrollView) getLayoutInflater().inflate(R.layout.trackable_details_view, parentView, false);
ButterKnife.inject(this, view);
final CacheDetailsCreator details = new CacheDetailsCreator(TrackableActivity.this, detailsList);
// action bar icon
if (StringUtils.isNotBlank(trackable.getIconUrl())) {
setupIcon(getSupportActionBar(), trackable.getIconUrl());
}
// trackable name
addContextMenu(details.add(R.string.trackable_name, StringUtils.isNotBlank(trackable.getName()) ? Html.fromHtml(trackable.getName()).toString() : res.getString(R.string.trackable_unknown)).right);
// trackable type
final 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
addContextMenu(details.add(R.string.trackable_code, trackable.getGeocode()).right);
// trackable owner
final TextView owner = details.add(R.string.trackable_owner, res.getString(R.string.trackable_unknown)).right;
if (StringUtils.isNotBlank(trackable.getOwner())) {
owner.setText(Html.fromHtml(trackable.getOwner()), TextView.BufferType.SPANNABLE);
owner.setOnClickListener(new UserActionsClickListener(trackable));
}
// trackable spotted
if (StringUtils.isNotBlank(trackable.getSpottedName()) ||
trackable.getSpottedType() == Trackable.SPOTTED_UNKNOWN ||
trackable.getSpottedType() == Trackable.SPOTTED_OWNER) {
final StringBuilder text;
boolean showTimeSpan = true;
switch (trackable.getSpottedType()) {
case Trackable.SPOTTED_CACHE:
text = new StringBuilder(res.getString(R.string.trackable_spotted_in_cache) + ' ' + Html.fromHtml(trackable.getSpottedName()).toString());
break;
case Trackable.SPOTTED_USER:
text = new StringBuilder(res.getString(R.string.trackable_spotted_at_user) + ' ' + Html.fromHtml(trackable.getSpottedName()).toString());
break;
case Trackable.SPOTTED_UNKNOWN:
text = new StringBuilder(res.getString(R.string.trackable_spotted_unknown_location));
break;
case Trackable.SPOTTED_OWNER:
text = new StringBuilder(res.getString(R.string.trackable_spotted_owner));
break;
default:
text = new StringBuilder("N/A");
showTimeSpan = false;
break;
}
// days since last spotting
if (showTimeSpan) {
for (final 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) {
text.append(" (").append(Formatter.formatDaysAgo(log.date)).append(')');
break;
}
}
}
final TextView spotted = details.add(R.string.trackable_spotted, text.toString()).right;
spotted.setClickable(true);
if (Trackable.SPOTTED_CACHE == trackable.getSpottedType()) {
spotted.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View arg0) {
if (StringUtils.isNotBlank(trackable.getSpottedGuid())) {
CacheDetailActivity.startActivityGuid(TrackableActivity.this, trackable.getSpottedGuid(), trackable.getSpottedName());
}
else {
// for geokrety we only know the cache geocode
final String cacheCode = trackable.getSpottedName();
if (ConnectorFactory.canHandle(cacheCode)) {
CacheDetailActivity.startActivity(TrackableActivity.this, cacheCode);
}
}
}
});
} else if (Trackable.SPOTTED_USER == trackable.getSpottedType()) {
spotted.setOnClickListener(new UserNameClickListener(trackable, Html.fromHtml(trackable.getSpottedName()).toString()));
} else if (Trackable.SPOTTED_OWNER == trackable.getSpottedType()) {
spotted.setOnClickListener(new UserNameClickListener(trackable, Html.fromHtml(trackable.getOwner()).toString()));
}
}
// trackable origin
if (StringUtils.isNotBlank(trackable.getOrigin())) {
final TextView origin = details.add(R.string.trackable_origin, "").right;
origin.setText(Html.fromHtml(trackable.getOrigin()), TextView.BufferType.SPANNABLE);
addContextMenu(origin);
}
// trackable released
if (trackable.getReleased() != null) {
addContextMenu(details.add(R.string.trackable_released, Formatter.formatDate(trackable.getReleased().getTime())).right);
}
// trackable distance
if (trackable.getDistance() >= 0) {
addContextMenu(details.add(R.string.trackable_distance, Units.getDistanceFromKilometers(trackable.getDistance())).right);
}
// trackable goal
if (StringUtils.isNotBlank(HtmlUtils.extractText(trackable.getGoal()))) {
goalBox.setVisibility(View.VISIBLE);
goalTextView.setVisibility(View.VISIBLE);
goalTextView.setText(Html.fromHtml(trackable.getGoal(), new HtmlImage(geocode, true, 0, false, goalTextView), null), TextView.BufferType.SPANNABLE);
goalTextView.setMovementMethod(AnchorAwareLinkMovementMethod.getInstance());
addContextMenu(goalTextView);
}
// trackable details
if (StringUtils.isNotBlank(HtmlUtils.extractText(trackable.getDetails()))) {
detailsBox.setVisibility(View.VISIBLE);
detailsTextView.setVisibility(View.VISIBLE);
detailsTextView.setText(Html.fromHtml(trackable.getDetails(), new HtmlImage(geocode, true, 0, false, detailsTextView), new UnknownTagsHandler()), TextView.BufferType.SPANNABLE);
detailsTextView.setMovementMethod(AnchorAwareLinkMovementMethod.getInstance());
addContextMenu(detailsTextView);
}
// trackable image
if (StringUtils.isNotBlank(trackable.getImage())) {
imageBox.setVisibility(View.VISIBLE);
final ImageView trackableImage = (ImageView) inflater.inflate(R.layout.trackable_image, imageView, false);
trackableImage.setImageResource(R.drawable.image_not_loaded);
trackableImage.setClickable(true);
ViewObservable.clicks(trackableImage, false).subscribe(new Action1<OnClickEvent>() {
@Override
public void call(final OnClickEvent onClickEvent) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(trackable.getImage())));
}
});
AppObservable.bindActivity(TrackableActivity.this, new HtmlImage(geocode, true, 0, false).fetchDrawable(trackable.getImage())).subscribe(new Action1<BitmapDrawable>() {
@Override
public void call(final BitmapDrawable bitmapDrawable) {
trackableImage.setImageDrawable(bitmapDrawable);
}
});
imageView.addView(trackableImage);
}
return view;
}
}
@Override
public void addContextMenu(final View view) {
view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(final View v) {
return startContextualActionBar(view);
}
});
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
startContextualActionBar(view);
}
});
}
private boolean startContextualActionBar(final View view) {
if (currentActionMode != null) {
return false;
}
currentActionMode = startSupportActionMode(new ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(final ActionMode actionMode, final Menu menu) {
return prepareClipboardActionMode(view, actionMode, menu);
}
private boolean prepareClipboardActionMode(final View view, final ActionMode actionMode, final Menu menu) {
final int viewId = view.getId();
assert view instanceof TextView;
clickedItemText = ((TextView) view).getText();
switch (viewId) {
case R.id.value: // name, TB-code, origin, released, distance
final CharSequence itemTitle = ((TextView) ((View) view.getParent()).findViewById(R.id.name)).getText();
buildDetailsContextMenu(actionMode, menu, itemTitle, true);
return true;
case R.id.goal:
buildDetailsContextMenu(actionMode, menu, res.getString(R.string.trackable_goal), false);
return true;
case R.id.details:
buildDetailsContextMenu(actionMode, menu, res.getString(R.string.trackable_details), false);
return true;
case R.id.log:
buildDetailsContextMenu(actionMode, menu, res.getString(R.string.cache_logs), false);
return true;
}
return false;
}
@Override
public void onDestroyActionMode(final ActionMode actionMode) {
currentActionMode = null;
}
@Override
public boolean onCreateActionMode(final ActionMode actionMode, final Menu menu) {
actionMode.getMenuInflater().inflate(R.menu.details_context, menu);
prepareClipboardActionMode(view, actionMode, menu);
return true;
}
@Override
public boolean onActionItemClicked(final ActionMode actionMode, final MenuItem menuItem) {
return onClipboardItemSelected(actionMode, menuItem, clickedItemText);
}
});
return false;
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
// Refresh the logs view after coming back from logging a trackable
if (requestCode == LogTrackableActivity.LOG_TRACKABLE && resultCode == RESULT_OK) {
refreshTrackable(StringUtils.defaultIfBlank(trackable.getName(), trackable.getGeocode()));
}
}
@Override
protected void onDestroy() {
createSubscriptions.unsubscribe();
super.onDestroy();
}
public Trackable getTrackable() {
return trackable;
}
}
|