ad0672c6847ddac18a05f51445d1e95555d8e0ee
[fw/altos] / altosui / AltosBTKnown.java
1 /*
2  * Copyright © 2011 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
18 package AltosUI;
19 import java.lang.*;
20 import java.util.*;
21 import libaltosJNI.*;
22 import java.util.prefs.*;
23
24 public class AltosBTKnown implements Iterable<AltosBTDevice> {
25         LinkedList<AltosBTDevice>       devices = new LinkedList<AltosBTDevice>();
26         Preferences                     bt_pref = AltosUIPreferences.bt_devices();
27
28         private String get_address(String name) {
29                 return bt_pref.get(name, "");
30         }
31
32         private void set_address(String name, String addr) {
33                 bt_pref.put(name, addr);
34         }
35
36         private void remove(String name) {
37                 bt_pref.remove(name);
38         }
39
40         private void load() {
41                 try {
42                         String[] names = bt_pref.keys();
43                         for (int i = 0; i < names.length; i++) {
44                                 String  name = names[i];
45                                 String  addr = get_address(name);
46                                 devices.add(new AltosBTDevice(name, addr));
47                         }
48                 } catch (BackingStoreException be) {
49                 } catch (IllegalStateException ie) {
50                 }
51         }
52
53         public Iterator<AltosBTDevice> iterator() {
54                 return devices.iterator();
55         }
56
57         private void flush() {
58                 AltosUIPreferences.flush_preferences();
59         }
60
61         public void set(Iterable<AltosBTDevice> new_devices) {
62                 for (AltosBTDevice old : devices) {
63                         boolean found = false;
64                         for (AltosBTDevice new_device : new_devices) {
65                                 if (new_device.equals(old)) {
66                                         found = true;
67                                         break;
68                                 }
69                         }
70                         if (!found)
71                                 remove(old.getName());
72                 }
73                 devices = new LinkedList<AltosBTDevice>();
74                 for (AltosBTDevice new_device : new_devices) {
75                         devices.add(new_device);
76                         set_address(new_device.getName(), new_device.getAddr());
77                 }
78                 flush();
79         }
80
81         public List<AltosDevice> list(int product) {
82                 LinkedList<AltosDevice> list = new LinkedList<AltosDevice>();
83                 for (AltosBTDevice device : devices) {
84                         if (device.matchProduct(product))
85                                 list.add(device);
86                 }
87                 return list;
88         }
89
90         public AltosBTKnown() {
91                 devices = new LinkedList<AltosBTDevice>();
92                 bt_pref = AltosUIPreferences.bt_devices();
93                 load();
94         }
95
96         static AltosBTKnown     known;
97
98         static public AltosBTKnown bt_known() {
99                 if (known == null)
100                         known = new AltosBTKnown();
101                 return known;
102         }
103 }