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;
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 /* voice preference name */
39 public final static String voicePreference = "VOICE";
41 /* callsign preference name */
42 public final static String callsignPreference = "CALLSIGN";
44 /* firmware directory preference name */
45 public final static String firmwaredirPreference = "FIRMWARE";
47 /* serial debug preference name */
48 public final static String serialDebugPreference = "SERIAL-DEBUG";
50 /* scanning telemetry preferences name */
51 public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
53 /* Launcher serial preference name */
54 public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
56 /* Launcher channel preference name */
57 public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
59 /* Default logdir is ~/TeleMetrum */
60 public final static String logdirName = "TeleMetrum";
63 public static File logdir;
65 /* Map directory -- hangs of logdir */
66 public static File mapdir;
68 /* Frequency (map serial to frequency) */
69 public static Hashtable<Integer, Double> frequencies;
71 /* Telemetry (map serial to telemetry format) */
72 public static Hashtable<Integer, Integer> telemetries;
74 /* Voice preference */
75 public static boolean voice;
77 /* Callsign preference */
78 public static String callsign;
80 /* Firmware directory */
81 public static File firmwaredir;
83 /* Scanning telemetry */
84 public static int scanning_telemetry;
86 /* List of frequencies */
87 public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
88 public static AltosFrequency[] common_frequencies;
90 public final static String frequency_count = "COUNT";
91 public final static String frequency_format = "FREQUENCY-%d";
92 public final static String description_format = "DESCRIPTION-%d";
94 /* Units preference */
96 public final static String unitsPreference = "IMPERIAL-UNITS";
98 public static AltosFrequency[] load_common_frequencies() {
99 AltosFrequency[] frequencies = null;
100 boolean existing = false;
101 existing = backend.nodeExists(common_frequencies_node_name);
104 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
105 int count = node.getInt(frequency_count, 0);
107 frequencies = new AltosFrequency[count];
108 for (int i = 0; i < count; i++) {
112 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
113 description = node.getString(String.format(description_format, i), null);
114 frequencies[i] = new AltosFrequency(frequency, description);
117 frequencies = new AltosFrequency[10];
118 for (int i = 0; i < 10; i++) {
119 frequencies[i] = new AltosFrequency(434.550 + i * .1,
120 String.format("Channel %d", i));
126 public static void save_common_frequencies(AltosFrequency[] frequencies) {
127 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
129 node.putInt(frequency_count, frequencies.length);
130 for (int i = 0; i < frequencies.length; i++) {
131 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
132 node.putString(String.format(description_format, i), frequencies[i].description);
135 public static int launcher_serial;
137 public static int launcher_channel;
139 public static void init(AltosPreferencesBackend in_backend) {
140 backend = in_backend;
142 /* Initialize logdir from preferences */
143 String logdir_string = backend.getString(logdirPreference, null);
144 if (logdir_string != null)
145 logdir = new File(logdir_string);
147 logdir = new File(backend.homeDirectory(), logdirName);
148 if (!logdir.exists())
151 mapdir = new File(logdir, "maps");
152 if (!mapdir.exists())
155 frequencies = new Hashtable<Integer, Double>();
157 telemetries = new Hashtable<Integer,Integer>();
159 voice = backend.getBoolean(voicePreference, true);
161 callsign = backend.getString(callsignPreference,"N0CALL");
163 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
165 launcher_serial = backend.getInt(launcherSerialPreference, 0);
167 launcher_channel = backend.getInt(launcherChannelPreference, 0);
169 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
170 if (firmwaredir_string != null)
171 firmwaredir = new File(firmwaredir_string);
175 common_frequencies = load_common_frequencies();
177 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
180 public static void flush_preferences() {
184 public static void set_logdir(File new_logdir) {
185 synchronized (backend) {
187 mapdir = new File(logdir, "maps");
188 if (!mapdir.exists())
190 backend.putString(logdirPreference, logdir.getPath());
195 public static File logdir() {
196 synchronized (backend) {
201 public static File mapdir() {
202 synchronized (backend) {
207 public static void set_frequency(int serial, double new_frequency) {
208 synchronized (backend) {
209 frequencies.put(serial, new_frequency);
210 backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
215 public static double frequency(int serial) {
216 synchronized (backend) {
217 if (frequencies.containsKey(serial))
218 return frequencies.get(serial);
219 double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
220 if (frequency == 0.0) {
221 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
222 frequency = AltosConvert.radio_channel_to_frequency(channel);
224 frequencies.put(serial, frequency);
229 public static void set_telemetry(int serial, int new_telemetry) {
230 synchronized (backend) {
231 telemetries.put(serial, new_telemetry);
232 backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
237 public static int telemetry(int serial) {
238 synchronized (backend) {
239 if (telemetries.containsKey(serial))
240 return telemetries.get(serial);
241 int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
242 AltosLib.ao_telemetry_standard);
243 telemetries.put(serial, telemetry);
248 public static void set_scanning_telemetry(int new_scanning_telemetry) {
249 synchronized (backend) {
250 scanning_telemetry = new_scanning_telemetry;
251 backend.putInt(scanningTelemetryPreference, scanning_telemetry);
256 public static int scanning_telemetry() {
257 synchronized (backend) {
258 return scanning_telemetry;
262 public static void set_voice(boolean new_voice) {
263 synchronized (backend) {
265 backend.putBoolean(voicePreference, voice);
270 public static boolean voice() {
271 synchronized (backend) {
276 public static void set_callsign(String new_callsign) {
277 synchronized(backend) {
278 callsign = new_callsign;
279 backend.putString(callsignPreference, callsign);
284 public static String callsign() {
285 synchronized(backend) {
290 public static void set_firmwaredir(File new_firmwaredir) {
291 synchronized (backend) {
292 firmwaredir = new_firmwaredir;
293 backend.putString(firmwaredirPreference, firmwaredir.getPath());
298 public static File firmwaredir() {
299 synchronized (backend) {
304 public static void set_launcher_serial(int new_launcher_serial) {
305 synchronized (backend) {
306 launcher_serial = new_launcher_serial;
307 backend.putInt(launcherSerialPreference, launcher_serial);
312 public static int launcher_serial() {
313 synchronized (backend) {
314 return launcher_serial;
318 public static void set_launcher_channel(int new_launcher_channel) {
319 synchronized (backend) {
320 launcher_channel = new_launcher_channel;
321 backend.putInt(launcherChannelPreference, launcher_channel);
326 public static int launcher_channel() {
327 synchronized (backend) {
328 return launcher_channel;
332 public static AltosPreferencesBackend bt_devices() {
333 synchronized (backend) {
334 return backend.node("bt_devices");
338 public static AltosFrequency[] common_frequencies() {
339 synchronized (backend) {
340 return common_frequencies;
344 public static void set_common_frequencies(AltosFrequency[] frequencies) {
345 synchronized(backend) {
346 common_frequencies = frequencies;
347 save_common_frequencies(frequencies);
352 public static void add_common_frequency(AltosFrequency frequency) {
353 AltosFrequency[] old_frequencies = common_frequencies();
354 AltosFrequency[] new_frequencies = new AltosFrequency[old_frequencies.length + 1];
357 for (i = 0; i < old_frequencies.length; i++) {
358 if (frequency.frequency == old_frequencies[i].frequency)
360 if (frequency.frequency < old_frequencies[i].frequency)
362 new_frequencies[i] = old_frequencies[i];
364 new_frequencies[i] = frequency;
365 for (; i < old_frequencies.length; i++)
366 new_frequencies[i+1] = old_frequencies[i];
367 set_common_frequencies(new_frequencies);
370 static LinkedList<AltosUnitsListener> units_listeners;
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);
384 if (units_listeners != null) {
385 for (AltosUnitsListener l : units_listeners) {
386 l.units_changed(imperial_units);
391 public static void register_units_listener(AltosUnitsListener l) {
392 synchronized(backend) {
393 if (units_listeners == null)
394 units_listeners = new LinkedList<AltosUnitsListener>();
395 units_listeners.add(l);
399 public static void unregister_units_listener(AltosUnitsListener l) {
400 synchronized(backend) {
401 units_listeners.remove(l);