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; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
19 package org.altusmetrum.altosuilib_12;
22 import org.altusmetrum.altoslib_12.*;
24 public class AltosBTKnown implements Iterable<AltosBTDevice> {
25 LinkedList<AltosBTDevice> devices = new LinkedList<AltosBTDevice>();
26 AltosPreferencesBackend bt_pref = AltosUIPreferences.bt_devices();
28 private String get_address(String name) {
29 return bt_pref.getString(name, "");
32 private void set_address(String name, String addr) {
33 bt_pref.putString(name, addr);
36 private void remove(String name) {
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));
48 } catch (IllegalStateException ie) {
52 public Iterator<AltosBTDevice> iterator() {
53 return devices.iterator();
56 private void flush() {
57 AltosUIPreferences.flush_preferences();
60 public void set(Iterable<AltosBTDevice> new_devices) {
61 for (AltosBTDevice old : devices) {
62 boolean found = false;
63 for (AltosBTDevice new_device : new_devices) {
64 if (new_device.equals(old)) {
70 remove(old.getName());
72 devices = new LinkedList<AltosBTDevice>();
73 for (AltosBTDevice new_device : new_devices) {
74 devices.add(new_device);
75 set_address(new_device.getName(), new_device.getAddr());
80 public List<AltosDevice> list(int product) {
81 LinkedList<AltosDevice> list = new LinkedList<AltosDevice>();
82 for (AltosBTDevice device : devices) {
83 if (device.matchProduct(product))
89 public AltosBTKnown() {
90 devices = new LinkedList<AltosBTDevice>();
91 bt_pref = AltosUIPreferences.bt_devices();
95 static AltosBTKnown known;
97 static public AltosBTKnown bt_known() {
99 known = new AltosBTKnown();