Issue 1218 add possible support for hidden SSIDs

git-svn-id: https://zxing.googlecode.com/svn/trunk@2371 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2012-08-06 12:07:52 +00:00
parent 1acddf28a7
commit e4803b8799
4 changed files with 45 additions and 35 deletions

View file

@ -28,7 +28,8 @@ import com.google.zxing.client.result.WifiParsedResult;
/** /**
* Handles address book entries. * Handles address book entries.
* *
* @author viki@google.com (Vikram Aggarwal) * @author Vikram Aggarwal
* @author Sean Owen
*/ */
public final class WifiResultHandler extends ResultHandler { public final class WifiResultHandler extends ResultHandler {
@ -52,15 +53,11 @@ public final class WifiResultHandler extends ResultHandler {
@Override @Override
public void handleButtonPress(int index) { public void handleButtonPress(int index) {
// Get the underlying wifi config
WifiParsedResult wifiResult = (WifiParsedResult) getResult();
if (index == 0) { if (index == 0) {
String ssid = wifiResult.getSsid(); WifiParsedResult wifiResult = (WifiParsedResult) getResult();
String password = wifiResult.getPassword();
String networkType = wifiResult.getNetworkEncryption();
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE); WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
Toast.makeText(getActivity(), R.string.wifi_changing_network, Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(), R.string.wifi_changing_network, Toast.LENGTH_LONG).show();
WifiConfigManager.configure(wifiManager, ssid, password, networkType); WifiConfigManager.configure(wifiManager, wifiResult);
parent.restartPreviewAfterDelay(0L); parent.restartPreviewAfterDelay(0L);
} }
} }

View file

@ -23,6 +23,8 @@ import android.util.Log;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.google.zxing.client.result.WifiParsedResult;
/** /**
* @author Vikram Aggarwal * @author Vikram Aggarwal
* @author Sean Owen * @author Sean Owen
@ -36,10 +38,7 @@ public final class WifiConfigManager {
private WifiConfigManager() { private WifiConfigManager() {
} }
public static void configure(final WifiManager wifiManager, public static void configure(final WifiManager wifiManager, final WifiParsedResult wifiResult) {
final String ssid,
final String password,
final String networkTypeString) {
Runnable configureRunnable = new Runnable() { Runnable configureRunnable = new Runnable() {
@Override @Override
public void run() { public void run() {
@ -68,6 +67,7 @@ public final class WifiConfigManager {
count++; count++;
} }
} }
String networkTypeString = wifiResult.getNetworkEncryption();
NetworkType networkType; NetworkType networkType;
try { try {
networkType = NetworkType.forIntentValue(networkTypeString); networkType = NetworkType.forIntentValue(networkTypeString);
@ -76,15 +76,16 @@ public final class WifiConfigManager {
return; return;
} }
if (networkType == NetworkType.NO_PASSWORD) { if (networkType == NetworkType.NO_PASSWORD) {
changeNetworkUnEncrypted(wifiManager, ssid); changeNetworkUnEncrypted(wifiManager, wifiResult);
} else { } else {
String password = wifiResult.getPassword();
if (password == null || password.length() == 0) { if (password == null || password.length() == 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
if (networkType == NetworkType.WEP) { if (networkType == NetworkType.WEP) {
changeNetworkWEP(wifiManager, ssid, password); changeNetworkWEP(wifiManager, wifiResult);
} else if (networkType == NetworkType.WPA) { } else if (networkType == NetworkType.WPA) {
changeNetworkWPA(wifiManager, ssid, password); changeNetworkWPA(wifiManager, wifiResult);
} }
} }
} }
@ -118,7 +119,7 @@ public final class WifiConfigManager {
} }
} }
private static WifiConfiguration changeNetworkCommon(String ssid) { private static WifiConfiguration changeNetworkCommon(WifiParsedResult wifiResult) {
WifiConfiguration config = new WifiConfiguration(); WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear(); config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear(); config.allowedGroupCiphers.clear();
@ -126,14 +127,15 @@ public final class WifiConfigManager {
config.allowedPairwiseCiphers.clear(); config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear(); config.allowedProtocols.clear();
// Android API insists that an ascii SSID must be quoted to be correctly handled. // Android API insists that an ascii SSID must be quoted to be correctly handled.
config.SSID = quoteNonHex(ssid); config.SSID = quoteNonHex(wifiResult.getSsid());
config.hiddenSSID = wifiResult.isHidden();
return config; return config;
} }
// Adding a WEP network // Adding a WEP network
private static void changeNetworkWEP(WifiManager wifiManager, String ssid, String password) { private static void changeNetworkWEP(WifiManager wifiManager, WifiParsedResult wifiResult) {
WifiConfiguration config = changeNetworkCommon(ssid); WifiConfiguration config = changeNetworkCommon(wifiResult);
config.wepKeys[0] = quoteNonHex(password, 10, 26, 58); config.wepKeys[0] = quoteNonHex(wifiResult.getPassword(), 10, 26, 58);
config.wepTxKeyIndex = 0; config.wepTxKeyIndex = 0;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
@ -145,10 +147,10 @@ public final class WifiConfigManager {
} }
// Adding a WPA or WPA2 network // Adding a WPA or WPA2 network
private static void changeNetworkWPA(WifiManager wifiManager, String ssid, String password) { private static void changeNetworkWPA(WifiManager wifiManager, WifiParsedResult wifiResult) {
WifiConfiguration config = changeNetworkCommon(ssid); WifiConfiguration config = changeNetworkCommon(wifiResult);
// Hex passwords that are 64 bits long are not to be quoted. // Hex passwords that are 64 bits long are not to be quoted.
config.preSharedKey = quoteNonHex(password, 64); config.preSharedKey = quoteNonHex(wifiResult.getPassword(), 64);
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2 config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
@ -162,8 +164,8 @@ public final class WifiConfigManager {
} }
// Adding an open, unsecured network // Adding an open, unsecured network
private static void changeNetworkUnEncrypted(WifiManager wifiManager, String ssid) { private static void changeNetworkUnEncrypted(WifiManager wifiManager, WifiParsedResult wifiResult) {
WifiConfiguration config = changeNetworkCommon(ssid); WifiConfiguration config = changeNetworkCommon(wifiResult);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
updateNetwork(wifiManager, config); updateNetwork(wifiManager, config);
} }

View file

@ -24,12 +24,18 @@ public final class WifiParsedResult extends ParsedResult {
private final String ssid; private final String ssid;
private final String networkEncryption; private final String networkEncryption;
private final String password; private final String password;
private final boolean hidden;
public WifiParsedResult(String networkEncryption, String ssid, String password) { public WifiParsedResult(String networkEncryption, String ssid, String password) {
this(networkEncryption, ssid, password, false);
}
public WifiParsedResult(String networkEncryption, String ssid, String password, boolean hidden) {
super(ParsedResultType.WIFI); super(ParsedResultType.WIFI);
this.ssid = ssid; this.ssid = ssid;
this.networkEncryption = networkEncryption; this.networkEncryption = networkEncryption;
this.password = password; this.password = password;
this.hidden = hidden;
} }
public String getSsid() { public String getSsid() {
@ -44,12 +50,18 @@ public final class WifiParsedResult extends ParsedResult {
return password; return password;
} }
public boolean isHidden() {
return hidden;
}
@Override @Override
public String getDisplayResult() { public String getDisplayResult() {
StringBuilder result = new StringBuilder(80); StringBuilder result = new StringBuilder(80);
maybeAppend(ssid, result); maybeAppend(ssid, result);
maybeAppend(networkEncryption, result); maybeAppend(networkEncryption, result);
maybeAppend(password, result); maybeAppend(password, result);
maybeAppend(Boolean.toString(hidden), result);
return result.toString(); return result.toString();
} }
} }

View file

@ -19,13 +19,14 @@ package com.google.zxing.client.result;
import com.google.zxing.Result; import com.google.zxing.Result;
/** /**
* Parses a WIFI configuration string. Strings will be of the form: * <p>Parses a WIFI configuration string. Strings will be of the form:</p>
* WIFI:T:WPA;S:mynetwork;P:mypass;;
* *
* The fields can come in any order, and there should be tests to see * <p>{@code WIFI:T:[network type];S:[network SSID];P:[network password];H:[hidden?];;}</p>
* if we can parse them all correctly. *
* <p>The fields can appear in any order. Only "S:" is required.</p>
* *
* @author Vikram Aggarwal * @author Vikram Aggarwal
* @author Sean Owen
*/ */
public final class WifiResultParser extends ResultParser { public final class WifiResultParser extends ResultParser {
@ -35,18 +36,16 @@ public final class WifiResultParser extends ResultParser {
if (!rawText.startsWith("WIFI:")) { if (!rawText.startsWith("WIFI:")) {
return null; return null;
} }
// Don't remove leading or trailing whitespace String ssid = matchSinglePrefixedField("S:", rawText, ';', false);
boolean trim = false;
String ssid = matchSinglePrefixedField("S:", rawText, ';', trim);
if (ssid == null || ssid.length() == 0) { if (ssid == null || ssid.length() == 0) {
return null; return null;
} }
String pass = matchSinglePrefixedField("P:", rawText, ';', trim); String pass = matchSinglePrefixedField("P:", rawText, ';', false);
String type = matchSinglePrefixedField("T:", rawText, ';', trim); String type = matchSinglePrefixedField("T:", rawText, ';', false);
if (type == null) { if (type == null) {
type = "nopass"; type = "nopass";
} }
boolean hidden = Boolean.parseBoolean(matchSinglePrefixedField("B:", rawText, ';', false));
return new WifiParsedResult(type, ssid, pass); return new WifiParsedResult(type, ssid, pass, hidden);
} }
} }