Some error checking put in. When a new network is created and the

connection fails, an alert message is displayed, and connection
attempts cease.  Retrying with a correct barcode works.  Not perfect
yet, but in my testing I have found this version much better than the
previous setup: at least errors are detected and the user is informed.



git-svn-id: https://zxing.googlecode.com/svn/trunk@1470 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
vikrama 2010-07-09 05:49:42 +00:00
parent 983d29eb62
commit 77207acb31
22 changed files with 109 additions and 91 deletions

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">شبكة نوع غير صحيح</string>
<string name="wifi_ssid_label">اسم الشبكة</string>
<string name="wifi_type_label">نوع</string>
<string name="wifi_connect_failed">فشل شبكة الاتصال</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">Typ sítě nesprávné</string>
<string name="wifi_ssid_label">Jméno sítě</string>
<string name="wifi_type_label">Typ</string>
<string name="wifi_connect_failed">Síťové připojení se nezdařilo</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">Netværk type forkert</string>
<string name="wifi_ssid_label">Netværksnavn</string>
<string name="wifi_type_label">Type</string>
<string name="wifi_connect_failed">Netværksforbindelse mislykkedes</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -130,4 +130,5 @@
<string name="wifi_type_incorrect">Netzwerk-Typ nicht korrekt</string>
<string name="wifi_ssid_label">Network Name</string>
<string name="wifi_type_label">Typ</string>
<string name="wifi_connect_failed">Network Verbindung fehlgeschlagen</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">Red de tipo incorrecto</string>
<string name="wifi_ssid_label">Nombre de red</string>
<string name="wifi_type_label">Tipo</string>
<string name="wifi_connect_failed">Conexión de red no</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">Verkon tyyppi virheellinen</string>
<string name="wifi_ssid_label">Verkon nimi</string>
<string name="wifi_type_label">Tyyppi</string>
<string name="wifi_connect_failed">Verkkoyhteys ei</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">Réseau de type incorrect</string>
<string name="wifi_ssid_label">Nom du réseau</string>
<string name="wifi_type_label">Type</string>
<string name="wifi_connect_failed">La connexion réseau a échoué</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">Hálózati típusú hibás</string>
<string name="wifi_ssid_label">Hálózat neve</string>
<string name="wifi_type_label">Típusú</string>
<string name="wifi_connect_failed">Hálózati kapcsolat nem</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">Tipo di rete non corretto</string>
<string name="wifi_ssid_label">Nome di rete</string>
<string name="wifi_type_label">Tipo</string>
<string name="wifi_connect_failed">La connessione di rete non riuscita</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">ネットワーク型不正</string>
<string name="wifi_ssid_label">ネットワーク名</string>
<string name="wifi_type_label">タイプ</string>
<string name="wifi_connect_failed">ネットワーク接続が失敗しました。</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">Netwerk soort onjuiste</string>
<string name="wifi_ssid_label">Network Name</string>
<string name="wifi_type_label">Type</string>
<string name="wifi_connect_failed">Netwerk verbinding is mislukt</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">Typ sieci nieprawidłowe</string>
<string name="wifi_ssid_label">Nazwa sieci</string>
<string name="wifi_type_label">Typ</string>
<string name="wifi_connect_failed">Połączenie sieciowe nie</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">Rede de tipo incorreto</string>
<string name="wifi_ssid_label">Nome da Rede</string>
<string name="wifi_type_label">Tipo</string>
<string name="wifi_connect_failed">A conexão de rede não</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">Тип сети неправильный</string>
<string name="wifi_ssid_label">Имя сети</string>
<string name="wifi_type_label">Тип</string>
<string name="wifi_connect_failed">Подключение к сети не удалось</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">Nätverkstyp felaktig</string>
<string name="wifi_ssid_label">Nätverksnamn</string>
<string name="wifi_type_label">Typ</string>
<string name="wifi_connect_failed">Nätverksanslutning misslyckades</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">网络类型不正确</string>
<string name="wifi_ssid_label">网络名称</string>
<string name="wifi_type_label">类型</string>
<string name="wifi_connect_failed">网络连接失败</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -129,5 +129,6 @@
<string name="wifi_type_incorrect">網絡類型不正確</string>
<string name="wifi_ssid_label">網絡名稱</string>
<string name="wifi_type_label">類型</string>
<string name="wifi_connect_failed">網絡連接失敗</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>

View file

