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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
|
package cgeo.geocaching;
import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.apps.cache.navi.NavigationAppFactory;
import cgeo.geocaching.apps.cache.navi.NavigationAppFactory.NavigationAppsEnum;
import cgeo.geocaching.compatibility.Compatibility;
import cgeo.geocaching.connector.gc.Login;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.files.SimpleDirChooser;
import cgeo.geocaching.maps.MapProviderFactory;
import cgeo.geocaching.maps.interfaces.MapSource;
import cgeo.geocaching.network.Cookies;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.twitter.TwitterAuthorizationActivity;
import cgeo.geocaching.ui.Formatter;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.LogTemplateProvider;
import cgeo.geocaching.utils.LogTemplateProvider.LogTemplate;
import ch.boye.httpclientandroidlib.HttpResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class SettingsActivity extends AbstractActivity {
private final static int SELECT_MAPFILE_REQUEST = 1;
private final static int SELECT_GPXDIR_REQUEST = 2;
private ProgressDialog loginDialog = null;
private ProgressDialog webDialog = null;
private boolean enableTemplatesMenu = false;
private Handler logInHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
if (loginDialog != null && loginDialog.isShowing()) {
loginDialog.dismiss();
}
if (msg.obj == null || (msg.obj instanceof Drawable)) {
helpDialog(res.getString(R.string.init_login_popup), res.getString(R.string.init_login_popup_ok),
(Drawable) msg.obj);
} else {
helpDialog(res.getString(R.string.init_login_popup),
res.getString(R.string.init_login_popup_failed_reason) + " " +
((StatusCode) msg.obj).getErrorString(res) + ".");
}
} catch (Exception e) {
showToast(res.getString(R.string.err_login_failed));
Log.e("SettingsActivity.logInHandler: " + e.toString());
}
if (loginDialog != null && loginDialog.isShowing()) {
loginDialog.dismiss();
}
init();
}
};
private Handler webAuthHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
try {
if (webDialog != null && webDialog.isShowing()) {
webDialog.dismiss();
}
if (msg.what > 0) {
helpDialog(res.getString(R.string.init_sendToCgeo), res.getString(R.string.init_sendToCgeo_register_ok).replace("####", String.valueOf(msg.what)));
} else {
helpDialog(res.getString(R.string.init_sendToCgeo), res.getString(R.string.init_sendToCgeo_register_fail));
}
} catch (Exception e) {
showToast(res.getString(R.string.init_sendToCgeo_register_fail));
Log.e("SettingsActivity.webHandler: " + e.toString());
}
if (webDialog != null && webDialog.isShowing()) {
webDialog.dismiss();
}
init();
}
};
public SettingsActivity() {
super("c:geo-configuration");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// init
setTheme();
setContentView(R.layout.init);
setTitle(res.getString(R.string.settings));
init();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
init();
}
@Override
public void onPause() {
saveValues();
super.onPause();
}
@Override
public void onStop() {
saveValues();
Compatibility.dataChanged(getPackageName());
super.onStop();
}
@Override
public void onDestroy() {
saveValues();
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, res.getString(R.string.init_clear)).setIcon(R.drawable.ic_menu_delete);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 0) {
boolean status;
((EditText) findViewById(R.id.username)).setText("");
((EditText) findViewById(R.id.password)).setText("");
((EditText) findViewById(R.id.passvote)).setText("");
status = saveValues();
if (status) {
showToast(res.getString(R.string.init_cleared));
} else {
showToast(res.getString(R.string.err_init_cleared));
}
finish();
}
return false;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (enableTemplatesMenu) {
menu.setHeaderTitle(R.string.init_signature_template_button);
for (LogTemplate template : LogTemplateProvider.getTemplates()) {
menu.add(0, template.getItemId(), 0, template.getResourceId());
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
LogTemplate template = LogTemplateProvider.getTemplate(item.getItemId());
if (template != null) {
return insertSignatureTemplate(template);
}
return super.onContextItemSelected(item);
}
private boolean insertSignatureTemplate(final LogTemplate template) {
EditText sig = (EditText) findViewById(R.id.signature);
String insertText = "[" + template.getTemplateString() + "]";
insertAtPosition(sig, insertText, true);
return true;
}
public void init() {
// geocaching.com settings
final ImmutablePair<String, String> login = Settings.getLogin();
if (login != null) {
((EditText) findViewById(R.id.username)).setText(login.left);
((EditText) findViewById(R.id.password)).setText(login.right);
}
Button logMeIn = (Button) findViewById(R.id.log_me_in);
logMeIn.setOnClickListener(new logIn());
TextView legalNote = (TextView) findViewById(R.id.legal_note);
legalNote.setClickable(true);
legalNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.geocaching.com/about/termsofuse.aspx")));
}
});
// gcvote settings
final ImmutablePair<String, String> gcvoteLogin = Settings.getGCvoteLogin();
if (null != gcvoteLogin && null != gcvoteLogin.right) {
((EditText) findViewById(R.id.passvote)).setText(gcvoteLogin.right);
}
// go4cache settings
TextView go4cache = (TextView) findViewById(R.id.about_go4cache);
go4cache.setClickable(true);
go4cache.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://go4cache.com/")));
}
});
final CheckBox publicButton = (CheckBox) findViewById(R.id.publicloc);
publicButton.setChecked(Settings.isPublicLoc());
publicButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setPublicLoc(publicButton.isChecked());
}
});
// Twitter settings
Button authorizeTwitter = (Button) findViewById(R.id.authorize_twitter);
authorizeTwitter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent authIntent = new Intent(SettingsActivity.this, TwitterAuthorizationActivity.class);
startActivity(authIntent);
}
});
final CheckBox twitterButton = (CheckBox) findViewById(R.id.twitter_option);
twitterButton.setChecked(Settings.isUseTwitter() && Settings.isTwitterLoginValid());
twitterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setUseTwitter(twitterButton.isChecked());
if (Settings.isUseTwitter() && !Settings.isTwitterLoginValid()) {
Intent authIntent = new Intent(SettingsActivity.this, TwitterAuthorizationActivity.class);
startActivity(authIntent);
}
twitterButton.setChecked(Settings.isUseTwitter());
}
});
// Signature settings
EditText sigEdit = (EditText) findViewById(R.id.signature);
if (sigEdit.getText().length() == 0) {
sigEdit.setText(Settings.getSignature());
}
Button sigBtn = (Button) findViewById(R.id.signature_help);
sigBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
helpDialog(res.getString(R.string.init_signature_help_title), res.getString(R.string.init_signature_help_text));
}
});
Button templates = (Button) findViewById(R.id.signature_template);
registerForContextMenu(templates);
templates.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enableTemplatesMenu = true;
openContextMenu(v);
enableTemplatesMenu = false;
}
});
final CheckBox autoinsertButton = (CheckBox) findViewById(R.id.sigautoinsert);
autoinsertButton.setChecked(Settings.isAutoInsertSignature());
autoinsertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setAutoInsertSignature(autoinsertButton.isChecked());
}
});
// Cache details
final CheckBox autoloadButton = (CheckBox) findViewById(R.id.autoload);
autoloadButton.setChecked(Settings.isAutoLoadDescription());
autoloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setAutoLoadDesc(autoloadButton.isChecked());
}
});
final CheckBox ratingWantedButton = (CheckBox) findViewById(R.id.ratingwanted);
ratingWantedButton.setChecked(Settings.isRatingWanted());
ratingWantedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setRatingWanted(ratingWantedButton.isChecked());
}
});
final CheckBox elevationWantedButton = (CheckBox) findViewById(R.id.elevationwanted);
elevationWantedButton.setChecked(Settings.isElevationWanted());
elevationWantedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setElevationWanted(elevationWantedButton.isChecked());
}
});
final CheckBox friendLogsWantedButton = (CheckBox) findViewById(R.id.friendlogswanted);
friendLogsWantedButton.setChecked(Settings.isFriendLogsWanted());
friendLogsWantedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setFriendLogsWanted(friendLogsWantedButton.isChecked());
}
});
final CheckBox openLastDetailsPageButton = (CheckBox) findViewById(R.id.openlastdetailspage);
openLastDetailsPageButton.setChecked(Settings.isOpenLastDetailsPage());
openLastDetailsPageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setOpenLastDetailsPage(openLastDetailsPageButton.isChecked());
}
});
// Other settings
final CheckBox skinButton = (CheckBox) findViewById(R.id.skin);
skinButton.setChecked(Settings.isLightSkin());
skinButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setLightSkin(skinButton.isChecked());
}
});
final CheckBox addressButton = (CheckBox) findViewById(R.id.address);
addressButton.setChecked(Settings.isShowAddress());
addressButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setShowAddress(addressButton.isChecked());
}
});
final CheckBox captchaButton = (CheckBox) findViewById(R.id.captcha);
captchaButton.setEnabled(!Settings.isPremiumMember());
captchaButton.setChecked(Settings.isShowCaptcha());
captchaButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setShowCaptcha(captchaButton.isChecked());
}
});
final CheckBox dirImgButton = (CheckBox) findViewById(R.id.loaddirectionimg);
dirImgButton.setEnabled(!Settings.isPremiumMember());
dirImgButton.setChecked(Settings.getLoadDirImg());
dirImgButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setLoadDirImg(!Settings.getLoadDirImg());
dirImgButton.setChecked(Settings.getLoadDirImg());
}
});
final CheckBox useEnglishButton = (CheckBox) findViewById(R.id.useenglish);
useEnglishButton.setChecked(Settings.isUseEnglish());
useEnglishButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setUseEnglish(useEnglishButton.isChecked());
}
});
final CheckBox excludeButton = (CheckBox) findViewById(R.id.exclude);
excludeButton.setChecked(Settings.isExcludeMyCaches());
excludeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setExcludeMine(excludeButton.isChecked());
}
});
final CheckBox disabledButton = (CheckBox) findViewById(R.id.disabled);
disabledButton.setChecked(Settings.isExcludeDisabledCaches());
disabledButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setExcludeDisabledCaches(disabledButton.isChecked());
}
});
TextView showWaypointsThreshold = (TextView) findViewById(R.id.showwaypointsthreshold);
showWaypointsThreshold.setText(String.valueOf(Settings.getWayPointsThreshold()));
final CheckBox autovisitButton = (CheckBox) findViewById(R.id.trackautovisit);
autovisitButton.setChecked(Settings.isTrackableAutoVisit());
autovisitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setTrackableAutoVisit(autovisitButton.isChecked());
}
});
final CheckBox offlineButton = (CheckBox) findViewById(R.id.offline);
offlineButton.setChecked(Settings.isStoreOfflineMaps());
offlineButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setStoreOfflineMaps(offlineButton.isChecked());
}
});
final CheckBox offlineWpButton = (CheckBox) findViewById(R.id.offline_wp);
offlineWpButton.setChecked(Settings.isStoreOfflineWpMaps());
offlineWpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setStoreOfflineWpMaps(offlineWpButton.isChecked());
}
});
final CheckBox saveLogImgButton = (CheckBox) findViewById(R.id.save_log_img);
saveLogImgButton.setChecked(Settings.isStoreLogImages());
saveLogImgButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setStoreLogImages(saveLogImgButton.isChecked());
}
});
final CheckBox livelistButton = (CheckBox) findViewById(R.id.livelist);
livelistButton.setChecked(Settings.isLiveList());
livelistButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setLiveList(livelistButton.isChecked());
}
});
final CheckBox unitsButton = (CheckBox) findViewById(R.id.units);
unitsButton.setChecked(!Settings.isUseMetricUnits());
unitsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setUseMetricUnits(!unitsButton.isChecked());
}
});
final CheckBox gnavButton = (CheckBox) findViewById(R.id.gnav);
gnavButton.setChecked(Settings.isUseGoogleNavigation());
gnavButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setUseGoogleNavigation(gnavButton.isChecked());
}
});
final CheckBox logOffline = (CheckBox) findViewById(R.id.log_offline);
logOffline.setChecked(Settings.getLogOffline());
logOffline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setLogOffline(!Settings.getLogOffline());
logOffline.setChecked(Settings.getLogOffline());
}
});
// Altitude settings
EditText altitudeEdit = (EditText) findViewById(R.id.altitude);
altitudeEdit.setText(String.valueOf(Settings.getAltCorrection()));
//Send2cgeo settings
String webDeviceName = Settings.getWebDeviceName();
if (StringUtils.isNotBlank(webDeviceName)) {
((EditText) findViewById(R.id.webDeviceName)).setText(webDeviceName);
} else {
String s = android.os.Build.MODEL;
((EditText) findViewById(R.id.webDeviceName)).setText(s);
}
Button webAuth = (Button) findViewById(R.id.sendToCgeo_register);
webAuth.setOnClickListener(new webAuth());
// Map source settings
Collection<String> mapSourceNames = new ArrayList<String>();
for (MapSource mapSource : MapProviderFactory.getMapSources().values()) {
mapSourceNames.add(mapSource.getName());
}
Spinner mapSourceSelector = (Spinner) findViewById(R.id.mapsource);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, mapSourceNames.toArray(new String[mapSourceNames.size()]));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mapSourceSelector.setAdapter(adapter);
int mapsource = Settings.getMapSource();
mapSourceSelector.setSelection(MapProviderFactory.getSourceOrdinalFromId(mapsource));
mapSourceSelector.setOnItemSelectedListener(new cgeoChangeMapSource());
initMapfileEdittext(false);
Button selectMapfile = (Button) findViewById(R.id.select_mapfile);
selectMapfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent selectIntent = new Intent(SettingsActivity.this, cgSelectMapfile.class);
startActivityForResult(selectIntent, SELECT_MAPFILE_REQUEST);
}
});
// GPX Export directory
final EditText gpxExportDir = (EditText) findViewById(R.id.gpx_exportdir);
gpxExportDir.setText(Settings.getGpxExportDir());
Button selectGpxExportDir = (Button) findViewById(R.id.select_gpx_exportdir);
selectGpxExportDir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent dirChooser = new Intent(SettingsActivity.this, SimpleDirChooser.class);
dirChooser.putExtra(SimpleDirChooser.START_DIR, Settings.getGpxExportDir());
startActivityForResult(dirChooser, SELECT_GPXDIR_REQUEST);
}
});
// Display trail on map
final CheckBox trailButton = (CheckBox) findViewById(R.id.trail);
trailButton.setChecked(Settings.isMapTrail());
trailButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setMapTrail(trailButton.isChecked());
}
});
// Default navigation tool settings
Spinner defaultNavigationToolSelector = (Spinner) findViewById(R.id.default_navigation_tool);
final List<NavigationAppsEnum> apps = NavigationAppFactory.getInstalledDefaultNavigationApps();
ArrayAdapter<NavigationAppsEnum> naviAdapter = new ArrayAdapter<NavigationAppsEnum>(this, android.R.layout.simple_spinner_item, apps) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
textView.setText(getItem(position).app.getName());
return textView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getDropDownView(position, convertView, parent);
textView.setText(getItem(position).app.getName());
return textView;
}
};
naviAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
defaultNavigationToolSelector.setAdapter(naviAdapter);
int defaultNavigationTool = Settings.getDefaultNavigationTool();
int ordinal = 0;
for (int i = 0; i < apps.size(); i++) {
if (apps.get(i).id == defaultNavigationTool) {
ordinal = i;
break;
}
}
defaultNavigationToolSelector.setSelection(ordinal);
defaultNavigationToolSelector.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
NavigationAppsEnum item = (NavigationAppsEnum) parent.getItemAtPosition(position);
if (item != null) {
Settings.setDefaultNavigationTool(item.id);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// noop
}
});
// 2nd Default navigation tool settings
Spinner defaultNavigationTool2Selector = (Spinner) findViewById(R.id.default_navigation_tool_2);
// final List<NavigationAppsEnum> apps = NavigationAppFactory.getInstalledNavigationApps(this);
ArrayAdapter<NavigationAppsEnum> navi2Adapter = new ArrayAdapter<NavigationAppsEnum>(this, android.R.layout.simple_spinner_item, apps) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
textView.setText(getItem(position).app.getName());
return textView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getDropDownView(position, convertView, parent);
textView.setText(getItem(position).app.getName());
return textView;
}
};
navi2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
defaultNavigationTool2Selector.setAdapter(navi2Adapter);
int defaultNavigationTool2 = Settings.getDefaultNavigationTool2();
int ordinal2 = 0;
for (int i = 0; i < apps.size(); i++) {
if (apps.get(i).id == defaultNavigationTool2) {
ordinal2 = i;
break;
}
}
defaultNavigationTool2Selector.setSelection(ordinal2);
defaultNavigationTool2Selector.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
NavigationAppsEnum item = (NavigationAppsEnum) parent.getItemAtPosition(position);
if (item != null) {
Settings.setDefaultNavigationTool2(item.id);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// noop
}
});
refreshBackupLabel();
// Database location
refreshDbOnSDCardSetting();
final CheckBox dbOnSDCardButton = (CheckBox) findViewById(R.id.dbonsdcard);
dbOnSDCardButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
app.moveDatabase(SettingsActivity.this);
}
});
// Debug settings
final CheckBox debugButton = (CheckBox) findViewById(R.id.debug);
debugButton.setChecked(Settings.isDebug());
debugButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Settings.setDebug(!Settings.isDebug());
debugButton.setChecked(Settings.isDebug());
}
});
}
private void initMapfileEdittext(boolean setFocus) {
EditText mfmapFileEdit = (EditText) findViewById(R.id.mapfile);
mfmapFileEdit.setText(Settings.getMapFile());
if (setFocus) {
mfmapFileEdit.requestFocus();
}
}
/**
* @param view
* unused here but needed since this method is referenced from XML layout
*/
public void backup(View view) {
// avoid overwriting an existing backup with an empty database (can happen directly after reinstalling the app)
if (app.getAllStoredCachesCount(true, CacheType.ALL) == 0) {
helpDialog(res.getString(R.string.init_backup), res.getString(R.string.init_backup_unnecessary));
return;
}
final ProgressDialog dialog = ProgressDialog.show(this, res.getString(R.string.init_backup), res.getString(R.string.init_backup_running), true, false);
new Thread() {
@Override
public void run() {
final String backupFileName = app.backupDatabase();
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
helpDialog(res.getString(R.string.init_backup_backup),
backupFileName != null ? res.getString(R.string.init_backup_success) + "\n" + backupFileName : res.getString(R.string.init_backup_failed));
refreshBackupLabel();
}
});
}
}.start();
}
private void refreshBackupLabel() {
TextView lastBackup = (TextView) findViewById(R.id.backup_last);
File lastBackupFile = cgeoapplication.isRestoreFile();
if (lastBackupFile != null) {
lastBackup.setText(res.getString(R.string.init_backup_last) + " " + Formatter.formatTime(lastBackupFile.lastModified()) + ", " + Formatter.formatDate(lastBackupFile.lastModified()));
} else {
lastBackup.setText(res.getString(R.string.init_backup_last_no));
}
}
private void refreshDbOnSDCardSetting() {
final CheckBox dbOnSDCardButton = (CheckBox) findViewById(R.id.dbonsdcard);
dbOnSDCardButton.setChecked(Settings.isDbOnSDCard());
}
/**
* @param view
* unused here but needed since this method is referenced from XML layout
*/
public void restore(View view) {
app.restoreDatabase(this);
}
public boolean saveValues() {
String usernameNew = StringUtils.trimToEmpty(((EditText) findViewById(R.id.username)).getText().toString());
String passwordNew = StringUtils.trimToEmpty(((EditText) findViewById(R.id.password)).getText().toString());
String passvoteNew = StringUtils.trimToEmpty(((EditText) findViewById(R.id.passvote)).getText().toString());
// don't trim signature, user may want to have whitespace at the beginning
String signatureNew = ((EditText) findViewById(R.id.signature)).getText().toString();
String altitudeNew = StringUtils.trimToNull(((EditText) findViewById(R.id.altitude)).getText().toString());
String mfmapFileNew = StringUtils.trimToEmpty(((EditText) findViewById(R.id.mapfile)).getText().toString());
int altitudeNewInt = 0;
if (altitudeNew != null) {
try {
altitudeNewInt = Integer.parseInt(altitudeNew);
} catch (NumberFormatException e) {
altitudeNewInt = 0;
}
}
final boolean status1 = Settings.setLogin(usernameNew, passwordNew);
final boolean status2 = Settings.setGCvoteLogin(passvoteNew);
final boolean status3 = Settings.setSignature(signatureNew);
final boolean status4 = Settings.setAltCorrection(altitudeNewInt);
final boolean status5 = Settings.setMapFile(mfmapFileNew);
TextView field = (TextView) findViewById(R.id.showwaypointsthreshold);
Settings.setShowWaypointsThreshold(safeParse(field, 5));
return status1 && status2 && status3 && status4 && status5;
}
/**
* Returns the Int Value in the Field
*
* @param field
* the field to retrieve the integer value from
* @param defaultValue
* the default value
* @return either the field content or the default value
*/
static private int safeParse(final TextView field, int defaultValue) {
try {
return Integer.parseInt(field.getText().toString());
} catch (NumberFormatException e) {
return defaultValue;
}
}
private static class cgeoChangeMapSource implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Settings.setMapSource(MapProviderFactory.getSourceIdFromOrdinal(arg2));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
arg0.setSelection(MapProviderFactory.getSourceIdFromOrdinal(Settings.getMapSource()));
}
}
private class logIn implements View.OnClickListener {
@Override
public void onClick(View arg0) {
final String username = ((EditText) findViewById(R.id.username)).getText().toString();
final String password = ((EditText) findViewById(R.id.password)).getText().toString();
if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
showToast(res.getString(R.string.err_missing_auth));
return;
}
loginDialog = ProgressDialog.show(SettingsActivity.this, res.getString(R.string.init_login_popup), res.getString(R.string.init_login_popup_working), true);
loginDialog.setCancelable(false);
Settings.setLogin(username, password);
Cookies.clearCookies();
(new Thread() {
@Override
public void run() {
final StatusCode loginResult = Login.login();
Object payload = loginResult;
if (loginResult == StatusCode.NO_ERROR) {
Login.detectGcCustomDate();
payload = Login.downloadAvatarAndGetMemberStatus();
}
logInHandler.obtainMessage(0, payload).sendToTarget();
}
}).start();
}
}
private class webAuth implements View.OnClickListener {
@Override
public void onClick(View arg0) {
final String deviceName = ((EditText) findViewById(R.id.webDeviceName)).getText().toString();
final String deviceCode = Settings.getWebDeviceCode();
if (StringUtils.isBlank(deviceName)) {
showToast(res.getString(R.string.err_missing_device_name));
return;
}
webDialog = ProgressDialog.show(SettingsActivity.this, res.getString(R.string.init_sendToCgeo), res.getString(R.string.init_sendToCgeo_registering), true);
webDialog.setCancelable(false);
(new Thread() {
@Override
public void run() {
int pin = 0;
final String nam = StringUtils.defaultString(deviceName);
final String cod = StringUtils.defaultString(deviceCode);
final Parameters params = new Parameters("name", nam, "code", cod);
HttpResponse response = Network.getRequest("http://send2.cgeo.org/auth.html", params);
if (response != null && response.getStatusLine().getStatusCode() == 200)
{
//response was OK
String[] strings = Network.getResponseData(response).split(",");
try {
pin = Integer.parseInt(strings[1].trim());
} catch (Exception e) {
Log.e("webDialog: " + e.toString());
}
String code = strings[0];
Settings.setWebNameCode(nam, code);
}
webAuthHandler.sendEmptyMessage(pin);
}
}).start();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_MAPFILE_REQUEST) {
if (resultCode == RESULT_OK) {
if (data.hasExtra("mapfile")) {
Settings.setMapFile(data.getStringExtra("mapfile"));
if (!Settings.isValidMapFile(Settings.getMapFile())) {
showToast(res.getString(R.string.warn_invalid_mapfile));
}
}
}
initMapfileEdittext(true);
}
if (requestCode == SELECT_GPXDIR_REQUEST) {
if (resultCode == RESULT_OK) {
if (data.hasExtra("chosenDir")) {
Settings.setGpxExportDir(data.getStringExtra("chosenDir"));
}
}
EditText gpxExportDir = (EditText) findViewById(R.id.gpx_exportdir);
gpxExportDir.setText(Settings.getGpxExportDir());
gpxExportDir.requestFocus();
}
}
public static void startActivity(Context fromActivity) {
final Intent initIntent = new Intent(fromActivity, SettingsActivity.class);
fromActivity.startActivity(initIntent);
}
}
|