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