Bump java library versions
[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_6;
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         /* log file format preference name */
42         public final static String logfilePreferenceFormat = "LOGFILE-%d";
43
44         /* state preference name */
45         public final static String statePreferenceFormat = "STATE-%d";
46
47         /* voice preference name */
48         public final static String voicePreference = "VOICE";
49
50         /* callsign preference name */
51         public final static String callsignPreference = "CALLSIGN";
52
53         /* firmware directory preference name */
54         public final static String firmwaredirPreference = "FIRMWARE";
55
56         /* serial debug preference name */
57         public final static String serialDebugPreference = "SERIAL-DEBUG";
58
59         /* scanning telemetry preferences name */
60         public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
61
62         /* scanning telemetry rate preferences name */
63         public final static String scanningTelemetryRatePreference = "SCANNING-RATE";
64
65         /* Launcher serial preference name */
66         public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
67
68         /* Launcher channel preference name */
69         public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
70
71         /* Default logdir is ~/TeleMetrum */
72         public final static String logdirName = "TeleMetrum";
73
74         /* Log directory */
75         public static File logdir;
76
77         /* Last log directory - use this next time we open or save something */
78         public static File last_logdir;
79
80         /* Map directory -- hangs of logdir */
81         public static File mapdir;
82
83         /* Frequency (map serial to frequency) */
84         public static Hashtable<Integer, Double> frequencies;
85
86         /* Telemetry (map serial to telemetry format) */
87         public static Hashtable<Integer, Integer> telemetries;
88
89         /* Telemetry rate (map serial to telemetry format) */
90         public static Hashtable<Integer, Integer> telemetry_rates;
91
92         /* Log file (map serial to logfile name) */
93         public static Hashtable<Integer, File> logfiles;
94
95         /* Voice preference */
96         public static boolean voice;
97
98         /* Callsign preference */
99         public static String callsign;
100
101         /* Firmware directory */
102         public static File firmwaredir;
103
104         /* Scanning telemetry */
105         public static int scanning_telemetry;
106
107         public static int scanning_telemetry_rate;
108
109         /* List of frequencies */
110         public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
111         public static AltosFrequency[] common_frequencies;
112
113         public final static String      frequency_count = "COUNT";
114         public final static String      frequency_format = "FREQUENCY-%d";
115         public final static String      description_format = "DESCRIPTION-%d";
116
117         /* Units preference */
118
119         public final static String      unitsPreference = "IMPERIAL-UNITS";
120
121         public static AltosFrequency[] load_common_frequencies() {
122                 AltosFrequency[] frequencies = null;
123                 boolean existing = false;
124                 existing = backend.nodeExists(common_frequencies_node_name);
125
126                 if (existing) {
127                         AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
128                         int             count = node.getInt(frequency_count, 0);
129
130                         frequencies = new AltosFrequency[count];
131                         for (int i = 0; i < count; i++) {
132                                 double  frequency;
133                                 String  description;
134
135                                 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
136                                 description = node.getString(String.format(description_format, i), null);
137                                 frequencies[i] = new AltosFrequency(frequency, description);
138                         }
139                 } else {
140                         frequencies = new AltosFrequency[10];
141                         for (int i = 0; i < 10; i++) {
142                                 frequencies[i] = new AltosFrequency(434.550 + i * .1,
143                                                                            String.format("Channel %d", i));
144                         }
145                 }
146                 return frequencies;
147         }
148
149         public static void save_common_frequencies(AltosFrequency[] frequencies) {
150                 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
151
152                 node.putInt(frequency_count, frequencies.length);
153                 for (int i = 0; i < frequencies.length; i++) {
154                         node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
155                         node.putString(String.format(description_format, i), frequencies[i].description);
156                 }
157         }
158         public static int launcher_serial;
159
160         public static int launcher_channel;
161
162         public static void init(AltosPreferencesBackend in_backend) {
163
164                 if (backend != null)
165                         return;
166
167                 backend = in_backend;
168
169                 /* Initialize logdir from preferences */
170                 String logdir_string = backend.getString(logdirPreference, null);
171                 if (logdir_string != null)
172                         logdir = new File(logdir_string);
173                 else {
174                         logdir = new File(backend.homeDirectory(), logdirName);
175                         if (!logdir.exists())
176                                 logdir.mkdirs();
177                 }
178                 mapdir = new File(logdir, "maps");
179                 if (!mapdir.exists())
180                         mapdir.mkdirs();
181
182                 frequencies = new Hashtable<Integer, Double>();
183
184                 telemetries = new Hashtable<Integer,Integer>();
185
186                 telemetry_rates = new Hashtable<Integer,Integer>();
187
188                 logfiles = new Hashtable<Integer,File>();
189
190                 voice = backend.getBoolean(voicePreference, true);
191
192                 callsign = backend.getString(callsignPreference,"N0CALL");
193
194                 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
195
196                 scanning_telemetry_rate = backend.getInt(scanningTelemetryRatePreference,(1 << AltosLib.ao_telemetry_rate_38400));
197
198                 launcher_serial = backend.getInt(launcherSerialPreference, 0);
199
200                 launcher_channel = backend.getInt(launcherChannelPreference, 0);
201
202                 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
203                 if (firmwaredir_string != null)
204                         firmwaredir = new File(firmwaredir_string);
205                 else
206                         firmwaredir = null;
207
208                 common_frequencies = load_common_frequencies();
209
210                 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
211         }
212
213         public static void flush_preferences() {
214                 backend.flush();
215         }
216
217         public static void set_logdir(File new_logdir) {
218                 synchronized (backend) {
219                         logdir = new_logdir;
220                         mapdir = new File(logdir, "maps");
221                         if (!mapdir.exists())
222                                 mapdir.mkdirs();
223                         backend.putString(logdirPreference, logdir.getPath());
224                         flush_preferences();
225                 }
226         }
227
228         public static File logdir() {
229                 synchronized (backend) {
230                         return logdir;
231                 }
232         }
233
234         public static File last_logdir() {
235                 synchronized (backend) {
236                         if (last_logdir == null)
237                                 last_logdir = logdir;
238                         return last_logdir;
239                 }
240         }
241
242         public static void set_last_logdir(File file) {
243                 synchronized(backend) {
244                         if (file != null && !file.isDirectory())
245                                 file = file.getParentFile();
246                         if (file == null)
247                                 file = new File(".");
248                         last_logdir = file;
249                 }
250         }
251
252         public static File mapdir() {
253                 synchronized (backend) {
254                         return mapdir;
255                 }
256         }
257
258         public static void set_frequency(int serial, double new_frequency) {
259                 synchronized (backend) {
260                         frequencies.put(serial, new_frequency);
261                         backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
262                         flush_preferences();
263                 }
264         }
265
266         public static double frequency(int serial) {
267                 synchronized (backend) {
268                         if (frequencies.containsKey(serial))
269                                 return frequencies.get(serial);
270                         double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
271                         if (frequency == 0.0) {
272                                 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
273                                 frequency = AltosConvert.radio_channel_to_frequency(channel);
274                         }
275                         frequencies.put(serial, frequency);
276                         return frequency;
277                 }
278         }
279
280         public static void set_telemetry(int serial, int new_telemetry) {
281                 synchronized (backend) {
282                         telemetries.put(serial, new_telemetry);
283                         backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
284                         flush_preferences();
285                 }
286         }
287
288         public static int telemetry(int serial) {
289                 synchronized (backend) {
290                         if (telemetries.containsKey(serial))
291                                 return telemetries.get(serial);
292                         int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
293                                                    AltosLib.ao_telemetry_standard);
294                         telemetries.put(serial, telemetry);
295                         return telemetry;
296                 }
297         }
298
299         public static void set_telemetry_rate(int serial, int new_telemetry_rate) {
300                 synchronized (backend) {
301                         telemetry_rates.put(serial, new_telemetry_rate);
302                         backend.putInt(String.format(telemetryRatePreferenceFormat, serial), new_telemetry_rate);
303                         flush_preferences();
304                 }
305         }
306
307         public static int telemetry_rate(int serial) {
308                 synchronized (backend) {
309                         if (telemetry_rates.containsKey(serial))
310                                 return telemetry_rates.get(serial);
311                         int telemetry_rate = backend.getInt(String.format(telemetryRatePreferenceFormat, serial),
312                                                             AltosLib.ao_telemetry_rate_38400);
313                         telemetry_rates.put(serial, telemetry_rate);
314                         return telemetry_rate;
315                 }
316         }
317
318         public static void set_logfile(int serial, File new_logfile) {
319                 synchronized(backend) {
320                         logfiles.put(serial, new_logfile);
321                         backend.putString(String.format(logfilePreferenceFormat, serial), new_logfile.getPath());
322                         flush_preferences();
323                 }
324         }
325
326         public static File logfile(int serial) {
327                 synchronized(backend) {
328                         if (logfiles.containsKey(serial))
329                                 return logfiles.get(serial);
330                         String logfile_string = backend.getString(String.format(logfilePreferenceFormat, serial), null);
331                         if (logfile_string == null)
332                                 return null;
333                         File logfile = new File(logfile_string);
334                         logfiles.put(serial, logfile);
335                         return logfile;
336                 }
337         }
338
339         public static void set_state(int serial, AltosState state, AltosListenerState listener_state) {
340
341                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
342
343                 try {
344                         ObjectOutputStream oos = new ObjectOutputStream(baos);
345
346                         AltosSavedState saved_state = new AltosSavedState(state, listener_state);
347                         oos.writeObject(saved_state);
348
349                         byte[] bytes = baos.toByteArray();
350
351                         synchronized(backend) {
352                                 backend.putBytes(String.format(statePreferenceFormat, serial), bytes);
353                                 flush_preferences();
354                         }
355                 } catch (IOException ie) {
356                 }
357         }
358
359         public static AltosSavedState state(int serial) {
360                 byte[] bytes = null;
361
362                 synchronized(backend) {
363                         bytes = backend.getBytes(String.format(statePreferenceFormat, serial), null);
364                 }
365
366                 if (bytes == null)
367                         return null;
368
369                 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
370
371                 try {
372                         ObjectInputStream ois = new ObjectInputStream(bais);
373                         AltosSavedState saved_state = (AltosSavedState) ois.readObject();
374                         return saved_state;
375                 } catch (IOException ie) {
376                 } catch (ClassNotFoundException ce) {
377                 }
378                 return null;
379         }
380
381         public static void set_scanning_telemetry(int new_scanning_telemetry) {
382                 synchronized (backend) {
383                         scanning_telemetry = new_scanning_telemetry;
384                         backend.putInt(scanningTelemetryPreference, scanning_telemetry);
385                         flush_preferences();
386                 }
387         }
388
389         public static int scanning_telemetry() {
390                 synchronized (backend) {
391                         return scanning_telemetry;
392                 }
393         }
394
395         public static void set_scanning_telemetry_rate(int new_scanning_telemetry_rate) {
396                 synchronized (backend) {
397                         scanning_telemetry_rate = new_scanning_telemetry_rate;
398                         backend.putInt(scanningTelemetryRatePreference, scanning_telemetry_rate);
399                         flush_preferences();
400                 }
401         }
402
403         public static int scanning_telemetry_rate() {
404                 synchronized(backend) {
405                         return scanning_telemetry_rate;
406                 }
407         }
408
409         public static void set_voice(boolean new_voice) {
410                 synchronized (backend) {
411                         voice = new_voice;
412                         backend.putBoolean(voicePreference, voice);
413                         flush_preferences();
414                 }
415         }
416
417         public static boolean voice() {
418                 synchronized (backend) {
419                         return voice;
420                 }
421         }
422
423         public static void set_callsign(String new_callsign) {
424                 synchronized(backend) {
425                         callsign = new_callsign;
426                         backend.putString(callsignPreference, callsign);
427                         flush_preferences();
428                 }
429         }
430
431         public static String callsign() {
432                 synchronized(backend) {
433                         return callsign;
434                 }
435         }
436
437         public static void set_firmwaredir(File new_firmwaredir) {
438                 synchronized (backend) {
439                         firmwaredir = new_firmwaredir;
440                         backend.putString(firmwaredirPreference, firmwaredir.getPath());
441                         flush_preferences();
442                 }
443         }
444
445         public static File firmwaredir() {
446                 synchronized (backend) {
447                         return firmwaredir;
448                 }
449         }
450
451         public static void set_launcher_serial(int new_launcher_serial) {
452                 synchronized (backend) {
453                         launcher_serial = new_launcher_serial;
454                         backend.putInt(launcherSerialPreference, launcher_serial);
455                         flush_preferences();
456                 }
457         }
458
459         public static int launcher_serial() {
460                 synchronized (backend) {
461                         return launcher_serial;
462                 }
463         }
464
465         public static void set_launcher_channel(int new_launcher_channel) {
466                 synchronized (backend) {
467                         launcher_channel = new_launcher_channel;
468                         backend.putInt(launcherChannelPreference, launcher_channel);
469                         flush_preferences();
470                 }
471         }
472
473         public static int launcher_channel() {
474                 synchronized (backend) {
475                         return launcher_channel;
476                 }
477         }
478
479         public static AltosPreferencesBackend bt_devices() {
480                 synchronized (backend) {
481                         return backend.node("bt_devices");
482                 }
483         }
484
485         public static AltosFrequency[] common_frequencies() {
486                 synchronized (backend) {
487                         return common_frequencies;
488                 }
489         }
490
491         public static void set_common_frequencies(AltosFrequency[] frequencies) {
492                 synchronized(backend) {
493                         common_frequencies = frequencies;
494                         save_common_frequencies(frequencies);
495                         flush_preferences();
496                 }
497         }
498
499         public static void add_common_frequency(AltosFrequency frequency) {
500                 AltosFrequency[]        old_frequencies = common_frequencies();
501                 AltosFrequency[]        new_frequencies = new AltosFrequency[old_frequencies.length + 1];
502                 int                     i;
503
504                 for (i = 0; i < old_frequencies.length; i++) {
505                         if (frequency.frequency == old_frequencies[i].frequency)
506                                 return;
507                         if (frequency.frequency < old_frequencies[i].frequency)
508                                 break;
509                         new_frequencies[i] = old_frequencies[i];
510                 }
511                 new_frequencies[i] = frequency;
512                 for (; i < old_frequencies.length; i++)
513                         new_frequencies[i+1] = old_frequencies[i];
514                 set_common_frequencies(new_frequencies);
515         }
516
517         static LinkedList<AltosUnitsListener> units_listeners;
518
519         public static boolean imperial_units() {
520                 synchronized(backend) {
521                         return AltosConvert.imperial_units;
522                 }
523         }
524
525         public static void set_imperial_units(boolean imperial_units) {
526                 synchronized (backend) {
527                         AltosConvert.imperial_units = imperial_units;
528                         backend.putBoolean(unitsPreference, imperial_units);
529                         flush_preferences();
530                 }
531                 if (units_listeners != null) {
532                         for (AltosUnitsListener l : units_listeners) {
533                                 l.units_changed(imperial_units);
534                         }
535                 }
536         }
537
538         public static void register_units_listener(AltosUnitsListener l) {
539                 synchronized(backend) {
540                         if (units_listeners == null)
541                                 units_listeners = new LinkedList<AltosUnitsListener>();
542                         units_listeners.add(l);
543                 }
544         }
545
546         public static void unregister_units_listener(AltosUnitsListener l) {
547                 synchronized(backend) {
548                         units_listeners.remove(l);
549                 }
550         }
551 }