f23955685389d43ed93bf4e12adf626676a375e2
[fw/altos] / altosdroid / app / src / main / java / 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.util.*;
21
22 import android.content.Context;
23 import org.altusmetrum.altoslib_13.*;
24
25 public class AltosDroidPreferences extends AltosPreferences {
26
27         /* Active device preference name */
28         final static String activeDeviceAddressPreference = "ACTIVE-DEVICE-ADDRESS";
29         final static String activeDeviceNamePreference = "ACTIVE-DEVICE-NAME";
30
31         public static final int font_size_small = 0;
32         public static final int font_size_medium = 1;
33         public static final int font_size_large = 2;
34         public static final int font_size_extra = 3;
35
36         final static String fontSizePreference = "FONT-SIZE";
37
38         static int font_size = font_size_medium;
39
40         static DeviceAddress    active_device_address;
41
42         /* Map source preference name */
43         final static String mapSourcePreference = "MAP-SOURCE";
44
45         static final int        MAP_SOURCE_OFFLINE = 0;
46         static final int        MAP_SOURCE_ONLINE = 1;
47
48         static int      map_source;
49
50         public static void init(Context context) {
51                 if (backend != null)
52                         return;
53
54                 AltosPreferences.init(new AltosDroidPreferencesBackend(context));
55
56                 font_size = backend.getInt(fontSizePreference, font_size_medium);
57
58                 String address = backend.getString(activeDeviceAddressPreference, null);
59                 String name = backend.getString(activeDeviceNamePreference, null);
60
61                 if (address != null && name != null)
62                         active_device_address = new DeviceAddress (address, name);
63
64                 map_source = backend.getInt(mapSourcePreference, MAP_SOURCE_ONLINE);
65         }
66
67         public static void set_active_device(DeviceAddress address) {
68                 synchronized(backend) {
69                         active_device_address = address;
70                         if (active_device_address != null) {
71                                 backend.putString(activeDeviceAddressPreference, active_device_address.address);
72                                 backend.putString(activeDeviceNamePreference, active_device_address.name);
73                         } else {
74                                 backend.remove(activeDeviceAddressPreference);
75                                 backend.remove(activeDeviceNamePreference);
76                         }
77                         flush_preferences();
78                 }
79         }
80
81         public static DeviceAddress active_device() {
82                 synchronized(backend) {
83                         return active_device_address;
84                 }
85         }
86
87         static LinkedList<AltosDroidMapSourceListener> map_source_listeners;
88
89         public static void set_map_source(int map_source) {
90                 synchronized(backend) {
91                         AltosDroidPreferences.map_source = map_source;
92                         backend.putInt(mapSourcePreference, map_source);
93                         flush_preferences();
94                 }
95                 if (map_source_listeners != null) {
96                         for (AltosDroidMapSourceListener l : map_source_listeners) {
97                                 l.map_source_changed(map_source);
98                         }
99                 }
100         }
101
102         public static int map_source() {
103                 synchronized(backend) {
104                         return map_source;
105                 }
106         }
107
108         public static void register_map_source_listener(AltosDroidMapSourceListener l) {
109                 synchronized(backend) {
110                         if (map_source_listeners == null)
111                                 map_source_listeners = new LinkedList<AltosDroidMapSourceListener>();
112                         map_source_listeners.add(l);
113                 }
114         }
115
116         public static void unregister_map_source_listener(AltosDroidMapSourceListener l) {
117                 synchronized(backend) {
118                         map_source_listeners.remove(l);
119                 }
120         }
121
122         public static int font_size() {
123                 synchronized (backend) {
124                         return font_size;
125                 }
126         }
127
128         public static void set_font_size(int new_font_size) {
129                 synchronized (backend) {
130                         if (font_size != new_font_size) {
131                                 font_size = new_font_size;
132                                 backend.putInt(fontSizePreference, font_size);
133                                 flush_preferences();
134                         }
135                 }
136         }
137 }