72cfeb4bc5a47e81c63c2ba0582e0de3438b5263
[fw/altos] / altoslib / AltosPreferences.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_5;
19
20 import java.io.*;
21 import java.util.*;
22
23 public class AltosPreferences {
24         public static AltosPreferencesBackend backend = null;
25
26         /* logdir preference name */
27         public final static String logdirPreference = "LOGDIR";
28
29         /* channel preference name */
30         public final static String channelPreferenceFormat = "CHANNEL-%d";
31
32         /* frequency preference name */
33         public final static String frequencyPreferenceFormat = "FREQUENCY-%d";
34
35         /* telemetry format preference name */
36         public final static String telemetryPreferenceFormat = "TELEMETRY-%d";
37
38         /* telemetry rate format preference name */
39         public final static String telemetryRatePreferenceFormat = "RATE-%d";
40
41         /* voice preference name */
42         public final static String voicePreference = "VOICE";
43
44         /* callsign preference name */
45         public final static String callsignPreference = "CALLSIGN";
46
47         /* firmware directory preference name */
48         public final static String firmwaredirPreference = "FIRMWARE";
49
50         /* serial debug preference name */
51         public final static String serialDebugPreference = "SERIAL-DEBUG";
52
53         /* scanning telemetry preferences name */
54         public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
55
56         /* scanning telemetry rate preferences name */
57         public final static String scanningTelemetryRatePreference = "SCANNING-RATE";
58
59         /* Launcher serial preference name */
60         public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
61
62         /* Launcher channel preference name */
63         public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
64
65         /* Default logdir is ~/TeleMetrum */
66         public final static String logdirName = "TeleMetrum";
67
68         /* Log directory */
69         public static File logdir;
70
71         /* Last log directory - use this next time we open or save something */
72         public static File last_logdir;
73
74         /* Map directory -- hangs of logdir */
75         public static File mapdir;
76
77         /* Frequency (map serial to frequency) */
78         public static Hashtable<Integer, Double> frequencies;
79
80         /* Telemetry (map serial to telemetry format) */
81         public static Hashtable<Integer, Integer> telemetries;
82
83         /* Telemetry rate (map serial to telemetry format) */
84         public static Hashtable<Integer, Integer> telemetry_rates;
85
86         /* Voice preference */
87         public static boolean voice;
88
89         /* Callsign preference */
90         public static String callsign;
91
92         /* Firmware directory */
93         public static File firmwaredir;
94
95         /* Scanning telemetry */
96         public static int scanning_telemetry;
97
98         public static int scanning_telemetry_rate;
99
100         /* List of frequencies */
101         public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
102         public static AltosFrequency[] common_frequencies;
103
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";
107
108         /* Units preference */
109
110         public final static String      unitsPreference = "IMPERIAL-UNITS";
111
112         public static AltosFrequency[] load_common_frequencies() {
113                 AltosFrequency[] frequencies = null;
114                 boolean existing = false;
115                 existing = backend.nodeExists(common_frequencies_node_name);
116
117                 if (existing) {
118                         AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
119                         int             count = node.getInt(frequency_count, 0);
120
121                         frequencies = new AltosFrequency[count];
122                         for (int i = 0; i < count; i++) {
123                                 double  frequency;
124                                 String  description;
125
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);
129                         }
130                 } else {
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));
135                         }
136                 }
137                 return frequencies;
138         }
139
140         public static void save_common_frequencies(AltosFrequency[] frequencies) {
141                 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
142
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);
147                 }
148         }
149         public static int launcher_serial;
150
151         public static int launcher_channel;
152
153         public static void init(AltosPreferencesBackend in_backend) {
154                 backend = in_backend;
155
156                 /* Initialize logdir from preferences */
157                 String logdir_string = backend.getString(logdirPreference, null);
158                 if (logdir_string != null)
159                         logdir = new File(logdir_string);
160                 else {
161                         logdir = new File(backend.homeDirectory(), logdirName);
162                         if (!logdir.exists())
163                                 logdir.mkdirs();
164                 }
165                 mapdir = new File(logdir, "maps");
166                 if (!mapdir.exists())
167                         mapdir.mkdirs();
168
169                 frequencies = new Hashtable<Integer, Double>();
170
171                 telemetries = new Hashtable<Integer,Integer>();
172
173                 telemetry_rates = new Hashtable<Integer,Integer>();
174
175                 voice = backend.getBoolean(voicePreference, true);
176
177                 callsign = backend.getString(callsignPreference,"N0CALL");
178
179                 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
180
181                 scanning_telemetry_rate = backend.getInt(scanningTelemetryRatePreference,(1 << AltosLib.ao_telemetry_rate_38400));
182
183                 launcher_serial = backend.getInt(launcherSerialPreference, 0);
184
185                 launcher_channel = backend.getInt(launcherChannelPreference, 0);
186
187                 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
188                 if (firmwaredir_string != null)
189                         firmwaredir = new File(firmwaredir_string);
190                 else
191                         firmwaredir = null;
192
193                 common_frequencies = load_common_frequencies();
194
195                 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
196         }
197
198         public static void flush_preferences() {
199                 backend.flush();
200         }
201
202         public static void set_logdir(File new_logdir) {
203                 synchronized (backend) {
204                         logdir = new_logdir;
205                         mapdir = new File(logdir, "maps");
206                         if (!mapdir.exists())
207                                 mapdir.mkdirs();
208                         backend.putString(logdirPreference, logdir.getPath());
209                         flush_preferences();
210                 }
211         }
212
213         public static File logdir() {
214                 synchronized (backend) {
215                         return logdir;
216                 }
217         }
218
219         public static File last_logdir() {
220                 synchronized (backend) {
221                         if (last_logdir == null)
222                                 last_logdir = logdir;
223                         return last_logdir;
224                 }
225         }
226
227         public static void set_last_logdir(File file) {
228                 synchronized(backend) {
229                         if (file != null && !file.isDirectory())
230                                 file = file.getParentFile();
231                         if (file == null)
232                                 file = new File(".");
233                         last_logdir = file;
234                 }
235         }
236
237         public static File mapdir() {
238                 synchronized (backend) {
239                         return mapdir;
240                 }
241         }
242
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);
247                         flush_preferences();
248                 }
249         }
250
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);
259                         }
260                         frequencies.put(serial, frequency);
261                         return frequency;
262                 }
263         }
264
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);
269                         flush_preferences();
270                 }
271         }
272
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);
280                         return telemetry;
281                 }
282         }
283
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);
288                         flush_preferences();
289                 }
290         }
291
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;
300                 }
301         }
302
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);
307                         flush_preferences();
308                 }
309         }
310
311         public static int scanning_telemetry() {
312                 synchronized (backend) {
313                         return scanning_telemetry;
314                 }
315         }
316
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);
321                         flush_preferences();
322                 }
323         }
324
325         public static int scanning_telemetry_rate() {
326                 synchronized(backend) {
327                         return scanning_telemetry_rate;
328                 }
329         }
330
331         public static void set_voice(boolean new_voice) {
332                 synchronized (backend) {
333                         voice = new_voice;
334                         backend.putBoolean(voicePreference, voice);
335                         flush_preferences();
336                 }
337         }
338
339         public static boolean voice() {
340                 synchronized (backend) {
341                         return voice;
342                 }
343         }
344
345         public static void set_callsign(String new_callsign) {
346                 synchronized(backend) {
347                         callsign = new_callsign;
348                         backend.putString(callsignPreference, callsign);
349                         flush_preferences();
350                 }
351         }
352
353         public static String callsign() {
354                 synchronized(backend) {
355                         return callsign;
356                 }
357         }
358
359         public static void set_firmwaredir(File new_firmwaredir) {
360                 synchronized (backend) {
361                         firmwaredir = new_firmwaredir;
362                         backend.putString(firmwaredirPreference, firmwaredir.getPath());
363                         flush_preferences();
364                 }
365         }
366
367         public static File firmwaredir() {
368                 synchronized (backend) {
369                         return firmwaredir;
370                 }
371         }
372
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);
377                         flush_preferences();
378                 }
379         }
380
381         public static int launcher_serial() {
382                 synchronized (backend) {
383                         return launcher_serial;
384                 }
385         }
386
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);
391                         flush_preferences();
392                 }
393         }
394
395         public static int launcher_channel() {
396                 synchronized (backend) {
397                         return launcher_channel;
398                 }
399         }
400
401         public static AltosPreferencesBackend bt_devices() {
402                 synchronized (backend) {
403                         return backend.node("bt_devices");
404                 }
405         }
406
407         public static AltosFrequency[] common_frequencies() {
408                 synchronized (backend) {
409                         return common_frequencies;
410                 }
411         }
412
413         public static void set_common_frequencies(AltosFrequency[] frequencies) {
414                 synchronized(backend) {
415                         common_frequencies = frequencies;
416                         save_common_frequencies(frequencies);
417                         flush_preferences();
418                 }
419         }
420
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];
424                 int                     i;
425
426                 for (i = 0; i < old_frequencies.length; i++) {
427                         if (frequency.frequency == old_frequencies[i].frequency)
428                                 return;
429                         if (frequency.frequency < old_frequencies[i].frequency)
430                                 break;
431                         new_frequencies[i] = old_frequencies[i];
432                 }
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);
437         }
438
439         static LinkedList<AltosUnitsListener> units_listeners;
440
441         public static boolean imperial_units() {
442                 synchronized(backend) {
443                         return AltosConvert.imperial_units;
444                 }
445         }
446
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);
451                         flush_preferences();
452                 }
453                 if (units_listeners != null) {
454                         for (AltosUnitsListener l : units_listeners) {
455                                 l.units_changed(imperial_units);
456                         }
457                 }
458         }
459
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);
465                 }
466         }
467
468         public static void unregister_units_listener(AltosUnitsListener l) {
469                 synchronized(backend) {
470                         units_listeners.remove(l);
471                 }
472         }
473 }