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)
|
||||
*/
|
||||
public final class WifiResultHandler extends ResultHandler {
|
||||
final Activity parent;
|
||||
|
||||
private final Activity parent;
|
||||
|
||||
public WifiResultHandler(Activity activity, ParsedResult result) {
|
||||
super(activity, result);
|
||||
|
@ -51,7 +52,7 @@ public final class WifiResultHandler extends ResultHandler {
|
|||
@Override
|
||||
public void handleButtonPress(int index) {
|
||||
// Get the underlying wifi config
|
||||
final WifiParsedResult wifiResult = (WifiParsedResult) getResult();
|
||||
WifiParsedResult wifiResult = (WifiParsedResult) getResult();
|
||||
if (index == 0) {
|
||||
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.
|
||||
@Override
|
||||
public CharSequence getDisplayContents() {
|
||||
final WifiParsedResult wifiResult = (WifiParsedResult) getResult();
|
||||
WifiParsedResult wifiResult = (WifiParsedResult) getResult();
|
||||
StringBuffer contents = new StringBuffer();
|
||||
final String wifiLabel = parent.getString(R.string.wifi_ssid_label);
|
||||
ParsedResult.maybeAppend(wifiLabel + "\n" + wifiResult.getSsid(), contents);
|
||||
final String typeLabel = parent.getString(R.string.wifi_type_label);
|
||||
ParsedResult.maybeAppend(typeLabel + "\n" + wifiResult.getNetworkEncryption(), contents);
|
||||
String wifiLabel = parent.getString(R.string.wifi_ssid_label);
|
||||
ParsedResult.maybeAppend(wifiLabel + '\n' + wifiResult.getSsid(), contents);
|
||||
String typeLabel = parent.getString(R.string.wifi_type_label);
|
||||
ParsedResult.maybeAppend(typeLabel + '\n' + wifiResult.getNetworkEncryption(), contents);
|
||||
return contents.toString();
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package com.google.zxing.client.android.wifi;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
/**
|
||||
|
@ -28,6 +30,8 @@ import android.text.TextUtils;
|
|||
*/
|
||||
final class NetworkUtil {
|
||||
|
||||
private static final Pattern HEX_DIGITS = Pattern.compile("[0-9A-Fa-f]+");
|
||||
|
||||
private NetworkUtil() {
|
||||
}
|
||||
|
||||
|
@ -43,26 +47,13 @@ final class NetworkUtil {
|
|||
if (TextUtils.isEmpty(string)) {
|
||||
return "";
|
||||
}
|
||||
final int lastPos = string.length() - 1;
|
||||
int lastPos = string.length() - 1;
|
||||
if (lastPos < 0 || (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
|
||||
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.
|
||||
* @param wepKey the input to be checked
|
||||
|
@ -72,9 +63,9 @@ final class NetworkUtil {
|
|||
if (wepKey == null) {
|
||||
return false;
|
||||
}
|
||||
final int length = wepKey.length();
|
||||
int length = wepKey.length();
|
||||
// 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;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
|
@ -36,9 +37,14 @@ import com.google.zxing.client.android.R;
|
|||
*
|
||||
* @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 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 TextView statusView;
|
||||
private WifiReceiver wifiReceiver;
|
||||
|
@ -47,11 +53,10 @@ public class WifiActivity extends Activity {
|
|||
private int errorCount;
|
||||
private IntentFilter mWifiStateFilter;
|
||||
|
||||
public void gotError(){
|
||||
final int maxErrorCount = 3;
|
||||
void gotError() {
|
||||
errorCount++;
|
||||
Log.d(TAG, "Encountered another error. Errorcount = " + errorCount);
|
||||
if (errorCount > maxErrorCount){
|
||||
if (errorCount > MAX_ERROR_COUNT){
|
||||
errorCount = 0;
|
||||
doError(R.string.wifi_connect_failed);
|
||||
}
|
||||
|
@ -102,7 +107,7 @@ public class WifiActivity extends Activity {
|
|||
private WifiConfiguration changeNetworkCommon(NetworkSetting input){
|
||||
statusView.setText(R.string.wifi_creating_network);
|
||||
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.allowedGroupCiphers.clear();
|
||||
|
@ -123,8 +128,8 @@ public class WifiActivity extends Activity {
|
|||
|
||||
// Adding a WEP network
|
||||
private int changeNetworkWEP(NetworkSetting input) {
|
||||
final WifiConfiguration config = changeNetworkCommon(input);
|
||||
final String pass = input.getPassword();
|
||||
WifiConfiguration config = changeNetworkCommon(input);
|
||||
String pass = input.getPassword();
|
||||
if (NetworkUtil.isHexWepKey(pass)) {
|
||||
config.wepKeys[0] = pass;
|
||||
} else {
|
||||
|
@ -142,10 +147,10 @@ public class WifiActivity extends Activity {
|
|||
|
||||
// Adding a WPA or WPA2 network
|
||||
private int changeNetworkWPA(NetworkSetting input) {
|
||||
final WifiConfiguration config = changeNetworkCommon(input);
|
||||
final String pass = input.getPassword();
|
||||
WifiConfiguration config = changeNetworkCommon(input);
|
||||
String pass = input.getPassword();
|
||||
// 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.");
|
||||
config.preSharedKey = pass;
|
||||
} else {
|
||||
|
@ -178,8 +183,8 @@ public class WifiActivity extends Activity {
|
|||
* @param ssid
|
||||
*/
|
||||
private WifiConfiguration findNetworkInExistingConfig(String ssid){
|
||||
final List <WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
|
||||
for (final WifiConfiguration existingConfig : existingConfigs) {
|
||||
List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
|
||||
for (WifiConfiguration existingConfig : existingConfigs) {
|
||||
if (existingConfig.SSID.equals(ssid)) {
|
||||
return existingConfig;
|
||||
}
|
||||
|
@ -191,15 +196,15 @@ public class WifiActivity extends Activity {
|
|||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
final Intent intent = getIntent();
|
||||
Intent intent = getIntent();
|
||||
if (intent == null || (!intent.getAction().equals(Intents.WifiConnect.ACTION))) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
final String ssid = intent.getStringExtra(Intents.WifiConnect.SSID);
|
||||
String ssid = intent.getStringExtra(Intents.WifiConnect.SSID);
|
||||
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);
|
||||
statusView = (TextView) findViewById(R.id.networkStatus);
|
||||
|
||||
|
@ -259,11 +264,10 @@ public class WifiActivity extends Activity {
|
|||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (wifiReceiver != null) {
|
||||
if (receiverRegistered) {
|
||||
unregisterReceiver(wifiReceiver);
|
||||
receiverRegistered = false;
|
||||
unregisterReceiver(wifiReceiver);
|
||||
receiverRegistered = false;
|
||||
}
|
||||
wifiReceiver = null;
|
||||
}
|
||||
|
@ -276,30 +280,31 @@ public class WifiActivity extends Activity {
|
|||
* @param disableOthers true if other networks must be disabled
|
||||
* @return network ID of the connected network.
|
||||
*/
|
||||
private int updateNetwork(WifiConfiguration config, boolean disableOthers){
|
||||
final int FAILURE = -1;
|
||||
private int updateNetwork(WifiConfiguration config, boolean disableOthers) {
|
||||
WifiConfiguration found = findNetworkInExistingConfig(config.SSID);
|
||||
wifiManager.disconnect();
|
||||
if (found == null){
|
||||
if (found == null) {
|
||||
statusView.setText(R.string.wifi_creating_network);
|
||||
} else {
|
||||
statusView.setText(R.string.wifi_modifying_network);
|
||||
Log.d(TAG, "Removing network " + found.networkId);
|
||||
wifiManager.removeNetwork(found.networkId);
|
||||
wifiManager.saveConfiguration();
|
||||
}
|
||||
}
|
||||
networkId = wifiManager.addNetwork(config);
|
||||
Log.d(TAG, "Inserted/Modified network " + networkId);
|
||||
if (networkId < 0)
|
||||
return FAILURE;
|
||||
if (networkId < 0) {
|
||||
return FAILURE_NO_NETWORK_ID;
|
||||
}
|
||||
|
||||
// Try to disable the current network and start a new one.
|
||||
if (!wifiManager.enableNetwork(networkId, disableOthers)) {
|
||||
networkId = -1;
|
||||
return FAILURE;
|
||||
networkId = FAILURE_NO_NETWORK_ID;
|
||||
return FAILURE_NO_NETWORK_ID;
|
||||
}
|
||||
errorCount = 0;
|
||||
wifiManager.reassociate();
|
||||
return networkId;
|
||||
}
|
||||
|
||||
}
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
package com.google.zxing.client.android.wifi;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
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.
|
||||
*/
|
||||
final class WifiReceiver extends BroadcastReceiver {
|
||||
private final String TAG = "WifiReceiver";
|
||||
|
||||
private static final String TAG = WifiReceiver.class.getSimpleName();
|
||||
|
||||
private final WifiManager mWifiManager;
|
||||
private final Activity parent;
|
||||
private final WifiActivity parent;
|
||||
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.statusView = statusView;
|
||||
this.mWifiManager = wifiManager;
|
||||
|
@ -47,28 +48,28 @@ final class WifiReceiver extends BroadcastReceiver {
|
|||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
|
||||
handleChange((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE),
|
||||
intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR),
|
||||
intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 0));
|
||||
handleChange(
|
||||
(SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE),
|
||||
intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR));
|
||||
} else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
|
||||
handleNetworkStateChanged((NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO));
|
||||
} else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
|
||||
final ConnectivityManager con = (ConnectivityManager) parent.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
final NetworkInfo[] s = con.getAllNetworkInfo();
|
||||
for (final NetworkInfo i : s){
|
||||
ConnectivityManager con = (ConnectivityManager) parent.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo[] s = con.getAllNetworkInfo();
|
||||
for (NetworkInfo i : s){
|
||||
if (i.getTypeName().contentEquals("WIFI")){
|
||||
final NetworkInfo.State state = i.getState();
|
||||
final String ssid = mWifiManager.getConnectionInfo().getSSID();
|
||||
NetworkInfo.State state = i.getState();
|
||||
String ssid = mWifiManager.getConnectionInfo().getSSID();
|
||||
|
||||
if (state == NetworkInfo.State.CONNECTED && ssid != null){
|
||||
mWifiManager.saveConfiguration();
|
||||
final String label = parent.getString(R.string.wifi_connected);
|
||||
statusView.setText(label + "\n" + ssid);
|
||||
String label = parent.getString(R.string.wifi_connected);
|
||||
statusView.setText(label + '\n' + ssid);
|
||||
Runnable delayKill = new Killer(parent);
|
||||
delayKill.run();
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -77,17 +78,17 @@ final class WifiReceiver extends BroadcastReceiver {
|
|||
}
|
||||
|
||||
private void handleNetworkStateChanged(NetworkInfo networkInfo) {
|
||||
final NetworkInfo.DetailedState state = networkInfo.getDetailedState();
|
||||
NetworkInfo.DetailedState state = networkInfo.getDetailedState();
|
||||
if (state == NetworkInfo.DetailedState.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){
|
||||
Log.d(TAG, "Found an error");
|
||||
((WifiActivity)parent).gotError();
|
||||
parent.gotError();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue