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;
22 import javax.swing.filechooser.FileSystemView;
24 public class AltosPreferences {
25 public static AltosPreferencesBackend backend = null;
27 /* logdir preference name */
28 public final static String logdirPreference = "LOGDIR";
30 /* channel preference name */
31 public final static String channelPreferenceFormat = "CHANNEL-%d";
33 /* frequency preference name */
34 public final static String frequencyPreferenceFormat = "FREQUENCY-%d";
36 /* telemetry format preference name */
37 public final static String telemetryPreferenceFormat = "TELEMETRY-%d";
39 /* voice preference name */
40 public final static String voicePreference = "VOICE";
42 /* callsign preference name */
43 public final static String callsignPreference = "CALLSIGN";
45 /* firmware directory preference name */
46 public final static String firmwaredirPreference = "FIRMWARE";
48 /* serial debug preference name */
49 public final static String serialDebugPreference = "SERIAL-DEBUG";
51 /* scanning telemetry preferences name */
52 public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
54 /* Launcher serial preference name */
55 public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
57 /* Launcher channel preference name */
58 public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
60 /* Default logdir is ~/TeleMetrum */
61 public final static String logdirName = "TeleMetrum";
64 public static File logdir;
66 /* Map directory -- hangs of logdir */
67 public static File mapdir;
69 /* Frequency (map serial to frequency) */
70 public static Hashtable<Integer, Double> frequencies;
72 /* Telemetry (map serial to telemetry format) */
73 public static Hashtable<Integer, Integer> telemetries;
75 /* Voice preference */
76 public static boolean voice;
78 /* Callsign preference */
79 public static String callsign;
81 /* Firmware directory */
82 public static File firmwaredir;
84 /* Scanning telemetry */
85 public static int scanning_telemetry;
87 /* List of frequencies */
88 public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
89 public static AltosFrequency[] common_frequencies;
91 public final static String frequency_count = "COUNT";
92 public final static String frequency_format = "FREQUENCY-%d";
93 public final static String description_format = "DESCRIPTION-%d";
95 /* Units preference */
97 public final static String unitsPreference = "IMPERIAL-UNITS";
99 public static AltosFrequency[] load_common_frequencies() {
100 AltosFrequency[] frequencies = null;
101 boolean existing = false;
102 existing = backend.nodeExists(common_frequencies_node_name);
105 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
106 int count = node.getInt(frequency_count, 0);
108 frequencies = new AltosFrequency[count];
109 for (int i = 0; i < count; i++) {
113 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
114 description = node.getString(String.format(description_format, i), null);
115 frequencies[i] = new AltosFrequency(frequency, description);
118 frequencies = new AltosFrequency[10];
119 for (int i = 0; i < 10; i++) {
120 frequencies[i] = new AltosFrequency(434.550 + i * .1,
121 String.format("Channel %d", i));
127 public static void save_common_frequencies(AltosFrequency[] frequencies) {
128 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
130 node.putInt(frequency_count, frequencies.length);
131 for (int i = 0; i < frequencies.length; i++) {
132 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
133 node.putString(String.format(description_format, i), frequencies[i].description);
136 public static int launcher_serial;
138 public static int launcher_channel;
140 public static void init(AltosPreferencesBackend in_backend) {
141 backend = in_backend;
143 /* Initialize logdir from preferences */
144 String logdir_string = backend.getString(logdirPreference, null);
145 if (logdir_string != null)
146 logdir = new File(logdir_string);
148 /* Use the file system view default directory */
149 logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
150 if (!logdir.exists())
153 mapdir = new File(logdir, "maps");
154 if (!mapdir.exists())
157 frequencies = new Hashtable<Integer, Double>();
159 telemetries = new Hashtable<Integer,Integer>();
161 voice = backend.getBoolean(voicePreference, true);
163 callsign = backend.getString(callsignPreference,"N0CALL");
165 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
167 launcher_serial = backend.getInt(launcherSerialPreference, 0);
169 launcher_channel = backend.getInt(launcherChannelPreference, 0);
171 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
172 if (firmwaredir_string != null)
173 firmwaredir = new File(firmwaredir_string);
177 common_frequencies = load_common_frequencies();
179 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
182 public static void flush_preferences() {
186 public static void set_logdir(File new_logdir) {
187 synchronized (backend) {
189 mapdir = new File(logdir, "maps");
190 if (!mapdir.exists())
192 backend.putString(logdirPreference, logdir.getPath());
197 public static File logdir() {
198 synchronized (backend) {
203 public static File mapdir() {
204 synchronized (backend) {
209 public static void set_frequency(int serial, double new_frequency) {
210 synchronized (backend) {
211 frequencies.put(serial, new_frequency);
212 backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
217 public static double frequency(int serial) {
218 synchronized (backend) {
219 if (frequencies.containsKey(serial))
220 return frequencies.get(serial);
221 double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
222 if (frequency == 0.0) {
223 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
224 frequency = AltosConvert.radio_channel_to_frequency(channel);
226 frequencies.put(serial, frequency);
231 public static void set_telemetry(int serial, int new_telemetry) {
232 synchronized (backend) {
233 telemetries.put(serial, new_telemetry);
234 backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
239 public static int telemetry(int serial) {
240 synchronized (backend) {
241 if (telemetries.containsKey(serial))
242 return telemetries.get(serial);
243 int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
244 AltosLib.ao_telemetry_standard);
245 telemetries.put(serial, telemetry);
250 public static void set_scanning_telemetry(int new_scanning_telemetry) {
251 synchronized (backend) {
252 scanning_telemetry = new_scanning_telemetry;
253 backend.putInt(scanningTelemetryPreference, scanning_telemetry);
258 public static int scanning_telemetry() {
259 synchronized (backend) {
260 return scanning_telemetry;
264 public static void set_voice(boolean new_voice) {
265 synchronized (backend) {
267 backend.putBoolean(voicePreference, voice);
272 public static boolean voice() {
273 synchronized (backend) {
278 public static void set_callsign(String new_callsign) {
279 synchronized(backend) {
280 callsign = new_callsign;
281 backend.putString(callsignPreference, callsign);
286 public static String callsign() {
287 synchronized(backend) {
292 public static void set_firmwaredir(File new_firmwaredir) {
293 synchronized (backend) {
294 firmwaredir = new_firmwaredir;
295 backend.putString(firmwaredirPreference, firmwaredir.getPath());
300 public static File firmwaredir() {
301 synchronized (backend) {
306 public static void set_launcher_serial(int new_launcher_serial) {
307 synchronized (backend) {
308 launcher_serial = new_launcher_serial;
309 backend.putInt(launcherSerialPreference, launcher_serial);
314 public static int launcher_serial() {
315 synchronized (backend) {
316 return launcher_serial;
320 public static void set_launcher_channel(int new_launcher_channel) {
321 synchronized (backend) {
322 launcher_channel = new_launcher_channel;
323 backend.putInt(launcherChannelPreference, launcher_channel);
328 public static int launcher_channel() {
329 synchronized (backend) {
330 return launcher_channel;
334 public static AltosPreferencesBackend bt_devices() {
335 synchronized (backend) {
336 return backend.node("bt_devices");
340 public static AltosFrequency[] common_frequencies() {
341 synchronized (backend) {
342 return common_frequencies;
346 public static void set_common_frequencies(AltosFrequency[] frequencies) {
347 synchronized(backend) {
348 common_frequencies = frequencies;
349 save_common_frequencies(frequencies);
354 public static void add_common_frequency(AltosFrequency frequency) {
355 AltosFrequency[] old_frequencies = common_frequencies();
356 AltosFrequency[] new_frequencies = new AltosFrequency[old_frequencies.length + 1];
359 for (i = 0; i < old_frequencies.length; i++) {
360 if (frequency.frequency == old_frequencies[i].frequency)
362 if (frequency.frequency < old_frequencies[i].frequency)
364 new_frequencies[i] = old_frequencies[i];
366 new_frequencies[i] = frequency;
367 for (; i < old_frequencies.length; i++)
368 new_frequencies[i+1] = old_frequencies[i];
369 set_common_frequencies(new_frequencies);
372 public static boolean imperial_units() {
373 synchronized(backend) {
374 return AltosConvert.imperial_units;
378 public static void set_imperial_units(boolean imperial_units) {
379 synchronized (backend) {
380 AltosConvert.imperial_units = imperial_units;
381 backend.putBoolean(unitsPreference, imperial_units);