@ -127,6 +127,7 @@
<string name="wifi_modifying_network">Modifying Network</string>
<string name="wifi_ssid_missing">Network name missing</string>
<string name="wifi_type_incorrect">Network type incorrect</string>
<string name="wifi_connect_failed">Network connection failed</string>
<string name="wifi_ssid_label">Network Name</string>
<string name="wifi_type_label">Type</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>

View file

@ -17,7 +17,6 @@
package com.google.zxing.client.android.result;
import android.app.Activity;
import com.google.zxing.client.android.R;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.WifiParsedResult;
@ -52,7 +51,7 @@ public final class WifiResultHandler extends ResultHandler {
@Override
public void handleButtonPress(int index) {
// Get the underlying wifi config
WifiParsedResult wifiResult = (WifiParsedResult) getResult();
final WifiParsedResult wifiResult = (WifiParsedResult) getResult();
if (index == 0) {
wifiConnect(wifiResult);
}
@ -61,7 +60,7 @@ public final class WifiResultHandler extends ResultHandler {
// Display the name of the network and the network type to the user.
@Override
public CharSequence getDisplayContents() {
WifiParsedResult wifiResult = (WifiParsedResult) getResult();
final 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);

View file

@ -1,59 +0,0 @@
/*
* Copyright (C) 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.android.wifi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.TextView;
import com.google.zxing.client.android.R;
/**
* Get a broadcast when the network is connected, and kill the activity.
*/
final class ConnectedReceiver extends BroadcastReceiver {
private final Activity parent;
private final TextView statusView;
ConnectedReceiver(Activity wifiActivity, TextView statusView) {
this.parent = wifiActivity;
this.statusView = statusView;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityManager con = (ConnectivityManager) parent.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo[] s = con.getAllNetworkInfo();
for (NetworkInfo i : s){
if (i.getTypeName().contentEquals("WIFI")){
NetworkInfo.State state = i.getState();
if (state == NetworkInfo.State.CONNECTED){
statusView.setText(R.string.wifi_connected);
Runnable delayKill = new Killer(parent);
delayKill.run();
}
}
}
}
}
}

View file

@ -77,4 +77,4 @@ final class NetworkUtil {
return (length == 10 || length == 26 || length == 58) && isHex(wepKey);
}
}
}

View file

