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 /* voice preference name */
45 public final static String voicePreference = "VOICE";
47 /* callsign preference name */
48 public final static String callsignPreference = "CALLSIGN";
50 /* firmware directory preference name */
51 public final static String firmwaredirPreference = "FIRMWARE";
53 /* serial debug preference name */
54 public final static String serialDebugPreference = "SERIAL-DEBUG";
56 /* scanning telemetry preferences name */
57 public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
59 /* scanning telemetry rate preferences name */
60 public final static String scanningTelemetryRatePreference = "SCANNING-RATE";
62 /* Launcher serial preference name */
63 public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
65 /* Launcher channel preference name */
66 public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
68 /* Default logdir is ~/TeleMetrum */
69 public final static String logdirName = "TeleMetrum";
72 public static File logdir;
74 /* Last log directory - use this next time we open or save something */
75 public static File last_logdir;
77 /* Map directory -- hangs of logdir */
78 public static File mapdir;
80 /* Frequency (map serial to frequency) */
81 public static Hashtable<Integer, Double> frequencies;
83 /* Telemetry (map serial to telemetry format) */
84 public static Hashtable<Integer, Integer> telemetries;
86 /* Telemetry rate (map serial to telemetry format) */
87 public static Hashtable<Integer, Integer> telemetry_rates;
89 /* Log file (map serial to logfile name) */
90 public static Hashtable<Integer, File> logfiles;
92 /* Voice preference */
93 public static boolean voice;
95 /* Callsign preference */
96 public static String callsign;
98 /* Firmware directory */
99 public static File firmwaredir;
101 /* Scanning telemetry */
102 public static int scanning_telemetry;
104 public static int scanning_telemetry_rate;
106 /* List of frequencies */
107 public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
108 public static AltosFrequency[] common_frequencies;
110 public final static String frequency_count = "COUNT";
111 public final static String frequency_format = "FREQUENCY-%d";
112 public final static String description_format = "DESCRIPTION-%d";
114 /* Units preference */
116 public final static String unitsPreference = "IMPERIAL-UNITS";
118 public static AltosFrequency[] load_common_frequencies() {
119 AltosFrequency[] frequencies = null;
120 boolean existing = false;
121 existing = backend.nodeExists(common_frequencies_node_name);
124 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
125 int count = node.getInt(frequency_count, 0);
127 frequencies = new AltosFrequency[count];
128 for (int i = 0; i < count; i++) {
132 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
133 description = node.getString(String.format(description_format, i), null);
134 frequencies[i] = new AltosFrequency(frequency, description);
137 frequencies = new AltosFrequency[10];
138 for (int i = 0; i < 10; i++) {
139 frequencies[i] = new AltosFrequency(434.550 + i * .1,
140 String.format("Channel %d", i));
146 public static void save_common_frequencies(AltosFrequency[] frequencies) {
147 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
149 node.putInt(frequency_count, frequencies.length);
150 for (int i = 0; i < frequencies.length; i++) {
151 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
152 node.putString(String.format(description_format, i), frequencies[i].description);
155 public static int launcher_serial;
157 public static int launcher_channel;
159 public static void init(AltosPreferencesBackend in_backend) {
164 backend = in_backend;
166 /* Initialize logdir from preferences */
167 String logdir_string = backend.getString(logdirPreference, null);
168 if (logdir_string != null)
169 logdir = new File(logdir_string);
171 logdir = new File(backend.homeDirectory(), logdirName);
172 if (!logdir.exists())
175 mapdir = new File(logdir, "maps");
176 if (!mapdir.exists())
179 frequencies = new Hashtable<Integer, Double>();
181 telemetries = new Hashtable<Integer,Integer>();
183 telemetry_rates = new Hashtable<Integer,Integer>();
185 logfiles = new Hashtable<Integer,File>();
187 voice = backend.getBoolean(voicePreference, true);
189 callsign = backend.getString(callsignPreference,"N0CALL");
191 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
193 scanning_telemetry_rate = backend.getInt(scanningTelemetryRatePreference,(1 << AltosLib.ao_telemetry_rate_38400));
195 launcher_serial = backend.getInt(launcherSerialPreference, 0);
197 launcher_channel = backend.getInt(launcherChannelPreference, 0);
199 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
200 if (firmwaredir_string != null)
201 firmwaredir = new File(firmwaredir_string);
205 common_frequencies = load_common_frequencies();
207 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
210 public static void flush_preferences() {
214 public static void set_logdir(File new_logdir) {
215 synchronized (backend) {
217 mapdir = new File(logdir, "maps");
218 if (!mapdir.exists())
220 backend.putString(logdirPreference, logdir.getPath());
225 public static File logdir() {
226 synchronized (backend) {
231 public static File last_logdir() {
232 synchronized (backend) {
233 if (last_logdir == null)
234 last_logdir = logdir;
239 public static void set_last_logdir(File file) {
240 synchronized(backend) {
241 if (file != null && !file.isDirectory())
242 file = file.getParentFile();
244 file = new File(".");
249 public static File mapdir() {
250 synchronized (backend) {
255 public static void set_frequency(int serial, double new_frequency) {
256 synchronized (backend) {
257 frequencies.put(serial, new_frequency);
258 backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
263 public static double frequency(int serial) {
264 synchronized (backend) {
265 if (frequencies.containsKey(serial))
266 return frequencies.get(serial);
267 double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
268 if (frequency == 0.0) {
269 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
270 frequency = AltosConvert.radio_channel_to_frequency(channel);
272 frequencies.put(serial, frequency);
277 public static void set_telemetry(int serial, int new_telemetry) {
278 synchronized (backend) {
279 telemetries.put(serial, new_telemetry);
280 backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
285 public static int telemetry(int serial) {
286 synchronized (backend) {
287 if (telemetries.containsKey(serial))
288 return telemetries.get(serial);
289 int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
290 AltosLib.ao_telemetry_standard);
291 telemetries.put(serial, telemetry);
296 public static void set_telemetry_rate(int serial, int new_telemetry_rate) {
297 synchronized (backend) {
298 telemetry_rates.put(serial, new_telemetry_rate);
299 backend.putInt(String.format(telemetryRatePreferenceFormat, serial), new_telemetry_rate);
304 public static int telemetry_rate(int serial) {
305 synchronized (backend) {
306 if (telemetry_rates.containsKey(serial))
307 return telemetry_rates.get(serial);
308 int telemetry_rate = backend.getInt(String.format(telemetryRatePreferenceFormat, serial),
309 AltosLib.ao_telemetry_rate_38400);
310 telemetry_rates.put(serial, telemetry_rate);
311 return telemetry_rate;
315 public static void set_logfile(int serial, File new_logfile) {
316 synchronized(backend) {
317 logfiles.put(serial, new_logfile);
318 backend.putString(String.format(logfilePreferenceFormat, serial), new_logfile.getPath());
323 public static File logfile(int serial) {
324 synchronized(backend) {
325 if (logfiles.containsKey(serial))
326 return logfiles.get(serial);
327 String logfile_string = backend.getString(String.format(logfilePreferenceFormat, serial), null);
328 if (logfile_string == null)
330 File logfile = new File(logfile_string);
331 logfiles.put(serial, logfile);
336 public static void set_scanning_telemetry(int new_scanning_telemetry) {
337 synchronized (backend) {
338 scanning_telemetry = new_scanning_telemetry;
339 backend.putInt(scanningTelemetryPreference, scanning_telemetry);
344 public static int scanning_telemetry() {
345 synchronized (backend) {
346 return scanning_telemetry;
350 public static void set_scanning_telemetry_rate(int new_scanning_telemetry_rate) {
351 synchronized (backend) {
352 scanning_telemetry_rate = new_scanning_telemetry_rate;
353 backend.putInt(scanningTelemetryRatePreference, scanning_telemetry_rate);
358 public static int scanning_telemetry_rate() {
359 synchronized(backend) {
360 return scanning_telemetry_rate;
364 public static void set_voice(boolean new_voice) {
365 synchronized (backend) {
367 backend.putBoolean(voicePreference, voice);
372 public static boolean voice() {
373 synchronized (backend) {
378 public static void set_callsign(String new_callsign) {
379 synchronized(backend) {
380 callsign = new_callsign;
381 backend.putString(callsignPreference, callsign);
386 public static String callsign() {
387 synchronized(backend) {
392 public static void set_firmwaredir(File new_firmwaredir) {
393 synchronized (backend) {
394 firmwaredir = new_firmwaredir;
395 backend.putString(firmwaredirPreference, firmwaredir.getPath());
400 public static File firmwaredir() {
401 synchronized (backend) {
406 public static void set_launcher_serial(int new_launcher_serial) {
407 synchronized (backend) {
408 launcher_serial = new_launcher_serial;
409 backend.putInt(launcherSerialPreference, launcher_serial);
414 public static int launcher_serial() {
415 synchronized (backend) {
416 return launcher_serial;
420 public static void set_launcher_channel(int new_launcher_channel) {
421 synchronized (backend) {
422 launcher_channel = new_launcher_channel;
423 backend.putInt(launcherChannelPreference, launcher_channel);
428 public static int launcher_channel() {
429 synchronized (backend) {
430 return launcher_channel;
434 public static AltosPreferencesBackend bt_devices() {
435 synchronized (backend) {
436 return backend.node("bt_devices");
440 public static AltosFrequency[] common_frequencies() {
441 synchronized (backend) {
442 return common_frequencies;
446 public static void set_common_frequencies(AltosFrequency[] frequencies) {
447 synchronized(backend) {
448 common_frequencies = frequencies;
449 save_common_frequencies(frequencies);
454 public static void add_common_frequency(AltosFrequency frequency) {
455 AltosFrequency[] old_frequencies = common_frequencies();
456 AltosFrequency[] new_frequencies = new AltosFrequency[old_frequencies.length + 1];
459 for (i = 0; i < old_frequencies.length; i++) {
460 if (frequency.frequency == old_frequencies[i].frequency)
462 if (frequency.frequency < old_frequencies[i].frequency)
464 new_frequencies[i] = old_frequencies[i];
466 new_frequencies[i] = frequency;
467 for (; i < old_frequencies.length; i++)
468 new_frequencies[i+1] = old_frequencies[i];
469 set_common_frequencies(new_frequencies);
472 static LinkedList<AltosUnitsListener> units_listeners;
474 public static boolean imperial_units() {
475 synchronized(backend) {
476 return AltosConvert.imperial_units;
480 public static void set_imperial_units(boolean imperial_units) {
481 synchronized (backend) {
482 AltosConvert.imperial_units = imperial_units;
483 backend.putBoolean(unitsPreference, imperial_units);
486 if (units_listeners != null) {
487 for (AltosUnitsListener l : units_listeners) {
488 l.units_changed(imperial_units);
493 public static void register_units_listener(AltosUnitsListener l) {
494 synchronized(backend) {
495 if (units_listeners == null)
496 units_listeners = new LinkedList<AltosUnitsListener>();
497 units_listeners.add(l);
501 public static void unregister_units_listener(AltosUnitsListener l) {
502 synchronized(backend) {
503 units_listeners.remove(l);