2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 import org.altusmetrum.altoslib_4.*;
21 import org.altusmetrum.altosuilib_2.*;
23 public class AltosBTKnown implements Iterable<AltosBTDevice> {
24 LinkedList<AltosBTDevice> devices = new LinkedList<AltosBTDevice>();
25 AltosPreferencesBackend bt_pref = AltosUIPreferences.bt_devices();
27 private String get_address(String name) {
28 return bt_pref.getString(name, "");
31 private void set_address(String name, String addr) {
32 bt_pref.putString(name, addr);
35 private void remove(String name) {
41 String[] names = bt_pref.keys();
42 for (int i = 0; i < names.length; i++) {
43 String name = names[i];
44 String addr = get_address(name);
45 devices.add(new AltosBTDevice(name, addr));
47 } catch (IllegalStateException ie) {
51 public Iterator<AltosBTDevice> iterator() {
52 return devices.iterator();
55 private void flush() {
56 AltosUIPreferences.flush_preferences();
59 public void set(Iterable<AltosBTDevice> new_devices) {
60 for (AltosBTDevice old : devices) {
61 boolean found = false;
62 for (AltosBTDevice new_device : new_devices) {
63 if (new_device.equals(old)) {
69 remove(old.getName());
71 devices = new LinkedList<AltosBTDevice>();
72 for (AltosBTDevice new_device : new_devices) {
73 devices.add(new_device);
74 set_address(new_device.getName(), new_device.getAddr());
79 public List<AltosDevice> list(int product) {
80 LinkedList<AltosDevice> list = new LinkedList<AltosDevice>();
81 for (AltosBTDevice device : devices) {
82 if (device.matchProduct(product))
88 public AltosBTKnown() {
89 devices = new LinkedList<AltosBTDevice>();
90 bt_pref = AltosUIPreferences.bt_devices();
94 static AltosBTKnown known;
96 static public AltosBTKnown bt_known() {
98 known = new AltosBTKnown();