Core library changes to include wifi type. One measly test included as well.
authorvikrama <vikrama@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Thu, 10 Jun 2010 03:49:18 +0000 (03:49 +0000)
committervikrama <vikrama@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Thu, 10 Jun 2010 03:49:18 +0000 (03:49 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@1421 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/client/result/ParsedResultType.java
core/src/com/google/zxing/client/result/ResultParser.java
core/src/com/google/zxing/client/result/WifiParsedResult.java [new file with mode: 0644]
core/src/com/google/zxing/client/result/WifiResultParser.java [new file with mode: 0644]
core/test/src/com/google/zxing/client/result/WifiParsedResultTestCase.java [new file with mode: 0644]

index 36705ef..92c81cb 100644 (file)
@@ -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.
@@ -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");
index f1b3476..df30865 100644 (file)
@@ -63,6 +63,8 @@ public abstract class ResultParser {
       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) {
diff --git a/core/src/com/google/zxing/client/result/WifiParsedResult.java b/core/src/com/google/zxing/client/result/WifiParsedResult.java
new file mode 100644 (file)
index 0000000..21fa03e
--- /dev/null
@@ -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();
+  }
+}
\ No newline at end of file
diff --git a/core/src/com/google/zxing/client/result/WifiResultParser.java b/core/src/com/google/zxing/client/result/WifiResultParser.java
new file mode 100644 (file)
index 0000000..32bf856
--- /dev/null
@@ -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);
+  }
+}
\ No newline at end of file
diff --git a/core/test/src/com/google/zxing/client/result/WifiParsedResultTestCase.java b/core/test/src/com/google/zxing/client/result/WifiParsedResultTestCase.java
new file mode 100644 (file)
index 0000000..1e954dc
--- /dev/null
@@ -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());
+  }
+}
\ No newline at end of file