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_3;
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 /* Last log directory - use this next time we open or save something */
66 public static File last_logdir;
68 /* Map directory -- hangs of logdir */
69 public static File mapdir;
71 /* Frequency (map serial to frequency) */
72 public static Hashtable<Integer, Double> frequencies;
74 /* Telemetry (map serial to telemetry format) */
75 public static Hashtable<Integer, Integer> telemetries;
77 /* Voice preference */
78 public static boolean voice;
80 /* Callsign preference */
81 public static String callsign;
83 /* Firmware directory */
84 public static File firmwaredir;
86 /* Scanning telemetry */
87 public static int scanning_telemetry;
89 /* List of frequencies */
90 public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
91 public static AltosFrequency[] common_frequencies;
93 public final static String frequency_count = "COUNT";
94 public final static String frequency_format = "FREQUENCY-%d";
95 public final static String description_format = "DESCRIPTION-%d";
97 /* Units preference */
99 public final static String unitsPreference = "IMPERIAL-UNITS";
101 public static AltosFrequency[] load_common_frequencies() {
102 AltosFrequency[] frequencies = null;
103 boolean existing = false;
104 existing = backend.nodeExists(common_frequencies_node_name);
107 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
108 int count = node.getInt(frequency_count, 0);
110 frequencies = new AltosFrequency[count];
111 for (int i = 0; i < count; i++) {
115 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
116 description = node.getString(String.format(description_format, i), null);
117 frequencies[i] = new AltosFrequency(frequency, description);
120 frequencies = new AltosFrequency[10];
121 for (int i = 0; i < 10; i++) {
122 frequencies[i] = new AltosFrequency(434.550 + i * .1,
123 String.format("Channel %d", i));
129 public static void save_common_frequencies(AltosFrequency[] frequencies) {
130 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
132 node.putInt(frequency_count, frequencies.length);
133 for (int i = 0; i < frequencies.length; i++) {
134 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
135 node.putString(String.format(description_format, i), frequencies[i].description);
138 public static int launcher_serial;
140 public static int launcher_channel;
142 public static void init(AltosPreferencesBackend in_backend) {
143 backend = in_backend;
145 /* Initialize logdir from preferences */
146 String logdir_string = backend.getString(logdirPreference, null);
147 if (logdir_string != null)
148 logdir = new File(logdir_string);
150 logdir = new File(backend.homeDirectory(), logdirName);
151 if (!logdir.exists())
154 mapdir = new File(logdir, "maps");
155 if (!mapdir.exists())
158 frequencies = new Hashtable<Integer, Double>();
160 telemetries = new Hashtable<Integer,Integer>();
162 voice = backend.getBoolean(voicePreference, true);
164 callsign = backend.getString(callsignPreference,"N0CALL");
166 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
168 launcher_serial = backend.getInt(launcherSerialPreference, 0);
170 launcher_channel = backend.getInt(launcherChannelPreference, 0);
172 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
173 if (firmwaredir_string != null)
174 firmwaredir = new File(firmwaredir_string);
178 common_frequencies = load_common_frequencies();
180 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
183 public static void flush_preferences() {
187 public static void set_logdir(File new_logdir) {
188 synchronized (backend) {
190 mapdir = new File(logdir, "maps");
191 if (!mapdir.exists())
193 backend.putString(logdirPreference, logdir.getPath());
198 public static File logdir() {
199 synchronized (backend) {
204 public static File last_logdir() {
205 synchronized (backend) {
206 if (last_logdir == null)
207 last_logdir = logdir;
212 public static void set_last_logdir(File file) {
213 synchronized(backend) {
214 if (file != null && !file.isDirectory())
215 file = file.getParentFile();
217 file = new File(".");
222 public static File mapdir() {
223 synchronized (backend) {
228 public static void set_frequency(int serial, double new_frequency) {
229 synchronized (backend) {
230 frequencies.put(serial, new_frequency);
231 backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
236 public static double frequency(int serial) {
237 synchronized (backend) {
238 if (frequencies.containsKey(serial))
239 return frequencies.get(serial);
240 double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
241 if (frequency == 0.0) {
242 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
243 frequency = AltosConvert.radio_channel_to_frequency(channel);
245 frequencies.put(serial, frequency);
250 public static void set_telemetry(int serial, int new_telemetry) {
251 synchronized (backend) {
252 telemetries.put(serial, new_telemetry);
253 backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
258 public static int telemetry(int serial) {
259 synchronized (backend) {
260 if (telemetries.containsKey(serial))
261 return telemetries.get(serial);
262 int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
263 AltosLib.ao_telemetry_standard);
264 telemetries.put(serial, telemetry);
269 public static void set_scanning_telemetry(int new_scanning_telemetry) {
270 synchronized (backend) {
271 scanning_telemetry = new_scanning_telemetry;
272 backend.putInt(scanningTelemetryPreference, scanning_telemetry);
277 public static int scanning_telemetry() {
278 synchronized (backend) {
279 return scanning_telemetry;
283 public static void set_voice(boolean new_voice) {
284 synchronized (backend) {
286 backend.putBoolean(voicePreference, voice);
291 public static boolean voice() {
292 synchronized (backend) {
297 public static void set_callsign(String new_callsign) {
298 synchronized(backend) {
299 callsign = new_callsign;
300 backend.putString(callsignPreference, callsign);
305 public static String callsign() {
306 synchronized(backend) {
311 public static void set_firmwaredir(File new_firmwaredir) {
312 synchronized (backend) {
313 firmwaredir = new_firmwaredir;
314 backend.putString(firmwaredirPreference, firmwaredir.getPath());
319 public static File firmwaredir() {
320 synchronized (backend) {
325 public static void set_launcher_serial(int new_launcher_serial) {
326 synchronized (backend) {
327 launcher_serial = new_launcher_serial;
328 backend.putInt(launcherSerialPreference, launcher_serial);
333 public static int launcher_serial() {
334 synchronized (backend) {
335 return launcher_serial;
339 public static void set_launcher_channel(int new_launcher_channel) {
340 synchronized (backend) {
341 launcher_channel = new_launcher_channel;
342 backend.putInt(launcherChannelPreference, launcher_channel);
347 public static int launcher_channel() {
348 synchronized (backend) {
349 return launcher_channel;
353 public static AltosPreferencesBackend bt_devices() {
354 synchronized (backend) {
355 return backend.node("bt_devices");
359 public static AltosFrequency[] common_frequencies() {
360 synchronized (backend) {
361 return common_frequencies;
365 public static void set_common_frequencies(AltosFrequency[] frequencies) {
366 synchronized(backend) {
367 common_frequencies = frequencies;
368 save_common_frequencies(frequencies);
373 public static void add_common_frequency(AltosFrequency frequency) {
374 AltosFrequency[] old_frequencies = common_frequencies();
375 AltosFrequency[] new_frequencies = new AltosFrequency[old_frequencies.length + 1];
378 for (i = 0; i < old_frequencies.length; i++) {
379 if (frequency.frequency == old_frequencies[i].frequency)
381 if (frequency.frequency < old_frequencies[i].frequency)
383 new_frequencies[i] = old_frequencies[i];
385 new_frequencies[i] = frequency;
386 for (; i < old_frequencies.length; i++)
387 new_frequencies[i+1] = old_frequencies[i];
388 set_common_frequencies(new_frequencies);
391 static LinkedList<AltosUnitsListener> units_listeners;
393 public static boolean imperial_units() {
394 synchronized(backend) {
395 return AltosConvert.imperial_units;
399 public static void set_imperial_units(boolean imperial_units) {
400 synchronized (backend) {
401 AltosConvert.imperial_units = imperial_units;
402 backend.putBoolean(unitsPreference, imperial_units);
405 if (units_listeners != null) {
406 for (AltosUnitsListener l : units_listeners) {
407 l.units_changed(imperial_units);
412 public static void register_units_listener(AltosUnitsListener l) {
413 synchronized(backend) {
414 if (units_listeners == null)
415 units_listeners = new LinkedList<AltosUnitsListener>();
416 units_listeners.add(l);
420 public static void unregister_units_listener(AltosUnitsListener l) {
421 synchronized(backend) {
422 units_listeners.remove(l);