1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package com.android.settings.wifi; 18 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; 38 39 import com.android.settings.R; 40 import com.android.settingslib.wifi.AccessPoint; 41 42 import java.io.IOException; 43 import java.net.HttpURLConnection; 44 import java.net.URL; 45 import java.net.UnknownHostException; 46 import java.util.List; 47 48 /** 49 * Show the current status details of Wifi related fields 50 */ 51 public class WifiStatusTest extends Activity { 52 53 private static final String TAG = "WifiStatusTest"; 54 55 private Button updateButton; 56 private TextView mWifiState; 57 private TextView mNetworkState; 58 private TextView mSupplicantState; 59 private TextView mRSSI; 60 private TextView mBSSID; 61 private TextView mSSID; 62 private TextView mHiddenSSID; 63 private TextView mIPAddr; 64 private TextView mMACAddr; 65 private TextView mNetworkId; 66 private TextView mTxLinkSpeed; 67 private TextView mRxLinkSpeed; 68 private TextView mScanList; 69 70 71 private TextView mPingHostname; 72 private TextView mHttpClientTest; 73 private Button pingTestButton; 74 75 private String mPingHostnameResult; 76 private String mHttpClientTestResult; 77 78 79 private WifiManager mWifiManager; 80 private IntentFilter mWifiStateFilter; 81 82 83 //============================ 84 // Activity lifecycle 85 //============================ 86 87 private final BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() { 88 @Override 89 public void onReceive(Context context, Intent intent) { 90 if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { 91 handleWifiStateChanged(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 92 WifiManager.WIFI_STATE_UNKNOWN)); 93 } else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { 94 handleNetworkStateChanged( 95 (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)); 96 } else if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { 97 handleScanResultsAvailable(); 98 } else if (intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) { 99 /* TODO: handle supplicant connection change later */ 100 } else if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) { 101 handleSupplicantStateChanged( 102 (SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE), 103 intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR), 104 intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 0)); 105 } else if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) { 106 handleSignalChanged(intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, 0)); 107 } else if (intent.getAction().equals(WifiManager.NETWORK_IDS_CHANGED_ACTION)) { 108 /* TODO: handle network id change info later */ 109 } else { 110 Log.e(TAG, "Received an unknown Wifi Intent"); 111 } 112 } 113 }; 114 115 @Override onCreate(Bundle savedInstanceState)116 protected void onCreate(Bundle savedInstanceState) { 117 super.onCreate(savedInstanceState); 118 119 mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE); 120 121 mWifiStateFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION); 122 mWifiStateFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 123 mWifiStateFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 124 mWifiStateFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); 125 mWifiStateFilter.addAction(WifiManager.RSSI_CHANGED_ACTION); 126 mWifiStateFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); 127 128 registerReceiver(mWifiStateReceiver, mWifiStateFilter, 129 Context.RECEIVER_EXPORTED_UNAUDITED); 130 131 setContentView(R.layout.wifi_status_test); 132 133 updateButton = (Button) findViewById(R.id.update); 134 updateButton.setOnClickListener(updateButtonHandler); 135 136 mWifiState = (TextView) findViewById(R.id.wifi_state); 137 mNetworkState = (TextView) findViewById(R.id.network_state); 138 mSupplicantState = (TextView) findViewById(R.id.supplicant_state); 139 mRSSI = (TextView) findViewById(R.id.rssi); 140 mBSSID = (TextView) findViewById(R.id.bssid); 141 mSSID = (TextView) findViewById(R.id.ssid); 142 mHiddenSSID = (TextView) findViewById(R.id.hidden_ssid); 143 mIPAddr = (TextView) findViewById(R.id.ipaddr); 144 mMACAddr = (TextView) findViewById(R.id.macaddr); 145 mNetworkId = (TextView) findViewById(R.id.networkid); 146 mTxLinkSpeed = (TextView) findViewById(R.id.tx_link_speed); 147 mRxLinkSpeed = (TextView) findViewById(R.id.rx_link_speed); 148 mScanList = (TextView) findViewById(R.id.scan_list); 149 150 151 mPingHostname = (TextView) findViewById(R.id.pingHostname); 152 mHttpClientTest = (TextView) findViewById(R.id.httpClientTest); 153 154 pingTestButton = (Button) findViewById(R.id.ping_test); 155 pingTestButton.setOnClickListener(mPingButtonHandler); 156 } 157 158 @Override onResume()159 protected void onResume() { 160 super.onResume(); 161 registerReceiver(mWifiStateReceiver, mWifiStateFilter, 162 Context.RECEIVER_EXPORTED_UNAUDITED); 163 } 164 165 @Override onPause()166 protected void onPause() { 167 super.onPause(); 168 unregisterReceiver(mWifiStateReceiver); 169 } 170 171 OnClickListener mPingButtonHandler = new OnClickListener() { 172 public void onClick(View v) { 173 updatePingState(); 174 } 175 }; 176 177 OnClickListener updateButtonHandler = new OnClickListener() { 178 public void onClick(View v) { 179 final WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); 180 181 setWifiStateText(mWifiManager.getWifiState()); 182 mBSSID.setText(wifiInfo.getBSSID()); 183 mHiddenSSID.setText(String.valueOf(wifiInfo.getHiddenSSID())); 184 int ipAddr = wifiInfo.getIpAddress(); 185 StringBuffer ipBuf = new StringBuffer(); 186 ipBuf.append(ipAddr & 0xff).append('.'). 187 append((ipAddr >>>= 8) & 0xff).append('.'). 188 append((ipAddr >>>= 8) & 0xff).append('.'). 189 append((ipAddr >>>= 8) & 0xff); 190 191 mIPAddr.setText(ipBuf); 192 mTxLinkSpeed.setText(String.valueOf(wifiInfo.getTxLinkSpeedMbps())+" Mbps"); 193 mRxLinkSpeed.setText(String.valueOf(wifiInfo.getRxLinkSpeedMbps())+" Mbps"); 194 mMACAddr.setText(wifiInfo.getMacAddress()); 195 mNetworkId.setText(String.valueOf(wifiInfo.getNetworkId())); 196 mRSSI.setText(String.valueOf(wifiInfo.getRssi())); 197 mSSID.setText(wifiInfo.getSSID()); 198 199 SupplicantState supplicantState = wifiInfo.getSupplicantState(); 200 setSupplicantStateText(supplicantState); 201 } 202 }; 203 setSupplicantStateText(SupplicantState supplicantState)204 private void setSupplicantStateText(SupplicantState supplicantState) { 205 if(SupplicantState.FOUR_WAY_HANDSHAKE.equals(supplicantState)) { 206 mSupplicantState.setText("FOUR WAY HANDSHAKE"); 207 } else if(SupplicantState.ASSOCIATED.equals(supplicantState)) { 208 mSupplicantState.setText("ASSOCIATED"); 209 } else if(SupplicantState.ASSOCIATING.equals(supplicantState)) { 210 mSupplicantState.setText("ASSOCIATING"); 211 } else if(SupplicantState.COMPLETED.equals(supplicantState)) { 212 mSupplicantState.setText("COMPLETED"); 213 } else if(SupplicantState.DISCONNECTED.equals(supplicantState)) { 214 mSupplicantState.setText("DISCONNECTED"); 215 } else if(SupplicantState.DORMANT.equals(supplicantState)) { 216 mSupplicantState.setText("DORMANT"); 217 } else if(SupplicantState.GROUP_HANDSHAKE.equals(supplicantState)) { 218 mSupplicantState.setText("GROUP HANDSHAKE"); 219 } else if(SupplicantState.INACTIVE.equals(supplicantState)) { 220 mSupplicantState.setText("INACTIVE"); 221 } else if(SupplicantState.INVALID.equals(supplicantState)) { 222 mSupplicantState.setText("INVALID"); 223 } else if(SupplicantState.SCANNING.equals(supplicantState)) { 224 mSupplicantState.setText("SCANNING"); 225 } else if(SupplicantState.UNINITIALIZED.equals(supplicantState)) { 226 mSupplicantState.setText("UNINITIALIZED"); 227 } else { 228 mSupplicantState.setText("BAD"); 229 Log.e(TAG, "supplicant state is bad"); 230 } 231 } 232 setWifiStateText(int wifiState)233 private void setWifiStateText(int wifiState) { 234 String wifiStateString; 235 switch(wifiState) { 236 case WifiManager.WIFI_STATE_DISABLING: 237 wifiStateString = getString(R.string.wifi_state_disabling); 238 break; 239 case WifiManager.WIFI_STATE_DISABLED: 240 wifiStateString = getString(R.string.wifi_state_disabled); 241 break; 242 case WifiManager.WIFI_STATE_ENABLING: 243 wifiStateString = getString(R.string.wifi_state_enabling); 244 break; 245 case WifiManager.WIFI_STATE_ENABLED: 246 wifiStateString = getString(R.string.wifi_state_enabled); 247 break; 248 case WifiManager.WIFI_STATE_UNKNOWN: 249 wifiStateString = getString(R.string.wifi_state_unknown); 250 break; 251 default: 252 wifiStateString = "BAD"; 253 Log.e(TAG, "wifi state is bad"); 254 break; 255 } 256 257 mWifiState.setText(wifiStateString); 258 } 259 handleSignalChanged(int rssi)260 private void handleSignalChanged(int rssi) { 261 mRSSI.setText(String.valueOf(rssi)); 262 } 263 handleWifiStateChanged(int wifiState)264 private void handleWifiStateChanged(int wifiState) { 265 setWifiStateText(wifiState); 266 } 267 handleScanResultsAvailable()268 private void handleScanResultsAvailable() { 269 List<ScanResult> list = mWifiManager.getScanResults(); 270 271 StringBuffer scanList = new StringBuffer(); 272 if (list != null) { 273 for (int i = list.size() - 1; i >= 0; i--) { 274 final ScanResult scanResult = list.get(i); 275 276 if (scanResult == null) { 277 continue; 278 } 279 280 if (TextUtils.isEmpty(scanResult.SSID)) { 281 continue; 282 } 283 284 scanList.append(scanResult.SSID+" "); 285 } 286 } 287 mScanList.setText(scanList); 288 } 289 handleSupplicantStateChanged(SupplicantState state, boolean hasError, int error)290 private void handleSupplicantStateChanged(SupplicantState state, boolean hasError, int error) { 291 if (hasError) { 292 mSupplicantState.setText("ERROR AUTHENTICATING"); 293 } else { 294 setSupplicantStateText(state); 295 } 296 } 297 handleNetworkStateChanged(NetworkInfo networkInfo)298 private void handleNetworkStateChanged(NetworkInfo networkInfo) { 299 if (mWifiManager.isWifiEnabled()) { 300 WifiInfo info = mWifiManager.getConnectionInfo(); 301 String summary = AccessPoint.getSummary(this, info.getSSID(), 302 networkInfo.getDetailedState(), 303 info.getNetworkId() == WifiConfiguration.INVALID_NETWORK_ID, 304 /* suggestionOrSpecifierPackageName */ null); 305 mNetworkState.setText(summary); 306 } 307 } 308 updatePingState()309 private final void updatePingState() { 310 final Handler handler = new Handler(); 311 // Set all to unknown since the threads will take a few secs to update. 312 mPingHostnameResult = getResources().getString(R.string.radioInfo_unknown); 313 mHttpClientTestResult = getResources().getString(R.string.radioInfo_unknown); 314 315 mPingHostname.setText(mPingHostnameResult); 316 mHttpClientTest.setText(mHttpClientTestResult); 317 318 final Runnable updatePingResults = new Runnable() { 319 public void run() { 320 mPingHostname.setText(mPingHostnameResult); 321 mHttpClientTest.setText(mHttpClientTestResult); 322 } 323 }; 324 325 Thread hostnameThread = new Thread() { 326 @Override 327 public void run() { 328 pingHostname(); 329 handler.post(updatePingResults); 330 } 331 }; 332 hostnameThread.start(); 333 334 Thread httpClientThread = new Thread() { 335 @Override 336 public void run() { 337 httpClientTest(); 338 handler.post(updatePingResults); 339 } 340 }; 341 httpClientThread.start(); 342 } 343 pingHostname()344 private final void pingHostname() { 345 try { 346 // TODO: Hardcoded for now, make it UI configurable 347 Process p = Runtime.getRuntime().exec("ping -c 1 -w 100 www.google.com"); 348 int status = p.waitFor(); 349 if (status == 0) { 350 mPingHostnameResult = "Pass"; 351 } else { 352 mPingHostnameResult = "Fail: Host unreachable"; 353 } 354 } catch (UnknownHostException e) { 355 mPingHostnameResult = "Fail: Unknown Host"; 356 } catch (IOException e) { 357 mPingHostnameResult= "Fail: IOException"; 358 } catch (InterruptedException e) { 359 mPingHostnameResult = "Fail: InterruptedException"; 360 } 361 } 362 httpClientTest()363 private void httpClientTest() { 364 HttpURLConnection urlConnection = null; 365 try { 366 // TODO: Hardcoded for now, make it UI configurable 367 URL url = new URL("https://www.google.com"); 368 urlConnection = (HttpURLConnection) url.openConnection(); 369 if (urlConnection.getResponseCode() == 200) { 370 mHttpClientTestResult = "Pass"; 371 } else { 372 mHttpClientTestResult = "Fail: Code: " + urlConnection.getResponseMessage(); 373 } 374 } catch (IOException e) { 375 mHttpClientTestResult = "Fail: IOException"; 376 } finally { 377 if (urlConnection != null) { 378 urlConnection.disconnect(); 379 } 380 } 381 } 382 383 } 384