altoslib,altosuilib: Bump library version numbers
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosDroidPreferences.java
1 /*
2  * Copyright © 2014 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18 package org.altusmetrum.AltosDroid;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 import android.content.Context;
25 import org.altusmetrum.altoslib_12.*;
26
27 public class AltosDroidPreferences extends AltosPreferences {
28
29         /* Active device preference name */
30         final static String activeDeviceAddressPreference = "ACTIVE-DEVICE-ADDRESS";
31         final static String activeDeviceNamePreference = "ACTIVE-DEVICE-NAME";
32
33         static DeviceAddress    active_device_address;
34
35         /* Map source preference name */
36         final static String mapSourcePreference = "MAP-SOURCE";
37
38         static final int        MAP_SOURCE_OFFLINE = 0;
39         static final int        MAP_SOURCE_ONLINE = 1;
40
41         static int      map_source;
42
43         public static void init(Context context) {
44                 if (backend != null)
45                         return;
46
47                 AltosPreferences.init(new AltosDroidPreferencesBackend(context));
48
49                 String address = backend.getString(activeDeviceAddressPreference, null);
50                 String name = backend.getString(activeDeviceNamePreference, null);
51
52                 if (address != null && name != null)
53                         active_device_address = new DeviceAddress (address, name);
54
55                 map_source = backend.getInt(mapSourcePreference, MAP_SOURCE_ONLINE);
56         }
57
58         public static void set_active_device(DeviceAddress address) {
59                 synchronized(backend) {
60                         active_device_address = address;
61                         if (active_device_address != null) {
62                                 backend.putString(activeDeviceAddressPreference, active_device_address.address);
63                                 backend.putString(activeDeviceNamePreference, active_device_address.name);
64                         } else {
65                                 backend.remove(activeDeviceAddressPreference);
66                                 backend.remove(activeDeviceNamePreference);
67                         }
68                         flush_preferences();
69                 }
70         }
71
72         public static DeviceAddress active_device() {
73                 synchronized(backend) {
74                         return active_device_address;
75                 }
76         }
77
78         static LinkedList<AltosDroidMapSourceListener> map_source_listeners;
79
80         public static void set_map_source(int map_source) {
81                 synchronized(backend) {
82                         AltosDroidPreferences.map_source = map_source;
83                         backend.putInt(mapSourcePreference, map_source);
84                         flush_preferences();
85                 }
86                 if (map_source_listeners != null) {
87                         for (AltosDroidMapSourceListener l : map_source_listeners) {
88                                 l.map_source_changed(map_source);
89                         }
90                 }
91         }
92
93         public static int map_source() {
94                 synchronized(backend) {
95                         return map_source;
96                 }
97         }
98
99         public static void register_map_source_listener(AltosDroidMapSourceListener l) {
100                 synchronized(backend) {
101                         if (map_source_listeners == null)
102                                 map_source_listeners = new LinkedList<AltosDroidMapSourceListener>();
103                         map_source_listeners.add(l);
104                 }
105         }
106
107         public static void unregister_map_source_listener(AltosDroidMapSourceListener l) {
108                 synchronized(backend) {
109                         map_source_listeners.remove(l);
110                 }
111         }
112 }