Bump java library versions
[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; 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 package org.altusmetrum.AltosDroid;
18
19 import java.io.*;
20 import java.util.*;
21 import java.text.*;
22
23 import android.content.Context;
24 import org.altusmetrum.altoslib_11.*;
25
26 public class AltosDroidPreferences extends AltosPreferences {
27
28         /* Active device preference name */
29         final static String activeDeviceAddressPreference = "ACTIVE-DEVICE-ADDRESS";
30         final static String activeDeviceNamePreference = "ACTIVE-DEVICE-NAME";
31
32         static DeviceAddress    active_device_address;
33
34         /* Map source preference name */
35         final static String mapSourcePreference = "MAP-SOURCE";
36
37         static final int        MAP_SOURCE_OFFLINE = 0;
38         static final int        MAP_SOURCE_ONLINE = 1;
39
40         static int      map_source;
41
42         public static void init(Context context) {
43                 if (backend != null)
44                         return;
45
46                 AltosPreferences.init(new AltosDroidPreferencesBackend(context));
47
48                 String address = backend.getString(activeDeviceAddressPreference, null);
49                 String name = backend.getString(activeDeviceNamePreference, null);
50
51                 if (address != null && name != null)
52                         active_device_address = new DeviceAddress (address, name);
53
54                 map_source = backend.getInt(mapSourcePreference, MAP_SOURCE_ONLINE);
55         }
56
57         public static void set_active_device(DeviceAddress address) {
58                 synchronized(backend) {
59                         active_device_address = address;
60                         if (active_device_address != null) {
61                                 backend.putString(activeDeviceAddressPreference, active_device_address.address);
62                                 backend.putString(activeDeviceNamePreference, active_device_address.name);
63                         } else {
64                                 backend.remove(activeDeviceAddressPreference);
65                                 backend.remove(activeDeviceNamePreference);
66                         }
67                         flush_preferences();
68                 }
69         }
70
71         public static DeviceAddress active_device() {
72                 synchronized(backend) {
73                         return active_device_address;
74                 }
75         }
76
77         static LinkedList<AltosDroidMapSourceListener> map_source_listeners;
78
79         public static void set_map_source(int map_source) {
80                 synchronized(backend) {
81                         AltosDroidPreferences.map_source = map_source;
82                         backend.putInt(mapSourcePreference, map_source);
83                         flush_preferences();
84                 }
85                 if (map_source_listeners != null) {
86                         for (AltosDroidMapSourceListener l : map_source_listeners) {
87                                 l.map_source_changed(map_source);
88                         }
89                 }
90         }
91
92         public static int map_source() {
93                 synchronized(backend) {
94                         return map_source;
95                 }
96         }
97
98         public static void register_map_source_listener(AltosDroidMapSourceListener l) {
99                 synchronized(backend) {
100                         if (map_source_listeners == null)
101                                 map_source_listeners = new LinkedList<AltosDroidMapSourceListener>();
102                         map_source_listeners.add(l);
103                 }
104         }
105
106         public static void unregister_map_source_listener(AltosDroidMapSourceListener l) {
107                 synchronized(backend) {
108                         map_source_listeners.remove(l);
109                 }
110         }
111 }