Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Core library changes to include wifi type. One measly test included a…
…s well.
  • Loading branch information
vikrama committed Jun 10, 2010
1 parent 42639ec commit 0f94ff3
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 1 deletion.
@@ -1,5 +1,5 @@
/*
* Copyright 2007 ZXing authors
* 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.
Expand Down Expand Up @@ -34,6 +34,7 @@ public final class ParsedResultType {
public static final ParsedResultType TEL = new ParsedResultType("TEL");
public static final ParsedResultType SMS = new ParsedResultType("SMS");
public static final ParsedResultType CALENDAR = new ParsedResultType("CALENDAR");
public static final ParsedResultType WIFI = new ParsedResultType("WIFI");
// "optional" types
public static final ParsedResultType NDEF_SMART_POSTER = new ParsedResultType("NDEF_SMART_POSTER");
public static final ParsedResultType MOBILETAG_RICH_WEB = new ParsedResultType("MOBILETAG_RICH_WEB");
Expand Down
2 changes: 2 additions & 0 deletions core/src/com/google/zxing/client/result/ResultParser.java
Expand Up @@ -63,6 +63,8 @@ public static ParsedResult parseResult(Result theResult) {
return result;
} else if ((result = GeoResultParser.parse(theResult)) != null) {
return result;
} else if ((result = WifiResultParser.parse(theResult)) != null) {
return result;
} else if ((result = URLTOResultParser.parse(theResult)) != null) {
return result;
} else if ((result = URIResultParser.parse(theResult)) != null) {
Expand Down
53 changes: 53 additions & 0 deletions core/src/com/google/zxing/client/result/WifiParsedResult.java
@@ -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();
}
}
50 changes: 50 additions & 0 deletions core/src/com/google/zxing/client/result/WifiResultParser.java
@@ -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);
}
}
@@ -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());
}
}

0 comments on commit 0f94ff3

Please sign in to comment.