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