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