2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.altoslib_7;
23 public class AltosPreferences {
24 public static AltosPreferencesBackend backend = null;
26 /* logdir preference name */
27 public final static String logdirPreference = "LOGDIR";
29 /* channel preference name */
30 public final static String channelPreferenceFormat = "CHANNEL-%d";
32 /* frequency preference name */
33 public final static String frequencyPreferenceFormat = "FREQUENCY-%d";
35 /* telemetry format preference name */
36 public final static String telemetryPreferenceFormat = "TELEMETRY-%d";
38 /* telemetry rate format preference name */
39 public final static String telemetryRatePreferenceFormat = "RATE-%d";
41 /* log file format preference name */
42 public final static String logfilePreferenceFormat = "LOGFILE-%d";
44 /* state preference name */
45 public final static String statePreferenceFormat = "STATE-%d";
47 /* voice preference name */
48 public final static String voicePreference = "VOICE";
50 /* callsign preference name */
51 public final static String callsignPreference = "CALLSIGN";
53 /* firmware directory preference name */
54 public final static String firmwaredirPreference = "FIRMWARE";
56 /* serial debug preference name */
57 public final static String serialDebugPreference = "SERIAL-DEBUG";
59 /* scanning telemetry preferences name */
60 public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
62 /* scanning telemetry rate preferences name */
63 public final static String scanningTelemetryRatePreference = "SCANNING-RATE";
65 /* Launcher serial preference name */
66 public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
68 /* Launcher channel preference name */
69 public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
71 /* Default logdir is ~/TeleMetrum */
72 public final static String logdirName = "TeleMetrum";
75 public static File logdir;
77 /* Last log directory - use this next time we open or save something */
78 public static File last_logdir;
80 /* Map directory -- hangs of logdir */
81 public static File mapdir;
83 /* Frequency (map serial to frequency) */
84 public static Hashtable<Integer, Double> frequencies;
86 /* Telemetry (map serial to telemetry format) */
87 public static Hashtable<Integer, Integer> telemetries;
89 /* Telemetry rate (map serial to telemetry format) */
90 public static Hashtable<Integer, Integer> telemetry_rates;
92 /* Log file (map serial to logfile name) */
93 public static Hashtable<Integer, File> logfiles;
95 /* Voice preference */
96 public static boolean voice;
98 /* Callsign preference */
99 public static String callsign;
101 /* Firmware directory */
102 public static File firmwaredir;
104 /* Scanning telemetry */
105 public static int scanning_telemetry;
107 public static int scanning_telemetry_rate;
109 /* List of frequencies */
110 public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
111 public static AltosFrequency[] common_frequencies;
113 public final static String frequency_count = "COUNT";
114 public final static String frequency_format = "FREQUENCY-%d";
115 public final static String description_format = "DESCRIPTION-%d";
117 /* Units preference */
119 public final static String unitsPreference = "IMPERIAL-UNITS";
121 /* Maps cache size preference name */
122 final static String mapCachePreference = "MAP-CACHE";
124 static LinkedList<AltosMapCacheListener> map_cache_listeners;
126 public static int map_cache = 9;
128 public static AltosFrequency[] load_common_frequencies() {
129 AltosFrequency[] frequencies = null;
130 boolean existing = false;
131 existing = backend.nodeExists(common_frequencies_node_name);
134 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
135 int count = node.getInt(frequency_count, 0);
137 frequencies = new AltosFrequency[count];
138 for (int i = 0; i < count; i++) {
142 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
143 description = node.getString(String.format(description_format, i), null);
144 frequencies[i] = new AltosFrequency(frequency, description);
147 frequencies = new AltosFrequency[10];
148 for (int i = 0; i < 10; i++) {
149 frequencies[i] = new AltosFrequency(434.550 + i * .1,
150 String.format("Channel %d", i));
156 public static void save_common_frequencies(AltosFrequency[] frequencies) {
157 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
159 node.putInt(frequency_count, frequencies.length);
160 for (int i = 0; i < frequencies.length; i++) {
161 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
162 node.putString(String.format(description_format, i), frequencies[i].description);
165 public static int launcher_serial;
167 public static int launcher_channel;
169 public static void init(AltosPreferencesBackend in_backend) {
174 backend = in_backend;
176 /* Initialize logdir from preferences */
177 String logdir_string = backend.getString(logdirPreference, null);
178 if (logdir_string != null)
179 logdir = new File(logdir_string);
181 logdir = new File(backend.homeDirectory(), logdirName);
182 if (!logdir.exists())
185 mapdir = new File(logdir, "maps");
186 if (!mapdir.exists())
189 frequencies = new Hashtable<Integer, Double>();
191 telemetries = new Hashtable<Integer,Integer>();
193 telemetry_rates = new Hashtable<Integer,Integer>();
195 logfiles = new Hashtable<Integer,File>();
197 voice = backend.getBoolean(voicePreference, true);
199 callsign = backend.getString(callsignPreference,"N0CALL");
201 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
203 scanning_telemetry_rate = backend.getInt(scanningTelemetryRatePreference,(1 << AltosLib.ao_telemetry_rate_38400));
205 launcher_serial = backend.getInt(launcherSerialPreference, 0);
207 launcher_channel = backend.getInt(launcherChannelPreference, 0);
209 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
210 if (firmwaredir_string != null)
211 firmwaredir = new File(firmwaredir_string);
215 common_frequencies = load_common_frequencies();
217 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
219 map_cache = backend.getInt(mapCachePreference, 9);
220 map_cache_listeners = new LinkedList<AltosMapCacheListener>();
223 public static void flush_preferences() {
227 public static void set_logdir(File new_logdir) {
228 synchronized (backend) {
230 mapdir = new File(logdir, "maps");
231 if (!mapdir.exists())
233 backend.putString(logdirPreference, logdir.getPath());
238 public static File logdir() {
239 synchronized (backend) {
244 public static File last_logdir() {
245 synchronized (backend) {
246 if (last_logdir == null)
247 last_logdir = logdir;
252 public static void set_last_logdir(File file) {
253 synchronized(backend) {
254 if (file != null && !file.isDirectory())
255 file = file.getParentFile();
257 file = new File(".");
262 public static File mapdir() {
263 synchronized (backend) {
268 public static void set_frequency(int serial, double new_frequency) {
269 synchronized (backend) {
270 frequencies.put(serial, new_frequency);
271 backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
276 public static double frequency(int serial) {
277 synchronized (backend) {
278 if (frequencies.containsKey(serial))
279 return frequencies.get(serial);
280 double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
281 if (frequency == 0.0) {
282 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
283 frequency = AltosConvert.radio_channel_to_frequency(channel);
285 frequencies.put(serial, frequency);
290 public static void set_telemetry(int serial, int new_telemetry) {
291 synchronized (backend) {
292 telemetries.put(serial, new_telemetry);
293 backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
298 public static int telemetry(int serial) {
299 synchronized (backend) {
300 if (telemetries.containsKey(serial))
301 return telemetries.get(serial);
302 int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
303 AltosLib.ao_telemetry_standard);
304 telemetries.put(serial, telemetry);
309 public static void set_telemetry_rate(int serial, int new_telemetry_rate) {
310 synchronized (backend) {
311 telemetry_rates.put(serial, new_telemetry_rate);
312 backend.putInt(String.format(telemetryRatePreferenceFormat, serial), new_telemetry_rate);
317 public static int telemetry_rate(int serial) {
318 synchronized (backend) {
319 if (telemetry_rates.containsKey(serial))
320 return telemetry_rates.get(serial);
321 int telemetry_rate = backend.getInt(String.format(telemetryRatePreferenceFormat, serial),
322 AltosLib.ao_telemetry_rate_38400);
323 telemetry_rates.put(serial, telemetry_rate);
324 return telemetry_rate;
328 public static void set_logfile(int serial, File new_logfile) {
329 synchronized(backend) {
330 logfiles.put(serial, new_logfile);
331 backend.putString(String.format(logfilePreferenceFormat, serial), new_logfile.getPath());
336 public static File logfile(int serial) {
337 synchronized(backend) {
338 if (logfiles.containsKey(serial))
339 return logfiles.get(serial);
340 String logfile_string = backend.getString(String.format(logfilePreferenceFormat, serial), null);
341 if (logfile_string == null)
343 File logfile = new File(logfile_string);
344 logfiles.put(serial, logfile);
349 public static void set_state(int serial, AltosState state, AltosListenerState listener_state) {
351 ByteArrayOutputStream baos = new ByteArrayOutputStream();
354 ObjectOutputStream oos = new ObjectOutputStream(baos);
356 AltosSavedState saved_state = new AltosSavedState(state, listener_state);
357 oos.writeObject(saved_state);
359 byte[] bytes = baos.toByteArray();
361 synchronized(backend) {
362 backend.putBytes(String.format(statePreferenceFormat, serial), bytes);
365 } catch (IOException ie) {
369 public static AltosSavedState state(int serial) {
372 synchronized(backend) {
373 bytes = backend.getBytes(String.format(statePreferenceFormat, serial), null);
379 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
382 ObjectInputStream ois = new ObjectInputStream(bais);
383 AltosSavedState saved_state = (AltosSavedState) ois.readObject();
385 } catch (IOException ie) {
386 } catch (ClassNotFoundException ce) {
391 public static void set_scanning_telemetry(int new_scanning_telemetry) {
392 synchronized (backend) {
393 scanning_telemetry = new_scanning_telemetry;
394 backend.putInt(scanningTelemetryPreference, scanning_telemetry);
399 public static int scanning_telemetry() {
400 synchronized (backend) {
401 return scanning_telemetry;
405 public static void set_scanning_telemetry_rate(int new_scanning_telemetry_rate) {
406 synchronized (backend) {
407 scanning_telemetry_rate = new_scanning_telemetry_rate;
408 backend.putInt(scanningTelemetryRatePreference, scanning_telemetry_rate);
413 public static int scanning_telemetry_rate() {
414 synchronized(backend) {
415 return scanning_telemetry_rate;
419 public static void set_voice(boolean new_voice) {
420 synchronized (backend) {
422 backend.putBoolean(voicePreference, voice);
427 public static boolean voice() {
428 synchronized (backend) {
433 public static void set_callsign(String new_callsign) {
434 synchronized(backend) {
435 callsign = new_callsign;
436 backend.putString(callsignPreference, callsign);
441 public static String callsign() {
442 synchronized(backend) {
447 public static void set_firmwaredir(File new_firmwaredir) {
448 synchronized (backend) {
449 firmwaredir = new_firmwaredir;
450 backend.putString(firmwaredirPreference, firmwaredir.getPath());
455 public static File firmwaredir() {
456 synchronized (backend) {
461 public static void set_launcher_serial(int new_launcher_serial) {
462 synchronized (backend) {
463 launcher_serial = new_launcher_serial;
464 backend.putInt(launcherSerialPreference, launcher_serial);
469 public static int launcher_serial() {
470 synchronized (backend) {
471 return launcher_serial;
475 public static void set_launcher_channel(int new_launcher_channel) {
476 synchronized (backend) {
477 launcher_channel = new_launcher_channel;
478 backend.putInt(launcherChannelPreference, launcher_channel);
483 public static int launcher_channel() {
484 synchronized (backend) {
485 return launcher_channel;
489 public static AltosPreferencesBackend bt_devices() {
490 synchronized (backend) {
491 return backend.node("bt_devices");
495 public static AltosFrequency[] common_frequencies() {
496 synchronized (backend) {
497 return common_frequencies;
501 public static void set_common_frequencies(AltosFrequency[] frequencies) {
502 synchronized(backend) {
503 common_frequencies = frequencies;
504 save_common_frequencies(frequencies);
509 public static void add_common_frequency(AltosFrequency frequency) {
510 AltosFrequency[] old_frequencies = common_frequencies();
511 AltosFrequency[] new_frequencies = new AltosFrequency[old_frequencies.length + 1];
514 for (i = 0; i < old_frequencies.length; i++) {
515 if (frequency.frequency == old_frequencies[i].frequency)
517 if (frequency.frequency < old_frequencies[i].frequency)
519 new_frequencies[i] = old_frequencies[i];
521 new_frequencies[i] = frequency;
522 for (; i < old_frequencies.length; i++)
523 new_frequencies[i+1] = old_frequencies[i];
524 set_common_frequencies(new_frequencies);
527 static LinkedList<AltosUnitsListener> units_listeners;
529 public static boolean imperial_units() {
530 synchronized(backend) {
531 return AltosConvert.imperial_units;
535 public static void set_imperial_units(boolean imperial_units) {
536 synchronized (backend) {
537 AltosConvert.imperial_units = imperial_units;
538 backend.putBoolean(unitsPreference, imperial_units);
541 if (units_listeners != null) {
542 for (AltosUnitsListener l : units_listeners) {
543 l.units_changed(imperial_units);
548 public static void register_units_listener(AltosUnitsListener l) {
549 synchronized(backend) {
550 if (units_listeners == null)
551 units_listeners = new LinkedList<AltosUnitsListener>();
552 units_listeners.add(l);
556 public static void unregister_units_listener(AltosUnitsListener l) {
557 synchronized(backend) {
558 units_listeners.remove(l);
563 public static void register_map_cache_listener(AltosMapCacheListener l) {
564 synchronized(backend) {
565 map_cache_listeners.add(l);
569 public static void unregister_map_cache_listener(AltosMapCacheListener l) {
570 synchronized (backend) {
571 map_cache_listeners.remove(l);
575 public static void set_map_cache(int new_map_cache) {
576 synchronized(backend) {
577 map_cache = new_map_cache;
578 backend.putInt(mapCachePreference, map_cache);
580 for (AltosMapCacheListener l: map_cache_listeners)
581 l.map_cache_changed(map_cache);
585 public static int map_cache() {
586 synchronized(backend) {