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_5;
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 public static AltosFrequency[] load_common_frequencies() {
122 AltosFrequency[] frequencies = null;
123 boolean existing = false;
124 existing = backend.nodeExists(common_frequencies_node_name);
127 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
128 int count = node.getInt(frequency_count, 0);
130 frequencies = new AltosFrequency[count];
131 for (int i = 0; i < count; i++) {
135 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
136 description = node.getString(String.format(description_format, i), null);
137 frequencies[i] = new AltosFrequency(frequency, description);
140 frequencies = new AltosFrequency[10];
141 for (int i = 0; i < 10; i++) {
142 frequencies[i] = new AltosFrequency(434.550 + i * .1,
143 String.format("Channel %d", i));
149 public static void save_common_frequencies(AltosFrequency[] frequencies) {
150 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
152 node.putInt(frequency_count, frequencies.length);
153 for (int i = 0; i < frequencies.length; i++) {
154 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
155 node.putString(String.format(description_format, i), frequencies[i].description);
158 public static int launcher_serial;
160 public static int launcher_channel;
162 public static void init(AltosPreferencesBackend in_backend) {
167 backend = in_backend;
169 /* Initialize logdir from preferences */
170 String logdir_string = backend.getString(logdirPreference, null);
171 if (logdir_string != null)
172 logdir = new File(logdir_string);
174 logdir = new File(backend.homeDirectory(), logdirName);
175 if (!logdir.exists())
178 mapdir = new File(logdir, "maps");
179 if (!mapdir.exists())
182 frequencies = new Hashtable<Integer, Double>();
184 telemetries = new Hashtable<Integer,Integer>();
186 telemetry_rates = new Hashtable<Integer,Integer>();
188 logfiles = new Hashtable<Integer,File>();
190 voice = backend.getBoolean(voicePreference, true);
192 callsign = backend.getString(callsignPreference,"N0CALL");
194 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
196 scanning_telemetry_rate = backend.getInt(scanningTelemetryRatePreference,(1 << AltosLib.ao_telemetry_rate_38400));
198 launcher_serial = backend.getInt(launcherSerialPreference, 0);
200 launcher_channel = backend.getInt(launcherChannelPreference, 0);
202 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
203 if (firmwaredir_string != null)
204 firmwaredir = new File(firmwaredir_string);
208 common_frequencies = load_common_frequencies();
210 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
213 public static void flush_preferences() {
217 public static void set_logdir(File new_logdir) {
218 synchronized (backend) {
220 mapdir = new File(logdir, "maps");
221 if (!mapdir.exists())
223 backend.putString(logdirPreference, logdir.getPath());
228 public static File logdir() {
229 synchronized (backend) {
234 public static File last_logdir() {
235 synchronized (backend) {
236 if (last_logdir == null)
237 last_logdir = logdir;
242 public static void set_last_logdir(File file) {
243 synchronized(backend) {
244 if (file != null && !file.isDirectory())
245 file = file.getParentFile();
247 file = new File(".");
252 public static File mapdir() {
253 synchronized (backend) {
258 public static void set_frequency(int serial, double new_frequency) {
259 synchronized (backend) {
260 frequencies.put(serial, new_frequency);
261 backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
266 public static double frequency(int serial) {
267 synchronized (backend) {
268 if (frequencies.containsKey(serial))
269 return frequencies.get(serial);
270 double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
271 if (frequency == 0.0) {
272 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
273 frequency = AltosConvert.radio_channel_to_frequency(channel);
275 frequencies.put(serial, frequency);
280 public static void set_telemetry(int serial, int new_telemetry) {
281 synchronized (backend) {
282 telemetries.put(serial, new_telemetry);
283 backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
288 public static int telemetry(int serial) {
289 synchronized (backend) {
290 if (telemetries.containsKey(serial))
291 return telemetries.get(serial);
292 int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
293 AltosLib.ao_telemetry_standard);
294 telemetries.put(serial, telemetry);
299 public static void set_telemetry_rate(int serial, int new_telemetry_rate) {
300 synchronized (backend) {
301 telemetry_rates.put(serial, new_telemetry_rate);
302 backend.putInt(String.format(telemetryRatePreferenceFormat, serial), new_telemetry_rate);
307 public static int telemetry_rate(int serial) {
308 synchronized (backend) {
309 if (telemetry_rates.containsKey(serial))
310 return telemetry_rates.get(serial);
311 int telemetry_rate = backend.getInt(String.format(telemetryRatePreferenceFormat, serial),
312 AltosLib.ao_telemetry_rate_38400);
313 telemetry_rates.put(serial, telemetry_rate);
314 return telemetry_rate;
318 public static void set_logfile(int serial, File new_logfile) {
319 synchronized(backend) {
320 logfiles.put(serial, new_logfile);
321 backend.putString(String.format(logfilePreferenceFormat, serial), new_logfile.getPath());
326 public static File logfile(int serial) {
327 synchronized(backend) {
328 if (logfiles.containsKey(serial))
329 return logfiles.get(serial);
330 String logfile_string = backend.getString(String.format(logfilePreferenceFormat, serial), null);
331 if (logfile_string == null)
333 File logfile = new File(logfile_string);
334 logfiles.put(serial, logfile);
339 public static void set_state(int serial, AltosState state, AltosListenerState listener_state) {
341 ByteArrayOutputStream baos = new ByteArrayOutputStream();
344 ObjectOutputStream oos = new ObjectOutputStream(baos);
346 AltosSavedState saved_state = new AltosSavedState(state, listener_state);
347 oos.writeObject(saved_state);
349 byte[] bytes = baos.toByteArray();
351 synchronized(backend) {
352 backend.putBytes(String.format(statePreferenceFormat, serial), bytes);
355 } catch (IOException ie) {
359 public static AltosSavedState state(int serial) {
362 synchronized(backend) {
363 bytes = backend.getBytes(String.format(statePreferenceFormat, serial), null);
369 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
372 ObjectInputStream ois = new ObjectInputStream(bais);
373 AltosSavedState saved_state = (AltosSavedState) ois.readObject();
375 } catch (IOException ie) {
376 } catch (ClassNotFoundException ce) {
381 public static void set_scanning_telemetry(int new_scanning_telemetry) {
382 synchronized (backend) {
383 scanning_telemetry = new_scanning_telemetry;
384 backend.putInt(scanningTelemetryPreference, scanning_telemetry);
389 public static int scanning_telemetry() {
390 synchronized (backend) {
391 return scanning_telemetry;
395 public static void set_scanning_telemetry_rate(int new_scanning_telemetry_rate) {
396 synchronized (backend) {
397 scanning_telemetry_rate = new_scanning_telemetry_rate;
398 backend.putInt(scanningTelemetryRatePreference, scanning_telemetry_rate);
403 public static int scanning_telemetry_rate() {
404 synchronized(backend) {
405 return scanning_telemetry_rate;
409 public static void set_voice(boolean new_voice) {
410 synchronized (backend) {
412 backend.putBoolean(voicePreference, voice);
417 public static boolean voice() {
418 synchronized (backend) {
423 public static void set_callsign(String new_callsign) {
424 synchronized(backend) {
425 callsign = new_callsign;
426 backend.putString(callsignPreference, callsign);
431 public static String callsign() {
432 synchronized(backend) {
437 public static void set_firmwaredir(File new_firmwaredir) {
438 synchronized (backend) {
439 firmwaredir = new_firmwaredir;
440 backend.putString(firmwaredirPreference, firmwaredir.getPath());
445 public static File firmwaredir() {
446 synchronized (backend) {
451 public static void set_launcher_serial(int new_launcher_serial) {
452 synchronized (backend) {
453 launcher_serial = new_launcher_serial;
454 backend.putInt(launcherSerialPreference, launcher_serial);
459 public static int launcher_serial() {
460 synchronized (backend) {
461 return launcher_serial;
465 public static void set_launcher_channel(int new_launcher_channel) {
466 synchronized (backend) {
467 launcher_channel = new_launcher_channel;
468 backend.putInt(launcherChannelPreference, launcher_channel);
473 public static int launcher_channel() {
474 synchronized (backend) {
475 return launcher_channel;
479 public static AltosPreferencesBackend bt_devices() {
480 synchronized (backend) {
481 return backend.node("bt_devices");
485 public static AltosFrequency[] common_frequencies() {
486 synchronized (backend) {
487 return common_frequencies;
491 public static void set_common_frequencies(AltosFrequency[] frequencies) {
492 synchronized(backend) {
493 common_frequencies = frequencies;
494 save_common_frequencies(frequencies);
499 public static void add_common_frequency(AltosFrequency frequency) {
500 AltosFrequency[] old_frequencies = common_frequencies();
501 AltosFrequency[] new_frequencies = new AltosFrequency[old_frequencies.length + 1];
504 for (i = 0; i < old_frequencies.length; i++) {
505 if (frequency.frequency == old_frequencies[i].frequency)
507 if (frequency.frequency < old_frequencies[i].frequency)
509 new_frequencies[i] = old_frequencies[i];
511 new_frequencies[i] = frequency;
512 for (; i < old_frequencies.length; i++)
513 new_frequencies[i+1] = old_frequencies[i];
514 set_common_frequencies(new_frequencies);
517 static LinkedList<AltosUnitsListener> units_listeners;
519 public static boolean imperial_units() {
520 synchronized(backend) {
521 return AltosConvert.imperial_units;
525 public static void set_imperial_units(boolean imperial_units) {
526 synchronized (backend) {
527 AltosConvert.imperial_units = imperial_units;
528 backend.putBoolean(unitsPreference, imperial_units);
531 if (units_listeners != null) {
532 for (AltosUnitsListener l : units_listeners) {
533 l.units_changed(imperial_units);
538 public static void register_units_listener(AltosUnitsListener l) {
539 synchronized(backend) {
540 if (units_listeners == null)
541 units_listeners = new LinkedList<AltosUnitsListener>();
542 units_listeners.add(l);
546 public static void unregister_units_listener(AltosUnitsListener l) {
547 synchronized(backend) {
548 units_listeners.remove(l);