altosdroid: Use single object to pass data to UI
[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                 logfiles = new Hashtable<Integer,File>();
182
183                 voice = backend.getBoolean(voicePreference, true);
184
185                 callsign = backend.getString(callsignPreference,"N0CALL");
186
187                 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
188
189                 scanning_telemetry_rate = backend.getInt(scanningTelemetryRatePreference,(1 << AltosLib.ao_telemetry_rate_38400));
190
191                 launcher_serial = backend.getInt(launcherSerialPreference, 0);
192
193                 launcher_channel = backend.getInt(launcherChannelPreference, 0);
194
195                 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
196                 if (firmwaredir_string != null)
197                         firmwaredir = new File(firmwaredir_string);
198                 else
199                         firmwaredir = null;
200
201                 common_frequencies = load_common_frequencies();
202
203                 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
204         }
205
206         public static void flush_preferences() {
207                 backend.flush();
208         }
209
210         public static void set_logdir(File new_logdir) {
211                 synchronized (backend) {
212                         logdir = new_logdir;
213                         mapdir = new File(logdir, "maps");
214                         if (!mapdir.exists())
215                                 mapdir.mkdirs();
216                         backend.putString(logdirPreference, logdir.getPath());
217                         flush_preferences();
218                 }
219         }
220
221         public static File logdir() {
222                 synchronized (backend) {
223                         return logdir;
224                 }
225         }
226
227         public static File last_logdir() {
228                 synchronized (backend) {
229                         if (last_logdir == null)
230                                 last_logdir = logdir;
231                         return last_logdir;
232                 }
233         }
234
235         public static void set_last_logdir(File file) {
236                 synchronized(backend) {
237                         if (file != null && !file.isDirectory())
238                                 file = file.getParentFile();
239                         if (file == null)
240                                 file = new File(".");
241                         last_logdir = file;
242                 }
243         }
244
245         public static File mapdir() {
246                 synchronized (backend) {
247                         return mapdir;
248                 }
249         }
250
251         public static void set_frequency(int serial, double new_frequency) {
252                 synchronized (backend) {
253                         frequencies.put(serial, new_frequency);
254                         backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
255                         flush_preferences();
256                 }
257         }
258
259         public static double frequency(int serial) {
260                 synchronized (backend) {
261                         if (frequencies.containsKey(serial))
262                                 return frequencies.get(serial);
263                         double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
264                         if (frequency == 0.0) {
265                                 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
266                                 frequency = AltosConvert.radio_channel_to_frequency(channel);
267                         }
268                         frequencies.put(serial, frequency);
269                         return frequency;
270                 }
271         }
272
273         public static void set_telemetry(int serial, int new_telemetry) {
274                 synchronized (backend) {
275                         telemetries.put(serial, new_telemetry);
276                         backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
277                         flush_preferences();
278                 }
279         }
280
281         public static int telemetry(int serial) {
282                 synchronized (backend) {
283                         if (telemetries.containsKey(serial))
284                                 return telemetries.get(serial);
285                         int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
286                                                    AltosLib.ao_telemetry_standard);
287                         telemetries.put(serial, telemetry);
288                         return telemetry;
289                 }
290         }
291
292         public static void set_telemetry_rate(int serial, int new_telemetry_rate) {
293                 synchronized (backend) {
294                         telemetry_rates.put(serial, new_telemetry_rate);
295                         backend.putInt(String.format(telemetryRatePreferenceFormat, serial), new_telemetry_rate);
296                         flush_preferences();
297                 }
298         }
299
300         public static int telemetry_rate(int serial) {
301                 synchronized (backend) {
302                         if (telemetry_rates.containsKey(serial))
303                                 return telemetry_rates.get(serial);
304                         int telemetry_rate = backend.getInt(String.format(telemetryRatePreferenceFormat, serial),
305                                                             AltosLib.ao_telemetry_rate_38400);
306                         telemetry_rates.put(serial, telemetry_rate);
307                         return telemetry_rate;
308                 }
309         }
310
311         public static void set_logfile(int serial, File new_logfile) {
312                 synchronized(backend) {
313                         logfiles.put(serial, new_logfile);
314                         backend.putString(String.format(logfilePreferenceFormat, serial), new_logfile.getPath());
315                         flush_preferences();
316                 }
317         }
318
319         public static File logfile(int serial) {
320                 synchronized(backend) {
321                         if (logfiles.containsKey(serial))
322                                 return logfiles.get(serial);
323                         String logfile_string = backend.getString(String.format(logfilePreferenceFormat, serial), null);
324                         if (logfile_string == null)
325                                 return null;
326                         File logfile = new File(logfile_string);
327                         logfiles.put(serial, logfile);
328                         return logfile;
329                 }
330         }
331
332         public static void set_scanning_telemetry(int new_scanning_telemetry) {
333                 synchronized (backend) {
334                         scanning_telemetry = new_scanning_telemetry;
335                         backend.putInt(scanningTelemetryPreference, scanning_telemetry);
336                         flush_preferences();
337                 }
338         }
339
340         public static int scanning_telemetry() {
341                 synchronized (backend) {
342                         return scanning_telemetry;
343                 }
344         }
345
346         public static void set_scanning_telemetry_rate(int new_scanning_telemetry_rate) {
347                 synchronized (backend) {
348                         scanning_telemetry_rate = new_scanning_telemetry_rate;
349                         backend.putInt(scanningTelemetryRatePreference, scanning_telemetry_rate);
350                         flush_preferences();
351                 }
352         }
353
354         public static int scanning_telemetry_rate() {
355                 synchronized(backend) {
356                         return scanning_telemetry_rate;
357                 }
358         }
359
360         public static void set_voice(boolean new_voice) {
361                 synchronized (backend) {
362                         voice = new_voice;
363                         backend.putBoolean(voicePreference, voice);
364                         flush_preferences();
365                 }
366         }
367
368         public static boolean voice() {
369                 synchronized (backend) {
370                         return voice;
371                 }
372         }
373
374         public static void set_callsign(String new_callsign) {
375                 synchronized(backend) {
376                         callsign = new_callsign;
377                         backend.putString(callsignPreference, callsign);
378                         flush_preferences();
379                 }
380         }
381
382         public static String callsign() {
383                 synchronized(backend) {
384                         return callsign;
385                 }
386         }
387
388         public static void set_firmwaredir(File new_firmwaredir) {
389                 synchronized (backend) {
390                         firmwaredir = new_firmwaredir;
391                         backend.putString(firmwaredirPreference, firmwaredir.getPath());
392                         flush_preferences();
393                 }
394         }
395
396         public static File firmwaredir() {
397                 synchronized (backend) {
398                         return firmwaredir;
399                 }
400         }
401
402         public static void set_launcher_serial(int new_launcher_serial) {
403                 synchronized (backend) {
404                         launcher_serial = new_launcher_serial;
405                         backend.putInt(launcherSerialPreference, launcher_serial);
406                         flush_preferences();
407                 }
408         }
409
410         public static int launcher_serial() {
411                 synchronized (backend) {
412                         return launcher_serial;
413                 }
414         }
415
416         public static void set_launcher_channel(int new_launcher_channel) {
417                 synchronized (backend) {
418                         launcher_channel = new_launcher_channel;
419                         backend.putInt(launcherChannelPreference, launcher_channel);
420                         flush_preferences();
421                 }
422         }
423
424         public static int launcher_channel() {
425                 synchronized (backend) {
426                         return launcher_channel;
427                 }
428         }
429
430         public static AltosPreferencesBackend bt_devices() {
431                 synchronized (backend) {
432                         return backend.node("bt_devices");
433                 }
434         }
435
436         public static AltosFrequency[] common_frequencies() {
437                 synchronized (backend) {
438                         return common_frequencies;
439                 }
440         }
441
442         public static void set_common_frequencies(AltosFrequency[] frequencies) {
443                 synchronized(backend) {
444                         common_frequencies = frequencies;
445                         save_common_frequencies(frequencies);
446                         flush_preferences();
447                 }
448         }
449
450         public static void add_common_frequency(AltosFrequency frequency) {
451                 AltosFrequency[]        old_frequencies = common_frequencies();
452                 AltosFrequency[]        new_frequencies = new AltosFrequency[old_frequencies.length + 1];
453                 int                     i;
454
455                 for (i = 0; i < old_frequencies.length; i++) {
456                         if (frequency.frequency == old_frequencies[i].frequency)
457                                 return;
458                         if (frequency.frequency < old_frequencies[i].frequency)
459                                 break;
460                         new_frequencies[i] = old_frequencies[i];
461                 }
462                 new_frequencies[i] = frequency;
463                 for (; i < old_frequencies.length; i++)
464                         new_frequencies[i+1] = old_frequencies[i];
465                 set_common_frequencies(new_frequencies);
466         }
467
468         static LinkedList<AltosUnitsListener> units_listeners;
469
470         public static boolean imperial_units() {
471                 synchronized(backend) {
472                         return AltosConvert.imperial_units;
473                 }
474         }
475
476         public static void set_imperial_units(boolean imperial_units) {
477                 synchronized (backend) {
478                         AltosConvert.imperial_units = imperial_units;
479                         backend.putBoolean(unitsPreference, imperial_units);
480                         flush_preferences();
481                 }
482                 if (units_listeners != null) {
483                         for (AltosUnitsListener l : units_listeners) {
484                                 l.units_changed(imperial_units);
485                         }
486                 }
487         }
488
489         public static void register_units_listener(AltosUnitsListener l) {
490                 synchronized(backend) {
491                         if (units_listeners == null)
492                                 units_listeners = new LinkedList<AltosUnitsListener>();
493                         units_listeners.add(l);
494                 }
495         }
496
497         public static void unregister_units_listener(AltosUnitsListener l) {
498                 synchronized(backend) {
499                         units_listeners.remove(l);
500                 }
501         }
502 }