]> git.gag.com Git - fw/altos/blob - altosuilib/AltosBTKnown.java
altos: A bit more LCO code sharing
[fw/altos] / altosuilib / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altosuilib_12;
20
21 import java.util.*;
22 import org.altusmetrum.altoslib_12.*;
23
24 public class AltosBTKnown implements Iterable<AltosBTDevice> {
25         LinkedList<AltosBTDevice>       devices = new LinkedList<AltosBTDevice>();
26         AltosPreferencesBackend         bt_pref = AltosUIPreferences.bt_devices();
27
28         private String get_address(String name) {
29                 return bt_pref.getString(name, "");
30         }
31
32         private void set_address(String name, String addr) {
33                 bt_pref.putString(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 (IllegalStateException ie) {
49                 }
50         }
51
52         public Iterator<AltosBTDevice> iterator() {
53                 return devices.iterator();
54         }
55
56         private void flush() {
57                 AltosUIPreferences.flush_preferences();
58         }
59
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)) {
65                                         found = true;
66                                         break;
67                                 }
68                         }
69                         if (!found)
70                                 remove(old.getName());
71                 }
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());
76                 }
77                 flush();
78         }
79
80         public List<AltosDevice> list(int product) {
81                 LinkedList<AltosDevice> list = new LinkedList<AltosDevice>();
82                 for (AltosBTDevice device : devices) {
83                         if (device.matchProduct(product))
84                                 list.add(device);
85                 }
86                 return list;
87         }
88
89         public AltosBTKnown() {
90                 devices = new LinkedList<AltosBTDevice>();
91                 bt_pref = AltosUIPreferences.bt_devices();
92                 load();
93         }
94
95         static AltosBTKnown     known;
96
97         static public AltosBTKnown bt_known() {
98                 if (known == null)
99                         known = new AltosBTKnown();
100                 return known;
101         }
102 }