first cut at turnon scripts for EasyTimer v2
[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                 if (backend == null)
76                         return;
77                 synchronized(backend) {
78                         active_device_address = address;
79                         if (active_device_address != null) {
80                                 backend.putString(activeDeviceAddressPreference, active_device_address.address);
81                                 backend.putString(activeDeviceNamePreference, active_device_address.name);
82                         } else {
83                                 backend.remove(activeDeviceAddressPreference);
84                                 backend.remove(activeDeviceNamePreference);
85                         }
86                         flush_preferences();
87                 }
88         }
89
90         public static DeviceAddress active_device() {
91                 if (backend == null)
92                         return null;
93
94                 synchronized(backend) {
95                         return active_device_address;
96                 }
97         }
98
99         static LinkedList<AltosDroidMapSourceListener> map_source_listeners;
100
101         public static void set_map_source(int map_source) {
102                 if (backend == null)
103                         return;
104                 synchronized(backend) {
105                         AltosDroidPreferences.map_source = map_source;
106                         backend.putInt(mapSourcePreference, map_source);
107                         flush_preferences();
108                 }
109                 if (map_source_listeners != null) {
110                         for (AltosDroidMapSourceListener l : map_source_listeners) {
111                                 l.map_source_changed(map_source);
112                         }
113                 }
114         }
115
116         public static int map_source() {
117                 if (backend == null)
118                         return MAP_SOURCE_ONLINE;
119                 synchronized(backend) {
120                         return map_source;
121                 }
122         }
123
124         public static void register_map_source_listener(AltosDroidMapSourceListener l) {
125                 if (backend == null)
126                         return;
127
128                 synchronized(backend) {
129                         if (map_source_listeners == null)
130                                 map_source_listeners = new LinkedList<AltosDroidMapSourceListener>();
131                         map_source_listeners.add(l);
132                 }
133         }
134
135         public static void unregister_map_source_listener(AltosDroidMapSourceListener l) {
136                 if (backend == null)
137                         return;
138                 synchronized(backend) {
139                         map_source_listeners.remove(l);
140                 }
141         }
142
143         public static int font_size() {
144                 if (backend == null)
145                         return font_size_medium;
146
147                 synchronized (backend) {
148                         return font_size;
149                 }
150         }
151
152         public static void set_font_size(int new_font_size) {
153                 if (backend == null)
154                         return;
155                 synchronized (backend) {
156                         if (font_size != new_font_size) {
157                                 font_size = new_font_size;
158                                 backend.putInt(fontSizePreference, font_size);
159                                 flush_preferences();
160                         }
161                 }
162         }
163
164
165         public static int tracker_sort() {
166                 if (backend == null)
167                         return 0;
168
169                 synchronized(backend) {
170                         return tracker_sort;
171                 }
172         }
173
174         public static void set_tracker_sort(int new_tracker_sort) {
175                 if (backend == null)
176                         return;
177
178                 synchronized(backend) {
179                         if (tracker_sort != new_tracker_sort) {
180                                 tracker_sort = new_tracker_sort;
181                                 backend.putInt(trackerSortPreference, tracker_sort);
182                                 flush_preferences();
183                         }
184                 }
185         }
186 }