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() {
141 //preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
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);
184 public static void flush_preferences() {
188 public static void set_logdir(File new_logdir) {
189 synchronized (backend) {
191 mapdir = new File(logdir, "maps");
192 if (!mapdir.exists())
194 backend.putString(logdirPreference, logdir.getPath());
199 public static File logdir() {
200 synchronized (backend) {
205 public static File mapdir() {
206 synchronized (backend) {
211 public static void set_frequency(int serial, double new_frequency) {
212 synchronized (backend) {
213 frequencies.put(serial, new_frequency);
214 backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
219 public static double frequency(int serial) {
220 synchronized (backend) {
221 if (frequencies.containsKey(serial))
222 return frequencies.get(serial);
223 double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
224 if (frequency == 0.0) {
225 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
226 frequency = AltosConvert.radio_channel_to_frequency(channel);
228 frequencies.put(serial, frequency);
233 public static void set_telemetry(int serial, int new_telemetry) {
234 synchronized (backend) {
235 telemetries.put(serial, new_telemetry);
236 backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
241 public static int telemetry(int serial) {
242 synchronized (backend) {
243 if (telemetries.containsKey(serial))
244 return telemetries.get(serial);
245 int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
246 AltosLib.ao_telemetry_standard);
247 telemetries.put(serial, telemetry);
252 public static void set_scanning_telemetry(int new_scanning_telemetry) {
253 synchronized (backend) {
254 scanning_telemetry = new_scanning_telemetry;
255 backend.putInt(scanningTelemetryPreference, scanning_telemetry);
260 public static int scanning_telemetry() {
261 synchronized (backend) {
262 return scanning_telemetry;
266 public static void set_voice(boolean new_voice) {
267 synchronized (backend) {
269 backend.putBoolean(voicePreference, voice);
274 public static boolean voice() {
275 synchronized (backend) {
280 public static void set_callsign(String new_callsign) {
281 synchronized(backend) {
282 callsign = new_callsign;
283 backend.putString(callsignPreference, callsign);
288 public static String callsign() {
289 synchronized(backend) {
294 public static void set_firmwaredir(File new_firmwaredir) {
295 synchronized (backend) {
296 firmwaredir = new_firmwaredir;
297 backend.putString(firmwaredirPreference, firmwaredir.getPath());
302 public static File firmwaredir() {
303 synchronized (backend) {
308 public static void set_launcher_serial(int new_launcher_serial) {
309 synchronized (backend) {
310 launcher_serial = new_launcher_serial;
311 backend.putInt(launcherSerialPreference, launcher_serial);
316 public static int launcher_serial() {
317 synchronized (backend) {
318 return launcher_serial;
322 public static void set_launcher_channel(int new_launcher_channel) {
323 synchronized (backend) {
324 launcher_channel = new_launcher_channel;
325 backend.putInt(launcherChannelPreference, launcher_channel);
330 public static int launcher_channel() {
331 synchronized (backend) {
332 return launcher_channel;
336 public static AltosPreferencesBackend bt_devices() {
337 synchronized (backend) {
338 return backend.node("bt_devices");
342 public static AltosFrequency[] common_frequencies() {
343 synchronized (backend) {
344 return common_frequencies;
348 public static void set_common_frequencies(AltosFrequency[] frequencies) {
349 synchronized(backend) {
350 common_frequencies = frequencies;
351 save_common_frequencies(frequencies);
356 public static void add_common_frequency(AltosFrequency frequency) {
357 AltosFrequency[] old_frequencies = common_frequencies();
358 AltosFrequency[] new_frequencies = new AltosFrequency[old_frequencies.length + 1];
361 for (i = 0; i < old_frequencies.length; i++) {
362 if (frequency.frequency == old_frequencies[i].frequency)
364 if (frequency.frequency < old_frequencies[i].frequency)
366 new_frequencies[i] = old_frequencies[i];
368 new_frequencies[i] = frequency;
369 for (; i < old_frequencies.length; i++)
370 new_frequencies[i+1] = old_frequencies[i];
371 set_common_frequencies(new_frequencies);
374 public static boolean imperial_units() {
375 synchronized(backend) {
376 return AltosConvert.imperial_units;
380 public static void set_imperial_units(boolean imperial_units) {
381 synchronized (backend) {
382 AltosConvert.imperial_units = imperial_units;
383 backend.putBoolean(unitsPreference, imperial_units);