Bump java lib versions in preparation for 1.9.2
[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_14.*;
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         /* Tracker sort selection */
51         final static String trackerSortPreference = "TRACKER-SORT";
52
53         static int      tracker_sort;
54
55         public static void init(Context context) {
56                 if (backend != null)
57                         return;
58
59                 AltosPreferences.init(new AltosDroidPreferencesBackend(context));
60
61                 font_size = backend.getInt(fontSizePreference, font_size_medium);
62
63                 String address = backend.getString(activeDeviceAddressPreference, null);
64                 String name = backend.getString(activeDeviceNamePreference, null);
65
66                 if (address != null && name != null)
67                         active_device_address = new DeviceAddress (address, name);
68
69                 map_source = backend.getInt(mapSourcePreference, MAP_SOURCE_ONLINE);
70
71                 tracker_sort = backend.getInt(trackerSortPreference, 0);
72         }
73
74         public static void set_active_device(DeviceAddress address) {
75                 synchronized(backend) {
76                         active_device_address = address;
77                         if (active_device_address != null) {
78                                 backend.putString(activeDeviceAddressPreference, active_device_address.address);
79                                 backend.putString(activeDeviceNamePreference, active_device_address.name);
80                         } else {
81                                 backend.remove(activeDeviceAddressPreference);
82                                 backend.remove(activeDeviceNamePreference);
83                         }
84                         flush_preferences();
85                 }
86         }
87
88         public static DeviceAddress active_device() {
89                 synchronized(backend) {
90                         return active_device_address;
91                 }
92         }
93
94         static LinkedList<AltosDroidMapSourceListener> map_source_listeners;
95
96         public static void set_map_source(int map_source) {
97                 synchronized(backend) {
98                         AltosDroidPreferences.map_source = map_source;
99                         backend.putInt(mapSourcePreference, map_source);
100                         flush_preferences();
101                 }
102                 if (map_source_listeners != null) {
103                         for (AltosDroidMapSourceListener l : map_source_listeners) {
104                                 l.map_source_changed(map_source);
105                         }
106                 }
107         }
108
109         public static int map_source() {
110                 synchronized(backend) {
111                         return map_source;
112                 }
113         }
114
115         public static void register_map_source_listener(AltosDroidMapSourceListener l) {
116                 synchronized(backend) {
117                         if (map_source_listeners == null)
118                                 map_source_listeners = new LinkedList<AltosDroidMapSourceListener>();
119                         map_source_listeners.add(l);
120                 }
121         }
122
123         public static void unregister_map_source_listener(AltosDroidMapSourceListener l) {
124                 synchronized(backend) {
125                         map_source_listeners.remove(l);
126                 }
127         }
128
129         public static int font_size() {
130                 synchronized (backend) {
131                         return font_size;
132                 }
133         }
134
135         public static void set_font_size(int new_font_size) {
136                 synchronized (backend) {
137                         if (font_size != new_font_size) {
138                                 font_size = new_font_size;
139                                 backend.putInt(fontSizePreference, font_size);
140                                 flush_preferences();
141                         }
142                 }
143         }
144
145
146         public static int tracker_sort() {
147                 synchronized(backend) {
148                         return tracker_sort;
149                 }
150         }
151
152         public static void set_tracker_sort(int new_tracker_sort) {
153                 synchronized(backend) {
154                         if (tracker_sort != new_tracker_sort) {
155                                 tracker_sort = new_tracker_sort;
156                                 backend.putInt(trackerSortPreference, tracker_sort);
157                                 flush_preferences();
158                         }
159                 }
160         }
161 }