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