altoslib: Store saved state in version-independent format
[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_11;
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         public final static String      frequenciesPreference = "FREQUENCIES-1";
120
121         /* Units preference */
122
123         public final static String      unitsPreference = "IMPERIAL-UNITS";
124
125         /* Maps cache size preference name */
126         final static String mapCachePreference = "MAP-CACHE";
127
128         static LinkedList<AltosMapCacheListener> map_cache_listeners;
129
130         public static int map_cache = 9;
131
132         final static String mapTypePreference = "MAP-TYPE";
133         static int      map_type;
134
135         public static AltosFrequency[] load_common_frequencies() {
136
137                 AltosFrequency[] frequencies = null;
138
139                 AltosHashSet[]  sets = AltosHashSet.array(backend.getString(frequenciesPreference,null));
140                 if (sets != null) {
141                         ArrayList<AltosFrequency>       freqs = new ArrayList<AltosFrequency>();
142
143                         for (int i = 0; i < sets.length; i++) {
144                                 AltosFrequency f = AltosFrequency.fromHashSet(sets[i], null);
145                                 if (f != null)
146                                         freqs.add(f);
147                         }
148                         frequencies = freqs.toArray(new AltosFrequency[0]);
149                 }
150
151                 if (frequencies == null) {
152                         if (backend.nodeExists(common_frequencies_node_name)) {
153                                 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
154                                 int             count = node.getInt(frequency_count, 0);
155
156                                 if (count > 0) {
157                                         frequencies = new AltosFrequency[count];
158                                         for (int i = 0; i < count; i++) {
159                                                 double  frequency;
160                                                 String  description;
161
162                                                 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
163                                                 description = node.getString(String.format(description_format, i), null);
164                                                 frequencies[i] = new AltosFrequency(frequency, description);
165                                         }
166                                 }
167                         }
168                 }
169
170                 if (frequencies == null) {
171                         frequencies = new AltosFrequency[10];
172                         for (int i = 0; i < 10; i++) {
173                                 frequencies[i] = new AltosFrequency(434.550 + i * .1,
174                                                                            String.format("Channel %d", i));
175                         }
176                 }
177                 return frequencies;
178         }
179
180         public static void save_common_frequencies() {
181                 AltosHashSet[]  sets = new AltosHashSet[common_frequencies.length];
182                 for (int i = 0; i < sets.length; i++)
183                         sets[i] = common_frequencies[i].hashSet();
184                 backend.putString(frequenciesPreference, AltosHashSet.toString(sets));
185                 flush_preferences();
186         }
187
188         public static int launcher_serial;
189
190         public static int launcher_channel;
191
192         public static void init(AltosPreferencesBackend in_backend) {
193
194                 if (backend != null)
195                         return;
196
197                 backend = in_backend;
198
199                 /* Initialize logdir from preferences */
200                 String logdir_string = backend.getString(logdirPreference, null);
201                 if (logdir_string != null)
202                         logdir = new File(logdir_string);
203                 else {
204                         logdir = new File(backend.homeDirectory(), logdirName);
205                         if (!logdir.exists())
206                                 logdir.mkdirs();
207                 }
208                 mapdir = new File(logdir, "maps");
209                 if (!mapdir.exists())
210                         mapdir.mkdirs();
211
212                 frequencies = new Hashtable<Integer, Double>();
213
214                 telemetries = new Hashtable<Integer,Integer>();
215
216                 telemetry_rates = new Hashtable<Integer,Integer>();
217
218                 logfiles = new Hashtable<Integer,File>();
219
220                 voice = backend.getBoolean(voicePreference, true);
221
222                 callsign = backend.getString(callsignPreference,"N0CALL");
223
224                 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
225
226                 scanning_telemetry_rate = backend.getInt(scanningTelemetryRatePreference,(1 << AltosLib.ao_telemetry_rate_38400));
227
228                 launcher_serial = backend.getInt(launcherSerialPreference, 0);
229
230                 launcher_channel = backend.getInt(launcherChannelPreference, 0);
231
232                 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
233                 if (firmwaredir_string != null)
234                         firmwaredir = new File(firmwaredir_string);
235                 else
236                         firmwaredir = null;
237
238                 common_frequencies = load_common_frequencies();
239
240                 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
241
242                 map_cache = backend.getInt(mapCachePreference, 9);
243                 map_cache_listeners = new LinkedList<AltosMapCacheListener>();
244                 map_type = backend.getInt(mapTypePreference, AltosMap.maptype_hybrid);
245         }
246
247         public static void flush_preferences() {
248                 backend.flush();
249         }
250
251         public static void set_logdir(File new_logdir) {
252                 synchronized (backend) {
253                         logdir = new_logdir;
254                         mapdir = new File(logdir, "maps");
255                         if (!mapdir.exists())
256                                 mapdir.mkdirs();
257                         backend.putString(logdirPreference, logdir.getPath());
258                         flush_preferences();
259                 }
260         }
261
262         public static File logdir() {
263                 synchronized (backend) {
264                         return logdir;
265                 }
266         }
267
268         public static File last_logdir() {
269                 synchronized (backend) {
270                         if (last_logdir == null)
271                                 last_logdir = logdir;
272                         return last_logdir;
273                 }
274         }
275
276         public static void set_last_logdir(File file) {
277                 synchronized(backend) {
278                         if (file != null && !file.isDirectory())
279                                 file = file.getParentFile();
280                         if (file == null)
281                                 file = new File(".");
282                         last_logdir = file;
283                 }
284         }
285
286         public static File mapdir() {
287                 synchronized (backend) {
288                         return mapdir;
289                 }
290         }
291
292         public static void set_frequency(int serial, double new_frequency) {
293                 synchronized (backend) {
294                         frequencies.put(serial, new_frequency);
295                         backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
296                         flush_preferences();
297                 }
298         }
299
300         public static double frequency(int serial) {
301                 synchronized (backend) {
302                         if (frequencies.containsKey(serial))
303                                 return frequencies.get(serial);
304                         double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
305                         if (frequency == 0.0) {
306                                 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
307                                 frequency = AltosConvert.radio_channel_to_frequency(channel);
308                         }
309                         frequencies.put(serial, frequency);
310                         return frequency;
311                 }
312         }
313
314         public static void set_telemetry(int serial, int new_telemetry) {
315                 synchronized (backend) {
316                         telemetries.put(serial, new_telemetry);
317                         backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
318                         flush_preferences();
319                 }
320         }
321
322         public static int telemetry(int serial) {
323                 synchronized (backend) {
324                         if (telemetries.containsKey(serial))
325                                 return telemetries.get(serial);
326                         int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
327                                                    AltosLib.ao_telemetry_standard);
328                         telemetries.put(serial, telemetry);
329                         return telemetry;
330                 }
331         }
332
333         public static void set_telemetry_rate(int serial, int new_telemetry_rate) {
334                 synchronized (backend) {
335                         telemetry_rates.put(serial, new_telemetry_rate);
336                         backend.putInt(String.format(telemetryRatePreferenceFormat, serial), new_telemetry_rate);
337                         flush_preferences();
338                 }
339         }
340
341         public static int telemetry_rate(int serial) {
342                 synchronized (backend) {
343                         if (telemetry_rates.containsKey(serial))
344                                 return telemetry_rates.get(serial);
345                         int telemetry_rate = backend.getInt(String.format(telemetryRatePreferenceFormat, serial),
346                                                             AltosLib.ao_telemetry_rate_38400);
347                         telemetry_rates.put(serial, telemetry_rate);
348                         return telemetry_rate;
349                 }
350         }
351
352         public static void set_logfile(int serial, File new_logfile) {
353                 synchronized(backend) {
354                         logfiles.put(serial, new_logfile);
355                         backend.putString(String.format(logfilePreferenceFormat, serial), new_logfile.getPath());
356                         flush_preferences();
357                 }
358         }
359
360         public static File logfile(int serial) {
361                 synchronized(backend) {
362                         if (logfiles.containsKey(serial))
363                                 return logfiles.get(serial);
364                         String logfile_string = backend.getString(String.format(logfilePreferenceFormat, serial), null);
365                         if (logfile_string == null)
366                                 return null;
367                         File logfile = new File(logfile_string);
368                         logfiles.put(serial, logfile);
369                         return logfile;
370                 }
371         }
372
373         public static void set_state(AltosState state) {
374
375                 synchronized(backend) {
376                         backend.putHashSet(String.format(statePreferenceFormat, state.serial), state.hashSet());
377                         backend.putInt(statePreferenceLatest, state.serial);
378                         flush_preferences();
379                 }
380         }
381
382         public static ArrayList<Integer> list_states() {
383                 String[]                keys = backend.keys();
384                 ArrayList<Integer>      states = new ArrayList<Integer>();
385
386                 for (String key : keys) {
387                         if (key.startsWith(statePreferenceHead)) {
388                                 try {
389                                         int serial = AltosParse.parse_int(key.substring(statePreferenceHead.length()));
390                                         states.add(serial);
391                                 } catch (ParseException pe) {
392                                 }
393                         }
394                 }
395                 return states;
396         }
397
398         public static void remove_state(int serial) {
399                 synchronized(backend) {
400                         backend.remove(String.format(statePreferenceFormat, serial));
401                         flush_preferences();
402                 }
403         }
404
405         public static int latest_state() {
406                 int     latest = 0;
407                 synchronized (backend) {
408                         latest = backend.getInt(statePreferenceLatest, 0);
409                 }
410                 return latest;
411         }
412
413         public static AltosState state(int serial) {
414                 synchronized(backend) {
415                         try {
416                                 return AltosState.fromHashSet(backend.getHashSet(String.format(statePreferenceFormat, serial)));
417                         } catch (Exception e) {
418                                 return null;
419                         }
420                 }
421         }
422
423         public static void set_scanning_telemetry(int new_scanning_telemetry) {
424                 synchronized (backend) {
425                         scanning_telemetry = new_scanning_telemetry;
426                         backend.putInt(scanningTelemetryPreference, scanning_telemetry);
427                         flush_preferences();
428                 }
429         }
430
431         public static int scanning_telemetry() {
432                 synchronized (backend) {
433                         return scanning_telemetry;
434                 }
435         }
436
437         public static void set_scanning_telemetry_rate(int new_scanning_telemetry_rate) {
438                 synchronized (backend) {
439                         scanning_telemetry_rate = new_scanning_telemetry_rate;
440                         backend.putInt(scanningTelemetryRatePreference, scanning_telemetry_rate);
441                         flush_preferences();
442                 }
443         }
444
445         public static int scanning_telemetry_rate() {
446                 synchronized(backend) {
447                         return scanning_telemetry_rate;
448                 }
449         }
450
451         public static void set_voice(boolean new_voice) {
452                 synchronized (backend) {
453                         voice = new_voice;
454                         backend.putBoolean(voicePreference, voice);
455                         flush_preferences();
456                 }
457         }
458
459         public static boolean voice() {
460                 synchronized (backend) {
461                         return voice;
462                 }
463         }
464
465         public static void set_callsign(String new_callsign) {
466                 synchronized(backend) {
467                         callsign = new_callsign;
468                         backend.putString(callsignPreference, callsign);
469                         flush_preferences();
470                 }
471         }
472
473         public static String callsign() {
474                 synchronized(backend) {
475                         return callsign;
476                 }
477         }
478
479         public static void set_firmwaredir(File new_firmwaredir) {
480                 synchronized (backend) {
481                         firmwaredir = new_firmwaredir;
482                         backend.putString(firmwaredirPreference, firmwaredir.getPath());
483                         flush_preferences();
484                 }
485         }
486
487         public static File firmwaredir() {
488                 synchronized (backend) {
489                         return firmwaredir;
490                 }
491         }
492
493         public static void set_launcher_serial(int new_launcher_serial) {
494                 synchronized (backend) {
495                         launcher_serial = new_launcher_serial;
496                         backend.putInt(launcherSerialPreference, launcher_serial);
497                         flush_preferences();
498                 }
499         }
500
501         public static int launcher_serial() {
502                 synchronized (backend) {
503                         return launcher_serial;
504                 }
505         }
506
507         public static void set_launcher_channel(int new_launcher_channel) {
508                 synchronized (backend) {
509                         launcher_channel = new_launcher_channel;
510                         backend.putInt(launcherChannelPreference, launcher_channel);
511                         flush_preferences();
512                 }
513         }
514
515         public static int launcher_channel() {
516                 synchronized (backend) {
517                         return launcher_channel;
518                 }
519         }
520
521         public static AltosPreferencesBackend bt_devices() {
522                 synchronized (backend) {
523                         return backend.node("bt_devices");
524                 }
525         }
526
527         public static AltosFrequency[] common_frequencies() {
528                 synchronized (backend) {
529                         return common_frequencies;
530                 }
531         }
532
533         public static void set_common_frequencies(AltosFrequency[] frequencies) {
534                 synchronized(backend) {
535                         common_frequencies = frequencies;
536
537                         save_common_frequencies();
538                 }
539         }
540
541         public static void add_common_frequency(AltosFrequency frequency) {
542                 AltosFrequency[]        old_frequencies = common_frequencies();
543                 AltosFrequency[]        new_frequencies = new AltosFrequency[old_frequencies.length + 1];
544                 int                     i;
545
546                 for (i = 0; i < old_frequencies.length; i++) {
547                         if (frequency.frequency == old_frequencies[i].frequency)
548                                 return;
549                         if (frequency.frequency < old_frequencies[i].frequency)
550                                 break;
551                         new_frequencies[i] = old_frequencies[i];
552                 }
553                 new_frequencies[i] = frequency;
554                 for (; i < old_frequencies.length; i++)
555                         new_frequencies[i+1] = old_frequencies[i];
556                 set_common_frequencies(new_frequencies);
557         }
558
559         static LinkedList<AltosUnitsListener> units_listeners;
560
561         public static boolean imperial_units() {
562                 synchronized(backend) {
563                         return AltosConvert.imperial_units;
564                 }
565         }
566
567         public static void set_imperial_units(boolean imperial_units) {
568                 synchronized (backend) {
569                         AltosConvert.imperial_units = imperial_units;
570                         backend.putBoolean(unitsPreference, imperial_units);
571                         flush_preferences();
572                 }
573                 if (units_listeners != null) {
574                         for (AltosUnitsListener l : units_listeners) {
575                                 l.units_changed(imperial_units);
576                         }
577                 }
578         }
579
580         public static void register_units_listener(AltosUnitsListener l) {
581                 synchronized(backend) {
582                         if (units_listeners == null)
583                                 units_listeners = new LinkedList<AltosUnitsListener>();
584                         units_listeners.add(l);
585                 }
586         }
587
588         public static void unregister_units_listener(AltosUnitsListener l) {
589                 synchronized(backend) {
590                         units_listeners.remove(l);
591                 }
592         }
593
594
595         public static void register_map_cache_listener(AltosMapCacheListener l) {
596                 synchronized(backend) {
597                         map_cache_listeners.add(l);
598                 }
599         }
600
601         public static void unregister_map_cache_listener(AltosMapCacheListener l) {
602                 synchronized (backend) {
603                         map_cache_listeners.remove(l);
604                 }
605         }
606
607         public static void set_map_cache(int new_map_cache) {
608                 synchronized(backend) {
609                         map_cache = new_map_cache;
610                         backend.putInt(mapCachePreference, map_cache);
611                         flush_preferences();
612                         for (AltosMapCacheListener l: map_cache_listeners)
613                                 l.map_cache_changed(map_cache);
614                 }
615         }
616
617         public static int map_cache() {
618                 synchronized(backend) {
619                         return map_cache;
620                 }
621         }
622
623         static LinkedList<AltosMapTypeListener> map_type_listeners;
624
625         public static void set_map_type(int map_type) {
626                 synchronized(backend) {
627                         AltosPreferences.map_type = map_type;
628                         backend.putInt(mapTypePreference, map_type);
629                         flush_preferences();
630                 }
631                 if (map_type_listeners != null) {
632                         for (AltosMapTypeListener l : map_type_listeners) {
633                                 l.map_type_changed(map_type);
634                         }
635                 }
636         }
637
638         public static int map_type() {
639                 synchronized(backend) {
640                         return map_type;
641                 }
642         }
643
644         public static void register_map_type_listener(AltosMapTypeListener l) {
645                 synchronized(backend) {
646                         if (map_type_listeners == null)
647                                 map_type_listeners = new LinkedList<AltosMapTypeListener>();
648                         map_type_listeners.add(l);
649                 }
650         }
651
652         public static void unregister_map_type_listener(AltosMapTypeListener l) {
653                 synchronized(backend) {
654                         map_type_listeners.remove(l);
655                 }
656         }
657 }