4bf48f6df025e6433973e84460847d40219dcd07
[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_10;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosPreferences {
25         public static AltosPreferencesBackend backend = null;
26
27         /* logdir preference name */
28         public final static String logdirPreference = "LOGDIR";
29
30         /* channel preference name */
31         public final static String channelPreferenceFormat = "CHANNEL-%d";
32
33         /* frequency preference name */
34         public final static String frequencyPreferenceFormat = "FREQUENCY-%d";
35
36         /* telemetry format preference name */
37         public final static String telemetryPreferenceFormat = "TELEMETRY-%d";
38
39         /* telemetry rate format preference name */
40         public final static String telemetryRatePreferenceFormat = "RATE-%d";
41
42         /* log file format preference name */
43         public final static String logfilePreferenceFormat = "LOGFILE-%d";
44
45         /* state preference name */
46         public final static String statePreferenceHead = "STATE-";
47         public final static String statePreferenceFormat = "STATE-%d";
48         public final static String statePreferenceLatest = "STATE-LATEST";
49
50         /* voice preference name */
51         public final static String voicePreference = "VOICE";
52
53         /* callsign preference name */
54         public final static String callsignPreference = "CALLSIGN";
55
56         /* firmware directory preference name */
57         public final static String firmwaredirPreference = "FIRMWARE";
58
59         /* serial debug preference name */
60         public final static String serialDebugPreference = "SERIAL-DEBUG";
61
62         /* scanning telemetry preferences name */
63         public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
64
65         /* scanning telemetry rate preferences name */
66         public final static String scanningTelemetryRatePreference = "SCANNING-RATE";
67
68         /* Launcher serial preference name */
69         public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
70
71         /* Launcher channel preference name */
72         public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
73
74         /* Default logdir is ~/TeleMetrum */
75         public final static String logdirName = "TeleMetrum";
76
77         /* Log directory */
78         public static File logdir;
79
80         /* Last log directory - use this next time we open or save something */
81         public static File last_logdir;
82
83         /* Map directory -- hangs of logdir */
84         public static File mapdir;
85
86         /* Frequency (map serial to frequency) */
87         public static Hashtable<Integer, Double> frequencies;
88
89         /* Telemetry (map serial to telemetry format) */
90         public static Hashtable<Integer, Integer> telemetries;
91
92         /* Telemetry rate (map serial to telemetry format) */
93         public static Hashtable<Integer, Integer> telemetry_rates;
94
95         /* Log file (map serial to logfile name) */
96         public static Hashtable<Integer, File> logfiles;
97
98         /* Voice preference */
99         public static boolean voice;
100
101         /* Callsign preference */
102         public static String callsign;
103
104         /* Firmware directory */
105         public static File firmwaredir;
106
107         /* Scanning telemetry */
108         public static int scanning_telemetry;
109
110         public static int scanning_telemetry_rate;
111
112         /* List of frequencies */
113         public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
114         public static AltosFrequency[] common_frequencies;
115
116         public final static String      frequency_count = "COUNT";
117         public final static String      frequency_format = "FREQUENCY-%d";
118         public final static String      description_format = "DESCRIPTION-%d";
119
120         /* Units preference */
121
122         public final static String      unitsPreference = "IMPERIAL-UNITS";
123
124         /* Maps cache size preference name */
125         final static String mapCachePreference = "MAP-CACHE";
126
127         static LinkedList<AltosMapCacheListener> map_cache_listeners;
128
129         public static int map_cache = 9;
130
131         final static String mapTypePreference = "MAP-TYPE";
132         static int      map_type;
133
134         public static AltosFrequency[] load_common_frequencies() {
135                 AltosFrequency[] frequencies = null;
136                 boolean existing = false;
137                 existing = backend.nodeExists(common_frequencies_node_name);
138
139                 if (existing) {
140                         AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
141                         int             count = node.getInt(frequency_count, 0);
142
143                         frequencies = new AltosFrequency[count];
144                         for (int i = 0; i < count; i++) {
145                                 double  frequency;
146                                 String  description;
147
148                                 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
149                                 description = node.getString(String.format(description_format, i), null);
150                                 frequencies[i] = new AltosFrequency(frequency, description);
151                         }
152                 } else {
153                         frequencies = new AltosFrequency[10];
154                         for (int i = 0; i < 10; i++) {
155                                 frequencies[i] = new AltosFrequency(434.550 + i * .1,
156                                                                            String.format("Channel %d", i));
157                         }
158                 }
159                 return frequencies;
160         }
161
162         public static void save_common_frequencies(AltosFrequency[] frequencies) {
163                 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
164
165                 node.putInt(frequency_count, frequencies.length);
166                 for (int i = 0; i < frequencies.length; i++) {
167                         node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
168                         node.putString(String.format(description_format, i), frequencies[i].description);
169                 }
170         }
171         public static int launcher_serial;
172
173         public static int launcher_channel;
174
175         public static void init(AltosPreferencesBackend in_backend) {
176
177                 if (backend != null)
178                         return;
179
180                 backend = in_backend;
181
182                 /* Initialize logdir from preferences */
183                 String logdir_string = backend.getString(logdirPreference, null);
184                 if (logdir_string != null)
185                         logdir = new File(logdir_string);
186                 else {
187                         logdir = new File(backend.homeDirectory(), logdirName);
188                         if (!logdir.exists())
189                                 logdir.mkdirs();
190                 }
191                 mapdir = new File(logdir, "maps");
192                 if (!mapdir.exists())
193                         mapdir.mkdirs();
194
195                 frequencies = new Hashtable<Integer, Double>();
196
197                 telemetries = new Hashtable<Integer,Integer>();
198
199                 telemetry_rates = new Hashtable<Integer,Integer>();
200
201                 logfiles = new Hashtable<Integer,File>();
202
203                 voice = backend.getBoolean(voicePreference, true);
204
205                 callsign = backend.getString(callsignPreference,"N0CALL");
206
207                 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
208
209                 scanning_telemetry_rate = backend.getInt(scanningTelemetryRatePreference,(1 << AltosLib.ao_telemetry_rate_38400));
210
211                 launcher_serial = backend.getInt(launcherSerialPreference, 0);
212
213                 launcher_channel = backend.getInt(launcherChannelPreference, 0);
214
215                 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
216                 if (firmwaredir_string != null)
217                         firmwaredir = new File(firmwaredir_string);
218                 else
219                         firmwaredir = null;
220
221                 common_frequencies = load_common_frequencies();
222
223                 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
224
225                 map_cache = backend.getInt(mapCachePreference, 9);
226                 map_cache_listeners = new LinkedList<AltosMapCacheListener>();
227                 map_type = backend.getInt(mapTypePreference, AltosMap.maptype_hybrid);
228         }
229
230         public static void flush_preferences() {
231                 backend.flush();
232         }
233
234         public static void set_logdir(File new_logdir) {
235                 synchronized (backend) {
236                         logdir = new_logdir;
237                         mapdir = new File(logdir, "maps");
238                         if (!mapdir.exists())
239                                 mapdir.mkdirs();
240                         backend.putString(logdirPreference, logdir.getPath());
241                         flush_preferences();
242                 }
243         }
244
245         public static File logdir() {
246                 synchronized (backend) {
247                         return logdir;
248                 }
249         }
250
251         public static File last_logdir() {
252                 synchronized (backend) {
253                         if (last_logdir == null)
254                                 last_logdir = logdir;
255                         return last_logdir;
256                 }
257         }
258
259         public static void set_last_logdir(File file) {
260                 synchronized(backend) {
261                         if (file != null && !file.isDirectory())
262                                 file = file.getParentFile();
263                         if (file == null)
264                                 file = new File(".");
265                         last_logdir = file;
266                 }
267         }
268
269         public static File mapdir() {
270                 synchronized (backend) {
271                         return mapdir;
272                 }
273         }
274
275         public static void set_frequency(int serial, double new_frequency) {
276                 synchronized (backend) {
277                         frequencies.put(serial, new_frequency);
278                         backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
279                         flush_preferences();
280                 }
281         }
282
283         public static double frequency(int serial) {
284                 synchronized (backend) {
285                         if (frequencies.containsKey(serial))
286                                 return frequencies.get(serial);
287                         double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
288                         if (frequency == 0.0) {
289                                 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
290                                 frequency = AltosConvert.radio_channel_to_frequency(channel);
291                         }
292                         frequencies.put(serial, frequency);
293                         return frequency;
294                 }
295         }
296
297         public static void set_telemetry(int serial, int new_telemetry) {
298                 synchronized (backend) {
299                         telemetries.put(serial, new_telemetry);
300                         backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
301                         flush_preferences();
302                 }
303         }
304
305         public static int telemetry(int serial) {
306                 synchronized (backend) {
307                         if (telemetries.containsKey(serial))
308                                 return telemetries.get(serial);
309                         int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
310                                                    AltosLib.ao_telemetry_standard);
311                         telemetries.put(serial, telemetry);
312                         return telemetry;
313                 }
314         }
315
316         public static void set_telemetry_rate(int serial, int new_telemetry_rate) {
317                 synchronized (backend) {
318                         telemetry_rates.put(serial, new_telemetry_rate);
319                         backend.putInt(String.format(telemetryRatePreferenceFormat, serial), new_telemetry_rate);
320                         flush_preferences();
321                 }
322         }
323
324         public static int telemetry_rate(int serial) {
325                 synchronized (backend) {
326                         if (telemetry_rates.containsKey(serial))
327                                 return telemetry_rates.get(serial);
328                         int telemetry_rate = backend.getInt(String.format(telemetryRatePreferenceFormat, serial),
329                                                             AltosLib.ao_telemetry_rate_38400);
330                         telemetry_rates.put(serial, telemetry_rate);
331                         return telemetry_rate;
332                 }
333         }
334
335         public static void set_logfile(int serial, File new_logfile) {
336                 synchronized(backend) {
337                         logfiles.put(serial, new_logfile);
338                         backend.putString(String.format(logfilePreferenceFormat, serial), new_logfile.getPath());
339                         flush_preferences();
340                 }
341         }
342
343         public static File logfile(int serial) {
344                 synchronized(backend) {
345                         if (logfiles.containsKey(serial))
346                                 return logfiles.get(serial);
347                         String logfile_string = backend.getString(String.format(logfilePreferenceFormat, serial), null);
348                         if (logfile_string == null)
349                                 return null;
350                         File logfile = new File(logfile_string);
351                         logfiles.put(serial, logfile);
352                         return logfile;
353                 }
354         }
355
356         public static void set_state(int serial, AltosState state, AltosListenerState listener_state) {
357
358                 backend.debug("set_state for %d pos %g,%g\n",
359                               serial,
360                               state.gps != null ? state.gps.lat : 0.0,
361                               state.gps != null ? state.gps.lon : 0.0);
362
363                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
364
365                 try {
366                         ObjectOutputStream oos = new ObjectOutputStream(baos);
367
368                         AltosSavedState saved_state = new AltosSavedState(state, listener_state);
369                         oos.writeObject(saved_state);
370
371                         byte[] bytes = baos.toByteArray();
372
373                         synchronized(backend) {
374                                 backend.putBytes(String.format(statePreferenceFormat, serial), bytes);
375                                 backend.putInt(statePreferenceLatest, serial);
376                                 flush_preferences();
377                         }
378                 } catch (IOException ie) {
379                         backend.debug("set_state failed %s\n", ie.toString());
380                 }
381         }
382
383         public static ArrayList<Integer> list_states() {
384                 String[]                keys = backend.keys();
385                 ArrayList<Integer>      states = new ArrayList<Integer>();
386
387                 for (String key : keys) {
388                         if (key.startsWith(statePreferenceHead)) {
389                                 backend.debug("list_states %s\n", key);
390                                 try {
391                                         int serial = AltosParse.parse_int(key.substring(statePreferenceHead.length()));
392                                         states.add(serial);
393                                 } catch (ParseException pe) {
394                                 }
395                         }
396                 }
397                 return states;
398         }
399
400         public static void remove_state(int serial) {
401                 synchronized(backend) {
402                         backend.remove(String.format(statePreferenceFormat, serial));
403                 }
404         }
405
406         public static int latest_state() {
407                 int     latest = 0;
408                 synchronized (backend) {
409                         latest = backend.getInt(statePreferenceLatest, 0);
410                 }
411                 return latest;
412         }
413
414         public static AltosSavedState state(int serial) {
415                 byte[] bytes = null;
416
417                 backend.debug("get state %d\n", serial);
418
419                 synchronized(backend) {
420                         bytes = backend.getBytes(String.format(statePreferenceFormat, serial), null);
421                 }
422
423                 if (bytes == null) {
424                         backend.debug("no state for %d\n", serial);
425                         return null;
426                 }
427
428                 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
429
430                 try {
431                         ObjectInputStream ois = new ObjectInputStream(bais);
432                         AltosSavedState saved_state = (AltosSavedState) ois.readObject();
433                         backend.debug("got saved state for %d: %g,%g\n",
434                                       serial,
435                                       saved_state.state.gps != null ? saved_state.state.gps.lat : 0.0,
436                                       saved_state.state.gps != null ? saved_state.state.gps.lon : 0.0);
437                         return saved_state;
438                 } catch (IOException ie) {
439                         backend.debug("IO exception %s\n", ie.toString());
440                 } catch (ClassNotFoundException ce) {
441                         backend.debug("ClassNotFoundException %s\n", ce.toString());
442                 }
443                 return null;
444         }
445
446         public static void set_scanning_telemetry(int new_scanning_telemetry) {
447                 synchronized (backend) {
448                         scanning_telemetry = new_scanning_telemetry;
449                         backend.putInt(scanningTelemetryPreference, scanning_telemetry);
450                         flush_preferences();
451                 }
452         }
453
454         public static int scanning_telemetry() {
455                 synchronized (backend) {
456                         return scanning_telemetry;
457                 }
458         }
459
460         public static void set_scanning_telemetry_rate(int new_scanning_telemetry_rate) {
461                 synchronized (backend) {
462                         scanning_telemetry_rate = new_scanning_telemetry_rate;
463                         backend.putInt(scanningTelemetryRatePreference, scanning_telemetry_rate);
464                         flush_preferences();
465                 }
466         }
467
468         public static int scanning_telemetry_rate() {
469                 synchronized(backend) {
470                         return scanning_telemetry_rate;
471                 }
472         }
473
474         public static void set_voice(boolean new_voice) {
475                 synchronized (backend) {
476                         voice = new_voice;
477                         backend.putBoolean(voicePreference, voice);
478                         flush_preferences();
479                 }
480         }
481
482         public static boolean voice() {
483                 synchronized (backend) {
484                         return voice;
485                 }
486         }
487
488         public static void set_callsign(String new_callsign) {
489                 synchronized(backend) {
490                         callsign = new_callsign;
491                         backend.putString(callsignPreference, callsign);
492                         flush_preferences();
493                 }
494         }
495
496         public static String callsign() {
497                 synchronized(backend) {
498                         return callsign;
499                 }
500         }
501
502         public static void set_firmwaredir(File new_firmwaredir) {
503                 synchronized (backend) {
504                         firmwaredir = new_firmwaredir;
505                         backend.putString(firmwaredirPreference, firmwaredir.getPath());
506                         flush_preferences();
507                 }
508         }
509
510         public static File firmwaredir() {
511                 synchronized (backend) {
512                         return firmwaredir;
513                 }
514         }
515
516         public static void set_launcher_serial(int new_launcher_serial) {
517                 synchronized (backend) {
518                         launcher_serial = new_launcher_serial;
519                         backend.putInt(launcherSerialPreference, launcher_serial);
520                         flush_preferences();
521                 }
522         }
523
524         public static int launcher_serial() {
525                 synchronized (backend) {
526                         return launcher_serial;
527                 }
528         }
529
530         public static void set_launcher_channel(int new_launcher_channel) {
531                 synchronized (backend) {
532                         launcher_channel = new_launcher_channel;
533                         backend.putInt(launcherChannelPreference, launcher_channel);
534                         flush_preferences();
535                 }
536         }
537
538         public static int launcher_channel() {
539                 synchronized (backend) {
540                         return launcher_channel;
541                 }
542         }
543
544         public static AltosPreferencesBackend bt_devices() {
545                 synchronized (backend) {
546                         return backend.node("bt_devices");
547                 }
548         }
549
550         public static AltosFrequency[] common_frequencies() {
551                 synchronized (backend) {
552                         return common_frequencies;
553                 }
554         }
555
556         public static void set_common_frequencies(AltosFrequency[] frequencies) {
557                 synchronized(backend) {
558                         common_frequencies = frequencies;
559                         save_common_frequencies(frequencies);
560                         flush_preferences();
561                 }
562         }
563
564         public static void add_common_frequency(AltosFrequency frequency) {
565                 AltosFrequency[]        old_frequencies = common_frequencies();
566                 AltosFrequency[]        new_frequencies = new AltosFrequency[old_frequencies.length + 1];
567                 int                     i;
568
569                 for (i = 0; i < old_frequencies.length; i++) {
570                         if (frequency.frequency == old_frequencies[i].frequency)
571                                 return;
572                         if (frequency.frequency < old_frequencies[i].frequency)
573                                 break;
574                         new_frequencies[i] = old_frequencies[i];
575                 }
576                 new_frequencies[i] = frequency;
577                 for (; i < old_frequencies.length; i++)
578                         new_frequencies[i+1] = old_frequencies[i];
579                 set_common_frequencies(new_frequencies);
580         }
581
582         static LinkedList<AltosUnitsListener> units_listeners;
583
584         public static boolean imperial_units() {
585                 synchronized(backend) {
586                         return AltosConvert.imperial_units;
587                 }
588         }
589
590         public static void set_imperial_units(boolean imperial_units) {
591                 synchronized (backend) {
592                         AltosConvert.imperial_units = imperial_units;
593                         backend.putBoolean(unitsPreference, imperial_units);
594                         flush_preferences();
595                 }
596                 if (units_listeners != null) {
597                         for (AltosUnitsListener l : units_listeners) {
598                                 l.units_changed(imperial_units);
599                         }
600                 }
601         }
602
603         public static void register_units_listener(AltosUnitsListener l) {
604                 synchronized(backend) {
605                         if (units_listeners == null)
606                                 units_listeners = new LinkedList<AltosUnitsListener>();
607                         units_listeners.add(l);
608                 }
609         }
610
611         public static void unregister_units_listener(AltosUnitsListener l) {
612                 synchronized(backend) {
613                         units_listeners.remove(l);
614                 }
615         }
616
617
618         public static void register_map_cache_listener(AltosMapCacheListener l) {
619                 synchronized(backend) {
620                         map_cache_listeners.add(l);
621                 }
622         }
623
624         public static void unregister_map_cache_listener(AltosMapCacheListener l) {
625                 synchronized (backend) {
626                         map_cache_listeners.remove(l);
627                 }
628         }
629
630         public static void set_map_cache(int new_map_cache) {
631                 synchronized(backend) {
632                         map_cache = new_map_cache;
633                         backend.putInt(mapCachePreference, map_cache);
634                         flush_preferences();
635                         for (AltosMapCacheListener l: map_cache_listeners)
636                                 l.map_cache_changed(map_cache);
637                 }
638         }
639
640         public static int map_cache() {
641                 synchronized(backend) {
642                         return map_cache;
643                 }
644         }
645
646         static LinkedList<AltosMapTypeListener> map_type_listeners;
647
648         public static void set_map_type(int map_type) {
649                 synchronized(backend) {
650                         AltosPreferences.map_type = map_type;
651                         backend.putInt(mapTypePreference, map_type);
652                         flush_preferences();
653                 }
654                 if (map_type_listeners != null) {
655                         for (AltosMapTypeListener l : map_type_listeners) {
656                                 l.map_type_changed(map_type);
657                         }
658                 }
659         }
660
661         public static int map_type() {
662                 synchronized(backend) {
663                         return map_type;
664                 }
665         }
666
667         public static void register_map_type_listener(AltosMapTypeListener l) {
668                 synchronized(backend) {
669                         if (map_type_listeners == null)
670                                 map_type_listeners = new LinkedList<AltosMapTypeListener>();
671                         map_type_listeners.add(l);
672                 }
673         }
674
675         public static void unregister_map_type_listener(AltosMapTypeListener l) {
676                 synchronized(backend) {
677                         map_type_listeners.remove(l);
678                 }
679         }
680 }