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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
|
package cgeo.geocaching;
import cgeo.geocaching.connector.ILoggingManager;
import cgeo.geocaching.connector.ImageResult;
import cgeo.geocaching.connector.LogResult;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.enumerations.LogTypeTrackable;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.gcvote.GCVote;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.twitter.Twitter;
import cgeo.geocaching.ui.Formatter;
import cgeo.geocaching.ui.dialog.DateDialog;
import cgeo.geocaching.ui.dialog.Dialogs;
import cgeo.geocaching.utils.AsyncTaskWithProgress;
import cgeo.geocaching.utils.DateUtils;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.LogTemplateProvider;
import cgeo.geocaching.utils.LogTemplateProvider.LogContext;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class LogCacheActivity extends AbstractLoggingActivity implements DateDialog.DateDialogParent {
static final String EXTRAS_GEOCODE = "geocode";
static final String EXTRAS_ID = "id";
private static final int SUBMENU_VOTE = 3;
private static final String SAVED_STATE_RATING = "cgeo.geocaching.saved_state_rating";
private static final String SAVED_STATE_TYPE = "cgeo.geocaching.saved_state_type";
private static final String SAVED_STATE_DATE = "cgeo.geocaching.saved_state_date";
private static final String SAVED_STATE_IMAGE_CAPTION = "cgeo.geocaching.saved_state_image_caption";
private static final String SAVED_STATE_IMAGE_DESCRIPTION = "cgeo.geocaching.saved_state_image_description";
private static final String SAVED_STATE_IMAGE_URI = "cgeo.geocaching.saved_state_image_uri";
private static final int SELECT_IMAGE = 101;
private LayoutInflater inflater = null;
private Geocache cache = null;
private String cacheid = null;
private String geocode = null;
private String text = null;
private List<LogType> possibleLogTypes = new ArrayList<LogType>();
private List<TrackableLog> trackables = null;
private Button postButton = null;
private CheckBox tweetCheck = null;
private LinearLayout tweetBox = null;
private LinearLayout logPasswordBox = null;
private boolean tbChanged = false;
private SparseArray<TrackableLog> actionButtons;
private ILoggingManager loggingManager;
// Data to be saved while reconfiguring
private float rating;
private LogType typeSelected;
private Calendar date;
private String imageCaption;
private String imageDescription;
private Uri imageUri;
public void onLoadFinished() {
if (loggingManager.hasLoaderError()) {
showErrorLoadingData();
return;
}
trackables = loggingManager.getTrackables();
possibleLogTypes = loggingManager.getPossibleLogTypes();
if (possibleLogTypes.isEmpty()) {
showErrorLoadingData();
return;
}
if (!possibleLogTypes.contains(typeSelected)) {
typeSelected = possibleLogTypes.get(0);
setType(typeSelected);
showToast(res.getString(R.string.info_log_type_changed));
}
enablePostButton(true);
initializeTrackablesAction();
updateTrackablesList();
showProgress(false);
}
private void showErrorLoadingData() {
showToast(res.getString(R.string.err_log_load_data));
showProgress(false);
}
private void initializeTrackablesAction() {
if (Settings.isTrackableAutoVisit()) {
for (TrackableLog trackable : trackables) {
trackable.action = LogTypeTrackable.VISITED;
tbChanged = true;
}
}
}
private void updateTrackablesList() {
if (CollectionUtils.isEmpty(trackables)) {
return;
}
if (inflater == null) {
inflater = getLayoutInflater();
}
actionButtons = new SparseArray<TrackableLog>();
final LinearLayout inventoryView = (LinearLayout) findViewById(R.id.inventory);
inventoryView.removeAllViews();
for (TrackableLog tb : trackables) {
LinearLayout inventoryItem = (LinearLayout) inflater.inflate(R.layout.logcache_trackable_item, null);
((TextView) inventoryItem.findViewById(R.id.trackcode)).setText(tb.trackCode);
((TextView) inventoryItem.findViewById(R.id.name)).setText(tb.name);
final TextView actionButton = (TextView) inventoryItem.findViewById(R.id.action);
actionButton.setId(tb.id);
actionButtons.put(actionButton.getId(), tb);
actionButton.setText(tb.action.getLabel() + " ▼");
actionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectTrackableAction(view);
}
});
final String tbCode = tb.trackCode;
inventoryItem.setClickable(true);
inventoryItem.findViewById(R.id.info).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Intent trackablesIntent = new Intent(LogCacheActivity.this, TrackableActivity.class);
trackablesIntent.putExtra(Intents.EXTRA_GEOCODE, tbCode);
startActivity(trackablesIntent);
}
});
inventoryView.addView(inventoryItem);
}
if (inventoryView.getChildCount() > 0) {
findViewById(R.id.inventory_box).setVisibility(View.VISIBLE);
}
if (inventoryView.getChildCount() > 1) {
final LinearLayout inventoryChangeAllView = (LinearLayout) findViewById(R.id.inventory_changeall);
final Button changeButton = (Button) inventoryChangeAllView.findViewById(R.id.changebutton);
changeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectAllTrackablesAction();
}
});
inventoryChangeAllView.setVisibility(View.VISIBLE);
}
}
private void enablePostButton(boolean enabled) {
postButton.setEnabled(enabled);
if (enabled) {
postButton.setOnClickListener(new PostListener());
}
else {
postButton.setOnTouchListener(null);
postButton.setOnClickListener(null);
}
updatePostButtonText();
}
private void updatePostButtonText() {
postButton.setText(getPostButtonText());
}
private String getPostButtonText() {
if (!postButton.isEnabled()) {
return res.getString(R.string.log_post_not_possible);
}
if (!GCVote.isVotingPossible(cache)) {
return res.getString(R.string.log_post);
}
if (GCVote.isValidRating(rating)) {
return res.getString(R.string.log_post_rate) + " " + GCVote.getRatingText(rating) + "*";
}
return res.getString(R.string.log_post_no_rate);
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState, R.layout.logcache_activity);
// Get parameters from intent and basic cache information from database
final Bundle extras = getIntent().getExtras();
if (extras != null) {
cacheid = extras.getString(EXTRAS_ID);
geocode = extras.getString(EXTRAS_GEOCODE);
}
if ((StringUtils.isBlank(cacheid)) && StringUtils.isNotBlank(geocode)) {
cacheid = DataStore.getCacheidForGeocode(geocode);
}
if (StringUtils.isBlank(geocode) && StringUtils.isNotBlank(cacheid)) {
geocode = DataStore.getGeocodeForGuid(cacheid);
}
cache = DataStore.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB);
possibleLogTypes = cache.getPossibleLogTypes();
if (StringUtils.isNotBlank(cache.getName())) {
setTitle(res.getString(R.string.log_new_log) + ": " + cache.getName());
} else {
setTitle(res.getString(R.string.log_new_log) + ": " + cache.getGeocode());
}
// Get ids for later use
postButton = (Button) findViewById(R.id.post);
tweetBox = (LinearLayout) findViewById(R.id.tweet_box);
tweetCheck = (CheckBox) findViewById(R.id.tweet);
logPasswordBox = (LinearLayout) findViewById(R.id.log_password_box);
// initialize with default values
setDefaultValues();
// Restore previous state
if (savedInstanceState != null) {
rating = savedInstanceState.getFloat(SAVED_STATE_RATING);
typeSelected = LogType.getById(savedInstanceState.getInt(SAVED_STATE_TYPE));
date.setTimeInMillis(savedInstanceState.getLong(SAVED_STATE_DATE));
imageCaption = savedInstanceState.getString(SAVED_STATE_IMAGE_CAPTION);
imageDescription = savedInstanceState.getString(SAVED_STATE_IMAGE_DESCRIPTION);
imageUri = Uri.parse(savedInstanceState.getString(SAVED_STATE_IMAGE_URI));
} else {
// If log had been previously saved, load it now, otherwise initialize signature as needed
final LogEntry log = DataStore.loadLogOffline(geocode);
if (log != null) {
typeSelected = log.type;
date.setTime(new Date(log.date));
text = log.log;
} else if (StringUtils.isNotBlank(Settings.getSignature())
&& Settings.isAutoInsertSignature()
&& StringUtils.isBlank(currentLogText())) {
insertIntoLog(LogTemplateProvider.applyTemplates(Settings.getSignature(), new LogContext(cache, null)), false);
}
}
updatePostButtonText();
updateImageButton();
enablePostButton(false);
final Button typeButton = (Button) findViewById(R.id.type);
typeButton.setText(typeSelected.getL10n());
typeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectLogType();
}
});
final Button dateButton = (Button) findViewById(R.id.date);
setDate(date);
dateButton.setOnClickListener(new DateListener());
final EditText logView = (EditText) findViewById(R.id.log);
if (StringUtils.isBlank(currentLogText()) && StringUtils.isNotBlank(text)) {
logView.setText(text);
Dialogs.moveCursorToEnd(logView);
}
tweetCheck.setChecked(true);
updateTweetBox(typeSelected);
updateLogPasswordBox(typeSelected);
final Button imageButton = (Button) findViewById(R.id.image_btn);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectImage();
}
});
final Button saveButton = (Button) findViewById(R.id.save);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveLog(true);
}
});
final Button clearButton = (Button) findViewById(R.id.clear);
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearLog();
}
});
loggingManager = cache.getLoggingManager(this);
loggingManager.init();
}
private void setDefaultValues() {
date = Calendar.getInstance();
rating = GCVote.NO_RATING;
if (cache.isEventCache()) {
final Date eventDate = cache.getHiddenDate();
boolean expired = DateUtils.isPastEvent(cache);
if (cache.hasOwnLog(LogType.WILL_ATTEND) || expired) {
typeSelected = cache.hasOwnLog(LogType.ATTENDED) ? LogType.NOTE : LogType.ATTENDED;
} else {
typeSelected = LogType.WILL_ATTEND;
}
// it this is an attended event log, use the event date by default instead of the current date
if (expired && typeSelected == LogType.ATTENDED) {
date.setTime(eventDate);
}
} else {
if (cache.isFound()) {
typeSelected = LogType.NOTE;
} else {
typeSelected = cache.getType() == CacheType.WEBCAM ? LogType.WEBCAM_PHOTO_TAKEN : LogType.FOUND_IT;
}
}
text = null;
imageCaption = StringUtils.EMPTY;
imageDescription = StringUtils.EMPTY;
imageUri = Uri.EMPTY;
}
private void clearLog() {
cache.clearOfflineLog();
setDefaultValues();
setType(typeSelected);
setDate(date);
final EditText logView = (EditText) findViewById(R.id.log);
logView.setText(StringUtils.EMPTY);
final EditText logPasswordView = (EditText) findViewById(R.id.log_password);
logPasswordView.setText(StringUtils.EMPTY);
updateImageButton();
showToast(res.getString(R.string.info_log_cleared));
}
@Override
public void finish() {
saveLog(false);
super.finish();
}
@Override
public void onStop() {
saveLog(false);
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
super.onCreateOptionsMenu(menu);
final SubMenu menuStars = menu.addSubMenu(0, SUBMENU_VOTE, 0, res.getString(R.string.log_rating)).setIcon(R.drawable.ic_menu_sort_by_size);
menuStars.add(0, 10, 0, res.getString(R.string.log_no_rating));
menuStars.add(0, 19, 0, res.getString(R.string.log_stars_5) + " (" + res.getString(R.string.log_stars_5_description) + ")");
menuStars.add(0, 18, 0, res.getString(R.string.log_stars_45) + " (" + res.getString(R.string.log_stars_45_description) + ")");
menuStars.add(0, 17, 0, res.getString(R.string.log_stars_4) + " (" + res.getString(R.string.log_stars_4_description) + ")");
menuStars.add(0, 16, 0, res.getString(R.string.log_stars_35) + " (" + res.getString(R.string.log_stars_35_description) + ")");
menuStars.add(0, 15, 0, res.getString(R.string.log_stars_3) + " (" + res.getString(R.string.log_stars_3_description) + ")");
menuStars.add(0, 14, 0, res.getString(R.string.log_stars_25) + " (" + res.getString(R.string.log_stars_25_description) + ")");
menuStars.add(0, 13, 0, res.getString(R.string.log_stars_2) + " (" + res.getString(R.string.log_stars_2_description) + ")");
menuStars.add(0, 12, 0, res.getString(R.string.log_stars_15) + " (" + res.getString(R.string.log_stars_15_description) + ")");
menuStars.add(0, 11, 0, res.getString(R.string.log_stars_1) + " (" + res.getString(R.string.log_stars_1_description) + ")");
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(SUBMENU_VOTE).setVisible(GCVote.isVotingPossible(cache));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (super.onOptionsItemSelected(item)) {
return true;
}
final int id = item.getItemId();
if (id >= 10 && id <= 19) {
rating = (id - 9) / 2.0f;
if (!GCVote.isValidRating(rating)) {
rating = GCVote.NO_RATING;
}
updatePostButtonText();
return true;
}
return false;
}
@Override
protected void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
outState.putDouble(SAVED_STATE_RATING, rating);
outState.putInt(SAVED_STATE_TYPE, typeSelected.id);
outState.putLong(SAVED_STATE_DATE, date.getTimeInMillis());
outState.putString(SAVED_STATE_IMAGE_URI, imageUri.getPath());
outState.putString(SAVED_STATE_IMAGE_CAPTION, imageCaption);
outState.putString(SAVED_STATE_IMAGE_DESCRIPTION, imageDescription);
}
@Override
public void setDate(Calendar dateIn) {
date = dateIn;
final Button dateButton = (Button) findViewById(R.id.date);
dateButton.setText(Formatter.formatShortDateVerbally(date.getTime().getTime()));
}
public void setType(LogType type) {
final Button typeButton = (Button) findViewById(R.id.type);
typeSelected = type;
typeButton.setText(typeSelected.getL10n());
if (LogType.FOUND_IT == type && !tbChanged) {
// TODO: change action
} else if (LogType.FOUND_IT != type && !tbChanged) {
// TODO: change action
}
updateTweetBox(type);
updatePostButtonText();
}
private void updateTweetBox(LogType type) {
if (type == LogType.FOUND_IT && Settings.isUseTwitter() && Settings.isTwitterLoginValid()) {
tweetBox.setVisibility(View.VISIBLE);
} else {
tweetBox.setVisibility(View.GONE);
}
}
private void updateLogPasswordBox(LogType type) {
if (type == LogType.FOUND_IT && cache.isLogPasswordRequired()) {
logPasswordBox.setVisibility(View.VISIBLE);
} else {
logPasswordBox.setVisibility(View.GONE);
}
}
private class DateListener implements View.OnClickListener {
@Override
public void onClick(View arg0) {
final Dialog dateDialog = new DateDialog(LogCacheActivity.this, LogCacheActivity.this, date);
dateDialog.setCancelable(true);
dateDialog.show();
}
}
private class PostListener implements View.OnClickListener {
@Override
public void onClick(View arg0) {
final String message = res.getString(StringUtils.isBlank(imageUri.getPath()) ?
R.string.log_saving :
R.string.log_saving_and_uploading);
new Poster(LogCacheActivity.this, message).execute(currentLogText(), currentLogPassword());
}
}
private class Poster extends AsyncTaskWithProgress<String, StatusCode> {
public Poster(final Activity activity, final String progressMessage) {
super(activity, null, progressMessage, true);
}
@Override
protected StatusCode doInBackgroundInternal(final String[] logTexts) {
final String log = logTexts[0];
final String logPwd = logTexts.length > 1 ? logTexts[1] : null;
try {
final LogResult logResult = loggingManager.postLog(cache, typeSelected, date, log, logPwd, trackables);
if (logResult.getPostLogResult() == StatusCode.NO_ERROR) {
final LogEntry logNow = new LogEntry(date.getTimeInMillis(), typeSelected, log);
cache.getLogs().add(0, logNow);
if (typeSelected == LogType.FOUND_IT || typeSelected == LogType.ATTENDED) {
cache.setFound(true);
cache.setVisitedDate(new Date().getTime());
}
DataStore.saveChangedCache(cache);
cache.clearOfflineLog();
if (typeSelected == LogType.FOUND_IT) {
if (tweetCheck.isChecked() && tweetBox.getVisibility() == View.VISIBLE) {
Twitter.postTweetCache(geocode, logNow);
}
}
if (GCVote.isValidRating(rating)) {
GCVote.setRating(cache, rating);
}
if (StringUtils.isNotBlank(imageUri.getPath())) {
ImageResult imageResult = loggingManager.postLogImage(logResult.getLogId(), imageCaption, imageDescription, imageUri);
final String uploadedImageUrl = imageResult.getImageUri();
if (StringUtils.isNotEmpty(uploadedImageUrl)) {
logNow.addLogImage(new Image(uploadedImageUrl, imageCaption, imageDescription));
DataStore.saveChangedCache(cache);
}
return imageResult.getPostResult();
}
}
return logResult.getPostLogResult();
} catch (RuntimeException e) {
Log.e("VisitCacheActivity.Poster.doInBackgroundInternal", e);
}
return StatusCode.LOG_POST_ERROR;
}
@Override
protected void onPostExecuteInternal(final StatusCode status) {
if (status == StatusCode.NO_ERROR) {
showToast(res.getString(R.string.info_log_posted));
// No need to save the log when quitting if it has been posted.
text = currentLogText();
finish();
} else if (status == StatusCode.LOG_SAVED) {
showToast(res.getString(R.string.info_log_saved));
finish();
} else {
showToast(status.getErrorString(res));
}
}
}
private void saveLog(final boolean force) {
final String log = currentLogText();
// Do not erase the saved log if the user has removed all the characters
// without using "Clear". This may be a manipulation mistake, and erasing
// again will be easy using "Clear" while retyping the text may not be.
if (force || (StringUtils.isNotEmpty(log) && !StringUtils.equals(log, text))) {
cache.logOffline(this, log, date, typeSelected);
}
text = log;
}
private String currentLogText() {
return ((EditText) findViewById(R.id.log)).getText().toString();
}
private String currentLogPassword() {
return ((EditText) findViewById(R.id.log_password)).getText().toString();
}
@Override
protected LogContext getLogContext() {
return new LogContext(cache, null);
}
private void selectAllTrackablesAction() {
Builder alert = new AlertDialog.Builder(this);
alert.setTitle(res.getString(R.string.log_tb_changeall));
String[] tbLogTypes = getTBLogTypes();
alert.setItems(tbLogTypes, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
final LogTypeTrackable logType = LogTypeTrackable.values()[position];
for (TrackableLog tb : trackables) {
tb.action = logType;
}
tbChanged = true;
updateTrackablesList();
dialog.dismiss();
}
});
alert.create().show();
}
private static String[] getTBLogTypes() {
final LogTypeTrackable[] logTypeValues = LogTypeTrackable.values();
String[] logTypes = new String[logTypeValues.length];
for (int i = 0; i < logTypes.length; i++) {
logTypes[i] = logTypeValues[i].getLabel();
}
return logTypes;
}
private void selectLogType() {
// use a local copy of the possible types, as that one might be modified in the background by the loader
final ArrayList<LogType> possible = new ArrayList<LogType>(possibleLogTypes);
Builder alert = new AlertDialog.Builder(this);
String[] choices = new String[possible.size()];
for (int i = 0; i < choices.length; i++) {
choices[i] = possible.get(i).getL10n();
}
alert.setSingleChoiceItems(choices, possible.indexOf(typeSelected), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
setType(possible.get(position));
dialog.dismiss();
}
});
alert.create().show();
}
private void selectTrackableAction(View view) {
final int realViewId = view.getId();
Builder alert = new AlertDialog.Builder(this);
final TrackableLog trackableLog = actionButtons.get(realViewId);
alert.setTitle(trackableLog.name);
String[] tbLogTypes = getTBLogTypes();
alert.setItems(tbLogTypes, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
final LogTypeTrackable logType = LogTypeTrackable.values()[position];
tbChanged = true;
trackableLog.action = logType;
Log.i("Trackable " + trackableLog.trackCode + " (" + trackableLog.name + ") has new action: #" + logType);
updateTrackablesList();
dialog.dismiss();
}
});
alert.create().show();
}
private void selectImage() {
Intent selectImageIntent = new Intent(this, ImageSelectActivity.class);
selectImageIntent.putExtra(ImageSelectActivity.EXTRAS_CAPTION, imageCaption);
selectImageIntent.putExtra(ImageSelectActivity.EXTRAS_DESCRIPTION, imageDescription);
selectImageIntent.putExtra(ImageSelectActivity.EXTRAS_URI_AS_STRING, imageUri.toString());
startActivityForResult(selectImageIntent, SELECT_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_IMAGE) {
if (resultCode == RESULT_OK) {
imageCaption = data.getStringExtra(ImageSelectActivity.EXTRAS_CAPTION);
imageDescription = data.getStringExtra(ImageSelectActivity.EXTRAS_DESCRIPTION);
imageUri = Uri.parse(data.getStringExtra(ImageSelectActivity.EXTRAS_URI_AS_STRING));
} else if (resultCode != RESULT_CANCELED) {
// Image capture failed, advise user
showToast(getResources().getString(R.string.err_select_logimage_failed));
}
updateImageButton();
}
}
private void updateImageButton() {
final Button imageButton = (Button) findViewById(R.id.image_btn);
if (cache.supportsLogImages()) {
imageButton.setVisibility(View.VISIBLE);
imageButton.setText(StringUtils.isNotBlank(imageUri.getPath()) ?
res.getString(R.string.log_image_edit) : res.getString(R.string.log_image_attach));
} else {
imageButton.setVisibility(View.GONE);
}
}
}
|