2626import android .net .wifi .WifiConfiguration ;
2727import android .net .wifi .WifiConfiguration .AuthAlgorithm ;
2828import android .net .wifi .WifiConfiguration .KeyMgmt ;
29+ import android .net .DhcpInfo ;
2930
30- import android .util .Log ;
3131import java .io .InputStream ;
32+ import java .net .UnknownHostException ;
3233import java .util .ArrayList ;
34+ import java .util .HashMap ;
3335import java .util .List ;
3436
3537
3840 * The configurations of an access point is included in tag
3941 * <accesspoint></accesspoint>. The supported configuration includes: ssid,
4042 * security, eap, phase2, identity, password, anonymousidentity, cacert, usercert,
41- * in which each is included in the corresponding tags. All access points have to be
43+ * in which each is included in the corresponding tags. Static IP setting is also supported.
44+ * Tags that can be used include: ip, gateway, netmask, dns1, dns2. All access points have to be
4245 * enclosed in tags of <resources></resources>.
4346 *
4447 * The following is a sample configuration file for an access point using EAP-PEAP with MSCHAP2.
@@ -62,6 +65,7 @@ public class AccessPointParserHelper {
6265 static final int EAP = 3 ;
6366
6467 List <WifiConfiguration > networks = new ArrayList <WifiConfiguration >();
68+ HashMap <String , DhcpInfo > ssidToDhcpInfoHM = new HashMap <String , DhcpInfo >();
6569
6670 private int getSecurityType (String security ) {
6771 if (security .equalsIgnoreCase ("NONE" )) {
@@ -87,15 +91,34 @@ private boolean validateEapValue(String value) {
8791 }
8892 }
8993
94+ private static int stringToIpAddr (String addrString ) throws UnknownHostException {
95+ try {
96+ String [] parts = addrString .split ("\\ ." );
97+ if (parts .length != 4 ) {
98+ throw new UnknownHostException (addrString );
99+ }
100+
101+ int a = Integer .parseInt (parts [0 ]) ;
102+ int b = Integer .parseInt (parts [1 ]) << 8 ;
103+ int c = Integer .parseInt (parts [2 ]) << 16 ;
104+ int d = Integer .parseInt (parts [3 ]) << 24 ;
105+
106+ return a | b | c | d ;
107+ } catch (NumberFormatException ex ) {
108+ throw new UnknownHostException (addrString );
109+ }
110+ }
111+
90112 DefaultHandler mHandler = new DefaultHandler () {
91113
92114 boolean ssid = false ;
93115 boolean security = false ;
94116 boolean password = false ;
95117 boolean ip = false ;
96- boolean subnetmask = false ;
118+ boolean netmask = false ;
97119 boolean gateway = false ;
98- boolean dns = false ;
120+ boolean dns1 = false ;
121+ boolean dns2 = false ;
99122 boolean eap = false ;
100123 boolean phase2 = false ;
101124 boolean identity = false ;
@@ -104,6 +127,7 @@ private boolean validateEapValue(String value) {
104127 boolean usercert = false ;
105128 WifiConfiguration config = null ;
106129 int securityType = NONE ;
130+ DhcpInfo mDhcpInfo = null ;
107131
108132 @ Override
109133 public void startElement (String uri , String localName , String tagName ,
@@ -138,28 +162,43 @@ public void startElement(String uri, String localName, String tagName,
138162 if (tagName .equalsIgnoreCase ("usercert" )) {
139163 usercert = true ;
140164 }
165+ if (tagName .equalsIgnoreCase ("ip" )) {
166+ ip = true ;
167+ mDhcpInfo = new DhcpInfo ();
168+ }
169+ if (tagName .equalsIgnoreCase ("gateway" )) {
170+ gateway = true ;
171+ }
172+ if (tagName .equalsIgnoreCase ("netmask" )) {
173+ netmask = true ;
174+ }
175+ if (tagName .equalsIgnoreCase ("dns1" )) {
176+ dns1 = true ;
177+ }
178+ if (tagName .equalsIgnoreCase ("dns2" )) {
179+ dns2 = true ;
180+ }
141181 }
142182
143183 @ Override
144184 public void endElement (String uri , String localName , String tagName ) throws SAXException {
145- Log .v (TAG , "endElement: " + tagName );
146185 if (tagName .equalsIgnoreCase ("accesspoint" )) {
147186 networks .add (config );
187+ if (mDhcpInfo != null ) {
188+ ssidToDhcpInfoHM .put (config .SSID , mDhcpInfo );
189+ }
148190 }
149191 }
150192
151193 @ Override
152194 public void characters (char ch [], int start , int length ) throws SAXException {
153195 if (ssid ) {
154196 config .SSID = new String (ch , start , length );
155- Log .v (TAG , "ssid: " + config .SSID );
156197 ssid = false ;
157198 }
158199 if (security ) {
159200 String securityStr = (new String (ch , start , length )).toUpperCase ();
160- Log .v (TAG , "security: " + securityStr );
161201 securityType = getSecurityType (securityStr );
162- Log .v (TAG , "securityType = " + securityType );
163202 switch (securityType ) {
164203 case NONE :
165204 config .allowedKeyManagement .set (KeyMgmt .NONE );
@@ -187,7 +226,6 @@ public void characters(char ch[], int start, int length) throws SAXException {
187226 if (len == 0 ) {
188227 throw new SAXException ();
189228 }
190- Log .v (TAG , "passwordStr:" + passwordStr );
191229 if (securityType == WEP ) {
192230 if ((len == 10 || len == 26 || len == 58 ) &&
193231 passwordStr .matches ("[0-9A-Fa-f]*" )) {
@@ -242,21 +280,65 @@ public void characters(char ch[], int start, int length) throws SAXException {
242280 config .client_cert .setValue (KEYSTORE_SPACE );
243281 usercert = false ;
244282 }
283+ if (ip ) {
284+ try {
285+ mDhcpInfo .ipAddress = stringToIpAddr (new String (ch , start , length ));
286+ } catch (UnknownHostException e ) {
287+ throw new SAXException ();
288+ }
289+ ip = false ;
290+ }
291+ if (gateway ) {
292+ try {
293+ mDhcpInfo .gateway = stringToIpAddr (new String (ch , start , length ));
294+ } catch (UnknownHostException e ) {
295+ throw new SAXException ();
296+ }
297+ gateway = false ;
298+ }
299+ if (netmask ) {
300+ try {
301+ mDhcpInfo .netmask = stringToIpAddr (new String (ch , start , length ));
302+ } catch (UnknownHostException e ) {
303+ throw new SAXException ();
304+ }
305+ netmask = false ;
306+ }
307+ if (dns1 ) {
308+ try {
309+ mDhcpInfo .dns1 = stringToIpAddr (new String (ch , start , length ));
310+ } catch (UnknownHostException e ) {
311+ throw new SAXException ();
312+ }
313+ dns1 = false ;
314+ }
315+ if (dns2 ) {
316+ try {
317+ mDhcpInfo .dns2 = stringToIpAddr (new String (ch , start , length ));
318+ } catch (UnknownHostException e ) {
319+ throw new SAXException ();
320+ }
321+ dns2 = false ;
322+ }
245323 }
246324 };
247325
248- public AccessPointParserHelper () {
249- }
250-
251326 /**
252- * Process the accesspoint.xml file
253- * @return List of WifiConfiguration
254- * @throws Exception when parsing the XML file
327+ * Process the InputStream in
328+ * @param in is the InputStream that can be used for XML parsing
329+ * @throws Exception
255330 */
256- public List < WifiConfiguration > processAccessPoint (InputStream in ) throws Exception {
331+ public AccessPointParserHelper (InputStream in ) throws Exception {
257332 SAXParserFactory factory = SAXParserFactory .newInstance ();
258333 SAXParser saxParser = factory .newSAXParser ();
259334 saxParser .parse (in , mHandler );
335+ }
336+
337+ public List <WifiConfiguration > getNetworkConfigurations () throws Exception {
260338 return networks ;
261339 }
340+
341+ public HashMap <String , DhcpInfo > getSsidToDhcpInfoHashMap () {
342+ return ssidToDhcpInfoHM ;
343+ }
262344}
0 commit comments