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 
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         mTxLinkSpeed = (TextView) findViewById(R.id.tx_link_speed);
146         mRxLinkSpeed = (TextView) findViewById(R.id.rx_link_speed);
147         mScanList = (TextView) findViewById(R.id.scan_list);
148 
149 
150         mPingHostname = (TextView) findViewById(R.id.pingHostname);
151         mHttpClientTest = (TextView) findViewById(R.id.httpClientTest);
152 
153         pingTestButton = (Button) findViewById(R.id.ping_test);
154         pingTestButton.setOnClickListener(mPingButtonHandler);
155     }
156 
157     @Override
onResume()158     protected void onResume() {
159         super.onResume();
160         registerReceiver(mWifiStateReceiver, mWifiStateFilter);
161     }
162 
163     @Override
onPause()164     protected void onPause() {
165         super.onPause();
166         unregisterReceiver(mWifiStateReceiver);
167     }
168 
169     OnClickListener mPingButtonHandler = new OnClickListener() {
170         public void onClick(View v) {
171             updatePingState();
172         }
173     };
174 
175     OnClickListener updateButtonHandler = new OnClickListener() {
176         public void onClick(View v) {
177             final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
178 
179             setWifiStateText(mWifiManager.getWifiState());
180             mBSSID.setText(wifiInfo.getBSSID());
181             mHiddenSSID.setText(String.valueOf(wifiInfo.getHiddenSSID()));
182             int ipAddr = wifiInfo.getIpAddress();
183             StringBuffer ipBuf = new StringBuffer();
184             ipBuf.append(ipAddr  & 0xff).append('.').
185                 append((ipAddr >>>= 8) & 0xff).append('.').
186                 append((ipAddr >>>= 8) & 0xff).append('.').
187                 append((ipAddr >>>= 8) & 0xff);
188 
189             mIPAddr.setText(ipBuf);
190             mTxLinkSpeed.setText(String.valueOf(wifiInfo.getTxLinkSpeedMbps())+" Mbps");
191             mRxLinkSpeed.setText(String.valueOf(wifiInfo.getRxLinkSpeedMbps())+" 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());
196 
197             SupplicantState supplicantState = wifiInfo.getSupplicantState();
198             setSupplicantStateText(supplicantState);
199         }
200     };
201 
setSupplicantStateText(SupplicantState 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");
225         } else {
226             mSupplicantState.setText("BAD");
227             Log.e(TAG, "supplicant state is bad");
228         }
229     }
230 
setWifiStateText(int wifiState)231     private void setWifiStateText(int wifiState) {
232         String wifiStateString;
233         switch(wifiState) {
234             case WifiManager.WIFI_STATE_DISABLING:
235                 wifiStateString = getString(R.string.wifi_state_disabling);
236                 break;
237             case WifiManager.WIFI_STATE_DISABLED:
238                 wifiStateString = getString(R.string.wifi_state_disabled);
239                 break;
240             case WifiManager.WIFI_STATE_ENABLING:
241                 wifiStateString = getString(R.string.wifi_state_enabling);
242                 break;
243             case WifiManager.WIFI_STATE_ENABLED:
244                 wifiStateString = getString(R.string.wifi_state_enabled);
245                 break;
246             case WifiManager.WIFI_STATE_UNKNOWN:
247                 wifiStateString = getString(R.string.wifi_state_unknown);
248                 break;
249             default:
250                 wifiStateString = "BAD";
251                 Log.e(TAG, "wifi state is bad");
252                 break;
253         }
254 
255         mWifiState.setText(wifiStateString);
256     }
257 
handleSignalChanged(int rssi)258     private void handleSignalChanged(int rssi) {
259         mRSSI.setText(String.valueOf(rssi));
260     }
261 
handleWifiStateChanged(int wifiState)262     private void handleWifiStateChanged(int wifiState) {
263         setWifiStateText(wifiState);
264     }
265 
handleScanResultsAvailable()266     private void handleScanResultsAvailable() {
267         List<ScanResult> list = mWifiManager.getScanResults();
268 
269         StringBuffer scanList = new StringBuffer();
270         if (list != null) {
271             for (int i = list.size() - 1; i >= 0; i--) {
272                 final ScanResult scanResult = list.get(i);
273 
274                 if (scanResult == null) {
275                     continue;
276                 }
277 
278                 if (TextUtils.isEmpty(scanResult.SSID)) {
279                     continue;
280                 }
281 
282                 scanList.append(scanResult.SSID+" ");
283             }
284         }
285         mScanList.setText(scanList);
286     }
287 
handleSupplicantStateChanged(SupplicantState state, boolean hasError, int error)288     private void handleSupplicantStateChanged(SupplicantState state, boolean hasError, int error) {
289         if (hasError) {
290             mSupplicantState.setText("ERROR AUTHENTICATING");
291         } else {
292             setSupplicantStateText(state);
293         }
294     }
295 
handleNetworkStateChanged(NetworkInfo networkInfo)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,
302                     /* suggestionOrSpecifierPackageName */ null);
303             mNetworkState.setText(summary);
304         }
305     }
306 
updatePingState()307     private final void updatePingState() {
308         final Handler handler = new Handler();
309         // Set all to unknown since the threads will take a few secs to update.
310         mPingHostnameResult = getResources().getString(R.string.radioInfo_unknown);
311         mHttpClientTestResult = getResources().getString(R.string.radioInfo_unknown);
312 
313         mPingHostname.setText(mPingHostnameResult);
314         mHttpClientTest.setText(mHttpClientTestResult);
315 
316         final Runnable updatePingResults = new Runnable() {
317             public void run() {
318                 mPingHostname.setText(mPingHostnameResult);
319                 mHttpClientTest.setText(mHttpClientTestResult);
320             }
321         };
322 
323         Thread hostnameThread = new Thread() {
324             @Override
325             public void run() {
326                 pingHostname();
327                 handler.post(updatePingResults);
328             }
329         };
330         hostnameThread.start();
331 
332         Thread httpClientThread = new Thread() {
333             @Override
334             public void run() {
335                 httpClientTest();
336                 handler.post(updatePingResults);
337             }
338         };
339         httpClientThread.start();
340     }
341 
pingHostname()342     private final void pingHostname() {
343         try {
344             // TODO: Hardcoded for now, make it UI configurable
345             Process p = Runtime.getRuntime().exec("ping -c 1 -w 100 www.google.com");
346             int status = p.waitFor();
347             if (status == 0) {
348                 mPingHostnameResult = "Pass";
349             } else {
350                 mPingHostnameResult = "Fail: Host unreachable";
351             }
352         } catch (UnknownHostException e) {
353             mPingHostnameResult = "Fail: Unknown Host";
354         } catch (IOException e) {
355             mPingHostnameResult= "Fail: IOException";
356         } catch (InterruptedException e) {
357             mPingHostnameResult = "Fail: InterruptedException";
358         }
359     }
360 
httpClientTest()361     private void httpClientTest() {
362         HttpURLConnection urlConnection = null;
363         try {
364             // TODO: Hardcoded for now, make it UI configurable
365             URL url = new URL("https://www.google.com");
366             urlConnection = (HttpURLConnection) url.openConnection();
367             if (urlConnection.getResponseCode() == 200) {
368                 mHttpClientTestResult = "Pass";
369             } else {
370                 mHttpClientTestResult = "Fail: Code: " + urlConnection.getResponseMessage();
371             }
372         } catch (IOException e) {
373             mHttpClientTestResult = "Fail: IOException";
374         } finally {
375             if (urlConnection != null) {
376                 urlConnection.disconnect();
377             }
378         }
379     }
380 
381 }
382