/[aagtl_public1]/src/com/zoffcc/applications/aagtl/aagtl.java
aagtl

Contents of /src/com/zoffcc/applications/aagtl/aagtl.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations) (download)
Sun Aug 5 13:48:36 2012 UTC (11 years, 8 months ago) by zoffadmin
File size: 84922 byte(s)
initial import of aagtl source code
1 zoffadmin 2 /**
2     * aagtl Advanced Geocaching Tool for Android
3     * loosely based on agtl by Daniel Fett <fett@danielfett.de>
4     * Copyright (C) 2010 - 2012 Zoff.cc <aagtl@work.zoff.cc>
5     *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * version 2 as published by the Free Software Foundation.
9     *
10     * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     * GNU General Public License for more details.
14     *
15     * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the
17     * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18     * Boston, MA 02110-1301, USA.
19     */
20    
21     package com.zoffcc.applications.aagtl;
22    
23     import java.io.BufferedInputStream;
24     import java.io.BufferedOutputStream;
25     import java.io.BufferedReader;
26     import java.io.ByteArrayInputStream;
27     import java.io.ByteArrayOutputStream;
28     import java.io.DataInputStream;
29     import java.io.File;
30     import java.io.FileInputStream;
31     import java.io.FileNotFoundException;
32     import java.io.FileOutputStream;
33     import java.io.IOException;
34     import java.io.InputStreamReader;
35     import java.io.ObjectInputStream;
36     import java.io.ObjectOutputStream;
37     import java.io.OptionalDataException;
38     import java.io.Serializable;
39     import java.io.StreamCorruptedException;
40     import java.text.SimpleDateFormat;
41     import java.util.ArrayList;
42     import java.util.Calendar;
43     import java.util.Iterator;
44     import java.util.List;
45     import java.util.Locale;
46     import java.util.TimeZone;
47    
48     import android.app.Activity;
49     import android.app.AlertDialog;
50     import android.app.Dialog;
51     import android.app.ProgressDialog;
52     import android.content.Context;
53     import android.content.DialogInterface;
54     import android.content.Intent;
55     import android.content.pm.ActivityInfo;
56     import android.content.res.Configuration;
57     import android.graphics.Bitmap;
58     import android.graphics.BitmapFactory;
59     import android.graphics.Color;
60     import android.graphics.RectF;
61     import android.hardware.Sensor;
62     import android.hardware.SensorEvent;
63     import android.hardware.SensorEventListener;
64     import android.hardware.SensorManager;
65     import android.location.Criteria;
66     import android.location.GpsSatellite;
67     import android.location.GpsStatus;
68     import android.location.Location;
69     import android.location.LocationListener;
70     import android.location.LocationManager;
71     import android.location.LocationProvider;
72     import android.media.AudioManager;
73     import android.net.Uri;
74     import android.os.Bundle;
75     import android.os.Environment;
76     import android.os.Handler;
77     import android.os.Message;
78     import android.os.PowerManager;
79     import android.os.PowerManager.WakeLock;
80     import android.os.SystemClock;
81     import android.text.SpannableString;
82     import android.text.method.LinkMovementMethod;
83     import android.text.util.Linkify;
84     import android.util.DisplayMetrics;
85     import android.util.Log;
86     import android.view.Display;
87     import android.view.KeyEvent;
88     import android.view.Menu;
89     import android.view.MenuItem;
90     import android.view.View;
91     import android.view.View.OnClickListener;
92     import android.webkit.WebSettings;
93     import android.webkit.WebView;
94     import android.widget.Button;
95     import android.widget.RelativeLayout;
96     import android.widget.TextView;
97     import android.widget.Toast;
98     import android.widget.ToggleButton;
99    
100     import com.example.widget.NumberPicker;
101    
102     public class aagtl extends Activity implements LocationListener, GpsStatus.Listener, SensorEventListener
103     // ,OnClickListener
104     {
105    
106     static final boolean EMULATOR = false;
107    
108     static final boolean _RELEASE_ = true;
109     boolean __ZOFF_PHONE__ = false;
110    
111     // OSD buttons/icons -------- START -----------
112     public Bitmap follow_on = null;
113     public Bitmap follow_off = null;
114     public Bitmap follow_current = null;
115     public Boolean follow_mode = true;
116     public RectF follow_button_rect = new RectF(-100, 1, 1, 1);
117    
118     public Bitmap arrow_button = null;
119     public RectF arrow_button_rect = new RectF(-100, 1, 1, 1);
120     // OSD buttons/icons -------- END -----------
121    
122     Button btn_ok = null;
123     Button btn_cancel = null;
124     ToggleButton orient_1_toggle = null;
125     NumberPicker xx1 = null;
126     NumberPicker xx2 = null;
127     NumberPicker xx3 = null;
128     Boolean is_lat_current_change_target_coords = true;
129    
130     // Instantiating the Handler associated with the main thread.
131     private Handler global_messageHandler = new Handler();
132    
133     ProgressThread progressThread;
134     ProgressDialog pbarDialog;
135    
136     public String main_dir = "/sdcard/zoffcc/applications/aagtl";
137     public String main_data_dir = "/sdcard/external_sd/zoffcc/applications/aagtl";
138    
139     public String status_text_string = "";
140     public String status_append_string = "";
141    
142     Location mLastLocation = null;
143     long mLastLocationMillis = -1;
144     Boolean isGPSFix = false;
145     int used_sats = 0;
146    
147     String main_language = null;
148     String sub_language = null;
149     DisplayMetrics metrics;
150     WakeLock wl = null;
151    
152     List<GeocacheCoordinate> downloaded_caches = null;
153    
154     public LocationManager lm = null;
155     public LocationProvider low = null;
156     public LocationProvider high = null;
157     public SensorManager sensorManager = null;
158     public long last_heading_update = 0L;
159    
160     static final int DISPLAY_VIEW_MAP = 1;
161     static final int DISPLAY_VIEW_GC = 2;
162     static final int DISPLAY_VIEW_ARROW = 3;
163    
164     private Boolean show_no_loc_warning = false;
165    
166     public int current_display_view = DISPLAY_VIEW_MAP;
167     static int TOUCH_CACHES_AFTER_THIS_ZOOM_LEVEL = 16;
168    
169     public CharSequence[] route_file_items = null;
170     public String current_routefile_name = null;
171     public List<GeocacheCoordinate> route_file_caches = null;
172     public AlertDialog alert = null;
173    
174     public Rose rose;
175     public CrossHair cross;
176     public GeocachesView gcview;
177     public ArrowView arrowview;
178     public GCacheView cacheview;
179     public RelativeLayout mainscreen_map_view;
180     public int mainscreen_map_view_statusbar_height = 30;
181     public RelativeLayout.LayoutParams layoutParams_mainscreen_map_view;
182     public TextView status_text;
183     public WebView wv;
184    
185     public double cur_lat_gps_save = -1;
186     public double cur_lon_gps_save = -1;
187    
188     public String db_path;
189    
190     MapDownloader mdl;
191     HTMLDownloader wdl;
192     PointProvider pv;
193     CacheDownloader cdol;
194    
195     public static class settings implements Serializable
196     {
197     private static final long serialVersionUID = 171513126339437464L;
198     public Boolean download_visible = true;
199     public Boolean download_notfound = true;
200     public Boolean download_new = true;
201     public Boolean download_nothing = false;
202     public Boolean download_create_index = true;
203     public Boolean download_run_after = false;
204     // download_run_after_string --> is now used as a generic config
205     // container!!
206     public String download_run_after_string = "CFG:v2.0:";
207     //
208     public String download_output_dir = "/sdcard/external_sd/zoffcc/applications/aagtl";
209     public double map_position_lat = 48.23428;
210     public double map_position_lon = 16.391514;
211     public int map_zoom = 15;
212     public int map_type = MapDownloader.MAP_OSM;
213     public Boolean download_resize = true;
214     public int download_resize_pixel = 400;
215     public Boolean options_show_name = true;
216     public String options_username = "Username";
217     public String options_password = "Password";
218     public double last_target_lat = 50.0;
219     public double last_target_lon = 10.0;
220     public String last_target_name = "default";
221     // "default" -> no target set
222     // "GC:GCXXXX" -> gc cache GC-Code
223     // "M:GCXXXX" -> manually entered coord (for GC-Code cache)
224     // "M:manual" -> manually entered coords
225     public Boolean download_noimages = false;
226     public String download_map_path = "/sdcard/external_sd/zoffcc/applications/aagtl";
227     public Boolean options_hide_found = false;
228     public Boolean options_use_compass_heading = false;
229     public String options_gc_filter__type = "";
230     public Boolean options_turn_map_on_heading = false;
231     }
232    
233     public settings global_settings = new settings();
234    
235     /** this criteria will settle for less accuracy, high power, and cost */
236     public static Criteria createCoarseCriteria()
237     {
238    
239     Criteria c = new Criteria();
240     c.setAccuracy(Criteria.ACCURACY_COARSE);
241     c.setAltitudeRequired(false);
242     c.setBearingRequired(false);
243     c.setSpeedRequired(false);
244     c.setCostAllowed(true);
245     c.setPowerRequirement(Criteria.POWER_HIGH);
246     return c;
247    
248     }
249    
250     /** this criteria needs high accuracy, high power, and cost */
251     public static Criteria createFineCriteria()
252     {
253    
254     Criteria c = new Criteria();
255     c.setAccuracy(Criteria.ACCURACY_FINE);
256     c.setAltitudeRequired(false);
257     c.setBearingRequired(false);
258     c.setSpeedRequired(false);
259     c.setCostAllowed(true);
260     c.setPowerRequirement(Criteria.POWER_HIGH);
261     return c;
262    
263     }
264    
265     public void load_settings()
266     {
267     System.out.println("load settings");
268    
269     // O P E N
270     FileInputStream fis = null;
271     try
272     {
273     fis = new FileInputStream(this.main_dir + "/config/settings.cfg");
274     }
275     catch (FileNotFoundException e)
276     {
277     if (CacheDownloader.DEBUG_)
278     {
279     System.out.println("FileNotFoundException");
280     }
281     global_settings = new settings();
282     return;
283     }
284    
285     BufferedInputStream bis = new BufferedInputStream(fis, 65536 /* 64K bytes */);
286     ObjectInputStream ois = null;
287     try
288     {
289     ois = new ObjectInputStream(bis);
290     }
291     catch (StreamCorruptedException e)
292     {
293     if (CacheDownloader.DEBUG_)
294     {
295     System.out.println("StreamCorruptedException");
296     }
297     global_settings = new settings();
298     return;
299     }
300     catch (IOException e)
301     {
302     if (CacheDownloader.DEBUG_)
303     {
304     System.out.println("IOException");
305     }
306     global_settings = new settings();
307     return;
308     }
309    
310     // R E A D
311     try
312     {
313     this.global_settings = (aagtl.settings) ois.readObject();
314     // check for encrypted password
315     if (this.global_settings.download_run_after_string.startsWith("CFG:v2.0:"))
316     {
317     // ok v2.0 so password is already encrypted
318     String inputString = this.global_settings.options_password;
319     if (CacheDownloader.DEBUG_)
320     {
321     System.out.println(this.global_settings.download_run_after_string);
322     }
323     String salt = this.global_settings.download_run_after_string.split(":")[2];
324     String cryptword = salt + "aagtl-" + "52-" + "Crypt";
325    
326     // Create encrypter/decrypter class
327     StringEnc desEncrypter = new StringEnc(cryptword);
328    
329     // Decrypt the string
330     String desDecrypted = desEncrypter.decrypt(inputString);
331    
332     // Print out values
333     if (CacheDownloader.DEBUG_)
334     {
335     System.out.println(" Original String : " + inputString);
336     System.out.println(" Salt (read) : " + salt);
337     System.out.println(" Decrypted String : " + desDecrypted);
338     System.out.println();
339     }
340    
341     this.global_settings.options_password = desDecrypted;
342     }
343     else
344     {
345     // old config so password is clear text
346     // now generate a salt value
347     String inputString = this.global_settings.options_password;
348     String salt = String.valueOf((int) (1000f * Math.random()));
349     if (CacheDownloader.DEBUG_)
350     {
351     System.out.println(" Original String : " + inputString);
352     System.out.println(" Salt (new) : " + salt);
353     }
354     this.global_settings.download_run_after_string = "CFG:v2.0:" + salt + ":";
355     }
356     }
357     catch (OptionalDataException e)
358     {
359     global_settings = new settings();
360     return;
361     }
362     catch (ClassNotFoundException e)
363     {
364     global_settings = new settings();
365     return;
366     }
367     catch (IOException e)
368     {
369     global_settings = new settings();
370     return;
371     }
372     catch (Exception e)
373     {
374     // fail safe, catch all!
375     global_settings = new settings();
376     return;
377     }
378    
379     // C L O S E
380     try
381     {
382     ois.close();
383     }
384     catch (IOException e)
385     {
386     global_settings = new settings();
387     return;
388     }
389    
390     // check if currently viewed cache exists, if not set it to something
391     // sane
392     if ((this.cacheview == null) || (this.cacheview.gc == null))
393     {
394     this.set_init_target_cache();
395     }
396    
397     if (CacheDownloader.DEBUG_)
398     {
399     System.out.println("settings:" + this.global_settings.options_username);
400     System.out.println("settings:" + this.global_settings.options_password);
401     }
402     }
403    
404     public void save_settings()
405     {
406     System.out.println("save settings");
407    
408     // make dirs
409     File dir1 = new File(this.main_dir + "/config/settings.cfg");
410     File dir2 = new File(dir1.getParent());
411     dir2.mkdirs();
412    
413     // O P E N
414     FileOutputStream fos = null;
415     try
416     {
417     fos = new FileOutputStream(this.main_dir + "/config/settings.cfg", false /* append */);
418     }
419     catch (FileNotFoundException e)
420     {
421     if (CacheDownloader.DEBUG_)
422     {
423     System.out.println("FileNotFoundException");
424     }
425     return;
426     }
427    
428     BufferedOutputStream bos = new BufferedOutputStream(fos, 65536 /*
429     * 64K
430     * bytes
431     */);
432     ObjectOutputStream oos = null;
433    
434     try
435     {
436     oos = new ObjectOutputStream(bos);
437     }
438     catch (IOException e)
439     {
440     if (CacheDownloader.DEBUG_)
441     {
442     System.out.println("IOException");
443     }
444     return;
445     }
446    
447     // W R I T E
448     // object to write, and objects it points to must implement
449     // java.io.Serializable
450     // Ideally such objects should also have have a field:
451     // static final long serialVersionUID = 1L;
452     // to prevent spurious InvalidClassExceptions.
453    
454     try
455     {
456     // +++++++++++++++++++++++++
457     // copy the settings object
458     // +++++++++++++++++++++++++
459     // Serialize to a byte array
460     ByteArrayOutputStream bos_save = new ByteArrayOutputStream();
461     ObjectOutputStream out_save = new ObjectOutputStream(bos_save);
462     out_save.writeObject(this.global_settings);
463     out_save.close();
464     // Get the bytes of the serialized object
465     byte[] buf_save = bos_save.toByteArray();
466     // Deserialize from a byte array
467     ObjectInputStream in_save = new ObjectInputStream(new ByteArrayInputStream(buf_save));
468     settings save_settings = (settings) in_save.readObject();
469     in_save.close();
470     // +++++++++++++++++++++++++
471     // copy the settings object
472     // +++++++++++++++++++++++++
473    
474     if (save_settings.download_run_after_string.startsWith("CFG:v2.0:"))
475     {
476     // ok v2.0 so password is clear text new, encrypt it
477     String inputString = save_settings.options_password;
478     String salt = "";
479     try
480     {
481     salt = save_settings.download_run_after_string.split(":")[2];
482     if (CacheDownloader.DEBUG_)
483     {
484     System.out.println("using salt: " + salt);
485     }
486     }
487     catch (Exception e)
488     {
489     // now generate a salt value
490     salt = String.valueOf((int) (1000f * Math.random()));
491     this.global_settings.download_run_after_string = "CFG:v2.0:" + salt + ":";
492     save_settings.download_run_after_string = this.global_settings.download_run_after_string;
493     if (CacheDownloader.DEBUG_)
494     {
495     System.out.println("generated salt: " + salt);
496     }
497     }
498     String cryptword = salt + "aagtl-" + "52-" + "Crypt";
499    
500     // Create encrypter/decrypter class
501     StringEnc desEncrypter = new StringEnc(cryptword);
502    
503     // Encrypt the string
504     String desEncrypted = desEncrypter.encrypt(inputString);
505    
506     if (CacheDownloader.DEBUG_)
507     {
508     // Print out values
509     System.out.println(" Original String : " + inputString);
510     System.out.println(" Salt (read) : " + salt);
511     System.out.println(" Encrypted String : " + desEncrypted);
512     System.out.println();
513     }
514    
515     save_settings.options_password = desEncrypted;
516     }
517    
518     oos.writeObject(save_settings);
519     oos.flush();
520     // C L O S E
521     oos.close();
522    
523     // free some memory
524     save_settings = null;
525     }
526     catch (IOException e)
527     {
528     if (CacheDownloader.DEBUG_)
529     {
530     System.out.println("IOException 2");
531     }
532     return;
533     }
534     catch (Exception e)
535     {
536     if (CacheDownloader.DEBUG_)
537     {
538     System.out.println("**Exception**");
539     }
540     // fail safe, catch all!
541     return;
542     }
543    
544     }
545    
546     @Override
547     public void onSaveInstanceState(Bundle savedInstanceState)
548     {
549     // Save UI state changes to the savedInstanceState.
550     // This bundle will be passed to onCreate if the process is
551     // killed and restarted.
552     savedInstanceState.putDouble("global_settings.map_position_lat", global_settings.map_position_lat);
553     savedInstanceState.putDouble("global_settings.map_position_lon", global_settings.map_position_lon);
554     System.out.println(String.valueOf(this.cacheview.gc));
555     savedInstanceState.putSerializable("cacheview.gc", this.cacheview.gc);
556     savedInstanceState.putSerializable("rose.current_target", this.rose.current_target);
557     this.save_settings();
558    
559     super.onSaveInstanceState(savedInstanceState);
560     System.out.println("save bundle");
561     }
562    
563     @Override
564     public void onRestoreInstanceState(Bundle savedInstanceState)
565     {
566     super.onRestoreInstanceState(savedInstanceState);
567    
568     if (!savedInstanceState.isEmpty())
569     {
570     global_settings.map_position_lat = savedInstanceState.getDouble("global_settings.map_position_lat");
571     global_settings.map_position_lon = savedInstanceState.getDouble("global_settings.map_position_lon");
572    
573     // try
574     // {
575     // System.out.println(String.valueOf(this.cacheview.gc));
576     // this.cacheview.gc = (GeocacheCoordinate) savedInstanceState
577     // .getSerializable("cacheview.gc");
578     // System.out.println(String.valueOf(this.cacheview.gc));
579     // this.rose.current_target = (GeocacheCoordinate)
580     // savedInstanceState
581     // .getSerializable("rose.current_target");
582     // }
583     // catch (Exception e)
584     // {
585     // System.out.println("error in restoring values");
586     // }
587     }
588    
589     System.out.println("restore bundle");
590    
591     // System.out.println("map_position_lat:" +
592     // String.valueOf(global_settings.map_position_lat));
593     // System.out.println("map_position_lon:" +
594     // String.valueOf(global_settings.map_position_lon));
595     }
596    
597     public void set_center(double lat, double lon)
598     {
599     global_settings.map_position_lat = lat;
600     global_settings.map_position_lon = lon;
601     }
602    
603     @Override
604     protected void onCreate(Bundle savedInstanceState)
605     {
606     super.onCreate(savedInstanceState);
607    
608     // choose the correct volume to change (for TTS)
609     // setVolumeControlStream(AudioManager.STREAM_SYSTEM);
610     // setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);
611     setVolumeControlStream(AudioManager.STREAM_MUSIC); // --> this seems to
612     // be used for TTS
613     // setVolumeControlStream(AudioManager.STREAM_DTMF);
614    
615     String default_sdcard_dir = Environment.getExternalStorageDirectory().getAbsolutePath();
616     Log.e("aagtl", "sdcard dir=" + default_sdcard_dir);
617    
618     main_dir = default_sdcard_dir + "/zoffcc/applications/aagtl";
619     main_data_dir = default_sdcard_dir + "/external_sd/zoffcc/applications/aagtl";
620    
621     // get the local language -------------
622     Locale locale = java.util.Locale.getDefault();
623     String lang = locale.getLanguage();
624     String langu = lang;
625     String langc = lang;
626     Log.e("aagtl", "lang=" + lang);
627     int pos = langu.indexOf('_');
628     if (pos != -1)
629     {
630     langc = langu.substring(0, pos);
631     langu = langc + langu.substring(pos).toUpperCase(locale);
632     Log.e("aagtl", "substring lang " + langu.substring(pos).toUpperCase(locale));
633     // set lang. for translation
634     main_language = langc;
635     sub_language = langu.substring(pos).toUpperCase(locale);
636     }
637     else
638     {
639     String country = locale.getCountry();
640     Log.e("aagtl", "Country1 " + country);
641     Log.e("aagtl", "Country2 " + country.toUpperCase(locale));
642     langu = langc + "_" + country.toUpperCase(locale);
643     // set lang. for translation
644     main_language = langc;
645     sub_language = country.toUpperCase(locale);
646     }
647     Log.e("aagtl", "Language " + lang);
648     // get the local language -------------
649    
650     // make dirs
651     File dir11 = new File(this.main_dir);
652     dir11.mkdirs();
653     File dir22 = new File(this.main_data_dir);
654     dir22.mkdirs();
655     // create nomedia files
656     File nomedia_file = new File(this.main_dir + "/.nomedia");
657     try
658     {
659     nomedia_file.createNewFile();
660     }
661     catch (IOException e1)
662     {
663     e1.printStackTrace();
664     }
665     nomedia_file = new File(this.main_data_dir + "/.nomedia");
666     try
667     {
668     nomedia_file.createNewFile();
669     }
670     catch (IOException e1)
671     {
672     e1.printStackTrace();
673     }
674     // create nomedia files
675    
676     // make dirs (for "route" files, you can select a file, and only those
677     // GC will be shown)
678     File dir71 = new File(this.main_dir + "/routes/");
679     dir71.mkdirs();
680    
681     /*
682     * show info box for first time users
683     */
684     AlertDialog.Builder infobox = new AlertDialog.Builder(this);
685     infobox.setTitle(aagtlTextTranslations.INFO_BOX_TITLE); // TRANS
686     infobox.setCancelable(false);
687     final TextView message = new TextView(this);
688     message.setFadingEdgeLength(20);
689     message.setVerticalFadingEdgeEnabled(true);
690     message.setVerticalScrollBarEnabled(true);
691     RelativeLayout.LayoutParams rlpib = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
692    
693     // margins seem not to work, hmm strange
694     // so add a " " at start of every line. well whadda you gonna do ...
695     // rlp.leftMargin = 8; -> we use "m" string
696    
697     message.setLayoutParams(rlpib);
698     final SpannableString s = new SpannableString(aagtlTextTranslations.INFO_BOX_TEXT); // TRANS
699     Linkify.addLinks(s, Linkify.WEB_URLS);
700     message.setText(s);
701     message.setMovementMethod(LinkMovementMethod.getInstance());
702     infobox.setView(message);
703    
704     // TRANS
705     infobox.setPositiveButton("Ok", new DialogInterface.OnClickListener()
706     {
707     public void onClick(DialogInterface arg0, int arg1)
708     {
709     Log.e("aagtl", "Ok, user saw the infobox");
710     }
711     });
712    
713     // TRANS
714     infobox.setNeutralButton(aagtlTextTranslations.JAVA_MENU_MOREINFO, new DialogInterface.OnClickListener()
715     {
716     public void onClick(DialogInterface arg0, int arg1)
717     {
718     Log.e("aagtl", "user wants more info, show the website");
719     String url = "http://aagtl.work.zoff.cc/";
720     Intent i = new Intent(Intent.ACTION_VIEW);
721     i.setData(Uri.parse(url));
722     startActivity(i);
723     }
724     });
725    
726     //
727     //
728     //
729     show_no_loc_warning = false;
730     try
731     {
732     // get location services
733     // lm = LocationUtils.getLocationManager(ctx.getMyContext());
734     this.lm = (LocationManager) getSystemService(LOCATION_SERVICE);
735     // get low accuracy provider
736     this.low = lm.getProvider(lm.getBestProvider(createCoarseCriteria(), true));
737    
738     // get high accuracy provider
739     this.high = lm.getProvider(lm.getBestProvider(createFineCriteria(), true));
740     // ++++++ lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 700L,
741     // 1.0f, this);
742    
743     if (!EMULATOR)
744     {
745     sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
746     }
747     }
748     catch (Exception e)
749     {
750     e.printStackTrace();
751     // Toast.makeText(getApplicationContext(), "You dont have any Location providers!\nthis program will not work on your device!", Toast.LENGTH_LONG).show();
752     // set the infobox text to this error message, to tell the user it wont work correctly
753     message.setText("\n You don't have any Location providers!\n This program will not work fully!\n");
754     show_no_loc_warning = true;
755     }
756    
757     //
758     //
759     //
760     //
761    
762     String FIRST_STARTUP_FILE = main_dir + "/infobox_seen.txt";
763     File aagtl_first_startup = new File(FIRST_STARTUP_FILE);
764     // if file does NOT exist, show the info box
765     // or if "no location providers", show info box
766     if ((!aagtl_first_startup.exists()) || (show_no_loc_warning))
767     {
768     // if "no location providers" text, then dont show/save info box file
769     if (!show_no_loc_warning)
770     {
771     FileOutputStream fos_temp;
772     try
773     {
774     fos_temp = new FileOutputStream(aagtl_first_startup);
775     fos_temp.write((int) 65); // just write an "A" to the file, but
776     // really doesnt matter
777     fos_temp.flush();
778     fos_temp.close();
779     }
780     catch (Exception e)
781     {
782     e.printStackTrace();
783     }
784     }
785    
786     try
787     {
788     infobox.show();
789     }
790     catch (Exception e)
791     {
792     e.printStackTrace();
793     }
794     }
795     /*
796     * show info box for first time users
797     */
798    
799     // get display properties
800     Display display_ = getWindowManager().getDefaultDisplay();
801     int width_ = display_.getWidth();
802     int height_ = display_.getHeight();
803     this.metrics = new DisplayMetrics();
804     display_.getMetrics(this.metrics);
805     Log.e("aagtl", "aagtl -> pixels x=" + width_ + " pixels y=" + height_);
806     Log.e("aagtl", "aagtl -> dpi=" + this.metrics.densityDpi);
807     Log.e("aagtl", "aagtl -> density=" + this.metrics.density);
808     Log.e("aagtl", "aagtl -> scaledDensity=" + this.metrics.scaledDensity);
809     // get display properties
810    
811     System.out.println("Create: " + String.valueOf(savedInstanceState));
812     if (savedInstanceState != null)
813     {
814     this.onRestoreInstanceState(savedInstanceState);
815     }
816    
817     this.load_settings();
818    
819     // Set full screen view
820     // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
821     // WindowManager.LayoutParams.FLAG_FULLSCREEN);
822     getWindow().setFlags(0, 0);
823     // requestWindowFeature(Window.FEATURE_NO_TITLE);
824     requestWindowFeature(0);
825    
826     // System.out.println("CR map_position_lat:" +
827     // String.valueOf(global_settings.map_position_lat));
828     // System.out.println("CR map_position_lon:" +
829     // String.valueOf(global_settings.map_position_lon));
830    
831     // setContentView(R.layout.main);
832    
833     Boolean sensors_fullblown = false;
834     if (sensors_fullblown)
835     {
836     List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
837     Sensor ourSensor = null;
838     for (int i = 0; i < sensors.size(); ++i)
839     {
840     ourSensor = sensors.get(i);
841     // System.out.println(ourSensor.getName());
842     if (ourSensor != null)
843     {
844     sensorManager.registerListener(this, ourSensor, SensorManager.SENSOR_DELAY_NORMAL);
845     }
846     }
847     }
848    
849     // System.out.println("main: create");
850     this.mdl = new MapDownloader(global_messageHandler, this);
851     this.mdl.start();
852    
853     this.wdl = new HTMLDownloader(this);
854    
855     this.cdol = new CacheDownloader(this, wdl);
856    
857     // ------- MAP VIEW: CREATE -------
858     layoutParams_mainscreen_map_view = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
859     mainscreen_map_view = new RelativeLayout(this);
860     mainscreen_map_view.setLayoutParams(layoutParams_mainscreen_map_view);
861    
862     status_text = new TextView(this);
863     status_text.setBackgroundColor(Color.BLACK);
864     status_text.setTextColor(Color.WHITE);
865     status_text.setMaxLines(1);
866     status_text.setTextSize(12);
867     status_text.setText("--- --- --- --- ---");
868     status_text.setVisibility(0); // set to visible
869    
870     rose = new Rose(this, this);
871     RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
872     rlp.bottomMargin = mainscreen_map_view_statusbar_height;
873     rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
874     mainscreen_map_view.addView(rose, rlp);
875    
876     RelativeLayout.LayoutParams tvlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, mainscreen_map_view_statusbar_height);
877     tvlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
878     mainscreen_map_view.addView(status_text, tvlp);
879    
880     this.wv = new WebView(this);
881     this.wv.getSettings().setSupportZoom(true);
882     this.wv.getSettings().setBuiltInZoomControls(true);
883     this.wv.getSettings().setDefaultFontSize(13);
884    
885     // zoom seems to be NULL on some systems (e.g. honeycomb)
886     final View zoom = this.wv.getZoomControls();
887    
888     try
889     {
890     zoom.setVisibility(View.INVISIBLE);
891     }
892     catch (Exception e)
893     {
894     e.printStackTrace();
895     }
896    
897     this.wv.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
898     RelativeLayout.LayoutParams wvlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
899     mainscreen_map_view.addView(wv, wvlp);
900    
901     cross = new CrossHair(this, this);
902     RelativeLayout.LayoutParams chlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
903    
904     gcview = new GeocachesView(this, this);
905     RelativeLayout.LayoutParams gcwlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
906    
907     arrowview = new ArrowView(this, this);
908     RelativeLayout.LayoutParams avlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
909    
910     cacheview = new GCacheView(this, this);
911     RelativeLayout.LayoutParams cvlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
912    
913     // get the png's
914     this.follow_on = BitmapFactory.decodeResource(getResources(), R.drawable.follow);
915     this.follow_off = BitmapFactory.decodeResource(getResources(), R.drawable.follow_off);
916     this.follow_current = this.follow_on;
917     this.arrow_button = BitmapFactory.decodeResource(getResources(), R.drawable.arrow_off);
918    
919     chlp.bottomMargin = mainscreen_map_view_statusbar_height;
920     gcwlp.bottomMargin = mainscreen_map_view_statusbar_height;
921     avlp.bottomMargin = mainscreen_map_view_statusbar_height;
922     cvlp.bottomMargin = mainscreen_map_view_statusbar_height;
923     wvlp.bottomMargin = mainscreen_map_view_statusbar_height;
924     wvlp.topMargin = 80 + 4 * 40;
925     mainscreen_map_view.addView(cross, chlp);
926     mainscreen_map_view.addView(gcview, gcwlp);
927     mainscreen_map_view.addView(arrowview, avlp);
928     mainscreen_map_view.addView(cacheview, cvlp);
929     // ------- MAP VIEW: CREATE -------
930    
931     // ------- VIEWS -------
932     // make correct Z-order
933     rose.bringToFront();
934     gcview.bringToFront();
935     cross.bringToFront();
936     status_text.bringToFront();
937     wv.bringToFront();
938     arrowview.setVisibility(View.INVISIBLE);
939     cacheview.setVisibility(View.INVISIBLE);
940     wv.setVisibility(View.INVISIBLE);
941     setContentView(mainscreen_map_view);
942     // ------- VIEWS -------
943    
944     // ------- set default VIEW on display
945     this.set_display_screen(aagtl.DISPLAY_VIEW_MAP);
946     // ------- set default VIEW on display
947    
948     db_path = this.main_data_dir + "/config/caches.db";
949     File dir1 = new File(db_path);
950     File dir2 = new File(dir1.getParent());
951     dir2.mkdirs();
952     // pv = new PointProvider(db_path, this.mdl, "", "geocaches");
953     // *** DANGER *** pv._clear_database_();
954    
955     // get position on first startup
956     turn_off_gps();
957     try
958     {
959     // wait for gsm cell fix
960     Thread.sleep(1000L);
961     }
962     catch (InterruptedException e)
963     {
964     e.printStackTrace();
965     }
966     // ok, now switch to real gps
967     turn_on_gps();
968    
969     PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
970     // this.wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK ,
971     // "aagtlWakeLock"); // i think this is not working
972     this.wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "aagtlWakeLock");
973     }
974    
975     @Override
976     public boolean onCreateOptionsMenu(Menu menu)
977     {
978     super.onCreateOptionsMenu(menu);
979     // System.out.println("onCreateOptionsMenu");
980     return true;
981     }
982    
983     @Override
984     public boolean onPrepareOptionsMenu(Menu menu)
985     {
986     super.onPrepareOptionsMenu(menu);
987     // this gets called every time the menu is opened!!
988     // change menu items here!
989     // System.out.println("onPrepareOptionsMenu");
990    
991     switch (this.current_display_view)
992     {
993     case DISPLAY_VIEW_MAP:
994     // main map view
995    
996     menu.clear();
997     // g-id,i-id,order
998     menu.add(1, 2, 20, "zoom in");
999     menu.add(1, 3, 22, "zoom out");
1000    
1001     menu.add(1, 4, 30, "maptype: OSM");
1002     if (__ZOFF_PHONE__)
1003     {
1004     menu.add(1, 5, 32, "maptype: Sat");
1005     }
1006     else
1007     {
1008     menu.add(1, 29, 33, "maptype: OCM");
1009     }
1010    
1011     menu.add(1, 1, 40, "get caches in view");
1012     menu.add(1, 20, 41, "get details in view");
1013    
1014     menu.add(1, 21, 42, "turn on GPS");
1015     menu.add(1, 22, 43, "turn off GPS");
1016     menu.add(1, 25, 44, "show arrow view");
1017    
1018     menu.add(1, 30, 45, "navigate to target");
1019     menu.add(1, 32, 46, "change target lat.");
1020     menu.add(1, 33, 47, "change target lon.");
1021    
1022     menu.add(1, 27, 49, "select routefile");
1023     menu.add(1, 28, 50, "get caches from routefile");
1024     menu.add(1, 23, 51, "upload fieldnotes");
1025     if (this.global_settings.options_turn_map_on_heading)
1026     {
1027     menu.add(1, 26, 52, "turn off rotating map");
1028     }
1029     else
1030     {
1031     menu.add(1, 26, 52, "turn on rotating map");
1032     }
1033    
1034     menu.add(1, 6, 78, "-");
1035     menu.add(1, 19, 79, "set username/password");
1036     menu.add(1, 6, 80, "-");
1037     menu.add(1, 7, 82, "empty database");
1038    
1039     break;
1040     case DISPLAY_VIEW_GC:
1041     // gc-view
1042     menu.clear();
1043     // g-id,i-id,order
1044     menu.add(1, 8, 20, "map view");
1045     menu.add(1, 14, 22, "update details");
1046     menu.add(1, 9, 24, "set as target -> map");
1047     menu.add(1, 10, 26, "set as target -> arrow");
1048     menu.add(1, 15, 28, "description");
1049     menu.add(1, 16, 30, "hints");
1050     menu.add(1, 17, 32, "logs");
1051     menu.add(1, 34, 33, "waypoints");
1052     menu.add(1, 18, 34, "shorttext");
1053     menu.add(1, 31, 35, "show Cache website");
1054     // seems to be buggy, so for the moment remove it
1055     // if (this.rose.current_target != null)
1056     // {
1057     // menu.add(1, 24, 36, "post fieldnote");
1058     // }
1059     break;
1060     case DISPLAY_VIEW_ARROW:
1061     // arrow view
1062     menu.clear();
1063     // g-id,i-id,order
1064     menu.add(1, 11, 20, "cache view");
1065     menu.add(1, 12, 22, "map view");
1066     String st_compass = "use compass heading";
1067     if (this.global_settings.options_use_compass_heading)
1068     {
1069     st_compass = "use gps heading";
1070    
1071     }
1072     menu.add(1, 13, 24, st_compass);
1073     if (this.rose.current_target != null)
1074     {
1075     menu.add(1, 24, 26, "post fieldnote");
1076     }
1077     break;
1078     }
1079    
1080     return true;
1081     }
1082    
1083     public void onSensorChanged(SensorEvent event)
1084     {
1085     if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
1086     {
1087     // System.out.println("Sensor.TYPE_MAGNETIC_FIELD");
1088     return;
1089     }
1090    
1091     if (event.sensor.getType() == Sensor.TYPE_ORIENTATION)
1092     {
1093     // System.out.println("Sensor.TYPE_ORIENTATION");
1094    
1095     Boolean set_c_heading = false;
1096     if (this.global_settings.options_use_compass_heading)
1097     {
1098     // if "options_use_compass_heading" is set the use compass
1099     // always
1100     set_c_heading = true;
1101     // System.out.println("true 1");
1102     }
1103     else
1104     {
1105     if (!this.isGPSFix)
1106     {
1107     // if no gps-fix then use compass until gps fix found
1108     set_c_heading = true;
1109     // System.out.println("true 2");
1110     }
1111     }
1112    
1113     if (set_c_heading)
1114     {
1115     // compass
1116     double myAzimuth = event.values[0];
1117     // double myPitch = event.values[1];
1118     // double myRoll = event.values[2];
1119    
1120     // String out = String.format("Azimuth: %.2f", myAzimuth);
1121     // System.out.println("compass: " + out);
1122    
1123     if (this.current_display_view == DISPLAY_VIEW_MAP)
1124     {
1125     long normal_delay = 1000L; // update every 1.0 seconds
1126     if (this.global_settings.options_turn_map_on_heading)
1127     {
1128     normal_delay = 400L; // update every 0.4 seconds with
1129     // turning map!
1130     }
1131     if (System.currentTimeMillis() - last_heading_update < normal_delay)
1132     {
1133     // System.out.println("-> compass heading: update to fast, skipped");
1134     }
1135     else
1136     {
1137     // System.out.println("-> compass heading (slow)");
1138     last_heading_update = System.currentTimeMillis();
1139     this.cross.set_gps_heading(myAzimuth);
1140     this.cross.invalidate();
1141     this.arrowview.invalidate();
1142     }
1143     }
1144     else
1145     {
1146     // System.out.println("-> compass heading (fast)");
1147     last_heading_update = System.currentTimeMillis();
1148     this.cross.set_gps_heading(myAzimuth);
1149     this.cross.invalidate();
1150     this.arrowview.invalidate();
1151     }
1152     }
1153    
1154     // String out =
1155     // String.format("Azimuth: %.2f\n\nPitch:%.2f\n\nRoll:%.2f\n\n",
1156     // myAzimuth,
1157     // myPitch, myRoll);
1158     // System.out.println("compass: " + out);
1159     }
1160     }
1161    
1162     public void onAccuracyChanged(Sensor sensor, int accuracy)
1163     {
1164     // compass
1165     }
1166    
1167     @Override
1168     public void onBackPressed()
1169     {
1170     // do something on back.
1171     // System.out.println("no back key!");
1172    
1173     if (this.current_display_view == DISPLAY_VIEW_MAP)
1174     {
1175     this.finish(); // -> this calls "onPause"
1176     }
1177     else if (this.current_display_view == DISPLAY_VIEW_GC)
1178     {
1179     this.set_display_screen(DISPLAY_VIEW_MAP);
1180     }
1181     else if (this.current_display_view == DISPLAY_VIEW_ARROW)
1182     {
1183     if (this.rose.current_target != null)
1184     {
1185     // show gc view, if we have a target
1186     this.set_display_screen(DISPLAY_VIEW_GC);
1187     }
1188     else
1189     {
1190     // no target -> show map view
1191     this.set_display_screen(DISPLAY_VIEW_MAP);
1192     }
1193     }
1194    
1195     return;
1196     }
1197    
1198     public void set_init_target_cache()
1199     {
1200     if (this.global_settings.last_target_name.compareTo("default") == 0)
1201     {
1202     // no target set!!
1203     }
1204     else
1205     {
1206     String my_tmp_name = "last target";
1207     GeocacheCoordinate tmp_gc = new GeocacheCoordinate(this.global_settings.last_target_lat, this.global_settings.last_target_lon, my_tmp_name);
1208     if (this.cacheview != null)
1209     {
1210     System.out.println("xxxxx1");
1211     this.cacheview.set_cache(tmp_gc);
1212     // this.set_display_screen(aagtl.DISPLAY_VIEW_GC);
1213     // this.set_display_screen(aagtl.DISPLAY_VIEW_MAP);
1214     }
1215     if (this.rose != null)
1216     {
1217     System.out.println("xxxxx2");
1218     if (this.global_settings != null)
1219     {
1220     System.out.println("xxxxx3");
1221     System.out.println("" + this.global_settings.last_target_name);
1222     System.out.println("" + this.global_settings.last_target_lat);
1223     System.out.println("" + this.global_settings.last_target_lon);
1224     this.rose.current_target = tmp_gc;
1225     }
1226     }
1227     }
1228     }
1229    
1230     public void set_target()
1231     {
1232     this.rose.current_target = this.cacheview.get_cache();
1233     this.global_settings.last_target_lat = this.rose.current_target.lat;
1234     this.global_settings.last_target_lon = this.rose.current_target.lon;
1235     this.global_settings.last_target_name = "GC:" + this.rose.current_target.name;
1236     // "default" -> no target set
1237     // "GC:GCXXXX" -> gc cache GC-Code
1238     // "M:GCXXXX" -> manually entered coord (for GC-Code cache)
1239     // "M:manual" -> manually entered coords
1240     }
1241    
1242     @Override
1243     public boolean onOptionsItemSelected(MenuItem item)
1244     {
1245     // Handle item selection
1246     switch (item.getItemId())
1247     {
1248     case 1:
1249     // download caches (no full details)
1250     this.download_caches_in_visible_view(false);
1251     return true;
1252     case 2:
1253     this.rose.zoom_in();
1254     return true;
1255     case 3:
1256     this.rose.zoom_out();
1257     return true;
1258     case 4:
1259     this.rose.change_map_type(MapDownloader.MAP_OSM);
1260     return true;
1261     case 5:
1262     this.rose.change_map_type(MapDownloader.MAP_BIM);
1263     return true;
1264     case 6:
1265     // dummy, do nothing!!
1266     // dummy, do nothing!!
1267     // dummy, do nothing!!
1268     // dummy, do nothing!!
1269     // dummy, do nothing!!
1270     return true;
1271     case 7:
1272     // drop table, and create new -> in DB
1273     this.pv._clear_database_();
1274     return true;
1275     case 8:
1276     this.set_display_screen(DISPLAY_VIEW_MAP);
1277     return true;
1278     case 9:
1279     // set as target -> map
1280     this.set_target();
1281     this.set_display_screen(DISPLAY_VIEW_MAP);
1282     return true;
1283     case 10:
1284     // set as target -> arrow
1285     this.set_target();
1286     this.set_display_screen(DISPLAY_VIEW_ARROW);
1287     return true;
1288     case 11:
1289     // cache view
1290     this.set_display_screen(DISPLAY_VIEW_GC);
1291     return true;
1292     case 12:
1293     // map view
1294     this.set_display_screen(DISPLAY_VIEW_MAP);
1295     return true;
1296     case 13:
1297     // toggle compass/gps heading
1298     this.global_settings.options_use_compass_heading = !this.global_settings.options_use_compass_heading;
1299     return true;
1300     case 14:
1301     // download gc-details again
1302     this.cacheview.details_loaded = 0;
1303     this.cacheview.download_details_thread_finished = false;
1304     this.cacheview.gc_name_previous = "";
1305     this.cacheview.need_repaint = true;
1306     this.cacheview.override_download = true;
1307     this.cacheview.invalidate();
1308     return true;
1309     case 15:
1310     // show description
1311     this.cacheview.show_field = GCacheView.SHOW_DESC;
1312     this.cacheview.need_repaint = true;
1313     this.cacheview.invalidate();
1314     return true;
1315     case 16:
1316     // show hints
1317     this.cacheview.show_field = GCacheView.SHOW_HINTS;
1318     this.cacheview.need_repaint = true;
1319     this.cacheview.invalidate();
1320     return true;
1321     case 17:
1322     // show logs
1323     this.cacheview.show_field = GCacheView.SHOW_LOGS;
1324     this.cacheview.need_repaint = true;
1325     this.cacheview.invalidate();
1326     return true;
1327     case 18:
1328     // show shortdesc
1329     this.cacheview.show_field = GCacheView.SHOW_SHORT_DESC;
1330     this.cacheview.need_repaint = true;
1331     this.cacheview.invalidate();
1332     return true;
1333     case 19:
1334     // set gc.com user/pass
1335     // save setting before we leave this activity!!
1336     this.save_settings();
1337     // ok startup the user/pass activity
1338     Intent foo = new Intent(this, TextEntryActivity.class);
1339     foo.putExtra("title", "Enter geocaching.com userdetails");
1340     foo.putExtra("username", this.global_settings.options_username);
1341     foo.putExtra("password", this.global_settings.options_password);
1342     this.startActivityForResult(foo, 7);
1343     return true;
1344     case 20:
1345     // download caches (with full details!)
1346     this.download_caches_in_visible_view(true);
1347     return true;
1348     case 21:
1349     // turn gps on
1350     this.turn_on_gps();
1351     this.cross.invalidate();
1352     this.arrowview.invalidate();
1353     return true;
1354     case 22:
1355     // turn gps off
1356     this.turn_off_gps();
1357     this.cross.invalidate();
1358     this.arrowview.invalidate();
1359     return true;
1360     case 23:
1361     // upload fieldnotes
1362     this.upload_fieldnotes();
1363     return true;
1364     case 24:
1365     // open fieldnote entry form
1366     // ok startup the activity
1367     Intent foo2 = new Intent(this, PostLogEntryActivity.class);
1368     foo2.putExtra("title", "Fieldnote for " + this.rose.current_target.name);
1369     Calendar cal = Calendar.getInstance();
1370     SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
1371     foo2.putExtra("msg", "\nTFTC\n" + sdf2.format(cal.getTime()) + "");
1372     this.startActivityForResult(foo2, 8);
1373     return true;
1374     case 25:
1375     // set "arrow" as current view
1376     this.set_display_screen(DISPLAY_VIEW_ARROW);
1377     return true;
1378     case 26:
1379     // toggle "turn map with heading"
1380     this.global_settings.options_turn_map_on_heading = !this.global_settings.options_turn_map_on_heading;
1381     return true;
1382     case 27:
1383     // select routefile
1384     this.get_route_files();
1385     return true;
1386     case 28:
1387     // download caches from route file
1388     if ((this.route_file_caches != null) && (this.route_file_caches.size() > 0))
1389     {
1390     showDialog(3);
1391     }
1392     else
1393     {
1394     Toast.makeText(getApplicationContext(), "No caches in Route!", Toast.LENGTH_SHORT).show();
1395     }
1396     return true;
1397     case 29:
1398     // map type open cycle maps
1399     this.rose.change_map_type(MapDownloader.MAP_OCM);
1400     return true;
1401     case 30:
1402     // call navigation intent to drive to target
1403     this.navigate_to_target();
1404     return true;
1405     case 31:
1406     // show gc cache page in browser
1407     this.show_cache_page_in_browser();
1408     return true;
1409     case 32:
1410     // change target coords lat
1411     if (this.rose.current_target != null)
1412     {
1413     this.change_target_coords_lat();
1414     }
1415     else
1416     {
1417     Toast.makeText(getApplicationContext(), "No target selected", Toast.LENGTH_SHORT).show();
1418     }
1419     return true;
1420     case 33:
1421     // change target coords lon
1422     if (this.rose.current_target != null)
1423     {
1424     this.change_target_coords_lon();
1425     }
1426     else
1427     {
1428     Toast.makeText(getApplicationContext(), "No target selected", Toast.LENGTH_SHORT).show();
1429     }
1430     return true;
1431     case 34:
1432     // show waypoints
1433     this.cacheview.show_field = GCacheView.SHOW_WAYPOINTS;
1434     this.cacheview.need_repaint = true;
1435     this.cacheview.invalidate();
1436     return true;
1437     default:
1438     return super.onOptionsItemSelected(item);
1439     }
1440     }
1441    
1442     public void toast_me(String in_text, int duration)
1443     {
1444     Toast.makeText(getApplicationContext(), in_text, duration).show();
1445     }
1446    
1447     public Coordinate pixmappoint2coord(int[] point)
1448     {
1449     Coordinate ret = null;
1450     ret = this.rose.num2deg(((double) point[0] + (double) (this.rose.map_center_x * this.rose.tile_size_x) - (double) (this.rose.mCanvasWidth / 2)) / (double) this.rose.tile_size_x, ((double) point[1] + (double) (this.rose.map_center_y * this.rose.tile_size_y) - (double) (this.rose.mCanvasHeight / 2)) / (double) this.rose.tile_size_y);
1451     return ret;
1452     }
1453    
1454     public void set_display_screen(int dt)
1455     {
1456     switch (dt)
1457     {
1458     case DISPLAY_VIEW_MAP:
1459     // main map view
1460     if (!this.global_settings.options_use_compass_heading)
1461     {
1462     this.turn_off_compass();
1463     }
1464     else
1465     {
1466     this.turn_on_compass();
1467     }
1468     this.current_display_view = DISPLAY_VIEW_MAP;
1469    
1470     arrowview.setVisibility(View.INVISIBLE);
1471     cacheview.setVisibility(View.INVISIBLE);
1472     rose.setVisibility(View.VISIBLE);
1473     gcview.setVisibility(View.VISIBLE);
1474     cross.setVisibility(View.VISIBLE);
1475     status_text.setVisibility(View.VISIBLE);
1476     wv.setVisibility(View.VISIBLE);
1477    
1478     rose.bringToFront();
1479     gcview.bringToFront();
1480     cross.bringToFront();
1481     status_text.bringToFront();
1482     cross.invalidate();
1483     gcview.invalidate();
1484     status_text.invalidate();
1485    
1486     break;
1487     case DISPLAY_VIEW_GC:
1488     // gc-view
1489     this.turn_off_compass();
1490     this.current_display_view = DISPLAY_VIEW_GC;
1491    
1492     rose.setVisibility(View.INVISIBLE);
1493     gcview.setVisibility(View.INVISIBLE);
1494     cross.setVisibility(View.INVISIBLE);
1495     arrowview.setVisibility(View.INVISIBLE);
1496     wv.setVisibility(View.INVISIBLE);
1497     status_text.setVisibility(View.VISIBLE);
1498     cacheview.setVisibility(View.VISIBLE);
1499    
1500     // make sure we dont start cachedownload immediately
1501     cacheview.override_download = true;
1502     cacheview.gc.desc = "please update details!!";
1503     cacheview.download_details_thread_finished = true;
1504     cacheview.details_loaded = 2;
1505     cacheview.get_gc_from_db = 1;
1506    
1507     status_text.bringToFront();
1508     cacheview.bringToFront();
1509     cacheview.invalidate();
1510     status_text.invalidate();
1511    
1512     break;
1513     case DISPLAY_VIEW_ARROW:
1514     // gc-view
1515     this.turn_on_compass();
1516     this.current_display_view = DISPLAY_VIEW_ARROW;
1517    
1518     rose.setVisibility(View.INVISIBLE);
1519     gcview.setVisibility(View.INVISIBLE);
1520     cross.setVisibility(View.INVISIBLE);
1521     cacheview.setVisibility(View.INVISIBLE);
1522     wv.setVisibility(View.INVISIBLE);
1523     status_text.setVisibility(View.VISIBLE);
1524     arrowview.setVisibility(View.VISIBLE);
1525    
1526     status_text.bringToFront();
1527     arrowview.bringToFront();
1528     arrowview.invalidate();
1529     status_text.invalidate();
1530    
1531     break;
1532     }
1533     }
1534    
1535     public Coordinate[] get_visible_area()
1536     {
1537     Coordinate[] ret = new Coordinate[2];
1538     int[] ii = new int[2];
1539     ii[0] = -10;
1540     ii[1] = -10;
1541     // System.out.println(" " + ii[0] + " " + ii[1]);
1542     ret[0] = this.pixmappoint2coord(ii);
1543     // System.out.println(" " + ret[0]);
1544    
1545     ii[0] = this.rose.mCanvasWidth + 10;
1546     ii[1] = this.rose.mCanvasHeight + 10;
1547     // System.out.println(" " + ii[0] + " " + ii[1]);
1548     ret[1] = this.pixmappoint2coord(ii);
1549     // System.out.println(" " + ret[1]);
1550    
1551     return ret;
1552     }
1553    
1554     public Coordinate[] get_visible_area_large()
1555     {
1556     Coordinate[] ret = new Coordinate[2];
1557     int[] ii = new int[2];
1558     ii[0] = -60;
1559     ii[1] = -60;
1560     // System.out.println(" " + ii[0] + " " + ii[1]);
1561     ret[0] = this.pixmappoint2coord(ii);
1562     // System.out.println(" " + ret[0]);
1563    
1564     ii[0] = this.rose.mCanvasWidth + 60;
1565     ii[1] = this.rose.mCanvasHeight + 60;
1566     // System.out.println(" " + ii[0] + " " + ii[1]);
1567     ret[1] = this.pixmappoint2coord(ii);
1568     // System.out.println(" " + ret[1]);
1569    
1570     return ret;
1571     }
1572    
1573     // Sets screen rotation as fixed to current rotation setting
1574     public void mLockScreenRotation()
1575     {
1576     // Stop the screen orientation changing during an event
1577     switch (this.getResources().getConfiguration().orientation)
1578     {
1579     case Configuration.ORIENTATION_PORTRAIT:
1580     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
1581     break;
1582     case Configuration.ORIENTATION_LANDSCAPE:
1583     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
1584     break;
1585     }
1586     }
1587    
1588     // allow screen rotations again
1589     public void mUnlockScreenRotation()
1590     {
1591     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
1592     }
1593    
1594     public void download_caches_in_visible_view(Boolean download_details)
1595     {
1596     if (download_details)
1597     {
1598     this.downloaded_caches = null;
1599     showDialog(2);
1600     }
1601     else
1602     {
1603     this.downloaded_caches = null;
1604     showDialog(0);
1605     }
1606     }
1607    
1608     public void set_bar(Handler h, String title, String text, int cur, int max, Boolean show)
1609     {
1610     Message msg = h.obtainMessage();
1611     Bundle b = new Bundle();
1612     b.putInt("max", max);
1613     b.putInt("cur", cur);
1614     b.putString("title", title);
1615     b.putString("text", text);
1616     b.putBoolean("show", true);
1617     msg.setData(b);
1618     h.sendMessage(msg);
1619     SystemClock.sleep(1);
1620     }
1621    
1622     public void set_bar_slow(Handler h, String title, String text, int cur, int max, Boolean show)
1623     {
1624     Message msg = h.obtainMessage();
1625     Bundle b = new Bundle();
1626     b.putInt("max", max);
1627     b.putInt("cur", cur);
1628     b.putString("title", title);
1629     b.putString("text", text);
1630     b.putBoolean("show", true);
1631     msg.setData(b);
1632     h.sendMessage(msg);
1633     SystemClock.sleep(50);
1634     }
1635    
1636     public void download_caches_from_route_file(Handler h)
1637     {
1638     if (this.route_file_caches == null)
1639     {
1640     return;
1641     }
1642    
1643     // dont go to sleep
1644     this.wl.acquire();
1645    
1646     int count_p = 0;
1647     int max_p = this.route_file_caches.size();
1648     this.mLockScreenRotation();
1649     set_bar(h, "get geocaches", "downloading caches from route file", 0, max_p, true);
1650    
1651     GeocacheCoordinate this_gc = null;
1652     int reopen_after = 30;
1653     int cur = 0;
1654    
1655     this.pv.begin_trans();
1656     try
1657     {
1658    
1659     for (int _cache = 0; _cache < this.route_file_caches.size(); _cache++)
1660     {
1661     this_gc = this.route_file_caches.get(_cache);
1662     set_bar(h, "get geocaches", "downloading caches from route file", _cache, max_p, true);
1663     this.pv.add_point(this_gc);
1664     this.cdol.update_coordinate(this_gc);
1665    
1666     cur++;
1667     if (cur > reopen_after)
1668     {
1669     this.pv.commit();
1670     this.pv.end_trans();
1671     this.pv.reopen_db();
1672     this.pv.begin_trans();
1673     cur = 0;
1674     }
1675    
1676     }
1677     this.pv.commit();
1678     }
1679     finally
1680     {
1681     this.pv.end_trans();
1682     }
1683     set_bar(h, "get geocaches", "finished", max_p, max_p, true);
1684     }
1685    
1686     public void download_caches_in_visible_view_wrapper(Handler h, Boolean download_details)
1687     {
1688     Coordinate[] location = this.get_visible_area();
1689     int count_p = 0;
1690     int max_p;
1691     if (this.rose.zoom > 11)
1692     {
1693     // this is just an estimate!! so that progressbar looks better
1694     max_p = (1 * 4) + (4 * 4);
1695     }
1696     else
1697     {
1698     max_p = (1 * 4) + (4 * 4) + (4 * 4 * 4);
1699     }
1700    
1701     // dont go to sleep
1702     this.wl.acquire();
1703     this.mLockScreenRotation();
1704    
1705     set_bar(h, "get geocaches", "downloading ...", 0, max_p, true);
1706    
1707     HTMLDownloader.get_geocaches_ret ps = null;
1708     ps = wdl.get_geocaches(location, count_p, max_p, 0, h, this.rose.zoom);
1709    
1710     set_bar(h, "get geocaches", "downloading ...", max_p, max_p, true);
1711    
1712     if (ps.points != null)
1713     {
1714    
1715     set_bar(h, "get geocaches", "inserting into DB ...", 0, ps.points.length, true);
1716    
1717     GeocacheCoordinate this_gc = null;
1718    
1719     int reopen_after = 30;
1720     int cur = 0;
1721    
1722     this.pv.begin_trans();
1723     try
1724     {
1725     for (int _cache = 0; _cache < ps.points.length; _cache++)
1726     {
1727     this_gc = ps.points[_cache];
1728     this.pv.add_point(this_gc);
1729     set_bar(h, "get geocaches", "inserting into DB ...", (_cache + 1), ps.points.length, true);
1730    
1731     cur++;
1732     if (cur > reopen_after)
1733     {
1734     this.pv.commit();
1735     this.pv.end_trans();
1736     this.pv.reopen_db();
1737     this.pv.begin_trans();
1738     cur = 0;
1739     }
1740    
1741     }
1742     this.pv.commit();
1743     }
1744     finally
1745     {
1746     this.pv.end_trans();
1747     }
1748    
1749     set_bar(h, "get geocaches", "inserting into DB ...", ps.points.length, ps.points.length, true);
1750    
1751     set_bar(h, "get geocaches", "parsing ...", 0, ps.points.length, true);
1752    
1753     this.downloaded_caches = new ArrayList<GeocacheCoordinate>();
1754     for (int _cache = 0; _cache < ps.points.length; _cache++)
1755     {
1756     this_gc = ps.points[_cache];
1757     this.downloaded_caches.add(this_gc);
1758     set_bar(h, "get geocaches", "parsing ...", (_cache + 1), ps.points.length, true);
1759     }
1760    
1761     if (download_details)
1762     {
1763     // now download cache details 1-by-1
1764     // this is going to be slow!!!!! so watch out!
1765    
1766     reopen_after = 15;
1767     cur = 0;
1768    
1769     long timestamp1 = 0;
1770     long timestamp2 = 0;
1771     float remain = 0;
1772     float timestamp3 = 0;
1773    
1774     timestamp1 = SystemClock.elapsedRealtime();
1775     this.pv.compact();
1776     set_bar(h, "get geocaches", "downloading details ...", 0, this.downloaded_caches.size(), true);
1777     this.pv.begin_trans();
1778     try
1779     {
1780    
1781     for (int _cache = 0; _cache < this.downloaded_caches.size(); _cache++)
1782     {
1783     timestamp2 = SystemClock.elapsedRealtime();
1784     this_gc = this.downloaded_caches.get(_cache);
1785     this.cdol.update_coordinate(this_gc);
1786    
1787     cur++;
1788     if (cur > reopen_after)
1789     {
1790     this.pv.commit();
1791     this.pv.end_trans();
1792     this.pv.reopen_db();
1793     this.pv.begin_trans();
1794     cur = 0;
1795     }
1796     timestamp3 = ((float) SystemClock.elapsedRealtime() - (float) timestamp2) / 1000f;
1797     remain = ((SystemClock.elapsedRealtime() - timestamp1) / (_cache + 1)) * (this.downloaded_caches.size() - (_cache + 1)) / 1000;
1798     set_bar(h, "get geocaches", String.format("this: %.1f s, remaining: %.0f s", timestamp3, remain), (_cache + 1), this.downloaded_caches.size(), true);
1799     }
1800     this.pv.commit();
1801     }
1802     finally
1803     {
1804     this.pv.end_trans();
1805     }
1806     set_bar(h, "get geocaches", "finished", this.downloaded_caches.size(), this.downloaded_caches.size(), true);
1807     }
1808     else
1809     {
1810     set_bar(h, "get geocaches", "finished", ps.points.length, ps.points.length, true);
1811     }
1812     }
1813     }
1814    
1815     public Handler toast_handler = new Handler()
1816     {
1817     @Override
1818     public void handleMessage(Message msg44)
1819     {
1820     if (msg44.getData().getInt("command") == 1)
1821     {
1822     toast_me(msg44.getData().getString("text"), msg44.getData().getInt("duration"));
1823     }
1824     }
1825     };
1826    
1827     public Handler dl_handler = new Handler()
1828     {
1829     @Override
1830     public void handleMessage(Message msg33)
1831     {
1832     // System.out.println("msg33");
1833     if (msg33.getData().getInt("command") == 1)
1834     {
1835     removeDialog(1);
1836     }
1837     else if (msg33.getData().getInt("command") == 2)
1838     {
1839     cacheview.invalidate();
1840     }
1841     else
1842     {
1843     showDialog(1);
1844     }
1845     }
1846     };
1847    
1848     private Handler progress_handler = new Handler()
1849     {
1850     public void handleMessage(Message msg22)
1851     {
1852     pbarDialog.setMax(msg22.getData().getInt("max"));
1853     pbarDialog.setProgress(msg22.getData().getInt("cur"));
1854     pbarDialog.setTitle(msg22.getData().getString("title"));
1855     pbarDialog.setMessage(msg22.getData().getString("text"));
1856     // System.out.println("msg: "
1857     // + msg22.getData().getInt("cur") + " "
1858     // + msg22.getData().getInt("max") + " "
1859     // + msg22.getData().getString("text"));
1860     // if (!msg22.getData().getBoolean("show"))
1861     // {
1862     // dismissDialog(0);
1863     // }
1864     }
1865     };
1866    
1867     Handler gcv_Refresh = new Handler()
1868     {
1869     @Override
1870     public void handleMessage(Message msg)
1871     {
1872     switch (msg.what)
1873     {
1874     case 0:
1875     // gcview.set_loaded_caches(downloaded_caches);
1876     rose.load_caches_from_db();
1877     break;
1878     }
1879     }
1880     };
1881    
1882     protected Dialog onCreateDialog(int id)
1883     {
1884     switch (id)
1885     {
1886     case 0:
1887     pbarDialog = new ProgressDialog(this);
1888     pbarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
1889     pbarDialog.setTitle("--");
1890     pbarDialog.setMessage("--");
1891     pbarDialog.setCancelable(true);
1892     pbarDialog.setProgress(0);
1893     pbarDialog.setMax(200);
1894     progressThread = new ProgressThread(progress_handler, 0);
1895     progressThread.start();
1896     return pbarDialog;
1897     case 1:
1898     ProgressDialog xDialog = new ProgressDialog(this);
1899     // xDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
1900     xDialog.setTitle("Geocache");
1901     xDialog.setMessage("downloading details from internet");
1902     xDialog.setCancelable(false);
1903     xDialog.setIndeterminate(true);
1904     // xDialog.setProgress(0);
1905     // xDialog.setMax(1);
1906     return xDialog;
1907     case 2:
1908     pbarDialog = new ProgressDialog(this);
1909     pbarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
1910     pbarDialog.setTitle("--");
1911     pbarDialog.setMessage("--");
1912     pbarDialog.setCancelable(true);
1913     pbarDialog.setProgress(0);
1914     pbarDialog.setMax(200);
1915     progressThread = new ProgressThread(progress_handler, 1);
1916     progressThread.start();
1917     return pbarDialog;
1918     case 3:
1919     pbarDialog = new ProgressDialog(this);
1920     pbarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
1921     pbarDialog.setTitle("--");
1922     pbarDialog.setMessage("--");
1923     pbarDialog.setCancelable(true);
1924     pbarDialog.setProgress(0);
1925     pbarDialog.setMax(200);
1926     progressThread = new ProgressThread(progress_handler, 3);
1927     progressThread.start();
1928     return pbarDialog;
1929     default:
1930     return null;
1931     }
1932     }
1933    
1934     private class ProgressThread extends Thread
1935     {
1936     Handler mHandler;
1937     int with_details = 0;
1938    
1939     ProgressThread(Handler h, int with_details)
1940     {
1941     this.mHandler = h;
1942     this.with_details = with_details;
1943     }
1944    
1945     public void run()
1946     {
1947     try
1948     {
1949     pv.compact();
1950     }
1951     catch (Exception e)
1952     {
1953     e.printStackTrace();
1954     }
1955    
1956     if (with_details == 0)
1957     {
1958     download_caches_in_visible_view_wrapper(mHandler, false);
1959     dismissDialog(0);
1960     removeDialog(0);
1961     }
1962     else if (with_details == 1)
1963     {
1964     download_caches_in_visible_view_wrapper(mHandler, true);
1965     dismissDialog(2);
1966     removeDialog(2);
1967     }
1968     else if (with_details == 3)
1969     {
1970     download_caches_from_route_file(mHandler);
1971     dismissDialog(3);
1972     removeDialog(3);
1973     }
1974     // put the caches into the correct List
1975     gcv_Refresh.sendEmptyMessage(0);
1976     mUnlockScreenRotation();
1977     // allow sleep again
1978     wl.release();
1979     }
1980     }
1981    
1982     @Override
1983     public void onResume()
1984     {
1985     super.onResume();
1986     // System.out.println("main: resume");
1987    
1988     // mgpsl = new MyGPSListener();
1989    
1990     this.load_settings();
1991     this.wdl.loadCookies();
1992    
1993     if (mdl.running != true)
1994     {
1995     // System.out.println(mdl.getState());
1996     if (mdl.getState() == Thread.State.TERMINATED)
1997     {
1998     this.mdl = null;
1999     this.mdl = new MapDownloader(global_messageHandler, this);
2000     this.mdl.start();
2001     // System.out.println(mdl.getState());
2002     }
2003     }
2004    
2005     // assume we have no gps fix after resume
2006     isGPSFix = false;
2007    
2008     File dir1 = new File(this.db_path);
2009     File dir2 = new File(dir1.getParent());
2010     dir2.mkdirs();
2011     this.pv = null;
2012     this.pv = new PointProvider(this.db_path, this.mdl, "", "geocaches");
2013     // this.pv.set_filter("type='" + GeocacheCoordinate.TYPE_REGULAR + "'");
2014    
2015     // !!!!!!!!!!! DEVELOPMENT !!!!!!!!!!
2016     // !!!!!!!!!!! DEVELOPMENT !!!!!!!!!!
2017     // !!!!!!!!!!! DEVELOPMENT !!!!!!!!!!
2018     File dir3 = new File(dir2.toString() + "/zoff.txt");
2019     if (dir3.exists())
2020     {
2021     this.__ZOFF_PHONE__ = true;
2022     }
2023     // !!!!!!!!!!! DEVELOPMENT !!!!!!!!!!
2024     // !!!!!!!!!!! DEVELOPMENT !!!!!!!!!!
2025     // !!!!!!!!!!! DEVELOPMENT !!!!!!!!!!
2026    
2027     // start listening for GPS
2028     this.turn_on_locations();
2029    
2030     // start compass
2031     this.turn_on_compass();
2032    
2033     // System.out.println("x1=" + this.cacheview.gc_name_current);
2034     // System.out.println("x2=" + this.cacheview.gc_name_previous);
2035     // System.out.println("x3=" + this.cacheview.details_loaded);
2036     // System.out.println("x4=" + String.valueOf(this.cacheview.gc));
2037    
2038     this.rose.load_caches_from_db();
2039     }
2040    
2041     protected void onActivityResult(int requestCode, int resultCode, Intent data)
2042     {
2043     switch (requestCode)
2044     {
2045     case 7:
2046     try
2047     {
2048     String value = data.getStringExtra("username");
2049     if (value != null && value.length() > 0)
2050     {
2051     this.global_settings.options_username = value;
2052     // System.out.println("u=" + String.valueOf(value));
2053     }
2054    
2055     value = data.getStringExtra("password");
2056     if (value != null && value.length() > 0)
2057     {
2058     this.global_settings.options_password = value;
2059     // System.out.println("p=" + String.valueOf(value));
2060     }
2061     }
2062     catch (Exception e)
2063     {
2064     }
2065    
2066     this.save_settings();
2067    
2068     break;
2069     case 8:
2070     try
2071     {
2072     String value2 = data.getStringExtra("msg");
2073     int value3 = data.getIntExtra("logtype", GeocacheCoordinate.LOG_AS_FOUND);
2074    
2075     if (value2 != null && value2.length() > 0)
2076     {
2077     System.out.println("fieldnote=" + String.valueOf(value2));
2078     Calendar cal = Calendar.getInstance();
2079     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
2080     sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
2081     GeocacheCoordinate temp_gc = this.rose.current_target;
2082     System.out.println("temp_gc=" + String.valueOf(temp_gc));
2083     temp_gc.fieldnotes = value2;
2084     temp_gc.log_date = sdf.format(cal.getTime());
2085    
2086     // set according to selected value!!
2087     if (value3 == GeocacheCoordinate.LOG_AS_FOUND)
2088     {
2089     temp_gc.aagtl_status = GeocacheCoordinate.AAGTL_STATUS_FOUND;
2090     temp_gc.found = true;
2091     temp_gc.log_as = value3;
2092     }
2093     else if (value3 == GeocacheCoordinate.LOG_AS_NOTFOUND)
2094     {
2095     temp_gc.aagtl_status = GeocacheCoordinate.AAGTL_STATUS_NORMAL;
2096     temp_gc.found = false;
2097     temp_gc.log_as = value3;
2098     }
2099     else if (value3 == GeocacheCoordinate.LOG_AS_NOTE)
2100     {
2101     temp_gc.aagtl_status = GeocacheCoordinate.AAGTL_STATUS_NORMAL;
2102     temp_gc.found = false;
2103     temp_gc.log_as = value3;
2104     }
2105     // set according to selected value!!
2106    
2107     // System.out.println("v3=" + value3 + " f=" + temp_gc.found
2108     // + " as="
2109     // + temp_gc.aagtl_status);
2110    
2111     this.pv.reopen_db();
2112     this.pv.compact();
2113     this.pv.begin_trans();
2114     try
2115     {
2116     this.pv.add_point_fn(temp_gc);
2117     this.pv.commit();
2118     }
2119     finally
2120     {
2121     this.pv.end_trans();
2122     }
2123     this.pv.close();
2124     }
2125    
2126     }
2127     catch (Exception e)
2128     {
2129     System.out.println("Error saving fieldnote!");
2130     e.printStackTrace();
2131     }
2132    
2133     break;
2134     default:
2135     break;
2136     }
2137     }
2138    
2139     public void onClickXXX(DialogInterface d, int i)
2140     {
2141     // Perform action on clicks
2142     // Toast.makeText(HelloFormStuff.this, "Beep Bop",
2143     // Toast.LENGTH_SHORT).show();
2144     System.out.println("button pressed");
2145     }
2146    
2147     public void turn_on_compass()
2148     {
2149     try
2150     {
2151     sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
2152     }
2153     catch (Exception e)
2154     {
2155     e.printStackTrace();
2156     }
2157     }
2158    
2159     public void turn_off_compass()
2160     {
2161     try
2162     {
2163     sensorManager.unregisterListener(this);
2164     }
2165     catch (Exception e)
2166     {
2167     e.printStackTrace();
2168     }
2169     }
2170    
2171     @Override
2172     protected void onPause()
2173     {
2174     // System.out.println(lx.getHeight());
2175     // System.out.println(rose.getHeight());
2176     // System.out.println(cross.getHeight());
2177     // System.out.println(status_text.getHeight());
2178    
2179     // System.out.println("main: pause");
2180    
2181     // stop listening for GPS
2182     this.turn_off_locations();
2183     // stop compass
2184     this.turn_off_compass();
2185    
2186     this.save_settings();
2187     this.wdl.saveCookies();
2188    
2189     if (mdl != null)
2190     {
2191     mdl.interrupt();
2192     mdl.request_stop();
2193     try
2194     {
2195     mdl.join();
2196     // System.out.println(mdl.getState());
2197     // System.out.println("join mdl");
2198     }
2199     catch (InterruptedException e)
2200     {
2201     e.printStackTrace();
2202     }
2203     }
2204    
2205     // close sql db
2206     pv.compact();
2207     pv.close();
2208    
2209     super.onPause();
2210     }
2211    
2212     public void turn_off_locations()
2213     {
2214     try
2215     {
2216     // stop listening (for any type of location)
2217     lm.removeUpdates(this);
2218     lm.removeGpsStatusListener(this);
2219     // we cant have a fix now
2220     isGPSFix = false;
2221     }
2222     catch (Exception e)
2223     {
2224     e.printStackTrace();
2225     }
2226     }
2227    
2228     public void turn_on_locations()
2229     {
2230     // start with GPS
2231     this.turn_on_gps();
2232     }
2233    
2234     public void turn_on_follow_mode()
2235     {
2236     this.follow_mode = true;
2237     this.follow_current = this.follow_on;
2238     }
2239    
2240     public void turn_off_follow_mode()
2241     {
2242     this.follow_mode = false;
2243     this.follow_current = this.follow_off;
2244     }
2245    
2246     public void turn_off_gps()
2247     {
2248     try
2249     {
2250    
2251     // stop listening for GPS
2252     lm.removeUpdates(this);
2253     lm.removeGpsStatusListener(this);
2254     // we cant have a fix now
2255     isGPSFix = false;
2256    
2257     // start listening for network location
2258     lm.requestLocationUpdates(this.low.getName(), 0L, 0f, this);
2259     }
2260     catch (Exception e)
2261     {
2262     e.printStackTrace();
2263     }
2264     }
2265    
2266     public void turn_on_gps()
2267     {
2268     try
2269     {
2270     // stop listening for network location
2271     lm.removeUpdates(this);
2272     // assume we have no gps fix after turing on gps
2273     isGPSFix = false;
2274    
2275     // start listening for GPS
2276     lm.requestLocationUpdates(this.high.getName(), 0L, 0f, this);
2277     lm.addGpsStatusListener(this);
2278     }
2279     catch (Exception e)
2280     {
2281     e.printStackTrace();
2282     }
2283     }
2284    
2285     public void onLocationChanged(Location location)
2286     {
2287     Boolean must_reload_caches_from_db = false;
2288    
2289     if (location == null) return;
2290    
2291     if (!follow_mode) return;
2292    
2293     // for external bluetooth GPS -----------------
2294     Boolean old_fix_status = isGPSFix;
2295     if (mLastLocation != null) isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;
2296     if (isGPSFix.compareTo(old_fix_status) != 0)
2297     {
2298     cross.invalidate();
2299     }
2300     mLastLocationMillis = SystemClock.elapsedRealtime();
2301     // for external bluetooth GPS -----------------
2302    
2303     mLastLocation = location;
2304     // check distance that we moved
2305     if ((cur_lat_gps_save == -1) || (cur_lon_gps_save == -1))
2306     {
2307     // System.out.println("gps 1");
2308     // first fix
2309     cur_lat_gps_save = location.getLatitude();
2310     cur_lon_gps_save = location.getLongitude();
2311     }
2312     else
2313     {
2314     // System.out.println("gps 2");
2315    
2316     // lat (48.2200 -> 48.2201) 0.0001 =~ 11.12m
2317     // lon (16.4000 -> 16.4002) 0.0002 =~ 14.82m
2318    
2319     double my_pos_delta_lat;
2320     double my_pos_delta_lon;
2321    
2322     my_pos_delta_lat = (double) (0.0001 * 4 * (20 - this.rose.zoom));
2323     my_pos_delta_lon = (double) (0.0002 * 4 * (20 - this.rose.zoom));
2324    
2325     // // lat,lon 1
2326     // Coordinate a = this.rose.num2deg(this.rose.map_center_x,
2327     // this.rose.map_center_y);
2328     // // lat,lon 2
2329     // Coordinate b = this.rose.num2deg(this.rose.map_center_x +
2330     // (this.rose.tile_size_x * 0.04),
2331     // this.rose.map_center_y + (this.rose.tile_size_y * 0.04));
2332     // // distance lat for 1 maptile (from pixel -> lat)
2333     // my_pos_delta_lat = Math.abs(b.lat - a.lat);
2334     // // distance lon for 1 maptile (from pixel -> lon)
2335     // my_pos_delta_lon = Math.abs(b.lon - a.lon);
2336     // // System.out.println("" + my_pos_delta_lat + " " +
2337     // my_pos_delta_lon);
2338    
2339     if (Math.abs(cur_lat_gps_save - location.getLatitude()) > my_pos_delta_lat)
2340     {
2341     // System.out.println("gps 3");
2342    
2343     must_reload_caches_from_db = true;
2344     // remember this location
2345     cur_lat_gps_save = location.getLatitude();
2346     cur_lon_gps_save = location.getLongitude();
2347     }
2348     else if (Math.abs(cur_lon_gps_save - location.getLongitude()) > my_pos_delta_lon)
2349     {
2350     // System.out.println("gps 4");
2351    
2352     must_reload_caches_from_db = true;
2353     // remember this location
2354     cur_lat_gps_save = location.getLatitude();
2355     cur_lon_gps_save = location.getLongitude();
2356     }
2357     }
2358    
2359     // update mapview
2360     this.rose.set_center(new Coordinate(location.getLatitude(), location.getLongitude()));
2361     this.rose.__calc_tiles_on_display();
2362     if (must_reload_caches_from_db)
2363     {
2364     // System.out.println("gps: reload caches from db");
2365     this.rose.load_caches_from_db();
2366     }
2367     if (this.current_display_view == DISPLAY_VIEW_MAP)
2368     {
2369     // System.out.println("iigc1");
2370     this.rose.main_object.gcview.invalidate();
2371     }
2372     this.rose.draw_me();
2373     // update mapview
2374    
2375     if (!this.global_settings.options_use_compass_heading)
2376     {
2377     // System.out.println("-> gps heading");
2378     this.cross.set_gps_heading(location.getBearing());
2379     }
2380    
2381     this.cross.set_gps_acc(location.getAccuracy());
2382     this.cross.invalidate();
2383    
2384     if (this.current_display_view == DISPLAY_VIEW_ARROW)
2385     {
2386     // now redraw arrow (if visible)
2387     this.arrowview.invalidate();
2388     }
2389    
2390     // status_text.setText(String.format("lat %.5f", location.getLatitude())
2391     // + " "
2392     // + String.format("lon %.5f", location.getLongitude()));
2393     this.change_status_text(String.format("lat %.5f", location.getLatitude()) + " " + String.format("lon %.5f", location.getLongitude()));
2394     // status_text.append(" " + String.format("%.1f",
2395     // this.rose.map_center_x) + " "
2396     // + String.format("%.1f", this.rose.map_center_y));
2397    
2398     // sb.append("Timestamp: ");
2399     // sb.append(location.getTime());
2400     // sb.append('\n');
2401    
2402     // System.out.println(sb.toString());
2403    
2404     }
2405    
2406     private Handler handle_status_text = new Handler()
2407     {
2408     public void handleMessage(Message msg)
2409     {
2410     Bundle b = msg.getData();
2411     int id = b.getInt("id");
2412     if (id == 0)
2413     {
2414     change_status_text_real(b.getString("text"));
2415     }
2416     else if (id == 1)
2417     {
2418     append_status_text_real(b.getString("text"));
2419     }
2420     }
2421     };
2422    
2423     public void change_status_text(String new_text)
2424     {
2425     Message msg = handle_status_text.obtainMessage();
2426     Bundle b = new Bundle();
2427     b.putString("text", new_text);
2428     b.putInt("id", 0);
2429     msg.setData(b);
2430     handle_status_text.sendMessage(msg);
2431     }
2432    
2433     public void change_status_text_real(String new_text)
2434     {
2435     this.status_text_string = new_text;
2436     status_text.setText(this.status_text_string + " " + this.status_append_string);
2437     this.status_text.postInvalidate();
2438     }
2439    
2440     public void append_status_text(String new_text)
2441     {
2442     Message msg = handle_status_text.obtainMessage();
2443     Bundle b = new Bundle();
2444     b.putString("text", new_text);
2445     b.putInt("id", 1);
2446     msg.setData(b);
2447     handle_status_text.sendMessage(msg);
2448     }
2449    
2450     public void append_status_text_real(String new_text)
2451     {
2452     this.status_append_string = new_text;
2453     status_text.setText(this.status_text_string + " " + this.status_append_string);
2454     this.status_text.postInvalidate();
2455     }
2456    
2457     public void onProviderDisabled(String provider)
2458     {
2459     // status_text.setText("onProviderDisabled");
2460     // System.out.println("onProviderDisabled");
2461     }
2462    
2463     public void onProviderEnabled(String provider)
2464     {
2465     // status_text.setText("onProviderEnabled");
2466     // System.out.println("onProviderEnabled");
2467     }
2468    
2469     public void onStatusChanged(String provider, int status, Bundle extras)
2470     {
2471     // status_text.setText("onStatusChanged " + String.valueOf(status) + " "
2472     // + extras.toString());
2473     // ** good ** // System.out.println("onStatusChanged " +
2474     // String.valueOf(status) + " " + extras.toString());
2475     // System.out.println(extras.toString());
2476     // GpsStatus.GPS_EVENT_SATELLITE_STATUS -> 4
2477     // GpsStatus.GPS_EVENT_STARTED -> 1
2478     // GpsStatus.GPS_EVENT_STOPPED; -> 2
2479     }
2480    
2481     public void onGpsStatusChanged(int event)
2482     {
2483     GpsStatus stat = lm.getGpsStatus(null);
2484     Iterable<GpsSatellite> iSatellites = stat.getSatellites();
2485     // System.out.println("" + String.valueOf(iSatellites.toString()));
2486     Iterator<GpsSatellite> it = iSatellites.iterator();
2487    
2488     int used_sats_new = 0;
2489     while (it.hasNext())
2490     {
2491     GpsSatellite oSat = (GpsSatellite) it.next();
2492     if (oSat.usedInFix())
2493     {
2494     used_sats_new++;
2495     }
2496     // System.out.println("SAT-info: " + oSat.toString());
2497     }
2498     if (this.used_sats != used_sats_new)
2499     {
2500     this.used_sats = used_sats_new;
2501     cross.set_used_sats(used_sats);
2502     cross.invalidate();
2503     }
2504    
2505     // for INTERNAL GPS -----------------
2506     Boolean old_fix_status = isGPSFix;
2507     switch (event)
2508     {
2509     case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
2510     if (mLastLocation != null) isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;
2511    
2512     if (isGPSFix.compareTo(old_fix_status) != 0)
2513     {
2514     cross.invalidate();
2515     }
2516     old_fix_status = isGPSFix;
2517    
2518     if (isGPSFix)
2519     { // A fix has been acquired.
2520     // Do something.
2521     // System.out.println("FIX found ##");
2522     }
2523     else
2524     { // The fix has been lost.
2525     // Do something.
2526     // System.out.println("lost FIX ==");
2527     }
2528    
2529     break;
2530     case GpsStatus.GPS_EVENT_FIRST_FIX:
2531     // Do something.
2532     isGPSFix = true;
2533     // System.out.println("first FIX **");
2534    
2535     break;
2536     }
2537     // for INTERNAL GPS -----------------
2538     }
2539    
2540     public void upload_fieldnotes()
2541     {
2542     List<GeocacheCoordinate> caches = this.pv.get_new_fieldnotes();
2543     if ((caches == null) || (caches.size() == 0))
2544     {
2545     // no filednotes to upload
2546     Toast.makeText(getApplicationContext(), "No Fieldnotes to upload", Toast.LENGTH_SHORT).show();
2547    
2548     }
2549     else
2550     {
2551     FieldnotesUploader fu = new FieldnotesUploader(this.wdl, caches);
2552     boolean ret = fu.upload();
2553     if (ret)
2554     {
2555     Toast.makeText(getApplicationContext(), "" + caches.size() + " Fieldnotes uploaded", Toast.LENGTH_SHORT).show();
2556     }
2557     else
2558     {
2559     Toast.makeText(getApplicationContext(), "Error while uploading Fieldnotes", Toast.LENGTH_SHORT).show();
2560     }
2561     }
2562     }
2563    
2564     public void get_route_files()
2565     {
2566     File folder = new File(this.main_dir + "/routes/");
2567     File[] listOfFiles = folder.listFiles();
2568     this.current_routefile_name = null;
2569     this.route_file_items = new CharSequence[listOfFiles.length + 1];
2570    
2571     for (int i = 0; i < listOfFiles.length; i++)
2572     {
2573     if (listOfFiles[i].isFile())
2574     {
2575     System.out.println("File " + listOfFiles[i].getName());
2576     // current_routefile_name = listOfFiles[i].getName();
2577     this.route_file_items[i] = listOfFiles[i].getName();
2578     }
2579     else if (listOfFiles[i].isDirectory())
2580     {
2581     // System.out.println("Directory " +
2582     // listOfFiles[i].getName());
2583     }
2584     }
2585     // this on to clear "routes"
2586     this.route_file_items[listOfFiles.length] = "--None--";
2587    
2588     AlertDialog.Builder builder = new AlertDialog.Builder(this);
2589     builder.setTitle("Pick a Routefile");
2590     builder.setItems(route_file_items, new DialogInterface.OnClickListener()
2591     {
2592     public void onClick(DialogInterface dialog, int j)
2593     {
2594     current_routefile_name = route_file_items[j].toString();
2595     System.out.println("crfn1=" + current_routefile_name);
2596     Toast.makeText(getApplicationContext(), "Routefile selected: " + route_file_items[j].toString(), Toast.LENGTH_SHORT).show();
2597    
2598     if (route_file_items[j].toString().equals("--None--"))
2599     {
2600     route_file_caches = null;
2601     pv.clear_filter();
2602     // load caches from DB, so that filter will be cleared
2603     rose.load_caches_from_db();
2604     return;
2605     }
2606    
2607     try
2608     {
2609     // Open the file
2610     FileInputStream fstream = new FileInputStream(main_dir + "/routes/" + current_routefile_name);
2611     // Get the object of DataInputStream
2612     DataInputStream in = new DataInputStream(fstream);
2613     BufferedReader br = new BufferedReader(new InputStreamReader(in));
2614     String strLine;
2615     route_file_caches = new ArrayList<GeocacheCoordinate>();
2616     route_file_caches.clear();
2617     String new_pv_filter = "";
2618     String sep = "";
2619     // Read File Line By Line
2620     while ((strLine = br.readLine()) != null)
2621     {
2622     strLine = strLine.trim();
2623     if (!strLine.equals(""))
2624     {
2625     // Print the content on the console
2626     // System.out.println(strLine);
2627     // add GC to global list for route caches
2628     route_file_caches.add(new GeocacheCoordinate(0.0, 0.0, strLine));
2629     new_pv_filter = new_pv_filter + sep + " name='" + strLine + "' ";
2630     if (sep.equals(""))
2631     {
2632     sep = " or ";
2633     }
2634     }
2635     }
2636     // Close the input stream
2637     in.close();
2638    
2639     // System.out.println("new filter=" + new_pv_filter);
2640     pv.set_filter(new_pv_filter);
2641     // load caches from DB, so that filter will be active
2642     rose.load_caches_from_db();
2643     }
2644     catch (Exception e)
2645     {
2646     // System.err.println("Error: " + e.getMessage());
2647     route_file_caches = null;
2648     pv.clear_filter();
2649     // load caches from DB, so that filter will be cleared
2650     rose.load_caches_from_db();
2651     }
2652     alert.dismiss();
2653     }
2654     });
2655     this.alert = builder.create();
2656     this.alert.show(); // this will return immediately!!
2657     }
2658    
2659     public void change_target_coords_lat()
2660     {
2661     int num1 = 0;
2662     int num2 = 0;
2663     int num3 = 0;
2664     Boolean ns_ew_toggle = true;
2665     String toggle_text_on = "N";
2666     String toggle_text_off = "S";
2667    
2668     Coordinate.coords_d_m_m conv = this.rose.current_target.d_to_dm_m();
2669     // Coordinate.dm_m_to_d(conv);
2670    
2671     num1 = conv.lat_deg;
2672     num2 = conv.lat_min;
2673     num3 = conv.lat_min_fractions;
2674     ns_ew_toggle = true;
2675     if (!conv.lat_plus_minus)
2676     {
2677     ns_ew_toggle = false;
2678    
2679     }
2680    
2681     this.show_geocoord_picker(true, num1, num2, num3, ns_ew_toggle, toggle_text_on, toggle_text_off);
2682     }
2683    
2684     public void change_target_coords_lon()
2685     {
2686     int num1 = 0;
2687     int num2 = 0;
2688     int num3 = 0;
2689     Boolean ns_ew_toggle = true;
2690     String toggle_text_on = "E";
2691     String toggle_text_off = "W";
2692    
2693     Coordinate.coords_d_m_m conv = this.rose.current_target.d_to_dm_m();
2694     // Coordinate.dm_m_to_d(conv);
2695    
2696     num1 = conv.lon_deg;
2697     num2 = conv.lon_min;
2698     num3 = conv.lon_min_fractions;
2699     ns_ew_toggle = true;
2700     if (!conv.lon_plus_minus)
2701     {
2702     ns_ew_toggle = false;
2703    
2704     }
2705    
2706     this.show_geocoord_picker(false, num1, num2, num3, ns_ew_toggle, toggle_text_on, toggle_text_off);
2707     }
2708    
2709     public void show_geocoord_picker(Boolean is_lat, int num1, int num2, int num3, Boolean toggle, String t_on, String t_off)
2710     {
2711     this.is_lat_current_change_target_coords = is_lat;
2712    
2713     xx1 = new NumberPicker(this);
2714     xx1.setFormatter(NumberPicker.THREE_DIGIT_FORMATTER);
2715     xx1.setRange(0, 999);
2716     xx1.setCurrent(num1);
2717     RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(150, RelativeLayout.LayoutParams.FILL_PARENT);
2718     lp1.topMargin = 20;
2719     lp1.leftMargin = 5;
2720     lp1.height = 200;
2721     lp1.width = (int) (this.rose.mCanvasWidth * 0.3); // calc width
2722     lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
2723     mainscreen_map_view.addView(xx1, lp1);
2724    
2725     orient_1_toggle = new ToggleButton(this);
2726     RelativeLayout.LayoutParams otb_1 = new RelativeLayout.LayoutParams(150, RelativeLayout.LayoutParams.FILL_PARENT);
2727     orient_1_toggle.setChecked(toggle);
2728     if (toggle)
2729     {
2730     orient_1_toggle.setText(t_on);
2731     }
2732     else
2733     {
2734     orient_1_toggle.setText(t_off);
2735     }
2736     orient_1_toggle.setTextOff(t_off);
2737     orient_1_toggle.setTextOn(t_on);
2738     otb_1.topMargin = 20 + 2;
2739     otb_1.rightMargin = 1;
2740     otb_1.leftMargin = 4 + lp1.width;
2741     otb_1.height = 200 - 10;
2742     otb_1.width = 55;
2743     otb_1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
2744     mainscreen_map_view.addView(orient_1_toggle, otb_1);
2745    
2746     // ToggleButton orient_2_toggle = new ToggleButton(this);
2747     // RelativeLayout.LayoutParams otb_2 = new
2748     // RelativeLayout.LayoutParams(150,
2749     // RelativeLayout.LayoutParams.FILL_PARENT);
2750     // orient_2_toggle.setChecked(false);
2751     // orient_2_toggle.setText("S");
2752     // orient_2_toggle.setTextOff("S");
2753     // orient_2_toggle.setTextOn("S");
2754     // otb_2.topMargin = 20 + 100;
2755     // otb_2.leftMargin = 5 + lp1.width;
2756     // otb_2.height = 100;
2757     // otb_2.width = 45;
2758     // otb_2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
2759     // mainscreen_map_view.addView(orient_2_toggle, otb_2);
2760    
2761     xx2 = new NumberPicker(this);
2762     xx2.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
2763     xx2.setRange(0, 99);
2764     xx2.setCurrent(num2);
2765     RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(150, RelativeLayout.LayoutParams.FILL_PARENT);
2766     lp2.rightMargin = 5;
2767     lp2.topMargin = 20;
2768     lp2.height = 200;
2769     lp2.leftMargin = lp1.width + 2 + 55; // width of other number picker
2770     lp2.width = 110;
2771     lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
2772     mainscreen_map_view.addView(xx2, lp2);
2773    
2774     xx3 = new NumberPicker(this);
2775     xx3.setFormatter(NumberPicker.THREE_DIGIT_FORMATTER);
2776     xx3.setRange(0, 999);
2777     xx3.setCurrent(num3);
2778     RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(150, RelativeLayout.LayoutParams.FILL_PARENT);
2779     lp3.rightMargin = 5;
2780     lp3.topMargin = 20;
2781     lp3.height = 200;
2782     lp3.leftMargin = lp1.width + lp2.width + 2 + 55; // width of other
2783     // number picker
2784     lp3.width = this.rose.mCanvasWidth - lp1.width - lp2.width - 5 - 55; // take
2785     // rest
2786     // of
2787     // width
2788     lp3.addRule(RelativeLayout.ALIGN_PARENT_TOP);
2789     mainscreen_map_view.addView(xx3, lp3);
2790    
2791     RelativeLayout.LayoutParams bt_ok = new RelativeLayout.LayoutParams(150, RelativeLayout.LayoutParams.FILL_PARENT);
2792     btn_ok = new Button(this);
2793     bt_ok.width = ((int) (this.rose.mCanvasWidth * 0.4));
2794     bt_ok.height = 70;
2795     bt_ok.leftMargin = 5;
2796     bt_ok.topMargin = 20 + 3 + lp2.height;
2797     btn_ok.setText("OK");
2798     bt_ok.addRule(RelativeLayout.ALIGN_PARENT_TOP);
2799     this.btn_ok.setOnClickListener(new OnClickListener()
2800     {
2801     public void onClick(View v)
2802     {
2803     System.out.println("toggle=" + orient_1_toggle.isChecked());
2804     System.out.println("xx1=" + xx1.getCurrent());
2805     System.out.println("xx2=" + xx2.getCurrent());
2806     System.out.println("xx3=" + xx3.getCurrent());
2807    
2808     Coordinate ccc = new Coordinate(2, 2);
2809     Coordinate.coords_d_m_m conv2 = new Coordinate.coords_d_m_m();
2810     if (is_lat_current_change_target_coords)
2811     {
2812     conv2.lat_deg = xx1.getCurrent();
2813     conv2.lat_min = xx2.getCurrent();
2814     conv2.lat_min_fractions = xx3.getCurrent();
2815     if (orient_1_toggle.isChecked())
2816     {
2817     conv2.lat_plus_minus = true;
2818     conv2.lat_sign = "+";
2819     }
2820     else
2821     {
2822     conv2.lat_plus_minus = false;
2823     conv2.lat_sign = "-";
2824     }
2825     ccc = Coordinate.dm_m_to_d(conv2);
2826     ccc.lon = rose.current_target.lon;
2827     }
2828     else
2829     {
2830     conv2.lon_deg = xx1.getCurrent();
2831     conv2.lon_min = xx2.getCurrent();
2832     conv2.lon_min_fractions = xx3.getCurrent();
2833     if (orient_1_toggle.isChecked())
2834     {
2835     conv2.lon_plus_minus = true;
2836     conv2.lon_sign = "+";
2837     }
2838     else
2839     {
2840     conv2.lon_plus_minus = false;
2841     conv2.lon_sign = "-";
2842     }
2843     ccc = Coordinate.dm_m_to_d(conv2);
2844     ccc.lat = rose.current_target.lat;
2845     }
2846     System.out.println("ccc lat=" + ccc.lat);
2847     System.out.println("ccc lon=" + ccc.lon);
2848     // now set new target coords
2849     GeocacheCoordinate tmp_gc2 = new GeocacheCoordinate(ccc.lat, ccc.lon, "*GCmanual*");
2850     tmp_gc2.title = "*manual target";
2851     rose.current_target = tmp_gc2;
2852    
2853     mainscreen_map_view.removeView(btn_cancel);
2854     mainscreen_map_view.removeView(btn_ok);
2855     mainscreen_map_view.removeView(orient_1_toggle);
2856     mainscreen_map_view.removeView(xx1);
2857     mainscreen_map_view.removeView(xx2);
2858     mainscreen_map_view.removeView(xx3);
2859     }
2860     });
2861     mainscreen_map_view.addView(btn_ok, bt_ok);
2862    
2863     RelativeLayout.LayoutParams bt_cl = new RelativeLayout.LayoutParams(150, RelativeLayout.LayoutParams.FILL_PARENT);
2864     btn_cancel = new Button(this);
2865     bt_cl.width = ((int) (this.rose.mCanvasWidth * 0.4));
2866     bt_cl.height = 70;
2867     bt_cl.topMargin = 20 + 3 + lp2.height;
2868     bt_cl.leftMargin = this.rose.mCanvasWidth - 5 - bt_cl.width;
2869     bt_cl.rightMargin = 5;
2870     btn_cancel.setText("Cancel");
2871     bt_cl.addRule(RelativeLayout.ALIGN_PARENT_TOP);
2872     this.btn_cancel.setOnClickListener(new OnClickListener()
2873     {
2874     public void onClick(View v)
2875     {
2876     mainscreen_map_view.removeView(btn_cancel);
2877     mainscreen_map_view.removeView(btn_ok);
2878     mainscreen_map_view.removeView(orient_1_toggle);
2879     mainscreen_map_view.removeView(xx1);
2880     mainscreen_map_view.removeView(xx2);
2881     mainscreen_map_view.removeView(xx3);
2882     }
2883     });
2884     mainscreen_map_view.addView(btn_cancel, bt_cl);
2885     // mainscreen_map_view.removeView(btn_cancel);
2886     }
2887    
2888     public void navigate_to_target()
2889     {
2890     // save before starting new activity!!
2891     this.save_settings();
2892     try
2893     {
2894     // try to start navi. activity
2895     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + this.rose.current_target.lat + "," + this.rose.current_target.lon)));
2896     }
2897     catch (Exception e)
2898     {
2899     // System.out.println("Error starting navigation");
2900     Toast.makeText(getApplicationContext(), "Error starting navigation!", Toast.LENGTH_SHORT).show();
2901     e.printStackTrace();
2902     }
2903     }
2904    
2905     public void show_cache_page_in_browser()
2906     {
2907     // save before starting new activity!!
2908    
2909     // http://www.geocaching.com/seek/cache_details.aspx?guid=b89040ae-5bca-4a14-960b-d9dcbe645d8a
2910     // or
2911     // http://www.geocaching.com/seek/cache_details.aspx?wp=GC2EVEY
2912    
2913     // System.out.println("1=" + this.cacheview.gc_name_current);
2914     // System.out.println("2=" + this.cacheview.gc_name_previous);
2915     // System.out.println("3=" + this.cacheview.details_loaded);
2916     // System.out.println("4=" + String.valueOf(this.cacheview.gc));
2917    
2918     this.save_settings();
2919     try
2920     {
2921     // try to start browser activity
2922     // old form, no more supported // startActivity(new
2923     // Intent(Intent.ACTION_VIEW,
2924     // ContentURI.create("http://www.myurl.com")));
2925     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.geocaching.com/seek/cache_details.aspx?wp=" + cacheview.gc.name)));
2926     }
2927     catch (Exception e)
2928     {
2929     // System.out.println("Error starting navigation");
2930     Toast.makeText(getApplicationContext(), "Error starting Browser!", Toast.LENGTH_SHORT).show();
2931     e.printStackTrace();
2932     }
2933     }
2934    
2935     // @Override
2936     public boolean XXXXXXXXXXXXXXonKeyDown(int keyCode, KeyEvent event)
2937     {
2938     int i;
2939     String s = null;
2940     i = event.getUnicodeChar();
2941     System.out.println("onKeyDown " + keyCode + " " + i);
2942     if (i == 0)
2943     {
2944     if (keyCode == android.view.KeyEvent.KEYCODE_DEL)
2945     {
2946     s = java.lang.String.valueOf((char) 8);
2947     }
2948     else if (keyCode == android.view.KeyEvent.KEYCODE_MENU)
2949     {
2950     s = java.lang.String.valueOf((char) 1);
2951     return false;
2952     }
2953     else if (keyCode == android.view.KeyEvent.KEYCODE_SEARCH)
2954     {
2955     s = java.lang.String.valueOf((char) 19);
2956     }
2957     else if (keyCode == android.view.KeyEvent.KEYCODE_BACK)
2958     {
2959     s = java.lang.String.valueOf((char) 27);
2960     return false;
2961     }
2962     else if (keyCode == android.view.KeyEvent.KEYCODE_CALL)
2963     {
2964     s = java.lang.String.valueOf((char) 3);
2965     }
2966     else if (keyCode == android.view.KeyEvent.KEYCODE_VOLUME_UP)
2967     {
2968     s = java.lang.String.valueOf((char) 21);
2969     System.out.println("ss " + s);
2970     return false;
2971     }
2972     else if (keyCode == android.view.KeyEvent.KEYCODE_VOLUME_DOWN)
2973     {
2974     s = java.lang.String.valueOf((char) 4);
2975     System.out.println("ss " + s);
2976     return false;
2977     }
2978     else if (keyCode == android.view.KeyEvent.KEYCODE_DPAD_CENTER)
2979     {
2980     s = java.lang.String.valueOf((char) 13);
2981     }
2982     else if (keyCode == android.view.KeyEvent.KEYCODE_DPAD_DOWN)
2983     {
2984     s = java.lang.String.valueOf((char) 16);
2985     }
2986     else if (keyCode == android.view.KeyEvent.KEYCODE_DPAD_LEFT)
2987     {
2988     s = java.lang.String.valueOf((char) 2);
2989     }
2990     else if (keyCode == android.view.KeyEvent.KEYCODE_DPAD_RIGHT)
2991     {
2992     s = java.lang.String.valueOf((char) 6);
2993     }
2994     else if (keyCode == android.view.KeyEvent.KEYCODE_DPAD_UP)
2995     {
2996     s = java.lang.String.valueOf((char) 14);
2997     }
2998     }
2999     else if (i == 10)
3000     {
3001     s = java.lang.String.valueOf((char) 13);
3002     }
3003     else
3004     {
3005     s = java.lang.String.valueOf((char) i);
3006     }
3007     if (s != null)
3008     {
3009     // KeypressCallback(KeypressCallbackID, s);
3010     System.out.println("s: " + s);
3011     }
3012     return true;
3013     }
3014    
3015     }

   
Visit the aagtl Website