2 * Copyright (C) 2009 The Android Open Source Project
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.android.settings.wifi;
19 import android.app.Activity;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.net.NetworkInfo;
25 import android.net.wifi.ScanResult;
26 import android.net.wifi.SupplicantState;
27 import android.net.wifi.WifiConfiguration;
28 import android.net.wifi.WifiInfo;
29 import android.net.wifi.WifiManager;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.text.TextUtils;
33 import android.util.Log;
34 import android.view.View;
35 import android.view.View.OnClickListener;
36 import android.widget.Button;
37 import android.widget.TextView;
39 import com.android.settings.R;
40 import com.android.settingslib.wifi.AccessPoint;
42 import java.io.IOException;
43 import java.net.HttpURLConnection;
45 import java.net.UnknownHostException;
46 import java.util.List;
50 * Show the current status details of Wifi related fields
52 public class WifiStatusTest extends Activity {
54 private static final String TAG = "WifiStatusTest";
56 private Button updateButton;
57 private TextView mWifiState;
58 private TextView mNetworkState;
59 private TextView mSupplicantState;
60 private TextView mRSSI;
61 private TextView mBSSID;
62 private TextView mSSID;
63 private TextView mHiddenSSID;
64 private TextView mIPAddr;
65 private TextView mMACAddr;
66 private TextView mNetworkId;
67 private TextView mLinkSpeed;
68 private TextView mScanList;
71 private TextView mPingIpAddr;
72 private TextView mPingHostname;
73 private TextView mHttpClientTest;
74 private Button pingTestButton;
76 private String mPingIpAddrResult;
77 private String mPingHostnameResult;
78 private String mHttpClientTestResult;
81 private WifiManager mWifiManager;
82 private IntentFilter mWifiStateFilter;
85 //============================
87 //============================
89 private final BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() {
91 public void onReceive(Context context, Intent intent) {
92 if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
93 handleWifiStateChanged(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
94 WifiManager.WIFI_STATE_UNKNOWN));
95 } else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
96 handleNetworkStateChanged(
97 (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO));
98 } else if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
99 handleScanResultsAvailable();
100 } else if (intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
101 /* TODO: handle supplicant connection change later */
102 } else if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
103 handleSupplicantStateChanged(
104 (SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE),
105 intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR),
106 intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 0));
107 } else if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) {
108 handleSignalChanged(intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, 0));
109 } else if (intent.getAction().equals(WifiManager.NETWORK_IDS_CHANGED_ACTION)) {
110 /* TODO: handle network id change info later */
112 Log.e(TAG, "Received an unknown Wifi Intent");
118 protected void onCreate(Bundle savedInstanceState) {
119 super.onCreate(savedInstanceState);
121 mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
123 mWifiStateFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
124 mWifiStateFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
125 mWifiStateFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
126 mWifiStateFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
127 mWifiStateFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
128 mWifiStateFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
130 registerReceiver(mWifiStateReceiver, mWifiStateFilter);
132 setContentView(R.layout.wifi_status_test);
134 updateButton = (Button) findViewById(R.id.update);
135 updateButton.setOnClickListener(updateButtonHandler);
137 mWifiState = (TextView) findViewById(R.id.wifi_state);
138 mNetworkState = (TextView) findViewById(R.id.network_state);
139 mSupplicantState = (TextView) findViewById(R.id.supplicant_state);
140 mRSSI = (TextView) findViewById(R.id.rssi);
141 mBSSID = (TextView) findViewById(R.id.bssid);
142 mSSID = (TextView) findViewById(R.id.ssid);
143 mHiddenSSID = (TextView) findViewById(R.id.hidden_ssid);
144 mIPAddr = (TextView) findViewById(R.id.ipaddr);
145 mMACAddr = (TextView) findViewById(R.id.macaddr);
146 mNetworkId = (TextView) findViewById(R.id.networkid);
147 mLinkSpeed = (TextView) findViewById(R.id.link_speed);
148 mScanList = (TextView) findViewById(R.id.scan_list);
151 mPingHostname = (TextView) findViewById(R.id.pingHostnameV4);
152 mHttpClientTest = (TextView) findViewById(R.id.httpClientTest);
154 pingTestButton = (Button) findViewById(R.id.ping_test);
155 pingTestButton.setOnClickListener(mPingButtonHandler);
159 protected void onResume() {
161 registerReceiver(mWifiStateReceiver, mWifiStateFilter);
165 protected void onPause() {
167 unregisterReceiver(mWifiStateReceiver);
170 OnClickListener mPingButtonHandler = new OnClickListener() {
171 public void onClick(View v) {
176 OnClickListener updateButtonHandler = new OnClickListener() {
177 public void onClick(View v) {
178 final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
180 setWifiStateText(mWifiManager.getWifiState());
181 mBSSID.setText(wifiInfo.getBSSID());
182 mHiddenSSID.setText(String.valueOf(wifiInfo.getHiddenSSID()));
183 int ipAddr = wifiInfo.getIpAddress();
184 StringBuffer ipBuf = new StringBuffer();
185 ipBuf.append(ipAddr & 0xff).append('.').
186 append((ipAddr >>>= 8) & 0xff).append('.').
187 append((ipAddr >>>= 8) & 0xff).append('.').
188 append((ipAddr >>>= 8) & 0xff);
190 mIPAddr.setText(ipBuf);
191 mLinkSpeed.setText(String.valueOf(wifiInfo.getLinkSpeed())+" Mbps");
192 mMACAddr.setText(wifiInfo.getMacAddress());
193 mNetworkId.setText(String.valueOf(wifiInfo.getNetworkId()));
194 mRSSI.setText(String.valueOf(wifiInfo.getRssi()));
195 mSSID.setText(wifiInfo.getSSID());
197 SupplicantState supplicantState = wifiInfo.getSupplicantState();
198 setSupplicantStateText(supplicantState);
202 private void setSupplicantStateText(SupplicantState supplicantState) {
203 if(SupplicantState.FOUR_WAY_HANDSHAKE.equals(supplicantState)) {
204 mSupplicantState.setText("FOUR WAY HANDSHAKE");
205 } else if(SupplicantState.ASSOCIATED.equals(supplicantState)) {
206 mSupplicantState.setText("ASSOCIATED");
207 } else if(SupplicantState.ASSOCIATING.equals(supplicantState)) {
208 mSupplicantState.setText("ASSOCIATING");
209 } else if(SupplicantState.COMPLETED.equals(supplicantState)) {
210 mSupplicantState.setText("COMPLETED");
211 } else if(SupplicantState.DISCONNECTED.equals(supplicantState)) {
212 mSupplicantState.setText("DISCONNECTED");
213 } else if(SupplicantState.DORMANT.equals(supplicantState)) {
214 mSupplicantState.setText("DORMANT");
215 } else if(SupplicantState.GROUP_HANDSHAKE.equals(supplicantState)) {
216 mSupplicantState.setText("GROUP HANDSHAKE");
217 } else if(SupplicantState.INACTIVE.equals(supplicantState)) {
218 mSupplicantState.setText("INACTIVE");
219 } else if(SupplicantState.INVALID.equals(supplicantState)) {
220 mSupplicantState.setText("INVALID");
221 } else if(SupplicantState.SCANNING.equals(supplicantState)) {
222 mSupplicantState.setText("SCANNING");
223 } else if(SupplicantState.UNINITIALIZED.equals(supplicantState)) {
224 mSupplicantState.setText("UNINITIALIZED");
226 mSupplicantState.setText("BAD");
227 Log.e(TAG, "supplicant state is bad");
231 private void setWifiStateText(int wifiState) {
232 String wifiStateString;
234 case WifiManager.WIFI_STATE_DISABLING:
235 wifiStateString = getString(R.string.wifi_state_disabling);
237 case WifiManager.WIFI_STATE_DISABLED:
238 wifiStateString = getString(R.string.wifi_state_disabled);
240 case WifiManager.WIFI_STATE_ENABLING:
241 wifiStateString = getString(R.string.wifi_state_enabling);
243 case WifiManager.WIFI_STATE_ENABLED:
244 wifiStateString = getString(R.string.wifi_state_enabled);
246 case WifiManager.WIFI_STATE_UNKNOWN:
247 wifiStateString = getString(R.string.wifi_state_unknown);
250 wifiStateString = "BAD";
251 Log.e(TAG, "wifi state is bad");
255 mWifiState.setText(wifiStateString);
258 private void handleSignalChanged(int rssi) {
259 mRSSI.setText(String.valueOf(rssi));
262 private void handleWifiStateChanged(int wifiState) {
263 setWifiStateText(wifiState);
266 private void handleScanResultsAvailable() {
267 List<ScanResult> list = mWifiManager.getScanResults();
269 StringBuffer scanList = new StringBuffer();
271 for (int i = list.size() - 1; i >= 0; i--) {
272 final ScanResult scanResult = list.get(i);
274 if (scanResult == null) {
278 if (TextUtils.isEmpty(scanResult.SSID)) {
282 scanList.append(scanResult.SSID+" ");
285 mScanList.setText(scanList);
288 private void handleSupplicantStateChanged(SupplicantState state, boolean hasError, int error) {
290 mSupplicantState.setText("ERROR AUTHENTICATING");
292 setSupplicantStateText(state);
296 private void handleNetworkStateChanged(NetworkInfo networkInfo) {
297 if (mWifiManager.isWifiEnabled()) {
298 WifiInfo info = mWifiManager.getConnectionInfo();
299 String summary = AccessPoint.getSummary(this, info.getSSID(),
300 networkInfo.getDetailedState(),
301 info.getNetworkId() == WifiConfiguration.INVALID_NETWORK_ID, null);
302 mNetworkState.setText(summary);
306 private final void updatePingState() {
307 final Handler handler = new Handler();
308 // Set all to unknown since the threads will take a few secs to update.
309 mPingIpAddrResult = getResources().getString(R.string.radioInfo_unknown);
310 mPingHostnameResult = getResources().getString(R.string.radioInfo_unknown);
311 mHttpClientTestResult = getResources().getString(R.string.radioInfo_unknown);
313 mPingIpAddr.setText(mPingIpAddrResult);
314 mPingHostname.setText(mPingHostnameResult);
315 mHttpClientTest.setText(mHttpClientTestResult);
317 final Runnable updatePingResults = new Runnable() {
319 mPingHostname.setText(mPingHostnameResult);
320 mHttpClientTest.setText(mHttpClientTestResult);
324 Thread hostnameThread = new Thread() {
328 handler.post(updatePingResults);
331 hostnameThread.start();
333 Thread httpClientThread = new Thread() {
337 handler.post(updatePingResults);
340 httpClientThread.start();
343 private final void pingHostname() {
345 // TODO: Hardcoded for now, make it UI configurable
346 Process p = Runtime.getRuntime().exec("ping -c 1 -w 100 www.google.com");
347 int status = p.waitFor();
349 mPingHostnameResult = "Pass";
351 mPingHostnameResult = "Fail: Host unreachable";
353 } catch (UnknownHostException e) {
354 mPingHostnameResult = "Fail: Unknown Host";
355 } catch (IOException e) {
356 mPingHostnameResult= "Fail: IOException";
357 } catch (InterruptedException e) {
358 mPingHostnameResult = "Fail: InterruptedException";
362 private void httpClientTest() {
363 HttpURLConnection urlConnection = null;
365 // TODO: Hardcoded for now, make it UI configurable
366 URL url = new URL("https://www.google.com");
367 urlConnection = (HttpURLConnection) url.openConnection();
368 if (urlConnection.getResponseCode() == 200) {
369 mHttpClientTestResult = "Pass";
371 mHttpClientTestResult = "Fail: Code: " + urlConnection.getResponseMessage();
373 } catch (IOException e) {
374 mHttpClientTestResult = "Fail: IOException";
376 if (urlConnection != null) {
377 urlConnection.disconnect();