X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altoslib%2FAltosPreferences.java;h=35d44631ed22918acab2f4c5bada4959e36c33dc;hp=cdff93f6ed80bf2286d5b570bf300e25c058c4a1;hb=1085ec5d57e0ed5d132f2bbdac1a0b6a32c0ab4a;hpb=643c2fb03833d658320f476ef731bbb06fe3cc31 diff --git a/altoslib/AltosPreferences.java b/altoslib/AltosPreferences.java index cdff93f6..35d44631 100644 --- a/altoslib/AltosPreferences.java +++ b/altoslib/AltosPreferences.java @@ -3,7 +3,8 @@ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -15,7 +16,7 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -package org.altusmetrum.altoslib_7; +package org.altusmetrum.altoslib_11; import java.io.*; import java.util.*; @@ -116,6 +117,7 @@ public class AltosPreferences { public final static String frequency_count = "COUNT"; public final static String frequency_format = "FREQUENCY-%d"; public final static String description_format = "DESCRIPTION-%d"; + public final static String frequenciesPreference = "FREQUENCIES-1"; /* Units preference */ @@ -128,25 +130,39 @@ 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; - existing = backend.nodeExists(common_frequencies_node_name); - - if (existing) { - AltosPreferencesBackend node = backend.node(common_frequencies_node_name); - int count = node.getInt(frequency_count, 0); - - frequencies = new AltosFrequency[count]; - for (int i = 0; i < count; i++) { - double frequency; - String description; - frequency = node.getDouble(String.format(frequency_format, i), 0.0); - description = node.getString(String.format(description_format, i), null); - frequencies[i] = new AltosFrequency(frequency, description); + try { + AltosJson json = AltosJson.fromString(backend.getString(frequenciesPreference, + null)); + frequencies = (AltosFrequency[]) json.make(frequencies.getClass()); + } catch (Exception e) { + } + + if (frequencies == null) { + if (backend.nodeExists(common_frequencies_node_name)) { + AltosPreferencesBackend node = backend.node(common_frequencies_node_name); + int count = node.getInt(frequency_count, 0); + + if (count > 0) { + frequencies = new AltosFrequency[count]; + for (int i = 0; i < count; i++) { + double frequency; + String description; + + frequency = node.getDouble(String.format(frequency_format, i), 0.0); + description = node.getString(String.format(description_format, i), null); + frequencies[i] = new AltosFrequency(frequency, description); + } + } } - } else { + } + + if (frequencies == null) { frequencies = new AltosFrequency[10]; for (int i = 0; i < 10; i++) { frequencies[i] = new AltosFrequency(434.550 + i * .1, @@ -156,15 +172,12 @@ public class AltosPreferences { return frequencies; } - public static void save_common_frequencies(AltosFrequency[] frequencies) { - AltosPreferencesBackend node = backend.node(common_frequencies_node_name); - - node.putInt(frequency_count, frequencies.length); - for (int i = 0; i < frequencies.length; i++) { - node.putDouble(String.format(frequency_format, i), frequencies[i].frequency); - node.putString(String.format(description_format, i), frequencies[i].description); - } + public static void save_common_frequencies() { + AltosJson json = new AltosJson(common_frequencies); + backend.putString(frequenciesPreference, json.toString()); + flush_preferences(); } + public static int launcher_serial; public static int launcher_channel; @@ -221,6 +234,7 @@ public class AltosPreferences { map_cache = backend.getInt(mapCachePreference, 9); map_cache_listeners = new LinkedList(); + map_type = backend.getInt(mapTypePreference, AltosMap.maptype_hybrid); } public static void flush_preferences() { @@ -349,24 +363,12 @@ public class AltosPreferences { } } - public static void set_state(int serial, AltosState state, AltosListenerState listener_state) { - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - try { - ObjectOutputStream oos = new ObjectOutputStream(baos); - - AltosSavedState saved_state = new AltosSavedState(state, listener_state); - oos.writeObject(saved_state); - - byte[] bytes = baos.toByteArray(); + public static void set_state(AltosState state) { - synchronized(backend) { - backend.putBytes(String.format(statePreferenceFormat, serial), bytes); - backend.putInt(statePreferenceLatest, serial); - flush_preferences(); - } - } catch (IOException ie) { + synchronized(backend) { + backend.putJson(String.format(statePreferenceFormat, state.serial), new AltosJson(state)); + backend.putInt(statePreferenceLatest, state.serial); + flush_preferences(); } } @@ -389,6 +391,7 @@ public class AltosPreferences { public static void remove_state(int serial) { synchronized(backend) { backend.remove(String.format(statePreferenceFormat, serial)); + flush_preferences(); } } @@ -400,26 +403,16 @@ public class AltosPreferences { return latest; } - public static AltosSavedState state(int serial) { - byte[] bytes = null; - + public static AltosState state(int serial) { synchronized(backend) { - bytes = backend.getBytes(String.format(statePreferenceFormat, serial), null); - } - - if (bytes == null) + try { + AltosJson json = backend.getJson(String.format(statePreferenceFormat, serial)); + if (json != null) + return (AltosState) (json.make(AltosState.class)); + } catch (Exception e) { + } return null; - - ByteArrayInputStream bais = new ByteArrayInputStream(bytes); - - try { - ObjectInputStream ois = new ObjectInputStream(bais); - AltosSavedState saved_state = (AltosSavedState) ois.readObject(); - return saved_state; - } catch (IOException ie) { - } catch (ClassNotFoundException ce) { } - return null; } public static void set_scanning_telemetry(int new_scanning_telemetry) { @@ -535,8 +528,8 @@ public class AltosPreferences { public static void set_common_frequencies(AltosFrequency[] frequencies) { synchronized(backend) { common_frequencies = frequencies; - save_common_frequencies(frequencies); - flush_preferences(); + + save_common_frequencies(); } } @@ -621,4 +614,39 @@ public class AltosPreferences { return map_cache; } } + + static LinkedList 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(); + map_type_listeners.add(l); + } + } + + public static void unregister_map_type_listener(AltosMapTypeListener l) { + synchronized(backend) { + map_type_listeners.remove(l); + } + } }