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