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 /** 50 * Show the current status details of Wifi related fields 51 */ 52 public class WifiStatusTest extends Activity { 53 54 private static final String TAG = "WifiStatusTest"; 55 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; 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 130 setContentView(R.layout.wifi_status_test); 131 132 updateButton = (Button) findViewById(R.id.update); 133 updateButton.setOnClickListener(updateButtonHandler); 134 135 mWifiState = (TextView) findViewById(R.id.wifi_state); 136 mNetworkState = (TextView) findViewById(R.id.network_state); 137 mSupplicantState = (TextView) findViewById(R.id.supplicant_state); 138 mRSSI = (TextView) findViewById(R.id.rssi); 139 mBSSID = (TextView) findViewById(R.id.bssid); 140 mSSID = (TextView) findViewById(R.id.ssid); 141 mHiddenSSID = (TextView) findViewById(R.id.hidden_ssid); 142 mIPAddr = (TextView) findViewById(R.id.ipaddr); 143 mMACAddr = (TextView) findViewById(R.id.macaddr); 144 mNetworkId = (TextView) findViewById(R.id.networkid); 145 mLinkSpeed = (TextView) findViewById(R.id.link_speed); 146 mScanList = (TextView) findViewById(R.id.scan_list); 147 148 149 mPingHostname = (TextView) findViewById(R.id.pingHostname); 150 mHttpClientTest = (TextView) findViewById(R.id.httpClientTest); 151 152 pingTestButton = (Button) findViewById(R.id.ping_test); 153 pingTestButton.setOnClickListener(mPingButtonHandler); 154 } 155 156 @Override onResume()157 protected void onResume() { 158 super.onResume(); 159 registerReceiver(mWifiStateReceiver, mWifiStateFilter); 160 } 161 162 @Override onPause()163 protected void onPause() { 164 super.onPause(); 165 unregisterReceiver(mWifiStateReceiver); 166 } 167 168 OnClickListener mPingButtonHandler = new OnClickListener() { 169 public void onClick(View v) { 170 updatePingState(); 171 } 172 }; 173 174 OnClickListener updateButtonHandler = new OnClickListener() { 175 public void onClick(View v) { 176 final WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); 177 178 setWifiStateText(mWifiManager.getWifiState()); 179 mBSSID.setText(wifiInfo.getBSSID()); 180 mHiddenSSID.setText(String.valueOf(wifiInfo.getHiddenSSID())); 181 int ipAddr = wifiInfo.getIpAddress(); 182 StringBuffer ipBuf = new StringBuffer(); 183 ipBuf.append(ipAddr & 0xff).append('.'). 184 append((ipAddr >>>= 8) & 0xff).append('.'). 185 append((ipAddr >>>= 8) & 0xff).append('.'). 186 append((ipAddr >>>= 8) & 0xff); 187 188 mIPAddr.setText(ipBuf); 189 mLinkSpeed.setText(String.valueOf(wifiInfo.getLinkSpeed())+" Mbps"); 190 mMACAddr.setText(wifiInfo.getMacAddress()); 191 mNetworkId.setText(String.valueOf(wifiInfo.getNetworkId())); 192 mRSSI.setText(String.valueOf(wifiInfo.getRssi())); 193 mSSID.setText(wifiInfo.getSSID()); 194 195 SupplicantState supplicantState = wifiInfo.getSupplicantState(); 196 setSupplicantStateText(supplicantState); 197 } 198 }; 199 setSupplicantStateText(SupplicantState supplicantState)200 private void setSupplicantStateText(SupplicantState supplicantState) { 201 if(SupplicantState.FOUR_WAY_HANDSHAKE.equals(supplicantState)) { 202 mSupplicantState.setText("FOUR WAY HANDSHAKE"); 203 } else if(SupplicantState.ASSOCIATED.equals(supplicantState)) { 204 mSupplicantState.setText("ASSOCIATED"); 205 } else if(SupplicantState.ASSOCIATING.equals(supplicantState)) { 206 mSupplicantState.setText("ASSOCIATING"); 207 } else if(SupplicantState.COMPLETED.equals(supplicantState)) { 208 mSupplicantState.setText("COMPLETED"); 209 } else if(SupplicantState.DISCONNECTED.equals(supplicantState)) { 210 mSupplicantState.setText("DISCONNECTED"); 211 } else if(SupplicantState.DORMANT.equals(supplicantState)) { 212 mSupplicantState.setText("DORMANT"); 213 } else if(SupplicantState.GROUP_HANDSHAKE.equals(supplicantState)) { 214 mSupplicantState.setText("GROUP HANDSHAKE"); 215 } else if(SupplicantState.INACTIVE.equals(supplicantState)) { 216 mSupplicantState.setText("INACTIVE"); 217 } else if(SupplicantState.INVALID.equals(supplicantState)) { 218 mSupplicantState.setText("INVALID"); 219 } else if(SupplicantState.SCANNING.equals(supplicantState)) { 220 mSupplicantState.setText("SCANNING"); 221 } else if(SupplicantState.UNINITIALIZED.equals(supplicantState)) { 222 mSupplicantState.setText("UNINITIALIZED"); 223 } else { 224 mSupplicantState.setText("BAD"); 225 Log.e(TAG, "supplicant state is bad"); 226 } 227 } 228 setWifiStateText(int wifiState)229 private void setWifiStateText(int wifiState) { 230 String wifiStateString; 231 switch(wifiState) { 232 case WifiManager.WIFI_STATE_DISABLING: 233 wifiStateString = getString(R.string.wifi_state_disabling); 234 break; 235 case WifiManager.WIFI_STATE_DISABLED: 236 wifiStateString = getString(R.string.wifi_state_disabled); 237 break; 238 case WifiManager.WIFI_STATE_ENABLING: 239 wifiStateString = getString(R.string.wifi_state_enabling); 240 break; 241 case WifiManager.WIFI_STATE_ENABLED: 242 wifiStateString = getString(R.string.wifi_state_enabled); 243 break; 244 case WifiManager.WIFI_STATE_UNKNOWN: 245 wifiStateString = getString(R.string.wifi_state_unknown); 246 break; 247 default: 248 wifiStateString = "BAD"; 249 Log.e(TAG, "wifi state is bad"); 250 break; 251 } 252 253 mWifiState.setText(wifiStateString); 254 } 255 handleSignalChanged(int rssi)256 private void handleSignalChanged(int rssi) { 257 mRSSI.setText(String.valueOf(rssi)); 258 } 259 handleWifiStateChanged(int wifiState)260 private void handleWifiStateChanged(int wifiState) { 261 setWifiStateText(wifiState); 262 } 263 handleScanResultsAvailable()264 private void handleScanResultsAvailable() { 265 List<ScanResult> list = mWifiManager.getScanResults(); 266 267 StringBuffer scanList = new StringBuffer(); 268 if (list != null) { 269 for (int i = list.size() - 1; i >= 0; i--) { 270 final ScanResult scanResult = list.get(i); 271 272 if (scanResult == null) { 273 continue; 274 } 275 276 if (TextUtils.isEmpty(scanResult.SSID)) { 277 continue; 278 } 279 280 scanList.append(scanResult.SSID+" "); 281 } 282 } 283 mScanList.setText(scanList); 284 } 285 handleSupplicantStateChanged(SupplicantState state, boolean hasError, int error)286 private void handleSupplicantStateChanged(SupplicantState state, boolean hasError, int error) { 287 if (hasError) { 288 mSupplicantState.setText("ERROR AUTHENTICATING"); 289 } else { 290 setSupplicantStateText(state); 291 } 292 } 293 handleNetworkStateChanged(NetworkInfo networkInfo)294 private void handleNetworkStateChanged(NetworkInfo networkInfo) { 295 if (mWifiManager.isWifiEnabled()) { 296 WifiInfo info = mWifiManager.getConnectionInfo(); 297 String summary = AccessPoint.getSummary(this, info.getSSID(), 298 networkInfo.getDetailedState(), 299 info.getNetworkId() == WifiConfiguration.INVALID_NETWORK_ID, null); 300 mNetworkState.setText(summary); 301 } 302 } 303 updatePingState()304 private final void updatePingState() { 305 final Handler handler = new Handler(); 306 // Set all to unknown since the threads will take a few secs to update. 307 mPingHostnameResult = getResources().getString(R.string.radioInfo_unknown); 308 mHttpClientTestResult = getResources().getString(R.string.radioInfo_unknown); 309 310 mPingHostname.setText(mPingHostnameResult); 311 mHttpClientTest.setText(mHttpClientTestResult); 312 313 final Runnable updatePingResults = new Runnable() { 314 public void run() { 315 mPingHostname.setText(mPingHostnameResult); 316 mHttpClientTest.setText(mHttpClientTestResult); 317 } 318 }; 319 320 Thread hostnameThread = new Thread() { 321 @Override 322 public void run() { 323 pingHostname(); 324 handler.post(updatePingResults); 325 } 326 }; 327 hostnameThread.start(); 328 329 Thread httpClientThread = new Thread() { 330 @Override 331 public void run() { 332 httpClientTest(); 333 handler.post(updatePingResults); 334 } 335 }; 336 httpClientThread.start(); 337 } 338 pingHostname()339 private final void pingHostname() { 340 try { 341 // TODO: Hardcoded for now, make it UI configurable 342 Process p = Runtime.getRuntime().exec("ping -c 1 -w 100 www.google.com"); 343 int status = p.waitFor(); 344 if (status == 0) { 345 mPingHostnameResult = "Pass"; 346 } else { 347 mPingHostnameResult = "Fail: Host unreachable"; 348 } 349 } catch (UnknownHostException e) { 350 mPingHostnameResult = "Fail: Unknown Host"; 351 } catch (IOException e) { 352 mPingHostnameResult= "Fail: IOException"; 353 } catch (InterruptedException e) { 354 mPingHostnameResult = "Fail: InterruptedException"; 355 } 356 } 357 httpClientTest()358 private void httpClientTest() { 359 HttpURLConnection urlConnection = null; 360 try { 361 // TODO: Hardcoded for now, make it UI configurable 362 URL url = new URL("https://www.google.com"); 363 urlConnection = (HttpURLConnection) url.openConnection(); 364 if (urlConnection.getResponseCode() == 200) { 365 mHttpClientTestResult = "Pass"; 366 } else { 367 mHttpClientTestResult = "Fail: Code: " + urlConnection.getResponseMessage(); 368 } 369 } catch (IOException e) { 370 mHttpClientTestResult = "Fail: IOException"; 371 } finally { 372 if (urlConnection != null) { 373 urlConnection.disconnect(); 374 } 375 } 376 } 377 378 } 379