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 /* voice preference name */
42 public final static String voicePreference = "VOICE";
44 /* callsign preference name */
45 public final static String callsignPreference = "CALLSIGN";
47 /* firmware directory preference name */
48 public final static String firmwaredirPreference = "FIRMWARE";
50 /* serial debug preference name */
51 public final static String serialDebugPreference = "SERIAL-DEBUG";
53 /* scanning telemetry preferences name */
54 public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
56 /* scanning telemetry rate preferences name */
57 public final static String scanningTelemetryRatePreference = "SCANNING-RATE";
59 /* Launcher serial preference name */
60 public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
62 /* Launcher channel preference name */
63 public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
65 /* Default logdir is ~/TeleMetrum */
66 public final static String logdirName = "TeleMetrum";
69 public static File logdir;
71 /* Last log directory - use this next time we open or save something */
72 public static File last_logdir;
74 /* Map directory -- hangs of logdir */
75 public static File mapdir;
77 /* Frequency (map serial to frequency) */
78 public static Hashtable<Integer, Double> frequencies;
80 /* Telemetry (map serial to telemetry format) */
81 public static Hashtable<Integer, Integer> telemetries;
83 /* Telemetry rate (map serial to telemetry format) */
84 public static Hashtable<Integer, Integer> telemetry_rates;
86 /* Voice preference */
87 public static boolean voice;
89 /* Callsign preference */
90 public static String callsign;
92 /* Firmware directory */
93 public static File firmwaredir;
95 /* Scanning telemetry */
96 public static int scanning_telemetry;
98 public static int scanning_telemetry_rate;
100 /* List of frequencies */
101 public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
102 public static AltosFrequency[] common_frequencies;
104 public final static String frequency_count = "COUNT";
105 public final static String frequency_format = "FREQUENCY-%d";
106 public final static String description_format = "DESCRIPTION-%d";
108 /* Units preference */
110 public final static String unitsPreference = "IMPERIAL-UNITS";
112 public static AltosFrequency[] load_common_frequencies() {
113 AltosFrequency[] frequencies = null;
114 boolean existing = false;
115 existing = backend.nodeExists(common_frequencies_node_name);
118 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
119 int count = node.getInt(frequency_count, 0);
121 frequencies = new AltosFrequency[count];
122 for (int i = 0; i < count; i++) {
126 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
127 description = node.getString(String.format(description_format, i), null);
128 frequencies[i] = new AltosFrequency(frequency, description);
131 frequencies = new AltosFrequency[10];
132 for (int i = 0; i < 10; i++) {
133 frequencies[i] = new AltosFrequency(434.550 + i * .1,
134 String.format("Channel %d", i));
140 public static void save_common_frequencies(AltosFrequency[] frequencies) {
141 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
143 node.putInt(frequency_count, frequencies.length);
144 for (int i = 0; i < frequencies.length; i++) {
145 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
146 node.putString(String.format(description_format, i), frequencies[i].description);
149 public static int launcher_serial;
151 public static int launcher_channel;
153 public static void init(AltosPreferencesBackend in_backend) {
154 backend = in_backend;
156 /* Initialize logdir from preferences */
157 String logdir_string = backend.getString(logdirPreference, null);
158 if (logdir_string != null)
159 logdir = new File(logdir_string);
161 logdir = new File(backend.homeDirectory(), logdirName);
162 if (!logdir.exists())
165 mapdir = new File(logdir, "maps");
166 if (!mapdir.exists())
169 frequencies = new Hashtable<Integer, Double>();
171 telemetries = new Hashtable<Integer,Integer>();
173 telemetry_rates = new Hashtable<Integer,Integer>();
175 voice = backend.getBoolean(voicePreference, true);
177 callsign = backend.getString(callsignPreference,"N0CALL");
179 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
181 scanning_telemetry_rate = backend.getInt(scanningTelemetryRatePreference,(1 << AltosLib.ao_telemetry_rate_38400));
183 launcher_serial = backend.getInt(launcherSerialPreference, 0);
185 launcher_channel = backend.getInt(launcherChannelPreference, 0);
187 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
188 if (firmwaredir_string != null)
189 firmwaredir = new File(firmwaredir_string);
193 common_frequencies = load_common_frequencies();
195 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
198 public static void flush_preferences() {
202 public static void set_logdir(File new_logdir) {
203 synchronized (backend) {
205 mapdir = new File(logdir, "maps");
206 if (!mapdir.exists())
208 backend.putString(logdirPreference, logdir.getPath());
213 public static File logdir() {
214 synchronized (backend) {
219 public static File last_logdir() {
220 synchronized (backend) {
221 if (last_logdir == null)
222 last_logdir = logdir;
227 public static void set_last_logdir(File file) {
228 synchronized(backend) {
229 if (file != null && !file.isDirectory())
230 file = file.getParentFile();
232 file = new File(".");
237 public static File mapdir() {
238 synchronized (backend) {
243 public static void set_frequency(int serial, double new_frequency) {
244 synchronized (backend) {
245 frequencies.put(serial, new_frequency);
246 backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
251 public static double frequency(int serial) {
252 synchronized (backend) {
253 if (frequencies.containsKey(serial))
254 return frequencies.get(serial);
255 double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
256 if (frequency == 0.0) {
257 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
258 frequency = AltosConvert.radio_channel_to_frequency(channel);
260 frequencies.put(serial, frequency);
265 public static void set_telemetry(int serial, int new_telemetry) {
266 synchronized (backend) {
267 telemetries.put(serial, new_telemetry);
268 backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
273 public static int telemetry(int serial) {
274 synchronized (backend) {
275 if (telemetries.containsKey(serial))
276 return telemetries.get(serial);
277 int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
278 AltosLib.ao_telemetry_standard);
279 telemetries.put(serial, telemetry);
284 public static void set_telemetry_rate(int serial, int new_telemetry_rate) {
285 synchronized (backend) {
286 telemetry_rates.put(serial, new_telemetry_rate);
287 backend.putInt(String.format(telemetryRatePreferenceFormat, serial), new_telemetry_rate);
292 public static int telemetry_rate(int serial) {
293 synchronized (backend) {
294 if (telemetry_rates.containsKey(serial))
295 return telemetry_rates.get(serial);
296 int telemetry_rate = backend.getInt(String.format(telemetryRatePreferenceFormat, serial),
297 AltosLib.ao_telemetry_rate_38400);
298 telemetry_rates.put(serial, telemetry_rate);
299 return telemetry_rate;
303 public static void set_scanning_telemetry(int new_scanning_telemetry) {
304 synchronized (backend) {
305 scanning_telemetry = new_scanning_telemetry;
306 backend.putInt(scanningTelemetryPreference, scanning_telemetry);
311 public static int scanning_telemetry() {
312 synchronized (backend) {
313 return scanning_telemetry;
317 public static void set_scanning_telemetry_rate(int new_scanning_telemetry_rate) {
318 synchronized (backend) {
319 scanning_telemetry_rate = new_scanning_telemetry_rate;
320 backend.putInt(scanningTelemetryRatePreference, scanning_telemetry_rate);
325 public static int scanning_telemetry_rate() {
326 synchronized(backend) {
327 return scanning_telemetry_rate;
331 public static void set_voice(boolean new_voice) {
332 synchronized (backend) {
334 backend.putBoolean(voicePreference, voice);
339 public static boolean voice() {
340 synchronized (backend) {
345 public static void set_callsign(String new_callsign) {
346 synchronized(backend) {
347 callsign = new_callsign;
348 backend.putString(callsignPreference, callsign);
353 public static String callsign() {
354 synchronized(backend) {
359 public static void set_firmwaredir(File new_firmwaredir) {
360 synchronized (backend) {
361 firmwaredir = new_firmwaredir;
362 backend.putString(firmwaredirPreference, firmwaredir.getPath());
367 public static File firmwaredir() {
368 synchronized (backend) {
373 public static void set_launcher_serial(int new_launcher_serial) {
374 synchronized (backend) {
375 launcher_serial = new_launcher_serial;
376 backend.putInt(launcherSerialPreference, launcher_serial);
381 public static int launcher_serial() {
382 synchronized (backend) {
383 return launcher_serial;
387 public static void set_launcher_channel(int new_launcher_channel) {
388 synchronized (backend) {
389 launcher_channel = new_launcher_channel;
390 backend.putInt(launcherChannelPreference, launcher_channel);
395 public static int launcher_channel() {
396 synchronized (backend) {
397 return launcher_channel;
401 public static AltosPreferencesBackend bt_devices() {
402 synchronized (backend) {
403 return backend.node("bt_devices");
407 public static AltosFrequency[] common_frequencies() {
408 synchronized (backend) {
409 return common_frequencies;
413 public static void set_common_frequencies(AltosFrequency[] frequencies) {
414 synchronized(backend) {
415 common_frequencies = frequencies;
416 save_common_frequencies(frequencies);
421 public static void add_common_frequency(AltosFrequency frequency) {
422 AltosFrequency[] old_frequencies = common_frequencies();
423 AltosFrequency[] new_frequencies = new AltosFrequency[old_frequencies.length + 1];
426 for (i = 0; i < old_frequencies.length; i++) {
427 if (frequency.frequency == old_frequencies[i].frequency)
429 if (frequency.frequency < old_frequencies[i].frequency)
431 new_frequencies[i] = old_frequencies[i];
433 new_frequencies[i] = frequency;
434 for (; i < old_frequencies.length; i++)
435 new_frequencies[i+1] = old_frequencies[i];
436 set_common_frequencies(new_frequencies);
439 static LinkedList<AltosUnitsListener> units_listeners;
441 public static boolean imperial_units() {
442 synchronized(backend) {
443 return AltosConvert.imperial_units;
447 public static void set_imperial_units(boolean imperial_units) {
448 synchronized (backend) {
449 AltosConvert.imperial_units = imperial_units;
450 backend.putBoolean(unitsPreference, imperial_units);
453 if (units_listeners != null) {
454 for (AltosUnitsListener l : units_listeners) {
455 l.units_changed(imperial_units);
460 public static void register_units_listener(AltosUnitsListener l) {
461 synchronized(backend) {
462 if (units_listeners == null)
463 units_listeners = new LinkedList<AltosUnitsListener>();
464 units_listeners.add(l);
468 public static void unregister_units_listener(AltosUnitsListener l) {
469 synchronized(backend) {
470 units_listeners.remove(l);