2 * Copyright (C) 2010 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.client.android.wifi;
19 import java.util.List;
21 import android.app.Activity;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.net.ConnectivityManager;
25 import android.net.wifi.WifiConfiguration;
26 import android.net.wifi.WifiManager;
27 import android.os.Bundle;
28 import android.util.Log;
29 import android.widget.TextView;
31 import com.google.zxing.client.android.Intents;
32 import com.google.zxing.client.android.R;
35 * A new activity showing the progress of Wifi connection
37 * TODO(viki): Tell the user when the network is not available here
38 * TODO(viki): Incorrect password, could not connect, give an error
40 * @author Vikram Aggarwal
42 public class WifiActivity extends Activity {
44 private static final String TAG = WifiActivity.class.getSimpleName();
46 private WifiManager wifiManager;
47 private TextView statusView;
48 private ConnectedReceiver connectedReceiver;
50 public enum NetworkType {
51 NETWORK_WEP, NETWORK_WPA, NETWORK_NOPASS, NETWORK_INVALID,
54 private int changeNetwork(NetworkSetting setting) {
55 // If the SSID is empty, throw an error and return
56 if (setting.getSsid() == null || setting.getSsid().length() == 0) {
57 return doError(R.string.wifi_ssid_missing);
59 // If the network type is invalid
60 if (setting.getNetworkType() == NetworkType.NETWORK_INVALID){
61 return doError(R.string.wifi_type_incorrect);
64 // If the password is empty, this is an unencrypted network
65 if (setting.getPassword() == null || setting.getPassword().length() == 0 ||
66 setting.getNetworkType() == null ||
67 setting.getNetworkType() == NetworkType.NETWORK_NOPASS) {
68 return changeNetworkUnEncrypted(setting);
70 if (setting.getNetworkType() == NetworkType.NETWORK_WPA) {
71 return changeNetworkWPA(setting);
73 return changeNetworkWEP(setting);
77 private int doError(int resource_string) {
78 statusView.setText(resource_string);
82 private WifiConfiguration changeNetworkCommon(NetworkSetting input){
83 statusView.setText(R.string.wifi_creating_network);
84 Log.d(TAG, "Adding new configuration: \nSSID: " + input.getSsid() + "\nType: " + input.getNetworkType());
85 WifiConfiguration config = new WifiConfiguration();
87 config.allowedAuthAlgorithms.clear();
88 config.allowedGroupCiphers.clear();
89 config.allowedKeyManagement.clear();
90 config.allowedPairwiseCiphers.clear();
91 config.allowedProtocols.clear();
93 // Android API insists that an ascii SSID must be quoted to be correctly handled.
94 config.SSID = NetworkUtil.convertToQuotedString(input.getSsid());
95 config.hiddenSSID = true;
99 private int requestNetworkChange(WifiConfiguration config){
100 statusView.setText(R.string.wifi_changing_network);
101 return updateNetwork(config, false);
104 // Adding a WEP network
105 private int changeNetworkWEP(NetworkSetting input) {
106 WifiConfiguration config = changeNetworkCommon(input);
107 if (NetworkUtil.isHexWepKey(input.getPassword())) {
108 config.wepKeys[0] = input.getPassword();
110 config.wepKeys[0] = NetworkUtil.convertToQuotedString(input.getPassword());
112 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
113 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
114 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
115 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
116 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
117 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
118 config.wepTxKeyIndex = 0;
119 return requestNetworkChange(config);
122 // Adding a WPA or WPA2 network
123 private int changeNetworkWPA(NetworkSetting input) {
124 WifiConfiguration config = changeNetworkCommon(input);
125 final String pass = input.getPassword();
126 // Hex passwords that are 64 bits long are not to be quoted.
127 if (pass.matches("[0-9A-Fa-f]{64}")){
128 Log.d(TAG, "A 64 bit hex password entered.");
129 config.preSharedKey = pass;
131 Log.d(TAG, "A normal password entered: I am quoting it.");
132 config.preSharedKey = NetworkUtil.convertToQuotedString(pass);
134 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
136 config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
138 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
139 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
140 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
141 config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
142 return requestNetworkChange(config);
145 // Adding an open, unsecured network
146 private int changeNetworkUnEncrypted(NetworkSetting input){
147 WifiConfiguration config = changeNetworkCommon(input);
148 Log.d(TAG, "Empty password prompting a simple account setting");
149 config.wepKeys[0] = "";
150 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
151 config.wepTxKeyIndex = 0;
152 return requestNetworkChange(config);
156 * If the given ssid name exists in the settings, then change its password to the one given here, and save
159 private WifiConfiguration findNetworkInExistingConfig(String ssid){
160 final List <WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
161 for (WifiConfiguration existingConfig : existingConfigs) {
162 if (existingConfig.SSID.equals(ssid)) {
163 return existingConfig;
170 protected void onCreate(Bundle savedInstanceState) {
171 super.onCreate(savedInstanceState);
173 final Intent intent = getIntent();
174 if (intent == null || (!intent.getAction().equals(Intents.WifiConnect.ACTION))) {
179 final String ssid = intent.getStringExtra(Intents.WifiConnect.SSID);
180 String password = intent.getStringExtra(Intents.WifiConnect.PASSWORD);
181 final String networkType = intent.getStringExtra(Intents.WifiConnect.TYPE);
182 setContentView(R.layout.network);
183 statusView = (TextView) findViewById(R.id.networkStatus);
185 // TODO(vikrama): Error checking here, to ensure ssid exists.
186 NetworkType networkT;
187 if (networkType.equals("WPA")) {
188 networkT = NetworkType.NETWORK_WPA;
189 } else if (networkType.equals("WEP")) {
190 networkT = NetworkType.NETWORK_WEP;
191 } else if (networkType.equals("nopass")) {
192 networkT = NetworkType.NETWORK_NOPASS;
194 // Got an incorrect network type. Give an error
195 doError(R.string.wifi_type_incorrect);
199 // This is not available before onCreate
200 wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
202 // So we know when the network changes
203 connectedReceiver = new ConnectedReceiver(this, statusView);
204 registerReceiver(connectedReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
206 if (password == null) {
209 Log.d(TAG, "Adding new configuration: \nSSID: " + ssid + "Type: " + networkT);
210 NetworkSetting setting = new NetworkSetting(ssid, password, networkT);
211 changeNetwork(setting);
215 protected void onDestroy() {
216 if (connectedReceiver != null) {
217 unregisterReceiver(connectedReceiver);
218 connectedReceiver = null;
224 * Update the network: either create a new network or modify an existing network
225 * @param config the new network configuration
226 * @param disableOthers true if other networks must be disabled
227 * @return network ID of the connected network.
229 private int updateNetwork(WifiConfiguration config, boolean disableOthers){
231 if (findNetworkInExistingConfig(config.SSID) == null){
232 statusView.setText(R.string.wifi_creating_network);
233 networkId = wifiManager.addNetwork(config);
235 statusView.setText(R.string.wifi_modifying_network);
236 networkId = wifiManager.updateNetwork(config);
238 if (networkId == -1 || !wifiManager.enableNetwork(networkId, disableOthers)) {
241 wifiManager.saveConfiguration();