altosdroid: start restoring from log data on startup
[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_5;
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         /* voice preference name */
45         public final static String voicePreference = "VOICE";
46
47         /* callsign preference name */
48         public final static String callsignPreference = "CALLSIGN";
49
50         /* firmware directory preference name */
51         public final static String firmwaredirPreference = "FIRMWARE";
52
53         /* serial debug preference name */
54         public final static String serialDebugPreference = "SERIAL-DEBUG";
55
56         /* scanning telemetry preferences name */
57         public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
58
59         /* scanning telemetry rate preferences name */
60         public final static String scanningTelemetryRatePreference = "SCANNING-RATE";
61
62         /* Launcher serial preference name */
63         public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
64
65         /* Launcher channel preference name */
66         public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
67
68         /* Default logdir is ~/TeleMetrum */
69         public final static String logdirName = "TeleMetrum";
70
71         /* Log directory */
72         public static File logdir;
73
74         /* Last log directory - use this next time we open or save something */
75         public static File last_logdir;
76
77         /* Map directory -- hangs of logdir */
78         public static File mapdir;
79
80         /* Frequency (map serial to frequency) */
81         public static Hashtable<Integer, Double> frequencies;
82
83         /* Telemetry (map serial to telemetry format) */
84         public static Hashtable<Integer, Integer> telemetries;
85
86         /* Telemetry rate (map serial to telemetry format) */
87         public static Hashtable<Integer, Integer> telemetry_rates;
88
89         /* Log file (map serial to logfile name) */
90         public static Hashtable<Integer, File> logfiles;
91
92         /* Voice preference */
93         public static boolean voice;
94
95         /* Callsign preference */
96         public static String callsign;
97
98         /* Firmware directory */
99         public static File firmwaredir;
100
101         /* Scanning telemetry */
102         public static int scanning_telemetry;
103
104         public static int scanning_telemetry_rate;
105
106         /* List of frequencies */
107         public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
108         public static AltosFrequency[] common_frequencies;
109
110         public final static String      frequency_count = "COUNT";
111         public final static String      frequency_format = "FREQUENCY-%d";
112         public final static String      description_format = "DESCRIPTION-%d";
113
114         /* Units preference */
115
116         public final static String      unitsPreference = "IMPERIAL-UNITS";
117
118         public static AltosFrequency[] load_common_frequencies() {
119                 AltosFrequency[] frequencies = null;
120                 boolean existing = false;
121                 existing = backend.nodeExists(common_frequencies_node_name);
122
123                 if (existing) {
124                         AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
125                         int             count = node.getInt(frequency_count, 0);
126
127                         frequencies = new AltosFrequency[count];
128                         for (int i = 0; i < count; i++) {
129                                 double  frequency;
130                                 String  description;
131
132                                 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
133                                 description = node.getString(String.format(description_format, i), null);
134                                 frequencies[i] = new AltosFrequency(frequency, description);
135                         }
136                 } else {
137                         frequencies = new AltosFrequency[10];
138                         for (int i = 0; i < 10; i++) {
139                                 frequencies[i] = new AltosFrequency(434.550 + i * .1,
140                                                                            String.format("Channel %d", i));
141                         }
142                 }
143                 return frequencies;
144         }
145
146         public static void save_common_frequencies(AltosFrequency[] frequencies) {
147                 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
148
149                 node.putInt(frequency_count, frequencies.length);
150                 for (int i = 0; i < frequencies.length; i++) {
151                         node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
152                         node.putString(String.format(description_format, i), frequencies[i].description);
153                 }
154         }
155         public static int launcher_serial;
156
157         public static int launcher_channel;
158
159         public static void init(AltosPreferencesBackend in_backend) {
160                 backend = in_backend;
161
162                 /* Initialize logdir from preferences */
163                 String logdir_string = backend.getString(logdirPreference, null);
164                 if (logdir_string != null)
165                         logdir = new File(logdir_string);
166                 else {
167                         logdir = new File(backend.homeDirectory(), logdirName);
168                         if (!logdir.exists())
169                                 logdir.mkdirs();
170                 }
171                 mapdir = new File(logdir, "maps");
172                 if (!mapdir.exists())
173                         mapdir.mkdirs();
174
175                 frequencies = new Hashtable<Integer, Double>();
176
177                 telemetries = new Hashtable<Integer,Integer>();
178
179                 telemetry_rates = new Hashtable<Integer,Integer>();
180
181                 voice = backend.getBoolean(voicePreference, true);
182
183                 callsign = backend.getString(callsignPreference,"N0CALL");
184
185                 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
186
187                 scanning_telemetry_rate = backend.getInt(scanningTelemetryRatePreference,(1 << AltosLib.ao_telemetry_rate_38400));
188
189                 launcher_serial = backend.getInt(launcherSerialPreference, 0);
190
191                 launcher_channel = backend.getInt(launcherChannelPreference, 0);
192
193                 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
194                 if (firmwaredir_string != null)
195                         firmwaredir = new File(firmwaredir_string);
196                 else
197                         firmwaredir = null;
198
199                 common_frequencies = load_common_frequencies();
200
201                 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
202         }
203
204         public static void flush_preferences() {
205                 backend.flush();
206         }
207
208         public static void set_logdir(File new_logdir) {
209                 synchronized (backend) {
210                         logdir = new_logdir;
211                         mapdir = new File(logdir, "maps");
212                         if (!mapdir.exists())
213                                 mapdir.mkdirs();
214                         backend.putString(logdirPreference, logdir.getPath());
215                         flush_preferences();
216                 }
217         }
218
219         public static File logdir() {
220                 synchronized (backend) {
221                         return logdir;
222                 }
223         }
224
225         public static File last_logdir() {
226                 synchronized (backend) {
227                         if (last_logdir == null)
228                                 last_logdir = logdir;
229                         return last_logdir;
230                 }
231         }
232
233         public static void set_last_logdir(File file) {
234                 synchronized(backend) {
235                         if (file != null && !file.isDirectory())
236                                 file = file.getParentFile();
237                         if (file == null)
238                                 file = new File(".");
239                         last_logdir = file;
240                 }
241         }
242
243         public static File mapdir() {
244                 synchronized (backend) {
245                         return mapdir;
246                 }
247         }
248
249         public static void set_frequency(int serial, double new_frequency) {
250                 synchronized (backend) {
251                         frequencies.put(serial, new_frequency);
252                         backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
253                         flush_preferences();
254                 }
255         }
256
257         public static double frequency(int serial) {
258                 synchronized (backend) {
259                         if (frequencies.containsKey(serial))
260                                 return frequencies.get(serial);
261                         double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
262                         if (frequency == 0.0) {
263                                 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
264                                 frequency = AltosConvert.radio_channel_to_frequency(channel);
265                         }
266                         frequencies.put(serial, frequency);
267                         return frequency;
268                 }
269         }
270
271         public static void set_telemetry(int serial, int new_telemetry) {
272                 synchronized (backend) {
273                         telemetries.put(serial, new_telemetry);
274                         backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
275                         flush_preferences();
276                 }
277         }
278
279         public static int telemetry(int serial) {
280                 synchronized (backend) {
281                         if (telemetries.containsKey(serial))
282                                 return telemetries.get(serial);
283                         int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
284                                                    AltosLib.ao_telemetry_standard);
285                         telemetries.put(serial, telemetry);
286                         return telemetry;
287                 }
288         }
289
290         public static void set_telemetry_rate(int serial, int new_telemetry_rate) {
291                 synchronized (backend) {
292                         telemetry_rates.put(serial, new_telemetry_rate);
293                         backend.putInt(String.format(telemetryRatePreferenceFormat, serial), new_telemetry_rate);
294                         flush_preferences();
295                 }
296         }
297
298         public static int telemetry_rate(int serial) {
299                 synchronized (backend) {
300                         if (telemetry_rates.containsKey(serial))
301                                 return telemetry_rates.get(serial);
302                         int telemetry_rate = backend.getInt(String.format(telemetryRatePreferenceFormat, serial),
303                                                             AltosLib.ao_telemetry_rate_38400);
304                         telemetry_rates.put(serial, telemetry_rate);
305                         return telemetry_rate;
306                 }
307         }
308
309         public static void set_logfile(int serial, File new_logfile) {
310                 synchronized(backend) {
311                         logfiles.put(serial, new_logfile);
312                         backend.putString(String.format(logfilePreferenceFormat, serial), new_logfile.getPath());
313                         flush_preferences();
314                 }
315         }
316
317         public static File logfile(int serial) {
318                 synchronized(backend) {
319                         if (logfiles.containsKey(serial))
320                                 return logfiles.get(serial);
321                         String logfile_string = backend.getString(String.format(logfilePreferenceFormat, serial), null);
322                         if (logfile_string == null)
323                                 return null;
324                         File logfile = new File(logfile_string);
325                         logfiles.put(serial, logfile);
326                         return logfile;
327                 }
328         }
329
330         public static void set_scanning_telemetry(int new_scanning_telemetry) {
331                 synchronized (backend) {
332                         scanning_telemetry = new_scanning_telemetry;
333                         backend.putInt(scanningTelemetryPreference, scanning_telemetry);
334                         flush_preferences();
335                 }
336         }
337
338         public static int scanning_telemetry() {
339                 synchronized (backend) {
340                         return scanning_telemetry;
341                 }
342         }
343
344         public static void set_scanning_telemetry_rate(int new_scanning_telemetry_rate) {
345                 synchronized (backend) {
346                         scanning_telemetry_rate = new_scanning_telemetry_rate;
347                         backend.putInt(scanningTelemetryRatePreference, scanning_telemetry_rate);
348                         flush_preferences();
349                 }
350         }
351
352         public static int scanning_telemetry_rate() {
353                 synchronized(backend) {
354                         return scanning_telemetry_rate;
355                 }
356         }
357
358         public static void set_voice(boolean new_voice) {
359                 synchronized (backend) {
360                         voice = new_voice;
361                         backend.putBoolean(voicePreference, voice);
362                         flush_preferences();
363                 }
364         }
365
366         public static boolean voice() {
367                 synchronized (backend) {
368                         return voice;
369                 }
370         }
371
372         public static void set_callsign(String new_callsign) {
373                 synchronized(backend) {
374                         callsign = new_callsign;
375                         backend.putString(callsignPreference, callsign);
376                         flush_preferences();
377                 }
378         }
379
380         public static String callsign() {
381                 synchronized(backend) {
382                         return callsign;
383                 }
384         }
385
386         public static void set_firmwaredir(File new_firmwaredir) {
387                 synchronized (backend) {
388                         firmwaredir = new_firmwaredir;
389                         backend.putString(firmwaredirPreference, firmwaredir.getPath());
390                         flush_preferences();
391                 }
392         }
393
394         public static File firmwaredir() {
395                 synchronized (backend) {
396                         return firmwaredir;
397                 }
398         }
399
400         public static void set_launcher_serial(int new_launcher_serial) {
401                 synchronized (backend) {
402                         launcher_serial = new_launcher_serial;
403                         backend.putInt(launcherSerialPreference, launcher_serial);
404                         flush_preferences();
405                 }
406         }
407
408         public static int launcher_serial() {
409                 synchronized (backend) {
410                         return launcher_serial;
411                 }
412         }
413
414         public static void set_launcher_channel(int new_launcher_channel) {
415                 synchronized (backend) {
416                         launcher_channel = new_launcher_channel;
417                         backend.putInt(launcherChannelPreference, launcher_channel);
418                         flush_preferences();
419                 }
420         }
421
422         public static int launcher_channel() {
423                 synchronized (backend) {
424                         return launcher_channel;
425                 }
426         }
427
428         public static AltosPreferencesBackend bt_devices() {
429                 synchronized (backend) {
430                         return backend.node("bt_devices");
431                 }
432         }
433
434         public static AltosFrequency[] common_frequencies() {
435                 synchronized (backend) {
436                         return common_frequencies;
437                 }
438         }
439
440         public static void set_common_frequencies(AltosFrequency[] frequencies) {
441                 synchronized(backend) {
442                         common_frequencies = frequencies;
443                         save_common_frequencies(frequencies);
444                         flush_preferences();
445                 }
446         }
447
448         public static void add_common_frequency(AltosFrequency frequency) {
449                 AltosFrequency[]        old_frequencies = common_frequencies();
450                 AltosFrequency[]        new_frequencies = new AltosFrequency[old_frequencies.length + 1];
451                 int                     i;
452
453                 for (i = 0; i < old_frequencies.length; i++) {
454                         if (frequency.frequency == old_frequencies[i].frequency)
455                                 return;
456                         if (frequency.frequency < old_frequencies[i].frequency)
457                                 break;
458                         new_frequencies[i] = old_frequencies[i];
459                 }
460                 new_frequencies[i] = frequency;
461                 for (; i < old_frequencies.length; i++)
462                         new_frequencies[i+1] = old_frequencies[i];
463                 set_common_frequencies(new_frequencies);
464         }
465
466         static LinkedList<AltosUnitsListener> units_listeners;
467
468         public static boolean imperial_units() {
469                 synchronized(backend) {
470                         return AltosConvert.imperial_units;
471                 }
472         }
473
474         public static void set_imperial_units(boolean imperial_units) {
475                 synchronized (backend) {
476                         AltosConvert.imperial_units = imperial_units;
477                         backend.putBoolean(unitsPreference, imperial_units);
478                         flush_preferences();
479                 }
480                 if (units_listeners != null) {
481                         for (AltosUnitsListener l : units_listeners) {
482                                 l.units_changed(imperial_units);
483                         }
484                 }
485         }
486
487         public static void register_units_listener(AltosUnitsListener l) {
488                 synchronized(backend) {
489                         if (units_listeners == null)
490                                 units_listeners = new LinkedList<AltosUnitsListener>();
491                         units_listeners.add(l);
492                 }
493         }
494
495         public static void unregister_units_listener(AltosUnitsListener l) {
496                 synchronized(backend) {
497                         units_listeners.remove(l);
498                 }
499         }
500 }