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