Core library changes to include wifi type. One measly test included as well.

git-svn-id: https://zxing.googlecode.com/svn/trunk@1421 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
vikrama 2010-06-10 03:49:18 +00:00
parent 69c3af2ef0
commit 58fefb095c
5 changed files with 186 additions and 1 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright 2007 ZXing authors * Copyright 2010 ZXing authors
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,6 +34,7 @@ public final class ParsedResultType {
public static final ParsedResultType TEL = new ParsedResultType("TEL"); public static final ParsedResultType TEL = new ParsedResultType("TEL");
public static final ParsedResultType SMS = new ParsedResultType("SMS"); public static final ParsedResultType SMS = new ParsedResultType("SMS");
public static final ParsedResultType CALENDAR = new ParsedResultType("CALENDAR"); public static final ParsedResultType CALENDAR = new ParsedResultType("CALENDAR");
public static final ParsedResultType WIFI = new ParsedResultType("WIFI");
// "optional" types // "optional" types
public static final ParsedResultType NDEF_SMART_POSTER = new ParsedResultType("NDEF_SMART_POSTER"); public static final ParsedResultType NDEF_SMART_POSTER = new ParsedResultType("NDEF_SMART_POSTER");
public static final ParsedResultType MOBILETAG_RICH_WEB = new ParsedResultType("MOBILETAG_RICH_WEB"); public static final ParsedResultType MOBILETAG_RICH_WEB = new ParsedResultType("MOBILETAG_RICH_WEB");

View file

@ -63,6 +63,8 @@ public abstract class ResultParser {
return result; return result;
} else if ((result = GeoResultParser.parse(theResult)) != null) { } else if ((result = GeoResultParser.parse(theResult)) != null) {
return result; return result;
} else if ((result = WifiResultParser.parse(theResult)) != null) {
return result;
} else if ((result = URLTOResultParser.parse(theResult)) != null) { } else if ((result = URLTOResultParser.parse(theResult)) != null) {
return result; return result;
} else if ((result = URIResultParser.parse(theResult)) != null) { } else if ((result = URIResultParser.parse(theResult)) != null) {

View file

@ -0,0 +1,53 @@
/*
* Copyright 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.result;
/**
* @author Vikram Aggarwal
*/
public final class WifiParsedResult extends ParsedResult {
private final String ssid;
private final String networkEncryption;
private final String password;
public WifiParsedResult(String networkEncryption, String ssid, String password) {
super(ParsedResultType.WIFI);
this.ssid = ssid;
this.networkEncryption = networkEncryption;
this.password = password;
}
public String getSsid() {
return ssid;
}
public String getNetworkEncryption() {
return networkEncryption;
}
public String getPassword() {
return password;
}
public String getDisplayResult() {
StringBuffer result = new StringBuffer(80);
maybeAppend(ssid, result);
maybeAppend(networkEncryption, result);
maybeAppend(password, result);
return result.toString();
}
}

View file

@ -0,0 +1,50 @@
/*
* Copyright 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.result;
import com.google.zxing.Result;
/**
* Parses a WIFI configuration string. Strings will be of the form:
* WIFI:T:WPA;S:mynetwork;P:mypass;;
*
* The fields can come in any order, and there should be tests to see
* if we can parse them all correctly.
*
* @author Vikram Aggarwal
*/
final class WifiResultParser extends ResultParser {
private WifiResultParser() {
}
public static WifiParsedResult parse(Result result) {
String rawText = result.getText();
if (rawText == null || !rawText.startsWith("WIFI:")) {
return null;
}
// Don't remove leading or trailing whitespace
boolean trim = false;
String ssid = matchSinglePrefixedField("S:", rawText, ';', trim);
String pass = matchSinglePrefixedField("P:", rawText, ';', trim);
String type = matchSinglePrefixedField("T:", rawText, ';', trim);
return new WifiParsedResult(type, ssid, pass);
}
}

View file

@ -0,0 +1,79 @@
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.result;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import junit.framework.TestCase;
import java.util.Arrays;
/**
* Tests {@link WifiParsedResult}.
*
* @author Vikram Aggarwal
*/
public final class WifiParsedResultTestCase extends TestCase {
public void testNoPassword() {
doTest("WIFI:S:NoPassword;P:;T:;;", "NoPassword", "", "");
doTest("WIFI:S:No Password;P:;T:;;", "No Password", "", "");
}
public void testWep() {
doTest("WIFI:S:TenChars;P:0123456789;T:WEP;;", "TenChars", "0123456789", "WEP");
doTest("WIFI:S:TenChars;P:abcde56789;T:WEP;;", "TenChars", "abcde56789", "WEP");
// Non hex should not fail at this level
doTest("WIFI:S:TenChars;P:hellothere;T:WEP;;", "TenChars", "hellothere", "WEP");
// Escaped semicolons
doTest("WIFI:S:Ten\\;\\;Chars;P:0123456789;T:WEP;;", "Ten;;Chars", "0123456789", "WEP");
// Escaped colons
doTest("WIFI:S:Ten\\:\\:Chars;P:0123456789;T:WEP;;", "Ten::Chars", "0123456789", "WEP");
// TODO(vikrama) Need a test for SB as well.
}
// Put in checks for the length of the password for wep.
public void testWpa() {
doTest("WIFI:S:TenChars;P:wow;T:WPA;;", "TenChars", "wow", "WPA");
doTest("WIFI:S:TenChars;P:space is silent;T:WPA;;", "TenChars", "space is silent", "WPA");
doTest("WIFI:S:TenChars;P:hellothere;T:WEP;;", "TenChars", "hellothere", "WEP");
// Escaped semicolons
doTest("WIFI:S:TenChars;P:hello\\;there;T:WEP;;", "TenChars", "hello;there", "WEP");
// Escaped colons
doTest("WIFI:S:TenChars;P:hello\\:there;T:WEP;;", "TenChars", "hello:there", "WEP");
}
// Given the string contents for the barcode, check that it matches
// our expectations
private static void doTest(String contents,
String ssid,
String password,
String type) {
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
ParsedResult result = ResultParser.parseResult(fakeResult);
// Ensure it is a wifi code
assertSame(ParsedResultType.WIFI, result.getType());
WifiParsedResult wifiResult = (WifiParsedResult) result;
assertEquals(ssid, wifiResult.getSsid());
assertEquals(password, wifiResult.getPassword());
assertEquals(type, wifiResult.getNetworkEncryption());
}
}