altosui: Use persistent list of bluetooth devices for device dialogs
[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 = AltosPreferences.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                 System.out.printf("saving known %s %s\n", name, addr);
35         }
36
37         private void remove(String name) {
38                 bt_pref.remove(name);
39         }
40
41         private void load() {
42                 try {
43                         String[] names = bt_pref.keys();
44                         for (int i = 0; i < names.length; i++) {
45                                 String  name = names[i];
46                                 String  addr = get_address(name);
47                                 System.out.printf("Known device %s %s\n", name, addr);
48                                 devices.add(new AltosBTDevice(name, addr));
49                         }
50                 } catch (BackingStoreException be) {
51                 } catch (IllegalStateException ie) {
52                 }
53         }
54
55         public Iterator<AltosBTDevice> iterator() {
56                 return devices.iterator();
57         }
58
59         private void flush() {
60                 AltosPreferences.flush_preferences();
61         }
62
63         public void set(Iterable<AltosBTDevice> new_devices) {
64                 for (AltosBTDevice old : devices) {
65                         boolean found = false;
66                         for (AltosBTDevice new_device : new_devices) {
67                                 if (new_device.equals(old)) {
68                                         found = true;
69                                         break;
70                                 }
71                         }
72                         if (!found)
73                                 remove(old.getName());
74                 }
75                 devices = new LinkedList<AltosBTDevice>();
76                 for (AltosBTDevice new_device : new_devices) {
77                         devices.add(new_device);
78                         set_address(new_device.getName(), new_device.getAddr());
79                 }
80                 flush();
81         }
82
83         public List<AltosDevice> list(int product) {
84                 LinkedList<AltosDevice> list = new LinkedList<AltosDevice>();
85                 for (AltosBTDevice device : devices) {
86                         if (device.matchProduct(product))
87                                 list.add(device);
88                 }
89                 return list;
90         }
91
92         public AltosBTKnown() {
93                 devices = new LinkedList<AltosBTDevice>();
94                 bt_pref = AltosPreferences.bt_devices();
95                 load();
96         }
97 }