@ -33,19 +33,33 @@ import com.google.zxing.client.android.R;
/**
* A new activity showing the progress of Wifi connection
*
* TODO(viki): Tell the user when the network is not available here
* TODO(viki): Incorrect password, could not connect, give an error
*
* @author Vikram Aggarwal
*/
public class WifiActivity extends Activity {
private static final String TAG = WifiActivity.class.getSimpleName();
private WifiManager wifiManager;
private TextView statusView;
private ConnectedReceiver connectedReceiver;
private WifiReceiver wifiReceiver;
private boolean receiverRegistered;
private int networkId;
private static int errorCount;
private IntentFilter mWifiStateFilter;
static {
errorCount = 0;
}
public void gotError(){
final int maxErrorCount = 3;
errorCount++;
Log.d(TAG, "Encountered another error. Errorcount = " + errorCount);
if (errorCount > maxErrorCount){
errorCount = 0;
doError(R.string.wifi_connect_failed);
}
}
public enum NetworkType {
NETWORK_WEP, NETWORK_WPA, NETWORK_NOPASS, NETWORK_INVALID,
@ -76,13 +90,23 @@ public class WifiActivity extends Activity {
private int doError(int resource_string) {
statusView.setText(resource_string);
// Give up on the connection
wifiManager.disconnect();
if (networkId > 0) {
wifiManager.removeNetwork(networkId);
networkId = -1;
}
if (receiverRegistered) {
unregisterReceiver(wifiReceiver);
receiverRegistered = false;
}
return -1;
}
private WifiConfiguration changeNetworkCommon(NetworkSetting input){
statusView.setText(R.string.wifi_creating_network);
Log.d(TAG, "Adding new configuration: \nSSID: " + input.getSsid() + "\nType: " + input.getNetworkType());
WifiConfiguration config = new WifiConfiguration();
final WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
@ -103,11 +127,12 @@ public class WifiActivity extends Activity {
// Adding a WEP network
private int changeNetworkWEP(NetworkSetting input) {
WifiConfiguration config = changeNetworkCommon(input);
if (NetworkUtil.isHexWepKey(input.getPassword())) {
config.wepKeys[0] = input.getPassword();
final WifiConfiguration config = changeNetworkCommon(input);
final String pass = input.getPassword();
if (NetworkUtil.isHexWepKey(pass)) {
config.wepKeys[0] = pass;
} else {
config.wepKeys[0] = NetworkUtil.convertToQuotedString(input.getPassword());
config.wepKeys[0] = NetworkUtil.convertToQuotedString(pass);
}
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
@ -121,7 +146,7 @@ public class WifiActivity extends Activity {
// Adding a WPA or WPA2 network
private int changeNetworkWPA(NetworkSetting input) {
WifiConfiguration config = changeNetworkCommon(input);
final WifiConfiguration config = changeNetworkCommon(input);
final String pass = input.getPassword();
// Hex passwords that are 64 bits long are not to be quoted.
if (pass.matches("[0-9A-Fa-f]{64}")){
@ -144,8 +169,8 @@ public class WifiActivity extends Activity {
// Adding an open, unsecured network
private int changeNetworkUnEncrypted(NetworkSetting input){
WifiConfiguration config = changeNetworkCommon(input);
Log.d(TAG, "Empty password prompting a simple account setting");
WifiConfiguration config = changeNetworkCommon(input);
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
@ -158,7 +183,7 @@ public class WifiActivity extends Activity {
*/
private WifiConfiguration findNetworkInExistingConfig(String ssid){
final List <WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
for (WifiConfiguration existingConfig : existingConfigs) {
for (final WifiConfiguration existingConfig : existingConfigs) {
if (existingConfig.SSID.equals(ssid)) {
return existingConfig;
}
@ -182,7 +207,6 @@ public class WifiActivity extends Activity {
setContentView(R.layout.network);
statusView = (TextView) findViewById(R.id.networkStatus);
// TODO(vikrama): Error checking here, to ensure ssid exists.
NetworkType networkT;
if (networkType.equals("WPA")) {
networkT = NetworkType.NETWORK_WPA;
@ -191,17 +215,25 @@ public class WifiActivity extends Activity {
} else if (networkType.equals("nopass")) {
networkT = NetworkType.NETWORK_NOPASS;
} else {
// Got an incorrect network type. Give an error
doError(R.string.wifi_type_incorrect);
return;
}
// This is not available before onCreate
wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
// Start WiFi, otherwise nothing will work
wifiManager.setWifiEnabled(true);
// So we know when the network changes
connectedReceiver = new ConnectedReceiver(this, statusView);
registerReceiver(connectedReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
wifiReceiver = new WifiReceiver(wifiManager, this, statusView, ssid);
// The order matters!
mWifiStateFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
mWifiStateFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mWifiStateFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
mWifiStateFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(wifiReceiver, mWifiStateFilter);
receiverRegistered = true;
if (password == null) {
password = "";
@ -211,11 +243,28 @@ public class WifiActivity extends Activity {
changeNetwork(setting);
}
public void pause() {
if (receiverRegistered) {
unregisterReceiver(wifiReceiver);
receiverRegistered = false;
}
}
public void resume() {
if (wifiReceiver != null && mWifiStateFilter != null && !receiverRegistered) {
registerReceiver(wifiReceiver, mWifiStateFilter);
receiverRegistered = true;
}
}
@Override
protected void onDestroy() {
if (connectedReceiver != null) {
unregisterReceiver(connectedReceiver);
connectedReceiver = null;
if (wifiReceiver != null) {
if (receiverRegistered) {
unregisterReceiver(wifiReceiver);
receiverRegistered = false;
}
wifiReceiver = null;
}
super.onDestroy();
}
@ -227,18 +276,29 @@ public class WifiActivity extends Activity {
* @return network ID of the connected network.
*/
private int updateNetwork(WifiConfiguration config, boolean disableOthers){
int networkId;
if (findNetworkInExistingConfig(config.SSID) == null){
final int FAILURE = -1;
WifiConfiguration found = findNetworkInExistingConfig(config.SSID);
wifiManager.disconnect();
if (found == null){
statusView.setText(R.string.wifi_creating_network);
networkId = wifiManager.addNetwork(config);
} else {
statusView.setText(R.string.wifi_modifying_network);
networkId = wifiManager.updateNetwork(config);
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;
// Try to disable the current network and start a new one.
if (!wifiManager.enableNetwork(networkId, disableOthers)) {
networkId = -1;
return FAILURE;
}
if (networkId == -1 || !wifiManager.enableNetwork(networkId, disableOthers)) {
return -1;
}
wifiManager.saveConfiguration();
errorCount = 0;
wifiManager.reassociate();
return networkId;
}
}