Convert to AndroidX from support_v4
[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         static DeviceAddress    active_device_address;
32
33         /* Map source preference name */
34         final static String mapSourcePreference = "MAP-SOURCE";
35
36         static final int        MAP_SOURCE_OFFLINE = 0;
37         static final int        MAP_SOURCE_ONLINE = 1;
38
39         static int      map_source;
40
41         public static void init(Context context) {
42                 if (backend != null)
43                         return;
44
45                 AltosPreferences.init(new AltosDroidPreferencesBackend(context));
46
47                 String address = backend.getString(activeDeviceAddressPreference, null);
48                 String name = backend.getString(activeDeviceNamePreference, null);
49
50                 if (address != null && name != null)
51                         active_device_address = new DeviceAddress (address, name);
52
53                 map_source = backend.getInt(mapSourcePreference, MAP_SOURCE_ONLINE);
54         }
55
56         public static void set_active_device(DeviceAddress address) {
57                 synchronized(backend) {
58                         active_device_address = address;
59                         if (active_device_address != null) {
60                                 backend.putString(activeDeviceAddressPreference, active_device_address.address);
61                                 backend.putString(activeDeviceNamePreference, active_device_address.name);
62                         } else {
63                                 backend.remove(activeDeviceAddressPreference);
64                                 backend.remove(activeDeviceNamePreference);
65                         }
66                         flush_preferences();
67                 }
68         }
69
70         public static DeviceAddress active_device() {
71                 synchronized(backend) {
72                         return active_device_address;
73                 }
74         }
75
76         static LinkedList<AltosDroidMapSourceListener> map_source_listeners;
77
78         public static void set_map_source(int map_source) {
79                 synchronized(backend) {
80                         AltosDroidPreferences.map_source = map_source;
81                         backend.putInt(mapSourcePreference, map_source);
82                         flush_preferences();
83                 }
84                 if (map_source_listeners != null) {
85                         for (AltosDroidMapSourceListener l : map_source_listeners) {
86                                 l.map_source_changed(map_source);
87                         }
88                 }
89         }
90
91         public static int map_source() {
92                 synchronized(backend) {
93                         return map_source;
94                 }
95         }
96
97         public static void register_map_source_listener(AltosDroidMapSourceListener l) {
98                 synchronized(backend) {
99                         if (map_source_listeners == null)
100                                 map_source_listeners = new LinkedList<AltosDroidMapSourceListener>();
101                         map_source_listeners.add(l);
102                 }
103         }
104
105         public static void unregister_map_source_listener(AltosDroidMapSourceListener l) {
106                 synchronized(backend) {
107                         map_source_listeners.remove(l);
108                 }
109         }
110 }