mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Style bits on wi-fi code
git-svn-id: https://zxing.googlecode.com/svn/trunk@1476 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
426636cea4
commit
04a62c6879
|
@ -27,7 +27,8 @@ import com.google.zxing.client.result.WifiParsedResult;
|
||||||
* @author viki@google.com (Vikram Aggarwal)
|
* @author viki@google.com (Vikram Aggarwal)
|
||||||
*/
|
*/
|
||||||
public final class WifiResultHandler extends ResultHandler {
|
public final class WifiResultHandler extends ResultHandler {
|
||||||
final Activity parent;
|
|
||||||
|
private final Activity parent;
|
||||||
|
|
||||||
public WifiResultHandler(Activity activity, ParsedResult result) {
|
public WifiResultHandler(Activity activity, ParsedResult result) {
|
||||||
super(activity, result);
|
super(activity, result);
|
||||||
|
@ -51,7 +52,7 @@ public final class WifiResultHandler extends ResultHandler {
|
||||||
@Override
|
@Override
|
||||||
public void handleButtonPress(int index) {
|
public void handleButtonPress(int index) {
|
||||||
// Get the underlying wifi config
|
// Get the underlying wifi config
|
||||||
final WifiParsedResult wifiResult = (WifiParsedResult) getResult();
|
WifiParsedResult wifiResult = (WifiParsedResult) getResult();
|
||||||
if (index == 0) {
|
if (index == 0) {
|
||||||
wifiConnect(wifiResult);
|
wifiConnect(wifiResult);
|
||||||
}
|
}
|
||||||
|
@ -60,12 +61,12 @@ public final class WifiResultHandler extends ResultHandler {
|
||||||
// Display the name of the network and the network type to the user.
|
// Display the name of the network and the network type to the user.
|
||||||
@Override
|
@Override
|
||||||
public CharSequence getDisplayContents() {
|
public CharSequence getDisplayContents() {
|
||||||
final WifiParsedResult wifiResult = (WifiParsedResult) getResult();
|
WifiParsedResult wifiResult = (WifiParsedResult) getResult();
|
||||||
StringBuffer contents = new StringBuffer();
|
StringBuffer contents = new StringBuffer();
|
||||||
final String wifiLabel = parent.getString(R.string.wifi_ssid_label);
|
String wifiLabel = parent.getString(R.string.wifi_ssid_label);
|
||||||
ParsedResult.maybeAppend(wifiLabel + "\n" + wifiResult.getSsid(), contents);
|
ParsedResult.maybeAppend(wifiLabel + '\n' + wifiResult.getSsid(), contents);
|
||||||
final String typeLabel = parent.getString(R.string.wifi_type_label);
|
String typeLabel = parent.getString(R.string.wifi_type_label);
|
||||||
ParsedResult.maybeAppend(typeLabel + "\n" + wifiResult.getNetworkEncryption(), contents);
|
ParsedResult.maybeAppend(typeLabel + '\n' + wifiResult.getNetworkEncryption(), contents);
|
||||||
return contents.toString();
|
return contents.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
package com.google.zxing.client.android.wifi;
|
package com.google.zxing.client.android.wifi;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,6 +30,8 @@ import android.text.TextUtils;
|
||||||
*/
|
*/
|
||||||
final class NetworkUtil {
|
final class NetworkUtil {
|
||||||
|
|
||||||
|
private static final Pattern HEX_DIGITS = Pattern.compile("[0-9A-Fa-f]+");
|
||||||
|
|
||||||
private NetworkUtil() {
|
private NetworkUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,26 +47,13 @@ final class NetworkUtil {
|
||||||
if (TextUtils.isEmpty(string)) {
|
if (TextUtils.isEmpty(string)) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
final int lastPos = string.length() - 1;
|
int lastPos = string.length() - 1;
|
||||||
if (lastPos < 0 || (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
|
if (lastPos < 0 || (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
return '\"' + string + '\"';
|
return '\"' + string + '\"';
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isHex(CharSequence key) {
|
|
||||||
if (key == null){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for (int i = key.length() - 1; i >= 0; i--) {
|
|
||||||
final char c = key.charAt(i);
|
|
||||||
if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f')) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if wepKey is a valid hexadecimal string.
|
* Check if wepKey is a valid hexadecimal string.
|
||||||
* @param wepKey the input to be checked
|
* @param wepKey the input to be checked
|
||||||
|
@ -72,9 +63,9 @@ final class NetworkUtil {
|
||||||
if (wepKey == null) {
|
if (wepKey == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
final int length = wepKey.length();
|
int length = wepKey.length();
|
||||||
// WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?)
|
// WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?)
|
||||||
return (length == 10 || length == 26 || length == 58) && isHex(wepKey);
|
return (length == 10 || length == 26 || length == 58) && HEX_DIGITS.matcher(wepKey).matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -17,6 +17,7 @@
|
||||||
package com.google.zxing.client.android.wifi;
|
package com.google.zxing.client.android.wifi;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
@ -36,9 +37,14 @@ import com.google.zxing.client.android.R;
|
||||||
*
|
*
|
||||||
* @author Vikram Aggarwal
|
* @author Vikram Aggarwal
|
||||||
*/
|
*/
|
||||||
public class WifiActivity extends Activity {
|
public final class WifiActivity extends Activity {
|
||||||
|
|
||||||
private static final String TAG = WifiActivity.class.getSimpleName();
|
private static final String TAG = WifiActivity.class.getSimpleName();
|
||||||
|
|
||||||
|
private static final int MAX_ERROR_COUNT = 3;
|
||||||
|
private static final int FAILURE_NO_NETWORK_ID = -1;
|
||||||
|
private static final Pattern HEX_DIGITS_64 = Pattern.compile("[0-9A-Fa-f]{64}");
|
||||||
|
|
||||||
private WifiManager wifiManager;
|
private WifiManager wifiManager;
|
||||||
private TextView statusView;
|
private TextView statusView;
|
||||||
private WifiReceiver wifiReceiver;
|
private WifiReceiver wifiReceiver;
|
||||||
|
@ -47,11 +53,10 @@ public class WifiActivity extends Activity {
|
||||||
private int errorCount;
|
private int errorCount;
|
||||||
private IntentFilter mWifiStateFilter;
|
private IntentFilter mWifiStateFilter;
|
||||||
|
|
||||||
public void gotError(){
|
void gotError() {
|
||||||
final int maxErrorCount = 3;
|
|
||||||
errorCount++;
|
errorCount++;
|
||||||
Log.d(TAG, "Encountered another error. Errorcount = " + errorCount);
|
Log.d(TAG, "Encountered another error. Errorcount = " + errorCount);
|
||||||
if (errorCount > maxErrorCount){
|
if (errorCount > MAX_ERROR_COUNT){
|
||||||
errorCount = 0;
|
errorCount = 0;
|
||||||
doError(R.string.wifi_connect_failed);
|
doError(R.string.wifi_connect_failed);
|
||||||
}
|
}
|
||||||
|
@ -102,7 +107,7 @@ public class WifiActivity extends Activity {
|
||||||
private WifiConfiguration changeNetworkCommon(NetworkSetting input){
|
private WifiConfiguration changeNetworkCommon(NetworkSetting input){
|
||||||
statusView.setText(R.string.wifi_creating_network);
|
statusView.setText(R.string.wifi_creating_network);
|
||||||
Log.d(TAG, "Adding new configuration: \nSSID: " + input.getSsid() + "\nType: " + input.getNetworkType());
|
Log.d(TAG, "Adding new configuration: \nSSID: " + input.getSsid() + "\nType: " + input.getNetworkType());
|
||||||
final WifiConfiguration config = new WifiConfiguration();
|
WifiConfiguration config = new WifiConfiguration();
|
||||||
|
|
||||||
config.allowedAuthAlgorithms.clear();
|
config.allowedAuthAlgorithms.clear();
|
||||||
config.allowedGroupCiphers.clear();
|
config.allowedGroupCiphers.clear();
|
||||||
|
@ -123,8 +128,8 @@ public class WifiActivity extends Activity {
|
||||||
|
|
||||||
// Adding a WEP network
|
// Adding a WEP network
|
||||||
private int changeNetworkWEP(NetworkSetting input) {
|
private int changeNetworkWEP(NetworkSetting input) {
|
||||||
final WifiConfiguration config = changeNetworkCommon(input);
|
WifiConfiguration config = changeNetworkCommon(input);
|
||||||
final String pass = input.getPassword();
|
String pass = input.getPassword();
|
||||||
if (NetworkUtil.isHexWepKey(pass)) {
|
if (NetworkUtil.isHexWepKey(pass)) {
|
||||||
config.wepKeys[0] = pass;
|
config.wepKeys[0] = pass;
|
||||||
} else {
|
} else {
|
||||||
|
@ -142,10 +147,10 @@ public class WifiActivity extends Activity {
|
||||||
|
|
||||||
// Adding a WPA or WPA2 network
|
// Adding a WPA or WPA2 network
|
||||||
private int changeNetworkWPA(NetworkSetting input) {
|
private int changeNetworkWPA(NetworkSetting input) {
|
||||||
final WifiConfiguration config = changeNetworkCommon(input);
|
WifiConfiguration config = changeNetworkCommon(input);
|
||||||
final String pass = input.getPassword();
|
String pass = input.getPassword();
|
||||||
// Hex passwords that are 64 bits long are not to be quoted.
|
// Hex passwords that are 64 bits long are not to be quoted.
|
||||||
if (pass.matches("[0-9A-Fa-f]{64}")){
|
if (HEX_DIGITS_64.matcher(pass).matches()){
|
||||||
Log.d(TAG, "A 64 bit hex password entered.");
|
Log.d(TAG, "A 64 bit hex password entered.");
|
||||||
config.preSharedKey = pass;
|
config.preSharedKey = pass;
|
||||||
} else {
|
} else {
|
||||||
|
@ -178,8 +183,8 @@ public class WifiActivity extends Activity {
|
||||||
* @param ssid
|
* @param ssid
|
||||||
*/
|
*/
|
||||||
private WifiConfiguration findNetworkInExistingConfig(String ssid){
|
private WifiConfiguration findNetworkInExistingConfig(String ssid){
|
||||||
final List <WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
|
List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
|
||||||
for (final WifiConfiguration existingConfig : existingConfigs) {
|
for (WifiConfiguration existingConfig : existingConfigs) {
|
||||||
if (existingConfig.SSID.equals(ssid)) {
|
if (existingConfig.SSID.equals(ssid)) {
|
||||||
return existingConfig;
|
return existingConfig;
|
||||||
}
|
}
|
||||||
|
@ -191,15 +196,15 @@ public class WifiActivity extends Activity {
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
final Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
if (intent == null || (!intent.getAction().equals(Intents.WifiConnect.ACTION))) {
|
if (intent == null || (!intent.getAction().equals(Intents.WifiConnect.ACTION))) {
|
||||||
finish();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String ssid = intent.getStringExtra(Intents.WifiConnect.SSID);
|
String ssid = intent.getStringExtra(Intents.WifiConnect.SSID);
|
||||||
String password = intent.getStringExtra(Intents.WifiConnect.PASSWORD);
|
String password = intent.getStringExtra(Intents.WifiConnect.PASSWORD);
|
||||||
final String networkType = intent.getStringExtra(Intents.WifiConnect.TYPE);
|
String networkType = intent.getStringExtra(Intents.WifiConnect.TYPE);
|
||||||
setContentView(R.layout.network);
|
setContentView(R.layout.network);
|
||||||
statusView = (TextView) findViewById(R.id.networkStatus);
|
statusView = (TextView) findViewById(R.id.networkStatus);
|
||||||
|
|
||||||
|
@ -259,7 +264,6 @@ public class WifiActivity extends Activity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
|
||||||
if (wifiReceiver != null) {
|
if (wifiReceiver != null) {
|
||||||
if (receiverRegistered) {
|
if (receiverRegistered) {
|
||||||
unregisterReceiver(wifiReceiver);
|
unregisterReceiver(wifiReceiver);
|
||||||
|
@ -277,7 +281,6 @@ public class WifiActivity extends Activity {
|
||||||
* @return network ID of the connected network.
|
* @return network ID of the connected network.
|
||||||
*/
|
*/
|
||||||
private int updateNetwork(WifiConfiguration config, boolean disableOthers) {
|
private int updateNetwork(WifiConfiguration config, boolean disableOthers) {
|
||||||
final int FAILURE = -1;
|
|
||||||
WifiConfiguration found = findNetworkInExistingConfig(config.SSID);
|
WifiConfiguration found = findNetworkInExistingConfig(config.SSID);
|
||||||
wifiManager.disconnect();
|
wifiManager.disconnect();
|
||||||
if (found == null) {
|
if (found == null) {
|
||||||
|
@ -290,16 +293,18 @@ public class WifiActivity extends Activity {
|
||||||
}
|
}
|
||||||
networkId = wifiManager.addNetwork(config);
|
networkId = wifiManager.addNetwork(config);
|
||||||
Log.d(TAG, "Inserted/Modified network " + networkId);
|
Log.d(TAG, "Inserted/Modified network " + networkId);
|
||||||
if (networkId < 0)
|
if (networkId < 0) {
|
||||||
return FAILURE;
|
return FAILURE_NO_NETWORK_ID;
|
||||||
|
}
|
||||||
|
|
||||||
// Try to disable the current network and start a new one.
|
// Try to disable the current network and start a new one.
|
||||||
if (!wifiManager.enableNetwork(networkId, disableOthers)) {
|
if (!wifiManager.enableNetwork(networkId, disableOthers)) {
|
||||||
networkId = -1;
|
networkId = FAILURE_NO_NETWORK_ID;
|
||||||
return FAILURE;
|
return FAILURE_NO_NETWORK_ID;
|
||||||
}
|
}
|
||||||
errorCount = 0;
|
errorCount = 0;
|
||||||
wifiManager.reassociate();
|
wifiManager.reassociate();
|
||||||
return networkId;
|
return networkId;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
package com.google.zxing.client.android.wifi;
|
package com.google.zxing.client.android.wifi;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
@ -33,12 +32,14 @@ import com.google.zxing.client.android.R;
|
||||||
* Get a broadcast when the network is connected, and kill the activity.
|
* Get a broadcast when the network is connected, and kill the activity.
|
||||||
*/
|
*/
|
||||||
final class WifiReceiver extends BroadcastReceiver {
|
final class WifiReceiver extends BroadcastReceiver {
|
||||||
private final String TAG = "WifiReceiver";
|
|
||||||
|
private static final String TAG = WifiReceiver.class.getSimpleName();
|
||||||
|
|
||||||
private final WifiManager mWifiManager;
|
private final WifiManager mWifiManager;
|
||||||
private final Activity parent;
|
private final WifiActivity parent;
|
||||||
private final TextView statusView;
|
private final TextView statusView;
|
||||||
|
|
||||||
WifiReceiver(WifiManager wifiManager, Activity wifiActivity, TextView statusView, String ssid) {
|
WifiReceiver(WifiManager wifiManager, WifiActivity wifiActivity, TextView statusView, String ssid) {
|
||||||
this.parent = wifiActivity;
|
this.parent = wifiActivity;
|
||||||
this.statusView = statusView;
|
this.statusView = statusView;
|
||||||
this.mWifiManager = wifiManager;
|
this.mWifiManager = wifiManager;
|
||||||
|
@ -47,28 +48,28 @@ final class WifiReceiver extends BroadcastReceiver {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
|
if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
|
||||||
handleChange((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE),
|
handleChange(
|
||||||
intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR),
|
(SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE),
|
||||||
intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 0));
|
intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR));
|
||||||
} else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
|
} else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
|
||||||
handleNetworkStateChanged((NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO));
|
handleNetworkStateChanged((NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO));
|
||||||
} else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
|
} else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
|
||||||
final ConnectivityManager con = (ConnectivityManager) parent.getSystemService(Context.CONNECTIVITY_SERVICE);
|
ConnectivityManager con = (ConnectivityManager) parent.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
final NetworkInfo[] s = con.getAllNetworkInfo();
|
NetworkInfo[] s = con.getAllNetworkInfo();
|
||||||
for (final NetworkInfo i : s){
|
for (NetworkInfo i : s){
|
||||||
if (i.getTypeName().contentEquals("WIFI")){
|
if (i.getTypeName().contentEquals("WIFI")){
|
||||||
final NetworkInfo.State state = i.getState();
|
NetworkInfo.State state = i.getState();
|
||||||
final String ssid = mWifiManager.getConnectionInfo().getSSID();
|
String ssid = mWifiManager.getConnectionInfo().getSSID();
|
||||||
|
|
||||||
if (state == NetworkInfo.State.CONNECTED && ssid != null){
|
if (state == NetworkInfo.State.CONNECTED && ssid != null){
|
||||||
mWifiManager.saveConfiguration();
|
mWifiManager.saveConfiguration();
|
||||||
final String label = parent.getString(R.string.wifi_connected);
|
String label = parent.getString(R.string.wifi_connected);
|
||||||
statusView.setText(label + "\n" + ssid);
|
statusView.setText(label + '\n' + ssid);
|
||||||
Runnable delayKill = new Killer(parent);
|
Runnable delayKill = new Killer(parent);
|
||||||
delayKill.run();
|
delayKill.run();
|
||||||
}
|
}
|
||||||
if (state == NetworkInfo.State.DISCONNECTED){
|
if (state == NetworkInfo.State.DISCONNECTED){
|
||||||
Log.d(TAG, "Got state: " + state.toString() + " for ssid: " + ssid);
|
Log.d(TAG, "Got state: " + state + " for ssid: " + ssid);
|
||||||
((WifiActivity)parent).gotError();
|
((WifiActivity)parent).gotError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,17 +78,17 @@ final class WifiReceiver extends BroadcastReceiver {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleNetworkStateChanged(NetworkInfo networkInfo) {
|
private void handleNetworkStateChanged(NetworkInfo networkInfo) {
|
||||||
final NetworkInfo.DetailedState state = networkInfo.getDetailedState();
|
NetworkInfo.DetailedState state = networkInfo.getDetailedState();
|
||||||
if (state == NetworkInfo.DetailedState.FAILED){
|
if (state == NetworkInfo.DetailedState.FAILED){
|
||||||
Log.d(TAG, "Detailed Network state failed");
|
Log.d(TAG, "Detailed Network state failed");
|
||||||
((WifiActivity)parent).gotError();
|
parent.gotError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleChange(SupplicantState state, boolean hasError, int error) {
|
private void handleChange(SupplicantState state, boolean hasError) {
|
||||||
if (hasError || state == SupplicantState.INACTIVE){
|
if (hasError || state == SupplicantState.INACTIVE){
|
||||||
Log.d(TAG, "Found an error");
|
Log.d(TAG, "Found an error");
|
||||||
((WifiActivity)parent).gotError();
|
parent.gotError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue