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