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