altosui: Make bluetooth dialog modal
[fw/altos] / altosui / AltosBTDevice.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
23 public class AltosBTDevice extends altos_bt_device implements AltosDevice {
24
25         public String getProductName() {
26                 String  name = getName();
27                 if (name == null)
28                         return "Altus Metrum";
29                 int     dash = name.lastIndexOf("-");
30                 if (dash < 0)
31                         return name;
32                 return name.substring(0,dash);
33         }
34
35         public int getProduct() {
36                 if (Altos.bt_product_telebt.equals(getProductName()))
37                         return Altos.product_telebt;
38                 return 0;
39         }
40
41         public String getPath() {
42                 return getAddr();
43         }
44
45         public int getSerial() {
46                 String name = getName();
47                 if (name == null)
48                         return 0;
49                 int dash = name.lastIndexOf("-");
50                 if (dash < 0 || dash >= name.length())
51                         return 0;
52                 String sn = name.substring(dash + 1, name.length());
53                 try {
54                         return Integer.parseInt(sn);
55                 } catch (NumberFormatException ne) {
56                         return 0;
57                 }
58         }
59
60         public String toString() {
61                 String  name = getName();
62                 if (name == null)
63                         name = "Altus Metrum";
64                 return String.format("%-20.20s %4d %s",
65                                      getProductName(), getSerial(), getAddr());
66         }
67
68         public String toShortString() {
69                 String  name = getName();
70                 if (name == null)
71                         name = "Altus Metrum";
72                 return String.format("%s %d %s",
73                                      getProduct(), getSerial(), getAddr());
74
75         }
76
77         public SWIGTYPE_p_altos_file open() {
78                 return libaltos.altos_bt_open(this);
79         }
80
81         private boolean isAltusMetrum() {
82                 if (getName().startsWith(Altos.bt_product_telebt))
83                         return true;
84                 return false;
85         }
86
87         public boolean matchProduct(int want_product) {
88
89                 if (!isAltusMetrum())
90                         return false;
91
92                 if (want_product == Altos.product_any)
93                         return true;
94
95                 if (want_product == Altos.product_basestation)
96                         return matchProduct(Altos.product_telebt);
97
98                 if (want_product == getProduct())
99                         return true;
100
101                 return false;
102         }
103 }