altosdroid: Split setup functions to separate dialog
[fw/altos] / altoslib / AltosPreferences.java
index 91cfc6b6fa76d5a8d0f5c891d7118e1166759a68..4bf48f6df025e6433973e84460847d40219dcd07 100644 (file)
@@ -15,7 +15,7 @@
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
-package org.altusmetrum.altoslib_9;
+package org.altusmetrum.altoslib_10;
 
 import java.io.*;
 import java.util.*;
@@ -128,6 +128,9 @@ public class AltosPreferences {
 
        public static int map_cache = 9;
 
+       final static String mapTypePreference = "MAP-TYPE";
+       static int      map_type;
+
        public static AltosFrequency[] load_common_frequencies() {
                AltosFrequency[] frequencies = null;
                boolean existing = false;
@@ -221,6 +224,7 @@ public class AltosPreferences {
 
                map_cache = backend.getInt(mapCachePreference, 9);
                map_cache_listeners = new LinkedList<AltosMapCacheListener>();
+               map_type = backend.getInt(mapTypePreference, AltosMap.maptype_hybrid);
        }
 
        public static void flush_preferences() {
@@ -351,6 +355,11 @@ public class AltosPreferences {
 
        public static void set_state(int serial, AltosState state, AltosListenerState listener_state) {
 
+               backend.debug("set_state for %d pos %g,%g\n",
+                             serial,
+                             state.gps != null ? state.gps.lat : 0.0,
+                             state.gps != null ? state.gps.lon : 0.0);
+
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
                try {
@@ -367,6 +376,7 @@ public class AltosPreferences {
                                flush_preferences();
                        }
                } catch (IOException ie) {
+                       backend.debug("set_state failed %s\n", ie.toString());
                }
        }
 
@@ -376,6 +386,7 @@ public class AltosPreferences {
 
                for (String key : keys) {
                        if (key.startsWith(statePreferenceHead)) {
+                               backend.debug("list_states %s\n", key);
                                try {
                                        int serial = AltosParse.parse_int(key.substring(statePreferenceHead.length()));
                                        states.add(serial);
@@ -403,21 +414,31 @@ public class AltosPreferences {
        public static AltosSavedState state(int serial) {
                byte[] bytes = null;
 
+               backend.debug("get state %d\n", serial);
+
                synchronized(backend) {
                        bytes = backend.getBytes(String.format(statePreferenceFormat, serial), null);
                }
 
-               if (bytes == null)
+               if (bytes == null) {
+                       backend.debug("no state for %d\n", serial);
                        return null;
+               }
 
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
 
                try {
                        ObjectInputStream ois = new ObjectInputStream(bais);
                        AltosSavedState saved_state = (AltosSavedState) ois.readObject();
+                       backend.debug("got saved state for %d: %g,%g\n",
+                                     serial,
+                                     saved_state.state.gps != null ? saved_state.state.gps.lat : 0.0,
+                                     saved_state.state.gps != null ? saved_state.state.gps.lon : 0.0);
                        return saved_state;
                } catch (IOException ie) {
+                       backend.debug("IO exception %s\n", ie.toString());
                } catch (ClassNotFoundException ce) {
+                       backend.debug("ClassNotFoundException %s\n", ce.toString());
                }
                return null;
        }
@@ -621,4 +642,39 @@ public class AltosPreferences {
                        return map_cache;
                }
        }
+
+       static LinkedList<AltosMapTypeListener> map_type_listeners;
+
+       public static void set_map_type(int map_type) {
+               synchronized(backend) {
+                       AltosPreferences.map_type = map_type;
+                       backend.putInt(mapTypePreference, map_type);
+                       flush_preferences();
+               }
+               if (map_type_listeners != null) {
+                       for (AltosMapTypeListener l : map_type_listeners) {
+                               l.map_type_changed(map_type);
+                       }
+               }
+       }
+
+       public static int map_type() {
+               synchronized(backend) {
+                       return map_type;
+               }
+       }
+
+       public static void register_map_type_listener(AltosMapTypeListener l) {
+               synchronized(backend) {
+                       if (map_type_listeners == null)
+                               map_type_listeners = new LinkedList<AltosMapTypeListener>();
+                       map_type_listeners.add(l);
+               }
+       }
+
+       public static void unregister_map_type_listener(AltosMapTypeListener l) {
+               synchronized(backend) {
+                       map_type_listeners.remove(l);
+               }
+       }
 }